redux-devtools/packages/redux-devtools-cli/src/routes.ts

143 lines
3.7 KiB
TypeScript
Raw Normal View History

import path from 'path';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import express from 'express';
import type { Router } from 'express';
import morgan from 'morgan';
import * as http from 'http';
import bodyParser from 'body-parser';
import cors from 'cors';
import { AGServer } from 'socketcluster-server';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import type { AddData, ReportBaseFields, Store } from './store.js';
import { resolvers, schema } from './api/schema.js';
2019-01-04 22:44:48 +03:00
const app = express.Router();
2019-01-04 22:44:48 +03:00
const require = createRequire(import.meta.url);
function serveUmdModule(name: string) {
app.use(
express.static(
path.dirname(require.resolve(name + '/package.json')) + '/umd',
),
);
2019-01-04 22:44:48 +03:00
}
interface Context {
store?: Store;
}
function routes(
options: AGServer.AGServerOptions,
store: Store,
scServer: AGServer,
): Router {
const limit = options.maxRequestBody;
const logHTTPRequests = options.logHTTPRequests;
2019-01-10 21:51:14 +03:00
2019-01-04 22:44:48 +03:00
if (logHTTPRequests) {
2019-01-10 21:51:14 +03:00
if (typeof logHTTPRequests === 'object')
app.use(
morgan(
'combined',
logHTTPRequests as morgan.Options<
http.IncomingMessage,
http.ServerResponse
>,
),
);
2019-01-04 22:44:48 +03:00
else app.use(morgan('combined'));
}
const server = new ApolloServer<Context>({
typeDefs: schema,
resolvers,
});
server
.start()
.then(() => {
app.use(
'/graphql',
cors<cors.CorsRequest>(),
bodyParser.json(),
expressMiddleware(server, {
context: () => Promise.resolve({ store }),
}),
);
})
.catch((error) => {
console.error(error); // eslint-disable-line no-console
});
2019-01-04 22:44:48 +03:00
serveUmdModule('react');
serveUmdModule('react-dom');
serveUmdModule('@redux-devtools/app');
2019-01-04 22:44:48 +03:00
app.get('/port.js', function (req, res) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
res.send(`reduxDevToolsPort = ${options.port}`);
2019-01-04 22:44:48 +03:00
});
app.get('*', function (req, res) {
res.sendFile(
path.join(
path.dirname(fileURLToPath(import.meta.url)),
'../app/index.html',
),
);
2019-01-04 22:44:48 +03:00
});
2019-01-10 21:51:14 +03:00
app.use(cors({ methods: 'POST' }));
app.use(bodyParser.json({ limit: limit }));
app.use(bodyParser.urlencoded({ limit: limit, extended: false }));
2019-01-04 22:44:48 +03:00
app.post('/', function (req, res) {
2019-01-04 22:44:48 +03:00
if (!req.body) return res.status(404).end();
switch (req.body.op) {
case 'get':
2019-01-10 21:51:14 +03:00
store
.get(req.body.id as string)
.then(function (r) {
2019-01-10 21:51:14 +03:00
res.send(r || {});
})
.catch(function (error) {
2019-01-10 21:51:14 +03:00
console.error(error); // eslint-disable-line no-console
res.sendStatus(500);
});
2019-01-04 22:44:48 +03:00
break;
case 'list':
2019-01-10 21:51:14 +03:00
store
.list(req.body.query as string, req.body.fields as string[])
.then(function (r) {
2019-01-10 21:51:14 +03:00
res.send(r);
})
.catch(function (error) {
2019-01-10 21:51:14 +03:00
console.error(error); // eslint-disable-line no-console
res.sendStatus(500);
});
2019-01-04 22:44:48 +03:00
break;
default:
2019-01-10 21:51:14 +03:00
store
.add(req.body as AddData)
.then(function (r) {
res.send({
id: (r as ReportBaseFields).id,
error: (r as { error: string }).error,
});
void scServer.exchange.transmitPublish('report', {
2019-01-10 21:51:14 +03:00
type: 'add',
data: r,
2019-01-10 21:51:14 +03:00
});
})
.catch(function (error) {
2019-01-10 21:51:14 +03:00
console.error(error); // eslint-disable-line no-console
res.status(500).send({});
2019-01-04 22:44:48 +03:00
});
}
});
return app;
}
export default routes;