From 19482be2b8359c8bdc2523940041c27ec237acb6 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 2 Feb 2021 22:26:55 -0800 Subject: [PATCH] Use ES module syntax in backend instead of require MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ok cool, so apparently another win we get from using `ts-node` is that I can finally easily use some non-native-Node features like ES module import syntax, for consistency with what I'm doing in the main app source! That was getting on my nerves tbh. Ooh I bet I can finally use `?.` too, I've had to rewrite that a bunch… --- src/server/auth.js | 4 ++-- src/server/db.js | 2 +- src/server/index.js | 15 ++++++--------- src/server/lib/apollo-server-vercel.js | 9 ++++----- src/server/lib/beeline-graphql.js | 2 +- src/server/loaders.js | 4 ++-- src/server/neopets-assets.js | 2 +- src/server/outfit-images.js | 2 +- src/server/outfit-images.test.js | 5 ++--- src/server/query-tests/Color.test.js | 4 ++-- src/server/query-tests/Item.test.js | 6 +++--- src/server/query-tests/ItemSearch.test.js | 4 ++-- src/server/query-tests/Outfit.test.js | 4 ++-- src/server/query-tests/Pet.test.js | 6 +++--- src/server/query-tests/PetAppearance.test.js | 4 ++-- src/server/query-tests/Species.test.js | 4 ++-- src/server/query-tests/User.test.js | 4 ++-- src/server/query-tests/setup.js | 18 ++++++++---------- src/server/types/AppearanceLayer.js | 4 ++-- src/server/types/ClosetList.js | 2 +- src/server/types/Item.js | 4 ++-- src/server/types/MutationsForSupport.js | 8 ++++---- src/server/types/Outfit.js | 2 +- src/server/types/Pet.js | 14 ++++++-------- src/server/types/PetAppearance.js | 7 ++++--- src/server/types/User.js | 2 +- src/server/types/Zone.js | 2 +- src/server/util.js | 4 ++-- src/server/util.test.js | 9 ++++----- 29 files changed, 74 insertions(+), 83 deletions(-) diff --git a/src/server/auth.js b/src/server/auth.js index 90f8fbd..6cc1e15 100644 --- a/src/server/auth.js +++ b/src/server/auth.js @@ -1,7 +1,7 @@ -const util = require("util"); +import util from "util"; const jwtVerify = util.promisify(require("jsonwebtoken").verify); -const jwksClient = require("jwks-rsa"); +import jwksClient from "jwks-rsa"; const jwks = jwksClient({ jwksUri: "https://openneo.us.auth0.com/.well-known/jwks.json", diff --git a/src/server/db.js b/src/server/db.js index 5acdac0..97a5a6d 100644 --- a/src/server/db.js +++ b/src/server/db.js @@ -1,4 +1,4 @@ -const mysql = require("mysql2"); +import mysql from "mysql2"; let globalDbs = new Map(); diff --git a/src/server/index.js b/src/server/index.js index 910864a..e1b6c5f 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -1,12 +1,9 @@ -const { beelinePlugin } = require("./lib/beeline-graphql"); -const { gql, makeExecutableSchema } = require("apollo-server"); - -const { getUserIdFromToken } = require("./auth"); -const connectToDb = require("./db"); -const buildLoaders = require("./loaders"); -const { - plugin: cacheControlPluginFork, -} = require("./lib/apollo-cache-control-fork"); +import { beelinePlugin } from "./lib/beeline-graphql"; +import { gql, makeExecutableSchema } from "apollo-server"; +import { getUserIdFromToken } from "./auth"; +import connectToDb from "./db"; +import buildLoaders from "./loaders"; +import { plugin as cacheControlPluginFork } from "./lib/apollo-cache-control-fork"; const rootTypeDefs = gql` enum CacheScope { diff --git a/src/server/lib/apollo-server-vercel.js b/src/server/lib/apollo-server-vercel.js index 4ecae57..1d5b1ba 100644 --- a/src/server/lib/apollo-server-vercel.js +++ b/src/server/lib/apollo-server-vercel.js @@ -1,10 +1,9 @@ // Adapted from https://github.com/apollographql/apollo-server/blob/201630ad284754248fc9ab6ebedc7506fcc3d951/packages/apollo-server-lambda/src/ApolloServer.ts -const { ApolloServerBase, runHttpQuery } = require("apollo-server-core"); -const { Headers } = require("apollo-server-env"); -const { - renderPlaygroundPage, -} = require("@apollographql/graphql-playground-html"); +import { ApolloServerBase, runHttpQuery } from "apollo-server-core"; + +import { Headers } from "apollo-server-env"; +import { renderPlaygroundPage } from "@apollographql/graphql-playground-html"; function graphqlVercel(options) { if (!options) { diff --git a/src/server/lib/beeline-graphql.js b/src/server/lib/beeline-graphql.js index c5972ab..474669b 100644 --- a/src/server/lib/beeline-graphql.js +++ b/src/server/lib/beeline-graphql.js @@ -1,4 +1,4 @@ -const beeline = require("honeycomb-beeline"); +import beeline from "honeycomb-beeline"; const beelinePlugin = { requestDidStart() { diff --git a/src/server/loaders.js b/src/server/loaders.js index ee6c089..8ef97b2 100644 --- a/src/server/loaders.js +++ b/src/server/loaders.js @@ -1,5 +1,5 @@ -const DataLoader = require("dataloader"); -const { normalizeRow } = require("./util"); +import DataLoader from "dataloader"; +import { normalizeRow } from "./util"; const buildClosetListLoader = (db) => new DataLoader(async (ids) => { diff --git a/src/server/neopets-assets.js b/src/server/neopets-assets.js index 43cd7c9..d34a3e4 100644 --- a/src/server/neopets-assets.js +++ b/src/server/neopets-assets.js @@ -1,4 +1,4 @@ -const fetch = require("node-fetch"); +import fetch from "node-fetch"; async function loadAssetManifest(swfUrl) { const manifestUrl = convertSwfUrlToManifestUrl(swfUrl); diff --git a/src/server/outfit-images.js b/src/server/outfit-images.js index 6e84588..d5efdad 100644 --- a/src/server/outfit-images.js +++ b/src/server/outfit-images.js @@ -1,4 +1,4 @@ -const { createCanvas, loadImage } = require("canvas"); +import { createCanvas, loadImage } from "canvas"; async function renderOutfitImage(layerRefs, size) { const canvas = createCanvas(size, size); diff --git a/src/server/outfit-images.test.js b/src/server/outfit-images.test.js index cb782c2..dbae5d4 100644 --- a/src/server/outfit-images.test.js +++ b/src/server/outfit-images.test.js @@ -1,6 +1,5 @@ -const { renderOutfitImage } = require("./outfit-images"); - -const { toMatchImageSnapshot } = require("jest-image-snapshot"); +import { renderOutfitImage } from "./outfit-images"; +import { toMatchImageSnapshot } from "jest-image-snapshot"; expect.extend({ toMatchImageSnapshot }); const originalConsoleWarn = console.warn; diff --git a/src/server/query-tests/Color.test.js b/src/server/query-tests/Color.test.js index 3f64d19..bfb8079 100644 --- a/src/server/query-tests/Color.test.js +++ b/src/server/query-tests/Color.test.js @@ -1,5 +1,5 @@ -const gql = require("graphql-tag"); -const { query, getDbCalls } = require("./setup.js"); +import gql from "graphql-tag"; +import { query, getDbCalls } from "./setup.js"; describe("Color", () => { it("loads a single color", async () => { diff --git a/src/server/query-tests/Item.test.js b/src/server/query-tests/Item.test.js index 4c9eaae..24bb7ed 100644 --- a/src/server/query-tests/Item.test.js +++ b/src/server/query-tests/Item.test.js @@ -1,12 +1,12 @@ -const gql = require("graphql-tag"); -const { +import gql from "graphql-tag"; +import { query, mutate, getDbCalls, useTestDb, logInAsTestUser, createItem, -} = require("./setup.js"); +} from "./setup.js"; describe("Item", () => { it("loads metadata", async () => { diff --git a/src/server/query-tests/ItemSearch.test.js b/src/server/query-tests/ItemSearch.test.js index e0027ab..63fe171 100644 --- a/src/server/query-tests/ItemSearch.test.js +++ b/src/server/query-tests/ItemSearch.test.js @@ -1,5 +1,5 @@ -const gql = require("graphql-tag"); -const { query, getDbCalls } = require("./setup.js"); +import gql from "graphql-tag"; +import { query, getDbCalls } from "./setup.js"; describe("ItemSearch", () => { it("loads Neopian Times items", async () => { diff --git a/src/server/query-tests/Outfit.test.js b/src/server/query-tests/Outfit.test.js index 8c49ba0..fae9278 100644 --- a/src/server/query-tests/Outfit.test.js +++ b/src/server/query-tests/Outfit.test.js @@ -1,5 +1,5 @@ -const gql = require("graphql-tag"); -const { query, getDbCalls } = require("./setup.js"); +import gql from "graphql-tag"; +import { query, getDbCalls } from "./setup.js"; describe("Outfit", () => { it("loads an outfit by ID", async () => { diff --git a/src/server/query-tests/Pet.test.js b/src/server/query-tests/Pet.test.js index 401e975..df7d71f 100644 --- a/src/server/query-tests/Pet.test.js +++ b/src/server/query-tests/Pet.test.js @@ -1,11 +1,11 @@ -const gql = require("graphql-tag"); -const { +import gql from "graphql-tag"; +import { query, getDbCalls, clearDbCalls, useTestDb, connectToDb, -} = require("./setup.js"); +} from "./setup.js"; describe("Pet", () => { it("looks up a pet", async () => { diff --git a/src/server/query-tests/PetAppearance.test.js b/src/server/query-tests/PetAppearance.test.js index 163e4f7..df4a2be 100644 --- a/src/server/query-tests/PetAppearance.test.js +++ b/src/server/query-tests/PetAppearance.test.js @@ -1,5 +1,5 @@ -const gql = require("graphql-tag"); -const { query, getDbCalls } = require("./setup.js"); +import gql from "graphql-tag"; +import { query, getDbCalls } from "./setup.js"; describe("PetAppearance", () => { it("loads for species and color", async () => { diff --git a/src/server/query-tests/Species.test.js b/src/server/query-tests/Species.test.js index 80083b8..1487e16 100644 --- a/src/server/query-tests/Species.test.js +++ b/src/server/query-tests/Species.test.js @@ -1,5 +1,5 @@ -const gql = require("graphql-tag"); -const { query, getDbCalls } = require("./setup.js"); +import gql from "graphql-tag"; +import { query, getDbCalls } from "./setup.js"; describe("Species", () => { it("loads a single species", async () => { diff --git a/src/server/query-tests/User.test.js b/src/server/query-tests/User.test.js index 28d889a..e8631fc 100644 --- a/src/server/query-tests/User.test.js +++ b/src/server/query-tests/User.test.js @@ -1,5 +1,5 @@ -const gql = require("graphql-tag"); -const { query, getDbCalls, logInAsTestUser } = require("./setup.js"); +import gql from "graphql-tag"; +import { query, getDbCalls, logInAsTestUser } from "./setup.js"; describe("User", () => { it("looks up a user", async () => { diff --git a/src/server/query-tests/setup.js b/src/server/query-tests/setup.js index 2e7383e..7a28ffd 100644 --- a/src/server/query-tests/setup.js +++ b/src/server/query-tests/setup.js @@ -1,15 +1,13 @@ -const fs = require("fs"); -const path = require("path"); - -const { ApolloServer } = require("apollo-server"); -const { createTestClient } = require("apollo-server-testing"); -const { AuthenticationClient } = require("auth0"); - -const auth = require("../auth"); +import fs from "fs"; +import path from "path"; +import { ApolloServer } from "apollo-server"; +import { createTestClient } from "apollo-server-testing"; +import { AuthenticationClient } from "auth0"; +import auth from "../auth"; const actualAuth = jest.requireActual("../auth"); -const connectToDb = require("../db"); +import connectToDb from "../db"; const actualConnectToDb = jest.requireActual("../db"); -const { config } = require("../index"); +import { config } from "../index"; let accessTokenForQueries = null; diff --git a/src/server/types/AppearanceLayer.js b/src/server/types/AppearanceLayer.js index 2045e71..a4f2476 100644 --- a/src/server/types/AppearanceLayer.js +++ b/src/server/types/AppearanceLayer.js @@ -1,5 +1,5 @@ -const { gql } = require("apollo-server"); -const { loadAssetManifest } = require("../neopets-assets"); +import { gql } from "apollo-server"; +import { loadAssetManifest } from "../neopets-assets"; const typeDefs = gql` enum LayerImageSize { diff --git a/src/server/types/ClosetList.js b/src/server/types/ClosetList.js index 6885368..d78c174 100644 --- a/src/server/types/ClosetList.js +++ b/src/server/types/ClosetList.js @@ -1,4 +1,4 @@ -const { gql } = require("apollo-server"); +import { gql } from "apollo-server"; const typeDefs = gql` enum OwnsOrWants { diff --git a/src/server/types/Item.js b/src/server/types/Item.js index 6d0fc48..637f70c 100644 --- a/src/server/types/Item.js +++ b/src/server/types/Item.js @@ -1,5 +1,5 @@ -const { gql } = require("apollo-server"); -const { getRestrictedZoneIds, oneWeek, oneDay, oneHour } = require("../util"); +import { gql } from "apollo-server"; +import { getRestrictedZoneIds, oneWeek, oneDay, oneHour } from "../util"; const typeDefs = gql` type Item @cacheControl(maxAge: ${oneDay}, staleWhileRevalidate: ${oneWeek}) { diff --git a/src/server/types/MutationsForSupport.js b/src/server/types/MutationsForSupport.js index 85b2d2b..6e3cbfe 100644 --- a/src/server/types/MutationsForSupport.js +++ b/src/server/types/MutationsForSupport.js @@ -1,5 +1,5 @@ -const { gql } = require("apollo-server"); -const { ManagementClient } = require("auth0"); +import { gql } from "apollo-server"; +import { ManagementClient } from "auth0"; const auth0 = new ManagementClient({ domain: "openneo.us.auth0.com", @@ -8,7 +8,7 @@ const auth0 = new ManagementClient({ scope: "read:users update:users", }); -const { +import { capitalize, getPoseFromPetState, getPetStateFieldsFromPose, @@ -16,7 +16,7 @@ const { loadBodyName, logToDiscord, normalizeRow, -} = require("../util"); +} from "../util"; const typeDefs = gql` type RemoveLayerFromItemMutationResult { diff --git a/src/server/types/Outfit.js b/src/server/types/Outfit.js index 7a31325..b2350f9 100644 --- a/src/server/types/Outfit.js +++ b/src/server/types/Outfit.js @@ -1,4 +1,4 @@ -const { gql } = require("apollo-server"); +import { gql } from "apollo-server"; const typeDefs = gql` type Outfit { diff --git a/src/server/types/Pet.js b/src/server/types/Pet.js index d57f094..4655e75 100644 --- a/src/server/types/Pet.js +++ b/src/server/types/Pet.js @@ -1,11 +1,9 @@ -const util = require("util"); - -const fetch = require("node-fetch"); -const { gql } = require("apollo-server"); -const xmlrpc = require("xmlrpc"); - -const { getPoseFromPetState } = require("../util"); -const { saveModelingData } = require("../modeling"); +import util from "util"; +import fetch from "node-fetch"; +import { gql } from "apollo-server"; +import xmlrpc from "xmlrpc"; +import { getPoseFromPetState } from "../util"; +import { saveModelingData } from "../modeling"; const typeDefs = gql` type Pet { diff --git a/src/server/types/PetAppearance.js b/src/server/types/PetAppearance.js index 9970390..9ca54de 100644 --- a/src/server/types/PetAppearance.js +++ b/src/server/types/PetAppearance.js @@ -1,12 +1,13 @@ -const { gql } = require("apollo-server"); -const { +import { gql } from "apollo-server"; + +import { capitalize, getPoseFromPetState, getRestrictedZoneIds, oneWeek, oneDay, oneHour, -} = require("../util"); +} from "../util"; const typeDefs = gql` type Color @cacheControl(maxAge: ${oneWeek}) { diff --git a/src/server/types/User.js b/src/server/types/User.js index 88190fb..a928270 100644 --- a/src/server/types/User.js +++ b/src/server/types/User.js @@ -1,4 +1,4 @@ -const { gql } = require("apollo-server"); +import { gql } from "apollo-server"; const typeDefs = gql` type User { diff --git a/src/server/types/Zone.js b/src/server/types/Zone.js index 0b96e24..a3a9a8b 100644 --- a/src/server/types/Zone.js +++ b/src/server/types/Zone.js @@ -1,4 +1,4 @@ -const { gql } = require("apollo-server"); +import { gql } from "apollo-server"; const typeDefs = gql` # Cache for 1 week (unlikely to change) diff --git a/src/server/util.js b/src/server/util.js index 35e9ef9..1fcefa4 100644 --- a/src/server/util.js +++ b/src/server/util.js @@ -1,5 +1,5 @@ -const beeline = require("honeycomb-beeline"); -const fetch = require("node-fetch"); +import beeline from "honeycomb-beeline"; +import fetch from "node-fetch"; function capitalize(str) { return str[0].toUpperCase() + str.slice(1); diff --git a/src/server/util.test.js b/src/server/util.test.js index a1622da..73f4ef2 100644 --- a/src/server/util.test.js +++ b/src/server/util.test.js @@ -1,8 +1,7 @@ -const gql = require("graphql-tag"); -const { getDbCalls } = require("./query-tests/setup.js"); - -const connectToDb = require("./db"); -const { loadBodyName } = require("./util"); +import gql from "graphql-tag"; +import { getDbCalls } from "./query-tests/setup.js"; +import connectToDb from "./db"; +import { loadBodyName } from "./util"; describe("loadBodyName", () => { it("returns placeholder string for 0", async () => {