From d675cf4477b4ca6359ad46faa6e3605e82bda996 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 14 Nov 2023 03:48:08 +0100 Subject: [PATCH 1/2] refactor: split preset types --- src/types/presets.ts | 185 -------------------------------- src/types/presets/azure.ts | 13 +++ src/types/presets/cloudflare.ts | 33 ++++++ src/types/presets/firebase.ts | 41 +++++++ src/types/presets/index.ts | 11 ++ src/types/presets/vercel.ts | 93 ++++++++++++++++ 6 files changed, 191 insertions(+), 185 deletions(-) delete mode 100644 src/types/presets.ts create mode 100644 src/types/presets/azure.ts create mode 100644 src/types/presets/cloudflare.ts create mode 100644 src/types/presets/firebase.ts create mode 100644 src/types/presets/index.ts create mode 100644 src/types/presets/vercel.ts diff --git a/src/types/presets.ts b/src/types/presets.ts deleted file mode 100644 index 713fcf25bb..0000000000 --- a/src/types/presets.ts +++ /dev/null @@ -1,185 +0,0 @@ -import type { HttpsOptions } from "firebase-functions/v2/https"; -import type { RuntimeOptions, region } from "firebase-functions"; -import type { CloudflarePagesRoutes } from "../presets/cloudflare-pages"; - -/** - * Vercel Build Output Configuration - * @see https://vercel.com/docs/build-output-api/v3 - */ -export interface VercelBuildConfigV3 { - version: 3; - routes?: ( - | { - src: string; - headers: { - "cache-control": string; - }; - continue: boolean; - } - | { - handle: string; - } - | { - src: string; - dest: string; - } - )[]; - images?: { - sizes: number[]; - domains: string[]; - remotePatterns?: { - protocol?: "http" | "https"; - hostname: string; - port?: string; - pathname?: string; - }[]; - minimumCacheTTL?: number; - formats?: ("image/avif" | "image/webp")[]; - dangerouslyAllowSVG?: boolean; - contentSecurityPolicy?: string; - }; - wildcard?: Array<{ - domain: string; - value: string; - }>; - overrides?: Record< - string, - { - path?: string; - contentType?: string; - } - >; - cache?: string[]; - bypassToken?: string; - crons?: { - path: string; - schedule: string; - }[]; -} - -interface FirebaseOptionsBase { - gen: 1 | 2; - /** - * Firebase functions node runtime version. - * @see https://cloud.google.com/functions/docs/concepts/nodejs-runtime - */ - nodeVersion?: "20" | "18" | "16"; - /** - * When deploying multiple apps within the same Firebase project - * you must give your server a unique name in order to avoid overwriting your functions. - * - * @default "server" - */ - serverFunctionName?: string; -} - -interface FirebaseOptionsGen1 extends FirebaseOptionsBase { - gen: 1; - /** - * Firebase functions 1st generation region passed to `functions.region()`. - */ - region?: Parameters[0]; - /** - * Firebase functions 1st generation runtime options passed to `functions.runWith()`. - */ - runtimeOptions?: RuntimeOptions; -} - -interface FirebaseOptionsGen2 extends FirebaseOptionsBase { - gen: 2; - /** - * Firebase functions 2nd generation https options passed to `onRequest`. - * @see https://firebase.google.com/docs/reference/functions/2nd-gen/node/firebase-functions.https.httpsoptions - */ - httpsOptions?: HttpsOptions; -} - -type FirebaseOptions = FirebaseOptionsGen1 | FirebaseOptionsGen2; - -/** - * https://vercel.com/docs/build-output-api/v3/primitives#serverless-function-configuration - */ -export interface VercelServerlessFunctionConfig { - /** - * Amount of memory (RAM in MB) that will be allocated to the Serverless Function. - */ - memory?: number; - - /** - * Maximum execution duration (in seconds) that will be allowed for the Serverless Function. - */ - maxDuration?: number; - - /** - * True if a custom runtime has support for Lambda runtime wrappers. - */ - supportsWrapper?: boolean; - - /** - * When true, the Serverless Function will stream the response to the client. - */ - supportsResponseStreaming?: boolean; - - [key: string]: unknown; -} - -interface AzureOptions { - config?: { - platform?: { - apiRuntime?: string; - [key: string]: unknown; - }; - navigationFallback?: { - rewrite?: string; - [key: string]: unknown; - }; - [key: string]: unknown; - }; -} - -export interface PresetOptions { - vercel: { - config: VercelBuildConfigV3; - - /** - * If you are using `vercel-edge`, you can specify the region(s) for your edge function. - * @see https://vercel.com/docs/concepts/functions/edge-functions#edge-function-regions - */ - regions?: string[]; - - functions?: VercelServerlessFunctionConfig; - }; - firebase: FirebaseOptions; - cloudflare: { - pages: { - /** - * Nitro will automatically generate a `_routes.json` that controls which files get served statically and - * which get served by the Worker. Using this config will override the automatic `_routes.json`. Or, if the - * `merge` options is set, it will merge the user-set routes with the auto-generated ones, giving priority - * to the user routes. - * - * @see https://developers.cloudflare.com/pages/platform/functions/routing/#functions-invocation-routes - * - * There are a maximum of 100 rules, and you must have at least one include rule. Wildcards are accepted. - * - * If any fields are unset, they default to: - * - * ```json - * { - * "version": 1, - * "include": ["/*"], - * "exclude": [] - * } - * ``` - */ - routes?: CloudflarePagesRoutes; - /** - * If set to `false`, nitro will disable the automatically generated `_routes.json` and instead use the user-set only ones. - * - * @default true - */ - defaultRoutes?: boolean; - }; - }; - azure: AzureOptions; -} diff --git a/src/types/presets/azure.ts b/src/types/presets/azure.ts new file mode 100644 index 0000000000..0b01a5ff41 --- /dev/null +++ b/src/types/presets/azure.ts @@ -0,0 +1,13 @@ +export interface AzureOptions { + config?: { + platform?: { + apiRuntime?: string; + [key: string]: unknown; + }; + navigationFallback?: { + rewrite?: string; + [key: string]: unknown; + }; + [key: string]: unknown; + }; +} diff --git a/src/types/presets/cloudflare.ts b/src/types/presets/cloudflare.ts new file mode 100644 index 0000000000..edd5d6dd31 --- /dev/null +++ b/src/types/presets/cloudflare.ts @@ -0,0 +1,33 @@ +import type { CloudflarePagesRoutes } from "../../presets/cloudflare-pages"; + +export interface CloudflareOptions { + pages: { + /** + * Nitro will automatically generate a `_routes.json` that controls which files get served statically and + * which get served by the Worker. Using this config will override the automatic `_routes.json`. Or, if the + * `merge` options is set, it will merge the user-set routes with the auto-generated ones, giving priority + * to the user routes. + * + * @see https://developers.cloudflare.com/pages/platform/functions/routing/#functions-invocation-routes + * + * There are a maximum of 100 rules, and you must have at least one include rule. Wildcards are accepted. + * + * If any fields are unset, they default to: + * + * ```json + * { + * "version": 1, + * "include": ["/*"], + * "exclude": [] + * } + * ``` + */ + routes?: CloudflarePagesRoutes; + /** + * If set to `false`, nitro will disable the automatically generated `_routes.json` and instead use the user-set only ones. + * + * @default true + */ + defaultRoutes?: boolean; + }; +} diff --git a/src/types/presets/firebase.ts b/src/types/presets/firebase.ts new file mode 100644 index 0000000000..722331b946 --- /dev/null +++ b/src/types/presets/firebase.ts @@ -0,0 +1,41 @@ +import type { HttpsOptions } from "firebase-functions/v2/https"; +import type { RuntimeOptions, region } from "firebase-functions"; + +export type FirebaseOptions = FirebaseOptionsGen1 | FirebaseOptionsGen2; + +export interface FirebaseOptionsBase { + gen: 1 | 2; + /** + * Firebase functions node runtime version. + * @see https://cloud.google.com/functions/docs/concepts/nodejs-runtime + */ + nodeVersion?: "20" | "18" | "16"; + /** + * When deploying multiple apps within the same Firebase project + * you must give your server a unique name in order to avoid overwriting your functions. + * + * @default "server" + */ + serverFunctionName?: string; +} + +export interface FirebaseOptionsGen1 extends FirebaseOptionsBase { + gen: 1; + /** + * Firebase functions 1st generation region passed to `functions.region()`. + */ + region?: Parameters[0]; + /** + * Firebase functions 1st generation runtime options passed to `functions.runWith()`. + */ + runtimeOptions?: RuntimeOptions; +} + +export interface FirebaseOptionsGen2 extends FirebaseOptionsBase { + gen: 2; + /** + * Firebase functions 2nd generation https options passed to `onRequest`. + * @see https://firebase.google.com/docs/reference/functions/2nd-gen/node/firebase-functions.https.httpsoptions + */ + httpsOptions?: HttpsOptions; +} diff --git a/src/types/presets/index.ts b/src/types/presets/index.ts new file mode 100644 index 0000000000..0894b379b6 --- /dev/null +++ b/src/types/presets/index.ts @@ -0,0 +1,11 @@ +import { AzureOptions } from "./azure"; +import { CloudflareOptions } from "./cloudflare"; +import { FirebaseOptions } from "./firebase"; +import { VercelOptions } from "./vercel"; + +export interface PresetOptions { + azure: AzureOptions; + cloudflare: CloudflareOptions; + firebase: FirebaseOptions; + vercel: VercelOptions; +} diff --git a/src/types/presets/vercel.ts b/src/types/presets/vercel.ts new file mode 100644 index 0000000000..0ca3a22cf1 --- /dev/null +++ b/src/types/presets/vercel.ts @@ -0,0 +1,93 @@ +/** + * Vercel Build Output Configuration + * @see https://vercel.com/docs/build-output-api/v3 + */ +export interface VercelBuildConfigV3 { + version: 3; + routes?: ( + | { + src: string; + headers: { + "cache-control": string; + }; + continue: boolean; + } + | { + handle: string; + } + | { + src: string; + dest: string; + } + )[]; + images?: { + sizes: number[]; + domains: string[]; + remotePatterns?: { + protocol?: "http" | "https"; + hostname: string; + port?: string; + pathname?: string; + }[]; + minimumCacheTTL?: number; + formats?: ("image/avif" | "image/webp")[]; + dangerouslyAllowSVG?: boolean; + contentSecurityPolicy?: string; + }; + wildcard?: Array<{ + domain: string; + value: string; + }>; + overrides?: Record< + string, + { + path?: string; + contentType?: string; + } + >; + cache?: string[]; + bypassToken?: string; + crons?: { + path: string; + schedule: string; + }[]; +} + +/** + * https://vercel.com/docs/build-output-api/v3/primitives#serverless-function-configuration + */ +export interface VercelServerlessFunctionConfig { + /** + * Amount of memory (RAM in MB) that will be allocated to the Serverless Function. + */ + memory?: number; + + /** + * Maximum execution duration (in seconds) that will be allowed for the Serverless Function. + */ + maxDuration?: number; + + /** + * True if a custom runtime has support for Lambda runtime wrappers. + */ + supportsWrapper?: boolean; + + /** + * When true, the Serverless Function will stream the response to the client. + */ + supportsResponseStreaming?: boolean; + + [key: string]: unknown; +} + +export interface VercelOptions { + config: VercelBuildConfigV3; + + /** + * If you are using `vercel-edge`, you can specify the region(s) for your edge function. + * @see https://vercel.com/docs/concepts/functions/edge-functions#edge-function-regions + */ + regions?: string[]; + + functions?: VercelServerlessFunctionConfig; +} From 9a1da3d50198b8353a7e75f071f4b910f1a53c61 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 14 Nov 2023 03:49:37 +0100 Subject: [PATCH 2/2] small refactors --- src/presets/cloudflare-pages.ts | 15 +-------------- src/presets/vercel.ts | 2 +- src/types/presets/cloudflare.ts | 14 +++++++++++++- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/presets/cloudflare-pages.ts b/src/presets/cloudflare-pages.ts index 0448d7994e..be1e843f17 100644 --- a/src/presets/cloudflare-pages.ts +++ b/src/presets/cloudflare-pages.ts @@ -9,6 +9,7 @@ import { import { globby } from "globby"; import { defineNitroPreset } from "../preset"; import type { Nitro } from "../types"; +import { CloudflarePagesRoutes } from "../types/presets/cloudflare"; export const cloudflarePages = defineNitroPreset({ extends: "cloudflare", @@ -59,20 +60,6 @@ export const cloudflarePagesStatic = defineNitroPreset({ }, }); -/** - * https://developers.cloudflare.com/pages/platform/functions/routing/#functions-invocation-routes - */ -export interface CloudflarePagesRoutes { - /** Defines the version of the schema. Currently there is only one version of the schema (version 1), however, we may add more in the future and aim to be backwards compatible. */ - version?: 1; - - /** Defines routes that will be invoked by Functions. Accepts wildcard behavior. */ - include?: string[]; - - /** Defines routes that will not be invoked by Functions. Accepts wildcard behavior. `exclude` always take priority over `include`. */ - exclude?: string[]; -} - async function writeCFRoutes(nitro: Nitro) { const _cfPagesConfig = nitro.options.cloudflare?.pages || {}; const routes: CloudflarePagesRoutes = { diff --git a/src/presets/vercel.ts b/src/presets/vercel.ts index 0d7bab7fd3..7863464815 100644 --- a/src/presets/vercel.ts +++ b/src/presets/vercel.ts @@ -8,7 +8,7 @@ import type { Nitro } from "../types"; import type { VercelBuildConfigV3, VercelServerlessFunctionConfig, -} from "../types/presets"; +} from "../types/presets/vercel"; // https://vercel.com/docs/build-output-api/v3 diff --git a/src/types/presets/cloudflare.ts b/src/types/presets/cloudflare.ts index edd5d6dd31..984ed2d573 100644 --- a/src/types/presets/cloudflare.ts +++ b/src/types/presets/cloudflare.ts @@ -1,4 +1,16 @@ -import type { CloudflarePagesRoutes } from "../../presets/cloudflare-pages"; +/** + * https://developers.cloudflare.com/pages/platform/functions/routing/#functions-invocation-routes + */ +export interface CloudflarePagesRoutes { + /** Defines the version of the schema. Currently there is only one version of the schema (version 1), however, we may add more in the future and aim to be backwards compatible. */ + version?: 1; + + /** Defines routes that will be invoked by Functions. Accepts wildcard behavior. */ + include?: string[]; + + /** Defines routes that will not be invoked by Functions. Accepts wildcard behavior. `exclude` always take priority over `include`. */ + exclude?: string[]; +} export interface CloudflareOptions { pages: {