UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

1 lines • 4.05 kB
{"version":3,"file":"router-client.mjs","names":["createTanStackRouter"],"sources":["../../src/ui/router-client.tsx"],"sourcesContent":["/**\n * Client router factory — creates a TanStack Router with browser history.\n * The app-specific route tree is injected as `defaultRouteTree` (the\n * bundled core-only fallback); the composed tree wins when manifest\n * construction produced one.\n */\n\nimport { dehydrate, hydrate } from \"@tanstack/react-query\";\nimport {\n type AnyRoute,\n createBrowserHistory,\n createRouter as createTanStackRouter,\n} from \"@tanstack/react-router\";\nimport { createAuthClient } from \"./auth\";\nimport { defaultNotFoundComponent, defaultPendingComponent } from \"./router-defaults\";\nimport { RouterError } from \"./router-error\";\nimport type { CreateRouterOptions, RouterContextWithApi } from \"./types\";\n\nexport interface CoreRouterOptions<TApiClient = unknown, TSession = unknown>\n extends CreateRouterOptions<TApiClient, TSession> {\n /** The app's generated route tree — the core-only fallback when no composed tree is passed. */\n defaultRouteTree?: unknown;\n}\n\nexport function createRouter<\n TApiClient = unknown,\n TSession = unknown,\n TRouteTree extends AnyRoute = AnyRoute,\n>(\n opts: CoreRouterOptions<TApiClient, TSession> & {\n context: RouterContextWithApi<TApiClient, TSession> & { authClient?: unknown };\n },\n) {\n const queryClient = opts.context.queryClient;\n const history = opts.history ?? createBrowserHistory();\n const cspNonce = opts.context.cspNonce;\n\n const routeTree = opts.routeTree ?? opts.defaultRouteTree;\n if (!routeTree) {\n throw new Error(\n \"createRouter: no route tree — compose failed and no core-only fallback was constructed\",\n );\n }\n\n const router = createTanStackRouter({\n routeTree: routeTree as TRouteTree,\n history,\n basepath: opts.basepath ?? opts.context.runtimeConfig?.runtime?.runtimeBasePath ?? \"/\",\n context: {\n queryClient,\n runtimeConfig: opts.context.runtimeConfig,\n cspNonce: opts.context.cspNonce,\n apiClient: opts.context.apiClient,\n authClient:\n opts.context.authClient ??\n createAuthClient({\n runtimeConfig: opts.context.runtimeConfig,\n cspNonce: opts.context.cspNonce,\n }),\n session: opts.context.session,\n locale: opts.context.locale,\n },\n ...(cspNonce ? { ssr: { nonce: cspNonce } } : {}),\n defaultPreload: \"intent\",\n scrollRestoration: true,\n defaultStructuralSharing: true,\n defaultPreloadStaleTime: 0,\n defaultPendingMinMs: 0,\n defaultErrorComponent: RouterError,\n defaultNotFoundComponent,\n defaultPendingComponent,\n dehydrate: () => {\n if (typeof window === \"undefined\") {\n return { queryClientState: dehydrate(queryClient) };\n }\n\n return { queryClientState: {} };\n },\n hydrate: (dehydrated: { queryClientState?: unknown }) => {\n if (typeof window !== \"undefined\" && dehydrated?.queryClientState) {\n hydrate(queryClient, dehydrated.queryClientState);\n }\n },\n });\n\n return { router, queryClient };\n}\n"],"mappings":";;;;;;;;;;;;;AAwBA,SAAgB,aAKd,MAGA;CACA,MAAM,cAAc,KAAK,QAAQ;CACjC,MAAM,UAAU,KAAK,WAAW,qBAAqB;CACrD,MAAM,WAAW,KAAK,QAAQ;CAE9B,MAAM,YAAY,KAAK,aAAa,KAAK;CACzC,IAAI,CAAC,WACH,MAAM,IAAI,MACR,wFACF;CA4CF,OAAO;EAAE,QAzCMA,eAAqB;GACvB;GACX;GACA,UAAU,KAAK,YAAY,KAAK,QAAQ,eAAe,SAAS,mBAAmB;GACnF,SAAS;IACP;IACA,eAAe,KAAK,QAAQ;IAC5B,UAAU,KAAK,QAAQ;IACvB,WAAW,KAAK,QAAQ;IACxB,YACE,KAAK,QAAQ,cACb,iBAAiB;KACf,eAAe,KAAK,QAAQ;KAC5B,UAAU,KAAK,QAAQ;IACzB,CAAC;IACH,SAAS,KAAK,QAAQ;IACtB,QAAQ,KAAK,QAAQ;GACvB;GACA,GAAI,WAAW,EAAE,KAAK,EAAE,OAAO,SAAS,EAAE,IAAI,CAAC;GAC/C,gBAAgB;GAChB,mBAAmB;GACnB,0BAA0B;GAC1B,yBAAyB;GACzB,qBAAqB;GACrB,uBAAuB;GACvB;GACA;GACA,iBAAiB;IACf,IAAI,OAAO,WAAW,aACpB,OAAO,EAAE,kBAAkB,UAAU,WAAW,EAAE;IAGpD,OAAO,EAAE,kBAAkB,CAAC,EAAE;GAChC;GACA,UAAU,eAA+C;IACvD,IAAI,OAAO,WAAW,eAAe,YAAY,kBAC/C,QAAQ,aAAa,WAAW,gBAAgB;GAEpD;EACF,CAEc;EAAG;CAAY;AAC/B"}