Skip to content

Commit

Permalink
General tidyup, enable SWC (#1303)
Browse files Browse the repository at this point in the history
* fix: types on accessibilityintersection

* fix: more typing improvements

* feat: switch to modules, run through let to const

* refactor: tidy schema.ts

* feat: turn on swc to replace our emotion babel transform hooray
  • Loading branch information
ob6160 committed Mar 27, 2022
1 parent cb8e7be commit 5e06c3b
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 141 deletions.
14 changes: 0 additions & 14 deletions .babelrc

This file was deleted.

6 changes: 0 additions & 6 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ module.exports = {
// !! WARN !!
ignoreBuildErrors: true,
},
// images: {
// loader: 'imgix',
// path: 'https://noop/',
// },
experimental: {
nftTracing: true,
},

// swcMinify: true, .. emotion transform is not working with swc yet
async rewrites() {
return [
// Map lng-lat routes to a single page
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"devDependencies": {
"@commitlint/cli": "16.2.3",
"@commitlint/config-conventional": "16.2.1",
"@emotion/babel-plugin": "11.7.2",
"@graphql-codegen/cli": "2.6.2",
"@graphql-codegen/import-types-preset": "2.1.15",
"@graphql-codegen/typescript": "2.4.8",
Expand Down
16 changes: 6 additions & 10 deletions src/api-client/schema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const schema = (authDirective, redactedDirective) => {
const typeDefs = require('../api/typeDefs');
const resolvers = require('../api/resolvers');

const { makeExecutableSchema } = require('@graphql-tools/schema');
/* eslint-disable @typescript-eslint/no-var-requires */
const { makeExecutableSchema } = require('@graphql-tools/schema');
const typeDefs = require('../api/typeDefs').default;
const resolvers = require('../api/resolvers');

const schema = (authDirective, redactedDirective) => {
// Build our executable schema and apply our custom directives
const { redactedDirectiveTypeDefs, redactedDirectiveTransformer } =
redactedDirective('redact');
Expand All @@ -13,11 +13,7 @@ const schema = (authDirective, redactedDirective) => {
return authDirectiveTransformer(
redactedDirectiveTransformer(
makeExecutableSchema({
typeDefs: [
redactedDirectiveTypeDefs,
authDirectiveTypeDefs,
typeDefs.default,
],
typeDefs: [redactedDirectiveTypeDefs, authDirectiveTypeDefs, typeDefs],
resolvers,
})
)
Expand Down
8 changes: 4 additions & 4 deletions src/api/OpeningTimesScalar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { GraphQLScalarType } = require('graphql');
const { Kind } = require('graphql/language');
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';

const astToOpeningTimes = (ast) => {
if (ast.kind !== Kind.LIST) {
Expand Down Expand Up @@ -54,11 +54,11 @@ const validateOpeningTimes = (value) => {

const OpeningTimesScalar = new GraphQLScalarType({
name: 'OpeningTimes',
desription:
description:
'An array of 7 elements in which each represent a day\'s opening times. Each element can be either an empty array (closed) or an array of opening and closing times ["09:00", "17:00"]',
serialize: validateOpeningTimes,
parseValue: validateOpeningTimes,
parseLiteral: (ast) => validateOpeningTimes(astToOpeningTimes(ast)),
});

module.exports = OpeningTimesScalar;
export default OpeningTimesScalar;
7 changes: 4 additions & 3 deletions src/api/db/area.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mongoose = require('mongoose');
const { Polygon } = require('./geojson');
import mongoose from 'mongoose';
import { Polygon } from './geojson';

const AreaSchema = new mongoose.Schema(
{
Expand Down Expand Up @@ -37,4 +37,5 @@ AreaSchema.statics.containing = async function (coords) {
return areas.sort((a, b) => b.priority - a.priority);
};

module.exports = mongoose.models.Area || new mongoose.model('Area', AreaSchema);
const Area = mongoose.models.Area || new mongoose.model('Area', AreaSchema);
export default Area;
6 changes: 3 additions & 3 deletions src/api/db/core.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const { Schema } = require('mongoose');
const { Point } = require('./geojson');
import { Schema } from 'mongoose';
import { Point } from './geojson';

/**
* The core schema of a loo and loo report. This is as a function because:
* - It will give a new object instance each time, protecting against mutation
* - There may be subtle differences in validation between reports and loos
* This is volatile work in progress.
*/
module.exports = exports = new Schema({
export default new Schema({
_id: false,
geometry: Point,
name: { type: String, trim: true },
Expand Down
6 changes: 3 additions & 3 deletions src/api/db/geojson.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { Schema } = require('mongoose');
import { Schema } from 'mongoose';

function ofLength(n) {
return [(list) => list.length === n, `{PATH} must be of length ${n}`];
}

exports.Point = new Schema({
export const Point = new Schema({
type: {
type: String,
enum: ['Point'],
Expand All @@ -19,7 +19,7 @@ exports.Point = new Schema({
});

// Not just Polygon, but MultiPolygon too!
exports.Polygon = new Schema({
export const Polygon = new Schema({
type: {
type: String,
enum: ['MultiPolygon', 'Polygon'],
Expand Down
29 changes: 10 additions & 19 deletions src/api/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const mongoose = require('mongoose');
const Loo = require('./loo');
const Report = require('./report');
const Area = require('./area');
const MapGeo = require('./mapgeo');
import mongoose from 'mongoose';

export { default as Loo } from './loo';
export { default as Report } from './report';
export { default as Area } from './area';
export { default as MapGeo } from './mapgeo';

const MONGODB_URI = process.env.MONGODB_URI;

Expand All @@ -15,13 +16,11 @@ if (!MONGODB_URI) {
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
let cached = global.mongoose;

if (!cached) {
cached = global.mongoose = { conn: null, promise: null };
}
export const cached = !!global.mongoose
? global.mongoose
: (global.mongoose = { conn: null, promise: null });

async function dbConnect() {
export async function dbConnect() {
if (cached.conn) {
return cached.conn;
}
Expand All @@ -34,11 +33,3 @@ async function dbConnect() {
cached.conn = await cached.promise;
return cached.conn;
}

module.exports = {
dbConnect,
Loo,
Report,
Area,
MapGeo,
};
14 changes: 8 additions & 6 deletions src/api/db/loo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate');
const CoreSchema = require('./core');
import mongoose from 'mongoose';
import { PipelineStage } from 'mongoose';
import mongoosePaginate from 'mongoose-paginate';
import CoreSchema from './core';

const LooSchema = new mongoose.Schema(
{
Expand Down Expand Up @@ -82,7 +83,7 @@ LooSchema.statics.fromReports = async function (reports, idOverride) {
};

LooSchema.statics.findNear = function (lon, lat, radius) {
let args = [
const args: PipelineStage[] = [
{
$geoNear: {
near: {
Expand Down Expand Up @@ -211,5 +212,6 @@ LooSchema.statics.getAreasCounters = async function () {
return areas;
};

module.exports =
mongoose.models.NewLoo || new mongoose.model('NewLoo', LooSchema);
const Loo = mongoose.models.NewLoo || new mongoose.model('NewLoo', LooSchema);

export default Loo;
5 changes: 3 additions & 2 deletions src/api/db/mapgeo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mongoose = require('mongoose');
import mongoose from 'mongoose';

const TopoGeometrySchema = new mongoose.Schema({
type: {
Expand Down Expand Up @@ -47,5 +47,6 @@ const MapGeoSchema = new mongoose.Schema(

MapGeoSchema.index({ areaType: 'text' });

module.exports =
const MapGeo =
mongoose.models.MapGeo || new mongoose.model('MapGeo', MapGeoSchema);
export default MapGeo;
31 changes: 17 additions & 14 deletions src/api/db/report.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const mongoose = require('mongoose');
const hasha = require('hasha');
const isEqual = require('lodash/isEqual');
import mongoose from 'mongoose';
import hasha from 'hasha';
import isEqual from 'lodash/isEqual';

const CoreSchema = require('./core');
import CoreSchema from './core';

const ReportSchema = new mongoose.Schema(
{
Expand Down Expand Up @@ -90,7 +90,8 @@ const ReportSchema = new mongoose.Schema(
* Produce an array of all the reports in the current roll preceeding and including the current target
*/
ReportSchema.methods.unroll = async function () {
let looroll = [];
const looroll = [];
// eslint-disable-next-line @typescript-eslint/no-this-alias
let report = this;

while (report) {
Expand All @@ -107,8 +108,8 @@ ReportSchema.methods.unroll = async function () {

ReportSchema.methods.generateLoo = async function (idOverride) {
idOverride = idOverride || null;
let looroll = await this.unroll();
let loo = await this.model('NewLoo').fromReports(looroll, idOverride);
const looroll = await this.unroll();
const loo = await this.model('NewLoo').fromReports(looroll, idOverride);
return loo;
};

Expand Down Expand Up @@ -141,12 +142,12 @@ ReportSchema.methods.nameSuccessor = function (next) {
ReportSchema.methods.suggestLooId = function () {
// Using the timestamp, the diff, and the contributor should be sufficiently unique
// whilst also being stable
let input = JSON.stringify({
const input = JSON.stringify({
coords: this.diff.geometry.coordinates,
created: this.createdAt,
by: this.contributor,
});
let hash = hasha(input, { algorithm: 'md5', encoding: 'hex' }).slice(0, 24);
const hash = hasha(input, { algorithm: 'md5', encoding: 'hex' }).slice(0, 24);
return hash;
};

Expand Down Expand Up @@ -179,13 +180,13 @@ ReportSchema.statics.submit = async function (data, user, from) {
}
}

let report = new this(reportData);
const report = new this(reportData);

let looId = null;
if (from) {
let oldloo = await this.model('NewLoo').findById(from);
let lastReportId = oldloo.reports[oldloo.reports.length - 1];
let previous = await this.model('NewReport').findById(lastReportId);
const oldloo = await this.model('NewLoo').findById(from);
const lastReportId = oldloo.reports[oldloo.reports.length - 1];
const previous = await this.model('NewReport').findById(lastReportId);
await report.deriveFrom(previous);
if (oldloo) {
looId = oldloo._id;
Expand Down Expand Up @@ -224,5 +225,7 @@ ReportSchema.statics.getCounters = async function () {
};
};

module.exports =
const Report =
mongoose.models.NewReport || new mongoose.model('NewReport', ReportSchema);

export default Report;

0 comments on commit 5e06c3b

Please sign in to comment.