1 | import { GraphQLSchema } from "graphql";
|
2 | import { StrategyCollection } from "./StrategyCollection";
|
3 | import { Strategy } from "./Strategy";
|
4 |
|
5 | export interface Config {
|
6 | readonly realm: string;
|
7 | readonly base: string;
|
8 | readonly privateKey: string;
|
9 | readonly publicKeys: string[];
|
10 | readonly codeValidityDuration: number;
|
11 | readonly jwtValidityDuration: number;
|
12 | readonly strategies: StrategyCollection | Strategy[];
|
13 | readonly pg: {
|
14 | readonly database?: string;
|
15 | readonly host?: string;
|
16 | readonly idleTimeoutMillis?: number;
|
17 | readonly max?: number;
|
18 | readonly password?: string;
|
19 | readonly port?: number;
|
20 | readonly ssl?: boolean;
|
21 | readonly user?: string;
|
22 | };
|
23 | readonly sendMail: (options: {
|
24 | readonly to: string;
|
25 | readonly subject: string;
|
26 | readonly text: string;
|
27 | readonly html: string;
|
28 | readonly from?: string;
|
29 | }) => Promise<any>;
|
30 | readonly processSchema?: (schema: GraphQLSchema) => GraphQLSchema;
|
31 | }
|
32 |
|
33 | export function assertConfig(config: Config): void {
|
34 | if (typeof config.realm !== "string") {
|
35 | throw new Error("The config option `realm` must be a string.");
|
36 | }
|
37 |
|
38 | if (typeof config.base !== "string")
|
39 | throw new Error("The config option `base` must be a string.");
|
40 |
|
41 | if (typeof config.codeValidityDuration !== "number") {
|
42 | throw new Error(
|
43 | "The config option `codeValidityDuration` must be a number."
|
44 | );
|
45 | }
|
46 |
|
47 | if (typeof config.jwtValidityDuration !== "number") {
|
48 | throw new Error(
|
49 | "The config option `jwtValidityDuration` must be a number."
|
50 | );
|
51 | }
|
52 |
|
53 | if (typeof config.privateKey !== "string") {
|
54 | throw new Error("The config option `privateKey` must be a string.");
|
55 | }
|
56 |
|
57 | if (!Array.isArray(config.publicKeys)) {
|
58 | throw new Error("The config option `publicKeys` must be an array.");
|
59 | }
|
60 |
|
61 | if (config.publicKeys.some((key): boolean => typeof key !== "string")) {
|
62 | throw new Error(
|
63 | "The config option `publicKeys` must only include strings."
|
64 | );
|
65 | }
|
66 |
|
67 | if (!config.publicKeys.length) {
|
68 | throw new Error(
|
69 | "The config option `publicKeys` must include at least one key."
|
70 | );
|
71 | }
|
72 |
|
73 | if (typeof config.sendMail !== "function") {
|
74 | throw new Error("The config option `sendMail` must be a function.");
|
75 | }
|
76 |
|
77 | if (
|
78 | !(config.strategies instanceof StrategyCollection) &&
|
79 | !Array.isArray(config.strategies)
|
80 | ) {
|
81 | throw new Error(
|
82 | "The config option `strategies` must either be an instance of `StrategyCollection` or an array of `Strategy` instances."
|
83 | );
|
84 | }
|
85 |
|
86 | if (typeof config.pg !== "object") {
|
87 | throw new Error("The config option `pg` must be an object.");
|
88 | }
|
89 |
|
90 | if (
|
91 | typeof config.processSchema !== "undefined" &&
|
92 | typeof config.processSchema !== "function"
|
93 | ) {
|
94 | throw new Error(
|
95 | "The config option `processSchema` must be a function if defined."
|
96 | );
|
97 | }
|
98 | }
|