-
Notifications
You must be signed in to change notification settings - Fork 75
/
migrate.js
46 lines (36 loc) · 1.27 KB
/
migrate.js
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
import semVer from 'semver';
const DEFINITION_V1 = 'v1';
const DEFINITION_V2 = 'v2';
const _removeUndefined = x => JSON.parse(JSON.stringify(x));
const _guessDefinitionVersion = (api) => {
if (['hosts', 'uris', 'methods'].filter(x => api.attributes.hasOwnProperty(x)).length > 0) {
return DEFINITION_V2;
}
return DEFINITION_V1;
};
const _migrateV1toV2 = (api) => {
const {
request_host,
request_path,
strip_request_path,
...oldAttributes,
} = api.attributes;
const newAttributes = {
hosts: api.attributes.request_host ? [api.attributes.request_host] : undefined,
uris: api.attributes.request_path ? [api.attributes.request_path] : undefined,
strip_uri: api.attributes.strip_request_path,
};
return _removeUndefined({ ...api, attributes: { ...oldAttributes, ...newAttributes }});
};
const _migrateApiDefinitionToVersion = (api, kongVersion) => {
switch (_guessDefinitionVersion(api)) {
case DEFINITION_V1:
if (semVer.gte(kongVersion, '0.10.0')) {
return _migrateV1toV2(api);
}
return api;
default:
return api;
}
}
export const migrateApiDefinition = (api, fn) => world => fn(_migrateApiDefinitionToVersion(api, world.getVersion()), world);