Skip to content

Commit 22add16

Browse files
committed
transform to normalize "property"
1 parent 9cbe4ad commit 22add16

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

dashi/src/lib/api.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,42 @@ import {
44
type Contributions,
55
} from "@/lib/types/model/extension";
66
import {
7+
type Callback,
78
type CallbackRequest,
89
type StateChangeRequest,
910
} from "@/lib/types/model/callback";
1011
import { callApi } from "@/lib/utils/fetchApiResult";
12+
import type { Input, Output } from "@/lib/types/model/channel";
13+
import { type ObjPath, toObjPath } from "@/lib/utils/objPath";
14+
import { mapObject } from "@/lib/utils/mapObject";
15+
import type { Contribution } from "@/lib/types/model/contribution";
1116

1217
const defaultServerUrl = "http://localhost:8888";
1318
const defaultEndpointName = "dashi";
1419

20+
interface WithStringProperty {
21+
property?: string;
22+
}
23+
24+
interface RawInput extends Omit<Input, "property">, WithStringProperty {}
25+
26+
interface RawOutput extends Omit<Output, "property">, WithStringProperty {}
27+
28+
interface RawCallback extends Omit<Omit<Callback, "inputs">, "outputs"> {
29+
inputs?: RawInput[];
30+
outputs?: RawOutput[];
31+
}
32+
33+
interface RawContribution
34+
extends Omit<Omit<Contribution, "layout">, "callbacks"> {
35+
layout?: RawCallback;
36+
callbacks: RawCallback[];
37+
}
38+
39+
interface RawContributions extends Omit<Contributions, "contributions"> {
40+
contributions: Record<ContribPoint, RawContribution[]>;
41+
}
42+
1543
export interface ApiOptions {
1644
serverUrl?: string;
1745
endpointName?: string;
@@ -20,7 +48,53 @@ export interface ApiOptions {
2048
export async function fetchContributions(
2149
options?: ApiOptions,
2250
): Promise<Contributions> {
23-
return callApi(makeUrl("contributions", options));
51+
return callApi<RawContributions, Contributions>(
52+
makeUrl("contributions", options),
53+
undefined,
54+
(contributions: RawContributions) => ({
55+
...contributions,
56+
contributions: mapObject(
57+
contributions.contributions,
58+
(contributions: RawContribution[]) =>
59+
contributions.map(
60+
(contribution): Contribution => ({
61+
...contribution,
62+
layout: contribution.layout
63+
? normalizeCallback(contribution.layout)
64+
: undefined,
65+
callbacks: normalizeCallbacks(contribution.callbacks),
66+
}),
67+
),
68+
),
69+
}),
70+
);
71+
}
72+
73+
function normalizeCallbacks(callbacks: RawCallback[] | undefined): Callback[] {
74+
return callbacks ? callbacks.map(normalizeCallback) : [];
75+
}
76+
77+
function normalizeCallback(callback: RawCallback): Callback {
78+
return {
79+
...callback,
80+
inputs: callback.inputs ? normalizeChannels(callback.inputs) : [],
81+
outputs: callback.outputs ? normalizeChannels(callback.outputs) : [],
82+
};
83+
}
84+
85+
function normalizeChannels<S extends WithStringProperty>(
86+
channels: S[],
87+
): (S & { property: ObjPath })[] {
88+
return channels ? channels.map(normalizeChannel) : [];
89+
}
90+
91+
function normalizeChannel<S extends WithStringProperty>(
92+
channel: S,
93+
): S & { property: ObjPath } {
94+
return {
95+
...channel,
96+
property: toObjPath(channel.property),
97+
};
2498
}
2599

26100
export async function fetchInitialComponentState(

dashi/src/lib/types/model/channel.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ObjPath } from "@/lib/utils/objPath";
2+
13
export type Link = "component" | "container" | "app";
24

35
/**
@@ -15,15 +17,10 @@ export interface Channel {
1517
*/
1618
id?: string;
1719

18-
// TODO: we must allow `property` to be a constant
19-
// expression of the form: name {"." name | index}.
20-
// Then we get the normalized form
21-
// property: string[];
22-
2320
/**
2421
* The property of an object or array index.
2522
*/
26-
property: string;
23+
property: string | ObjPath;
2724
}
2825

2926
export interface Input extends Channel {

0 commit comments

Comments
 (0)