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