fork of hey-api/openapi-ts because I need some additional things
1// This file is auto-generated by @hey-api/openapi-ts
2
3/**
4 * JSON-friendly union that mirrors what Pinia Colada can hash.
5 */
6export type JsonValue =
7 | null
8 | string
9 | number
10 | boolean
11 | JsonValue[]
12 | { [key: string]: JsonValue }
13
14/**
15 * Replacer that converts non-JSON values (bigint, Date, etc.) to safe substitutes.
16 */
17export const queryKeyJsonReplacer = (_key: string, value: unknown) => {
18 if (value === undefined || typeof value === 'function' || typeof value === 'symbol') {
19 return undefined
20 }
21 if (typeof value === 'bigint') {
22 return value.toString()
23 }
24 if (value instanceof Date) {
25 return value.toISOString()
26 }
27 return value
28}
29
30/**
31 * Safely stringifies a value and parses it back into a JsonValue.
32 */
33export const stringifyToJsonValue = (input: unknown): JsonValue | undefined => {
34 try {
35 const json = JSON.stringify(input, queryKeyJsonReplacer)
36 if (json === undefined) {
37 return undefined
38 }
39 return JSON.parse(json) as JsonValue
40 } catch {
41 return undefined
42 }
43}
44
45/**
46 * Detects plain objects (including objects with a null prototype).
47 */
48const isPlainObject = (value: unknown): value is Record<string, unknown> => {
49 if (value === null || typeof value !== 'object') {
50 return false
51 }
52 const prototype = Object.getPrototypeOf(value as object)
53 return prototype === Object.prototype || prototype === null
54}
55
56/**
57 * Turns URLSearchParams into a sorted JSON object for deterministic keys.
58 */
59const serializeSearchParams = (params: URLSearchParams): JsonValue => {
60 const entries = Array.from(params.entries()).sort(([a], [b]) => a.localeCompare(b))
61 const result: Record<string, JsonValue> = {}
62
63 for (const [key, value] of entries) {
64 const existing = result[key]
65 if (existing === undefined) {
66 result[key] = value
67 continue
68 }
69
70 if (Array.isArray(existing)) {
71 ;(existing as string[]).push(value)
72 } else {
73 result[key] = [existing, value]
74 }
75 }
76
77 return result
78}
79
80/**
81 * Normalizes any accepted value into a JSON-friendly shape for query keys.
82 */
83export const serializeQueryKeyValue = (value: unknown): JsonValue | undefined => {
84 if (value === null) {
85 return null
86 }
87
88 if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
89 return value
90 }
91
92 if (value === undefined || typeof value === 'function' || typeof value === 'symbol') {
93 return undefined
94 }
95
96 if (typeof value === 'bigint') {
97 return value.toString()
98 }
99
100 if (value instanceof Date) {
101 return value.toISOString()
102 }
103
104 if (Array.isArray(value)) {
105 return stringifyToJsonValue(value)
106 }
107
108 if (typeof URLSearchParams !== 'undefined' && value instanceof URLSearchParams) {
109 return serializeSearchParams(value)
110 }
111
112 if (isPlainObject(value)) {
113 return stringifyToJsonValue(value)
114 }
115
116 return undefined
117}