Skip to content

Commit 62c10ed

Browse files
committed
refactor: reduced usage of singleton to support parallel calls of fns
1 parent eb6c17f commit 62c10ed

File tree

22 files changed

+250
-520
lines changed

22 files changed

+250
-520
lines changed

.changeset/forty-boats-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-storefront/middleware": patch
3+
---
4+
5+
Fix scoping

packages/logger/__tests__/unit/structuredLog/gcpStructuredLog.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Metadata } from "../../../src/interfaces/LoggerInterface";
12
import { LogLevel } from "../../../src/interfaces/LogLevel";
23
import { GCPStructuredLog } from "../../../src/structuredLog/GCPStructuredLog";
34

@@ -157,7 +158,7 @@ describe("GCPStructuredLog", () => {
157158
logData,
158159
options,
159160
severity as LogLevel,
160-
metadata
161+
metadata as unknown as Metadata
161162
);
162163
expect(gcpStructuredLog).toEqual(expected);
163164
}

packages/middleware/__tests__/integration/bootstrap/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { success } from "./success";
2+
export { successParalell } from "./successParalell";
23
export { setCookieHeader } from "./setCookieHeader";
34
export { error } from "./error";
45
export { throwAxiosError } from "./throwAxiosError";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getLogger } from "../../../../../src";
2+
3+
export const successParalell = (context) => {
4+
const logger = getLogger(context);
5+
logger.info("successParalell");
6+
return Promise.resolve({
7+
status: 200,
8+
message: "ok",
9+
error: false,
10+
});
11+
};

packages/middleware/__tests__/integration/loggerScopes.spec.ts

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,47 @@ const testingExtension = {
3636
},
3737
async resueOtherIntegrationMethod(context) {
3838
const logger = getLogger(context);
39-
logger.info("some log");
39+
logger.info("resueOtherIntegrationMethod log");
4040
const int = await context.getApiClient("replicated_integration");
4141
return await int.api[
4242
"replicated-integration-extension"
4343
].orchestrationTest();
4444
},
45+
async paralellOrchestrationTest(context) {
46+
const logger = getLogger(context);
47+
logger.info("resueOtherIntegrationMethod log");
48+
// paralell_replicated_integration
49+
const int = await context.getApiClient("replicated_integration");
50+
const int2 = await context.getApiClient(
51+
"paralell_replicated_integration"
52+
);
53+
// await int.api["replicated-integration-extension"].orchestrationTest();
54+
// await int2.api.successParalell();
55+
return await Promise.all([
56+
int.api["replicated-integration-extension"].orchestrationTest(),
57+
int2.api.successParalell(),
58+
]);
59+
},
4560
},
4661
hooks(req, res, alokai) {
4762
const logger = getLogger(alokai);
4863
logger.info("hooks");
4964
return {
50-
beforeCall({ args }) {
65+
beforeCall({ args, logger }) {
5166
logger.info("bc1");
5267
return args;
5368
},
5469
beforeCreate(a) {
70+
const logger = getLogger(a);
5571
logger.info("bc2");
5672
return a;
5773
},
58-
afterCall({ response }) {
74+
afterCall({ response, logger }) {
5975
logger.info("after1");
6076
return response;
6177
},
6278
afterCreate(a) {
79+
const logger = getLogger(a);
6380
logger.info("after2");
6481
return a;
6582
},
@@ -132,6 +149,19 @@ describe("[Integration] Logger scopes", () => {
132149
];
133150
},
134151
},
152+
paralell_replicated_integration: {
153+
configuration: {},
154+
location: "./__tests__/integration/bootstrap/server",
155+
logger: {
156+
handler: Logger,
157+
},
158+
extensions() {
159+
return [
160+
testingExtension as any,
161+
replicatedIntegrationExtension as any,
162+
];
163+
},
164+
},
135165
},
136166
});
137167
});
@@ -339,6 +369,45 @@ describe("[Integration] Logger scopes", () => {
339369
});
340370
});
341371

372+
test("scope of log from: multiple different integrations called from extension", async () => {
373+
await request(app).post("/test_integration/paralellOrchestrationTest");
374+
375+
expect(Logger.info).toBeCalledWith(expect.any(String), {
376+
context: "middleware",
377+
scope: {
378+
extensionName: "testing-extension",
379+
functionName: "paralellOrchestrationTest",
380+
integrationName: "test_integration",
381+
type: "endpoint",
382+
},
383+
});
384+
expect(Logger.info).toBeCalledWith(expect.any(String), {
385+
context: "middleware",
386+
scope: {
387+
extensionName: "replicated-integration-extension",
388+
functionName: "orchestrationTest",
389+
integrationName: "replicated_integration",
390+
type: "endpoint",
391+
},
392+
});
393+
expect(Logger.info).toBeCalledWith(expect.any(String), {
394+
context: "middleware",
395+
scope: {
396+
functionName: "success",
397+
integrationName: "replicated_integration",
398+
type: "endpoint",
399+
},
400+
});
401+
expect(Logger.info).toBeCalledWith(expect.any(String), {
402+
context: "middleware",
403+
scope: {
404+
functionName: "successParalell",
405+
integrationName: "paralell_replicated_integration",
406+
type: "endpoint",
407+
},
408+
});
409+
});
410+
342411
test("scope of log extendApp", async () => {
343412
await createServer({
344413
logger: {

packages/middleware/__tests__/unit/apiClientFactory/apiClientFactory.spec.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,6 @@ describe("apiClientFactory", () => {
7575
expect(params.onCreate).toHaveBeenCalled();
7676
});
7777

78-
it("Should pass logger when calling onCreate", async () => {
79-
const params = {
80-
onCreate: jest.fn((config) => ({ config })),
81-
defaultSettings: {},
82-
};
83-
84-
const { createApiClient } = apiClientFactory<any, any>(params as any);
85-
86-
await createApiClient.call(
87-
{
88-
middleware: { req: null, res: null },
89-
},
90-
{}
91-
);
92-
93-
expect(params.onCreate).toBeCalledWith(expect.anything(), { logger });
94-
});
95-
9678
it("Should run initial config when onCreate is not provided", async () => {
9779
const params = {
9880
defaultSettings: {},
@@ -206,7 +188,8 @@ describe("apiClientFactory", () => {
206188
expect(logger.info).toBeCalledWith("test", {
207189
scope: {
208190
extensionName: "extTest",
209-
extensionNamePointsHookSource: true,
191+
hookName: "hooks",
192+
type: "requestHook",
210193
},
211194
});
212195
});

packages/middleware/__tests__/unit/handlers/callApiFunction.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Request, Response } from "express";
22
import { callApiFunction } from "../../../src/handlers";
3+
import { logger } from "../../../__mocks__/logger";
34

45
describe("[middleware-handlers] callApiFunction", () => {
56
const apiResponse = {};
@@ -11,7 +12,14 @@ describe("[middleware-handlers] callApiFunction", () => {
1112
const req = {} as Request;
1213
const res = {
1314
send: jest.fn(),
14-
locals: { apiFunction, errorHandler, args },
15+
locals: {
16+
apiFunction,
17+
errorHandler,
18+
args,
19+
alokai: {
20+
logger,
21+
},
22+
},
1523
} as unknown as Response;
1624

1725
beforeEach(() => {

packages/middleware/__tests__/unit/handlers/prepareApiFunction.spec.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,6 @@ describe("[middleware-handlers] prepareApiFunction", () => {
4949
expect(res.locals).toEqual(expect.objectContaining({ apiFunction }));
5050
});
5151

52-
it("adds alokai metadata to res.locals", async () => {
53-
const extensionName = "myExt";
54-
const req = {
55-
params: { integrationName, functionName, extensionName },
56-
} as unknown as Request;
57-
const res = {
58-
status: jest.fn(),
59-
send: jest.fn(),
60-
} as unknown as Response;
61-
res.locals = {
62-
alokai: {
63-
metadata: {},
64-
},
65-
};
66-
67-
await prepareApiFunction(integrations)(req, res, next);
68-
69-
expect(res.locals.alokai.metadata.scope).toEqual({
70-
integrationName,
71-
functionName,
72-
extensionName,
73-
});
74-
});
75-
7652
it("calls next middleware", async () => {
7753
await prepareApiFunction(integrations)(req, res, next);
7854

packages/middleware/__tests__/unit/handlers/prepareLogger.spec.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe("[middleware-handlers] prepareLogger", () => {
88
query: {},
99
params: {
1010
integrationName: "sapcc",
11+
functionName: "getProduct",
1112
},
1213
} as unknown as Request;
1314
const res = { locals: {} } as unknown as Response;
@@ -33,13 +34,6 @@ describe("[middleware-handlers] prepareLogger", () => {
3334

3435
it("appends metadata when logging", () => {
3536
prepareLogger(loggerManager)(req, res, next);
36-
res.locals.alokai.metadata = {
37-
context: "middleware",
38-
scope: {
39-
integrationName: "sapcc",
40-
functionName: "getProduct",
41-
},
42-
};
4337
res.locals.alokai.logger.info("test", { d: 15 });
4438

4539
expect(logger.info).toBeCalledWith("test", {

packages/middleware/__tests__/unit/handlers/prepareMetadataStorage.spec.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)