Skip to content

Commit dcb8fa7

Browse files
authored
feat: middleware logger better logger scopes (#7281)
* fix: moved global logger error to non overwritable part of code * chore: snapshot of done work * refactor: MethodDecoratorManager * refactor: reduced usage of singleton to support parallel calls of fns * fix: infinite loop constraint on types * Revert "fix: infinite loop constraint on types" This reverts commit 01904e3. * chore: bumped ts jest, and jest * fix: test
1 parent 0109f6b commit dcb8fa7

File tree

33 files changed

+1348
-1302
lines changed

33 files changed

+1348
-1302
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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"devDependencies": {
1818
"@babel/core": "^7.10.5",
1919
"@changesets/cli": "^2.26.1",
20-
"@types/jest": "^26.0.24",
20+
"@types/jest": "^29.0.3",
2121
"@types/node": "^16",
2222
"@vue-storefront/api-extractor-config": "*",
2323
"@vue-storefront/eslint-config-integrations": "*",
@@ -26,11 +26,11 @@
2626
"@vue-storefront/rollup-config": "^0.0.6",
2727
"cross-env": "^6.0.3",
2828
"husky": "^8.0.3",
29-
"jest": "^27.0.6",
29+
"jest": "^29.0.2",
3030
"lint-staged": "^13.2.2",
3131
"rimraf": "^5.0.0",
3232
"rollup": "^2.59.0",
33-
"ts-jest": "^27.0.3",
33+
"ts-jest": "^29.0.2",
3434
"ts-node": "^8.4.1",
3535
"tslib": "^2.1.0",
3636
"turbo": "^2.0.10-canary.1",

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/logger/src/interfaces/LoggerInterface.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/**
22
* Metadata is a record with string keys and arbitrary values.
33
*/
4-
export type Metadata = Record<string, unknown>;
4+
export type Metadata = {
5+
[key: string]: any;
6+
troubleshoot?: Record<string, any> | string[];
7+
alokai?: never;
8+
};
59

610
/**
711
* Log data can be a string, an error, or and arbitrary object.

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";

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export const success = () => {
1+
import { getLogger } from "../../../../../src";
2+
3+
export const success = (context) => {
4+
const logger = getLogger(context);
5+
logger.info("success");
26
return Promise.resolve({
37
status: 200,
48
message: "ok",
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/bootstrap/server.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { apiClientFactory } from "../../../src/apiClientFactory";
22
import * as api from "./api";
3+
import { AlokaiContainer, getLogger } from "../../../src";
4+
5+
const onCreate = async (
6+
config: Record<string, unknown> = {},
7+
alokai: AlokaiContainer
8+
) => {
9+
const logger = getLogger(alokai);
10+
logger.info("oncreate");
311

4-
const onCreate = async (config: Record<string, unknown> = {}) => {
512
return {
613
config,
714
client: null,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { apiClientFactory } from "../../../src/apiClientFactory";
2+
import * as api from "./api";
3+
import { AlokaiContainer, getLogger } from "../../../src";
4+
5+
const init = (settings: any, alokai: AlokaiContainer) => {
6+
const logger = getLogger(alokai);
7+
logger.info("Init fn!");
8+
return {
9+
config: settings,
10+
client: null,
11+
};
12+
};
13+
14+
const onCreate = async (
15+
config: Record<string, unknown> = {},
16+
alokai: AlokaiContainer
17+
) => {
18+
const logger = getLogger(alokai);
19+
logger.info("oncreate");
20+
21+
return {
22+
config,
23+
client: null,
24+
};
25+
};
26+
27+
const { createApiClient } = apiClientFactory({
28+
onCreate,
29+
api,
30+
});
31+
32+
export { createApiClient, init };

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import request from "supertest";
22
import { Server } from "http";
33
import { createServer } from "../../src/index";
44
import { success } from "./bootstrap/api";
5+
import { logger } from "../../__mocks__/logger";
56

67
describe("[Integration] Create server", () => {
78
let app: Server;
@@ -109,7 +110,15 @@ describe("[Integration] Create server", () => {
109110
const response = JSON.parse(text);
110111

111112
// This is the result of the original "success" function from the integration
112-
const apiMethodResult = await success();
113+
const apiMethodResult = await success({
114+
res: {
115+
locals: {
116+
alokai: {
117+
logger,
118+
},
119+
},
120+
},
121+
} as any);
113122

114123
expect(status).toEqual(200);
115124
expect(response).toEqual(apiMethodResult);
@@ -161,7 +170,15 @@ describe("[Integration] Create server", () => {
161170

162171
const response = JSON.parse(text);
163172
// This is the result of the original "success" function from the integration
164-
const apiMethodResult = await success();
173+
const apiMethodResult = await success({
174+
res: {
175+
locals: {
176+
alokai: {
177+
logger,
178+
},
179+
},
180+
},
181+
} as any);
165182

166183
expect(status).toEqual(200);
167184
expect(response).toEqual(apiMethodResult);
@@ -202,7 +219,15 @@ describe("[Integration] Create server", () => {
202219

203220
const response = JSON.parse(text);
204221
// This is the result of the original "success" function from the integration
205-
const apiMethodResult = await success();
222+
const apiMethodResult = await success({
223+
res: {
224+
locals: {
225+
alokai: {
226+
logger,
227+
},
228+
},
229+
},
230+
} as any);
206231

207232
// If merged, the response would be { message: "error", error: true, status: 404 }
208233
expect(status).toEqual(200);

0 commit comments

Comments
 (0)