queryKeySerializer.gen.ts 2.8 KB

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