import { ServerRouteMethod } from "./fetch.js"; import { ServerRoutes } from "@nuxt/schema"; import { Endpoint } from "fetchdts"; import { FetchOptions, FetchRequest } from "ofetch"; //#region src/app/types/fetch-internal-api.d.ts /** * Typing of `$fetch` and `useFetch` as it resolves under `compatibilityVersion: 4`: from the * response types nitro contributes to {@link ServerRoutes} through its `InternalApi` interface, * with no request-shape narrowing and no serialization of the response. * * Reached through the conditionals in `./fetch`, which pick this family or the generated route set * according to the engine the build resolved. Nothing here is part of the public API. */ /** * A route entry declared in the `fetchdts` shape (`{ [Endpoint]: { GET: { response: T } } }`), * rewritten to the `{ get: T }` shape nitro's `InternalApi` uses. * * @internal */ type NormalizeRouteEntry = Entry extends { [Endpoint]: infer Methods; } ? Omit & { [M in keyof Methods as Lowercase]: Methods[M] extends { response: infer R; } ? R : unknown; } : Entry; /** * The augmentable route registry with every entry in the `{ get: T }` shape. * * @internal */ type ServerRoutes$1 = { [Route in keyof ServerRoutes]: NormalizeRouteEntry; }; /** * A request accepted by `$fetch`: a route registered in {@link ServerRoutes}, any non-string * request `fetch` accepts, or an arbitrary string. * * Internal routes (`/_*` and `/api/_*`) are excluded from the suggested literals. * * @internal */ export type InternalApiRequest = Exclude | Exclude | (string & {}); /** @internal */ type HandlerOf = Method extends keyof ServerRoutes$1[MatchedServerRoutes] ? ServerRoutes$1[MatchedServerRoutes][Method] : never; /** * The type a request to `Route` resolves to, according to {@link ServerRoutes}, or `Default` * when the route is not registered (or is registered for other methods only). * * @internal */ export type InternalApiResponse = Default extends string | boolean | number | null | void | object ? Default : Route extends string ? HandlerOf extends never ? HandlerOf extends never ? Default : HandlerOf : HandlerOf : Default; /** * The methods `Request` can be fetched with: the methods registered for the matching route in * {@link ServerRoutes}, or every method when the route is not registered. * * @internal */ export type InternalApiMethods = Request extends string ? keyof ServerRoutes$1[MatchedServerRoutes] extends undefined ? ServerRouteMethod : Extract], 'default'> extends undefined ? Extract]> : ServerRouteMethod : ServerRouteMethod; /** The `body`, `query`, `headers` and `params` a request carries, as ofetch declares them. */ export interface InternalApiRequestShape { body?: FetchOptions['body']; query?: FetchOptions['query']; headers?: FetchOptions['headers']; params?: FetchOptions['params']; } /** @internal */ type MatchResult = { [k in Key]: { key: k; exact: Exact; score: Score; catchAll: CatchAll; }; }[Key]; /** @internal */ type Subtract = Minuend extends [...Subtrahend, ...infer Remainder] ? Remainder : never; /** @internal */ type TupleIfDiff = First extends `${Second}${infer Diff}` ? (Diff extends '' ? [] : Tuple) : []; /** @internal */ type MaxTuple = [N['length']] extends [Partial['length']] ? T : MaxTuple; /** @internal */ type CalcMatchScore = `${Key}/` extends `${infer KeySeg}/${infer KeyRest}` ? KeySeg extends FirstKeySegMatcher ? Subtract<[...Score, ...TupleIfDiff], TupleIfDiff> : `${Route}/` extends `${infer RouteSeg}/${infer RouteRest}` ? `${RouteSeg}?` extends `${infer RouteSegWithoutQuery}?${string}` ? RouteSegWithoutQuery extends KeySeg ? CalcMatchScore : KeySeg extends `:${string}` ? RouteSegWithoutQuery extends '' ? never : CalcMatchScore : KeySeg extends RouteSegWithoutQuery ? CalcMatchScore : never : never : never : never; /** @internal */ type _MatchedServerRoutes = MatchResult> = MatchedResultUnion['key'] extends (infer MatchedKeys) ? MatchedKeys extends string ? Route extends MatchedKeys ? MatchResult : MatchedKeys extends `${infer Root}/**${string}` ? MatchedKeys extends `${string}/**` ? Route extends `${Root}/${string}` ? MatchResult : never : MatchResult> : MatchResult> : never : never; /** * The score of the most specific `:param` match among candidates. * * @internal */ type MaxScore> = MaxTuple; /** * The keys of {@link ServerRoutes} matching a requested path. Exported for type tests; not part * of the public API. * * @internal */ export type MatchedServerRoutes = MatchResult, Matches extends MatchResult = _MatchedServerRoutes> = Route extends '/' ? keyof ServerRoutes$1 : Extract extends never ? Extract, { score: MaxScore; }>['key'] | Extract['key'] : Extract['key']; //#endregion export {};