Skip to content

Commit

Permalink
Merge pull request #181 from H4ad/feat/polka
Browse files Browse the repository at this point in the history
feat(frameworks): added support for polka
  • Loading branch information
H4ad authored Jan 8, 2024
2 parents 55db4f4 + 39377cb commit a234af2
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 16 deletions.
69 changes: 69 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"lambda edge",
"alb",
"lambda",
"lambda streaming",
"response streaming",
"apollo server",
"express",
"koa",
Expand All @@ -69,7 +71,8 @@
"digital ocean functions",
"digital ocean serverless",
"gcp",
"google cloud functions"
"google cloud functions",
"polka"
],
"bugs": {
"url": "https://github.com/H4ad/serverless-adapter/issues"
Expand Down Expand Up @@ -97,6 +100,7 @@
"@types/express": "4.17.21",
"@types/koa": "2.13.12",
"@types/node": "18.16.5",
"@types/polka": "0.5.7",
"@types/supertest": "6.0.1",
"@typescript-eslint/eslint-plugin": "6.15.0",
"@typescript-eslint/parser": "6.15.0",
Expand All @@ -120,6 +124,7 @@
"glob": "10.3.10",
"husky": "8.0.3",
"koa": "2.15.0",
"polka": "0.5.2",
"prettier": "3.1.1",
"stream-mock": "2.0.5",
"supertest": "6.3.3",
Expand Down Expand Up @@ -530,6 +535,26 @@
"default": "./lib/frameworks/lazy/index.cjs"
}
},
"./frameworks/polka": {
"import": {
"types": "./lib/frameworks/polka/index.d.ts",
"default": "./lib/frameworks/polka/index.mjs"
},
"require": {
"types": "./lib/frameworks/polka/index.d.cts",
"default": "./lib/frameworks/polka/index.cjs"
}
},
"./lib/frameworks/polka": {
"import": {
"types": "./lib/frameworks/polka/index.d.ts",
"default": "./lib/frameworks/polka/index.mjs"
},
"require": {
"types": "./lib/frameworks/polka/index.d.cts",
"default": "./lib/frameworks/polka/index.cjs"
}
},
"./frameworks/trpc": {
"import": {
"types": "./lib/frameworks/trpc/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/frameworks/polka/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './polka.framework';
26 changes: 26 additions & 0 deletions src/frameworks/polka/polka.framework.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//#region Imports

import type { IncomingMessage, ServerResponse } from 'http';
import polka, { type Polka } from 'polka';
import type { FrameworkContract } from '../../contracts';

//#endregion

/**
* The framework that forwards requests to polka handler
*
* @breadcrumb Frameworks / PolkaFramework
* @public
*/
export class PolkaFramework implements FrameworkContract<Polka> {
/**
* {@inheritDoc}
*/
sendRequest(
app: Polka,
request: IncomingMessage,
response: ServerResponse<IncomingMessage>,
): void {
app.handler(request as polka.Request, response);
}
}
1 change: 1 addition & 0 deletions src/index.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from './frameworks/fastify';
export * from './frameworks/koa';
export * from './frameworks/hapi';
export * from './frameworks/lazy';
export * from './frameworks/polka';
export * from './frameworks/trpc';
export * from './handlers/azure';
export * from './handlers/aws';
Expand Down
49 changes: 41 additions & 8 deletions test/frameworks/body-parser.framework.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { ServerResponse } from 'http';
import * as trpc from '@trpc/server';
import type { Options } from 'body-parser';
import express, { type Express } from 'express';
import fastify from 'fastify';
import Application from 'koa';
import { SpyInstance, describe, expect, it, vitest } from 'vitest';
import { type SpyInstance, describe, expect, it, vitest } from 'vitest';
import polka from 'polka';
import {
FrameworkContract,
type FrameworkContract,
ServerlessRequest,
ServerlessResponse,
waitForStreamComplete,
} from '../../src';
import {
BodyParserOptions,
type BodyParserOptions,
JsonBodyParserFramework,
RawBodyParserFramework,
TextBodyParserFramework,
Expand All @@ -22,7 +22,11 @@ import { ExpressFramework } from '../../src/frameworks/express';
import { FastifyFramework } from '../../src/frameworks/fastify';
import { setNoOpForContentType } from '../../src/frameworks/fastify/helpers/no-op-content-parser';
import { KoaFramework } from '../../src/frameworks/koa';
import { TrpcAdapterContext, TrpcFramework } from '../../src/frameworks/trpc';
import {
type TrpcAdapterContext,
TrpcFramework,
} from '../../src/frameworks/trpc';
import { PolkaFramework } from '../../src/frameworks/polka';

type BodyParserTest = {
name: string;
Expand All @@ -35,7 +39,14 @@ type BodyParserTest = {
notExpectedBody?: any;
status: number;
expectSendRequestOfTheFrameworkToBeCalled: boolean;
skipFrameworks?: ('express' | 'fastify' | 'koa' | 'hapi' | 'trpc')[];
skipFrameworks?: (
| 'express'
| 'fastify'
| 'koa'
| 'hapi'
| 'trpc'
| 'polka'
)[];
};

const bodyParserOptions: BodyParserTest[] = [
Expand Down Expand Up @@ -259,7 +270,7 @@ describe('BodyParserFramework', () => {
res.send('ok');
});

app.use((err, __, res: ServerResponse, _) => {
app.use((err, __, res, _) => {
res.emit('error', err);
});

Expand Down Expand Up @@ -290,7 +301,7 @@ describe('BodyParserFramework', () => {
res.send('ok');
});

app.setErrorHandler((err, req, reply) => {
app.setErrorHandler((err, _req, reply) => {
reply.raw.emit('error', err);
});

Expand Down Expand Up @@ -387,6 +398,28 @@ describe('BodyParserFramework', () => {
}
});

describe('polka', () => {
for (const bodyParserTest of bodyParserOptions) {
const itFn = bodyParserTest?.skipFrameworks?.includes('polka')
? it.skip
: it;

itFn(bodyParserTest.name, async () => {
const app = polka();

app.post('/body', (req, res) => {
if (bodyParserTest.expectedBody)
expect(req.body).toEqual(bodyParserTest.expectedBody);
else expect(req.body).not.toEqual(bodyParserTest.notExpectedBody);

res.end('ok');
});

await handleRestExpects(app, new PolkaFramework(), bodyParserTest);
});
}
});

it('should handle correctly on wrong content-encoding', async () => {
const app = express();

Expand Down
25 changes: 21 additions & 4 deletions test/frameworks/cors.framework.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import * as trpc from '@trpc/server';
import express from 'express';
import fastify from 'fastify';
import Application from 'koa';
import { SpyInstance, describe, expect, it, vitest } from 'vitest';
import { type SpyInstance, describe, expect, it, vitest } from 'vitest';
import polka from 'polka';
import {
BothValueHeaders,
FrameworkContract,
type BothValueHeaders,
type FrameworkContract,
ServerlessRequest,
ServerlessResponse,
waitForStreamComplete,
} from '../../src';
import { CorsFramework, CorsFrameworkOptions } from '../../src/frameworks/cors';
import {
CorsFramework,
type CorsFrameworkOptions,
} from '../../src/frameworks/cors';
import { ExpressFramework } from '../../src/frameworks/express';
import { FastifyFramework } from '../../src/frameworks/fastify';
import { KoaFramework } from '../../src/frameworks/koa';
import { TrpcFramework } from '../../src/frameworks/trpc';
import { PolkaFramework } from '../../src/frameworks/polka';

type CorsTest = {
name: string;
Expand Down Expand Up @@ -287,4 +292,16 @@ describe('CorsFramework', () => {
});
}
});

describe('polka', () => {
for (const corsTest of corsOptions) {
it(`${corsTest.method}: ${corsTest.name}`, async () => {
const app = polka();

app.get('/', (_, res) => res.end('ok'));

await handleRestExpects(app, new PolkaFramework(), corsTest);
});
}
});
});
Loading

0 comments on commit a234af2

Please sign in to comment.