@wepublish/api
Version:
API core for we.publish.
103 lines • 4.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WepublishServer = void 0;
const tslib_1 = require("tslib");
const apollo_server_express_1 = require("apollo-server-express");
const express_1 = tslib_1.__importDefault(require("express"));
const pino_http_1 = tslib_1.__importDefault(require("pino-http"));
const context_1 = require("./context");
const events_1 = require("./events");
const schema_1 = require("./graphql/schema");
const api_1 = require("../../mail-api/src");
const payments_1 = require("./payments");
const utility_1 = require("./utility");
const apollo_server_core_1 = require("apollo-server-core");
const graphql_upload_1 = require("graphql-upload");
const mails_1 = require("./mails");
const api_2 = require("../../utils-api/src");
class WepublishServer {
constructor(opts, app) {
this.opts = opts;
this.app = app;
}
listen(port, hostname) {
var _a, _b;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const app = this.app || (0, express_1.default)();
this.setupPrismaMiddlewares();
(0, api_2.setLogger)(this.opts.logger);
const adminServer = new apollo_server_express_1.ApolloServer({
schema: schema_1.GraphQLWepublishSchema,
plugins: [
this.opts.playground
? (0, apollo_server_core_1.ApolloServerPluginLandingPageGraphQLPlayground)()
: (0, apollo_server_core_1.ApolloServerPluginLandingPageDisabled)()
],
introspection: (_a = this.opts.introspection) !== null && _a !== void 0 ? _a : false,
context: ({ req }) => (0, context_1.contextFromRequest)(req, this.opts)
});
yield adminServer.start();
const publicServer = new apollo_server_express_1.ApolloServer({
schema: schema_1.GraphQLWepublishPublicSchema,
plugins: [
this.opts.playground
? (0, apollo_server_core_1.ApolloServerPluginLandingPageGraphQLPlayground)()
: (0, apollo_server_core_1.ApolloServerPluginLandingPageDisabled)()
],
introspection: (_b = this.opts.introspection) !== null && _b !== void 0 ? _b : false,
context: ({ req }) => (0, context_1.contextFromRequest)(req, this.opts)
});
yield publicServer.start();
const corsOptions = {
origin: true,
credentials: true,
allowedHeaders: [
'authorization',
'content-type',
'content-length',
'accept',
'origin',
'user-agent'
],
methods: ['POST', 'GET', 'OPTIONS']
};
app.use((0, pino_http_1.default)({
logger: api_2.serverLogger.logger,
useLevel: 'debug'
}));
app.use(`/${api_1.MAIL_WEBHOOK_PATH_PREFIX}`, (0, mails_1.setupMailProvider)(this.opts));
app.use(`/${payments_1.PAYMENT_WEBHOOK_PATH_PREFIX}`, (0, payments_1.setupPaymentProvider)(this.opts));
app.use((0, graphql_upload_1.graphqlUploadExpress)());
adminServer.applyMiddleware({
app,
path: '/v1/admin',
cors: corsOptions,
bodyParserConfig: { limit: utility_1.MAX_PAYLOAD_SIZE }
});
publicServer.applyMiddleware({
app,
path: '/v1',
cors: corsOptions
});
app.use((err, req, res, next) => {
(0, api_2.logger)('server').error(err);
if (err.status) {
res.status(err.status);
res.send({ error: err.message });
}
else {
res.status(500).end();
}
});
this.app = app;
});
}
setupPrismaMiddlewares() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this.opts.prisma.$use((0, events_1.onFindArticle)(this.opts.prisma));
this.opts.prisma.$use((0, events_1.onFindPage)(this.opts.prisma));
});
}
}
exports.WepublishServer = WepublishServer;
//# sourceMappingURL=server.js.map