@cobaltcore-dev/aurora 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"trpcClient-BxguzNYF.mjs","names":["createTRPCReact","createTRPCClient","httpBatchLink","httpBatchStreamLink","httpLink","splitLink","isNonJsonSerializable","httpSubscriptionLink","getCsrfHeaders","csrfToken","fetch","then","res","json","error","console","STREAMING_PROCEDURES","Set","_bffEndpoint","setBffEndpoint","endpoint","getLinks","condition","op","type","true","url","eventSourceOptions","headers","false","input","csrf","extra","context","has","path","trpcReact","_trpcReactClient","_trpcClient","trpcReactClient","Proxy","get","_target","prop","createClient","links","trpcClient"],"sources":["../../src/client/trpcClient.ts"],"sourcesContent":["import { createTRPCReact } from \"@trpc/react-query\"\nimport {\n createTRPCClient,\n httpBatchLink,\n httpBatchStreamLink,\n httpLink,\n splitLink,\n isNonJsonSerializable,\n httpSubscriptionLink,\n} from \"@trpc/client\"\nimport type { AuroraRouter } from \"../server/routers\"\n\n// CSRF headers factory\nconst getCsrfHeaders = async () => {\n try {\n const { csrfToken } = await fetch(\"/csrf-token\").then((res) => res.json())\n return {\n \"x-csrf-token\": csrfToken,\n }\n } catch (error) {\n console.error(\"Failed to fetch CSRF token:\", error)\n return {}\n }\n}\n\n// Procedures that return async iterables (chunked streaming responses).\n// These are routed through httpBatchStreamLink instead of httpBatchLink.\n// Add any new streaming procedure paths here.\nconst STREAMING_PROCEDURES = new Set<string>([\"storage.swift.downloadObject\"])\n\n// Mutable endpoint — set once by App before any tRPC calls are made.\nlet _bffEndpoint = \"/polaris-bff\"\n\nexport function setBffEndpoint(endpoint: string) {\n _bffEndpoint = endpoint\n}\n\nconst getLinks = () => [\n splitLink({\n // First check: is it a subscription?\n condition: (op) => op.type === \"subscription\",\n // Use HTTP subscription link for subscriptions (long-polling)\n true: httpSubscriptionLink({\n url: _bffEndpoint,\n // Callback to populate headers\n eventSourceOptions: async () => {\n return { headers: await getCsrfHeaders() }\n },\n }),\n // For non-subscriptions, check for non-JSON data or streaming procedures\n false: splitLink({\n // Non-JSON serializable input (FormData, Blob, ArrayBuffer, etc.) → plain httpLink.\n condition: (op) => isNonJsonSerializable(op.input),\n true: httpLink({\n url: _bffEndpoint,\n async headers({ op }) {\n const csrf = await getCsrfHeaders()\n // Per-request headers can be injected via tRPC operation context.\n // op.context is set by the caller and forwarded through the link chain.\n const extra = (op.context as { headers?: Record<string, string> } | undefined)?.headers ?? {}\n return { ...csrf, ...extra }\n },\n }),\n // For JSON procedures, decide between streaming and regular batching\n false: splitLink({\n // Procedures returning async iterables must use httpBatchStreamLink.\n // httpBatchLink buffers the full response before resolving, which\n // breaks streaming. httpBatchStreamLink preserves batching while\n // supporting chunked/streamed responses.\n // Note: httpBatchStreamLink is intentionally NOT used globally because\n // it is incompatible with the @fastify/csrf-protection cookie rotation\n // used in this app — CSRF tokens can expire mid-stream on long-lived\n // connections. Scope it only to procedures that actually need streaming.\n condition: (op) => STREAMING_PROCEDURES.has(op.path),\n true: httpBatchStreamLink({\n url: _bffEndpoint,\n async headers() {\n return getCsrfHeaders()\n },\n }),\n // Regular JSON mutations/queries — standard batching\n false: httpBatchLink({\n url: _bffEndpoint,\n async headers() {\n return getCsrfHeaders()\n },\n }),\n }),\n }),\n }),\n]\n\n// React Query client (for hooks like useQuery/useMutation/useSubscription)\nexport const trpcReact = createTRPCReact<AuroraRouter>()\n\nlet _trpcReactClient: ReturnType<typeof trpcReact.createClient> | null = null\nlet _trpcClient: ReturnType<typeof createTRPCClient<AuroraRouter>> | null = null\n\n// Lazily initialised — created on first access after setBffEndpoint() has been called by App.\nexport const trpcReactClient = new Proxy({} as ReturnType<typeof trpcReact.createClient>, {\n get(_target, prop) {\n if (!_trpcReactClient) {\n _trpcReactClient = trpcReact.createClient({ links: getLinks() })\n }\n return (_trpcReactClient as Record<string | symbol, unknown>)[prop]\n },\n})\n\nexport const trpcClient = new Proxy({} as ReturnType<typeof createTRPCClient<AuroraRouter>>, {\n get(_target, prop) {\n if (!_trpcClient) {\n _trpcClient = createTRPCClient<AuroraRouter>({ links: getLinks() })\n }\n return (_trpcClient as Record<string | symbol, unknown>)[prop]\n },\n})\n\nexport type TrpcReact = typeof trpcReact\nexport type TrpcClient = typeof trpcClient\n"],"mappings":";;;AAaA,IAAMQ,IAAiB,YAAA;AACrB,KAAI;EACF,IAAM,EAAEC,iBAAc,MAAMC,MAAM,cAAA,CAAeC,MAAMC,MAAQA,EAAIC,MAAI,CAAA;AACvE,SAAO,EACL,gBAAgBJ,GAClB;UACOK,GAAO;AAEd,SADAC,QAAQD,MAAM,+BAA+BA,EAAAA,EACtC,EAAC;;GAONE,IAAuB,IAAIC,IAAY,CAAC,+BAA+B,CAAA,EAGzEC,IAAe;AAEnB,SAAgBC,EAAeC,GAAgB;AAC7CF,KAAeE;;AAGjB,IAAMC,UAAiB,CACrBhB,EAAU;CAERiB,YAAYC,MAAOA,EAAGC,SAAS;CAE/BC,MAAMlB,EAAqB;EACzBmB,KAAKR;EAELS,oBAAoB,aACX,EAAEC,SAAS,MAAMpB,GAAAA,EAAiB;EAE7C,CAAA;CAEAqB,OAAOxB,EAAU;EAEfiB,YAAYC,MAAOjB,EAAsBiB,EAAGO,MAAK;EACjDL,MAAMrB,EAAS;GACbsB,KAAKR;GACL,MAAMU,QAAQ,EAAEL,SAAI;IAClB,IAAMQ,IAAO,MAAMvB,GAAAA,EAGbwB,IAAQ,EAAIC,SAA8DL,WAAW,EAAC;AAC5F,WAAO;KAAE,GAAGG;KAAM,GAAGC;KAAM;;GAE/B,CAAA;EAEAH,OAAOxB,EAAU;GASfiB,YAAYC,MAAOP,EAAqBkB,IAAIX,EAAGY,KAAI;GACnDV,MAAMtB,EAAoB;IACxBuB,KAAKR;IACL,MAAMU,UAAAA;AACJ,YAAOpB,GAAAA;;IAEX,CAAA;GAEAqB,OAAO3B,EAAc;IACnBwB,KAAKR;IACL,MAAMU,UAAAA;AACJ,YAAOpB,GAAAA;;IAEX,CAAA;GACF,CAAA;EACF,CAAA;CACF,CAAA,CACD,EAGY4B,IAAYpC,GAAAA,EAErBqC,IAAqE,MACrEC,IAAwE,MAG/DC,IAAkB,IAAIC,MAAM,EAAC,EAAgD,EACxFC,IAAIC,GAASC,GAAI;AAIf,QAHA,AACEN,MAAmBD,EAAUQ,aAAa,EAAEC,OAAOxB,GAAAA,EAAW,CAAA,EAEzD,EAAuDsB;GAElE,CAAA,EAEaG,IAAa,IAAIN,MAAM,EAAC,EAAwD,EAC3FC,IAAIC,GAASC,GAAI;AAIf,QAHA,AACEL,MAAcrC,EAA+B,EAAE4C,OAAOxB,GAAAA,EAAW,CAAA,EAE5D,EAAkDsB;GAE7D,CAAA"}
1
+ {"version":3,"file":"trpcClient-BxguzNYF.mjs","names":["createTRPCReact","createTRPCClient","httpBatchLink","httpBatchStreamLink","httpLink","splitLink","isNonJsonSerializable","httpSubscriptionLink","getCsrfHeaders","csrfToken","fetch","then","res","json","error","console","STREAMING_PROCEDURES","Set","_bffEndpoint","setBffEndpoint","endpoint","getLinks","condition","op","type","true","url","eventSourceOptions","headers","false","input","csrf","extra","context","has","path","trpcReact","_trpcReactClient","_trpcClient","trpcReactClient","Proxy","get","_target","prop","createClient","links","trpcClient"],"sources":["../../src/client/trpcClient.ts"],"sourcesContent":["import { createTRPCReact } from \"@trpc/react-query\"\nimport {\n createTRPCClient,\n httpBatchLink,\n httpBatchStreamLink,\n httpLink,\n splitLink,\n isNonJsonSerializable,\n httpSubscriptionLink,\n} from \"@trpc/client\"\nimport type { AuroraRouter } from \"../server/routers\"\n\n// CSRF headers factory\nconst getCsrfHeaders = async () => {\n try {\n const { csrfToken } = await fetch(\"/csrf-token\").then((res) => res.json())\n return {\n \"x-csrf-token\": csrfToken,\n }\n } catch (error) {\n console.error(\"Failed to fetch CSRF token:\", error)\n return {}\n }\n}\n\n// Procedures that return async iterables (chunked streaming responses).\n// These are routed through httpBatchStreamLink instead of httpBatchLink.\n// Add any new streaming procedure paths here.\nconst STREAMING_PROCEDURES = new Set<string>([\"storage.swift.downloadObject\"])\n\n// Mutable endpoint — set once by App before any tRPC calls are made.\nlet _bffEndpoint = \"/polaris-bff\"\n\nexport function setBffEndpoint(endpoint: string) {\n _bffEndpoint = endpoint\n}\n\nconst getLinks = () => [\n splitLink({\n // First check: is it a subscription?\n condition: (op) => op.type === \"subscription\",\n // Use HTTP subscription link for subscriptions (long-polling)\n true: httpSubscriptionLink({\n url: _bffEndpoint,\n // Callback to populate headers\n eventSourceOptions: async () => {\n return { headers: await getCsrfHeaders() }\n },\n }),\n // For non-subscriptions, check for non-JSON data or streaming procedures\n false: splitLink({\n // Non-JSON serializable input (FormData, Blob, ArrayBuffer, etc.) → plain httpLink.\n condition: (op) => isNonJsonSerializable(op.input),\n true: httpLink({\n url: _bffEndpoint,\n async headers({ op }) {\n const csrf = await getCsrfHeaders()\n // Per-request headers can be injected via tRPC operation context.\n // op.context is set by the caller and forwarded through the link chain.\n const extra = (op.context as { headers?: Record<string, string> } | undefined)?.headers ?? {}\n return { ...csrf, ...extra }\n },\n }),\n // For JSON procedures, decide between streaming and regular batching\n false: splitLink({\n // Procedures returning async iterables must use httpBatchStreamLink.\n // httpBatchLink buffers the full response before resolving, which\n // breaks streaming. httpBatchStreamLink preserves batching while\n // supporting chunked/streamed responses.\n // Note: httpBatchStreamLink is intentionally NOT used globally because\n // it is incompatible with the @fastify/csrf-protection cookie rotation\n // used in this app — CSRF tokens can expire mid-stream on long-lived\n // connections. Scope it only to procedures that actually need streaming.\n condition: (op) => STREAMING_PROCEDURES.has(op.path),\n true: httpBatchStreamLink({\n url: _bffEndpoint,\n async headers() {\n return getCsrfHeaders()\n },\n }),\n // Regular JSON mutations/queries — standard batching\n false: httpBatchLink({\n url: _bffEndpoint,\n async headers() {\n return getCsrfHeaders()\n },\n }),\n }),\n }),\n }),\n]\n\n// React Query client (for hooks like useQuery/useMutation/useSubscription)\nexport const trpcReact: ReturnType<typeof createTRPCReact<AuroraRouter>> = createTRPCReact<AuroraRouter>()\n\nlet _trpcReactClient: ReturnType<typeof trpcReact.createClient> | null = null\nlet _trpcClient: ReturnType<typeof createTRPCClient<AuroraRouter>> | null = null\n\n// Lazily initialised — created on first access after setBffEndpoint() has been called by App.\nexport const trpcReactClient = new Proxy({} as ReturnType<typeof trpcReact.createClient>, {\n get(_target, prop) {\n if (!_trpcReactClient) {\n _trpcReactClient = trpcReact.createClient({ links: getLinks() })\n }\n return (_trpcReactClient as Record<string | symbol, unknown>)[prop]\n },\n})\n\nexport const trpcClient = new Proxy({} as ReturnType<typeof createTRPCClient<AuroraRouter>>, {\n get(_target, prop) {\n if (!_trpcClient) {\n _trpcClient = createTRPCClient<AuroraRouter>({ links: getLinks() })\n }\n return (_trpcClient as Record<string | symbol, unknown>)[prop]\n },\n})\n\nexport type TrpcReact = typeof trpcReact\nexport type TrpcClient = typeof trpcClient\n"],"mappings":";;;AAaA,IAAMQ,IAAiB,YAAA;AACrB,KAAI;EACF,IAAM,EAAEC,iBAAc,MAAMC,MAAM,cAAA,CAAeC,MAAMC,MAAQA,EAAIC,MAAI,CAAA;AACvE,SAAO,EACL,gBAAgBJ,GAClB;UACOK,GAAO;AAEd,SADAC,QAAQD,MAAM,+BAA+BA,EAAAA,EACtC,EAAC;;GAONE,IAAuB,IAAIC,IAAY,CAAC,+BAA+B,CAAA,EAGzEC,IAAe;AAEnB,SAAgBC,EAAeC,GAAgB;AAC7CF,KAAeE;;AAGjB,IAAMC,UAAiB,CACrBhB,EAAU;CAERiB,YAAYC,MAAOA,EAAGC,SAAS;CAE/BC,MAAMlB,EAAqB;EACzBmB,KAAKR;EAELS,oBAAoB,aACX,EAAEC,SAAS,MAAMpB,GAAAA,EAAiB;EAE7C,CAAA;CAEAqB,OAAOxB,EAAU;EAEfiB,YAAYC,MAAOjB,EAAsBiB,EAAGO,MAAK;EACjDL,MAAMrB,EAAS;GACbsB,KAAKR;GACL,MAAMU,QAAQ,EAAEL,SAAI;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