Skip to content

Commit

Permalink
[api] address minor PR comments, bump mime to v4 (requires transforming
Browse files Browse the repository at this point in the history
for tests), improve lodash imports
  • Loading branch information
freemvmt committed Aug 9, 2024
1 parent 562fc56 commit da25ab5
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 34 deletions.
4 changes: 2 additions & 2 deletions api.planx.uk/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { gql } from "graphql-request";
import lodash from "lodash";
import capitalize from "lodash/capitalize.js";
import { Flow, Node } from "./types.js";
import { ComponentType, FlowGraph } from "@opensystemslab/planx-core/types";
import { $public, getClient } from "./client/index.js";
Expand Down Expand Up @@ -347,7 +347,7 @@ const getFormattedEnvironment = (): string => {
if (environment === "development") {
environment = "local";
}
return lodash.capitalize(environment);
return capitalize(environment);
};

export {
Expand Down
2 changes: 2 additions & 0 deletions api.planx.uk/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const config: JestConfigWithTsJest = {
},
],
},
// mime v4 (which moves to pure ESM) may still have commonJS traces, so we transform it
transformIgnorePatterns: ["node_modules\\/.pnpm\\/(?!(mime))"],
testPathIgnorePatterns: ["dist/*"],
setupFilesAfterEnv: ["./jest.setup.js"],
// handle .ts files first, as ESM modules, and remove .js from imports for jest
Expand Down
7 changes: 3 additions & 4 deletions api.planx.uk/modules/auth/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import jsonwebtoken from "jsonwebtoken";
import jwt from "jsonwebtoken";
import { $api } from "../../client/index.js";
import { User, Role } from "@opensystemslab/planx-core/types";

Expand All @@ -13,12 +13,11 @@ export const buildJWT = async (email: string): Promise<string | undefined> => {
"https://hasura.io/jwt/claims": generateHasuraClaimsForUser(user),
};

const jwt = jsonwebtoken.sign(data, process.env.JWT_SECRET!);
return jwt;
return jwt.sign(data, process.env.JWT_SECRET!);
};

export const buildJWTForAPIRole = () =>
jsonwebtoken.sign(
jwt.sign(
{
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": ["api"],
Expand Down
9 changes: 1 addition & 8 deletions api.planx.uk/modules/gis/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import * as digitalLand from "./digitalLand.js";
import * as scotland from "./local_authorities/scotland.js";
import * as braintree from "./local_authorities/braintree.js";

const localAuthorities = {
// braintree: await import("./local_authorities/braintree.js"),
// scotland: await import("./local_authorities/scotland.js"),
// digitalLand: await import("./digitalLand.js"),
digitalLand,
braintree,
scotland,
};
const localAuthorities = { digitalLand, braintree, scotland };

/**
* @swagger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import supertest from "supertest";
import lodash from "lodash";
import omit from "lodash/omit.js";
import app from "../../../server.js";
import { queryMock } from "../../../tests/graphqlQueryMock.js";
import {
Expand All @@ -22,7 +22,7 @@ const validateSessionPath = "/validate-session";
const getStoreMock = jest.spyOn(userContext, "getStore");

describe("Validate Session endpoint", () => {
const reconciledData = lodash.omit(mockLowcalSession.data, "passport");
const reconciledData = omit(mockLowcalSession.data, "passport");

beforeEach(() => {
getStoreMock.mockReturnValue({
Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/modules/saveAndReturn/service/validateSession.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { gql } from "graphql-request";
import lodash from "lodash";
import omit from "lodash/omit.js";
import { getMostRecentPublishedFlow } from "../../../helpers.js";
import { sortBreadcrumbs } from "@opensystemslab/planx-core";
import { ComponentType } from "@opensystemslab/planx-core/types";
Expand Down Expand Up @@ -29,7 +29,7 @@ export async function validateSession(
sessionId: string,
fetchedSession: Partial<LowCalSession>,
) {
const sessionData = lodash.omit(fetchedSession.data!, "passport");
const sessionData = omit(fetchedSession.data!, "passport");
const sessionUpdatedAt = fetchedSession.updated_at!;
const flowId = fetchedSession.flow_id!;

Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/modules/webhooks/service/validateInput/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lodash from "lodash";
import isObject from "lodash/isObject.js";
import { JSDOM } from "jsdom";
import createDOMPurify from "dompurify";
import he from "he";
Expand Down Expand Up @@ -27,7 +27,7 @@ export const isObjectValid = (
return input.every((child) => isObjectValid(child, validator));
}

if (lodash.isObject(input)) {
if (isObject(input)) {
return Object.values(input).every((child) =>
isObjectValid(child, validator),
);
Expand Down
3 changes: 1 addition & 2 deletions api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"jsondiffpatch": "^0.5.0",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"mime": "^3.0.0",
"mime": "^4.0.4",
"multer": "^1.4.5-lts.1",
"nanoid": "^3.3.7",
"notifications-node-client": "^8.2.0",
Expand Down Expand Up @@ -91,7 +91,6 @@
"@types/jsdom": "^21.1.6",
"@types/jsonwebtoken": "^9.0.5",
"@types/lodash": "^4.17.0",
"@types/mime": "^3.0.4",
"@types/multer": "^1.4.11",
"@types/node": "^18.19.13",
"@types/passport": "^1.0.16",
Expand Down
17 changes: 5 additions & 12 deletions api.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da25ab5

Please sign in to comment.