Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: self-composing UseFN #883

Merged
merged 22 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/cloudwatch-metrics/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import middy from '@middy/core'
import { MetricsLogger } from 'aws-embedded-metrics'
import type { Context as LambdaContext } from 'aws-lambda'
export { MetricsLogger } from 'aws-embedded-metrics'

interface Options {
namespace?: string
dimensions?: Array<Record<string, string>>
}

declare function cloudwatchMetrics (options?: Options): middy.MiddlewareObj
export type Context = LambdaContext & {
metrics: MetricsLogger
}

declare function cloudwatchMetrics (
options?: Options
): middy.MiddlewareObj<unknown, any, Error, Context>

export default cloudwatchMetrics
8 changes: 4 additions & 4 deletions packages/cloudwatch-metrics/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expectType } from 'tsd'
import middy from '@middy/core'
import cloudwatchMetrics from '.'
import { expectType } from 'tsd'
import cloudwatchMetrics, { Context } from '.'

// use with default options
let middleware = cloudwatchMetrics()
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<unknown, any, Error, Context>>(middleware)

// use with all options
middleware = cloudwatchMetrics({
namespace: 'myApp',
dimensions: [{ Action: 'Buy' }]
})
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<unknown, any, Error, Context>>(middleware)
15 changes: 14 additions & 1 deletion packages/cloudwatch-metrics/package-lock.json

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

3 changes: 2 additions & 1 deletion packages/cloudwatch-metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"aws-embedded-metrics": "3.0.0"
},
"devDependencies": {
"@middy/core": "3.1.1"
"@middy/core": "3.1.1",
"@types/aws-lambda": "^8.10.101"
},
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
}
18 changes: 11 additions & 7 deletions packages/core/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Callback as LambdaCallback,
Context as LambdaContext,
Handler as LambdaHandler,
Callback as LambdaCallback
Handler as LambdaHandler
} from 'aws-lambda'

declare type PluginHook = () => void
Expand Down Expand Up @@ -33,7 +33,7 @@ interface Request<TEvent = any, TResult = any, TErr = Error, TContext extends La

declare type MiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (request: Request<TEvent, TResult, TErr, TContext>) => any

export interface MiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> {
export interface MiddlewareObj<TEvent = unknown, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> {
before?: MiddlewareFn<TEvent, TResult, TErr, TContext>
after?: MiddlewareFn<TEvent, TResult, TErr, TContext>
onError?: MiddlewareFn<TEvent, TResult, TErr, TContext>
Expand All @@ -53,12 +53,16 @@ export interface MiddyfiedHandler<TEvent = any, TResult = any, TErr = Error, TCo
handler: (handler: MiddlewareHandler<LambdaHandler<TEvent, TResult>, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
}

declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareFn) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
(middleware: MiddlewareFn<TEvent, TResult, TErr, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>

declare type AttachMiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareObj) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
declare type AttachMiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
(middleware: MiddlewareObj<TEvent, TResult, TErr, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>

declare type UseFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
(middlewares: MiddlewareObj<TEvent, TResult, TErr, TContext> | Array<MiddlewareObj<TEvent, TResult, TErr, TContext>>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
<TMiddleware extends MiddlewareObj<any, any, Error, any>>(middlewares: TMiddleware | TMiddleware[]) => TMiddleware extends MiddlewareObj<infer TMiddlewareEvent, any, Error, infer TMiddlewareContext>
? MiddyfiedHandler<TMiddlewareEvent & TEvent, TResult, TErr, TMiddlewareContext & TContext> // always true
: never

declare type MiddlewareHandler<THandler extends LambdaHandler<any, any>, TContext extends LambdaContext = LambdaContext> =
THandler extends LambdaHandler<infer TEvent, infer TResult> // always true
Expand All @@ -70,7 +74,7 @@ declare type MiddlewareHandler<THandler extends LambdaHandler<any, any>, TContex
* @param handler your original AWS Lambda function
* @param plugin wraps around each middleware and handler to add custom lifecycle behaviours (e.g. to profile performance)
*/
declare function middy<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> (handler?: MiddlewareHandler<LambdaHandler<TEvent, TResult>, TContext>, plugin?: PluginObject): MiddyfiedHandler<TEvent, TResult, TErr, TContext>
declare function middy<TEvent = unknown, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> (handler?: MiddlewareHandler<LambdaHandler<TEvent, TResult>, TContext>, plugin?: PluginObject): MiddyfiedHandler<TEvent, TResult, TErr, TContext>

declare namespace middy {
export {
Expand Down
15 changes: 14 additions & 1 deletion packages/http-content-negotiation/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ interface Options {
failOnMismatch?: boolean
}

declare function httpContentNegotiation (options?: Options): middy.MiddlewareObj
export interface Event {
preferredCharsets: string[]
preferredCharset: string
preferredEncodings: string[]
preferredEncoding: string
preferredLanguages: string[]
preferredLanguage: string
preferredMediaTypes: string[]
preferredMediaType: string
}

declare function httpContentNegotiation (
options?: Options
): middy.MiddlewareObj<Event>

export default httpContentNegotiation
8 changes: 4 additions & 4 deletions packages/http-content-negotiation/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expectType } from 'tsd'
import middy from '@middy/core'
import httpContentNegotiationMiddleware from '.'
import { expectType } from 'tsd'
import httpContentNegotiationMiddleware, { Event } from '.'

// use with default options
let middleware = httpContentNegotiationMiddleware()
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<Event>>(middleware)

// use with all options
middleware = httpContentNegotiationMiddleware({
Expand All @@ -18,4 +18,4 @@ middleware = httpContentNegotiationMiddleware({
availableMediaTypes: ['application/xml', 'application/json'],
failOnMismatch: true
})
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<Event>>(middleware)
14 changes: 13 additions & 1 deletion packages/http-event-normalizer/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import middy from '@middy/core'
import {
APIGatewayEvent,
APIGatewayProxyEventMultiValueQueryStringParameters,
APIGatewayProxyEventPathParameters,
APIGatewayProxyEventQueryStringParameters
} from 'aws-lambda'

declare function httpEventNormalizer (): middy.MiddlewareObj
export type Event = APIGatewayEvent & {
multiValueQueryStringParameters: APIGatewayProxyEventMultiValueQueryStringParameters
pathParameters: APIGatewayProxyEventPathParameters
queryStringParameters: APIGatewayProxyEventQueryStringParameters
}

declare function httpEventNormalizer (): middy.MiddlewareObj<Event>

export default httpEventNormalizer
8 changes: 4 additions & 4 deletions packages/http-event-normalizer/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expectType } from 'tsd'
import middy from '@middy/core'
import httpEventNormalizer from '.'
import { expectType } from 'tsd'
import httpEventNormalizer, { Event } from '.'

// use with default options
let middleware = httpEventNormalizer()
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<Event>>(middleware)

// use with all options
middleware = httpEventNormalizer()
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<Event>>(middleware)
15 changes: 14 additions & 1 deletion packages/http-event-normalizer/package-lock.json

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

3 changes: 2 additions & 1 deletion packages/http-event-normalizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"homepage": "https://middy.js.org",
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
"devDependencies": {
"@middy/core": "3.1.1"
"@middy/core": "3.1.1",
"@types/aws-lambda": "^8.10.101"
}
}
8 changes: 7 additions & 1 deletion packages/http-header-normalizer/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ interface Options {
canonical?: boolean
}

declare function httpHeaderNormalizer (options?: Options): middy.MiddlewareObj
export interface Event {
rawHeaders: Record<string, string>
}

declare function httpHeaderNormalizer (
options?: Options
): middy.MiddlewareObj<Event>

export default httpHeaderNormalizer
8 changes: 4 additions & 4 deletions packages/http-header-normalizer/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expectType } from 'tsd'
import middy from '@middy/core'
import httpHeaderNormalizer from '.'
import { expectType } from 'tsd'
import httpHeaderNormalizer, { Event } from '.'

// use with default options
let middleware = httpHeaderNormalizer()
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<Event>>(middleware)

// use with all options
middleware = httpHeaderNormalizer({
normalizeHeaderKey: (key: string) => key.toLowerCase(),
canonical: false
})
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<Event>>(middleware)
12 changes: 11 additions & 1 deletion packages/http-json-body-parser/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import middy from '@middy/core'
import { APIGatewayEvent } from 'aws-lambda'
import { JsonValue } from 'type-fest'

interface Options {
reviver?: (key: string, value: any) => any
}

declare function jsonBodyParser (options?: Options): middy.MiddlewareObj
export type Event = Omit<APIGatewayEvent, 'body'> & {
/**
* The body of the HTTP request.
*/
body: JsonValue
rawBody: string
}

declare function jsonBodyParser (options?: Options): middy.MiddlewareObj<Event>

export default jsonBodyParser
8 changes: 4 additions & 4 deletions packages/http-json-body-parser/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expectType } from 'tsd'
import middy from '@middy/core'
import jsonBodyParser from '.'
import { expectType } from 'tsd'
import jsonBodyParser, { Event } from '.'

// use with default options
let middleware = jsonBodyParser()
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<Event>>(middleware)

// use with all options
middleware = jsonBodyParser({
reviver: (key: string, value: any) => Boolean(value)
})
expectType<middy.MiddlewareObj>(middleware)
expectType<middy.MiddlewareObj<Event>>(middleware)
34 changes: 33 additions & 1 deletion packages/http-json-body-parser/package-lock.json

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

4 changes: 3 additions & 1 deletion packages/http-json-body-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"@middy/util": "3.1.1"
},
"devDependencies": {
"@middy/core": "3.1.1"
"@middy/core": "3.1.1",
"@types/aws-lambda": "^8.10.101",
"type-fest": "^2.18.0"
},
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
}
Loading