【发布时间】:2022-01-19 19:22:21
【问题描述】:
我有这个验证中间件,我正在尝试编写它以在发送请求时验证空有效负载:
const _isMissing = (body: any): boolean => !body;
export const isMissing = Object.defineProperty(_isMissing, '_apiGatewayResponse', {
value: LAMBDA_400_RESPONSE,
});
interface ValidationFunction extends Function {
_apiGatewayResponse: APIGatewayProxyResult;
}
export function validateRequestBody(
handler: LambdaFunction,
validations: Array<ValidationFunction>,
): LambdaFunction {
const wrapper: LambdaFunction = async (
event: APIGatewayEvent,
): Promise<APIGatewayProxyResult> => {
const eventBody = event.body ? JSON.parse(event.body) : null;
for (const validation of validations) {
const invalidBody = validation(eventBody);
if (invalidBody) {
return validation._apiGatewayResponse || LAMBDA_400_RESPONSE;
}
}
const response = await handler(event);
return response;
};
return wrapper;
}
但是当我开始使用中间件功能时:
validateRequestBody(myPostFunction, [isMissing]);
我在isMissing 声明中收到 TypeScript 错误
Property '_apiGatewayResponse' is missing in type '(body: any) => boolean' but required in type 'ValidationFunction'.
谁能帮我解决这个问题?我真的找不到任何类似的问题,希望能得到任何帮助。
谢谢!
【问题讨论】: