apigen-ts 0.0.3 → 0.1.0

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/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # OpenAPI TypeScript client generator
1
+ # apigen-ts
2
2
 
3
3
  <div align="center">
4
4
 
@@ -10,15 +10,24 @@
10
10
 
11
11
  </div>
12
12
 
13
+ <div align="center">
14
+ <img src="./logo.svg" alt="apigen-ts logo" height="80" />
15
+ </div>
16
+
17
+ <div align="center">
18
+ TypeScript api client generator from OpenAPI specification
19
+ </div>
20
+
13
21
  ## Features
14
22
 
15
- - Generates ready to use ApiClient with types (using `fetch`)
23
+ - Generates ready to use `ApiClient` with types (using `fetch`)
16
24
  - Single output file, minimal third-party code
17
25
  - Load schema from JSON / YAML, locally and remote
18
26
  - Ability to customize `fetch` with your custom function
19
- - Uses `type` instead of `interface`, so no problem with declaration merging
27
+ - Uses `type` instead of `interface`, no problem with declaration merging
20
28
  - Automatic formating with Prettier
21
- - Parses dates automatically
29
+ - Can parse dates from date-time format (`--parse-dates` flag)
30
+ - Support OpenAPI v2, v3, v3.1
22
31
 
23
32
  ## Install
24
33
 
@@ -32,16 +41,18 @@ yarn install -D apigen-ts
32
41
 
33
42
  ```sh
34
43
  # From url
35
- yarn apigen-ts https://petstore3.swagger.io/api/v3/openapi.json ./api-generated.ts
44
+ yarn apigen-ts https://petstore3.swagger.io/api/v3/openapi.json ./api-client.ts
36
45
 
37
46
  # From file
38
- yarn apigen-ts ./openapi.json ./api-generated.ts
47
+ yarn apigen-ts ./openapi.json ./api-client.ts
39
48
  ```
40
49
 
50
+ Run `yarn apigen-ts --help` for more options. See examples of generated clients [here](./examples/).
51
+
41
52
  ### Import
42
53
 
43
54
  ```typescript
44
- import { ApiClient } from "./api-generated"
55
+ import { ApiClient } from "./api-client"
45
56
 
46
57
  const api = new ApiClient({
47
58
  baseUrl: "https://example.com/api",
@@ -58,8 +69,8 @@ await api.pet.getPetById(1) // -> Pet
58
69
  // GET /pet/findByStatus?status=sold
59
70
  await api.pet.findPetsByStatus({ status: "sold" }) // -> Pets[]
60
71
 
61
- // PUT /user/{username}
62
- await api.user.updateUser("username", { firstName: "John" }) // second arg is body with type User
72
+ // PUT /user/{username}; second arg body with type User
73
+ await api.user.updateUser("username", { firstName: "John" })
63
74
  ```
64
75
 
65
76
  ## Advanced
@@ -73,6 +84,31 @@ api.Config.headers = { Authorization: token }
73
84
  await api.protectedRoute.get() // here authenticated
74
85
  ```
75
86
 
87
+ ### NodeJS API
88
+
89
+ Create file like `apigen.mjs` with content:
90
+
91
+ ```javascript
92
+ import { apigen } from "apigen-ts"
93
+
94
+ await apigen({
95
+ source: "https://petstore3.swagger.io/api/v3/openapi.json",
96
+ output: "./api-client.ts",
97
+ // everything below is optional
98
+ name: "MyApiClient", // default "ApiClient"
99
+ parseDates: true, // default false
100
+ resolveName(ctx, op, proposal) {
101
+ // proposal is [string, string] which represents module.funcName
102
+ if (proposal[0] === "users") return // will use default proposal
103
+
104
+ const [a, b] = op.name.split("/").slice(3, 5) // eg. /api/v1/store/items/search
105
+ return [a, `${op.method}_${b}`] // [store, 'get_items'] -> apiClient.store.get_items()
106
+ },
107
+ })
108
+ ```
109
+
110
+ Then run with: `node apigen.mjs`
111
+
76
112
  ## Useful for development
77
113
 
78
114
  - https://ts-ast-viewer.com