api 4.5.1 → 4.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/index.js +40 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api",
|
|
3
|
-
"version": "4.5.
|
|
3
|
+
"version": "4.5.2",
|
|
4
4
|
"description": "Generate an SDK from an OpenAPI definition",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"make-dir": "^3.1.0",
|
|
35
35
|
"mimer": "^2.0.2",
|
|
36
36
|
"node-fetch": "^2.6.0",
|
|
37
|
-
"oas": "^18.
|
|
37
|
+
"oas": "^18.3.4"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@readme/eslint-config": "^8.0.2",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
".api-test/"
|
|
53
53
|
]
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "a4206f70f041c16dd748735e7ad2a3f808d3cd67"
|
|
56
56
|
}
|
package/src/index.js
CHANGED
|
@@ -20,16 +20,6 @@ class Sdk {
|
|
|
20
20
|
this.cacheDir = opts.cacheDir ? opts.cacheDir : false;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
static getOperations(spec) {
|
|
24
|
-
return Object.keys(spec.api.paths)
|
|
25
|
-
.map(path => {
|
|
26
|
-
return Object.keys(spec.api.paths[path]).map(method => {
|
|
27
|
-
return spec.operation(path, method);
|
|
28
|
-
});
|
|
29
|
-
})
|
|
30
|
-
.reduce((prev, next) => prev.concat(next), []);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
23
|
load() {
|
|
34
24
|
let authKeys = [];
|
|
35
25
|
const cache = new Cache(this.uri, this.cacheDir);
|
|
@@ -70,30 +60,62 @@ class Sdk {
|
|
|
70
60
|
});
|
|
71
61
|
}
|
|
72
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Create dynamic accessors for every HTTP method that the OpenAPI specification supports.
|
|
65
|
+
*
|
|
66
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#fixed-fields-7}
|
|
67
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#fixed-fields-7}
|
|
68
|
+
*/
|
|
73
69
|
function loadMethods(spec) {
|
|
74
70
|
const supportedVerbs = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
|
|
75
71
|
|
|
76
72
|
return supportedVerbs
|
|
77
|
-
.map(
|
|
73
|
+
.map(httpVerb => {
|
|
78
74
|
return {
|
|
79
|
-
[
|
|
75
|
+
[httpVerb]: ((method, path, ...args) => {
|
|
80
76
|
const operation = spec.operation(path, method);
|
|
81
77
|
return fetchOperation(spec, operation, ...args);
|
|
82
|
-
}).bind(null,
|
|
78
|
+
}).bind(null, httpVerb),
|
|
83
79
|
};
|
|
84
80
|
})
|
|
85
81
|
.reduce((prev, next) => Object.assign(prev, next));
|
|
86
82
|
}
|
|
87
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Create dynamic accessors for every operation with a defined operation ID. If an operation
|
|
86
|
+
* does not have an operation ID it can be accessed by its `.method('/path')` accessor instead.
|
|
87
|
+
*
|
|
88
|
+
* @param spec
|
|
89
|
+
*/
|
|
88
90
|
function loadOperations(spec) {
|
|
89
|
-
return
|
|
90
|
-
.
|
|
91
|
+
return Object.entries(spec.getPaths())
|
|
92
|
+
.map(([, operations]) => Object.values(operations))
|
|
93
|
+
.reduce((prev, next) => prev.concat(next), [])
|
|
94
|
+
.filter(operation => operation.hasOperationId())
|
|
91
95
|
.reduce((prev, next) => {
|
|
92
|
-
|
|
93
|
-
|
|
96
|
+
// `getOperationId()` creates dynamic operation IDs when one isn't available but we need
|
|
97
|
+
// to know here if we actually have one present or not. The `camelCase` option here also
|
|
98
|
+
// cleans up any `operationId` that we might have into something that can be used as a
|
|
99
|
+
// valid JS method.
|
|
100
|
+
const operationId = next.getOperationId({ camelCase: true });
|
|
101
|
+
const originalOperationId = next.getOperationId();
|
|
102
|
+
|
|
103
|
+
const op = {
|
|
104
|
+
[operationId]: ((operation, ...args) => {
|
|
94
105
|
return fetchOperation(spec, operation, ...args);
|
|
95
106
|
}).bind(null, next),
|
|
96
|
-
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
if (operationId !== originalOperationId) {
|
|
110
|
+
// If we cleaned up their operation ID into a friendly method accessor (`findPetById`
|
|
111
|
+
// versus `find pet by id`) we should still let them use the non-friendly version if
|
|
112
|
+
// they want.
|
|
113
|
+
op[originalOperationId] = ((operation, ...args) => {
|
|
114
|
+
return fetchOperation(spec, operation, ...args);
|
|
115
|
+
}).bind(null, next);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return Object.assign(prev, op);
|
|
97
119
|
}, {});
|
|
98
120
|
}
|
|
99
121
|
|