UNPKG

@mcansh/remix-fastify

Version:

Fastify server request handler for Remix and React Router

114 lines (113 loc) 4.91 kB
import { AppLoadContext as AppLoadContext$1, ServerBuild as ServerBuild$1 } from "react-router"; import { FastifyReply, FastifyRequest, RouteGenericInterface, RouteShorthandOptions } from "fastify"; import * as http from "node:http"; import * as http2 from "node:http2"; import * as https from "node:https"; import { AppLoadContext as AppLoadContext$2, ServerBuild as ServerBuild$2 } from "@remix-run/node"; import { FastifyStaticOptions } from "@fastify/static"; import { cacheHeader } from "pretty-cache-header"; import { InlineConfig } from "vite"; //#region src/shared.d.ts type HttpServer = http.Server | https.Server | http2.Http2Server | http2.Http2SecureServer; type RequestHandler<Server extends HttpServer> = (request: FastifyRequest<RouteGenericInterface, Server>, reply: FastifyReply<RouteGenericInterface, Server>) => Promise<void>; /** * A function that returns the value to use as `context` in route `loader` and * `action` functions. * * You can think of this as an escape hatch that allows you to pass * environment/platform-specific values through to your loader/action. */ type GetLoadContextFunction<Server extends HttpServer, AppLoadContext> = (request: FastifyRequest<RouteGenericInterface, Server>, reply: FastifyReply<RouteGenericInterface, Server>) => Promise<AppLoadContext> | AppLoadContext; //#endregion //#region src/servers/react-router.d.ts type GetLoadContextFunction$2<Server extends HttpServer = HttpServer> = GetLoadContextFunction<Server, AppLoadContext$1>; /** * Returns a request handler for Fastify that serves the response using Remix. */ declare function createReactRouterRequestHandler<Server extends HttpServer>({ build, getLoadContext, mode }: { build: ServerBuild$1 | (() => ServerBuild$1 | Promise<ServerBuild$1>); getLoadContext?: GetLoadContextFunction$2<Server>; mode?: string; }): RequestHandler<Server>; //#endregion //#region src/servers/remix.d.ts type GetLoadContextFunction$1<Server extends HttpServer = HttpServer> = GetLoadContextFunction<Server, AppLoadContext$2>; /** * Returns a request handler for Fastify that serves the response using Remix. */ declare function createRemixRequestHandler<Server extends HttpServer>({ build, getLoadContext, mode }: { build: ServerBuild$2 | (() => ServerBuild$2 | Promise<ServerBuild$2>); getLoadContext?: GetLoadContextFunction$1<Server>; mode?: string; }): RequestHandler<Server>; //#endregion //#region src/plugins/index.d.ts type PluginOptions<Server extends HttpServer = HttpServer, AppLoadContext = unknown, ServerBuild = unknown> = { /** * The base path for the Remix app. * match the `basename` in your Vite config. * @default "/" */ basename?: string; /** * The directory where the Remix app is built. * This should match the `buildDirectory` directory in your Remix config. * @default "build" */ buildDirectory?: string; /** * The Remix server output filename * This should match the `serverBuildFile` filename in your Remix config. * @default "index.js" */ serverBuildFile?: string; /** * A function that returns the value to use as `context` in route `loader` and * `action` functions. * * You can think of this as an escape hatch that allows you to pass * environment/platform-specific values through to your loader/action. */ getLoadContext?: GetLoadContextFunction<Server, AppLoadContext>; mode?: string; /** * Options to pass to the Vite server in development. */ viteOptions?: InlineConfig; /** * Options to pass to the `@fastify/static` plugin for serving compiled assets in production. */ fastifyStaticOptions?: FastifyStaticOptions; /** * The cache control options to use for build assets in production. * uses `pretty-cache-header` under the hood. * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control * @default { public: true, maxAge: '1 year', immutable: true } */ assetCacheControl?: Parameters<typeof cacheHeader>[0]; /** * The cache control options to use for other assets in production. * uses `pretty-cache-header` under the hood. * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control * @default { public: true, maxAge: '1 hour' } */ defaultCacheControl?: Parameters<typeof cacheHeader>[0]; /** * The Remix server build to use in production. Use this only if the default approach doesn't work for you. * * If not provided, it will be loaded using `import()` with the server build path provided in the options. */ productionServerBuild?: ServerBuild | (() => ServerBuild | Promise<ServerBuild>); childServerOptions?: RouteShorthandOptions<Server>; }; //#endregion export { GetLoadContextFunction, HttpServer, PluginOptions, RequestHandler, createReactRouterRequestHandler, createRemixRequestHandler }; //# sourceMappingURL=index-BXZ8a1kB.d.cts.map