swaggie 0.8.4 → 0.8.5-test
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/README.md +4 -3
- package/dist/types.d.ts +1 -1
- package/package.json +8 -5
- package/templates/xior/barrel.ejs +10 -0
- package/templates/xior/baseClient.ejs +17 -0
- package/templates/xior/client.ejs +7 -0
- package/templates/xior/operation.ejs +59 -0
package/README.md
CHANGED
|
@@ -88,6 +88,7 @@ The following templates are bundled with Swaggie:
|
|
|
88
88
|
|
|
89
89
|
```
|
|
90
90
|
axios Default template. Recommended for React / Vue / similar frameworks. Uses axios
|
|
91
|
+
xior Lightweight and modern alternative to axios. Uses [xior](https://github.com/suhaotian/xior#intro)
|
|
91
92
|
swr-axios Template that embraces SRW for GET requests and as a fallback uses axios.
|
|
92
93
|
fetch Template similar to axios, but with fetch API instead. Recommended for React / Vue / similar frameworks
|
|
93
94
|
ng1 Template for Angular 1 (this is for the old one)
|
|
@@ -126,7 +127,7 @@ Let's assume that you have a [PetStore API](http://petstore.swagger.io/) as your
|
|
|
126
127
|
|
|
127
128
|
Instead of writing any code by hand for fetching particular resources, we will let Swaggie do it for us.
|
|
128
129
|
|
|
129
|
-
> Please note that it's **recommended** to pipe Swaggie command to some prettifier like `prettier` or `dprint` to make the generated code look not only nice, but also persistent.
|
|
130
|
+
> Please note that it's **recommended** to pipe Swaggie command to some prettifier like `prettier`, `biome` or `dprint` to make the generated code look not only nice, but also persistent.
|
|
130
131
|
> Because Swaggie relies on a templating engine, whitespaces are generally a mess, so they may change between versions.
|
|
131
132
|
|
|
132
133
|
### Suggested prettiers
|
|
@@ -137,10 +138,10 @@ Instead of writing any code by hand for fetching particular resources, we will l
|
|
|
137
138
|
prettier ./FILE_PATH.ts --write
|
|
138
139
|
```
|
|
139
140
|
|
|
140
|
-
[
|
|
141
|
+
[biome](https://biomejs.dev) - the super fast one
|
|
141
142
|
|
|
142
143
|
```sh
|
|
143
|
-
|
|
144
|
+
biome check ./FILE_PATH.ts --apply-unsafe
|
|
144
145
|
```
|
|
145
146
|
|
|
146
147
|
You are not limited to any of these, but in our examples we will use Prettier. Please remember that these tools needs to be installed first and they need a config file in your project.
|
package/dist/types.d.ts
CHANGED
|
@@ -44,7 +44,7 @@ export interface ApiSpec {
|
|
|
44
44
|
accepts: string[];
|
|
45
45
|
contentTypes: string[];
|
|
46
46
|
}
|
|
47
|
-
export type Template = 'axios' | 'fetch' | 'ng1' | 'ng2' | 'swr-axios';
|
|
47
|
+
export type Template = 'axios' | 'fetch' | 'ng1' | 'ng2' | 'swr-axios' | 'xior';
|
|
48
48
|
export type HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch';
|
|
49
49
|
export type DateSupport = 'string' | 'Date';
|
|
50
50
|
export interface ApiOperation {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swaggie",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.5-test",
|
|
4
4
|
"description": "Generate ES6 or TypeScript service integration code from an OpenAPI spec",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Piotr Dabrowski",
|
|
@@ -29,7 +29,10 @@
|
|
|
29
29
|
"types": "tsc src/types.ts --outDir dist/ --declaration --emitDeclarationOnly && cp test/index.d.ts ./dist/",
|
|
30
30
|
"test": "mocha"
|
|
31
31
|
},
|
|
32
|
-
"files": [
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"templates"
|
|
35
|
+
],
|
|
33
36
|
"keywords": [
|
|
34
37
|
"swagger",
|
|
35
38
|
"swagger 2.0",
|
|
@@ -49,15 +52,15 @@
|
|
|
49
52
|
"node-fetch": "^2.6.7"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|
|
52
|
-
"@types/chai": "4.3.
|
|
55
|
+
"@types/chai": "4.3.16",
|
|
53
56
|
"@types/js-yaml": "4.0.9",
|
|
54
57
|
"@types/mocha": "10.0.6",
|
|
55
58
|
"@types/node-fetch": "2.6.11",
|
|
56
59
|
"@types/sinon": "17.0.3",
|
|
57
60
|
"chai": "4.4.1",
|
|
58
61
|
"mocha": "10.4.0",
|
|
59
|
-
"sinon": "
|
|
62
|
+
"sinon": "18.0.0",
|
|
60
63
|
"sucrase": "3.35.0",
|
|
61
|
-
"typescript": "5.4.
|
|
64
|
+
"typescript": "5.4.5"
|
|
62
65
|
}
|
|
63
66
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
function serializeQueryParam(obj: any) {
|
|
3
|
+
if (obj === null || obj === undefined) return '';
|
|
4
|
+
if (obj instanceof Date) return obj.toJSON();
|
|
5
|
+
if (typeof obj !== 'object' || Array.isArray(obj)) return obj;
|
|
6
|
+
return Object.keys(obj)
|
|
7
|
+
.reduce((a: any, b) => a.push(b + '=' + obj[b]) && a, [])
|
|
8
|
+
.join('&');
|
|
9
|
+
}
|
|
10
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
//----------------------
|
|
4
|
+
// <auto-generated>
|
|
5
|
+
// Generated using Swaggie (https://github.com/yhnavein/swaggie)
|
|
6
|
+
// Please avoid doing any manual changes in this file
|
|
7
|
+
// </auto-generated>
|
|
8
|
+
//----------------------
|
|
9
|
+
// ReSharper disable InconsistentNaming
|
|
10
|
+
// deno-lint-ignore-file
|
|
11
|
+
|
|
12
|
+
import xior, { type XiorResponse, type XiorRequestConfig } from "xior";
|
|
13
|
+
|
|
14
|
+
export const http = xior.create({
|
|
15
|
+
baseURL: '<%= it.baseUrl || '' %>',
|
|
16
|
+
});
|
|
17
|
+
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
<% it.parameters.forEach((parameter) => { %>
|
|
3
|
+
* @param <%= parameter.name %> <%= parameter.optional ? '(optional)' : '' %> <%= parameter.name !== parameter.originalName ? `(API name: ${parameter.originalName})` : '' %>
|
|
4
|
+
|
|
5
|
+
<% }); -%>
|
|
6
|
+
*/
|
|
7
|
+
<%= it.name %>(<% it.parameters.forEach((parameter) => { %>
|
|
8
|
+
<%= parameter.name %>: <%~ parameter.type %> <%= parameter.optional ? '| null | undefined' : '' %>,
|
|
9
|
+
<% }); %>
|
|
10
|
+
$config?: XiorRequestConfig
|
|
11
|
+
): Promise<XiorResponse<<%~ it.returnType %>>> {
|
|
12
|
+
let url = '<%= it.url %>';
|
|
13
|
+
<% if(it.pathParams && it.pathParams.length > 0) {
|
|
14
|
+
it.pathParams.forEach((parameter) => { %>
|
|
15
|
+
url = url.replace('{<%= parameter.name %>}', encodeURIComponent("" + <%= parameter.name %>));
|
|
16
|
+
<% });
|
|
17
|
+
} %>
|
|
18
|
+
<% if(it.formData && it.formData.length > 0) { %>
|
|
19
|
+
const formDataBody = new FormData();
|
|
20
|
+
<% it.formData.forEach((parameter) => { %>
|
|
21
|
+
if (!!<%= parameter.name %>) {
|
|
22
|
+
<% if(parameter.original && parameter.original.type === 'array') { %>
|
|
23
|
+
<%= parameter.name %>.forEach((f: any) => formDataBody.append("<%= parameter.originalName %>", f));
|
|
24
|
+
<% } else { %>
|
|
25
|
+
formDataBody.append("<%= parameter.originalName %>", <%= parameter.name %><%= parameter.type !== 'string' && parameter.type !== 'File' && parameter.type !== 'Blob' ? '.toString()' : '' %>);
|
|
26
|
+
<% } %>
|
|
27
|
+
}
|
|
28
|
+
<% });
|
|
29
|
+
} %>
|
|
30
|
+
|
|
31
|
+
return http.request<<%~ it.returnType %>>({
|
|
32
|
+
url: url,
|
|
33
|
+
method: '<%= it.method %>',
|
|
34
|
+
<% if(it.formData && it.formData.length > 0) { %>
|
|
35
|
+
data: formDataBody,
|
|
36
|
+
<% } else if(it.body) { %>
|
|
37
|
+
data: <%= it.body.name %>,
|
|
38
|
+
<% } %>
|
|
39
|
+
<% if(it.query && it.query.length > 0) { %>
|
|
40
|
+
params: {
|
|
41
|
+
<% it.query.forEach((parameter) => { %>
|
|
42
|
+
'<%= parameter.originalName %>': serializeQueryParam(<%= parameter.name %>),
|
|
43
|
+
<% }); %>
|
|
44
|
+
},
|
|
45
|
+
<% } %>
|
|
46
|
+
<% if(it.headers && it.headers.length > 0) { %>
|
|
47
|
+
headers: {
|
|
48
|
+
<% it.headers.forEach((parameter) => { %>
|
|
49
|
+
<% if (parameter.value) { %>
|
|
50
|
+
'<%= parameter.originalName %>': '<%= parameter.value %>',
|
|
51
|
+
<% } else { %>
|
|
52
|
+
'<%= parameter.originalName %>': <%= parameter.name %>,
|
|
53
|
+
<% } %>
|
|
54
|
+
<% }); %>
|
|
55
|
+
},
|
|
56
|
+
<% } %>
|
|
57
|
+
...$config,
|
|
58
|
+
});
|
|
59
|
+
},
|