typedapi-client-helpers 0.1.2 → 0.1.4
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 +167 -0
- package/dist/index.cjs +590 -130
- package/dist/index.d.cts +74 -101
- package/dist/index.d.ts +74 -101
- package/dist/index.js +588 -125
- package/package.json +32 -49
- package/scripts/generate-api-minimal.mjs +740 -740
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# typedapi-client-helpers
|
|
2
|
+
|
|
3
|
+
Reusable TypeScript helpers for generated typed OpenAPI clients.
|
|
4
|
+
|
|
5
|
+
## What this package includes
|
|
6
|
+
|
|
7
|
+
* `typedapi-generate` CLI command
|
|
8
|
+
* OpenAPI TypeScript client generation
|
|
9
|
+
* Typed API wrapper methods
|
|
10
|
+
* Typed query and pagination helpers
|
|
11
|
+
* `ApiResult<T>` response wrapper
|
|
12
|
+
* Toast helpers using `react-toastify`
|
|
13
|
+
* `TypedApiToastProvider`
|
|
14
|
+
* `buildQuery` helper for filters, paging, and sorting
|
|
15
|
+
* `toFormData` helper for file uploads
|
|
16
|
+
* Unauthorized handling for `401` responses
|
|
17
|
+
* Sort direction conversion helpers
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install typedapi-client-helpers
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
This package expects React to already be installed:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install react react-dom react-toastify
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Generate API files
|
|
32
|
+
|
|
33
|
+
Add a script to your project:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"scripts": {
|
|
38
|
+
"generate:api": "typedapi-generate"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Run:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm run generate:api
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
By default, the generator reads Swagger from:
|
|
50
|
+
|
|
51
|
+
```text
|
|
52
|
+
https://localhost:7000/swagger/v1/swagger.json
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
and writes generated files to:
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
src/api
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Configuration
|
|
62
|
+
|
|
63
|
+
Configure the generator in your `package.json`:
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"config": {
|
|
68
|
+
"swaggerUrl": "https://localhost:7000/swagger/v1/swagger.json",
|
|
69
|
+
"swaggerFile": "./swagger.json",
|
|
70
|
+
"apiOutput": "src/api",
|
|
71
|
+
"typedApiUseTypeOnlyImports": true,
|
|
72
|
+
"typedApiUseFilterFormValues": false
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Available Settings
|
|
78
|
+
|
|
79
|
+
| Setting | Description | Default |
|
|
80
|
+
| ----------------------------- | ---------------------------------------------------------------- | ------------------------------------------------ |
|
|
81
|
+
| `swaggerUrl` | URL to the Swagger/OpenAPI document | `https://localhost:7000/swagger/v1/swagger.json` |
|
|
82
|
+
| `swaggerFile` | Local Swagger/OpenAPI file. Overrides `swaggerUrl` when provided | Not set |
|
|
83
|
+
| `apiOutput` | Output directory for generated files | `src/api` |
|
|
84
|
+
| `typedApiUseTypeOnlyImports` | Uses `import type` statements when possible | `false` |
|
|
85
|
+
| `typedApiUseFilterFormValues` | Generates pagination methods using `FilterFormValues[]` | `false` |
|
|
86
|
+
|
|
87
|
+
### Environment Variables
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
SWAGGER_URL=https://localhost:7000/swagger/v1/swagger.json npm run generate:api
|
|
91
|
+
SWAGGER_FILE=./swagger.json npm run generate:api
|
|
92
|
+
API_OUTPUT=src/api npm run generate:api
|
|
93
|
+
TYPED_API_USE_TYPE_ONLY_IMPORTS=true npm run generate:api
|
|
94
|
+
TYPED_API_USE_FILTER_FORM_VALUES=true npm run generate:api
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Usage
|
|
98
|
+
|
|
99
|
+
Import generated API functions:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { getProducts } from "./api";
|
|
103
|
+
|
|
104
|
+
const result = await getProducts();
|
|
105
|
+
|
|
106
|
+
if (result.ok) {
|
|
107
|
+
console.log(result.response);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Toast Notifications
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
await createProduct(product, {
|
|
115
|
+
toastSuccess: {
|
|
116
|
+
title: "Saved",
|
|
117
|
+
message: "Product created successfully."
|
|
118
|
+
},
|
|
119
|
+
toastError: {
|
|
120
|
+
title: "Error",
|
|
121
|
+
message: "Unable to create product."
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Add the provider once in your application:
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
import { TypedApiToastProvider } from "typedapi-client-helpers";
|
|
130
|
+
|
|
131
|
+
function App() {
|
|
132
|
+
return (
|
|
133
|
+
<>
|
|
134
|
+
<TypedApiToastProvider />
|
|
135
|
+
{/* application */}
|
|
136
|
+
</>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### File Uploads
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
import { toFormData } from "typedapi-client-helpers";
|
|
145
|
+
|
|
146
|
+
const formData = toFormData({
|
|
147
|
+
file,
|
|
148
|
+
description: "Import file"
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Recommended Project Setup
|
|
153
|
+
|
|
154
|
+
```text
|
|
155
|
+
src/
|
|
156
|
+
├── api/
|
|
157
|
+
│ ├── clients/
|
|
158
|
+
│ ├── models/
|
|
159
|
+
│ └── index.ts
|
|
160
|
+
├── pages/
|
|
161
|
+
├── components/
|
|
162
|
+
└── services/
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Notes
|
|
166
|
+
|
|
167
|
+
Generated files should not be manually modified. Regenerate them whenever the backend OpenAPI specification changes.
|