-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-params-to-global.js
97 lines (84 loc) · 3.11 KB
/
client-params-to-global.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
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
/**
* @copyright Copyright 2020 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/client-params-to-global.js"
*/
// Note: Undocumented function which is part of the public API:
// https://github.com/flitbit/json-ptr/issues/29
import { encodeUriFragmentIdentifier } from 'json-ptr';
import OpenApiTransformerBase from 'openapi-transformer-base';
import MatchingParameterManager from './lib/matching-parameter-manager.js';
const parameterManagerSymbol = Symbol('parameterManager');
const parametersPathSymbol = Symbol('parametersPath');
/**
* Transformer to move parameters with x-ms-parameter-location:client defined on
* Path Item Objects and Operation Objects to the Components or Definitions
* Object (which is required for x-ms-parameter-location to have any effect).
*
* Note: Authors should define such parameters in Components or Definitions.
* This script is mostly a workaround for api-spec-converter, which inlines
* all $ref parameters.
* https://github.com/Azure/autorest/tree/master/docs/extensions#x-ms-parameter-location
*/
export default class ClientParamsToGlobalTransformer
extends OpenApiTransformerBase {
transformParameter(parameter) {
if (!parameter || parameter['x-ms-parameter-location'] !== 'client') {
return parameter;
}
const defName = this[parameterManagerSymbol].add(parameter, parameter.name);
return {
$ref: encodeUriFragmentIdentifier([
...this[parametersPathSymbol],
defName,
]),
};
}
transformOpenApi(openApi) {
if (typeof openApi !== 'object' || openApi === null || !openApi.paths) {
return openApi;
}
const { components, openapi, paths } = openApi;
if (/^3(?:\.|$)/.test(openapi)
|| (typeof components === 'object' && components !== null)) {
const newParameters = { ...components && components.parameters };
this[parameterManagerSymbol] =
new MatchingParameterManager(newParameters);
this[parametersPathSymbol] = ['components', 'parameters'];
return {
...openApi,
components: {
...components,
parameters: newParameters,
},
paths: this.transformPaths(paths),
};
}
const { parameters, swagger } = openApi;
if (/^2(?:\.|$)/.test(swagger)
|| (typeof parameters === 'object' && parameters !== null)) {
const newParameters = { ...parameters };
this[parameterManagerSymbol] =
new MatchingParameterManager(newParameters);
this[parametersPathSymbol] = ['parameters'];
const newOpenApi = {
...openApi,
parameters: newParameters,
paths: this.transformPaths(openApi.paths),
};
const xMsParameterizedHost = openApi['x-ms-parameterized-host'];
if (xMsParameterizedHost
&& Array.isArray(xMsParameterizedHost.parameters)) {
newOpenApi['x-ms-parameterized-host'] = {
...xMsParameterizedHost,
parameters: this.transformArray(
xMsParameterizedHost.parameters,
this.transformParameter,
),
};
}
return newOpenApi;
}
return openApi;
}
}