@geekmidas/constructs 3.0.0 → 3.0.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/CHANGELOG.md +6 -0
- package/dist/AWSLambdaFunction-BF1gJJjG.d.mts.map +1 -1
- package/dist/{AWSLambdaFunction-wtE9Lx_-.cjs → AWSLambdaFunction-BVpc9xFN.cjs} +14 -2
- package/dist/AWSLambdaFunction-BVpc9xFN.cjs.map +1 -0
- package/dist/{AWSLambdaFunction-BDL7Thi_.mjs → AWSLambdaFunction-OPazEPhQ.mjs} +14 -2
- package/dist/AWSLambdaFunction-OPazEPhQ.mjs.map +1 -0
- package/dist/AWSLambdaFunction-tzqv5XU6.d.cts.map +1 -1
- package/dist/{AWSLambdaSubscriberAdaptor-BtBKExsh.cjs → AWSLambdaSubscriberAdaptor-BL7Yl1XE.cjs} +13 -2
- package/dist/AWSLambdaSubscriberAdaptor-BL7Yl1XE.cjs.map +1 -0
- package/dist/{AWSLambdaSubscriberAdaptor-DsRmmGm1.mjs → AWSLambdaSubscriberAdaptor-YxZ_z3XG.mjs} +14 -3
- package/dist/AWSLambdaSubscriberAdaptor-YxZ_z3XG.mjs.map +1 -0
- package/dist/{HonoEndpointAdaptor-vI3LPiyu.d.cts → HonoEndpointAdaptor-BCql3gLP.d.cts} +3 -3
- package/dist/{HonoEndpointAdaptor-vI3LPiyu.d.cts.map → HonoEndpointAdaptor-BCql3gLP.d.cts.map} +1 -1
- package/dist/TestFunctionAdaptor-BHk2bDOV.d.cts.map +1 -1
- package/dist/{TestFunctionAdaptor-CK-dbKpO.mjs → TestFunctionAdaptor-C5CLuR-9.mjs} +25 -17
- package/dist/TestFunctionAdaptor-C5CLuR-9.mjs.map +1 -0
- package/dist/{TestFunctionAdaptor-CSMX7myB.cjs → TestFunctionAdaptor-CH6-gO_B.cjs} +24 -16
- package/dist/TestFunctionAdaptor-CH6-gO_B.cjs.map +1 -0
- package/dist/TestFunctionAdaptor-DzT8GRdR.d.mts.map +1 -1
- package/dist/TestSubscriberAdaptor-CPEiqSfE.d.mts.map +1 -1
- package/dist/{TestSubscriberAdaptor-CrqLkyUE.mjs → TestSubscriberAdaptor-CwP3qNJ4.mjs} +22 -14
- package/dist/TestSubscriberAdaptor-CwP3qNJ4.mjs.map +1 -0
- package/dist/TestSubscriberAdaptor-DQk5xzb0.d.cts.map +1 -1
- package/dist/{TestSubscriberAdaptor-1m-2bM_j.cjs → TestSubscriberAdaptor-Dgxr_0vL.cjs} +21 -13
- package/dist/TestSubscriberAdaptor-Dgxr_0vL.cjs.map +1 -0
- package/dist/adaptors/aws.cjs +2 -2
- package/dist/adaptors/aws.d.cts +1 -1
- package/dist/adaptors/aws.d.mts +1 -1
- package/dist/adaptors/aws.mjs +2 -2
- package/dist/adaptors/hono.d.cts +2 -2
- package/dist/adaptors/hono.d.mts +1 -1
- package/dist/adaptors/testing.cjs +2 -2
- package/dist/adaptors/testing.d.cts +1 -1
- package/dist/adaptors/testing.d.mts +1 -1
- package/dist/adaptors/testing.mjs +2 -2
- package/dist/crons/Cron.d.cts +1 -1
- package/dist/crons/Cron.d.mts +1 -1
- package/dist/crons/CronBuilder.d.cts +1 -1
- package/dist/crons/CronBuilder.d.mts +1 -1
- package/dist/crons/index.d.cts +1 -1
- package/dist/crons/index.d.mts +5 -5
- package/dist/endpoints/AmazonApiGatewayEndpointAdaptor.d.cts +1 -1
- package/dist/endpoints/AmazonApiGatewayEndpointAdaptor.d.mts +1 -1
- package/dist/endpoints/AmazonApiGatewayV1EndpointAdaptor.d.cts +1 -1
- package/dist/endpoints/AmazonApiGatewayV1EndpointAdaptor.d.mts +1 -1
- package/dist/endpoints/AmazonApiGatewayV2EndpointAdaptor.d.cts +1 -1
- package/dist/endpoints/AmazonApiGatewayV2EndpointAdaptor.d.mts +1 -1
- package/dist/endpoints/Endpoint.d.cts +1 -1
- package/dist/endpoints/Endpoint.d.mts +1 -1
- package/dist/endpoints/EndpointBuilder.d.cts +1 -1
- package/dist/endpoints/EndpointBuilder.d.mts +1 -1
- package/dist/endpoints/EndpointFactory.d.cts +1 -1
- package/dist/endpoints/EndpointFactory.d.mts +1 -1
- package/dist/endpoints/HonoEndpointAdaptor.d.cts +2 -2
- package/dist/endpoints/HonoEndpointAdaptor.d.mts +1 -1
- package/dist/endpoints/TestEndpointAdaptor.d.cts +1 -1
- package/dist/endpoints/TestEndpointAdaptor.d.mts +1 -1
- package/dist/endpoints/audit.d.cts +1 -1
- package/dist/endpoints/audit.d.mts +1 -1
- package/dist/endpoints/helpers.d.cts +1 -1
- package/dist/endpoints/helpers.d.mts +1 -1
- package/dist/endpoints/index.d.cts +1 -1
- package/dist/endpoints/index.d.mts +1 -1
- package/dist/endpoints/lazyAccessors.d.cts +1 -1
- package/dist/endpoints/lazyAccessors.d.mts +1 -1
- package/dist/endpoints/processAudits.d.cts +1 -1
- package/dist/endpoints/processAudits.d.mts +1 -1
- package/dist/endpoints/rls.d.cts +1 -1
- package/dist/endpoints/rls.d.mts +1 -1
- package/dist/functions/AWSLambdaFunction.cjs +1 -1
- package/dist/functions/AWSLambdaFunction.mjs +1 -1
- package/dist/functions/TestFunctionAdaptor.cjs +1 -1
- package/dist/functions/TestFunctionAdaptor.mjs +1 -1
- package/dist/functions/index.d.cts +1 -1
- package/dist/functions/index.d.mts +1 -1
- package/dist/{index-DQo_Csy7.d.mts → index-BfeupgMl.d.cts} +2 -2
- package/dist/{index-BnoOaBQV.d.cts.map → index-BfeupgMl.d.cts.map} +1 -1
- package/dist/index-_5DYCNAt.d.mts +12 -0
- package/dist/{index-DQo_Csy7.d.mts.map → index-_5DYCNAt.d.mts.map} +1 -1
- package/dist/subscribers/AWSLambdaSubscriberAdaptor.cjs +1 -1
- package/dist/subscribers/AWSLambdaSubscriberAdaptor.mjs +1 -1
- package/dist/subscribers/TestSubscriberAdaptor.cjs +1 -1
- package/dist/subscribers/TestSubscriberAdaptor.mjs +1 -1
- package/package.json +6 -6
- package/src/functions/AWSLambdaFunction.ts +16 -2
- package/src/functions/TestFunctionAdaptor.ts +38 -31
- package/src/functions/__tests__/AWSLambdaFunctionAdaptor.spec.ts +55 -0
- package/src/functions/__tests__/TestFunctionAdaptor.spec.ts +82 -0
- package/src/subscribers/AWSLambdaSubscriberAdaptor.ts +17 -2
- package/src/subscribers/TestSubscriberAdaptor.ts +35 -29
- package/src/subscribers/__tests__/AWSLambdaSubscriberAdaptor.spec.ts +64 -0
- package/src/subscribers/__tests__/TestSubscriberAdaptor.spec.ts +94 -0
- package/dist/AWSLambdaFunction-BDL7Thi_.mjs.map +0 -1
- package/dist/AWSLambdaFunction-wtE9Lx_-.cjs.map +0 -1
- package/dist/AWSLambdaSubscriberAdaptor-BtBKExsh.cjs.map +0 -1
- package/dist/AWSLambdaSubscriberAdaptor-DsRmmGm1.mjs.map +0 -1
- package/dist/TestFunctionAdaptor-CK-dbKpO.mjs.map +0 -1
- package/dist/TestFunctionAdaptor-CSMX7myB.cjs.map +0 -1
- package/dist/TestSubscriberAdaptor-1m-2bM_j.cjs.map +0 -1
- package/dist/TestSubscriberAdaptor-CrqLkyUE.mjs.map +0 -1
- package/dist/index-BnoOaBQV.d.cts +0 -12
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FunctionBuilder } from "./FunctionBuilder-
|
|
1
|
+
import { FunctionBuilder } from "./FunctionBuilder-D_7f5MfS.cjs";
|
|
2
2
|
import * as _geekmidas_audit5 from "@geekmidas/audit";
|
|
3
3
|
import * as _geekmidas_logger4 from "@geekmidas/logger";
|
|
4
4
|
import * as _geekmidas_schema3 from "@geekmidas/schema";
|
|
@@ -9,4 +9,4 @@ declare const f: FunctionBuilder<_geekmidas_schema3.ComposableStandardSchema, un
|
|
|
9
9
|
|
|
10
10
|
//#endregion
|
|
11
11
|
export { f };
|
|
12
|
-
//# sourceMappingURL=index-
|
|
12
|
+
//# sourceMappingURL=index-BfeupgMl.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-
|
|
1
|
+
{"version":3,"file":"index-BfeupgMl.d.cts","names":[],"sources":["../src/functions/index.ts"],"sourcesContent":[],"mappings":";;;;;;cAWa,GAAC,gBAAwB,kBAAA,CAAxB,wBAAA,iBAAA,kBAAA,CAAA,MAAA,2DAAA,iBAAA,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FunctionBuilder } from "./FunctionBuilder-m5BNA2Ap.mjs";
|
|
2
|
+
import * as _geekmidas_audit2 from "@geekmidas/audit";
|
|
3
|
+
import * as _geekmidas_logger1 from "@geekmidas/logger";
|
|
4
|
+
import * as _geekmidas_schema0 from "@geekmidas/schema";
|
|
5
|
+
|
|
6
|
+
//#region src/functions/index.d.ts
|
|
7
|
+
declare const f: FunctionBuilder<_geekmidas_schema0.ComposableStandardSchema, undefined, [], _geekmidas_logger1.Logger, undefined, string, undefined, string, undefined, string, _geekmidas_audit2.AuditableAction<string, unknown>>;
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { f };
|
|
12
|
+
//# sourceMappingURL=index-_5DYCNAt.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-
|
|
1
|
+
{"version":3,"file":"index-_5DYCNAt.d.mts","names":[],"sources":["../src/functions/index.ts"],"sourcesContent":[],"mappings":";;;;;;cAWa,GAAC,gBAAwB,kBAAA,CAAxB,wBAAA,iBAAA,kBAAA,CAAA,MAAA,2DAAA,iBAAA,CAAA"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
const require_AWSLambdaSubscriberAdaptor = require('../AWSLambdaSubscriberAdaptor-
|
|
1
|
+
const require_AWSLambdaSubscriberAdaptor = require('../AWSLambdaSubscriberAdaptor-BL7Yl1XE.cjs');
|
|
2
2
|
|
|
3
3
|
exports.AWSLambdaSubscriber = require_AWSLambdaSubscriberAdaptor.AWSLambdaSubscriber;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
require('../publisher-lFQleddL.cjs');
|
|
2
|
-
const require_TestSubscriberAdaptor = require('../TestSubscriberAdaptor-
|
|
2
|
+
const require_TestSubscriberAdaptor = require('../TestSubscriberAdaptor-Dgxr_0vL.cjs');
|
|
3
3
|
|
|
4
4
|
exports.TestSubscriberAdaptor = require_TestSubscriberAdaptor.TestSubscriberAdaptor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/constructs",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -81,24 +81,24 @@
|
|
|
81
81
|
"@geekmidas/audit": "^2.0.0",
|
|
82
82
|
"@geekmidas/cache": "^1.1.0",
|
|
83
83
|
"@geekmidas/db": "^1.0.0",
|
|
84
|
-
"@geekmidas/envkit": "^1.0.3",
|
|
85
84
|
"@geekmidas/errors": "^1.0.0",
|
|
85
|
+
"@geekmidas/envkit": "^1.0.3",
|
|
86
86
|
"@geekmidas/events": "^1.1.0",
|
|
87
|
-
"@geekmidas/logger": "^1.0.0",
|
|
88
|
-
"@geekmidas/rate-limit": "^2.0.0",
|
|
89
87
|
"@geekmidas/schema": "^1.0.0",
|
|
90
88
|
"@geekmidas/services": "^1.0.1",
|
|
91
|
-
"@geekmidas/
|
|
89
|
+
"@geekmidas/logger": "^1.0.0",
|
|
90
|
+
"@geekmidas/testkit": "^1.0.2",
|
|
91
|
+
"@geekmidas/rate-limit": "^2.0.0"
|
|
92
92
|
},
|
|
93
93
|
"peerDependencies": {
|
|
94
94
|
"@middy/core": ">=6.3.1",
|
|
95
95
|
"@types/aws-lambda": ">=8.10.92",
|
|
96
96
|
"hono": ">=4.8.2",
|
|
97
97
|
"@geekmidas/audit": "^2.0.0",
|
|
98
|
-
"@geekmidas/cache": "^1.1.0",
|
|
99
98
|
"@geekmidas/db": "^1.0.0",
|
|
100
99
|
"@geekmidas/envkit": "^1.0.3",
|
|
101
100
|
"@geekmidas/errors": "^1.0.0",
|
|
101
|
+
"@geekmidas/cache": "^1.1.0",
|
|
102
102
|
"@geekmidas/events": "^1.1.0",
|
|
103
103
|
"@geekmidas/logger": "^1.0.0",
|
|
104
104
|
"@geekmidas/rate-limit": "^2.0.0",
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
InferStandardSchema,
|
|
10
10
|
} from '@geekmidas/schema';
|
|
11
11
|
import type { Service, ServiceRecord } from '@geekmidas/services';
|
|
12
|
+
import { runWithRequestContext } from '@geekmidas/services';
|
|
12
13
|
import middy, { type MiddlewareObj } from '@middy/core';
|
|
13
14
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
14
15
|
import type { Context, Handler } from 'aws-lambda';
|
|
@@ -321,7 +322,7 @@ export class AWSLambdaFunction<
|
|
|
321
322
|
const handler = this._handler.bind(this);
|
|
322
323
|
|
|
323
324
|
// Apply middleware in order
|
|
324
|
-
|
|
325
|
+
const chain = middy(handler)
|
|
325
326
|
.use(this.loggerMiddleware())
|
|
326
327
|
.use(this.baseInput())
|
|
327
328
|
.use(this.error())
|
|
@@ -329,6 +330,19 @@ export class AWSLambdaFunction<
|
|
|
329
330
|
.use(this.database())
|
|
330
331
|
.use(this.auditor())
|
|
331
332
|
.use(this.input())
|
|
332
|
-
.use(this.events())
|
|
333
|
+
.use(this.events());
|
|
334
|
+
|
|
335
|
+
// Wrap entire Middy chain in request context for service access
|
|
336
|
+
const wrappedHandler = async (event: unknown, context: Context) => {
|
|
337
|
+
const startTime = Date.now();
|
|
338
|
+
const requestId = context.awsRequestId;
|
|
339
|
+
const logger = this.fn.logger.child({ requestId }) as TLogger;
|
|
340
|
+
|
|
341
|
+
return runWithRequestContext({ logger, requestId, startTime }, () =>
|
|
342
|
+
chain(event as Parameters<typeof chain>[0], context),
|
|
343
|
+
);
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
return wrappedHandler as unknown as AWSLambdaHandler;
|
|
333
347
|
}
|
|
334
348
|
}
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
InferStandardSchema,
|
|
10
10
|
} from '@geekmidas/schema';
|
|
11
11
|
import {
|
|
12
|
+
runWithRequestContext,
|
|
12
13
|
type Service,
|
|
13
14
|
ServiceDiscovery,
|
|
14
15
|
type ServiceRecord,
|
|
@@ -153,41 +154,47 @@ export class TestFunctionAdaptor<
|
|
|
153
154
|
});
|
|
154
155
|
}
|
|
155
156
|
|
|
156
|
-
//
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
);
|
|
176
|
-
|
|
157
|
+
// Wrap execution in request context for service access via context.getLogger()
|
|
158
|
+
const requestId = `test-${Date.now()}`;
|
|
159
|
+
const startTime = Date.now();
|
|
160
|
+
|
|
161
|
+
return runWithRequestContext({ logger, requestId, startTime }, async () => {
|
|
162
|
+
// Execute the function
|
|
163
|
+
const response = await this.fn.fn({
|
|
164
|
+
input: parsedInput,
|
|
165
|
+
services,
|
|
166
|
+
logger,
|
|
167
|
+
db,
|
|
168
|
+
auditor,
|
|
169
|
+
} as any);
|
|
170
|
+
|
|
171
|
+
// Parse output if schema is provided
|
|
172
|
+
const output = await this.fn.parseOutput(response);
|
|
173
|
+
|
|
174
|
+
// Flush audits if any were recorded
|
|
175
|
+
if (auditor) {
|
|
176
|
+
const records = auditor.getRecords();
|
|
177
|
+
if (records.length > 0) {
|
|
178
|
+
logger.debug(
|
|
179
|
+
{ auditCount: records.length },
|
|
180
|
+
'Flushing function audits',
|
|
181
|
+
);
|
|
182
|
+
await auditor.flush();
|
|
183
|
+
}
|
|
177
184
|
}
|
|
178
|
-
}
|
|
179
185
|
|
|
180
|
-
|
|
186
|
+
// Register publisher service if provided in context
|
|
181
187
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
188
|
+
await publishEvents(
|
|
189
|
+
logger,
|
|
190
|
+
this.serviceDiscovery,
|
|
191
|
+
this.fn.events,
|
|
192
|
+
output,
|
|
193
|
+
this.fn.publisherService,
|
|
194
|
+
);
|
|
189
195
|
|
|
190
|
-
|
|
196
|
+
return output;
|
|
197
|
+
}) as Promise<InferStandardSchema<TOutSchema>>;
|
|
191
198
|
}
|
|
192
199
|
}
|
|
193
200
|
|
|
@@ -2,6 +2,7 @@ import { EnvironmentParser } from '@geekmidas/envkit';
|
|
|
2
2
|
import type { EventPublisher, PublishableMessage } from '@geekmidas/events';
|
|
3
3
|
import { ConsoleLogger } from '@geekmidas/logger/console';
|
|
4
4
|
import type { Service } from '@geekmidas/services';
|
|
5
|
+
import { serviceContext } from '@geekmidas/services';
|
|
5
6
|
import type { Context } from 'aws-lambda';
|
|
6
7
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
7
8
|
import { z } from 'zod/v4';
|
|
@@ -455,4 +456,58 @@ describe('AWSLambdaFunction', () => {
|
|
|
455
456
|
expect(result).toEqual({ hasDb: false });
|
|
456
457
|
});
|
|
457
458
|
});
|
|
459
|
+
|
|
460
|
+
describe('request context', () => {
|
|
461
|
+
it('should make serviceContext.getLogger() available inside handler', async () => {
|
|
462
|
+
let contextLogger: any;
|
|
463
|
+
let contextRequestId: string | undefined;
|
|
464
|
+
|
|
465
|
+
const fn = new Function(
|
|
466
|
+
async () => {
|
|
467
|
+
contextLogger = serviceContext.getLogger();
|
|
468
|
+
contextRequestId = serviceContext.getRequestId();
|
|
469
|
+
return { success: true };
|
|
470
|
+
},
|
|
471
|
+
undefined,
|
|
472
|
+
undefined,
|
|
473
|
+
undefined,
|
|
474
|
+
undefined,
|
|
475
|
+
[],
|
|
476
|
+
logger,
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
const adaptor = new AWSLambdaFunction(envParser, fn);
|
|
480
|
+
const handler = adaptor.handler;
|
|
481
|
+
|
|
482
|
+
const context = createMockContext();
|
|
483
|
+
await handler({}, context, vi.fn());
|
|
484
|
+
|
|
485
|
+
expect(contextLogger).toBeDefined();
|
|
486
|
+
expect(contextRequestId).toBe('test-request-id');
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it('should make serviceContext.hasContext() return true inside handler', async () => {
|
|
490
|
+
let hasContext = false;
|
|
491
|
+
|
|
492
|
+
const fn = new Function(
|
|
493
|
+
async () => {
|
|
494
|
+
hasContext = serviceContext.hasContext();
|
|
495
|
+
return { success: true };
|
|
496
|
+
},
|
|
497
|
+
undefined,
|
|
498
|
+
undefined,
|
|
499
|
+
undefined,
|
|
500
|
+
undefined,
|
|
501
|
+
[],
|
|
502
|
+
logger,
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
const adaptor = new AWSLambdaFunction(envParser, fn);
|
|
506
|
+
const handler = adaptor.handler;
|
|
507
|
+
|
|
508
|
+
await handler({}, createMockContext(), vi.fn());
|
|
509
|
+
|
|
510
|
+
expect(hasContext).toBe(true);
|
|
511
|
+
});
|
|
512
|
+
});
|
|
458
513
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { EventPublisher, PublishableMessage } from '@geekmidas/events';
|
|
2
2
|
import { ConsoleLogger } from '@geekmidas/logger/console';
|
|
3
3
|
import type { Service } from '@geekmidas/services';
|
|
4
|
+
import { serviceContext } from '@geekmidas/services';
|
|
4
5
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
6
|
import { z } from 'zod/v4';
|
|
6
7
|
|
|
@@ -395,4 +396,85 @@ describe.skip('TestFunctionAdaptor', () => {
|
|
|
395
396
|
).rejects.toThrow('Function failed');
|
|
396
397
|
});
|
|
397
398
|
});
|
|
399
|
+
|
|
400
|
+
describe('request context', () => {
|
|
401
|
+
it('should make serviceContext.getLogger() available inside handler', async () => {
|
|
402
|
+
let contextLogger: any;
|
|
403
|
+
|
|
404
|
+
const fn = new Function(
|
|
405
|
+
async () => {
|
|
406
|
+
contextLogger = serviceContext.getLogger();
|
|
407
|
+
return { success: true };
|
|
408
|
+
},
|
|
409
|
+
undefined,
|
|
410
|
+
undefined,
|
|
411
|
+
undefined,
|
|
412
|
+
undefined,
|
|
413
|
+
[],
|
|
414
|
+
logger,
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
const adaptor = new TestFunctionAdaptor(fn);
|
|
418
|
+
|
|
419
|
+
await adaptor.invoke({
|
|
420
|
+
input: {},
|
|
421
|
+
services: {},
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
expect(contextLogger).toBeDefined();
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it('should make serviceContext.hasContext() return true inside handler', async () => {
|
|
428
|
+
let hasContext = false;
|
|
429
|
+
|
|
430
|
+
const fn = new Function(
|
|
431
|
+
async () => {
|
|
432
|
+
hasContext = serviceContext.hasContext();
|
|
433
|
+
return { success: true };
|
|
434
|
+
},
|
|
435
|
+
undefined,
|
|
436
|
+
undefined,
|
|
437
|
+
undefined,
|
|
438
|
+
undefined,
|
|
439
|
+
[],
|
|
440
|
+
logger,
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
const adaptor = new TestFunctionAdaptor(fn);
|
|
444
|
+
|
|
445
|
+
await adaptor.invoke({
|
|
446
|
+
input: {},
|
|
447
|
+
services: {},
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
expect(hasContext).toBe(true);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it('should provide a request ID starting with test-', async () => {
|
|
454
|
+
let requestId: string | undefined;
|
|
455
|
+
|
|
456
|
+
const fn = new Function(
|
|
457
|
+
async () => {
|
|
458
|
+
requestId = serviceContext.getRequestId();
|
|
459
|
+
return { success: true };
|
|
460
|
+
},
|
|
461
|
+
undefined,
|
|
462
|
+
undefined,
|
|
463
|
+
undefined,
|
|
464
|
+
undefined,
|
|
465
|
+
[],
|
|
466
|
+
logger,
|
|
467
|
+
);
|
|
468
|
+
|
|
469
|
+
const adaptor = new TestFunctionAdaptor(fn);
|
|
470
|
+
|
|
471
|
+
await adaptor.invoke({
|
|
472
|
+
input: {},
|
|
473
|
+
services: {},
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
expect(requestId).toBeDefined();
|
|
477
|
+
expect(requestId).toMatch(/^test-/);
|
|
478
|
+
});
|
|
479
|
+
});
|
|
398
480
|
});
|
|
@@ -4,7 +4,7 @@ import type { EventPublisher } from '@geekmidas/events';
|
|
|
4
4
|
import type { Logger } from '@geekmidas/logger';
|
|
5
5
|
import type { InferStandardSchema } from '@geekmidas/schema';
|
|
6
6
|
import type { Service, ServiceRecord } from '@geekmidas/services';
|
|
7
|
-
import { ServiceDiscovery } from '@geekmidas/services';
|
|
7
|
+
import { runWithRequestContext, ServiceDiscovery } from '@geekmidas/services';
|
|
8
8
|
import middy, { type MiddlewareObj } from '@middy/core';
|
|
9
9
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
10
10
|
import type {
|
|
@@ -318,10 +318,25 @@ export class AWSLambdaSubscriber<
|
|
|
318
318
|
const handler = this._handler.bind(this);
|
|
319
319
|
|
|
320
320
|
// Apply middleware in order
|
|
321
|
-
|
|
321
|
+
const chain = middy(handler)
|
|
322
322
|
.use(this.loggerMiddleware())
|
|
323
323
|
.use(this.parseEvents())
|
|
324
324
|
.use(this.error())
|
|
325
325
|
.use(this.services());
|
|
326
|
+
|
|
327
|
+
// Wrap entire Middy chain in request context for service access
|
|
328
|
+
const wrappedHandler = async (event: unknown, context: Context) => {
|
|
329
|
+
const startTime = Date.now();
|
|
330
|
+
const requestId = context.awsRequestId;
|
|
331
|
+
const logger = this.subscriber.logger.child({
|
|
332
|
+
requestId,
|
|
333
|
+
}) as TLogger;
|
|
334
|
+
|
|
335
|
+
return runWithRequestContext({ logger, requestId, startTime }, () =>
|
|
336
|
+
chain(event as Parameters<typeof chain>[0], context),
|
|
337
|
+
);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
return wrappedHandler as unknown as AWSLambdaHandler;
|
|
326
341
|
}
|
|
327
342
|
}
|
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
import type { Logger } from '@geekmidas/logger';
|
|
7
7
|
import type { InferStandardSchema } from '@geekmidas/schema';
|
|
8
8
|
import type { Service, ServiceRecord } from '@geekmidas/services';
|
|
9
|
-
import { ServiceDiscovery } from '@geekmidas/services';
|
|
9
|
+
import { runWithRequestContext, ServiceDiscovery } from '@geekmidas/services';
|
|
10
10
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
11
11
|
import { publishEvents } from '../publisher';
|
|
12
12
|
import type { Subscriber } from './Subscriber';
|
|
@@ -72,36 +72,42 @@ export class TestSubscriberAdaptor<
|
|
|
72
72
|
return { batchItemFailures: [] } as any;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
//
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
75
|
+
// Wrap execution in request context for service access via context.getLogger()
|
|
76
|
+
const requestId = `test-${Date.now()}`;
|
|
77
|
+
const startTime = Date.now();
|
|
78
|
+
|
|
79
|
+
return runWithRequestContext({ logger, requestId, startTime }, async () => {
|
|
80
|
+
// Execute the subscriber handler
|
|
81
|
+
const result = await this.subscriber.handler({
|
|
82
|
+
events: filteredEvents,
|
|
83
|
+
services,
|
|
84
|
+
logger,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Validate output if schema is provided
|
|
88
|
+
let output: any = result;
|
|
89
|
+
if (this.subscriber.outputSchema && result) {
|
|
90
|
+
const validationResult =
|
|
91
|
+
await this.subscriber.outputSchema['~standard'].validate(result);
|
|
92
|
+
|
|
93
|
+
if (validationResult.issues) {
|
|
94
|
+
throw new Error('Subscriber output validation failed');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
output = validationResult.value;
|
|
90
98
|
}
|
|
91
99
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
)
|
|
103
|
-
|
|
104
|
-
return output;
|
|
100
|
+
// Publish events if configured
|
|
101
|
+
await publishEvents(
|
|
102
|
+
logger,
|
|
103
|
+
this.serviceDiscovery,
|
|
104
|
+
this.subscriber.events,
|
|
105
|
+
output,
|
|
106
|
+
this.subscriber.publisherService,
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
return output;
|
|
110
|
+
}) as Promise<InferStandardSchema<OutSchema>>;
|
|
105
111
|
}
|
|
106
112
|
|
|
107
113
|
private filterEvents(
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { EnvironmentParser } from '@geekmidas/envkit';
|
|
2
2
|
import { ConsoleLogger } from '@geekmidas/logger/console';
|
|
3
3
|
import type { Service } from '@geekmidas/services';
|
|
4
|
+
import { serviceContext } from '@geekmidas/services';
|
|
4
5
|
import type {
|
|
5
6
|
Context,
|
|
6
7
|
SNSEvent,
|
|
@@ -951,4 +952,67 @@ describe('AWSLambdaSubscriber', () => {
|
|
|
951
952
|
expect(handler).toHaveBeenCalled();
|
|
952
953
|
});
|
|
953
954
|
});
|
|
955
|
+
|
|
956
|
+
describe('request context', () => {
|
|
957
|
+
it('should make serviceContext.getLogger() available inside handler', async () => {
|
|
958
|
+
let contextLogger: any;
|
|
959
|
+
let contextRequestId: string | undefined;
|
|
960
|
+
|
|
961
|
+
const handler = vi.fn(async () => {
|
|
962
|
+
contextLogger = serviceContext.getLogger();
|
|
963
|
+
contextRequestId = serviceContext.getRequestId();
|
|
964
|
+
return {};
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
const subscriber = new Subscriber(
|
|
968
|
+
handler,
|
|
969
|
+
30000,
|
|
970
|
+
['user.created'] as any,
|
|
971
|
+
undefined,
|
|
972
|
+
[],
|
|
973
|
+
logger,
|
|
974
|
+
);
|
|
975
|
+
|
|
976
|
+
const adaptor = new AWSLambdaSubscriber(envParser, subscriber);
|
|
977
|
+
const lambdaHandler = adaptor.handler;
|
|
978
|
+
|
|
979
|
+
const sqsEvent = createSQSEvent([
|
|
980
|
+
{ type: 'user.created', payload: { userId: '1' } },
|
|
981
|
+
]);
|
|
982
|
+
|
|
983
|
+
await lambdaHandler(sqsEvent, createMockContext(), vi.fn());
|
|
984
|
+
|
|
985
|
+
expect(contextLogger).toBeDefined();
|
|
986
|
+
expect(contextRequestId).toBe('test-request-id');
|
|
987
|
+
});
|
|
988
|
+
|
|
989
|
+
it('should make serviceContext.hasContext() return true inside handler', async () => {
|
|
990
|
+
let hasContext = false;
|
|
991
|
+
|
|
992
|
+
const handler = vi.fn(async () => {
|
|
993
|
+
hasContext = serviceContext.hasContext();
|
|
994
|
+
return {};
|
|
995
|
+
});
|
|
996
|
+
|
|
997
|
+
const subscriber = new Subscriber(
|
|
998
|
+
handler,
|
|
999
|
+
30000,
|
|
1000
|
+
['user.created'] as any,
|
|
1001
|
+
undefined,
|
|
1002
|
+
[],
|
|
1003
|
+
logger,
|
|
1004
|
+
);
|
|
1005
|
+
|
|
1006
|
+
const adaptor = new AWSLambdaSubscriber(envParser, subscriber);
|
|
1007
|
+
const lambdaHandler = adaptor.handler;
|
|
1008
|
+
|
|
1009
|
+
const sqsEvent = createSQSEvent([
|
|
1010
|
+
{ type: 'user.created', payload: { userId: '1' } },
|
|
1011
|
+
]);
|
|
1012
|
+
|
|
1013
|
+
await lambdaHandler(sqsEvent, createMockContext(), vi.fn());
|
|
1014
|
+
|
|
1015
|
+
expect(hasContext).toBe(true);
|
|
1016
|
+
});
|
|
1017
|
+
});
|
|
954
1018
|
});
|