-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgetDeliveryServiceProperties.ts
57 lines (51 loc) · 2.22 KB
/
getDeliveryServiceProperties.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
import { parse } from 'yaml';
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import { logInfo, validateSchema } from '@dm3-org/dm3-lib-shared';
import { schema, DeliveryServiceProperties } from '@dm3-org/dm3-lib-delivery';
const DEFAULT_CONFIG_FILE_PATH = resolve(__dirname, './../config.yml');
const DEFAULT_DELIVERY_SERVICE_PROPERTIES: DeliveryServiceProperties = {
messageTTL: 0,
//100Kb
sizeLimit: 100000,
notificationChannel: [],
smtpHost: process.env.SMTP_HOST as string,
smtpPort: Number(process.env.SMTP_PORT),
smtpEmail: process.env.SMTP_EMAIL as string,
smtpUsername: process.env.SMTP_USERNAME as string,
smtpPassword: process.env.SMTP_PASSWORD as string,
};
export function getDeliveryServiceProperties(
path: string = DEFAULT_CONFIG_FILE_PATH,
defaultDeliveryServiceProperties: DeliveryServiceProperties = DEFAULT_DELIVERY_SERVICE_PROPERTIES,
): DeliveryServiceProperties {
if (!existsSync(path)) {
logInfo('Config file not found. Default Config is used');
return defaultDeliveryServiceProperties;
}
const yamlString = readFileSync(path, { encoding: 'utf-8' });
const deliveryServiceProfile = parse(yamlString);
const isSchemaValid = validateSchema(
// eslint-disable-next-line max-len
//The interface DeliveryServiceProperties requires all properties to be non-null. But since we are accepting a partially filled config.yml we are overwriting the required fields so basically no property is required at all. This can be done because every missing property is replaced by a default property
{
...schema.DeliveryServiceProperties,
definitions: {
...schema.DeliveryServiceProperties.definitions,
DeliveryServiceProperties: {
...schema.DeliveryServiceProperties.definitions
.DeliveryServiceProperties,
required: [],
},
},
},
deliveryServiceProfile,
);
if (!isSchemaValid) {
throw Error('Invalid config.yml');
}
return {
...defaultDeliveryServiceProperties,
...parse(yamlString),
} as DeliveryServiceProperties;
}