-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcheckProfile.ts
43 lines (38 loc) · 1.22 KB
/
checkProfile.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
import { RawAPISchema, SchemaParserMiddleware } from './middleware';
import {
APISchema,
ConfigurationError,
DataSource,
TYPES as CORE_TYPES,
} from '@vulcan-sql/core';
import { inject, interfaces } from 'inversify';
export class CheckProfile extends SchemaParserMiddleware {
private dataSourceFactory: interfaces.SimpleFactory<DataSource>;
constructor(
@inject(CORE_TYPES.Factory_DataSource)
dataSourceFactory: interfaces.SimpleFactory<DataSource>
) {
super();
this.dataSourceFactory = dataSourceFactory;
}
public async handle(schemas: RawAPISchema, next: () => Promise<void>) {
if (!schemas.profiles && schemas.profile) {
schemas.profiles = [schemas.profile];
}
await next();
const transformedSchemas = schemas as APISchema;
if (!transformedSchemas.profiles)
throw new ConfigurationError(
`The profile of schema ${transformedSchemas.urlPath} is not defined`
);
for (const profile of transformedSchemas.profiles) {
try {
this.dataSourceFactory(profile);
} catch (e: any) {
throw new ConfigurationError(
`The profile ${profile} of schema ${transformedSchemas.urlPath} is invalid: ${e?.message}`
);
}
}
}
}