@tanstack/ai-worldlabs 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -2
  3. package/dist/esm/adapters/world.d.ts +38 -0
  4. package/dist/esm/adapters/world.js +314 -0
  5. package/dist/esm/adapters/world.js.map +1 -0
  6. package/dist/esm/byok.d.ts +1 -0
  7. package/dist/esm/byok.js +11 -0
  8. package/dist/esm/byok.js.map +1 -0
  9. package/dist/esm/generated/client/client.gen.d.ts +2 -0
  10. package/dist/esm/generated/client/client.gen.js +174 -0
  11. package/dist/esm/generated/client/client.gen.js.map +1 -0
  12. package/dist/esm/generated/client/index.d.ts +10 -0
  13. package/dist/esm/generated/client/index.js +5 -0
  14. package/dist/esm/generated/client/types.gen.d.ts +120 -0
  15. package/dist/esm/generated/client/utils.gen.d.ts +37 -0
  16. package/dist/esm/generated/client/utils.gen.js +177 -0
  17. package/dist/esm/generated/client/utils.gen.js.map +1 -0
  18. package/dist/esm/generated/client.d.ts +1 -0
  19. package/dist/esm/generated/client.gen.d.ts +12 -0
  20. package/dist/esm/generated/client.gen.js +9 -0
  21. package/dist/esm/generated/client.gen.js.map +1 -0
  22. package/dist/esm/generated/client.js +5 -0
  23. package/dist/esm/generated/core/auth.gen.d.ts +25 -0
  24. package/dist/esm/generated/core/auth.gen.js +12 -0
  25. package/dist/esm/generated/core/auth.gen.js.map +1 -0
  26. package/dist/esm/generated/core/bodySerializer.gen.d.ts +25 -0
  27. package/dist/esm/generated/core/bodySerializer.gen.js +6 -0
  28. package/dist/esm/generated/core/bodySerializer.gen.js.map +1 -0
  29. package/dist/esm/generated/core/params.gen.d.ts +43 -0
  30. package/dist/esm/generated/core/params.gen.js +9 -0
  31. package/dist/esm/generated/core/params.gen.js.map +1 -0
  32. package/dist/esm/generated/core/pathSerializer.gen.d.ts +33 -0
  33. package/dist/esm/generated/core/pathSerializer.gen.js +82 -0
  34. package/dist/esm/generated/core/pathSerializer.gen.js.map +1 -0
  35. package/dist/esm/generated/core/queryKeySerializer.gen.d.ts +18 -0
  36. package/dist/esm/generated/core/serverSentEvents.gen.d.ts +71 -0
  37. package/dist/esm/generated/core/serverSentEvents.gen.js +96 -0
  38. package/dist/esm/generated/core/serverSentEvents.gen.js.map +1 -0
  39. package/dist/esm/generated/core/types.gen.d.ts +83 -0
  40. package/dist/esm/generated/core/utils.gen.d.ts +19 -0
  41. package/dist/esm/generated/core/utils.gen.js +78 -0
  42. package/dist/esm/generated/core/utils.gen.js.map +1 -0
  43. package/dist/esm/generated/index.d.ts +2 -0
  44. package/dist/esm/generated/sdk.gen.d.ts +275 -0
  45. package/dist/esm/generated/sdk.gen.js +79 -0
  46. package/dist/esm/generated/sdk.gen.js.map +1 -0
  47. package/dist/esm/generated/types.gen.d.ts +1610 -0
  48. package/dist/esm/index.d.ts +5 -0
  49. package/dist/esm/index.js +4 -0
  50. package/dist/esm/model-meta.d.ts +63 -0
  51. package/dist/esm/model-meta.js +17 -0
  52. package/dist/esm/model-meta.js.map +1 -0
  53. package/dist/esm/utils/client.d.ts +16 -0
  54. package/dist/esm/utils/client.js +35 -0
  55. package/dist/esm/utils/client.js.map +1 -0
  56. package/package.json +54 -6
  57. package/src/adapters/world.ts +535 -0
  58. package/src/byok.ts +7 -0
  59. package/src/generated/client/client.gen.ts +301 -0
  60. package/src/generated/client/index.ts +27 -0
  61. package/src/generated/client/types.gen.ts +247 -0
  62. package/src/generated/client/utils.gen.ts +332 -0
  63. package/src/generated/client.gen.ts +26 -0
  64. package/src/generated/client.ts +3 -0
  65. package/src/generated/core/auth.gen.ts +48 -0
  66. package/src/generated/core/bodySerializer.gen.ts +96 -0
  67. package/src/generated/core/params.gen.ts +186 -0
  68. package/src/generated/core/pathSerializer.gen.ts +186 -0
  69. package/src/generated/core/queryKeySerializer.gen.ts +139 -0
  70. package/src/generated/core/serverSentEvents.gen.ts +265 -0
  71. package/src/generated/core/types.gen.ts +124 -0
  72. package/src/generated/core/utils.gen.ts +146 -0
  73. package/src/generated/index.ts +113 -0
  74. package/src/generated/sdk.gen.ts +531 -0
  75. package/src/generated/types.gen.ts +1767 -0
  76. package/src/index.ts +20 -0
  77. package/src/model-meta.ts +79 -0
  78. package/src/utils/client.ts +50 -0
@@ -0,0 +1,332 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+
3
+ import { getAuthToken } from '../core/auth.gen'
4
+ import type { QuerySerializerOptions } from '../core/bodySerializer.gen'
5
+ import { jsonBodySerializer } from '../core/bodySerializer.gen'
6
+ import {
7
+ serializeArrayParam,
8
+ serializeObjectParam,
9
+ serializePrimitiveParam,
10
+ } from '../core/pathSerializer.gen'
11
+ import { getUrl } from '../core/utils.gen'
12
+ import type { Client, ClientOptions, Config, RequestOptions } from './types.gen'
13
+
14
+ export const createQuerySerializer = <T = unknown>({
15
+ parameters = {},
16
+ ...args
17
+ }: QuerySerializerOptions = {}): ((queryParams: T) => string) => {
18
+ const querySerializer = (queryParams: T): string => {
19
+ const search: string[] = []
20
+ if (queryParams && typeof queryParams === 'object') {
21
+ for (const name in queryParams) {
22
+ const value = queryParams[name]
23
+
24
+ if (value === undefined || value === null) {
25
+ continue
26
+ }
27
+
28
+ const options = parameters[name] || args
29
+
30
+ if (Array.isArray(value)) {
31
+ const serializedArray = serializeArrayParam({
32
+ allowReserved: options.allowReserved,
33
+ explode: true,
34
+ name,
35
+ style: 'form',
36
+ value,
37
+ ...options.array,
38
+ })
39
+ if (serializedArray) search.push(serializedArray)
40
+ } else if (typeof value === 'object') {
41
+ const serializedObject = serializeObjectParam({
42
+ allowReserved: options.allowReserved,
43
+ explode: true,
44
+ name,
45
+ style: 'deepObject',
46
+ value: value as Record<string, unknown>,
47
+ ...options.object,
48
+ })
49
+ if (serializedObject) search.push(serializedObject)
50
+ } else {
51
+ const serializedPrimitive = serializePrimitiveParam({
52
+ allowReserved: options.allowReserved,
53
+ name,
54
+ value: value as string,
55
+ })
56
+ if (serializedPrimitive) search.push(serializedPrimitive)
57
+ }
58
+ }
59
+ }
60
+ return search.join('&')
61
+ }
62
+ return querySerializer
63
+ }
64
+
65
+ /**
66
+ * Infers parseAs value from provided Content-Type header.
67
+ */
68
+ export const getParseAs = (
69
+ contentType: string | null,
70
+ ): Exclude<Config['parseAs'], 'auto'> => {
71
+ if (!contentType) {
72
+ // If no Content-Type header is provided, the best we can do is return the raw response body,
73
+ // which is effectively the same as the 'stream' option.
74
+ return 'stream'
75
+ }
76
+
77
+ const cleanContent = contentType.split(';')[0]?.trim()
78
+
79
+ if (!cleanContent) {
80
+ return
81
+ }
82
+
83
+ if (
84
+ cleanContent.startsWith('application/json') ||
85
+ cleanContent.endsWith('+json')
86
+ ) {
87
+ return 'json'
88
+ }
89
+
90
+ if (cleanContent === 'multipart/form-data') {
91
+ return 'formData'
92
+ }
93
+
94
+ if (
95
+ ['application/', 'audio/', 'image/', 'video/'].some((type) =>
96
+ cleanContent.startsWith(type),
97
+ )
98
+ ) {
99
+ return 'blob'
100
+ }
101
+
102
+ if (cleanContent.startsWith('text/')) {
103
+ return 'text'
104
+ }
105
+
106
+ return
107
+ }
108
+
109
+ const checkForExistence = (
110
+ options: Pick<RequestOptions, 'auth' | 'query'> & {
111
+ headers: Headers
112
+ },
113
+ name?: string,
114
+ ): boolean => {
115
+ if (!name) {
116
+ return false
117
+ }
118
+ if (
119
+ options.headers.has(name) ||
120
+ options.query?.[name] ||
121
+ options.headers.get('Cookie')?.includes(`${name}=`)
122
+ ) {
123
+ return true
124
+ }
125
+ return false
126
+ }
127
+
128
+ export async function setAuthParams(
129
+ options: Pick<RequestOptions, 'auth' | 'query' | 'security'> & {
130
+ headers: Headers
131
+ },
132
+ ): Promise<void> {
133
+ for (const auth of options.security ?? []) {
134
+ if (checkForExistence(options, auth.name)) {
135
+ continue
136
+ }
137
+
138
+ const token = await getAuthToken(auth, options.auth)
139
+
140
+ if (!token) {
141
+ continue
142
+ }
143
+
144
+ const name = auth.name ?? 'Authorization'
145
+
146
+ switch (auth.in) {
147
+ case 'query':
148
+ if (!options.query) {
149
+ options.query = {}
150
+ }
151
+ options.query[name] = token
152
+ break
153
+ case 'cookie':
154
+ options.headers.append('Cookie', `${name}=${token}`)
155
+ break
156
+ case 'header':
157
+ default:
158
+ options.headers.set(name, token)
159
+ break
160
+ }
161
+ }
162
+ }
163
+
164
+ export const buildUrl: Client['buildUrl'] = (options) =>
165
+ getUrl({
166
+ baseUrl: options.baseUrl as string,
167
+ path: options.path,
168
+ query: options.query,
169
+ querySerializer:
170
+ typeof options.querySerializer === 'function'
171
+ ? options.querySerializer
172
+ : createQuerySerializer(options.querySerializer),
173
+ url: options.url,
174
+ })
175
+
176
+ export const mergeConfigs = (a: Config, b: Config): Config => {
177
+ const config = { ...a, ...b }
178
+ if (config.baseUrl?.endsWith('/')) {
179
+ config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1)
180
+ }
181
+ config.headers = mergeHeaders(a.headers, b.headers)
182
+ return config
183
+ }
184
+
185
+ const headersEntries = (headers: Headers): Array<[string, string]> => {
186
+ const entries: Array<[string, string]> = []
187
+ headers.forEach((value, key) => {
188
+ entries.push([key, value])
189
+ })
190
+ return entries
191
+ }
192
+
193
+ export const mergeHeaders = (
194
+ ...headers: Array<Required<Config>['headers'] | undefined>
195
+ ): Headers => {
196
+ const mergedHeaders = new Headers()
197
+ for (const header of headers) {
198
+ if (!header) {
199
+ continue
200
+ }
201
+
202
+ const iterator =
203
+ header instanceof Headers
204
+ ? headersEntries(header)
205
+ : Object.entries(header)
206
+
207
+ for (const [key, value] of iterator) {
208
+ if (value === null) {
209
+ mergedHeaders.delete(key)
210
+ } else if (Array.isArray(value)) {
211
+ for (const v of value) {
212
+ mergedHeaders.append(key, v as string)
213
+ }
214
+ } else if (value !== undefined) {
215
+ // assume object headers are meant to be JSON stringified, i.e., their
216
+ // content value in OpenAPI specification is 'application/json'
217
+ mergedHeaders.set(
218
+ key,
219
+ typeof value === 'object' ? JSON.stringify(value) : (value as string),
220
+ )
221
+ }
222
+ }
223
+ }
224
+ return mergedHeaders
225
+ }
226
+
227
+ type ErrInterceptor<Err, Res, Req, Options> = (
228
+ error: Err,
229
+ /** response may be undefined due to a network error where no response object is produced */
230
+ response: Res | undefined,
231
+ /** request may be undefined, because error may be from building the request object itself */
232
+ request: Req | undefined,
233
+ options: Options,
234
+ ) => Err | Promise<Err>
235
+
236
+ type ReqInterceptor<Req, Options> = (
237
+ request: Req,
238
+ options: Options,
239
+ ) => Req | Promise<Req>
240
+
241
+ type ResInterceptor<Res, Req, Options> = (
242
+ response: Res,
243
+ request: Req,
244
+ options: Options,
245
+ ) => Res | Promise<Res>
246
+
247
+ class Interceptors<Interceptor> {
248
+ fns: Array<Interceptor | null> = []
249
+
250
+ clear(): void {
251
+ this.fns = []
252
+ }
253
+
254
+ eject(id: number | Interceptor): void {
255
+ const index = this.getInterceptorIndex(id)
256
+ if (this.fns[index]) {
257
+ this.fns[index] = null
258
+ }
259
+ }
260
+
261
+ exists(id: number | Interceptor): boolean {
262
+ const index = this.getInterceptorIndex(id)
263
+ return Boolean(this.fns[index])
264
+ }
265
+
266
+ getInterceptorIndex(id: number | Interceptor): number {
267
+ if (typeof id === 'number') {
268
+ return this.fns[id] ? id : -1
269
+ }
270
+ return this.fns.indexOf(id)
271
+ }
272
+
273
+ update(
274
+ id: number | Interceptor,
275
+ fn: Interceptor,
276
+ ): number | Interceptor | false {
277
+ const index = this.getInterceptorIndex(id)
278
+ if (this.fns[index]) {
279
+ this.fns[index] = fn
280
+ return id
281
+ }
282
+ return false
283
+ }
284
+
285
+ use(fn: Interceptor): number {
286
+ this.fns.push(fn)
287
+ return this.fns.length - 1
288
+ }
289
+ }
290
+
291
+ export interface Middleware<Req, Res, Err, Options> {
292
+ error: Interceptors<ErrInterceptor<Err, Res, Req, Options>>
293
+ request: Interceptors<ReqInterceptor<Req, Options>>
294
+ response: Interceptors<ResInterceptor<Res, Req, Options>>
295
+ }
296
+
297
+ export const createInterceptors = <Req, Res, Err, Options>(): Middleware<
298
+ Req,
299
+ Res,
300
+ Err,
301
+ Options
302
+ > => ({
303
+ error: new Interceptors<ErrInterceptor<Err, Res, Req, Options>>(),
304
+ request: new Interceptors<ReqInterceptor<Req, Options>>(),
305
+ response: new Interceptors<ResInterceptor<Res, Req, Options>>(),
306
+ })
307
+
308
+ const defaultQuerySerializer = createQuerySerializer({
309
+ allowReserved: false,
310
+ array: {
311
+ explode: true,
312
+ style: 'form',
313
+ },
314
+ object: {
315
+ explode: true,
316
+ style: 'deepObject',
317
+ },
318
+ })
319
+
320
+ const defaultHeaders = {
321
+ 'Content-Type': 'application/json',
322
+ }
323
+
324
+ export const createConfig = <T extends ClientOptions = ClientOptions>(
325
+ override: Config<Omit<ClientOptions, keyof T> & T> = {},
326
+ ): Config<Omit<ClientOptions, keyof T> & T> => ({
327
+ ...jsonBodySerializer,
328
+ headers: defaultHeaders,
329
+ parseAs: 'auto',
330
+ querySerializer: defaultQuerySerializer,
331
+ ...override,
332
+ })
@@ -0,0 +1,26 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+
3
+ import {
4
+ type Client,
5
+ type ClientOptions,
6
+ type Config,
7
+ createClient,
8
+ createConfig,
9
+ } from './client'
10
+ import type { ClientOptions as ClientOptions2 } from './types.gen'
11
+
12
+ /**
13
+ * The `createClientConfig()` function will be called on client initialization
14
+ * and the returned object will become the client's initial configuration.
15
+ *
16
+ * You may want to initialize your client this way instead of calling
17
+ * `setConfig()`. This is useful for example if you're using Next.js
18
+ * to ensure your client always has the correct values.
19
+ */
20
+ export type CreateClientConfig<T extends ClientOptions = ClientOptions2> = (
21
+ override?: Config<ClientOptions & T>,
22
+ ) => Config<Required<ClientOptions> & T>
23
+
24
+ export const client: Client = createClient(
25
+ createConfig<ClientOptions2>({ baseUrl: 'https://api.worldlabs.ai' }),
26
+ )
@@ -0,0 +1,3 @@
1
+ // Barrel so `from './client'` in generated files emits `./client.js`
2
+ // (scan-dangling-dts). Keep this file if you regenerate the Hey API client.
3
+ export * from './client/index'
@@ -0,0 +1,48 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+
3
+ export type AuthToken = string | undefined
4
+
5
+ export interface Auth {
6
+ /**
7
+ * Which part of the request do we use to send the auth?
8
+ *
9
+ * @default 'header'
10
+ */
11
+ in?: 'header' | 'query' | 'cookie'
12
+ /**
13
+ * A unique identifier for the security scheme.
14
+ *
15
+ * Defined only when there are multiple security schemes whose `Auth`
16
+ * shape would otherwise be identical.
17
+ */
18
+ key?: string
19
+ /**
20
+ * Header or query parameter name.
21
+ *
22
+ * @default 'Authorization'
23
+ */
24
+ name?: string
25
+ scheme?: 'basic' | 'bearer'
26
+ type: 'apiKey' | 'http'
27
+ }
28
+
29
+ export const getAuthToken = async (
30
+ auth: Auth,
31
+ callback: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken,
32
+ ): Promise<string | undefined> => {
33
+ const token = typeof callback === 'function' ? await callback(auth) : callback
34
+
35
+ if (!token) {
36
+ return
37
+ }
38
+
39
+ if (auth.scheme === 'bearer') {
40
+ return `Bearer ${token}`
41
+ }
42
+
43
+ if (auth.scheme === 'basic') {
44
+ return `Basic ${btoa(token)}`
45
+ }
46
+
47
+ return token
48
+ }
@@ -0,0 +1,96 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+
3
+ import type {
4
+ ArrayStyle,
5
+ ObjectStyle,
6
+ SerializerOptions,
7
+ } from './pathSerializer.gen'
8
+
9
+ export type QuerySerializer = (query: Record<string, unknown>) => string
10
+
11
+ export type BodySerializer = (body: unknown) => unknown
12
+
13
+ type QuerySerializerOptionsObject = {
14
+ allowReserved?: boolean
15
+ array?: Partial<SerializerOptions<ArrayStyle>>
16
+ object?: Partial<SerializerOptions<ObjectStyle>>
17
+ }
18
+
19
+ export type QuerySerializerOptions = QuerySerializerOptionsObject & {
20
+ /**
21
+ * Per-parameter serialization overrides. When provided, these settings
22
+ * override the global array/object settings for specific parameter names.
23
+ */
24
+ parameters?: Record<string, QuerySerializerOptionsObject>
25
+ }
26
+
27
+ const serializeFormDataPair = (
28
+ data: FormData,
29
+ key: string,
30
+ value: unknown,
31
+ ): void => {
32
+ if (typeof value === 'string' || value instanceof Blob) {
33
+ data.append(key, value)
34
+ } else if (value instanceof Date) {
35
+ data.append(key, value.toISOString())
36
+ } else {
37
+ data.append(key, JSON.stringify(value))
38
+ }
39
+ }
40
+
41
+ const serializeUrlSearchParamsPair = (
42
+ data: URLSearchParams,
43
+ key: string,
44
+ value: unknown,
45
+ ): void => {
46
+ if (typeof value === 'string') {
47
+ data.append(key, value)
48
+ } else {
49
+ data.append(key, JSON.stringify(value))
50
+ }
51
+ }
52
+
53
+ export const formDataBodySerializer = {
54
+ bodySerializer: (body: unknown): FormData => {
55
+ const data = new FormData()
56
+
57
+ Object.entries(body as Record<string, unknown>).forEach(([key, value]) => {
58
+ if (value === undefined || value === null) {
59
+ return
60
+ }
61
+ if (Array.isArray(value)) {
62
+ value.forEach((v) => serializeFormDataPair(data, key, v))
63
+ } else {
64
+ serializeFormDataPair(data, key, value)
65
+ }
66
+ })
67
+
68
+ return data
69
+ },
70
+ }
71
+
72
+ export const jsonBodySerializer = {
73
+ bodySerializer: (body: unknown): string =>
74
+ JSON.stringify(body, (_key, value) =>
75
+ typeof value === 'bigint' ? value.toString() : value,
76
+ ),
77
+ }
78
+
79
+ export const urlSearchParamsBodySerializer = {
80
+ bodySerializer: (body: unknown): string => {
81
+ const data = new URLSearchParams()
82
+
83
+ Object.entries(body as Record<string, unknown>).forEach(([key, value]) => {
84
+ if (value === undefined || value === null) {
85
+ return
86
+ }
87
+ if (Array.isArray(value)) {
88
+ value.forEach((v) => serializeUrlSearchParamsPair(data, key, v))
89
+ } else {
90
+ serializeUrlSearchParamsPair(data, key, value)
91
+ }
92
+ })
93
+
94
+ return data.toString()
95
+ },
96
+ }
@@ -0,0 +1,186 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+
3
+ type Slot = 'body' | 'headers' | 'path' | 'query'
4
+
5
+ export type Field =
6
+ | {
7
+ in: Exclude<Slot, 'body'>
8
+ /**
9
+ * Field name. This is the name we want the user to see and use.
10
+ */
11
+ key: string
12
+ /**
13
+ * Field mapped name. This is the name we want to use in the request.
14
+ * If omitted, we use the same value as `key`.
15
+ */
16
+ map?: string
17
+ }
18
+ | {
19
+ in: Extract<Slot, 'body'>
20
+ /**
21
+ * Key isn't required for bodies.
22
+ */
23
+ key?: string
24
+ map?: string
25
+ }
26
+ | {
27
+ /**
28
+ * Field name. This is the name we want the user to see and use.
29
+ */
30
+ key: string
31
+ /**
32
+ * Field mapped name. This is the name we want to use in the request.
33
+ * If `in` is omitted, `map` aliases `key` to the transport layer.
34
+ */
35
+ map: Slot
36
+ }
37
+
38
+ export interface Fields {
39
+ allowExtra?: Partial<Record<Slot, boolean>>
40
+ args?: ReadonlyArray<Field>
41
+ }
42
+
43
+ export type FieldsConfig = ReadonlyArray<Field | Fields>
44
+
45
+ const extraPrefixesMap: Record<string, Slot> = {
46
+ $body_: 'body',
47
+ $headers_: 'headers',
48
+ $path_: 'path',
49
+ $query_: 'query',
50
+ }
51
+ const extraPrefixes = Object.entries(extraPrefixesMap)
52
+
53
+ type KeyMap = Map<
54
+ string,
55
+ | {
56
+ in: Slot
57
+ map?: string
58
+ }
59
+ | {
60
+ in?: never
61
+ map: Slot
62
+ }
63
+ >
64
+
65
+ function buildKeyMap(fields: FieldsConfig, map?: KeyMap): KeyMap {
66
+ if (!map) {
67
+ map = new Map()
68
+ }
69
+
70
+ for (const config of fields) {
71
+ if ('in' in config) {
72
+ if (config.key) {
73
+ map.set(config.key, {
74
+ in: config.in,
75
+ map: config.map,
76
+ })
77
+ }
78
+ } else if ('key' in config) {
79
+ map.set(config.key, {
80
+ map: config.map,
81
+ })
82
+ } else if (config.args) {
83
+ buildKeyMap(config.args, map)
84
+ }
85
+ }
86
+
87
+ return map
88
+ }
89
+
90
+ interface Params {
91
+ body?: unknown
92
+ headers: Record<string, unknown>
93
+ path: Record<string, unknown>
94
+ query: Record<string, unknown>
95
+ }
96
+
97
+ function stripEmptySlots(params: Params): void {
98
+ for (const [slot, value] of Object.entries(params)) {
99
+ if (slot === 'body') continue
100
+ if (
101
+ value &&
102
+ typeof value === 'object' &&
103
+ !Array.isArray(value) &&
104
+ !Object.keys(value).length
105
+ ) {
106
+ delete params[slot as Slot]
107
+ }
108
+ }
109
+ }
110
+
111
+ export function buildClientParams(
112
+ args: ReadonlyArray<unknown>,
113
+ fields: FieldsConfig,
114
+ ): Params {
115
+ const params: Params = {
116
+ headers: Object.create(null),
117
+ path: Object.create(null),
118
+ query: Object.create(null),
119
+ }
120
+
121
+ const map = buildKeyMap(fields)
122
+
123
+ function writeSlot(slot: Slot, key: string, value: unknown): void {
124
+ let record = params[slot] as Record<string, unknown> | undefined
125
+ if (record === undefined) {
126
+ record = Object.create(null) as Record<string, unknown>
127
+ params[slot] = record
128
+ }
129
+ record[key] = value
130
+ }
131
+
132
+ let config: FieldsConfig[number] | undefined
133
+
134
+ for (const [index, arg] of args.entries()) {
135
+ if (fields[index]) {
136
+ config = fields[index]
137
+ }
138
+
139
+ if (!config) {
140
+ continue
141
+ }
142
+
143
+ if ('in' in config) {
144
+ if (config.key) {
145
+ const field = map.get(config.key)!
146
+ const name = field.map || config.key
147
+ if (field.in) {
148
+ writeSlot(field.in, name, arg)
149
+ }
150
+ } else {
151
+ params.body = arg
152
+ }
153
+ } else {
154
+ for (const [key, value] of Object.entries(arg ?? {})) {
155
+ const field = map.get(key)
156
+
157
+ if (field) {
158
+ if (field.in) {
159
+ const name = field.map || key
160
+ writeSlot(field.in, name, value)
161
+ } else {
162
+ params[field.map] = value
163
+ }
164
+ } else {
165
+ const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix))
166
+
167
+ if (extra) {
168
+ const [prefix, slot] = extra
169
+ writeSlot(slot, key.slice(prefix.length), value)
170
+ } else if ('allowExtra' in config && config.allowExtra) {
171
+ for (const [slot, allowed] of Object.entries(config.allowExtra)) {
172
+ if (allowed) {
173
+ writeSlot(slot as Slot, key, value)
174
+ break
175
+ }
176
+ }
177
+ }
178
+ }
179
+ }
180
+ }
181
+ }
182
+
183
+ stripEmptySlots(params)
184
+
185
+ return params
186
+ }