Skip to content

Commit

Permalink
feat(sveltekit): add support for sveltekit framework
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont committed Jun 3, 2024
1 parent 4fb8d47 commit af5dbe2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
75 changes: 51 additions & 24 deletions packages/echo/src/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,59 @@ import { type SupportedFrameworkName } from './types';

export const frameworkName: SupportedFrameworkName = 'sveltekit';

export const serve = (options: ServeHandlerOptions): any => {
const echoHandler = new EchoRequestHandler({
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: (incomingRequest: RequestEvent, response: Response) => ({
body: () => incomingRequest.request.body,
headers: (key) => {
const header = incomingRequest.request.headers[key];

return Array.isArray(header) ? header[0] : header;
},
method: () => incomingRequest.request.method || 'GET',
url: () => incomingRequest.url,
queryString: (key) => {
const qs = incomingRequest.url.searchParams.get(key);

return Array.isArray(qs) ? qs[0] : qs;
},
transformResponse: ({ body, headers, status }) => {
return new Response(JSON.stringify(body), {
status,
headers,
});
},
}),
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 polyfilling

Check warning on line 28 in packages/echo/src/sveltekit.ts

View workflow job for this annotation

GitHub Actions / Spell check

Unknown word (polyfilling)
// 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 echoHandler.createHandler();
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/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

0 comments on commit af5dbe2

Please sign in to comment.