console/packages/web/app/pages/api/lab/[...lab].ts
Laurin Quast 7656b7957d
feat: clean app env (#422)
* feat: document environment variables

* more env documentation

* wip

* add noop for backend env

* typo

* feat: embed environment validation/parsing in built app

* fix the sentry integration 😌

* feat: use env

* feat: decode the usage service environment

* feat: decode the webhooks service environment

* feat: disallow process.env

* feat: decode the tokens service environment

* feat: decode the stripe-billing service environment

* feat: decode server service environment

* feat: decode schema service environment

* feat: decode rate-limit service environment

* feat: decode usage-estimator service environment

* feat: decode emails service environment

* adjust env

* remove commented out code

* adjust env variable name

* use separate env variables

* env fixes

* more environmental stuff :)

* ...

* replace POSTGRES_CONNECTION_STRING with specific environment variables

* handle optional clickhouse (for now :)

* add missing POSTGRES_DB environment variable

* make ENVIRONMENT optional

* the other matters lol

* feat: support delivering mails via SMTP (#412)

* feat: optional CDN (#410)

* feat: optional CDN

* enable CDN in deployment

* enable the CDN in integration tests

* add sendmail provider

* remove unused env variables from the deployment

* only show login alert when legacy auth0 migration is enabled

* feat: make TOKENS_ENDPOINT mandatory and RATE_LIMIT_ENDPOINT optional for usage service

* feat: upgrade supertokens and enable server side email confirmation (#423)

* feat: upgrade supertokens and enable server side email confirmation

* feat: opt into required email verification

* docs: self hosting (#428)

* docs: self-hosting quick start

* Update packages/web/docs/pages/self-hosting/get-started.mdx

Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com>

Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com>

Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com>

* feat: optional GitHub and Slack environment variables (#440)

* feat: optional github integration environment variables

* feat: optional slack integration (#441)

* use latest stable supertokens

Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com>
2022-10-04 14:30:21 +02:00

110 lines
2.6 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from 'next';
import { buildSchema, execute, GraphQLError, parse } from 'graphql';
import { addMocksToSchema } from '@graphql-tools/mock';
import { extractAccessTokenFromRequest } from '@/lib/api/extract-access-token-from-request';
import { env } from '@/env/backend';
async function lab(req: NextApiRequest, res: NextApiResponse) {
const url = env.graphqlEndpoint;
const labParams = req.query.lab || [];
if (labParams.length < 3) {
res.status(400).json({
error: 'Missing Lab Params',
});
return;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [organization, project, target, mock] = labParams as string[];
const headers: Record<string, string> = {};
if (req.headers['x-hive-key']) {
// TODO: change that to Authorization: Bearer
headers['X-API-Token'] = req.headers['x-hive-key'] as string;
} else {
try {
const accessToken = await extractAccessTokenFromRequest(req, res);
if (!accessToken) {
throw 'Invalid Token!';
}
headers['Authorization'] = `Bearer ${accessToken}`;
} catch (error) {
console.warn('Lab auth failed:', error);
res.status(200).send({
errors: [new GraphQLError('Invalid or missing X-Hive-Key authentication')],
});
return;
}
}
const body = {
operationName: 'lab',
query: /* GraphQL */ `
query lab($selector: TargetSelectorInput!) {
lab(selector: $selector) {
schema
mocks
}
}
`,
variables: {
selector: {
organization,
project,
target,
},
},
};
const response = await fetch(url, {
headers: {
'content-type': 'application/json',
...headers,
},
method: 'POST',
body: JSON.stringify(body),
});
const parsedData = await response.json();
if (!parsedData.data?.lab?.schema) {
res.status(200).json({
errors: [new GraphQLError('Please publish your first schema to Hive')],
});
return;
}
if (parsedData.data?.errors?.length > 0) {
res.status(200).json(parsedData.data);
}
try {
const rawSchema = buildSchema(parsedData.data.lab?.schema);
const document = parse(req.body.query);
const mockedSchema = addMocksToSchema({
schema: rawSchema,
preserveResolvers: false,
});
const result = await execute({
schema: mockedSchema,
document,
variableValues: req.body.variables || {},
contextValue: {},
});
res.status(200).json(result);
} catch (e) {
console.log(e);
res.status(200).json({
errors: [e],
});
}
}
export default lab;