From d0a2129effaa820e2753ca38538e98c551b0d028 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Mon, 10 Jul 2023 12:00:56 +0200 Subject: [PATCH 1/5] feat: export v2 api --- package-lock.json | 14 ++++++++++++++ package.json | 1 + src/function/index.ts | 1 + src/function/v2.ts | 1 + 4 files changed, 17 insertions(+) create mode 100644 src/function/v2.ts diff --git a/package-lock.json b/package-lock.json index c8790262..22e37f92 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.6.0", "license": "MIT", "dependencies": { + "@netlify/serverless-functions-api": "^1.5.1", "is-promise": "^4.0.0" }, "devDependencies": { @@ -1366,6 +1367,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/@netlify/serverless-functions-api": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/@netlify/serverless-functions-api/-/serverless-functions-api-1.5.1.tgz", + "integrity": "sha512-c8RjIu+fkpfOGLaW0Z0iFG39GUK7sCa6M3Qy3fqouMpT00TaiheiyssnnDZGSLkNp4o+UtbRnITB1zuwDeHMLw==", + "engines": { + "node": "^14.18.0 || >=16.0.0" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -12219,6 +12228,11 @@ } } }, + "@netlify/serverless-functions-api": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/@netlify/serverless-functions-api/-/serverless-functions-api-1.5.1.tgz", + "integrity": "sha512-c8RjIu+fkpfOGLaW0Z0iFG39GUK7sCa6M3Qy3fqouMpT00TaiheiyssnnDZGSLkNp4o+UtbRnITB1zuwDeHMLw==" + }, "@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", diff --git a/package.json b/package.json index 2725b556..b0f24a45 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "test": "test" }, "dependencies": { + "@netlify/serverless-functions-api": "^1.5.1", "is-promise": "^4.0.0" }, "devDependencies": { diff --git a/src/function/index.ts b/src/function/index.ts index 01e092da..8f203236 100644 --- a/src/function/index.ts +++ b/src/function/index.ts @@ -2,3 +2,4 @@ export { Context as HandlerContext } from './context.js' export { Event as HandlerEvent } from './event.js' export { BuilderHandler, Handler, BackgroundHandler, HandlerCallback, StreamingHandler } from './handler.js' export { BuilderResponse, Response as HandlerResponse, StreamingResponse } from './response.js' +export * from './v2.js' diff --git a/src/function/v2.ts b/src/function/v2.ts new file mode 100644 index 00000000..1fd30e2b --- /dev/null +++ b/src/function/v2.ts @@ -0,0 +1 @@ +export type { Context } from '@netlify/serverless-functions-api' From 423960cd91b4cbc880a61e2a7c21a338c3dd4d81 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Mon, 10 Jul 2023 14:08:19 +0200 Subject: [PATCH 2/5] fix: pin serverless-functions-api --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 22e37f92..27ebe349 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.6.0", "license": "MIT", "dependencies": { - "@netlify/serverless-functions-api": "^1.5.1", + "@netlify/serverless-functions-api": "1.5.1", "is-promise": "^4.0.0" }, "devDependencies": { diff --git a/package.json b/package.json index b0f24a45..3fb1f2c5 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "test": "test" }, "dependencies": { - "@netlify/serverless-functions-api": "^1.5.1", + "@netlify/serverless-functions-api": "1.5.1", "is-promise": "^4.0.0" }, "devDependencies": { From 868e6093d18b40c741514ce835848fb2df3ad3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Fri, 11 Aug 2023 09:56:51 +0100 Subject: [PATCH 3/5] feat: update @netlify/serverless-functions-api --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 27ebe349..c6f48441 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.6.0", "license": "MIT", "dependencies": { - "@netlify/serverless-functions-api": "1.5.1", + "@netlify/serverless-functions-api": "1.6.0", "is-promise": "^4.0.0" }, "devDependencies": { @@ -1368,9 +1368,9 @@ } }, "node_modules/@netlify/serverless-functions-api": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/@netlify/serverless-functions-api/-/serverless-functions-api-1.5.1.tgz", - "integrity": "sha512-c8RjIu+fkpfOGLaW0Z0iFG39GUK7sCa6M3Qy3fqouMpT00TaiheiyssnnDZGSLkNp4o+UtbRnITB1zuwDeHMLw==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@netlify/serverless-functions-api/-/serverless-functions-api-1.6.0.tgz", + "integrity": "sha512-Lr5mxLAvSZyJhigSc0zhvAuusNR6VdJNvOmsDkxIN6f9xzmRpWyAEecCGtBc+hoSZlIeLzI7oFcKhaTzXcO2JA==", "engines": { "node": "^14.18.0 || >=16.0.0" } @@ -12229,9 +12229,9 @@ } }, "@netlify/serverless-functions-api": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/@netlify/serverless-functions-api/-/serverless-functions-api-1.5.1.tgz", - "integrity": "sha512-c8RjIu+fkpfOGLaW0Z0iFG39GUK7sCa6M3Qy3fqouMpT00TaiheiyssnnDZGSLkNp4o+UtbRnITB1zuwDeHMLw==" + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@netlify/serverless-functions-api/-/serverless-functions-api-1.6.0.tgz", + "integrity": "sha512-Lr5mxLAvSZyJhigSc0zhvAuusNR6VdJNvOmsDkxIN6f9xzmRpWyAEecCGtBc+hoSZlIeLzI7oFcKhaTzXcO2JA==" }, "@nodelib/fs.scandir": { "version": "2.1.5", diff --git a/package.json b/package.json index 3fb1f2c5..818c74fe 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "test": "test" }, "dependencies": { - "@netlify/serverless-functions-api": "1.5.1", + "@netlify/serverless-functions-api": "1.6.0", "is-promise": "^4.0.0" }, "devDependencies": { From 80a5e53fc0bca63624416d0c77c07adde0bbc551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Fri, 11 Aug 2023 09:57:00 +0100 Subject: [PATCH 4/5] refactor: use named export --- src/function/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/function/index.ts b/src/function/index.ts index 8f203236..6feadc92 100644 --- a/src/function/index.ts +++ b/src/function/index.ts @@ -2,4 +2,4 @@ export { Context as HandlerContext } from './context.js' export { Event as HandlerEvent } from './event.js' export { BuilderHandler, Handler, BackgroundHandler, HandlerCallback, StreamingHandler } from './handler.js' export { BuilderResponse, Response as HandlerResponse, StreamingResponse } from './response.js' -export * from './v2.js' +export { Context } from './v2.js' From ad005481315a9a2eef2aa7a8442a8c5e3191faea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Fri, 11 Aug 2023 10:18:31 +0100 Subject: [PATCH 5/5] refactor: rename things --- src/function/handler.ts | 25 +++++++++++-------- .../{context.ts => handler_context.ts} | 2 +- src/function/{event.ts => handler_event.ts} | 2 +- .../{response.ts => handler_response.ts} | 6 ++--- src/function/index.ts | 6 ++--- src/lib/builder.ts | 4 +-- 6 files changed, 24 insertions(+), 21 deletions(-) rename src/function/{context.ts => handler_context.ts} (96%) rename src/function/{event.ts => handler_event.ts} (95%) rename src/function/{response.ts => handler_response.ts} (70%) diff --git a/src/function/handler.ts b/src/function/handler.ts index 39b45dec..50b167ab 100644 --- a/src/function/handler.ts +++ b/src/function/handler.ts @@ -1,23 +1,26 @@ -import type { Context } from './context.js' -import type { Event } from './event.js' -import type { Response, BuilderResponse, StreamingResponse } from './response.js' +import type { HandlerContext } from './handler_context.js' +import type { HandlerEvent } from './handler_event.js' +import type { HandlerResponse, BuilderResponse, StreamingResponse } from './handler_response.js' -export interface HandlerCallback { +export interface HandlerCallback { // eslint-disable-next-line @typescript-eslint/no-explicit-any (error: any, response: ResponseType): void } -export interface BaseHandler { - (event: Event, context: C, callback?: HandlerCallback): void | Promise +export interface BaseHandler< + ResponseType extends HandlerResponse = HandlerResponse, + C extends HandlerContext = HandlerContext, +> { + (event: HandlerEvent, context: C, callback?: HandlerCallback): void | Promise } -export interface BackgroundHandler { - (event: Event, context: C): void | Promise +export interface BackgroundHandler { + (event: HandlerEvent, context: C): void | Promise } -export type Handler = BaseHandler -export type BuilderHandler = BaseHandler +export type Handler = BaseHandler +export type BuilderHandler = BaseHandler export interface StreamingHandler { - (event: Event, context: Context): Promise + (event: HandlerEvent, context: HandlerContext): Promise } diff --git a/src/function/context.ts b/src/function/handler_context.ts similarity index 96% rename from src/function/context.ts rename to src/function/handler_context.ts index 3e155d3c..ed12c5b2 100644 --- a/src/function/context.ts +++ b/src/function/handler_context.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ // From https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html. -export interface Context { +export interface HandlerContext { callbackWaitsForEmptyEventLoop: boolean functionName: string functionVersion: string diff --git a/src/function/event.ts b/src/function/handler_event.ts similarity index 95% rename from src/function/event.ts rename to src/function/handler_event.ts index 4951a390..56515503 100644 --- a/src/function/event.ts +++ b/src/function/handler_event.ts @@ -14,7 +14,7 @@ interface EventMultiValueQueryStringParameters { [name: string]: string[] | undefined } -export interface Event { +export interface HandlerEvent { rawUrl: string rawQuery: string path: string diff --git a/src/function/response.ts b/src/function/handler_response.ts similarity index 70% rename from src/function/response.ts rename to src/function/handler_response.ts index 99ad7dd4..56ba60fc 100644 --- a/src/function/response.ts +++ b/src/function/handler_response.ts @@ -1,6 +1,6 @@ import type { PipelineSource } from 'node:stream' -export interface Response { +export interface HandlerResponse { statusCode: number headers?: { [header: string]: boolean | number | string @@ -11,11 +11,11 @@ export interface Response { body?: string isBase64Encoded?: boolean } -export interface BuilderResponse extends Response { +export interface BuilderResponse extends HandlerResponse { ttl?: number } -export interface StreamingResponse extends Omit { +export interface StreamingResponse extends Omit { // eslint-disable-next-line @typescript-eslint/no-explicit-any body?: string | PipelineSource } diff --git a/src/function/index.ts b/src/function/index.ts index 6feadc92..117b60d9 100644 --- a/src/function/index.ts +++ b/src/function/index.ts @@ -1,5 +1,5 @@ -export { Context as HandlerContext } from './context.js' -export { Event as HandlerEvent } from './event.js' +export { HandlerContext } from './handler_context.js' +export { HandlerEvent } from './handler_event.js' export { BuilderHandler, Handler, BackgroundHandler, HandlerCallback, StreamingHandler } from './handler.js' -export { BuilderResponse, Response as HandlerResponse, StreamingResponse } from './response.js' +export { BuilderResponse, HandlerResponse, StreamingResponse } from './handler_response.js' export { Context } from './v2.js' diff --git a/src/lib/builder.ts b/src/lib/builder.ts index 281be434..e2144e79 100644 --- a/src/lib/builder.ts +++ b/src/lib/builder.ts @@ -1,8 +1,8 @@ import isPromise from 'is-promise' import { BuilderHandler, Handler, HandlerCallback } from '../function/handler.js' +import { HandlerResponse, BuilderResponse } from '../function/handler_response.js' import { HandlerContext, HandlerEvent } from '../function/index.js' -import { Response, BuilderResponse } from '../function/response.js' import { BUILDER_FUNCTIONS_FLAG, HTTP_STATUS_METHOD_NOT_ALLOWED, METADATA_VERSION } from './consts.js' @@ -21,7 +21,7 @@ const augmentResponse = (response: BuilderResponse) => { const wrapHandler = (handler: BuilderHandler): Handler => // eslint-disable-next-line promise/prefer-await-to-callbacks - (event: HandlerEvent, context: HandlerContext, callback?: HandlerCallback) => { + (event: HandlerEvent, context: HandlerContext, callback?: HandlerCallback) => { if (event.httpMethod !== 'GET' && event.httpMethod !== 'HEAD') { return Promise.resolve({ body: 'Method Not Allowed',