Skip to content

Commit

Permalink
[api] resolve runtime transpilation issues surfaced by tsx with esm
Browse files Browse the repository at this point in the history
  • Loading branch information
freemvmt committed Aug 1, 2024
1 parent 005a47d commit 3f221a6
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 38 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 { capitalize } from "lodash";
import lodash from "lodash";
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 capitalize(environment);
return lodash.capitalize(environment);
};

export {
Expand Down
6 changes: 3 additions & 3 deletions api.planx.uk/modules/auth/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sign } from "jsonwebtoken";
import jsonwebtoken from "jsonwebtoken";
import { $api } from "../../client/index.js";
import { User, Role } from "@opensystemslab/planx-core/types";

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

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

export const buildJWTForAPIRole = () =>
sign(
jsonwebtoken.sign(
{
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": ["api"],
Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/modules/file/service/uploadFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import S3 from "aws-sdk/clients/s3.js";
import { customAlphabet } from "nanoid";
import { getType } from "mime";
import mime from "mime";
import { s3Factory } from "./utils.js";
import { isLiveEnv } from "../../../helpers.js";
const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz", 8);
Expand Down Expand Up @@ -67,7 +67,7 @@ export function generateFileParams(
fileType: string | null;
key: string;
} {
const fileType = getType(filename);
const fileType = mime.getType(filename);
const key = `${filekey || nanoid()}/${filename}`;

const params = {
Expand Down
42 changes: 25 additions & 17 deletions api.planx.uk/modules/gis/service/digitalLand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,31 @@ export interface LocalAuthorityMetadata {

/** When a team publishes their granular Article 4 data, add them to this list. Key must match team slug */
export const localAuthorityMetadata: Record<string, LocalAuthorityMetadata> = {
"barking-and-dagenham": require("./local_authorities/metadata/barkingAndDagenham"),
barnet: require("./local_authorities/metadata/barnet"),
birmingham: require("./local_authorities/metadata/birmingham"),
buckinghamshire: require("./local_authorities/metadata/buckinghamshire"),
camden: require("./local_authorities/metadata/camden"),
canterbury: require("./local_authorities/metadata/canterbury"),
doncaster: require("./local_authorities/metadata/doncaster"),
"epsom-and-ewell": require("./local_authorities/metadata/epsomAndEwell"),
gateshead: require("./local_authorities/metadata/gateshead"),
gloucester: require("./local_authorities/metadata/gloucester"),
lambeth: require("./local_authorities/metadata/lambeth"),
medway: require("./local_authorities/metadata/medway"),
newcastle: require("./local_authorities/metadata/newcastle"),
southwark: require("./local_authorities/metadata/southwark"),
"st-albans": require("./local_authorities/metadata/stAlbans"),
tewkesbury: require("./local_authorities/metadata/tewkesbury"),
"west-berkshire": require("./local_authorities/metadata/westBerkshire"),
"barking-and-dagenham": await import(
"./local_authorities/metadata/barkingAndDagenham.js"
),
barnet: await import("./local_authorities/metadata/barnet.js"),
birmingham: await import("./local_authorities/metadata/birmingham.js"),
buckinghamshire: await import(
"./local_authorities/metadata/buckinghamshire.js"
),
camden: await import("./local_authorities/metadata/camden.js"),
canterbury: await import("./local_authorities/metadata/canterbury.js"),
doncaster: await import("./local_authorities/metadata/doncaster.js"),
"epsom-and-ewell": await import(
"./local_authorities/metadata/epsomAndEwell.js"
),
gateshead: await import("./local_authorities/metadata/gateshead.js"),
gloucester: await import("./local_authorities/metadata/gloucester.js"),
lambeth: await import("./local_authorities/metadata/lambeth.js"),
medway: await import("./local_authorities/metadata/medway.js"),
newcastle: await import("./local_authorities/metadata/newcastle.js"),
southwark: await import("./local_authorities/metadata/southwark.js"),
"st-albans": await import("./local_authorities/metadata/stAlbans.js"),
tewkesbury: await import("./local_authorities/metadata/tewkesbury.js"),
"west-berkshire": await import(
"./local_authorities/metadata/westBerkshire.js"
),
};

/**
Expand Down
6 changes: 3 additions & 3 deletions api.planx.uk/modules/gis/service/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const localAuthorities = {
braintree: require("./local_authorities/braintree"),
scotland: require("./local_authorities/scotland"),
digitalLand: require("./digitalLand"),
braintree: await import("./local_authorities/braintree.js"),
scotland: await import("./local_authorities/scotland.js"),
digitalLand: await import("./digitalLand.js"),
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import supertest from "supertest";
import { omit } from "lodash";
import lodash from "lodash";
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 = omit(mockLowcalSession.data, "passport");
const reconciledData = lodash.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 { omit } from "lodash";
import lodash from "lodash";
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 = omit(fetchedSession.data!, "passport");
const sessionData = lodash.omit(fetchedSession.data!, "passport");
const sessionUpdatedAt = fetchedSession.updated_at!;
const flowId = fetchedSession.flow_id!;

Expand Down
8 changes: 4 additions & 4 deletions api.planx.uk/modules/webhooks/service/validateInput/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isObject } from "lodash";
import lodash from "lodash";
import { JSDOM } from "jsdom";
import createDOMPurify from "dompurify";
import { decode } from "he";
import he from "he";
import { reportError } from "../../../pay/helpers.js";

// Setup JSDOM and DOMPurify
Expand All @@ -27,7 +27,7 @@ export const isObjectValid = (
return input.every((child) => isObjectValid(child, validator));
}

if (isObject(input)) {
if (lodash.isObject(input)) {
return Object.values(input).every((child) =>
isObjectValid(child, validator),
);
Expand All @@ -45,7 +45,7 @@ export const isCleanHTML = (input: unknown): boolean => {
// DOMPurify has not removed any attributes or values
const isClean =
cleanHTML.length === input.length ||
decode(cleanHTML).length === decode(input).length;
he.decode(cleanHTML).length === he.decode(input).length;

if (!isClean) logUncleanHTMLError(input, cleanHTML);

Expand Down
6 changes: 3 additions & 3 deletions api.planx.uk/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "isomorphic-fetch";
import "express-async-errors";
import { json, urlencoded } from "body-parser";
import bodyParser from "body-parser";
import assert from "assert";
import cookieParser from "cookie-parser";
import cookieSession from "cookie-session";
Expand Down Expand Up @@ -71,7 +71,7 @@ app.use(
}),
);

app.use(json({ limit: "100mb" }));
app.use(bodyParser.json({ limit: "100mb" }));

// Converts req.headers.cookie: string, to req.cookies: Record<string, string>
app.use(cookieParser());
Expand Down Expand Up @@ -127,7 +127,7 @@ passport.deserializeUser(function (obj: Express.User, cb) {
});
app.use(passport.initialize());
app.use(passport.session());
app.use(urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));

// Setup API routes
app.use(adminRoutes);
Expand Down

0 comments on commit 3f221a6

Please sign in to comment.