Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/iframe-coordinator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "6.5.0",
"description": "Tools for coordinating embedded apps via iframes.",
"dependencies": {
"decoders": "1.15.0"
"decoders": "^2.8.0"
},
"type": "module",
"files": [
Expand Down
28 changes: 13 additions & 15 deletions packages/iframe-coordinator/src/messages/ClientToHost.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dispatch, guard } from "decoders";
import { taggedUnion } from "decoders";
import { decoder as clickDecoder, LabeledClick } from "./Click";
import { decoder as keyDownDecoder, LabeledKeyDown } from "./KeyDown";
import { LabeledStarted, startedDecoder } from "./Lifecycle";
Expand Down Expand Up @@ -36,18 +36,16 @@ export type ClientToHost =
* @param msg The message requiring validation.
*/
export function validate(msg: any): ClientToHost {
return guard(
dispatch("msgType", {
publish: publicationDecoder,
registeredKeyFired: keyDownDecoder,
client_started: startedDecoder,
clickFired: clickDecoder,
navRequest: navRequestDecoder,
notifyRequest: notifyDecoder,
toastRequest: notifyDecoder,
modalRequest: modalDecoder,
pageMetadata: pageMetadataDecoder,
promptOnLeave: promptDecoder,
}),
)(msg);
return taggedUnion("msgType", {
publish: publicationDecoder,
registeredKeyFired: keyDownDecoder,
client_started: startedDecoder,
clickFired: clickDecoder,
navRequest: navRequestDecoder,
notifyRequest: notifyDecoder,
toastRequest: notifyDecoder,
modalRequest: modalDecoder,
pageMetadata: pageMetadataDecoder,
promptOnLeave: promptDecoder,
}).verify(msg);
}
9 changes: 5 additions & 4 deletions packages/iframe-coordinator/src/messages/HostToClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dispatch, guard } from "decoders";
import { taggedUnion } from "decoders";
import { envDecoder, LabeledEnvInit } from "./Lifecycle";
import {
decoder as publicationDecoder,
Expand All @@ -17,7 +17,8 @@ export type HostToClient = LabeledPublication | LabeledEnvInit;
* @param msg The message requiring validation.
*/
export function validate(msg: any): HostToClient {
return guard(
dispatch("msgType", { publish: publicationDecoder, env_init: envDecoder }),
)(msg);
return taggedUnion("msgType", {
publish: publicationDecoder,
env_init: envDecoder,
}).verify(msg);
}
28 changes: 10 additions & 18 deletions packages/iframe-coordinator/src/messages/LabeledMsg.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
constant,
Decoder,
either,
hardcoded,
map,
object,
optional,
string,
} from "decoders";
import { constant, Decoder, either, exact, optional, string } from "decoders";

// This is replaced by rollup-plugin-replace with the actual version from package.json
const version = "__PACKAGE_VERSION__";
Expand Down Expand Up @@ -87,20 +78,21 @@ export function labeledDecoder<T, V>(
typeDecoder: Decoder<T>,
msgDecoder: Decoder<V>,
): Decoder<LabeledMsg<T, V>> {
return object({
return exact({
// TODO: in 4.0.0 make protocol and version fields mandatory
protocol: either(
constant<"iframe-coordinator">(API_PROTOCOL),
hardcoded<"iframe-coordinator">(API_PROTOCOL),
),
version: either(string, hardcoded("unknown")),
protocol: optional(constant<"iframe-coordinator">(API_PROTOCOL)),
version: optional(string),
msgType: typeDecoder,
msg: msgDecoder,
direction: optional(directionDecoder),
});
}).transform((decoded) => ({
...decoded,
protocol: decoded.protocol ?? API_PROTOCOL,
version: decoded.version ?? "unknown",
})) as Decoder<LabeledMsg<T, V>>;
}

const directionDecoder: Decoder<MessageDirection, unknown> = either(
const directionDecoder: Decoder<MessageDirection> = either(
constant("ClientToHost"),
constant("HostToClient"),
);
12 changes: 6 additions & 6 deletions packages/iframe-coordinator/src/messages/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {
boolean,
constant,
Decoder,
mixed,
object,
exact,
optional,
string,
unknown,
} from "decoders";
import { applyClientProtocol, labeledDecoder, LabeledMsg } from "./LabeledMsg";

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

/**
Expand Down Expand Up @@ -78,13 +78,13 @@ export interface LabeledEnvInit extends LabeledMsg<"env_init", SetupData> {

const envDecoder: Decoder<LabeledEnvInit> = labeledDecoder(
constant<"env_init">("env_init"),
object({
exact({
locale: string,
hostRootUrl: string,
assignedRoute: string,
registeredKeys: optional(
array(
object({
exact({
key: string,
altKey: optional(boolean),
ctrlKey: optional(boolean),
Expand All @@ -93,7 +93,7 @@ const envDecoder: Decoder<LabeledEnvInit> = labeledDecoder(
}),
),
),
custom: mixed,
custom: unknown,
}),
);

Expand Down
8 changes: 4 additions & 4 deletions packages/iframe-coordinator/src/messages/ModalRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { constant, Decoder, mixed, object, string } from "decoders";
import { constant, Decoder, exact, string, unknown } from "decoders";
import { labeledDecoder, LabeledMsg } from "./LabeledMsg";

/**
Expand All @@ -9,7 +9,7 @@ export interface ModalRequest {
modalId: string;

/** Any data that the client wishes to send to the modal */
modalData: any;
modalData?: any;
}

/**
Expand All @@ -28,9 +28,9 @@ export interface LabeledModalRequest extends LabeledMsg<

const decoder: Decoder<LabeledModalRequest> = labeledDecoder(
constant<"modalRequest">("modalRequest"),
object({
exact({
modalId: string,
modalData: mixed,
modalData: unknown,
}),
);

Expand Down
19 changes: 8 additions & 11 deletions packages/iframe-coordinator/src/messages/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import {
constant,
Decoder,
either,
map,
mixed,
object,
exact,
optional,
string,
unknown,
} from "decoders";
import { labeledDecoder, LabeledMsg } from "./LabeledMsg";

Expand All @@ -20,7 +19,7 @@ export interface Notification {
/** The notification message */
message: string;
/** Additional host-specific options such as severity */
custom: any;
custom?: any;
}

/**
Expand All @@ -40,21 +39,19 @@ export interface LabeledNotification extends LabeledMsg<
/**
* Helper function to convert old message types to the new type
*/
function alwaysMsgType(msgType: "string"): "notifyRequest" {
function alwaysMsgType(msgType: "toastRequest"): "notifyRequest" {
return "notifyRequest";
}

const toastTypeDecoder: Decoder<"notifyRequest"> = map(
constant<"toastRequest">("toastRequest"),
alwaysMsgType,
);
const toastTypeDecoder: Decoder<"notifyRequest"> =
constant<"toastRequest">("toastRequest").transform(alwaysMsgType);

const decoder: Decoder<LabeledNotification> = labeledDecoder(
either(constant<"notifyRequest">("notifyRequest"), toastTypeDecoder),
object({
exact({
title: optional(string),
message: string,
custom: mixed,
custom: unknown,
}),
);

Expand Down
10 changes: 5 additions & 5 deletions packages/iframe-coordinator/src/messages/PageMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {
array,
constant,
Decoder,
mixed,
object,
exact,
optional,
string,
unknown,
} from "decoders";
import { labeledDecoder, LabeledMsg } from "./LabeledMsg";

Expand Down Expand Up @@ -47,15 +47,15 @@ export interface LabeledPageMetadata extends LabeledMsg<

const decoder: Decoder<LabeledPageMetadata> = labeledDecoder(
constant<"pageMetadata">("pageMetadata"),
object({
exact({
title: string,
breadcrumbs: array(
object({
exact({
text: string,
href: string,
}),
),
custom: optional(mixed),
custom: optional(unknown),
}),
);

Expand Down
16 changes: 4 additions & 12 deletions packages/iframe-coordinator/src/messages/Publication.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
constant,
Decoder,
map,
mixed,
object,
optional,
string,
} from "decoders";
import { constant, Decoder, exact, optional, string, unknown } from "decoders";
import { labeledDecoder, LabeledMsg } from "./LabeledMsg";

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

const decoder: Decoder<LabeledPublication> = labeledDecoder(
constant<"publish">("publish"),
object({
exact({
topic: string,
payload: mixed,
payload: unknown,
clientId: optional(string),
}),
);
Expand Down
Loading