Skip to content

Commit

Permalink
No longer try to detect installed packages
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Nov 4, 2024
1 parent fb98cc5 commit d7e8fd5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 78 deletions.
15 changes: 0 additions & 15 deletions src/common/packageVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,3 @@ export function getPackageVersion(name: string): string | null {
}
}
}

export function isPackageInstalled(name: string): boolean {
try {
require.resolve(name);
return true;
} catch (error) {
const _require = createRequire(import.meta.url);
try {
_require.resolve(name);
return true;
} catch (error) {
return false;
}
}
}
113 changes: 59 additions & 54 deletions src/express/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { performance } from "perf_hooks";

import { ApitallyClient } from "../common/client.js";
import { consumerFromStringOrObject } from "../common/consumerRegistry.js";
import {
getPackageVersion,
isPackageInstalled,
} from "../common/packageVersions.js";
import { getPackageVersion } from "../common/packageVersions.js";
import {
ApitallyConfig,
ApitallyConsumer,
Expand Down Expand Up @@ -35,10 +32,6 @@ export const useApitally = (
};

const getMiddleware = (app: Express | Router, client: ApitallyClient) => {
const validatorInstalled = isPackageInstalled("express-validator");
const celebrateInstalled = isPackageInstalled("celebrate");
const nestInstalled = isPackageInstalled("@nestjs/core");
const classValidatorInstalled = isPackageInstalled("class-validator");
let errorHandlerConfigured = false;

return (req: Request, res: Response, next: NextFunction) => {
Expand Down Expand Up @@ -80,17 +73,17 @@ const getMiddleware = (app: Express | Router, client: ApitallyClient) => {
res.locals.body
) {
const validationErrors: ValidationError[] = [];
if (validatorInstalled) {
if (validationErrors.length === 0) {
validationErrors.push(
...extractExpressValidatorErrors(res.locals.body),
);
}
if (celebrateInstalled) {
if (validationErrors.length === 0) {
validationErrors.push(
...extractCelebrateErrors(res.locals.body),
);
}
if (nestInstalled && classValidatorInstalled) {
if (validationErrors.length === 0) {
validationErrors.push(
...extractNestValidationErrors(res.locals.body),
);
Expand Down Expand Up @@ -187,60 +180,72 @@ const getConsumer = (req: Request) => {
};

const extractExpressValidatorErrors = (responseBody: any) => {
const errors: ValidationError[] = [];
if (
responseBody &&
responseBody.errors &&
Array.isArray(responseBody.errors)
) {
responseBody.errors.forEach((error: any) => {
if (error.location && error.path && error.msg && error.type) {
errors.push({
loc: `${error.location}.${error.path}`,
msg: error.msg,
type: error.type,
});
}
});
try {
const errors: ValidationError[] = [];
if (
responseBody &&
responseBody.errors &&
Array.isArray(responseBody.errors)
) {
responseBody.errors.forEach((error: any) => {
if (error.location && error.path && error.msg && error.type) {
errors.push({
loc: `${error.location}.${error.path}`,
msg: error.msg,
type: error.type,
});
}
});
}
return errors;
} catch (error) {
return [];
}
return errors;
};

const extractCelebrateErrors = (responseBody: any) => {
const errors: ValidationError[] = [];
if (responseBody && responseBody.validation) {
Object.values(responseBody.validation).forEach((error: any) => {
if (
error.source &&
error.keys &&
Array.isArray(error.keys) &&
error.message
) {
error.keys.forEach((key: string) => {
errors.push({
loc: `${error.source}.${key}`,
msg: subsetJoiMessage(error.message, key),
type: "",
try {
const errors: ValidationError[] = [];
if (responseBody && responseBody.validation) {
Object.values(responseBody.validation).forEach((error: any) => {
if (
error.source &&
error.keys &&
Array.isArray(error.keys) &&
error.message
) {
error.keys.forEach((key: string) => {
errors.push({
loc: `${error.source}.${key}`,
msg: subsetJoiMessage(error.message, key),
type: "",
});
});
});
}
});
}
});
}
return errors;
} catch (error) {
return [];
}
return errors;
};

const extractNestValidationErrors = (responseBody: any) => {
const errors: ValidationError[] = [];
if (responseBody && Array.isArray(responseBody.message)) {
responseBody.message.forEach((message: any) => {
errors.push({
loc: "",
msg: message,
type: "",
try {
const errors: ValidationError[] = [];
if (responseBody && Array.isArray(responseBody.message)) {
responseBody.message.forEach((message: any) => {
errors.push({
loc: "",
msg: message,
type: "",
});
});
});
}
return errors;
} catch (error) {
return [];
}
return errors;
};

const subsetJoiMessage = (message: string, key: string) => {
Expand Down
10 changes: 1 addition & 9 deletions tests/common/packageVersions.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { describe, expect, it } from "vitest";

import {
getPackageVersion,
isPackageInstalled,
} from "../../src/common/packageVersions.js";
import { getPackageVersion } from "../../src/common/packageVersions.js";

describe("Package versions", () => {
it("Check if package is installed", async () => {
expect(isPackageInstalled("vitest")).toBe(true);
expect(isPackageInstalled("nonexistent")).toBe(false);
});

it("Get package version", () => {
expect(getPackageVersion("vitest")).not.toBeNull();
expect(getPackageVersion("nonexistent")).toBeNull();
Expand Down

0 comments on commit d7e8fd5

Please sign in to comment.