Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(echo): add SvelteKit serve function #5650

Merged
merged 7 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/echo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@
"require": "./dist/h3.js",
"import": "./dist/h3.mjs",
"types": "./dist/h3.d.ts"
},
"./sveltekit": {
"require": "./dist/sveltekit.js",
"import": "./dist/sveltekit.mjs",
"types": "./dist/sveltekit.d.ts"
}
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@sveltejs/kit": "^2.5.10",
"@types/express": "^4.17.13",
"@types/jest": "^29.5.12",
"@types/json-schema": "^7.0.15",
Expand Down
62 changes: 62 additions & 0 deletions packages/echo/src/sveltekit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { RequestEvent } from '@sveltejs/kit';
import { EchoRequestHandler, ServeHandlerOptions } from './handler';
import { type SupportedFrameworkName } from './types';

export const frameworkName: SupportedFrameworkName = 'sveltekit';

export const serve = (
options: ServeHandlerOptions
): ((event: RequestEvent) => Promise<Response>) & {
GET: (event: RequestEvent) => Promise<Response>;
POST: (event: RequestEvent) => Promise<Response>;
PUT: (event: RequestEvent) => Promise<Response>;
} => {
const handler = new EchoRequestHandler({
frameworkName,
...options,
handler: (reqMethod: 'GET' | 'POST' | 'PUT' | undefined, event: RequestEvent) => {
return {
method: () => reqMethod || event.request.method || '',
body: () => event.request.json(),
headers: (key) => event.request.headers.get(key),
url: () => {
const protocol = process.env.NODE_ENV === 'development' ? 'http' : 'https';

return new URL(event.request.url, `${protocol}://${event.request.headers.get('host') || ''}`);
},
transformResponse: ({ body, headers, status }) => {
// 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 });
},
};
},
});

const baseFn = handler.createHandler();

const fn = baseFn.bind(null, undefined);
type Fn = typeof fn;

const handlerFn = Object.defineProperties(fn, {
GET: { value: baseFn.bind(null, 'GET') },
POST: { value: baseFn.bind(null, 'POST') },
PUT: { value: baseFn.bind(null, 'PUT') },
}) as Fn & {
GET: Fn;
POST: Fn;
PUT: Fn;
};

return handlerFn;
};
2 changes: 1 addition & 1 deletion packages/echo/src/types/framework.types.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type SupportedFrameworkName = 'next' | 'express' | 'nuxt' | 'h3';
export type SupportedFrameworkName = 'next' | 'express' | 'nuxt' | 'h3' | 'sveltekit';
2 changes: 1 addition & 1 deletion packages/echo/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'tsup';
import { type SupportedFrameworkName } from './src';

const frameworks: SupportedFrameworkName[] = ['h3', 'express', 'next', 'nuxt'];
const frameworks: SupportedFrameworkName[] = ['h3', 'express', 'next', 'nuxt', 'sveltekit'];

export default defineConfig({
entry: ['src/index.ts', ...frameworks.map((framework) => `src/${framework}.ts`)],
Expand Down
Loading
Loading