-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
166 lines (144 loc) · 4.05 KB
/
config.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import * as yaml from 'js-yaml';
import * as fs from 'fs';
import * as E from 'fp-ts/lib/Either';
import * as IOE from 'fp-ts/lib/IOEither';
import * as F from 'fp-ts/lib/function';
import * as T from './types';
import {
validate,
validateString,
validateRequired,
validateFqdn,
} from '@warungpintar/ninshu';
const VERB_LIST = ['get', 'post', 'delete', 'patch', 'put'];
const validateUrl = validate((url: string) => {
try {
// eslint-disable-next-line no-new
new URL(url);
return true;
} catch {
return false;
}
});
const validateEmptyQs = validate((url: URL) => {
return !url.search;
});
type ConfigError = {
problem: string;
reason: string;
};
const readFileSync = (file: string): IOE.IOEither<Error, string> =>
IOE.tryCatch(() => fs.readFileSync(file, 'utf8'), E.toError);
const emptyConfig: T.Config = {
rest: undefined,
graphql: undefined,
};
const returnValidObjectOrEmptyObject = (val: any): (() => T.Config) => () =>
typeof val === 'object' ? val : emptyConfig;
const yamlLoad = (
file: string,
opts?: yaml.LoadOptions,
): IOE.IOEither<ConfigError[], T.Config> =>
IOE.tryCatch(
F.pipe(yaml.load(file, opts), returnValidObjectOrEmptyObject),
(reason) => [
{
problem: file,
reason: String(reason),
},
],
);
const validateConfig = (
config: T.Config,
): E.Either<ConfigError[], T.Config> => {
const { rest } = config;
const sources = rest?.sources ?? [];
const errors = sources.reduce((prev, next) => {
const origin = next.origin ?? '';
const transforms = next.transforms ?? {};
const _origin = origin.replace(/^(http|https):\/\//, '');
const concatPrev = (e: ConfigError) => {
prev = [...prev, e];
};
// validate origin
F.flow(
validateRequired("origin can't be nil"),
E.chain(validateString('origin should be string')),
E.chain(validateFqdn('invalid origin')),
E.mapLeft((e) => {
concatPrev({ problem: origin ?? 'undefined origin', reason: e });
}),
)(_origin);
// validate path
Object.keys(transforms).forEach((tpath) => {
F.flow(
validateRequired("path can't be nil"),
E.chain(validateString('path should be string')),
E.map((val) => origin.concat(val)),
E.chain(validateUrl('broken origin')),
E.map((val) => new URL(val)),
E.chain(validateEmptyQs("path can't contain query string")),
E.mapLeft((e) => {
concatPrev({ problem: tpath, reason: e });
}),
)(tpath);
});
Object.values(transforms).forEach((tval) => {
const verbs = Object.keys(tval ?? {});
const verbsHandler = Object.values(tval ?? {});
// validate verb
verbs.forEach((verb) => {
if (!VERB_LIST.includes(verb.toLocaleLowerCase())) {
concatPrev({
problem: verb,
reason: 'only accept '.concat(VERB_LIST.join(', ')),
});
}
});
// validate handler
verbsHandler.forEach((handlers: T.WarlockConfig.Field[]) => {
handlers.forEach((handler) => {
const parts = handler.field?.split('.') ?? [];
// @TODO: validate resolver
if (parts[0] !== 'root') {
concatPrev({
problem: handler.field ?? 'undefined field',
reason: 'field should always starts with root',
});
}
});
});
});
return prev;
}, [] as ConfigError[]);
if (errors.length > 0) {
return E.left(errors);
}
return E.right(config);
};
/**
* this function return WarlockConfig
*
* @example
* ```ts
* import {getConfig} from '@warungpintar/warlock'
*
* getConfig(filepath) // right when ok left when error
* ```
*/
export const getConfig = (file: string) => {
const p = F.pipe(
file,
readFileSync,
IOE.bimap(() => [{ problem: file, reason: "can't read file" }], yamlLoad),
IOE.flatten,
);
return E.chain(validateConfig)(p());
};
export const getConfigWithDefault = (file: string) => {
return F.pipe(
file,
getConfig,
E.getOrElseW(() => emptyConfig),
);
};