diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 325921f..e58887a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,4 +14,5 @@ jobs: test: uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3 with: + lint: true license-check: true diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index ef59626..0000000 --- a/index.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { Context } from "aws-lambda"; -import { FastifyInstance, LightMyRequestResponse } from "fastify"; - -export interface LambdaFastifyOptions { - binaryMimeTypes?: string[]; - callbackWaitsForEmptyEventLoop?: boolean; - serializeLambdaArguments?: boolean; - decorateRequest?: boolean; - decorationPropertyName?: string; - enforceBase64?: (response: LightMyRequestResponse) => boolean; -} - -export interface LambdaResponse { - statusCode: number; - body: string; - headers: Record; - isBase64Encoded: boolean; - cookies?: string[] -} - -export type PromiseHandler = ( - event: TEvent, - context: Context -) => Promise; - -export type CallbackHandler = ( - event: TEvent, - context: Context, - callback: (err?: Error, result?: TResult) => void -) => void; - -export default function awsLambdaFastify( - app: FastifyInstance, - options?: LambdaFastifyOptions -): PromiseHandler; - -export default function awsLambdaFastify( - app: FastifyInstance, - options?: LambdaFastifyOptions -): CallbackHandler; diff --git a/index.js b/index.js index 4c9e026..3a8648a 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +'use strict' + const isCompressedDefault = (res) => { const contentEncoding = res.headers['content-encoding'] || res.headers['Content-Encoding'] return contentEncoding && contentEncoding !== 'identity' @@ -91,7 +93,7 @@ module.exports = (app, options) => { // API gateway v2 cookies: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html if (event.cookies && event.cookies.length) { - headers['cookie'] = event.cookies.join(';') + headers.cookie = event.cookies.join(';') } const prom = new Promise((resolve) => { @@ -149,3 +151,5 @@ module.exports = (app, options) => { return prom } } +module.exports.default = module.exports +module.exports.awsLambdaFastify = module.exports diff --git a/package.json b/package.json index 9663293..407d538 100644 --- a/package.json +++ b/package.json @@ -28,14 +28,17 @@ "license": "MIT", "version": "3.1.3", "main": "index.js", + "types": "types/index.d.ts", "scripts": { "lint": "eslint .", - "test": "npm run lint && tap -J -R specy --no-coverage test/*test.js && npm run test:typescript", + "test": "npm run test:unit && npm run test:typescript", + "test:unit": "tap -J -R specy --no-coverage test/*test.js", "test:typescript": "tsd", "performance": "npm run lint && node performanceTest/test" }, "devDependencies": { "@fastify/multipart": "7.3.0", + "@fastify/pre-commit": "^2.0.2", "@types/aws-lambda": "8.10.109", "aws-lambda": "^1.0.7", "aws-serverless-express": "^3.4.0", @@ -59,5 +62,9 @@ }, "publishConfig": { "access": "public" - } + }, + "pre-commit": [ + "lint", + "test" + ] } diff --git a/performanceTest/test.js b/performanceTest/test.js index 633b57f..7a24ed8 100644 --- a/performanceTest/test.js +++ b/performanceTest/test.js @@ -1,3 +1,5 @@ +'use strict' + const Benchmark = require('benchmark') const suite = new Benchmark.Suite() @@ -69,6 +71,6 @@ suite }) .on('complete', function () { console.log('Fastest is ' + this.filter('fastest').map('name')) - process.exit(0) + process.exit(0) // eslint-disable-line no-process-exit }) .run({ async: true }) diff --git a/test/alb.test.js b/test/alb.test.js index 929460f..4444399 100644 --- a/test/alb.test.js +++ b/test/alb.test.js @@ -1,3 +1,5 @@ +'use strict' + const { test } = require('tap') const fastify = require('fastify') const awsLambdaFastify = require('../index') diff --git a/test/basic.test.js b/test/basic.test.js index ce3e018..fb6902c 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,3 +1,5 @@ +'use strict' + const { promisify } = require('util') const { test } = require('tap') const fastify = require('fastify') @@ -20,7 +22,7 @@ test('GET', async (t) => { } app.get('/test', async (request, reply) => { t.equal(request.headers['x-my-header'], 'wuuusaaa') - t.equal(request.headers['cookie'], 'foo=bar') + t.equal(request.headers.cookie, 'foo=bar') t.equal(request.headers['x-apigateway-event'], '%7B%22version%22%3A%222.0%22%2C%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%7D%2C%22cookies%22%3A%5B%22foo%3Dbar%22%5D%2C%22queryStringParameters%22%3A%22%22%7D') t.equal(request.awsLambda.event, evt) t.equal(request.headers['user-agent'], 'lightMyRequest') @@ -405,7 +407,7 @@ test('with existing onRequest hook', async (t) => { }) app.get('/test', async (request, reply) => { t.equal(request.headers['x-my-header'], 'wuuusaaa') - t.equal(request.headers['cookie'], 'foo=bar') + t.equal(request.headers.cookie, 'foo=bar') t.equal(request.awsLambda.event, evt) t.equal(request.headers['user-agent'], 'lightMyRequest') t.equal(request.headers.host, 'localhost:80') diff --git a/test/multipart.test.js b/test/multipart.test.js index 3feee46..a1810dd 100644 --- a/test/multipart.test.js +++ b/test/multipart.test.js @@ -1,3 +1,5 @@ +'use strict' + const { test } = require('tap') const fastify = require('fastify') const awsLambdaFastify = require('../index') diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..3be6310 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,49 @@ +import { Context } from "aws-lambda"; +import { FastifyInstance, LightMyRequestResponse } from "fastify"; + +type AwsLambdaFastify = typeof awsLambdaFastify + +declare namespace awsLambdaFastify { + export interface LambdaFastifyOptions { + binaryMimeTypes?: string[]; + callbackWaitsForEmptyEventLoop?: boolean; + serializeLambdaArguments?: boolean; + decorateRequest?: boolean; + decorationPropertyName?: string; + enforceBase64?: (response: LightMyRequestResponse) => boolean; + } + + export interface LambdaResponse { + statusCode: number; + body: string; + headers: Record; + isBase64Encoded: boolean; + cookies?: string[] + } + + export type PromiseHandler = ( + event: TEvent, + context: Context + ) => Promise; + + export type CallbackHandler = ( + event: TEvent, + context: Context, + callback: (err?: Error, result?: TResult) => void + ) => void; + + export const awsLambdaFastify: AwsLambdaFastify + export { awsLambdaFastify as default } +} + +declare function awsLambdaFastify( + app: FastifyInstance, + options?: awsLambdaFastify.LambdaFastifyOptions +): awsLambdaFastify.PromiseHandler; + +declare function awsLambdaFastify( + app: FastifyInstance, + options?: awsLambdaFastify.LambdaFastifyOptions +): awsLambdaFastify.CallbackHandler; + +export = awsLambdaFastify diff --git a/index.test-d.ts b/types/index.test-d.ts similarity index 99% rename from index.test-d.ts rename to types/index.test-d.ts index f70bc61..f074417 100644 --- a/index.test-d.ts +++ b/types/index.test-d.ts @@ -4,7 +4,7 @@ import awsLambdaFastify, { CallbackHandler, LambdaFastifyOptions, LambdaResponse, -} from "."; +} from ".."; import { expectType, expectError, expectAssignable } from "tsd";