Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "node:debug"]
},
{
"name": "Debug partialTemplates test",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "test:partialBaseTemplate"]
},
{
"name": "Debug test:--templates",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "test:--templates"]
}
]
}
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# next release

# 6.0.0

BREAKING_CHANGES:
- Ability to override only one template (issue #166, thanks @Nihisil)
- removed `TPromise` type for `--responses` options (perf. problem, issue #182, thanks @mixalbl4-127)
- breaking changes in `http-client.eta`
- `securityWorker` now can return `Promise<RequestParams | void> | RequestParams | void`

Features:
- template path prefixes `@base`, `@default`, `@modular` (using in Eta templates, `includeFile()`, see README.md)
- `--axios` option for axios http client (issue #142, thanks @msklvsk, @mixalbl4-127 )

# 5.1.7

Fixes:
Expand Down
42 changes: 29 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ Any questions you can ask [**here**](https://github.com/acacode/swagger-typescri

All examples you can find [**here**](https://github.com/acacode/swagger-typescript-api/tree/master/tests)

## 🛑 It is new version with [ETA](https://eta.js.org/docs/syntax) templates
Version with `mustache` templates is `>4.0.0`

## 📄 Usage

```muse
Expand Down Expand Up @@ -58,6 +55,7 @@ Options:
--modular generate separated files for http client, data contracts, and routes (default: false)
--disableStrictSSL disabled strict SSL (default: false)
--clean-output clean output folder before generate api. WARNING: May cause data loss (default: false)
--axios generate axios http client (default: false)
--single-http-client Ability to send HttpClient instance to Api constructor (default: false)
--default-response <type> default type for empty response schema (default: "void")
-h, --help display help for command
Expand Down Expand Up @@ -128,25 +126,43 @@ generateApi({

## 💎 options
### **`--templates`**
This option needed for cases when you don't want to use default `swagger-typescript-api` output structure
This option needed for cases when you don't want to use the default `swagger-typescript-api` output structure

Templates:
- `api.eta` - Api class module
- `data-contracts.eta` - all types (data contracts) from swagger schema
- `http-client.eta` - HttpClient class module
- `procedure-call.eta` - route in Api class
- `route-docs.eta` - documentation for route in Api class
- `route-name.eta` - route name for route in Api class
- `route-type.eta` - *(`--route-types` option)*
- `route-types.eta` - *(`--route-types` option)*
- `api.eta` - Api class module (locations: [/templates/default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/api.eta), [/templates/modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/api.eta))
- `data-contracts.eta` - all types (data contracts) from swagger schema (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contracts.eta))
- `http-client.eta` - HttpClient class module (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-client.eta))
- `procedure-call.eta` - route in Api class (locations: [/templates/default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/procedure-call.eta), [/templates/modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/procedure-call.eta))
- `route-docs.eta` - documentation for route in Api class (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-docs.eta))
- `route-name.eta` - route name for route in Api class (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-name.eta))
- `route-type.eta` - *(`--route-types` option)* (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-type.eta))
- `route-types.eta` - *(`--route-types` option)* (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-types.eta))

How to use it:
1. copy swagger-typescript-api templates into your place in project
1. copy `swagger-typescript-api` templates into your place in project
- from [/templates/default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default) for single api file
- from [/templates/modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular) for multiple api files (with `--modular` option)
- from [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base) for base templates (templates using both in default and modular)
1. add `--templates PATH_TO_YOUR_TEMPLATES` option
2. modify [ETA](https://eta.js.org/docs/syntax) templates as you like

NOTE:
Eta has special directive to render template in your Eta templates - `includeFile(pathToTemplate, payload)`
If you want to use some default templates from this tool you can use path prefixes: `@base`, `@default`, `@modular`.
`@base` - [path to base templates](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base)
`@default` - [path to single api file templates](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default)
`@modular` - [path to multiple api files templates](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular)
Examples:
- `includeFile("@base/data-contracts.eta", configuration)`
- `includeFile("@default/api.eta", configuration)`
- `includeFile("@default/procedure-call.eta", configuration)`
- `includeFile("@modular/api.eta", configuration)`
- `includeFile("@modular/procedure-call.eta", configuration)`
- `includeFile("@base/route-docs.eta", configuration)`
- `includeFile("@base/route-name.eta", configuration)`
- `includeFile("@base/route-type.eta", configuration)`
- `includeFile("@base/route-types.eta", configuration)`

### **`--module-name-index`**
This option should be used in cases when you have api with one global prefix like `/api`
Example:
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { Command } = require("commander");
const { resolve } = require("path");
const { generateApi } = require("./src");
const { version, name: packageName } = require("./package.json");
const { TS_KEYWORDS } = require("./src/constants");
const { TS_KEYWORDS, HTTP_CLIENT } = require("./src/constants");

const program = new Command(packageName);

Expand Down Expand Up @@ -62,6 +62,7 @@ program
0,
)
.option("--disableStrictSSL", "disabled strict SSL", false)
.option("--axios", "generate axios http client", false)
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
.option(
Expand Down Expand Up @@ -91,13 +92,15 @@ const {
cleanOutput,
defaultResponse,
singleHttpClient,
axios,
} = program;

generateApi({
name,
url: path,
generateRouteTypes: routeTypes,
generateClient: client,
generateClient: !!(axios || client),
httpClientType: axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
defaultResponseAsSuccess: defaultAsSuccess,
defaultResponseType: defaultResponse,
generateUnionEnums: unionEnums,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-typescript-api",
"version": "5.1.7",
"version": "6.0.0",
"description": "Create typescript api module from swagger schema",
"scripts": {
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",
Expand Down Expand Up @@ -29,7 +29,10 @@
"test:--extract-request-params": "node tests/spec/extractRequestParams/test.js",
"test:--enum-names-as-values": "node tests/spec/enumNamesAsValues/test.js",
"test:--default-response": "node tests/spec/defaultResponse/test.js",
"test:--js": "node tests/spec/js/test.js"
"test:--js": "node tests/spec/js/test.js",
"test:--axios": "node tests/spec/axios/test.js",
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js"
},
"author": "acacode",
"license": "MIT",
Expand Down
21 changes: 18 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const constants = require("./constants");
const { HTTP_CLIENT, TS_KEYWORDS, PRETTIER_OPTIONS } = require("./constants");

const config = {
/** CLI flag */
Expand Down Expand Up @@ -37,7 +37,7 @@ const config = {
outOfModuleApi: "Common",
},
routeNameDuplicatesMap: new Map(),
prettierOptions: constants.PRETTIER_OPTIONS,
prettierOptions: PRETTIER_OPTIONS,
hooks: {
onCreateComponent: (schema) => schema,
onParseSchema: (originalSchema, parsedSchema) => parsedSchema,
Expand All @@ -47,8 +47,23 @@ const config = {
onCreateRequestParams: (rawType) => {},
onCreateRouteName: () => {},
},
defaultResponseType: constants.TS_KEYWORDS.VOID,
defaultResponseType: TS_KEYWORDS.VOID,
singleHttpClient: false,
httpClientType: HTTP_CLIENT.FETCH,
templatePaths: {
/** `templates/base` */
base: "",
/** `templates/default` */
default: "",
/** `templates/modular` */
modular: "",
/** usage path if `--templates` option is not set */
original: "",
/** custom path to templates (`--templates`) */
custom: "",
},
/** Record<templateName, templateContent> */
templatesToRender: {},
};

/** needs to use data everywhere in project */
Expand Down
6 changes: 6 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ const SCHEMA_TYPES = {
COMPLEX_NOT: "not",
};

const HTTP_CLIENT = {
FETCH: "fetch",
AXIOS: "axios",
};

module.exports = {
DEFAULT_BODY_ARG_NAME: "data",
SUCCESS_RESPONSE_STATUS_RANGE: [200, 300],
JS_PRIMITIVE_TYPES,
JS_EMPTY_TYPES,
TS_KEYWORDS,
SCHEMA_TYPES,
HTTP_CLIENT,
PRETTIER_OPTIONS: {
printWidth: 120,
tabWidth: 2,
Expand Down
4 changes: 3 additions & 1 deletion src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const { resolve } = require("path");
const { filePrefix } = require("./filePrefix");
const makeDir = require("make-dir");

const getFileContent = (path) => fs.readFileSync(path, { encoding: "UTF-8" });
const getFileContent = (path) => {
return fs.readFileSync(path, { encoding: "UTF-8" });
};

const pathIsDir = (path) => {
if (!path) return false;
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { getSwaggerObject, fixSwaggerScheme, convertSwaggerObject } = require("./
const { createComponentsMap, filterComponentsMap } = require("./components");
const { createFile, pathIsExist, pathIsDir, createDir, cleanDir } = require("./files");
const { addToConfig, config } = require("./config");
const { getTemplates } = require("./templates");
const { getTemplates, getTemplatePaths } = require("./templates");
const constants = require("./constants");
const { generateOutputFiles } = require("./output");

Expand All @@ -33,6 +33,7 @@ module.exports = {
defaultResponseAsSuccess = config.defaultResponseAsSuccess,
generateRouteTypes = config.generateRouteTypes,
generateClient = config.generateClient,
httpClientType = config.httpClientType,
generateUnionEnums = config.generateUnionEnums,
moduleNameIndex = config.moduleNameIndex,
extractRequestParams = config.extractRequestParams,
Expand All @@ -50,6 +51,7 @@ module.exports = {
defaultResponseAsSuccess,
generateRouteTypes,
generateClient,
httpClientType,
generateResponses,
templates,
generateUnionEnums,
Expand All @@ -63,9 +65,14 @@ module.exports = {
cleanOutput,
defaultResponseType,
singleHttpClient,
constants,
});
(spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url, disableStrictSSL))
.then(({ usageSchema, originalSchema }) => {
const templatePaths = getTemplatePaths(config);

addToConfig({ templatePaths });

const templatesToRender = getTemplates(config);

console.log("☄️ start generating your typescript api");
Expand Down
Loading