Skip to content

Commit cba4f9a

Browse files
committed
feat: new template for xior
chore: update libraries
1 parent 104da7e commit cba4f9a

File tree

11 files changed

+654
-77
lines changed

11 files changed

+654
-77
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ The following templates are bundled with Swaggie:
8888

8989
```
9090
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)
9192
swr-axios Template that embraces SRW for GET requests and as a fallback uses axios.
9293
fetch Template similar to axios, but with fetch API instead. Recommended for React / Vue / similar frameworks
9394
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
126127

127128
Instead of writing any code by hand for fetching particular resources, we will let Swaggie do it for us.
128129

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.
130131
> Because Swaggie relies on a templating engine, whitespaces are generally a mess, so they may change between versions.
131132
132133
### Suggested prettiers
@@ -137,10 +138,10 @@ Instead of writing any code by hand for fetching particular resources, we will l
137138
prettier ./FILE_PATH.ts --write
138139
```
139140

140-
[dprint](https://dprint.dev/cli/) - the super fast one
141+
[biome](https://biomejs.dev) - the super fast one
141142

142143
```sh
143-
dprint fmt ./FILE_PATH.ts
144+
biome check ./FILE_PATH.ts --apply-unsafe
144145
```
145146

146147
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.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swaggie",
3-
"version": "0.8.4",
3+
"version": "0.8.5",
44
"description": "Generate ES6 or TypeScript service integration code from an OpenAPI spec",
55
"author": {
66
"name": "Piotr Dabrowski",
@@ -29,7 +29,10 @@
2929
"types": "tsc src/types.ts --outDir dist/ --declaration --emitDeclarationOnly && cp test/index.d.ts ./dist/",
3030
"test": "mocha"
3131
},
32-
"files": ["dist", "templates"],
32+
"files": [
33+
"dist",
34+
"templates"
35+
],
3336
"keywords": [
3437
"swagger",
3538
"swagger 2.0",
@@ -49,15 +52,15 @@
4952
"node-fetch": "^2.6.7"
5053
},
5154
"devDependencies": {
52-
"@types/chai": "4.3.14",
55+
"@types/chai": "4.3.16",
5356
"@types/js-yaml": "4.0.9",
5457
"@types/mocha": "10.0.6",
5558
"@types/node-fetch": "2.6.11",
5659
"@types/sinon": "17.0.3",
5760
"chai": "4.4.1",
5861
"mocha": "10.4.0",
59-
"sinon": "17.0.1",
62+
"sinon": "18.0.0",
6063
"sucrase": "3.35.0",
61-
"typescript": "5.4.3"
64+
"typescript": "5.4.5"
6265
}
6366
}

schema.json

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
"template": {
1919
"default": "axios",
2020
"description": "Template that will be used for generating the API client. You can reference bundled templates by name or provide path to the custom template as well",
21-
"examples": ["axios", "swr-axios", "fetch", "ng1", "ng2"],
21+
"examples": [
22+
"axios",
23+
"xior",
24+
"swr-axios",
25+
"fetch",
26+
"ng1",
27+
"ng2"
28+
],
2229
"type": "string"
2330
},
2431
"baseUrl": {
@@ -44,13 +51,23 @@
4451
"dateFormat": {
4552
"default": "Date",
4653
"description": "It determines how Date fields will be handled and what type they will have in models",
47-
"enum": ["Date", "string"],
54+
"enum": [
55+
"Date",
56+
"string"
57+
],
4858
"type": "string"
4959
}
5060
},
51-
"required": ["out", "src"],
61+
"required": [
62+
"out",
63+
"src"
64+
],
5265
"type": "object"
5366
}
5467
},
55-
"allOf": [{ "$ref": "#/definitions/Globals" }]
68+
"allOf": [
69+
{
70+
"$ref": "#/definitions/Globals"
71+
}
72+
]
5673
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface ApiSpec {
4747
contentTypes: string[];
4848
}
4949

50-
export type Template = 'axios' | 'fetch' | 'ng1' | 'ng2' | 'swr-axios';
50+
export type Template = 'axios' | 'fetch' | 'ng1' | 'ng2' | 'swr-axios' | 'xior';
5151
export type HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch';
5252
export type DateSupport = 'string' | 'Date'; // 'luxon', 'momentjs', etc
5353

templates/xior/barrel.ejs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

templates/xior/baseClient.ejs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

templates/xior/client.ejs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const <%= it.camelCaseName %>Client = {
2+
<% it.operations.forEach((operation) => { %>
3+
<%~ include('operation.ejs', operation); %>
4+
5+
<% }); %>
6+
};
7+

templates/xior/operation.ejs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
},

test/snapshots.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'fs';
33
import { runCodeGenerator } from '../src/index';
44
import { FullAppOptions, Template } from '../src/types';
55

6-
const templates: Template[] = ['axios', 'swr-axios', 'fetch', 'ng1', 'ng2'];
6+
const templates: Template[] = ['axios', 'xior', 'swr-axios', 'fetch', 'ng1', 'ng2'];
77

88
describe('petstore snapshots', () => {
99
templates.forEach((template) => {

0 commit comments

Comments
 (0)