-
Notifications
You must be signed in to change notification settings - Fork 26
/
schema-manager-factory.ts
75 lines (62 loc) · 2.13 KB
/
schema-manager-factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { SchemaManagerHost } from './types';
import { SchemaManager, NoopSchemaManager } from './schema-manager';
import { FileSchemaManagerOptions, FileSchemaManager } from './file-schema-manager';
import { HttpSchemaManager } from './http-schema-manager';
import { ScriptedHttpSchemaManager } from './scripted-http-schema-manager';
import { RequestSetup } from './request-introspection-query';
interface FileSchemaConfigOptions {
file: FileSchemaManagerOptions;
}
interface HttpSchemaConfigOptions {
http: RequestSetup;
}
interface ScriptedHttpSchemaManagerOptions {
http: {
fromScript: string;
};
}
type SchemaConfigOptions = FileSchemaConfigOptions | HttpSchemaConfigOptions | ScriptedHttpSchemaManagerOptions;
function isFileType(conf: SchemaConfigOptions): conf is FileSchemaConfigOptions {
return !!(conf as any).file;
}
function isHttpType(conf: SchemaConfigOptions): conf is HttpSchemaConfigOptions {
return !!(conf as any).http?.url;
}
function isScriptedHttpType(conf: SchemaConfigOptions): conf is ScriptedHttpSchemaManagerOptions {
return !!(conf as any).http?.fromScript;
}
export class SchemaManagerFactory {
constructor(private _host: SchemaManagerHost) {}
create(): SchemaManager {
const schemaConfig = this._host.getConfig().schema;
let options: SchemaConfigOptions;
if (typeof schemaConfig === 'string') {
options = this._convertOptionsFromString(schemaConfig);
} else {
options = schemaConfig as SchemaConfigOptions;
}
if (isFileType(options)) {
return new FileSchemaManager(this._host, options.file);
} else if (isHttpType(options)) {
return new HttpSchemaManager(this._host, options.http);
} else if (isScriptedHttpType(options)) {
return new ScriptedHttpSchemaManager(this._host, options.http);
}
return new NoopSchemaManager(this._host);
}
_convertOptionsFromString(path: string): SchemaConfigOptions {
if (/https?/.test(path)) {
return {
http: {
url: path,
} as RequestSetup,
};
} else {
return {
file: {
path,
} as FileSchemaManagerOptions,
};
}
}
}