From fe793d189b0631234f6a3b73f194361ec63624c2 Mon Sep 17 00:00:00 2001 From: Richard Fontein <32132657+rifont@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:47:28 +0100 Subject: [PATCH] feat(echo): add support for Remix framework (#5666) --- packages/echo/package.json | 5 +++ packages/echo/src/remix.ts | 41 ++++++++++++++++++++++ packages/echo/src/types/framework.types.ts | 2 +- packages/echo/tsup.config.ts | 2 +- 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 packages/echo/src/remix.ts diff --git a/packages/echo/package.json b/packages/echo/package.json index bb4e8b4f3cc..90f3b7130b3 100644 --- a/packages/echo/package.json +++ b/packages/echo/package.json @@ -71,6 +71,11 @@ "require": "./dist/sveltekit.js", "import": "./dist/sveltekit.mjs", "types": "./dist/sveltekit.d.ts" + }, + "./remix": { + "require": "./dist/remix.js", + "import": "./dist/remix.mjs", + "types": "./dist/remix.d.ts" } }, "devDependencies": { diff --git a/packages/echo/src/remix.ts b/packages/echo/src/remix.ts new file mode 100644 index 00000000000..ec15aa822b4 --- /dev/null +++ b/packages/echo/src/remix.ts @@ -0,0 +1,41 @@ +import { EchoRequestHandler, ServeHandlerOptions } from './handler'; +import { type SupportedFrameworkName } from './types'; + +export const frameworkName: SupportedFrameworkName = 'remix'; + +export const serve = ( + options: ServeHandlerOptions +): ((ctx: { request: Request; context?: unknown }) => Promise) => { + const handler = new EchoRequestHandler({ + frameworkName, + ...options, + handler: ({ request: req }: { request: Request }) => { + return { + body: () => req.json(), + headers: (key) => req.headers.get(key), + method: () => req.method, + url: () => new URL(req.url, `https://${req.headers.get('host') || ''}`), + transformResponse: ({ body, status, headers }): Response => { + // Handle Response polyfills + // eslint-disable-next-line @typescript-eslint/naming-convention + let Res: typeof Response; + + if (typeof Response === 'undefined') { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-var-requires + Res = require('cross-fetch').Response; + } else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + Res = Response; + } + + return new Res(body, { + status, + headers, + }); + }, + }; + }, + }); + + return handler.createHandler(); +}; diff --git a/packages/echo/src/types/framework.types.ts b/packages/echo/src/types/framework.types.ts index 02ecd79d16c..9a1efae97f9 100644 --- a/packages/echo/src/types/framework.types.ts +++ b/packages/echo/src/types/framework.types.ts @@ -1 +1 @@ -export type SupportedFrameworkName = 'next' | 'express' | 'nuxt' | 'h3' | 'sveltekit'; +export type SupportedFrameworkName = 'next' | 'express' | 'nuxt' | 'h3' | 'sveltekit' | 'remix'; diff --git a/packages/echo/tsup.config.ts b/packages/echo/tsup.config.ts index a3d60b1396c..bb5c1827e51 100644 --- a/packages/echo/tsup.config.ts +++ b/packages/echo/tsup.config.ts @@ -1,7 +1,7 @@ import { defineConfig } from 'tsup'; import { type SupportedFrameworkName } from './src'; -const frameworks: SupportedFrameworkName[] = ['h3', 'express', 'next', 'nuxt', 'sveltekit']; +const frameworks: SupportedFrameworkName[] = ['h3', 'express', 'next', 'nuxt', 'sveltekit', 'remix']; export default defineConfig({ entry: ['src/index.ts', ...frameworks.map((framework) => `src/${framework}.ts`)],