@@ -16,34 +16,47 @@ type InitParam<Init> = RequiredKeysOf<Init> extends never
16
16
? [ ( Init & { [ key : string ] : unknown } ) ?]
17
17
: [ Init & { [ key : string ] : unknown } ] ;
18
18
19
- type PickQueryParam <
19
+ type ParamsOf <
20
20
P extends keyof paths ,
21
21
M extends HttpMethod ,
22
- // @ts -ignore: dyncamically inferred on usage
23
- K extends keyof NonNullable < paths [ P ] [ M ] [ "parameters" ] [ "query" ] > ,
24
- // @ts -ignore: dyncamically inferred on usage
25
- > = Pick < NonNullable < paths [ P ] [ M ] [ "parameters" ] [ "query" ] > , K > ;
22
+ W extends "query" | "path" | "body" ,
23
+ > = W extends "query"
24
+ ? // @ts -expect-error: dyncamically inferred on usage
25
+ NonNullable < paths [ P ] [ M ] [ "parameters" ] [ "query" ] >
26
+ : W extends "path"
27
+ ? // @ts -expect-error: dyncamically inferred on usage
28
+ NonNullable < paths [ P ] [ M ] [ "parameters" ] [ "path" ] >
29
+ : // @ts -expect-error: dyncamically inferred on usage
30
+ NonNullable < paths [ P ] [ M ] [ "requestBody" ] [ "content" ] [ "application/json" ] > ;
26
31
27
- type PickPathParam <
28
- P extends keyof paths ,
29
- M extends HttpMethod ,
30
- // @ts -ignore: dyncamically inferred on usage
31
- K extends keyof NonNullable < paths [ P ] [ M ] [ "parameters" ] [ "path " ] > ,
32
- // @ts -ignore: dyncamically inferred on usage
33
- > = Pick < NonNullable < paths [ P ] [ M ] [ "parameters" ] [ "path " ] > , K > ;
32
+ // type PickQueryParam <
33
+ // P extends keyof paths,
34
+ // M extends HttpMethod,
35
+ // // @ts -ignore: dyncamically inferred on usage
36
+ // K extends keyof NonNullable<paths[P][M]["parameters"]["query "]>,
37
+ // // @ts -ignore: dyncamically inferred on usage
38
+ // > = Pick<NonNullable<paths[P][M]["parameters"]["query "]>, K>;
34
39
35
- type PickBodyParam <
36
- P extends keyof paths ,
37
- M extends HttpMethod ,
38
- K extends keyof NonNullable <
39
- // @ts -ignore: dyncamically inferred on usage
40
- paths [ P ] [ M ] [ "requestBody" ] [ "content" ] [ "application/json" ]
41
- > ,
42
- > = Pick <
43
- // @ts -ignore: dyncamically inferred on usage
44
- NonNullable < paths [ P ] [ M ] [ "requestBody" ] [ "content" ] [ "application/json" ] > ,
45
- K
46
- > ;
40
+ // type PickPathParam<
41
+ // P extends keyof paths,
42
+ // M extends HttpMethod,
43
+ // // @ts -ignore: dyncamically inferred on usage
44
+ // K extends keyof NonNullable<paths[P][M]["parameters"]["path"]>,
45
+ // // @ts -ignore: dyncamically inferred on usage
46
+ // > = Pick<NonNullable<paths[P][M]["parameters"]["path"]>, K>;
47
+
48
+ // type PickBodyParam<
49
+ // P extends keyof paths,
50
+ // M extends HttpMethod,
51
+ // K extends keyof NonNullable<
52
+ // // @ts -ignore: dyncamically inferred on usage
53
+ // paths[P][M]["requestBody"]["content"]["application/json"]
54
+ // >,
55
+ // > = Pick<
56
+ // // @ts -ignore: dyncamically inferred on usage
57
+ // NonNullable<paths[P][M]["requestBody"]["content"]["application/json"]>,
58
+ // K
59
+ // >;
47
60
48
61
export function createDockerApi ( ) {
49
62
const client = createClient < paths > ( {
@@ -118,10 +131,8 @@ export function createDockerApi() {
118
131
119
132
return {
120
133
containers : { } ,
121
- network : ( nameOrId : PickPathParam < "/networks/{id}" , "get" , "id" > ) => ( {
122
- inspect : async (
123
- params ?: PickQueryParam < "/networks/{id}" , "get" , "verbose" | "scope" > ,
124
- ) => {
134
+ network : ( nameOrId : ParamsOf < "/networks/{id}" , "get" , "path" > ) => ( {
135
+ inspect : async ( params ?: InspectNetworkParams ) => {
125
136
const network = await GET ( "/networks/{id}" , {
126
137
params : {
127
138
path : { id : nameOrId . id } ,
@@ -140,13 +151,98 @@ export function createDockerApi() {
140
151
} ) ,
141
152
} ) ,
142
153
networks : {
143
- list : ( ) => GET ( "/networks" ) ,
144
- create : ( body : PickBodyParam < "/networks/create" , "post" , "Name" > ) =>
145
- POST ( "/networks/create" , { body : { Name : body . Name } } ) ,
154
+ // TODO(manuel): Implement filters query param
155
+ list : ( params ?: ListNetworksParams ) =>
156
+ GET ( "/networks" , {
157
+ params : { query : { filters : params ?. filters } } ,
158
+ } ) ,
159
+ create : ( params : NetworkCreateParams ) =>
160
+ POST ( "/networks/create" , {
161
+ body : {
162
+ Name : params . Name ,
163
+ Driver : params . Driver ,
164
+ Labels : params . Labels ,
165
+ EnableIPv4 : params . EnableIPv4 ,
166
+ EnableIPv6 : params . EnableIPv6 ,
167
+ } ,
168
+ } ) ,
169
+ // TODO(manuel): Implement filters query param
170
+ prune : ( ) => POST ( "/networks/prune" ) ,
146
171
} ,
147
172
} ;
148
173
}
149
174
175
+ interface InspectNetworkParams
176
+ extends ParamsOf < "/networks/{id}" , "get" , "query" > { }
177
+
178
+ interface ListNetworksParams extends ParamsOf < "/networks" , "get" , "query" > { }
179
+
180
+ interface NetworkCreateParams
181
+ extends ParamsOf < "/networks/create" , "post" , "body" > { }
182
+
183
+ const client = createDockerApi ( ) ;
184
+
185
+ // const network = await client.network({ id: "host" }).inspect({ verbose: true });
186
+ // const networks = await client.networks.list();
187
+ const network = await client . networks . create ( {
188
+ Name : "test" ,
189
+ Driver : "bridge" ,
190
+ Labels : { } ,
191
+ EnableIPv4 : true ,
192
+ EnableIPv6 : true ,
193
+ } ) ;
194
+ console . log ( network ) ;
195
+
196
+ // const createPicker =
197
+ // <
198
+ // P extends keyof paths,
199
+ // M extends HttpMethod,
200
+ // W extends "query" | "path" | "body",
201
+ // K extends (W extends "query"
202
+ // ? // @ts-expect-error: dyncamically inferred on usage
203
+ // keyof NonNullable<paths[P][M]["parameters"]["query"]>
204
+ // : W extends "path"
205
+ // ? // @ts-expect-error: dyncamically inferred on usage
206
+ // keyof NonNullable<paths[P][M]["parameters"]["path"]>
207
+ // : keyof NonNullable<
208
+ // // @ts -ignore: dyncamically inferred on usage
209
+ // paths[P][M]["requestBody"]["content"]["application/json"]
210
+ // >)[],
211
+ // >(
212
+ // path: P,
213
+ // method: M,
214
+ // type: W,
215
+ // keys: K,
216
+ // ) =>
217
+ // (
218
+ // params: W extends "query"
219
+ // ? // @ts-expect-error: dyncamically inferred on usage
220
+ // NonNullable<paths[P][M]["parameters"]["query"]>
221
+ // : W extends "path"
222
+ // ? // @ts-expect-error: dyncamically inferred on usage
223
+ // NonNullable<paths[P][M]["parameters"]["path"]>
224
+ // : NonNullable<
225
+ // // @ts -ignore: dyncamically inferred on usage
226
+ // paths[P][M]["requestBody"]["content"]["application/json"]
227
+ // >,
228
+ // // @ts -ignore: dyncamically inferred on usage
229
+ // ): Pick<typeof params, K[number]> =>
230
+ // // @ts -ignore: dyncamically inferred on usage
231
+ // Object.fromEntries(
232
+ // Object.entries(params).filter(([key]) => keys.includes(key as K[number])),
233
+ // );
234
+
235
+ // type ListNetworksParams = PickQueryParam<"/networks", "get", "filters">;
236
+
237
+ // const pickNetworkCreateParams = createPicker("/networks/create", "post", [
238
+ // "Name",
239
+ // "Driver",
240
+ // "Labels",
241
+ // "EnableIPv4",
242
+ // "EnableIPv6",
243
+ // ] as const);
244
+ // type NetworkCreateParams = ReturnType<typeof pickNetworkCreateParams>;
245
+
150
246
// class DockerHttpClient {
151
247
// constructor(private props: DockerHttpClientProps) {}
152
248
0 commit comments