typedapi-client-helpers 0.1.1 → 0.1.3
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.d.cts +1 -4
- package/dist/index.d.ts +1 -4
- package/package.json +3 -4
- package/scripts/generate-api-minimal.mjs +1 -6
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.
|
package/dist/index.d.cts
CHANGED
|
@@ -40,10 +40,7 @@ type ExtractDataIfPaginated<T> = T extends {
|
|
|
40
40
|
data?: (infer U)[] | null;
|
|
41
41
|
} ? U : T;
|
|
42
42
|
type SortableKeys<T> = keyof UnwrapArray<ExtractDataIfPaginated<T>>;
|
|
43
|
-
type WithoutRequestParams<T extends any[]> = T extends [
|
|
44
|
-
infer First,
|
|
45
|
-
...infer Rest
|
|
46
|
-
] ? First extends RequestParams ? Rest : [First, ...WithoutRequestParams<Rest>] : [];
|
|
43
|
+
type WithoutRequestParams<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends RequestParams ? Rest : [First, ...WithoutRequestParams<Rest>] : [];
|
|
47
44
|
|
|
48
45
|
/**
|
|
49
46
|
* Standard result wrapper for API calls.
|
package/dist/index.d.ts
CHANGED
|
@@ -40,10 +40,7 @@ type ExtractDataIfPaginated<T> = T extends {
|
|
|
40
40
|
data?: (infer U)[] | null;
|
|
41
41
|
} ? U : T;
|
|
42
42
|
type SortableKeys<T> = keyof UnwrapArray<ExtractDataIfPaginated<T>>;
|
|
43
|
-
type WithoutRequestParams<T extends any[]> = T extends [
|
|
44
|
-
infer First,
|
|
45
|
-
...infer Rest
|
|
46
|
-
] ? First extends RequestParams ? Rest : [First, ...WithoutRequestParams<Rest>] : [];
|
|
43
|
+
type WithoutRequestParams<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends RequestParams ? Rest : [First, ...WithoutRequestParams<Rest>] : [];
|
|
47
44
|
|
|
48
45
|
/**
|
|
49
46
|
* Standard result wrapper for API calls.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typedapi-client-helpers",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Reusable typed OpenAPI client helpers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"README.md"
|
|
13
13
|
],
|
|
14
14
|
"config": {
|
|
15
|
+
"swaggerFile": "swagger/TestSwagger.json",
|
|
15
16
|
"apiOutput": "src/api"
|
|
16
17
|
},
|
|
17
18
|
"bin": {
|
|
@@ -19,11 +20,9 @@
|
|
|
19
20
|
},
|
|
20
21
|
"scripts": {
|
|
21
22
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
22
|
-
"generate:api": "node ./scripts/generate-api-minimal.mjs"
|
|
23
|
-
"pack:local": "npm pack"
|
|
23
|
+
"generate:api": "node ./scripts/generate-api-minimal.mjs"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"prettier": "^3.8.3",
|
|
27
26
|
"react-toastify": "^11.0.5",
|
|
28
27
|
"swagger-typescript-api": "^13.2.16"
|
|
29
28
|
},
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
|
-
import prettier from "prettier";
|
|
5
4
|
import { generateApi } from "swagger-typescript-api";
|
|
6
5
|
|
|
7
6
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -85,11 +84,7 @@ function cleanDirectory(directory) {
|
|
|
85
84
|
}
|
|
86
85
|
|
|
87
86
|
async function writeFormattedFile(filePath, content) {
|
|
88
|
-
|
|
89
|
-
parser: "typescript",
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
fs.writeFileSync(filePath, formatted);
|
|
87
|
+
fs.writeFileSync(filePath, content);
|
|
93
88
|
}
|
|
94
89
|
|
|
95
90
|
async function generateOpenApiClient() {
|