Skip to content

Commit 2826006

Browse files
authored
Restructure + nodenext (#140)
* add pre-commit * activate lint in gh-workflow * add 'use strict' directive * fix standard lint issue * move types to types folder * nodenext compatibility
1 parent 03e584b commit 2826006

File tree

10 files changed

+76
-47
lines changed

10 files changed

+76
-47
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ jobs:
1414
test:
1515
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
1616
with:
17+
lint: true
1718
license-check: true

index.d.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const isCompressedDefault = (res) => {
24
const contentEncoding = res.headers['content-encoding'] || res.headers['Content-Encoding']
35
return contentEncoding && contentEncoding !== 'identity'
@@ -91,7 +93,7 @@ module.exports = (app, options) => {
9193

9294
// API gateway v2 cookies: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
9395
if (event.cookies && event.cookies.length) {
94-
headers['cookie'] = event.cookies.join(';')
96+
headers.cookie = event.cookies.join(';')
9597
}
9698

9799
const prom = new Promise((resolve) => {
@@ -149,3 +151,5 @@ module.exports = (app, options) => {
149151
return prom
150152
}
151153
}
154+
module.exports.default = module.exports
155+
module.exports.awsLambdaFastify = module.exports

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@
2828
"license": "MIT",
2929
"version": "3.1.3",
3030
"main": "index.js",
31+
"types": "types/index.d.ts",
3132
"scripts": {
3233
"lint": "eslint .",
33-
"test": "npm run lint && tap -J -R specy --no-coverage test/*test.js && npm run test:typescript",
34+
"test": "npm run test:unit && npm run test:typescript",
35+
"test:unit": "tap -J -R specy --no-coverage test/*test.js",
3436
"test:typescript": "tsd",
3537
"performance": "npm run lint && node performanceTest/test"
3638
},
3739
"devDependencies": {
3840
"@fastify/multipart": "7.3.0",
41+
"@fastify/pre-commit": "^2.0.2",
3942
"@types/aws-lambda": "8.10.109",
4043
"aws-lambda": "^1.0.7",
4144
"aws-serverless-express": "^3.4.0",
@@ -59,5 +62,9 @@
5962
},
6063
"publishConfig": {
6164
"access": "public"
62-
}
65+
},
66+
"pre-commit": [
67+
"lint",
68+
"test"
69+
]
6370
}

performanceTest/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const Benchmark = require('benchmark')
24
const suite = new Benchmark.Suite()
35

@@ -69,6 +71,6 @@ suite
6971
})
7072
.on('complete', function () {
7173
console.log('Fastest is ' + this.filter('fastest').map('name'))
72-
process.exit(0)
74+
process.exit(0) // eslint-disable-line no-process-exit
7375
})
7476
.run({ async: true })

test/alb.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const { test } = require('tap')
24
const fastify = require('fastify')
35
const awsLambdaFastify = require('../index')

test/basic.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const { promisify } = require('util')
24
const { test } = require('tap')
35
const fastify = require('fastify')
@@ -20,7 +22,7 @@ test('GET', async (t) => {
2022
}
2123
app.get('/test', async (request, reply) => {
2224
t.equal(request.headers['x-my-header'], 'wuuusaaa')
23-
t.equal(request.headers['cookie'], 'foo=bar')
25+
t.equal(request.headers.cookie, 'foo=bar')
2426
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')
2527
t.equal(request.awsLambda.event, evt)
2628
t.equal(request.headers['user-agent'], 'lightMyRequest')
@@ -405,7 +407,7 @@ test('with existing onRequest hook', async (t) => {
405407
})
406408
app.get('/test', async (request, reply) => {
407409
t.equal(request.headers['x-my-header'], 'wuuusaaa')
408-
t.equal(request.headers['cookie'], 'foo=bar')
410+
t.equal(request.headers.cookie, 'foo=bar')
409411
t.equal(request.awsLambda.event, evt)
410412
t.equal(request.headers['user-agent'], 'lightMyRequest')
411413
t.equal(request.headers.host, 'localhost:80')

test/multipart.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const { test } = require('tap')
24
const fastify = require('fastify')
35
const awsLambdaFastify = require('../index')

types/index.d.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Context } from "aws-lambda";
2+
import { FastifyInstance, LightMyRequestResponse } from "fastify";
3+
4+
type AwsLambdaFastify = typeof awsLambdaFastify
5+
6+
declare namespace awsLambdaFastify {
7+
export interface LambdaFastifyOptions {
8+
binaryMimeTypes?: string[];
9+
callbackWaitsForEmptyEventLoop?: boolean;
10+
serializeLambdaArguments?: boolean;
11+
decorateRequest?: boolean;
12+
decorationPropertyName?: string;
13+
enforceBase64?: (response: LightMyRequestResponse) => boolean;
14+
}
15+
16+
export interface LambdaResponse {
17+
statusCode: number;
18+
body: string;
19+
headers: Record<string, string>;
20+
isBase64Encoded: boolean;
21+
cookies?: string[]
22+
}
23+
24+
export type PromiseHandler<TEvent = any, TResult = LambdaResponse> = (
25+
event: TEvent,
26+
context: Context
27+
) => Promise<TResult>;
28+
29+
export type CallbackHandler<TEvent = any, TResult = LambdaResponse> = (
30+
event: TEvent,
31+
context: Context,
32+
callback: (err?: Error, result?: TResult) => void
33+
) => void;
34+
35+
export const awsLambdaFastify: AwsLambdaFastify
36+
export { awsLambdaFastify as default }
37+
}
38+
39+
declare function awsLambdaFastify<TEvent, TResult = awsLambdaFastify.LambdaResponse>(
40+
app: FastifyInstance,
41+
options?: awsLambdaFastify.LambdaFastifyOptions
42+
): awsLambdaFastify.PromiseHandler<TEvent, TResult>;
43+
44+
declare function awsLambdaFastify<TEvent, TResult = awsLambdaFastify.LambdaResponse>(
45+
app: FastifyInstance,
46+
options?: awsLambdaFastify.LambdaFastifyOptions
47+
): awsLambdaFastify.CallbackHandler<TEvent, TResult>;
48+
49+
export = awsLambdaFastify

index.test-d.ts renamed to types/index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import awsLambdaFastify, {
44
CallbackHandler,
55
LambdaFastifyOptions,
66
LambdaResponse,
7-
} from ".";
7+
} from "..";
88

99
import { expectType, expectError, expectAssignable } from "tsd";
1010

0 commit comments

Comments
 (0)