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

params.gen.ts

186 lines4.1 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​
65function 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​
97function 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 }
110​
111export 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