Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

params.gen.ts

179 lines3.9 KB

1// This file is auto-generated by @hey-api/openapi-ts
2​
3type Slot = 'body' | 'headers' | 'path' | 'query'
4​
5export 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​
38export interface Fields {
39 allowExtra?: Partial<Record<Slot, boolean>>
40 args?: ReadonlyArray<Field>
42​
43export type FieldsConfig = ReadonlyArray<Field | Fields>
44​
45const extraPrefixesMap: Record<string, Slot> = {
46 $body_: 'body',
47 $headers_: 'headers',
48 $path_: 'path',
49 $query_: 'query',
51const extraPrefixes = Object.entries(extraPrefixesMap)
52​
53type KeyMap = Map<
54 string,
55 | {
56 in: Slot
57 map?: string
58 }
59 | {
60 in?: never
61 map: Slot
62 }
64​
65const 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
89​
90interface Params {
91 body: unknown
92 headers: Record<string, unknown>
93 path: Record<string, unknown>
94 query: Record<string, unknown>
96​
97const stripEmptySlots = (params: Params) => {
98 for (const [slot, value] of Object.entries(params)) {
99 if (
100 value &&
101 typeof value === 'object' &&
102 !Array.isArray(value) &&
103 !Object.keys(value).length
104 ) {
105 delete params[slot as Slot]
106 }
107 }
109​
110export const buildClientParams = (
111 args: ReadonlyArray<unknown>,
112 fields: FieldsConfig,
113) => {
114 const params: Params = {
115 body: {},
116 headers: {},
117 path: {},
118 query: {},
119 }
120​
121 const map = buildKeyMap(fields)
122​
123 let config: FieldsConfig[number] | undefined
124​
125 for (const [index, arg] of args.entries()) {
126 if (fields[index]) {
127 config = fields[index]
128 }
129​
130 if (!config) {
131 continue
132 }
133​
134 if ('in' in config) {
135 if (config.key) {
136 const field = map.get(config.key)!
137 const name = field.map || config.key
138 if (field.in) {
139 ;(params[field.in] as Record<string, unknown>)[name] = arg
140 }
141 } else {
142 params.body = arg
143 }
144 } else {
145 for (const [key, value] of Object.entries(arg ?? {})) {
146 const field = map.get(key)
147​
148 if (field) {
149 if (field.in) {
150 const name = field.map || key
151 ;(params[field.in] as Record<string, unknown>)[name] = value
152 } else {
153 params[field.map] = value
154 }
155 } else {
156 const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix))
157​
158 if (extra) {
159 const [prefix, slot] = extra
160 ;(params[slot] as Record<string, unknown>)[
161 key.slice(prefix.length)
162 ] = value
163 } else if ('allowExtra' in config && config.allowExtra) {
164 for (const [slot, allowed] of Object.entries(config.allowExtra)) {
165 if (allowed) {
166 ;(params[slot as Slot] as Record<string, unknown>)[key] = value
167 break
168 }
169 }
170 }
171 }
172 }
173 }
174 }
175​
176 stripEmptySlots(params)
177​
178 return params