Skip to content

Commit e495c61

Browse files
committed
feat: first public version
0 parents  commit e495c61

104 files changed

Lines changed: 23967 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/test/pet-store/petstore-api-client
2+
/lib
3+
/docs

.eslintrc.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// eslint-disable-next-line no-undef
2+
module.exports = {
3+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier', 'plugin:prettier/recommended'],
4+
plugins: ['@typescript-eslint', 'prettier', 'import', 'unused-imports'],
5+
env: {
6+
mocha: true
7+
},
8+
rules: {
9+
'@typescript-eslint/explicit-function-return-type': 'off',
10+
'@typescript-eslint/explicit-module-boundary-types': 'off',
11+
'@typescript-eslint/no-use-before-define': [
12+
'error',
13+
{
14+
functions: false,
15+
classes: false
16+
}
17+
],
18+
'@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
19+
'@typescript-eslint/no-unused-vars': [
20+
'error',
21+
{
22+
vars: 'all',
23+
args: 'after-used',
24+
ignoreRestSiblings: true,
25+
argsIgnorePattern: '^_'
26+
}
27+
],
28+
'@typescript-eslint/no-explicit-any': 'off',
29+
'@typescript-eslint/no-empty-interface': 'error',
30+
'@typescript-eslint/no-non-null-assertion': 'off',
31+
'@typescript-eslint/ban-types': 'off',
32+
'@typescript-eslint/no-namespace': 'off',
33+
34+
'prettier/prettier': ['error', {singleQuote: true}],
35+
'no-case-declarations': 'off',
36+
'no-irregular-whitespace': 'off',
37+
'no-control-regex': 'off',
38+
'no-duplicate-imports': ['error', {includeExports: true}],
39+
'arrow-body-style': ['error', 'as-needed'],
40+
'no-restricted-globals': ['error', 'name', 'toString', 'pending'],
41+
'import/order': [
42+
'error',
43+
{
44+
groups: ['builtin', 'external', 'internal', 'sibling'],
45+
pathGroupsExcludedImportTypes: ['parent', 'sibling', 'index'],
46+
alphabetize: {
47+
order: 'asc',
48+
caseInsensitive: true
49+
}
50+
}
51+
],
52+
'no-unused-vars': 'off',
53+
'unused-imports/no-unused-imports': 'error'
54+
},
55+
settings: {
56+
'import/resolver': {
57+
node: {
58+
extensions: ['.ts']
59+
}
60+
}
61+
},
62+
parser: '@typescript-eslint/parser'
63+
};

.github/workflows/build.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: build
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
strategy:
7+
matrix:
8+
node-version: [18.x]
9+
steps:
10+
- uses: actions/checkout@v2
11+
- name: Use Node.js ${{ matrix.node-version }}
12+
uses: actions/setup-node@v1
13+
with:
14+
node-version: ${{ matrix.node-version }}
15+
- run: npm ci --prefer-offline --no-audit --no-update-notifier
16+
- run: npm run lint
17+
- run: npm run test:update
18+
- run: npm run test
19+
- run: npm run build
20+
- run: npm run build:docs

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
/lib
3+
/test/pet-store/petstore-api-client

.prettierrc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"plugins": ["prettier-plugin-jsdoc"],
3+
4+
"arrowParens": "always",
5+
"bracketSpacing": false,
6+
"endOfLine": "lf",
7+
"jsxBracketSameLine": true,
8+
"jsxSingleQuote": true,
9+
"parser": "typescript",
10+
"printWidth": 120,
11+
"proseWrap": "preserve",
12+
"semi": true,
13+
"singleQuote": true,
14+
"tabWidth": 4,
15+
"trailingComma": "none",
16+
17+
"jsdocCommentLineStrategy": "multiline",
18+
19+
"overrides": [
20+
{
21+
"files": ["*.json", ".eslintrc", ".prettierrc"],
22+
"options": {
23+
"parser": "json",
24+
"singleQuote": false,
25+
"trailingComma": "none",
26+
"useTabs": false,
27+
"tabWidth": 2
28+
}
29+
},
30+
{
31+
"files": "**/core/*.ts",
32+
"options": {
33+
"singleQuote": false
34+
}
35+
}
36+
]
37+
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2014 Dulin Marat
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Open API Typescript Client Generator
2+
3+
Generates Open API client for TypeScript. Extremely configurable.
4+
5+
## Features
6+
7+
1. Generates TypeScript models for all the schemas in the Open API document in a form of interfaces and type aliases.
8+
2. Generates TypeScript services for all the operations in the Open API document.
9+
3. Generates a client class that combines all the services.
10+
4. Uses `fetch` API for making HTTP requests by default, but can be configured to use any other HTTP client.
11+
5. May generate validation for the API responses if configured.
12+
13+
## Setup
14+
15+
Install using npm
16+
17+
```shell
18+
npm add --save-dev api-typescript-generator
19+
```
20+
21+
Or using yarn
22+
23+
```shell
24+
yarn add --dev api-typescript-generator
25+
```
26+
27+
## Configuring
28+
29+
Create a `api-typescript-generator.config.ts` file in the root of your project.
30+
31+
```ts
32+
import path from 'path';
33+
import {ApiTypescriptGeneratorConfig} from 'api-typescript-generator';
34+
35+
const configuration: ApiTypescriptGeneratorConfig = {
36+
generates: [
37+
{
38+
type: 'openapiClient',
39+
document: {
40+
// The source of the Open API document.
41+
// Can also be { type: 'file', path: 'path/to/file.yaml' }
42+
// Both YAML and JSON formats are supported.
43+
source: {
44+
type: 'url',
45+
url: 'https://raw.githubusercontent.com/readmeio/oas-examples/main/3.1/yaml/petstore.yaml'
46+
}
47+
},
48+
// The output directory for the generated client.
49+
outputDirPath: path.join(__dirname, 'petstore-api-client'),
50+
client: {
51+
// The name of the generated client class.
52+
name: 'PetStoreApiClient',
53+
// Overrides the default base URL for the API.
54+
baseUrl: 'https://petstore.swagger.io/v2'
55+
}
56+
}
57+
]
58+
};
59+
60+
export default configuration;
61+
```
62+
63+
Add the following script to your `package.json`:
64+
65+
```json
66+
{
67+
// ...
68+
"scripts": {
69+
// ...
70+
"openapi-codegen": "api-typescript-generator generate api-typescript-generator.config.ts"
71+
}
72+
}
73+
```
74+
75+
Run the script:
76+
77+
```shell
78+
npm run openapi-codegen
79+
```
80+
81+
Or using yarn:
82+
83+
```shell
84+
yarn openapi-codegen
85+
```
86+
87+
By default it generates:
88+
89+
1. Models for all the schemas in the Open API document. Stored at `models` directory by default.
90+
2. Services for all the operations in the Open API document. Stored at `services` directory by default.
91+
3. A client class that combines all the services. Stored at the root of the output directory by default.
92+
4. Core classes for handling HTTP requests and responses. Stored at `core` directory by default.
93+
94+
## Usage
95+
96+
The generated client can be used as follows:
97+
98+
```ts
99+
import {PetStoreApiClient} from './petstore-api-client';
100+
101+
async function testApiClient() {
102+
const apiClient = new PetStoreApiClient();
103+
console.log(await client.store.getInventory());
104+
}
105+
106+
testApiClient().catch(console.error);
107+
```
108+
109+
## What is configurable?
110+
111+
1. Validation of the API responses. See [validation](docs/interfaces/openapi_client.OpenApiClientGeneratorConfig.md#validation).
112+
2. Default base URL for the API. See [client.baseUrl](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigClient.md#baseUrl).
113+
3. Leading and trailing comments for the files. See [comments](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigComments.md).
114+
4. File naming conventions. I.e. [models.filenameFormat](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigModels.md#filenameFormat).
115+
5. Output directory structure. I.e. [models.relativeDirPath](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigModels.md#relativeDirPath).
116+
6. JSDoc generation. I.e. [models.generateJsDoc](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigModels.md#generateJsDoc).
117+
7. Many more. See the documentation below.
118+
119+
## Documentation
120+
121+
Types are exported as part of three modules, depending on the area of application:
122+
123+
1. [`api-typescript-generator`](docs/modules/index.md) - The main module that exports the common API Generator Configuration types.
124+
2. [`api-typescript-generator/openapi-client`](docs/modules/openapi_client.md) - The module that exports the Open API Client Generator Configuration types.
125+
3. [`api-typescript-generator/openapi`](docs/modules/openapi.md) - The module that exports the Open API Document types.
126+
127+
At the moment only types are exported to be used with CLI. Callable API is planned for the future.
128+
129+
## References
130+
131+
1. [Open API Specification](https://swagger.io/specification/)
132+
2. [JSON Schema](https://json-schema.org/)
133+
134+

babel.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// eslint-disable-next-line no-undef
2+
module.exports = {
3+
presets: [['@babel/preset-env', {targets: {node: 'current'}}], '@babel/preset-typescript']
4+
};

docs/.nojekyll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.

0 commit comments

Comments
 (0)