sdk-aws-lambda 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/access/sdks/lambda/genLambdaSdk.d.ts +8 -1
- package/dist/access/sdks/lambda/genLambdaSdk.js +57 -4
- package/dist/access/sdks/lambda/genLambdaSdk.js.map +1 -1
- package/dist/domain.objects/ApiGatewayRequestPayload.d.ts +12 -0
- package/dist/domain.objects/ApiGatewayRequestPayload.js +3 -0
- package/dist/domain.objects/ApiGatewayRequestPayload.js.map +1 -0
- package/dist/domain.objects/ApiGatewayResponse.d.ts +30 -0
- package/dist/domain.objects/ApiGatewayResponse.js +3 -0
- package/dist/domain.objects/ApiGatewayResponse.js.map +1 -0
- package/dist/domain.objects/ApiGatewayResponsePayload.d.ts +30 -0
- package/dist/domain.objects/ApiGatewayResponsePayload.js +3 -0
- package/dist/domain.objects/ApiGatewayResponsePayload.js.map +1 -0
- package/dist/domain.objects/HttpStatusCode.d.ts +1 -0
- package/dist/domain.objects/HttpStatusCode.js +1 -0
- package/dist/domain.objects/HttpStatusCode.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/TranslateLog.d.ts +29 -0
- package/dist/domain.operations/genLambdaEndpoint/TranslateLog.js +3 -0
- package/dist/domain.operations/genLambdaEndpoint/TranslateLog.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/asContextLogTrail.d.ts +29 -0
- package/dist/domain.operations/genLambdaEndpoint/asContextLogTrail.js +30 -0
- package/dist/domain.operations/genLambdaEndpoint/asContextLogTrail.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/UnifiedApiGatewayEvent.d.ts +36 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/UnifiedApiGatewayEvent.js +3 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/UnifiedApiGatewayEvent.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponsePayload.d.ts +19 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponsePayload.js +24 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponsePayload.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponsePayloadJson.d.ts +26 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponsePayloadJson.js +31 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponsePayloadJson.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponseSchema.d.ts +33 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponseSchema.js +39 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asApiGatewayResponseSchema.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asUnifiedApiGatewayEvent.d.ts +27 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asUnifiedApiGatewayEvent.js +118 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asUnifiedApiGatewayEvent.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/genLambdaEndpoint.forApiGateway.d.ts +76 -36
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/genLambdaEndpoint.forApiGateway.js +153 -45
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/genLambdaEndpoint.forApiGateway.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/getAllWirePayloadKeysFound.d.ts +8 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/getAllWirePayloadKeysFound.js +34 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/getAllWirePayloadKeysFound.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/isApiGatewayResponse.d.ts +19 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/isApiGatewayResponse.js +36 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/isApiGatewayResponse.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genApiGatewayEventNormalizationMiddleware.d.ts +7 -24
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genApiGatewayEventNormalizationMiddleware.js +19 -103
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genApiGatewayEventNormalizationMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genContentTypeCoherenceMiddleware.d.ts +20 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genContentTypeCoherenceMiddleware.js +48 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genContentTypeCoherenceMiddleware.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genZodBodyValidationMiddleware.d.ts +16 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genZodBodyValidationMiddleware.js +32 -0
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/middleware/genZodBodyValidationMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forAskEndpoint/genLambdaEndpoint.forAskEndpoint.d.ts +6 -22
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forAskEndpoint/genLambdaEndpoint.forAskEndpoint.js +24 -18
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forAskEndpoint/genLambdaEndpoint.forAskEndpoint.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forAskEndpoint/middleware/genZodEventValidationMiddleware.d.ts +11 -2
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forAskEndpoint/middleware/genZodEventValidationMiddleware.js +12 -2
- package/dist/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forAskEndpoint/middleware/genZodEventValidationMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/middleware/asContextTrailed.d.ts +37 -0
- package/dist/domain.operations/genLambdaEndpoint/middleware/asContextTrailed.js +24 -0
- package/dist/domain.operations/genLambdaEndpoint/middleware/asContextTrailed.js.map +1 -0
- package/dist/domain.operations/genLambdaEndpoint/middleware/genConstraintErrorMiddleware.d.ts +18 -10
- package/dist/domain.operations/genLambdaEndpoint/middleware/genConstraintErrorMiddleware.js +20 -23
- package/dist/domain.operations/genLambdaEndpoint/middleware/genConstraintErrorMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/middleware/genInternalServiceErrorMiddleware.d.ts +29 -6
- package/dist/domain.operations/genLambdaEndpoint/middleware/genInternalServiceErrorMiddleware.js +23 -28
- package/dist/domain.operations/genLambdaEndpoint/middleware/genInternalServiceErrorMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/middleware/genIntrospectionMiddleware.d.ts +16 -1
- package/dist/domain.operations/genLambdaEndpoint/middleware/genIntrospectionMiddleware.js +9 -13
- package/dist/domain.operations/genLambdaEndpoint/middleware/genIntrospectionMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/middleware/genIoLoggerMiddleware.d.ts +27 -6
- package/dist/domain.operations/genLambdaEndpoint/middleware/genIoLoggerMiddleware.js +29 -4
- package/dist/domain.operations/genLambdaEndpoint/middleware/genIoLoggerMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/middleware/genTrailMiddleware.js +12 -4
- package/dist/domain.operations/genLambdaEndpoint/middleware/genTrailMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/middleware/genZodOutputValidationMiddleware.d.ts +27 -2
- package/dist/domain.operations/genLambdaEndpoint/middleware/genZodOutputValidationMiddleware.js +34 -13
- package/dist/domain.operations/genLambdaEndpoint/middleware/genZodOutputValidationMiddleware.js.map +1 -1
- package/dist/domain.operations/genLambdaEndpoint/middleware/getErrorResponseBody.js +1 -0
- package/dist/domain.operations/genLambdaEndpoint/middleware/getErrorResponseBody.js.map +1 -1
- package/dist/index.d.ts +40 -2
- package/dist/index.js +28 -1
- package/dist/index.js.map +1 -1
- package/package.json +12 -11
- package/readme.md +50 -2
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.asUnifiedApiGatewayEvent = exports.DESERIALIZE_DEFAULT = void 0;
|
|
4
|
+
const helpful_errors_1 = require("helpful-errors");
|
|
5
|
+
const getIsV1ApiGatewayEvent_1 = require("./getIsV1ApiGatewayEvent");
|
|
6
|
+
const getIsV2ApiGatewayEvent_1 = require("./getIsV2ApiGatewayEvent");
|
|
7
|
+
/**
|
|
8
|
+
* .what = parses a json body string into an object, base64-decoded first when flagged
|
|
9
|
+
* .why = api gateway hands the body over as a string; most handlers want the object
|
|
10
|
+
*
|
|
11
|
+
* .note = a body that is not json is returned as the decoded string rather than refused —
|
|
12
|
+
* a form-encoded twilio webhook is a legitimate caller, and its own schema (or
|
|
13
|
+
* `z.any()`) is where that shape gets judged
|
|
14
|
+
*/
|
|
15
|
+
const asParsedApiGatewayBody = (input) => {
|
|
16
|
+
if (!input.body)
|
|
17
|
+
return null;
|
|
18
|
+
const decoded = input.isBase64Encoded
|
|
19
|
+
? Buffer.from(input.body, 'base64').toString('utf-8')
|
|
20
|
+
: input.body;
|
|
21
|
+
try {
|
|
22
|
+
return JSON.parse(decoded);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
/**
|
|
26
|
+
* .what = allowlists the ONE error a non-json body can raise, and rethrows every other
|
|
27
|
+
* .why = a bare `catch` would swallow a TypeError from a real defect, and the swallowed
|
|
28
|
+
* value would reach the handler as plain text (rule.forbid.failhide). `SyntaxError`
|
|
29
|
+
* is the only class `JSON.parse` throws for malformed input, per ecma-262 §25.5.1,
|
|
30
|
+
* so the allowlist is exhaustive rather than a guess
|
|
31
|
+
*/
|
|
32
|
+
if (!(error instanceof SyntaxError))
|
|
33
|
+
throw error;
|
|
34
|
+
return decoded;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* .what = reconciles either payload version into the unified shape, given the TWO values the
|
|
39
|
+
* versions disagree about
|
|
40
|
+
* .why = v1 and v2 differ in exactly two places — the method and path, top-level on v1 and
|
|
41
|
+
* under `requestContext.http` on v2. every OTHER field sits at the same path on both, so
|
|
42
|
+
* one function serves both and the unified shape gains a new field in ONE place
|
|
43
|
+
*
|
|
44
|
+
* .note = the two disputed values are handed in as plain strings rather than read through a
|
|
45
|
+
* per-version extractor, which would restore the duplication in a less legible form
|
|
46
|
+
*/
|
|
47
|
+
const asUnifiedEvent = (input) => ({
|
|
48
|
+
httpMethod: input.httpMethod,
|
|
49
|
+
path: input.path,
|
|
50
|
+
/**
|
|
51
|
+
* .as = v1 declares `headers` REQUIRED and v2 declares it optional, so the union's member is
|
|
52
|
+
* `… | undefined`, which the unified shape does not carry
|
|
53
|
+
* .removal = drops once `UnifiedApiGatewayEvent.headers` is itself optional. left because
|
|
54
|
+
* every consumer reads `event.headers[x]`, and `@middy/http-cors` reads this slot too
|
|
55
|
+
* (rule.require.read-the-slot-a-dependency-reads) — so the change is not local
|
|
56
|
+
*/
|
|
57
|
+
headers: input.payload.headers,
|
|
58
|
+
queryStringParameters: input.payload.queryStringParameters ?? null,
|
|
59
|
+
pathParameters: input.payload.pathParameters ?? null,
|
|
60
|
+
body: input.deserialize.body
|
|
61
|
+
? asParsedApiGatewayBody({
|
|
62
|
+
body: input.payload.body,
|
|
63
|
+
isBase64Encoded: input.payload.isBase64Encoded,
|
|
64
|
+
})
|
|
65
|
+
: input.payload.body,
|
|
66
|
+
rawBody: input.payload.body ?? null,
|
|
67
|
+
isBase64Encoded: input.payload.isBase64Encoded,
|
|
68
|
+
requestContext: {
|
|
69
|
+
requestId: input.payload.requestContext.requestId,
|
|
70
|
+
stage: input.payload.requestContext.stage,
|
|
71
|
+
domainName: input.payload.requestContext.domainName,
|
|
72
|
+
accountId: input.payload.requestContext.accountId,
|
|
73
|
+
},
|
|
74
|
+
_: { raw: input.payload },
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* .what = what `deserialize` means when a caller declares none — ONE declaration, read by both
|
|
78
|
+
* this transformer and `forApiGateway`'s config (rule.forbid.parallel-codepaths)
|
|
79
|
+
* .why body = json is the common case, and a caller who wants the raw string opts out by value:
|
|
80
|
+
* `deserialize: { body: false }`
|
|
81
|
+
*/
|
|
82
|
+
exports.DESERIALIZE_DEFAULT = { body: true };
|
|
83
|
+
/**
|
|
84
|
+
* .what = reconciles either api-gateway payload version into one shape
|
|
85
|
+
* .why = the chain must read one shape whichever version the trigger delivered, so this runs
|
|
86
|
+
* first and every step after it reads `UnifiedApiGatewayEvent`
|
|
87
|
+
*
|
|
88
|
+
* .note = a named transformer rather than middleware-local logic, so ONE implementation serves
|
|
89
|
+
* both entry points — `genApiGatewayEventNormalizationMiddleware` delegates here
|
|
90
|
+
* (rule.forbid.parallel-codepaths)
|
|
91
|
+
*/
|
|
92
|
+
const asUnifiedApiGatewayEvent = (input, options) => {
|
|
93
|
+
const deserialize = options?.deserialize ?? exports.DESERIALIZE_DEFAULT;
|
|
94
|
+
// v2 http-api: the method and path live under `requestContext.http`
|
|
95
|
+
if ((0, getIsV2ApiGatewayEvent_1.getIsV2ApiGatewayEvent)(input.payload)) {
|
|
96
|
+
const payload = input.payload;
|
|
97
|
+
return asUnifiedEvent({
|
|
98
|
+
payload,
|
|
99
|
+
httpMethod: payload.requestContext.http.method,
|
|
100
|
+
path: payload.requestContext.http.path,
|
|
101
|
+
deserialize,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
// v1 rest-api: the method and path live at the top level
|
|
105
|
+
if ((0, getIsV1ApiGatewayEvent_1.getIsV1ApiGatewayEvent)(input.payload)) {
|
|
106
|
+
const payload = input.payload;
|
|
107
|
+
return asUnifiedEvent({
|
|
108
|
+
payload,
|
|
109
|
+
httpMethod: payload.httpMethod,
|
|
110
|
+
path: payload.path,
|
|
111
|
+
deserialize,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// fail loud rather than hand a handler a shape neither branch produced
|
|
115
|
+
return helpful_errors_1.MalfunctionError.throw('payload is neither v1 nor v2 api gateway event shaped', { payload: input.payload });
|
|
116
|
+
};
|
|
117
|
+
exports.asUnifiedApiGatewayEvent = asUnifiedApiGatewayEvent;
|
|
118
|
+
//# sourceMappingURL=asUnifiedApiGatewayEvent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asUnifiedApiGatewayEvent.js","sourceRoot":"","sources":["../../../../src/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/asUnifiedApiGatewayEvent.ts"],"names":[],"mappings":";;;AACA,mDAAkD;AAGlD,qEAAkE;AAClE,qEAAkE;AAGlE;;;;;;;GAOG;AACH,MAAM,sBAAsB,GAAG,CAAC,KAG/B,EAAW,EAAE;IACZ,IAAI,CAAC,KAAK,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IAE7B,MAAM,OAAO,GAAG,KAAK,CAAC,eAAe;QACnC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,QA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
|
|
2
|
+
import type { Context } from 'aws-lambda';
|
|
3
3
|
import type { ContextLogTrail } from 'sdk-logs';
|
|
4
4
|
import type { ZodSchema } from 'zod';
|
|
5
|
+
import type { ApiGatewayRequestPayload } from '../../../domain.objects/ApiGatewayRequestPayload';
|
|
6
|
+
import type { ApiGatewayResponse } from '../../../domain.objects/ApiGatewayResponse';
|
|
7
|
+
import type { ApiGatewayResponsePayload } from '../../../domain.objects/ApiGatewayResponsePayload';
|
|
5
8
|
import type { ContextAwsLambdaServer } from '../../../domain.objects/ContextAwsLambdaServer';
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
9
|
+
import type { TranslateLog } from '../TranslateLog';
|
|
10
|
+
import type { UnifiedApiGatewayEvent } from './UnifiedApiGatewayEvent';
|
|
11
|
+
/**
|
|
12
|
+
* .what = the cors contract for an api-gateway handler
|
|
13
|
+
*
|
|
14
|
+
* ⚠️ .limits = TWO, both LIVE and both INHERITED — `main` and `simple-lambda-handlers` carry
|
|
15
|
+
* the identical pair. `[case11]` and `[case8]` of `local.wireResponse.acceptance.test.ts`
|
|
16
|
+
* assert the limited bytes and are GREEN, so each goes RED when repaired. writeup:
|
|
17
|
+
* `.behavior/…/seeds/issue.cors-and-headers-on-every-non-success-path.md`
|
|
18
|
+
*
|
|
19
|
+
* 1. **a preflight is answered by YOUR HANDLER.** `@middy/http-cors` defaults
|
|
20
|
+
* `disableBeforePreflightResponse: true` (`index.js:16`), so an `OPTIONS` falls through
|
|
21
|
+
* the chain — and succeeds only where `schema.input` accepts a body-less request
|
|
22
|
+
*
|
|
23
|
+
* 2. **no error response carries cors or owasp headers.** both vendors' `onError` hooks open
|
|
24
|
+
* `if (request.response === undefined) return` and sit ABOVE the error builders that set
|
|
25
|
+
* it, so both no-op. every 400 and 500 ships bare
|
|
26
|
+
*
|
|
27
|
+
* ⚠️ .unverified = whether either is reachable in a DEPLOYED api gateway, which commonly answers
|
|
28
|
+
* `OPTIONS` at the gateway with a MOCK integration. both were measured against the LOCAL
|
|
29
|
+
* harness, where every request reaches the handler
|
|
30
|
+
*/
|
|
8
31
|
export interface CorsConfig {
|
|
9
32
|
/**
|
|
10
33
|
* .what = origins to accept for cors
|
|
@@ -35,16 +58,58 @@ export interface CorsConfig {
|
|
|
35
58
|
* .what = input type for forApiGateway
|
|
36
59
|
* .why = sdk contract type, exported for consumer type inference
|
|
37
60
|
*/
|
|
38
|
-
export type ForApiGatewayInput<TInput,
|
|
61
|
+
export type ForApiGatewayInput<TInput, TBody> = {
|
|
39
62
|
schema: {
|
|
63
|
+
/**
|
|
64
|
+
* .what = describes `inputAfter` — what `invoke` receives
|
|
65
|
+
* .why = guards the point of consequence on the way in: a caller who sent the wrong
|
|
66
|
+
* shape is refused before the handler runs
|
|
67
|
+
*/
|
|
40
68
|
input: ZodSchema<TInput>;
|
|
41
|
-
|
|
69
|
+
/**
|
|
70
|
+
* .what = describes `outputBefore` — the whole response `invoke` returns
|
|
71
|
+
* .why = guards the point of consequence on the way out: a handler that built the
|
|
72
|
+
* wrong response is refused before the encode turns it into bytes
|
|
73
|
+
*
|
|
74
|
+
* .note = this describes the ENVELOPE, never the body inside it. use
|
|
75
|
+
* `asApiGatewayResponseSchema({ body })` to lift a body schema into one, and
|
|
76
|
+
* `z.any()` to opt out of validation entirely
|
|
77
|
+
*/
|
|
78
|
+
output: ZodSchema<ApiGatewayResponse<TBody>>;
|
|
42
79
|
};
|
|
43
80
|
invoke: (input: {
|
|
81
|
+
/**
|
|
82
|
+
* .what = `inputAfter` — the validated body
|
|
83
|
+
*/
|
|
44
84
|
event: TInput;
|
|
85
|
+
/**
|
|
86
|
+
* .what = the v1/v2-reconciled request; the wire payload itself is at `._.raw`
|
|
87
|
+
*
|
|
88
|
+
* ⚠️ .misnomer — this is NOT raw, it is normalized. the rename (`rawEvent` -> `payload`)
|
|
89
|
+
* is designed and unapplied, gated behind 🚧 F31
|
|
90
|
+
*/
|
|
45
91
|
rawEvent: UnifiedApiGatewayEvent;
|
|
46
|
-
}, context: ContextLogTrail) => Promise<
|
|
47
|
-
|
|
92
|
+
}, context: ContextLogTrail) => Promise<ApiGatewayResponse<TBody>>;
|
|
93
|
+
/**
|
|
94
|
+
* .what = projections applied to the io before it reaches cloudwatch
|
|
95
|
+
*
|
|
96
|
+
* ⚠️ .defect = LIVE, NOT FIXED — `logTranslate.input` NEVER RUNS for this family; only
|
|
97
|
+
* `.output` fires. so a projection written here to redact request bodies is dead
|
|
98
|
+
* code, and the log it would have shaped is never emitted at all. ⚠️ **redact
|
|
99
|
+
* inside `invoke` instead**
|
|
100
|
+
*
|
|
101
|
+
* .the cause = `genIoLoggerMiddleware`'s `before` reads `context.log`, which
|
|
102
|
+
* `genTrailMiddleware`'s `before` writes — and trail sits at a HIGHER index, so it
|
|
103
|
+
* has not run yet. `before` hooks run in FORWARD array order
|
|
104
|
+
*
|
|
105
|
+
* .proof it is live = `[case16][t1]` asserts the log branch does NOT run, and is GREEN. `[t0]`
|
|
106
|
+
* is its positive control — same middleware, same option, trail at a lower index
|
|
107
|
+
*
|
|
108
|
+
* .repair, UNAPPLIED = move `genTrailMiddleware()` from index 8 to index 3. blast radius,
|
|
109
|
+
* probed: ONE assertion. left because `main` carries the identical order and the
|
|
110
|
+
* move also puts trail ahead of event normalization
|
|
111
|
+
*/
|
|
112
|
+
logTranslate?: TranslateLog;
|
|
48
113
|
cors?: CorsConfig;
|
|
49
114
|
deserialize?: {
|
|
50
115
|
/**
|
|
@@ -56,35 +121,10 @@ export type ForApiGatewayInput<TInput, TOutput> = {
|
|
|
56
121
|
body: boolean;
|
|
57
122
|
};
|
|
58
123
|
};
|
|
59
|
-
/**
|
|
60
|
-
* .what = sdk contract type for forApiGateway context
|
|
61
|
-
* .why = exported for consumer type inference; the shared handler-side context
|
|
62
|
-
* ({ env?: EnvConfig; log? }) reused by genLambdaEndpoint + forApiGateway
|
|
63
|
-
*/
|
|
124
|
+
/** .what = sdk contract type for forApiGateway context, exported for consumer inference */
|
|
64
125
|
export type ForApiGatewayContext = ContextAwsLambdaServer;
|
|
65
126
|
/**
|
|
66
|
-
* .what = generates
|
|
67
|
-
* .why = adds cors, security headers, error
|
|
68
|
-
*
|
|
69
|
-
* follows simple-lambda-handlers createApiGatewayHandler pattern:
|
|
70
|
-
* - uses middy middleware chain
|
|
71
|
-
* - uses @middy/http-cors for dynamic origin match
|
|
72
|
-
* - uses @middy/http-security-headers for OWASP security headers
|
|
73
|
-
* - uses @middy/http-json-body-parser for json body parse
|
|
74
|
-
* - uses @middy/http-response-serializer for response serialization
|
|
127
|
+
* .what = generates an api-gateway lambda handler with http features
|
|
128
|
+
* .why = adds cors, security headers, error conversion, body serialization, and validation
|
|
75
129
|
*/
|
|
76
|
-
export declare const forApiGateway: <TInput,
|
|
77
|
-
schema: {
|
|
78
|
-
input: ZodSchema<TInput>;
|
|
79
|
-
output: ZodSchema<TOutput>;
|
|
80
|
-
};
|
|
81
|
-
invoke: (input: {
|
|
82
|
-
event: TInput;
|
|
83
|
-
rawEvent: UnifiedApiGatewayEvent;
|
|
84
|
-
}, context: ContextLogTrail) => Promise<TOutput>;
|
|
85
|
-
logTranslate?: IoLogTranslate;
|
|
86
|
-
cors?: CorsConfig;
|
|
87
|
-
deserialize?: {
|
|
88
|
-
body: boolean;
|
|
89
|
-
};
|
|
90
|
-
}, context?: ContextAwsLambdaServer) => middy.MiddyfiedHandler<APIGatewayProxyEvent, APIGatewayProxyResult, Error, Context>;
|
|
130
|
+
export declare const forApiGateway: <TInput, TBody>(config: ForApiGatewayInput<TInput, TBody>, context?: ContextAwsLambdaServer) => middy.MiddyfiedHandler<ApiGatewayRequestPayload, ApiGatewayResponsePayload, Error, Context>;
|
|
@@ -8,26 +8,42 @@ const core_1 = __importDefault(require("@middy/core"));
|
|
|
8
8
|
const http_cors_1 = __importDefault(require("@middy/http-cors"));
|
|
9
9
|
const http_response_serializer_1 = __importDefault(require("@middy/http-response-serializer"));
|
|
10
10
|
const http_security_headers_1 = __importDefault(require("@middy/http-security-headers"));
|
|
11
|
+
const helpful_errors_1 = require("helpful-errors");
|
|
12
|
+
const asContextLogTrail_1 = require("../asContextLogTrail");
|
|
11
13
|
const genConstraintErrorMiddleware_1 = require("../middleware/genConstraintErrorMiddleware");
|
|
12
14
|
const genInternalServiceErrorMiddleware_1 = require("../middleware/genInternalServiceErrorMiddleware");
|
|
13
15
|
const genIntrospectionMiddleware_1 = require("../middleware/genIntrospectionMiddleware");
|
|
14
16
|
const genIoLoggerMiddleware_1 = require("../middleware/genIoLoggerMiddleware");
|
|
15
17
|
const genTrailMiddleware_1 = require("../middleware/genTrailMiddleware");
|
|
16
18
|
const getValidatedOutput_1 = require("../middleware/getValidatedOutput");
|
|
19
|
+
const asApiGatewayResponsePayload_1 = require("./asApiGatewayResponsePayload");
|
|
20
|
+
const asApiGatewayResponsePayloadJson_1 = require("./asApiGatewayResponsePayloadJson");
|
|
21
|
+
const asUnifiedApiGatewayEvent_1 = require("./asUnifiedApiGatewayEvent");
|
|
22
|
+
const getAllWirePayloadKeysFound_1 = require("./getAllWirePayloadKeysFound");
|
|
23
|
+
const isApiGatewayResponse_1 = require("./isApiGatewayResponse");
|
|
17
24
|
const genApiGatewayEventNormalizationMiddleware_1 = require("./middleware/genApiGatewayEventNormalizationMiddleware");
|
|
25
|
+
const genContentTypeCoherenceMiddleware_1 = require("./middleware/genContentTypeCoherenceMiddleware");
|
|
18
26
|
const genZodBodyValidationMiddleware_1 = require("./middleware/genZodBodyValidationMiddleware");
|
|
19
|
-
/**
|
|
20
|
-
* .what = converts cors config to @middy/http-cors format
|
|
21
|
-
* .why = simple-lambda-handlers pattern for cors configuration
|
|
22
|
-
*/
|
|
27
|
+
/** .what = converts this sdk's cors config into `@middy/http-cors` options */
|
|
23
28
|
const corsInputToCorsConfig = (cors) => {
|
|
24
29
|
return {
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
/**
|
|
31
|
+
* ⚠️ .what = supplies EITHER `origin` or `origins`, and OMITS the key it does not supply.
|
|
32
|
+
* NEVER pass a key you do not mean to set — not `maxAge`, not any later addition
|
|
33
|
+
* .why = `@middy/http-cors` merges as `{ ...defaults, ...opts }` (`index.cjs:44-47`), so an
|
|
34
|
+
* explicit `undefined` CLOBBERS the default. its `origins` default is `[]` and
|
|
35
|
+
* `getOrigin` opens with `options.origins.length` (`index.cjs:12-13`), so
|
|
36
|
+
* `origins: undefined` throws from `after` and turns every success into a 500
|
|
37
|
+
*/
|
|
38
|
+
...(cors.origins === '*' ? { origin: '*' } : { origins: cors.origins }),
|
|
27
39
|
credentials: cors.credentials,
|
|
28
40
|
headers: cors.headers ?? 'content-type,authorization',
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
/**
|
|
42
|
+
* ⚠️ .absent by choice = `disableBeforePreflightResponse: false` would turn on the vendor's
|
|
43
|
+
* `OPTIONS` short-circuit — limit 1 on `CorsConfig`. blast radius, probed: ONE case
|
|
44
|
+
* (`[case11]` moves to a 204). left because it changes `OPTIONS` for every
|
|
45
|
+
* cors-configured handler, and one that answers `OPTIONS` itself would lose the request
|
|
46
|
+
*/
|
|
31
47
|
};
|
|
32
48
|
};
|
|
33
49
|
const serializers = [
|
|
@@ -37,52 +53,132 @@ const serializers = [
|
|
|
37
53
|
},
|
|
38
54
|
];
|
|
39
55
|
/**
|
|
40
|
-
* .what = generates
|
|
41
|
-
* .why = adds cors, security headers, error
|
|
42
|
-
*
|
|
43
|
-
* follows simple-lambda-handlers createApiGatewayHandler pattern:
|
|
44
|
-
* - uses middy middleware chain
|
|
45
|
-
* - uses @middy/http-cors for dynamic origin match
|
|
46
|
-
* - uses @middy/http-security-headers for OWASP security headers
|
|
47
|
-
* - uses @middy/http-json-body-parser for json body parse
|
|
48
|
-
* - uses @middy/http-response-serializer for response serialization
|
|
56
|
+
* .what = generates an api-gateway lambda handler with http features
|
|
57
|
+
* .why = adds cors, security headers, error conversion, body serialization, and validation
|
|
49
58
|
*/
|
|
50
59
|
const forApiGateway = (config, context) => {
|
|
51
|
-
const deserialize = config.deserialize ??
|
|
52
|
-
|
|
60
|
+
const deserialize = config.deserialize ?? asUnifiedApiGatewayEvent_1.DESERIALIZE_DEFAULT;
|
|
61
|
+
/**
|
|
62
|
+
* .what = the handler's own step: invoke, assure, validate, encode
|
|
63
|
+
* .why = by here the chain has reconciled the request and refused a wrong body shape, so
|
|
64
|
+
* this reads as narrative
|
|
65
|
+
*/
|
|
53
66
|
const logic = async (event, lambdaContext) => {
|
|
54
|
-
//
|
|
55
|
-
const log = lambdaContext
|
|
56
|
-
|
|
67
|
+
// read the trail log `genTrailMiddleware` wrote; the cast lives in the shared reader
|
|
68
|
+
const { log } = (0, asContextLogTrail_1.asContextLogTrail)({ context: lambdaContext });
|
|
69
|
+
/**
|
|
70
|
+
* .as = `event.body` is `TInput` by the time this runs, because
|
|
71
|
+
* `genZodBodyValidationMiddleware` has already parsed it against `schema.input`
|
|
72
|
+
* and replaced it with the parsed value
|
|
73
|
+
* .removal = drops when the input translate owns this seam, which needs `inputAfter` to
|
|
74
|
+
* have one home across both chains — see the 🚧 note in the execution yield
|
|
75
|
+
*/
|
|
57
76
|
const response = await config.invoke({ event: event.body, rawEvent: event }, { log });
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
/**
|
|
78
|
+
* .what = refuses the aws WIRE shape, and names the rename that fixes it
|
|
79
|
+
* .why = `isApiGatewayResponse` rejects it too, but `withAssure` takes a name and no message,
|
|
80
|
+
* so its throw can only say "does not satisfy type.check". the likeliest upgrade
|
|
81
|
+
* mistake earns the fix by name (rule.require.errors-name-the-fix)
|
|
82
|
+
* .why MalfunctionError = the handler author is at fault, not the http caller. a
|
|
83
|
+
* ConstraintError would answer 400 and tell the caller THEY erred, which is false
|
|
84
|
+
* (invariant.badrequesterror-not-lambda-error)
|
|
85
|
+
*/
|
|
86
|
+
const keysOfWirePayload = typeof response === 'object' && response !== null
|
|
87
|
+
? (0, getAllWirePayloadKeysFound_1.getAllWirePayloadKeysFound)({ response })
|
|
88
|
+
: [];
|
|
89
|
+
if (keysOfWirePayload.length)
|
|
90
|
+
helpful_errors_1.MalfunctionError.throw('handler returned the aws wire payload shape rather than an ApiGatewayResponse', {
|
|
91
|
+
keysOfWirePayload,
|
|
92
|
+
fix: 'rename `statusCode` to `status`, and return the body as a value rather than a JSON string',
|
|
93
|
+
response,
|
|
94
|
+
});
|
|
95
|
+
// the throw above took every wire-payload response, so this closes the REST — a string, an
|
|
96
|
+
// array, a null. one names a fix for the likely mistake; this one covers the others
|
|
97
|
+
isApiGatewayResponse_1.isApiGatewayResponse.assure(response);
|
|
98
|
+
// validate the whole response envelope against the declared output schema
|
|
99
|
+
const validatedResponse = (0, getValidatedOutput_1.getValidatedOutput)({
|
|
60
100
|
response,
|
|
61
101
|
schema: config.schema.output,
|
|
62
102
|
});
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
body: validatedOutput,
|
|
66
|
-
};
|
|
103
|
+
// the wire encode — status defaults here, before any middleware observes the response
|
|
104
|
+
return (0, asApiGatewayResponsePayload_1.asApiGatewayResponsePayload)({ response: validatedResponse });
|
|
67
105
|
};
|
|
68
|
-
//
|
|
69
|
-
// 1. error
|
|
70
|
-
// 2.
|
|
71
|
-
// 3.
|
|
72
|
-
// 4.
|
|
73
|
-
// 5.
|
|
74
|
-
// 6.
|
|
75
|
-
// 7.
|
|
76
|
-
// 8.
|
|
77
|
-
// 9.
|
|
106
|
+
// 0. content-type coherence (after + onError) - drops Content-Type when the body is absent
|
|
107
|
+
// 1. constraint error (onError) - a caller fault becomes a 400
|
|
108
|
+
// 2. internal service error (onError) - any other throw becomes a 500
|
|
109
|
+
// 3. io logger (before/after/onError) - debug observability
|
|
110
|
+
// 4. response serializer (after) - encodes the body; skips a preset Content-Type
|
|
111
|
+
// 5. cors (after) - adds cors headers
|
|
112
|
+
// 6. security headers (after) - adds owasp headers
|
|
113
|
+
// 7. event normalization (before) - reconciles a v1/v2 request
|
|
114
|
+
// 8. trail (before) - injects trail context
|
|
115
|
+
// 9. introspection (before) - short-circuits an introspect request
|
|
116
|
+
// 10. body validation (before) - validates event.body against schema.input
|
|
117
|
+
//
|
|
118
|
+
// ⚠️ ORDER IS LOAD-BEARING, ON TWO AXES — middy holds the hook phases in OPPOSITE orders, and
|
|
119
|
+
// a break on either axis no-ops silently: no throw, no log, no type error. state your
|
|
120
|
+
// entry's needs against BOTH before you place it.
|
|
121
|
+
//
|
|
122
|
+
// ── `before` — FORWARD order ── an entry that READS what another `before` PRODUCES sits
|
|
123
|
+
// at a HIGHER index. producers: 7 -> unified `request.event`; 8 -> `request.context.log`
|
|
124
|
+
// + `isContempCaller`; 10 -> the validated `event.body`
|
|
125
|
+
//
|
|
126
|
+
// ── `after` / `onError` — REVERSE order (middy `unshift`s them) ── an entry whose
|
|
127
|
+
// `onError` READS `request.response` sits at a LOWER index than the builders (1,2) that
|
|
128
|
+
// SET it. entry 0 is last on both hooks, which is the only slot that can correct a
|
|
129
|
+
// header the serializer already stamped
|
|
130
|
+
//
|
|
131
|
+
// the `onError` family that reads `request.response` — membership by CAUSE, never by guard
|
|
132
|
+
// syntax (the vendors test `=== undefined`, the io logger tests truthiness):
|
|
133
|
+
// ✅ 0 genContentTypeCoherenceMiddleware -> runs LAST, sees the response
|
|
134
|
+
// ⛔ 3 genIoLoggerMiddleware -> runs EARLY, response undefined
|
|
135
|
+
// ⛔ 5 @middy/http-cors -> so NO cors on a 4xx/5xx
|
|
136
|
+
// ⛔ 6 @middy/http-security-headers -> so NO owasp on a 4xx/5xx
|
|
137
|
+
//
|
|
138
|
+
// the three ⛔ are ONE deferred fix (F32) — the reorder also moves the SUCCESS-path `after`
|
|
139
|
+
// order for every handler. the `before` axis is violated too; see the note on entry 3.
|
|
78
140
|
const middlewares = [
|
|
79
|
-
(0,
|
|
80
|
-
|
|
141
|
+
(0, genContentTypeCoherenceMiddleware_1.genContentTypeCoherenceMiddleware)(),
|
|
142
|
+
/**
|
|
143
|
+
* .note = both keep the invocation a SUCCESS — a caller fault must not emit a cloudwatch
|
|
144
|
+
* error (invariant.badrequesterror-not-lambda-error) — and the 500 body carries a
|
|
145
|
+
* generic message plus the correlation id, never the error's own message
|
|
146
|
+
*
|
|
147
|
+
* .note = this family is ALWAYS-ANCIENT by construction, so the shared middleware's
|
|
148
|
+
* `isContempCaller` branch resolves one way and needs no guard: an http payload is
|
|
149
|
+
* an api-gateway envelope, which `getIsWrappedPayload` cannot match, and where trail
|
|
150
|
+
* never ran `asContextTrailed` defaults the flag to `false`
|
|
151
|
+
*/
|
|
152
|
+
(0, genConstraintErrorMiddleware_1.genConstraintErrorMiddleware)({
|
|
153
|
+
asOutputAfter: (body) => (0, asApiGatewayResponsePayloadJson_1.asApiGatewayResponsePayloadJson)({ status: 400, body }),
|
|
154
|
+
}),
|
|
155
|
+
(0, genInternalServiceErrorMiddleware_1.genInternalServiceErrorMiddleware)({
|
|
156
|
+
asOutputAfter: ({ exid }) => (0, asApiGatewayResponsePayloadJson_1.asApiGatewayResponsePayloadJson)({
|
|
157
|
+
status: 500,
|
|
158
|
+
body: {
|
|
159
|
+
errorMessage: 'internal server error',
|
|
160
|
+
errorType: 'InternalServiceError',
|
|
161
|
+
...(exid ? { correlationId: exid } : {}),
|
|
162
|
+
},
|
|
163
|
+
}),
|
|
164
|
+
}),
|
|
165
|
+
/**
|
|
166
|
+
* ⚠️ .breaks the `before` axis, deliberately — this reads `request.context.log`, which
|
|
167
|
+
* `genTrailMiddleware` (8) produces, so `handler.input` never logs. the record is on
|
|
168
|
+
* `logTranslate`; the repair moves TRAIL to index 3, never this entry — trail declares
|
|
169
|
+
* only a `before`, so its move cannot disturb the `after`/`onError` order
|
|
170
|
+
*/
|
|
81
171
|
(0, genIoLoggerMiddleware_1.genIoLoggerMiddleware)({ logTranslate: config.logTranslate }),
|
|
82
172
|
(0, http_response_serializer_1.default)({
|
|
83
173
|
serializers,
|
|
84
174
|
defaultContentType: 'application/json',
|
|
85
175
|
}),
|
|
176
|
+
/**
|
|
177
|
+
* ⚠️ .breaks the `onError` axis, deliberately — these two sit BELOW the error builders, so
|
|
178
|
+
* their `onError` hooks run first, see no response, and no-op. that is limit 2 on
|
|
179
|
+
* `CorsConfig`. the repair also moves their `after` hooks relative to the serializer,
|
|
180
|
+
* so it changes every SUCCESS response's headers too
|
|
181
|
+
*/
|
|
86
182
|
...(config.cors ? [(0, http_cors_1.default)(corsInputToCorsConfig(config.cors))] : []),
|
|
87
183
|
(0, http_security_headers_1.default)({
|
|
88
184
|
frameOptions: { action: 'DENY' },
|
|
@@ -92,14 +188,26 @@ const forApiGateway = (config, context) => {
|
|
|
92
188
|
(0, genIntrospectionMiddleware_1.genIntrospectionMiddleware)({
|
|
93
189
|
schema: config.schema,
|
|
94
190
|
env: context?.env,
|
|
95
|
-
|
|
191
|
+
/**
|
|
192
|
+
* .what = this family keeps `inputAfter` at `event.body`, and owes a WIRE payload back
|
|
193
|
+
* .why = `request.event` must stay http-shaped for `@middy/http-cors`, so the caller's
|
|
194
|
+
* value rides at `event.body` rather than in the event's place. and the
|
|
195
|
+
* short-circuit bypasses the serializer, so the body is stringified here
|
|
196
|
+
*/
|
|
197
|
+
asInputAfter: (request) => request.event?.body,
|
|
198
|
+
asOutputAfter: (schema) => (0, asApiGatewayResponsePayloadJson_1.asApiGatewayResponsePayloadJson)({ status: 200, body: schema }),
|
|
96
199
|
}),
|
|
97
200
|
(0, genZodBodyValidationMiddleware_1.genZodBodyValidationMiddleware)({ schema: config.schema.input }),
|
|
98
201
|
];
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
202
|
+
/**
|
|
203
|
+
* .as = the chain translates both ends, and typescript cannot track a transform across a
|
|
204
|
+
* middleware chain:
|
|
205
|
+
* - input: ApiGatewayRequestPayload -> TInput (before logic)
|
|
206
|
+
* - output: ApiGatewayResponsePayload<TBody> -> ApiGatewayResponsePayload (after)
|
|
207
|
+
* the second is the serializer's step — it encodes the body to bytes
|
|
208
|
+
* .removal = drops if middy gains typed inference across `.use`; every shape it spans is
|
|
209
|
+
* already named, so only the cast is owed
|
|
210
|
+
*/
|
|
103
211
|
return (0, core_1.default)(logic).use(middlewares);
|
|
104
212
|
};
|
|
105
213
|
exports.forApiGateway = forApiGateway;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"genLambdaEndpoint.forApiGateway.js","sourceRoot":"","sources":["../../../../src/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/genLambdaEndpoint.forApiGateway.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAgC;AAChC,iEAAwC;AACxC,+FAAqE;AACrE,yFAA+D;
|
|
1
|
+
{"version":3,"file":"genLambdaEndpoint.forApiGateway.js","sourceRoot":"","sources":["../../../../src/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/genLambdaEndpoint.forApiGateway.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAgC;AAChC,iEAAwC;AACxC,+FAAqE;AACrE,yFAA+D;AAE/D,mDAAkD;AAQlD,4DAAyD;AACzD,6FAA0F;AAC1F,uGAAoG;AACpG,yFAAsF;AACtF,+EAA4E;AAC5E,yEAAsE;AACtE,yEAAsE;AAEtE,+EAA4E;AAC5E,uFAAoF;AACpF,yEAAiE;AACjE,6EAA0E;AAC1E,iEAA8D;AAC9D,sHAAmH;AACnH,sGAAmG;AACnG,gGAA6F;AAgI7F,8EAA8E;AAC9E,MAAM,qBAAqB,GAAG,CAAC,IAAgB,EAAE,EAAE;IACjD,OAAO;QACL;;;;;;;WAOG;QACH,GAAG,CAAC,IAAI,CAAC,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC;QACvE,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,OAAO,EAAE,IAAI,CAAC,OAAO,IAAI,4BAA4B;QAErD;;;;;WAKG;KACJ,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG;IAClB;QACE,KA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AAY,EAAE,CAAC;QAC5D,IAAA,kCAAsB,EAAC;YACrB,WAAW;YACX,kBAAkB,EAAE,kBAAkB;SACvC,CAAC;QACF;;;;;WAKG;QACH,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAA,mBAAQ,EAAC,qBAAqB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACtE,IAAA,+BAAmB,EAAC;YAClB,YAAY,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE;SACjC,CAAC;QACF,IAAA,qFAAyC,EAAC,EAAE,SAAS,EAAE,WAAW,CAAC,IAAI,EAAE,CAAC;QAC1E,IAAA,uCAAkB,GAAE;QACpB,IAAA,uDAA0B,EAAC;YACzB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,GAAG,EAAE,OAAO,EAAE,GAAG;YACjB;;;;;eAKG;YACH,YAAY,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI;YAC9C,aAAa,EAAE,CAAC,MAAM,EAAE,EAAE,CACxB,IAAA,iEAA+B,EAAC,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;SACjE,CAAC;QACF,IAAA,+DAA8B,EAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;KAChE,CAAC;IAEF;;;;;;;;OAQG;IACH,OAAO,IAAA,cAAK,EACV,KAGuC,CACxC,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;AACrB,CAAC,CAAC;AA3LW,QAAA,aAAa,iBA2LxB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* .what = lists which aws wire-payload keys a handler's response carries
|
|
3
|
+
* .why = one detector, so the public type-check and the handler-side guard cannot drift. the
|
|
4
|
+
* check needs a boolean; the guard needs the key NAMES to tell the author the fix
|
|
5
|
+
*/
|
|
6
|
+
export declare const getAllWirePayloadKeysFound: (input: {
|
|
7
|
+
response: object;
|
|
8
|
+
}) => string[];
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAllWirePayloadKeysFound = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* .what = the wire keys a mid-migration consumer writes from habit, every one of which the
|
|
6
|
+
* encode would drop without a word
|
|
7
|
+
*
|
|
8
|
+
* .note = `statusCode` is the severe one — the one field whose name changed (`status`), so
|
|
9
|
+
* `status ?? 200` replaces a caller's 404 with a 200 AND double-encodes the body they
|
|
10
|
+
* already stringified. the rest are deferred keys (F7), quieter but equally silent
|
|
11
|
+
*
|
|
12
|
+
* .note = a `Record<K, true>` rather than a `string[]`, so the compiler holds it EXHAUSTIVE: a
|
|
13
|
+
* list proves each entry is real and says not one word about one being ABSENT, which is
|
|
14
|
+
* the drift that matters. clamp proven to bite — drop a key and tsc raises TS2741
|
|
15
|
+
*/
|
|
16
|
+
const IS_KEY_OF_WIRE_PAYLOAD_UNREAD = {
|
|
17
|
+
statusCode: true,
|
|
18
|
+
multiValueHeaders: true,
|
|
19
|
+
isBase64Encoded: true,
|
|
20
|
+
cookies: true,
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* .what = the same list, as keys
|
|
24
|
+
* .why = one source of truth, so the exhaustive map and the iterated list cannot drift apart
|
|
25
|
+
*/
|
|
26
|
+
const KEYS_OF_WIRE_PAYLOAD = Object.keys(IS_KEY_OF_WIRE_PAYLOAD_UNREAD);
|
|
27
|
+
/**
|
|
28
|
+
* .what = lists which aws wire-payload keys a handler's response carries
|
|
29
|
+
* .why = one detector, so the public type-check and the handler-side guard cannot drift. the
|
|
30
|
+
* check needs a boolean; the guard needs the key NAMES to tell the author the fix
|
|
31
|
+
*/
|
|
32
|
+
const getAllWirePayloadKeysFound = (input) => KEYS_OF_WIRE_PAYLOAD.filter((key) => key in input.response);
|
|
33
|
+
exports.getAllWirePayloadKeysFound = getAllWirePayloadKeysFound;
|
|
34
|
+
//# sourceMappingURL=getAllWirePayloadKeysFound.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getAllWirePayloadKeysFound.js","sourceRoot":"","sources":["../../../../src/domain.operations/genLambdaEndpoint/genLambdaEndpoint.forApiGateway/getAllWirePayloadKeysFound.ts"],"names":[],"mappings":";;;AAgBA;;;;;;;;;;;GAWG;AACH,MAAM,6BAA6B,GAAyC;IAC1E,UAAU,EAAE,IAAI;IAChB,iBAAiB,EAAE,IAAI;IACvB,eAAe,EAAE,IAAI;IACrB,OAAO,EAAE,IAAI;CACd,CAAC;AAEF;;;GAGG;AACH,MAAM,oBAAoB,GAAa,MAAM,CAAC,IAAI,CAChD,6BAA6B,CAC9B,CAAC;AAEF;;;;GAIG;AACI,MAAM,0BAA0B,GAAG,CAAC,KAE1C,EAAY,EAAE,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,IAAI,KAAK,CAAC,QAAQ,CAAC,CAAC;AAF/D,QAAA,0BAA0B,8BAEqC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ApiGatewayResponse } from '../../../domain.objects/ApiGatewayResponse';
|
|
2
|
+
/**
|
|
3
|
+
* .what = checks that a value is an ApiGatewayResponse — an object with at least one of
|
|
4
|
+
* status, headers, or body, and none of the aws wire-payload keys
|
|
5
|
+
* .why = the type refuses `{}` at compile time, but a handler's return crosses an
|
|
6
|
+
* `unknown` boundary at run time (a js caller, an `any`, a cast). this is the
|
|
7
|
+
* runtime backstop for the invariants the type holds
|
|
8
|
+
*
|
|
9
|
+
* .note = it checks CARDINALITY and FOREIGN KEYS, not field types. the field types are
|
|
10
|
+
* `schema.output`'s job; this exists so an empty, non-object, or wire-shaped return
|
|
11
|
+
* fails loud HERE rather than reach middy's `statusCode ??= 500` and emit a silent 500
|
|
12
|
+
*
|
|
13
|
+
* .note = the foreign-key half closes a MIGRATION hazard the cardinality half could not see.
|
|
14
|
+
* `{ statusCode: 404, body: JSON.stringify(x) }` — the aws-native shape, and the one
|
|
15
|
+
* the wish's own `.ground` evidence used — satisfies `'body' in input`, so it used to
|
|
16
|
+
* pass. it then lost its 404 to `status ?? 200` and had its body encoded twice
|
|
17
|
+
* (rule.forbid.failhide)
|
|
18
|
+
*/
|
|
19
|
+
export declare const isApiGatewayResponse: import("type-fns").AssessWithAssure<unknown, ApiGatewayResponse<unknown>>;
|