apigen-ts 0.0.3 → 0.1.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "apigen-ts",
4
- "version": "0.0.3",
4
+ "version": "0.1.1",
5
5
  "license": "MIT",
6
6
  "author": "Vlad Pronsky <v.pronsky@gmail.com>",
7
7
  "repository": "vladkens/apigen-ts",
@@ -18,12 +18,11 @@
18
18
  "build": "rm -rf dist && pkgroll && cp ./src/_template.ts ./dist && ls -lah dist",
19
19
  "test": "uvu -r tsm test '\\.test\\.ts$'",
20
20
  "test-cov": "c8 --include=src yarn test",
21
- "test-watch": "watchexec -c -e ts 'clear && yarn test'",
22
21
  "format": "prettier --write .",
23
- "ci": "yarn test-cov && yarn build"
22
+ "ci": "tsc --noEmit && yarn test-cov && yarn build"
24
23
  },
25
24
  "dependencies": {
26
- "@redocly/openapi-core": "^1.4.1",
25
+ "@redocly/openapi-core": "^1.6.0",
27
26
  "@types/lodash-es": "^4.17.12",
28
27
  "@types/swagger2openapi": "^7.0.4",
29
28
  "array-utils-ts": "^0.1.2",
@@ -32,8 +31,8 @@
32
31
  "swagger2openapi": "^7.0.8"
33
32
  },
34
33
  "devDependencies": {
35
- "@types/node": "^18.18.0",
36
- "c8": "^8.0.1",
34
+ "@types/node": "^20.10.8",
35
+ "c8": "^9.0.0",
37
36
  "fetch-mock": "^9.11.0",
38
37
  "pkgroll": "^2.0.1",
39
38
  "prettier": "^3.1.0",
@@ -46,10 +45,22 @@
46
45
  "prettier": "^3.0.0",
47
46
  "typescript": "^5.0.0"
48
47
  },
49
- "bin": {
50
- "apigen-ts": "./dist/cli.js"
51
- },
52
48
  "files": [
53
49
  "dist"
54
- ]
50
+ ],
51
+ "main": "./dist/main.js",
52
+ "types": "./dist/main.d.cts",
53
+ "exports": {
54
+ "require": {
55
+ "types": "./dist/main.d.cts",
56
+ "default": "./dist/main.cjs"
57
+ },
58
+ "import": {
59
+ "types": "./dist/main.d.mts",
60
+ "default": "./dist/main.mjs"
61
+ }
62
+ },
63
+ "bin": {
64
+ "apigen-ts": "./dist/cli.js"
65
+ }
55
66
  }
package/readme.md CHANGED
@@ -1,24 +1,30 @@
1
- # OpenAPI TypeScript client generator
1
+ # apigen-ts
2
2
 
3
3
  <div align="center">
4
4
 
5
5
  [<img src="https://badgen.net/npm/v/apigen-ts" alt="version" />](https://npmjs.org/package/apigen-ts)
6
- [<img src="https://github.com/vladkens/apigen-ts/workflows/test/badge.svg" alt="test status" />](https://github.com/vladkens/apigen-ts/actions)
7
6
  [<img src="https://badgen.net/packagephobia/publish/apigen-ts" alt="size" />](https://packagephobia.now.sh/result?p=apigen-ts)
8
7
  [<img src="https://badgen.net/npm/dm/apigen-ts" alt="downloads" />](https://npmjs.org/package/apigen-ts)
9
8
  [<img src="https://badgen.net/github/license/vladkens/apigen-ts" alt="license" />](https://github.com/vladkens/apigen-ts/blob/main/LICENSE)
9
+ [<img src="https://badgen.net/static/-/buy%20me%20a%20coffee/ff813f?icon=buymeacoffee&label" alt="donate" />](https://buymeacoffee.com/vladkens)
10
10
 
11
11
  </div>
12
12
 
13
+ <div align="center">
14
+ <img src="./logo.svg" alt="apigen-ts logo" height="80" />
15
+ <div>TypeScript client generator from OpenAPI schema</div>
16
+ </div>
17
+
13
18
  ## Features
14
19
 
15
- - Generates ready to use ApiClient with types (using `fetch`)
20
+ - Generates ready to use `ApiClient` with types (using `fetch`)
16
21
  - Single output file, minimal third-party code
17
- - Load schema from JSON / YAML, locally and remote
22
+ - Loads schemas from JSON / YAML, locally and remote
18
23
  - Ability to customize `fetch` with your custom function
19
- - Uses `type` instead of `interface`, so no problem with declaration merging
20
24
  - Automatic formating with Prettier
21
- - Parses dates automatically
25
+ - Can parse dates from date-time format (`--parse-dates` flag)
26
+ - Support OpenAPI v2, v3, v3.1
27
+ - Can be used with npx as well
22
28
 
23
29
  ## Install
24
30
 
@@ -28,20 +34,22 @@ yarn install -D apigen-ts
28
34
 
29
35
  ## Usage
30
36
 
31
- ### Generate
37
+ ### 1. Generate
32
38
 
33
39
  ```sh
34
40
  # From url
35
- yarn apigen-ts https://petstore3.swagger.io/api/v3/openapi.json ./api-generated.ts
41
+ yarn apigen-ts https://petstore3.swagger.io/api/v3/openapi.json ./api-client.ts
36
42
 
37
43
  # From file
38
- yarn apigen-ts ./openapi.json ./api-generated.ts
44
+ yarn apigen-ts ./openapi.json ./api-client.ts
39
45
  ```
40
46
 
41
- ### Import
47
+ Run `yarn apigen-ts --help` for more options. Examples of generated clients [here](./examples/).
48
+
49
+ ### 2. Import
42
50
 
43
- ```typescript
44
- import { ApiClient } from "./api-generated"
51
+ ```ts
52
+ import { ApiClient } from "./api-client"
45
53
 
46
54
  const api = new ApiClient({
47
55
  baseUrl: "https://example.com/api",
@@ -49,31 +57,104 @@ const api = new ApiClient({
49
57
  })
50
58
  ```
51
59
 
52
- ### Use
60
+ ### 3. Use
53
61
 
54
- ```typescript
62
+ ```ts
55
63
  // GET /pet/{petId}
56
64
  await api.pet.getPetById(1) // -> Pet
57
65
 
58
66
  // GET /pet/findByStatus?status=sold
59
67
  await api.pet.findPetsByStatus({ status: "sold" }) // -> Pets[]
60
68
 
61
- // PUT /user/{username}
62
- await api.user.updateUser("username", { firstName: "John" }) // second arg is body with type User
69
+ // PUT /user/{username}; second arg body with type User
70
+ await api.user.updateUser("username", { firstName: "John" })
63
71
  ```
64
72
 
65
73
  ## Advanced
66
74
 
67
75
  ### Login flow
68
76
 
69
- ```typescript
77
+ ```ts
70
78
  const { token } = await api.auth.login({ usename, password })
71
79
  api.Config.headers = { Authorization: token }
72
80
 
73
81
  await api.protectedRoute.get() // here authenticated
74
82
  ```
75
83
 
76
- ## Useful for development
84
+ ### Automatic date parsing
85
+
86
+ ```sh
87
+ yarn apigen-ts ./openapi.json ./api-client.ts --parse-dates
88
+ ```
89
+
90
+ ```ts
91
+ const pet = await api.pet.getPetById(1)
92
+ const createdAt: Date = pet.createdAt // date parsed from string with format=date-time
93
+ ```
94
+
95
+ ### Errors handling
96
+
97
+ An exception will be thrown for all unsuccessful return codes.
98
+
99
+ ```ts
100
+ try {
101
+ const pet = await api.pet.getPetById(404)
102
+ } catch (e) {
103
+ console.log(e) // parse error depend of your domain model, e is awaited response.json()
104
+ }
105
+ ```
106
+
107
+ Also you can define custom function to parse error:
108
+
109
+ ```ts
110
+ class MyClient extends ApiClient {
111
+ async ParseError(rep: Response) {
112
+ // do what you want
113
+ return { code: "API_ERROR" }
114
+ }
115
+ }
116
+
117
+ try {
118
+ const api = MyClient()
119
+ const pet = await api.pet.getPetById(404)
120
+ } catch (e) {
121
+ console.log(e) // e is { code: "API_ERROR" }
122
+ }
123
+ ```
124
+
125
+ ### Node.js API
126
+
127
+ Create file like `apigen.mjs` with content:
128
+
129
+ ```js
130
+ import { apigen } from "apigen-ts"
131
+
132
+ await apigen({
133
+ source: "https://petstore3.swagger.io/api/v3/openapi.json",
134
+ output: "./api-client.ts",
135
+ // everything below is optional
136
+ name: "MyApiClient", // default "ApiClient"
137
+ parseDates: true, // default false
138
+ resolveName(ctx, op, proposal) {
139
+ // proposal is [string, string] which represents module.funcName
140
+ if (proposal[0] === "users") return // will use default proposal
141
+
142
+ const [a, b] = op.name.split("/").slice(3, 5) // eg. /api/v1/store/items/search
143
+ return [a, `${op.method}_${b}`] // [store, 'get_items'] -> apiClient.store.get_items()
144
+ },
145
+ })
146
+ ```
147
+
148
+ Then run with: `node apigen.mjs`
149
+
150
+ ## Compare
151
+
152
+ - [openapi-typescript-codegen](https://github.com/ferdikoomen/openapi-typescript-codegen) ([npm](https://www.npmjs.com/package/openapi-typescript-codegen)): no single file mode [#1263](https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1263#issuecomment-1502890838)
153
+ - [openapi-typescript](https://github.com/drwpow/openapi-typescript) ([npm](https://www.npmjs.com/package/openapi-typescript)): low level api; no named types export to use in client code
154
+ - [openapi-generator-cli](https://github.com/OpenAPITools/openapi-generator-cli) ([npm](https://www.npmjs.com/package/@openapitools/openapi-generator-cli)): wrapper around java lib
155
+ - [swagger-typescript-api](https://github.com/acacode/swagger-typescript-api) ([npm](https://www.npmjs.com/package/swagger-typescript-api)): complicated configuration; user-api breaking changes between versions
156
+
157
+ ## Development
77
158
 
78
159
  - https://ts-ast-viewer.com
79
160
  - https://jsonschemalint.com