Open History to see the last commit
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 = (
18 _key: string,
19 value: unknown,
20): unknown | undefined => {
21 if (
22 value === undefined ||
23 typeof value === 'function' ||
24 typeof value === 'symbol'
25 ) {
26 return undefined
27 }
28 if (typeof value === 'bigint') {
29 return value.toString()
30 }
31 if (value instanceof Date) {
32 return value.toISOString()
33 }
34 return value
35}
36
37/**
38 * Safely stringifies a value and parses it back into a JsonValue.
39 */
40export const stringifyToJsonValue = (input: unknown): JsonValue | undefined => {
41 try {
42 const json = JSON.stringify(input, queryKeyJsonReplacer)
43 if (json === undefined) {
44 return undefined
45 }
46 return JSON.parse(json) as JsonValue
47 } catch {
48 return undefined
49 }
50}
51
52/**
53 * Detects plain objects (including objects with a null prototype).
54 */
55const isPlainObject = (value: unknown): value is Record<string, unknown> => {
56 if (value === null || typeof value !== 'object') {
57 return false
58 }
59 const prototype = Object.getPrototypeOf(value as object)
60 return prototype === Object.prototype || prototype === null
61}
62
63/**
64 * Turns URLSearchParams into a sorted JSON object for deterministic keys.
65 */
66const serializeSearchParams = (params: URLSearchParams): JsonValue => {
67 const entries = Array.from(params.entries()).sort(([a], [b]) =>
68 a.localeCompare(b),
69 )
70 const result: Record<string, JsonValue> = {}
71
72 for (const [key, value] of entries) {
73 const existing = result[key]
74 if (existing === undefined) {
75 result[key] = value
76 continue
77 }
78
79 if (Array.isArray(existing)) {
80 ;(existing as string[]).push(value)
81 } else {
82 result[key] = [existing, value]
83 }
84 }
85
86 return result
87}
88
89/**
90 * Normalizes any accepted value into a JSON-friendly shape for query keys.
91 */
92export const serializeQueryKeyValue = (
93 value: unknown,
94): JsonValue | undefined => {
95 if (value === null) {
96 return null
97 }
98
99 if (
100 typeof value === 'string' ||
101 typeof value === 'number' ||
102 typeof value === 'boolean'
103 ) {
104 return value
105 }
106
107 if (
108 value === undefined ||
109 typeof value === 'function' ||
110 typeof value === 'symbol'
111 ) {
112 return undefined
113 }
114
115 if (typeof value === 'bigint') {
116 return value.toString()
117 }
118
119 if (value instanceof Date) {
120 return value.toISOString()
121 }
122
123 if (Array.isArray(value)) {
124 return stringifyToJsonValue(value)
125 }
126
127 if (
128 typeof URLSearchParams !== 'undefined' &&
129 value instanceof URLSearchParams
130 ) {
131 return serializeSearchParams(value)
132 }
133
134 if (isPlainObject(value)) {
135 return stringifyToJsonValue(value)
136 }
137
138 return undefined
139}