-
-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1131 from naorpeled/fix/http-json-body-parser/res…
…olve-type-issues fix(middlewares/http-json-body-parser): narrow body type to string
- Loading branch information
Showing
2 changed files
with
69 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,13 @@ | ||
import middy from '@middy/core' | ||
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda' | ||
import { JsonValue } from 'type-fest' | ||
|
||
interface Options { | ||
reviver?: (key: string, value: any) => any | ||
disableContentTypeError?: boolean | ||
} | ||
|
||
type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2 | ||
export type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2 | ||
|
||
export type Event<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> = Omit<APIGatewayEventType, 'body'> & { | ||
/** | ||
* The body of the HTTP request. | ||
*/ | ||
body: JsonValue | ||
} | ||
|
||
declare function jsonBodyParser<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> (options?: Options): middy.MiddlewareObj<Event<APIGatewayEventType>> | ||
declare function jsonBodyParser<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> (options?: Options): middy.MiddlewareObj<APIGatewayEventType> | ||
|
||
export default jsonBodyParser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,82 @@ | ||
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda' | ||
import middy from '@middy/core' | ||
import { expectType } from 'tsd' | ||
import jsonBodyParser, { Event } from '.' | ||
import jsonBodyParser from '.' | ||
|
||
// use with default options | ||
let middleware = jsonBodyParser() | ||
expectType<middy.MiddlewareObj<Event>>(middleware) | ||
expectType<middy.MiddlewareObj<APIGatewayEvent | APIGatewayProxyEventV2>>(middleware) | ||
|
||
// use with all options | ||
middleware = jsonBodyParser({ | ||
reviver: (key: string, value: any) => Boolean(value) | ||
}) | ||
expectType<middy.MiddlewareObj<Event>>(middleware) | ||
expectType<middy.MiddlewareObj<APIGatewayEvent | APIGatewayProxyEventV2>>(middleware) | ||
|
||
const baseEvent: Omit<APIGatewayEvent, 'body'> = { | ||
headers: {}, | ||
isBase64Encoded: false, | ||
httpMethod: 'GET', | ||
path: '/', | ||
multiValueHeaders: {}, | ||
pathParameters: null, | ||
queryStringParameters: null, | ||
multiValueQueryStringParameters: null, | ||
stageVariables: null, | ||
requestContext: { | ||
accountId: '', | ||
apiId: '', | ||
authorizer: null, | ||
protocol: '', | ||
httpMethod: '', | ||
path: '', | ||
stage: '', | ||
requestId: '', | ||
requestTimeEpoch: 0, | ||
resourceId: '', | ||
resourcePath: '', | ||
identity: { | ||
accessKey: null, | ||
accountId: null, | ||
apiKey: null, | ||
apiKeyId: null, | ||
caller: null, | ||
clientCert: null, | ||
cognitoAuthenticationProvider: null, | ||
cognitoAuthenticationType: null, | ||
cognitoIdentityId: null, | ||
cognitoIdentityPoolId: null, | ||
principalOrgId: null, | ||
sourceIp: '', | ||
user: null, | ||
userAgent: null, | ||
userArn: null | ||
} | ||
}, | ||
resource: '' | ||
} | ||
|
||
// allow body to only be string or null | ||
middleware = jsonBodyParser() | ||
const middifiedHandler = middy(() => {}).use(middleware) | ||
|
||
expectType<Promise<void>>(middifiedHandler({ | ||
...baseEvent, | ||
body: 'string' | ||
}, {} as any)) | ||
expectType<Promise<void>>(middifiedHandler({ | ||
...baseEvent, | ||
body: null | ||
}, {} as any)) | ||
|
||
middifiedHandler({ | ||
...baseEvent, | ||
// @ts-expect-error | ||
body: {} | ||
}, {} as any).then(() => {}).catch(() => {}) | ||
|
||
// allow specifying the event type | ||
const apiGatewayV1Middleware = jsonBodyParser<APIGatewayEvent>() | ||
expectType<middy.MiddlewareObj<Event<APIGatewayEvent>>>(apiGatewayV1Middleware) | ||
expectType<middy.MiddlewareObj<APIGatewayEvent>>(apiGatewayV1Middleware) | ||
const apiGatewayV2Middleware = jsonBodyParser<APIGatewayProxyEventV2>() | ||
expectType<middy.MiddlewareObj<Event<APIGatewayProxyEventV2>>>(apiGatewayV2Middleware) | ||
expectType<middy.MiddlewareObj<APIGatewayProxyEventV2>>(apiGatewayV2Middleware) |