Skip to content

Commit

Permalink
Enable loading of countries in netlify functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
levino committed Jul 27, 2020
1 parent 8f3bb9f commit d075850
Show file tree
Hide file tree
Showing 21 changed files with 40 additions and 233 deletions.
212 changes: 0 additions & 212 deletions frontend/mock-api/autocomplete/countries.json

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/mock-api/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const version = require("../../package.json").version;
const config = require("./config.json");
const config = require("./functions/api/config.json");

config.version = version;

Expand Down
1 change: 1 addition & 0 deletions frontend/mock-api/functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.js
5 changes: 0 additions & 5 deletions frontend/mock-api/functions/api.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions frontend/mock-api/functions/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as serverless from "serverless-http";
import * as createApi from "../../index";
import * as express from "express";
const app = createApi();
const wrapper = express();

module.exports.handler = serverless(
wrapper.use("/.netlify/functions/api", app)
);
File renamed without changes.
File renamed without changes.
31 changes: 21 additions & 10 deletions frontend/mock-api/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const path = require("path");
const version = require("../package.json").version;
const EXPORT_FORM_CONFIG = require("./forms/export-form.json");
const EXPORT_FORM_CONFIG = require("./functions/api/forms/export-form.json");
const mockAuthMiddleware = require("./mockAuthMiddleware");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");

// Taken from:
// http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffleArray(array) {
Expand Down Expand Up @@ -113,7 +112,9 @@ function mountApi(app) {
req,
res
) {
res.sendFile(path.join(__dirname, `./results/${req.params.filename}`));
res.sendFile(
path.join(__dirname, "functions/api", `./results/${req.params.filename}`)
);
});

/*
Expand All @@ -123,14 +124,20 @@ function mountApi(app) {
req,
res
) {
res.sendFile(path.join(__dirname, "./concepts.json"));
res.sendFile(path.join(__dirname, "functions/api", "./concepts.json"));
});

app.get(
"/api/datasets/:datasetId/concepts/:id",
mockAuthMiddleware,
function response(req, res) {
res.sendFile(path.join(__dirname, `./concepts/${req.params.id}.json`));
res.sendFile(
path.join(
__dirname,
"functions/api",
`./concepts/${req.params.id}.json`
)
);
}
);

Expand Down Expand Up @@ -185,7 +192,9 @@ function mountApi(app) {
mockAuthMiddleware,
function response(req, res) {
setTimeout(() => {
res.sendFile(path.join(__dirname, "./stored-queries/25.json"));
res.sendFile(
path.join(__dirname, "functions/api", "./stored-queries/25.json")
);
}, LONG_DELAY);
}
);
Expand Down Expand Up @@ -250,7 +259,7 @@ function mountApi(app) {
const countriesRequested = req.params.filterId === "production_country";

const storedValues = countriesRequested
? require("./autocomplete/countries")
? require("./functions/api/autocomplete/countries")
: [
"1008508208",
"1015841172",
Expand Down Expand Up @@ -320,7 +329,7 @@ function mountApi(app) {

if (req.params.filterId !== "production_country") return null;

const countries = require("./autocomplete/countries");
const countries = require("./functions/api/autocomplete/countries");
const unknownCodes = values.filter(val => !countries.includes(val));
const resolvedValues = values.filter(val => countries.includes(val));

Expand All @@ -339,7 +348,7 @@ function mountApi(app) {
app.get("/api/config/frontend", mockAuthMiddleware, (req, res) => {
res.setHeader("Content-Type", "application/json");

const config = require("./config.json");
const config = require("./functions/api/config.json");

config.version = version;

Expand Down Expand Up @@ -459,7 +468,9 @@ function mountApi(app) {
mockAuthMiddleware,
function response(req, res) {
setTimeout(() => {
res.sendFile(path.join(__dirname, "./form-configs/testconf.json"));
res.sendFile(
path.join(__dirname, "functions/api", "./form-configs/testconf.json")
);
}, LONG_DELAY);
}
);
Expand Down
5 changes: 4 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"start": "react-app-rewired start",
"build-frontend": "react-app-rewired build",
"build": "yarn build-frontend && yarn build-mock-backend",
"build-mock-backend": "tsc mock-api/functions/*",
"build-mock-backend": "tsc mock-api/functions/*/**.ts",
"test": "react-app-rewired test",
"eject": "react-scripts eject",
"heroku-postbuild": "yarn run build",
Expand Down Expand Up @@ -75,6 +75,7 @@
"@storybook/addon-actions": "^5.3.19",
"@storybook/addon-links": "^5.3.19",
"@storybook/addons": "^5.3.19",
"@storybook/node-logger": "^5.3.19",
"@storybook/preset-create-react-app": "^3.1.4",
"@storybook/react": "^5.3.19",
"@types/axios": "^0.14.0",
Expand Down Expand Up @@ -109,6 +110,7 @@
"@types/redux-logger": "^3.0.8",
"@types/redux-mock-store": "^1.0.2",
"@types/redux-thunk": "^2.1.0",
"babel-loader": "^8.1.0",
"body-parser": "^1.18.2",
"cors": "^2.8.5",
"cross-env": "^5.1.3",
Expand All @@ -127,6 +129,7 @@
"react-scripts": "^3.4.1",
"redux-logger": "^3.0.6",
"redux-mock-store": "^1.5.1",
"regenerator-runtime": "^0.13.7",
"timekeeper": "^2.0.0",
"ts-loader": "^6.2.2",
"ts-node": "^8.8.1",
Expand Down
8 changes: 4 additions & 4 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@
dependencies:
lodash "^4.17.15"

"@storybook/[email protected]":
"@storybook/[email protected]", "@storybook/node-logger@^5.3.19":
version "5.3.19"
resolved "https://registry.npmjs.org/@storybook/node-logger/-/node-logger-5.3.19.tgz#c414e4d3781aeb06298715220012f552a36dff29"
integrity sha512-hKshig/u5Nj9fWy0OsyU04yqCxr0A9pydOHIassr4fpLAaePIN2YvqCqE2V+TxQHjZUnowSSIhbXrGt0DI5q2A==
Expand Down Expand Up @@ -4199,9 +4199,9 @@ babel-jest@^24.9.0:
chalk "^2.4.2"
slash "^2.0.0"

[email protected]:
[email protected], babel-loader@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-8.1.0.tgz#c611d5112bd5209abe8b9fa84c3e4da25275f1c3"
resolved "https://registry.npmjs.org/babel-loader/-/babel-loader-8.1.0.tgz#c611d5112bd5209abe8b9fa84c3e4da25275f1c3"
integrity sha512-7q7nC1tYOrqvUrN3LQK4GwSk/TQorZSOlO9C+RZDZpODgyN4ZlCqE5q9cDsyWOliN+aU9B4JX01xK9eJXowJLw==
dependencies:
find-cache-dir "^2.1.0"
Expand Down Expand Up @@ -15272,7 +15272,7 @@ regenerator-runtime@^0.11.0:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==

regenerator-runtime@^0.13.2:
regenerator-runtime@^0.13.2, regenerator-runtime@^0.13.7:
version "0.13.7"
resolved "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz#cac2dacc8a1ea675feaabaeb8ae833898ae46f55"
integrity sha512-a54FxoJDIr27pgf7IgeQGxmqUNYrcV338lf/6gH456HZ/PhX+5BcwHXG9ajESmwe6WRO0tAzRUrRmNONWgkrew==
Expand Down

0 comments on commit d075850

Please sign in to comment.