Use ES module syntax in backend instead of require

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…
This commit is contained in:
Emi Matchu 2021-02-02 22:26:55 -08:00
parent 1dda0d4edb
commit 19482be2b8
29 changed files with 74 additions and 83 deletions

View file

@ -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",

View file

@ -1,4 +1,4 @@
const mysql = require("mysql2");
import mysql from "mysql2";
let globalDbs = new Map();

View file

@ -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 {

View file

@ -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) {

View file

@ -1,4 +1,4 @@
const beeline = require("honeycomb-beeline");
import beeline from "honeycomb-beeline";
const beelinePlugin = {
requestDidStart() {

View file

@ -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) => {

View file

@ -1,4 +1,4 @@
const fetch = require("node-fetch");
import fetch from "node-fetch";
async function loadAssetManifest(swfUrl) {
const manifestUrl = convertSwfUrlToManifestUrl(swfUrl);

View file

@ -1,4 +1,4 @@
const { createCanvas, loadImage } = require("canvas");
import { createCanvas, loadImage } from "canvas";
async function renderOutfitImage(layerRefs, size) {
const canvas = createCanvas(size, size);

View file

@ -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;

View file

@ -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 () => {

View file

@ -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 () => {

View file

@ -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 () => {

View file

@ -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 () => {

View file

@ -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 () => {

View file

@ -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 () => {

View file

@ -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 () => {

View file

@ -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 () => {

View file

@ -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;

View file

@ -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 {

View file

@ -1,4 +1,4 @@
const { gql } = require("apollo-server");
import { gql } from "apollo-server";
const typeDefs = gql`
enum OwnsOrWants {

View file

@ -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}) {

View file

@ -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 {

View file

@ -1,4 +1,4 @@
const { gql } = require("apollo-server");
import { gql } from "apollo-server";
const typeDefs = gql`
type Outfit {

View file

@ -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 {

View file

@ -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}) {

View file

@ -1,4 +1,4 @@
const { gql } = require("apollo-server");
import { gql } from "apollo-server";
const typeDefs = gql`
type User {

View file

@ -1,4 +1,4 @@
const { gql } = require("apollo-server");
import { gql } from "apollo-server";
const typeDefs = gql`
# Cache for 1 week (unlikely to change)

View file

@ -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);

View file

@ -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 () => {