Skip to content

Commit 6968e43

Browse files
chore(dependencies): update decoders to v2
update decoders to v2 COMUI-4514
1 parent 380a100 commit 6968e43

File tree

10 files changed

+63
-99
lines changed

10 files changed

+63
-99
lines changed

package-lock.json

Lines changed: 7 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/iframe-coordinator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "6.5.0",
44
"description": "Tools for coordinating embedded apps via iframes.",
55
"dependencies": {
6-
"decoders": "1.15.0"
6+
"decoders": "^2.8.0"
77
},
88
"type": "module",
99
"files": [

packages/iframe-coordinator/src/messages/ClientToHost.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dispatch, guard } from "decoders";
1+
import { taggedUnion } from "decoders";
22
import { decoder as clickDecoder, LabeledClick } from "./Click";
33
import { decoder as keyDownDecoder, LabeledKeyDown } from "./KeyDown";
44
import { LabeledStarted, startedDecoder } from "./Lifecycle";
@@ -36,18 +36,16 @@ export type ClientToHost =
3636
* @param msg The message requiring validation.
3737
*/
3838
export function validate(msg: any): ClientToHost {
39-
return guard(
40-
dispatch("msgType", {
41-
publish: publicationDecoder,
42-
registeredKeyFired: keyDownDecoder,
43-
client_started: startedDecoder,
44-
clickFired: clickDecoder,
45-
navRequest: navRequestDecoder,
46-
notifyRequest: notifyDecoder,
47-
toastRequest: notifyDecoder,
48-
modalRequest: modalDecoder,
49-
pageMetadata: pageMetadataDecoder,
50-
promptOnLeave: promptDecoder,
51-
}),
52-
)(msg);
39+
return taggedUnion("msgType", {
40+
publish: publicationDecoder,
41+
registeredKeyFired: keyDownDecoder,
42+
client_started: startedDecoder,
43+
clickFired: clickDecoder,
44+
navRequest: navRequestDecoder,
45+
notifyRequest: notifyDecoder,
46+
toastRequest: notifyDecoder,
47+
modalRequest: modalDecoder,
48+
pageMetadata: pageMetadataDecoder,
49+
promptOnLeave: promptDecoder,
50+
}).verify(msg);
5351
}

packages/iframe-coordinator/src/messages/HostToClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dispatch, guard } from "decoders";
1+
import { taggedUnion } from "decoders";
22
import { envDecoder, LabeledEnvInit } from "./Lifecycle";
33
import {
44
decoder as publicationDecoder,
@@ -17,7 +17,8 @@ export type HostToClient = LabeledPublication | LabeledEnvInit;
1717
* @param msg The message requiring validation.
1818
*/
1919
export function validate(msg: any): HostToClient {
20-
return guard(
21-
dispatch("msgType", { publish: publicationDecoder, env_init: envDecoder }),
22-
)(msg);
20+
return taggedUnion("msgType", {
21+
publish: publicationDecoder,
22+
env_init: envDecoder,
23+
}).verify(msg);
2324
}

packages/iframe-coordinator/src/messages/LabeledMsg.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {
2-
constant,
3-
Decoder,
4-
either,
5-
hardcoded,
6-
map,
7-
object,
8-
optional,
9-
string,
10-
} from "decoders";
1+
import { constant, Decoder, either, exact, optional, string } from "decoders";
112

123
// This is replaced by rollup-plugin-replace with the actual version from package.json
134
const version = "__PACKAGE_VERSION__";
@@ -87,20 +78,21 @@ export function labeledDecoder<T, V>(
8778
typeDecoder: Decoder<T>,
8879
msgDecoder: Decoder<V>,
8980
): Decoder<LabeledMsg<T, V>> {
90-
return object({
81+
return exact({
9182
// TODO: in 4.0.0 make protocol and version fields mandatory
92-
protocol: either(
93-
constant<"iframe-coordinator">(API_PROTOCOL),
94-
hardcoded<"iframe-coordinator">(API_PROTOCOL),
95-
),
96-
version: either(string, hardcoded("unknown")),
83+
protocol: optional(constant<"iframe-coordinator">(API_PROTOCOL)),
84+
version: optional(string),
9785
msgType: typeDecoder,
9886
msg: msgDecoder,
9987
direction: optional(directionDecoder),
100-
});
88+
}).transform((decoded) => ({
89+
...decoded,
90+
protocol: decoded.protocol ?? API_PROTOCOL,
91+
version: decoded.version ?? "unknown",
92+
})) as Decoder<LabeledMsg<T, V>>;
10193
}
10294

103-
const directionDecoder: Decoder<MessageDirection, unknown> = either(
95+
const directionDecoder: Decoder<MessageDirection> = either(
10496
constant("ClientToHost"),
10597
constant("HostToClient"),
10698
);

packages/iframe-coordinator/src/messages/Lifecycle.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
boolean,
44
constant,
55
Decoder,
6-
mixed,
7-
object,
6+
exact,
87
optional,
98
string,
9+
unknown,
1010
} from "decoders";
1111
import { applyClientProtocol, labeledDecoder, LabeledMsg } from "./LabeledMsg";
1212

@@ -23,7 +23,7 @@ export interface LabeledStarted extends LabeledMsg<"client_started", any> {
2323
// We don't care what is in msg for Started messages.
2424
const startedDecoder: Decoder<LabeledStarted> = labeledDecoder(
2525
constant<"client_started">("client_started"),
26-
mixed,
26+
unknown,
2727
);
2828

2929
/**
@@ -78,13 +78,13 @@ export interface LabeledEnvInit extends LabeledMsg<"env_init", SetupData> {
7878

7979
const envDecoder: Decoder<LabeledEnvInit> = labeledDecoder(
8080
constant<"env_init">("env_init"),
81-
object({
81+
exact({
8282
locale: string,
8383
hostRootUrl: string,
8484
assignedRoute: string,
8585
registeredKeys: optional(
8686
array(
87-
object({
87+
exact({
8888
key: string,
8989
altKey: optional(boolean),
9090
ctrlKey: optional(boolean),
@@ -93,7 +93,7 @@ const envDecoder: Decoder<LabeledEnvInit> = labeledDecoder(
9393
}),
9494
),
9595
),
96-
custom: mixed,
96+
custom: unknown,
9797
}),
9898
);
9999

packages/iframe-coordinator/src/messages/ModalRequest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { constant, Decoder, mixed, object, string } from "decoders";
1+
import { constant, Decoder, exact, string, unknown } from "decoders";
22
import { labeledDecoder, LabeledMsg } from "./LabeledMsg";
33

44
/**
@@ -9,7 +9,7 @@ export interface ModalRequest {
99
modalId: string;
1010

1111
/** Any data that the client wishes to send to the modal */
12-
modalData: any;
12+
modalData?: any;
1313
}
1414

1515
/**
@@ -28,9 +28,9 @@ export interface LabeledModalRequest extends LabeledMsg<
2828

2929
const decoder: Decoder<LabeledModalRequest> = labeledDecoder(
3030
constant<"modalRequest">("modalRequest"),
31-
object({
31+
exact({
3232
modalId: string,
33-
modalData: mixed,
33+
modalData: unknown,
3434
}),
3535
);
3636

packages/iframe-coordinator/src/messages/Notification.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import {
22
constant,
33
Decoder,
44
either,
5-
map,
6-
mixed,
7-
object,
5+
exact,
86
optional,
97
string,
8+
unknown,
109
} from "decoders";
1110
import { labeledDecoder, LabeledMsg } from "./LabeledMsg";
1211

@@ -20,7 +19,7 @@ export interface Notification {
2019
/** The notification message */
2120
message: string;
2221
/** Additional host-specific options such as severity */
23-
custom: any;
22+
custom?: any;
2423
}
2524

2625
/**
@@ -40,21 +39,19 @@ export interface LabeledNotification extends LabeledMsg<
4039
/**
4140
* Helper function to convert old message types to the new type
4241
*/
43-
function alwaysMsgType(msgType: "string"): "notifyRequest" {
42+
function alwaysMsgType(msgType: "toastRequest"): "notifyRequest" {
4443
return "notifyRequest";
4544
}
4645

47-
const toastTypeDecoder: Decoder<"notifyRequest"> = map(
48-
constant<"toastRequest">("toastRequest"),
49-
alwaysMsgType,
50-
);
46+
const toastTypeDecoder: Decoder<"notifyRequest"> =
47+
constant<"toastRequest">("toastRequest").transform(alwaysMsgType);
5148

5249
const decoder: Decoder<LabeledNotification> = labeledDecoder(
5350
either(constant<"notifyRequest">("notifyRequest"), toastTypeDecoder),
54-
object({
51+
exact({
5552
title: optional(string),
5653
message: string,
57-
custom: mixed,
54+
custom: unknown,
5855
}),
5956
);
6057

packages/iframe-coordinator/src/messages/PageMetadata.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import {
22
array,
33
constant,
44
Decoder,
5-
mixed,
6-
object,
5+
exact,
76
optional,
87
string,
8+
unknown,
99
} from "decoders";
1010
import { labeledDecoder, LabeledMsg } from "./LabeledMsg";
1111

@@ -47,15 +47,15 @@ export interface LabeledPageMetadata extends LabeledMsg<
4747

4848
const decoder: Decoder<LabeledPageMetadata> = labeledDecoder(
4949
constant<"pageMetadata">("pageMetadata"),
50-
object({
50+
exact({
5151
title: string,
5252
breadcrumbs: array(
53-
object({
53+
exact({
5454
text: string,
5555
href: string,
5656
}),
5757
),
58-
custom: optional(mixed),
58+
custom: optional(unknown),
5959
}),
6060
);
6161

packages/iframe-coordinator/src/messages/Publication.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
constant,
3-
Decoder,
4-
map,
5-
mixed,
6-
object,
7-
optional,
8-
string,
9-
} from "decoders";
1+
import { constant, Decoder, exact, optional, string, unknown } from "decoders";
102
import { labeledDecoder, LabeledMsg } from "./LabeledMsg";
113

124
/**
@@ -19,7 +11,7 @@ export interface Publication {
1911
*/
2012
topic: string;
2113
/** Data to publish */
22-
payload: any;
14+
payload?: any;
2315
/**
2416
* Client the message originates from. This should not be provided when
2517
* calling client methods. The value will be ignored and the library
@@ -41,9 +33,9 @@ export interface LabeledPublication extends LabeledMsg<"publish", Publication> {
4133

4234
const decoder: Decoder<LabeledPublication> = labeledDecoder(
4335
constant<"publish">("publish"),
44-
object({
36+
exact({
4537
topic: string,
46-
payload: mixed,
38+
payload: unknown,
4739
clientId: optional(string),
4840
}),
4941
);

0 commit comments

Comments
 (0)