UNPKG

@dodi-smart/ttlock-graphql-api

Version:
58 lines 3.13 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.schema = exports.createTTLockGraphQLServer = void 0; const schema_1 = require("./schema"); Object.defineProperty(exports, "schema", { enumerable: true, get: function () { return schema_1.schema; } }); const session_1 = require("./session"); const utils_1 = require("./utils"); const graphql_yoga_1 = require("graphql-yoga"); const createTTLockGraphQLServer = (params) => { const { cors, isAllowed, provideAuth, onUpdateSession, graphiql, graphqlEndpoint, healthCheckEndpoint } = params; const context = (context) => __awaiter(void 0, void 0, void 0, function* () { const { request } = context; // user id const userClaims = (0, utils_1.getUserClaims)(request); // check if using correct `x-hasura-admin-secret` header const adminSecretFromHeader = request.headers.get('x-hasura-admin-secret'); const adminSecret = process.env.NHOST_ADMIN_SECRET; // check if the request is from Hasura const nhostWebhookSecretFromHeader = request.headers.get('x-nhost-webhook-secret'); const nhostWebhookSecret = process.env.NHOST_WEBHOOK_SECRET; const role = request.headers.get('x-hasura-role'); // variables const isAdmin = adminSecretFromHeader === adminSecret || (role === 'admin' && nhostWebhookSecretFromHeader === nhostWebhookSecret); // if no isAllowed function is provided, we will allow admin requests const isAllowedFunction = isAllowed || ((context) => { return context.isAdmin; }); const graphqlContext = Object.assign(Object.assign({}, context), { isAllowed: isAllowedFunction, userClaims, isAdmin }); const providedAuth = yield provideAuth(graphqlContext); const newAuthSession = yield (0, session_1.checkAuthSession)(providedAuth); if (newAuthSession) { onUpdateSession(graphqlContext, newAuthSession); } return graphqlContext; }); return (0, graphql_yoga_1.createYoga)({ cors, graphiql: graphiql !== null && graphiql !== void 0 ? graphiql : process.env.NODE_ENV === 'development', context, schema: schema_1.schema, graphqlEndpoint, healthCheckEndpoint }); }; exports.createTTLockGraphQLServer = createTTLockGraphQLServer; //# sourceMappingURL=server.js.map