Skip to content

Commit

Permalink
feat: middleware logger better logger scopes (#7281)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Fifciu authored Oct 21, 2024
1 parent 0109f6b commit dcb8fa7
Show file tree
Hide file tree
Showing 33 changed files with 1,348 additions and 1,302 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-boats-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-storefront/middleware": patch
---

Fix scoping
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@babel/core": "^7.10.5",
"@changesets/cli": "^2.26.1",
"@types/jest": "^26.0.24",
"@types/jest": "^29.0.3",
"@types/node": "^16",
"@vue-storefront/api-extractor-config": "*",
"@vue-storefront/eslint-config-integrations": "*",
Expand All @@ -26,11 +26,11 @@
"@vue-storefront/rollup-config": "^0.0.6",
"cross-env": "^6.0.3",
"husky": "^8.0.3",
"jest": "^27.0.6",
"jest": "^29.0.2",
"lint-staged": "^13.2.2",
"rimraf": "^5.0.0",
"rollup": "^2.59.0",
"ts-jest": "^27.0.3",
"ts-jest": "^29.0.2",
"ts-node": "^8.4.1",
"tslib": "^2.1.0",
"turbo": "^2.0.10-canary.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Metadata } from "../../../src/interfaces/LoggerInterface";
import { LogLevel } from "../../../src/interfaces/LogLevel";
import { GCPStructuredLog } from "../../../src/structuredLog/GCPStructuredLog";

Expand Down Expand Up @@ -157,7 +158,7 @@ describe("GCPStructuredLog", () => {
logData,
options,
severity as LogLevel,
metadata
metadata as unknown as Metadata
);
expect(gcpStructuredLog).toEqual(expected);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/logger/src/interfaces/LoggerInterface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/**
* Metadata is a record with string keys and arbitrary values.
*/
export type Metadata = Record<string, unknown>;
export type Metadata = {
[key: string]: any;
troubleshoot?: Record<string, any> | string[];
alokai?: never;
};

/**
* Log data can be a string, an error, or and arbitrary object.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { success } from "./success";
export { successParalell } from "./successParalell";
export { setCookieHeader } from "./setCookieHeader";
export { error } from "./error";
export { throwAxiosError } from "./throwAxiosError";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const success = () => {
import { getLogger } from "../../../../../src";

export const success = (context) => {
const logger = getLogger(context);
logger.info("success");
return Promise.resolve({
status: 200,
message: "ok",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getLogger } from "../../../../../src";

export const successParalell = (context) => {
const logger = getLogger(context);
logger.info("successParalell");
return Promise.resolve({
status: 200,
message: "ok",
error: false,
});
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { apiClientFactory } from "../../../src/apiClientFactory";
import * as api from "./api";
import { AlokaiContainer, getLogger } from "../../../src";

const onCreate = async (
config: Record<string, unknown> = {},
alokai: AlokaiContainer
) => {
const logger = getLogger(alokai);
logger.info("oncreate");

const onCreate = async (config: Record<string, unknown> = {}) => {
return {
config,
client: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { apiClientFactory } from "../../../src/apiClientFactory";
import * as api from "./api";
import { AlokaiContainer, getLogger } from "../../../src";

const init = (settings: any, alokai: AlokaiContainer) => {
const logger = getLogger(alokai);
logger.info("Init fn!");
return {
config: settings,
client: null,
};
};

const onCreate = async (
config: Record<string, unknown> = {},
alokai: AlokaiContainer
) => {
const logger = getLogger(alokai);
logger.info("oncreate");

return {
config,
client: null,
};
};

const { createApiClient } = apiClientFactory({
onCreate,
api,
});

export { createApiClient, init };
31 changes: 28 additions & 3 deletions packages/middleware/__tests__/integration/createServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import request from "supertest";
import { Server } from "http";
import { createServer } from "../../src/index";
import { success } from "./bootstrap/api";
import { logger } from "../../__mocks__/logger";

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

// This is the result of the original "success" function from the integration
const apiMethodResult = await success();
const apiMethodResult = await success({
res: {
locals: {
alokai: {
logger,
},
},
},
} as any);

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

const response = JSON.parse(text);
// This is the result of the original "success" function from the integration
const apiMethodResult = await success();
const apiMethodResult = await success({
res: {
locals: {
alokai: {
logger,
},
},
},
} as any);

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

const response = JSON.parse(text);
// This is the result of the original "success" function from the integration
const apiMethodResult = await success();
const apiMethodResult = await success({
res: {
locals: {
alokai: {
logger,
},
},
},
} as any);

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

0 comments on commit dcb8fa7

Please sign in to comment.