Skip to content

Commit

Permalink
feat!: export v2 api (#408)
Browse files Browse the repository at this point in the history
This PR adds the V2 functions API to the exports of
`@netlify/functions`.

---------

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
  • Loading branch information
Skn0tt and eduardoboucas authored Aug 21, 2023
1 parent 3481b22 commit 894fb91
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 21 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"test": "test"
},
"dependencies": {
"@netlify/serverless-functions-api": "1.6.0",
"is-promise": "^4.0.0"
},
"devDependencies": {
Expand Down
25 changes: 14 additions & 11 deletions src/function/handler.ts
Original file line number Diff line number Diff line change
@@ -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<ResponseType extends Response = Response> {
export interface HandlerCallback<ResponseType extends HandlerResponse = HandlerResponse> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error: any, response: ResponseType): void
}

export interface BaseHandler<ResponseType extends Response = Response, C extends Context = Context> {
(event: Event, context: C, callback?: HandlerCallback<ResponseType>): void | Promise<ResponseType>
export interface BaseHandler<
ResponseType extends HandlerResponse = HandlerResponse,
C extends HandlerContext = HandlerContext,
> {
(event: HandlerEvent, context: C, callback?: HandlerCallback<ResponseType>): void | Promise<ResponseType>
}

export interface BackgroundHandler<C extends Context = Context> {
(event: Event, context: C): void | Promise<void>
export interface BackgroundHandler<C extends HandlerContext = HandlerContext> {
(event: HandlerEvent, context: C): void | Promise<void>
}

export type Handler = BaseHandler<Response, Context>
export type BuilderHandler = BaseHandler<BuilderResponse, Context>
export type Handler = BaseHandler<HandlerResponse, HandlerContext>
export type BuilderHandler = BaseHandler<BuilderResponse, HandlerContext>

export interface StreamingHandler {
(event: Event, context: Context): Promise<StreamingResponse>
(event: HandlerEvent, context: HandlerContext): Promise<StreamingResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/function/event.ts → src/function/handler_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface EventMultiValueQueryStringParameters {
[name: string]: string[] | undefined
}

export interface Event {
export interface HandlerEvent {
rawUrl: string
rawQuery: string
path: string
Expand Down
6 changes: 3 additions & 3 deletions src/function/response.ts → src/function/handler_response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PipelineSource } from 'node:stream'

export interface Response {
export interface HandlerResponse {
statusCode: number
headers?: {
[header: string]: boolean | number | string
Expand All @@ -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<Response, 'body'> {
export interface StreamingResponse extends Omit<HandlerResponse, 'body'> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body?: string | PipelineSource<any>
}
7 changes: 4 additions & 3 deletions src/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +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'
1 change: 1 addition & 0 deletions src/function/v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { Context } from '@netlify/serverless-functions-api'
4 changes: 2 additions & 2 deletions src/lib/builder.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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<Response>) => {
(event: HandlerEvent, context: HandlerContext, callback?: HandlerCallback<HandlerResponse>) => {
if (event.httpMethod !== 'GET' && event.httpMethod !== 'HEAD') {
return Promise.resolve({
body: 'Method Not Allowed',
Expand Down

0 comments on commit 894fb91

Please sign in to comment.