@@ -4,14 +4,42 @@ import {
44 type Contributions ,
55} from "@/lib/types/model/extension" ;
66import {
7+ type Callback ,
78 type CallbackRequest ,
89 type StateChangeRequest ,
910} from "@/lib/types/model/callback" ;
1011import { 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
1217const defaultServerUrl = "http://localhost:8888" ;
1318const 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+
1543export interface ApiOptions {
1644 serverUrl ?: string ;
1745 endpointName ?: string ;
@@ -20,7 +48,53 @@ export interface ApiOptions {
2048export 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
26100export async function fetchInitialComponentState (
0 commit comments