Skip to content

Commit 3e37ba2

Browse files
committed
docs: update docs + examples for extra templates option
1 parent 2d0ca4e commit 3e37ba2

File tree

9 files changed

+139
-3
lines changed

9 files changed

+139
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ onBuildRoutePath: (data: BuildRoutePath) => BuildRoutePath | void;
1010
onInsertPathParam: (paramName: string, index: number, arr: BuildRouteParam[], resultRoute: string) => string | void;
1111
```
1212
feature: ability to modify route path params before insert them into string (request url, #446, with using hook `onInsertPathParam`)
13+
docs: update docs for `extraTemplates` option
1314

1415
# 11.1.3
1516

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ generateApi({
139139
typePrefix: '',
140140
typeSuffix: '',
141141
addReadonly: false,
142+
/** allow to generate extra files based with this extra templates, see more below */
142143
extraTemplates: [],
143144
anotherArrayType: false,
144145
codeGenConstructs: (constructs) => ({
@@ -239,8 +240,13 @@ with `--module-name-index 0` Api class will have one property `api`
239240
When we change it to `--module-name-index 1` then Api class have two properties `fruits` and `vegetables`
240241

241242
### **`--module-name-first-tag`**
242-
This option will group your API operations based on their first tag - mirroring how the Swagger UI groups displayed operations
243+
This option will group your API operations based on their first tag - mirroring how the Swagger UI groups displayed operations
243244

245+
### `extraTemplates` (NodeJS option)
246+
type `(Record<string, any> & { name: string, path: string })[]`
247+
This thing allow you to generate extra ts\js files based on extra templates (one extra template for one ts\js file)
248+
[Example here](https://github.com/acacode/swagger-typescript-api/tree/next/tests/spec/extra-templates)
249+
244250

245251
## `generate-templates` command
246252
This command allows you to generate source templates which using with option `--templates`

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
"test:additionalProperties2.0": "node tests/spec/additional-properties-2.0/test.js",
5959
"test:enums2.0": "node tests/spec/enums-2.0/test.js",
6060
"test:another-query-params": "node tests/spec/another-query-params/test.js",
61-
"test:on-insert-path-param": "node tests/spec/on-insert-path-param/test.js"
61+
"test:on-insert-path-param": "node tests/spec/on-insert-path-param/test.js",
62+
"test:extra-templates": "node tests/spec/extra-templates/test.js"
6263
},
6364
"author": "acacode",
6465
"license": "MIT",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable */
2+
/* tslint:disable */
3+
/*
4+
* ---------------------------------------------------------------
5+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
6+
* ## ##
7+
* ## AUTHOR: acacode ##
8+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
9+
* ---------------------------------------------------------------
10+
*/
11+
12+
/**
13+
Swagger Petstore
14+
1.0.0
15+
A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification
16+
*/
17+
export class Api {
18+
private request(params: Record<string, any>) {
19+
return fetch(params as any);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable */
2+
/* tslint:disable */
3+
/*
4+
* ---------------------------------------------------------------
5+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
6+
* ## ##
7+
* ## AUTHOR: acacode ##
8+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
9+
* ---------------------------------------------------------------
10+
*/
11+
12+
/**
13+
Swagger Petstore
14+
1.0.0
15+
A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification
16+
*/
17+
export class Api {
18+
private request(params: Record<string, any>) {
19+
return fetch(params as any);
20+
}
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Swagger Petstore",
6+
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
7+
"termsOfService": "http://swagger.io/terms/",
8+
"contact": {
9+
"name": "Swagger API Team"
10+
},
11+
"license": {
12+
"name": "MIT"
13+
}
14+
},
15+
"host": "petstore.swagger.io",
16+
"basePath": "/api",
17+
"schemes": ["http"],
18+
"consumes": ["application/json"],
19+
"produces": ["application/json"],
20+
"paths": {
21+
"/wrong-path-params1/{pathParam1}/{path_param2}/{path_param3}/:pathParam4": {
22+
"delete": {
23+
"description": "DDD",
24+
"operationId": "wrong-path-params1",
25+
"parameters": [],
26+
"produces": ["application/json"],
27+
"responses": {
28+
"200": { "description": "Successfully deleted" }
29+
},
30+
"tags": ["key", "delete"]
31+
}
32+
}
33+
},
34+
"definitions": {
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<%
2+
// console.info(it)
3+
%>
4+
5+
/**
6+
<%= it.apiConfig.info.title %>
7+
8+
<%= it.apiConfig.info.version %>
9+
10+
<%= it.apiConfig.info.description %>
11+
12+
*/
13+
export class <%= it.config.apiClassName %> {
14+
private request(params: Record<string, any>) {
15+
return fetch(params as any)
16+
}
17+
}
18+

tests/spec/extra-templates/test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { generateApiForTest } = require("../../helpers/generateApiForTest");
2+
const { resolve } = require("path");
3+
const validateGeneratedModule = require("../../helpers/validateGeneratedModule");
4+
const assertGeneratedModule = require("../../helpers/assertGeneratedModule");
5+
const createSchemaInfos = require("../../helpers/createSchemaInfos");
6+
7+
const schemas = createSchemaInfos({ absolutePathToSchemas: resolve(__dirname, "./") });
8+
9+
schemas.forEach(({ absolutePath, apiFileName }) => {
10+
generateApiForTest({
11+
testName: "extra-templates test",
12+
silent: true,
13+
name: apiFileName,
14+
input: absolutePath,
15+
output: resolve(__dirname, "./"),
16+
generateClient: false,
17+
generateResponses: false,
18+
generateRouteTypes: false,
19+
generateUnionEnums: false,
20+
templateExtensions: [".eta", ".ejs", ".ts"],
21+
extraTemplates: [
22+
{
23+
name: "external-template-name",
24+
path: resolve(__dirname, "./templates/test.ejs"),
25+
metadata: { arg1: 100, arg2: 200 },
26+
},
27+
],
28+
}).then(() => {
29+
validateGeneratedModule(resolve(__dirname, `./external-template-name.ts`));
30+
assertGeneratedModule(resolve(__dirname, `./external-template-name.ts`), resolve(__dirname, `./expected.ts`));
31+
});
32+
});

tests/spec/on-insert-path-param/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const schemas = createSchemaInfos({ absolutePathToSchemas: resolve(__dirname, ".
88

99
schemas.forEach(({ absolutePath, apiFileName }) => {
1010
generateApiForTest({
11-
testName: "object-types test",
11+
testName: "on-insert-path-param test",
1212
silent: true,
1313
name: apiFileName,
1414
input: absolutePath,

0 commit comments

Comments
 (0)