typedapi-client-helpers 0.1.8 → 0.1.10
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 +64 -20
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/package.json +1 -2
- package/scripts/generate-api-minimal.mjs +12 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
`typedapi-client-helpers` generates a typed TypeScript API client from an OpenAPI/Swagger document and adds reusable helper types and functions for API results, callbacks, filtering, sorting, request parameters, and multipart uploads.
|
|
4
4
|
|
|
5
|
-
The package is
|
|
5
|
+
The package is intended for frontend projects that want a generated API client while still keeping generated files inside their own source tree.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -28,7 +28,13 @@ Run the generator:
|
|
|
28
28
|
npm run generate:api
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
By default the generator reads the Swagger document from `https://localhost:7000/swagger/v1/swagger.json` and writes the generated API client to `src/api`.
|
|
31
|
+
By default, the generator reads the Swagger document from `https://localhost:7000/swagger/v1/swagger.json` and writes the generated API client to `src/api`.
|
|
32
|
+
|
|
33
|
+
You can also run the command directly:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npx typedapi-generate
|
|
37
|
+
```
|
|
32
38
|
|
|
33
39
|
## Configuration
|
|
34
40
|
|
|
@@ -44,7 +50,7 @@ Configuration can be supplied in the `config` section of your `package.json`, wi
|
|
|
44
50
|
"apiOutput": "src/api",
|
|
45
51
|
"typedApiUseTypeOnlyImports": true,
|
|
46
52
|
"typedApiUseFilterFormValues": false,
|
|
47
|
-
"typedApiDefaultFunctionsPath": "
|
|
53
|
+
"typedApiDefaultFunctionsPath": "src/defaultApiFunctions",
|
|
48
54
|
"typedApiDefaultSuccessHandler": "handleGoodResult",
|
|
49
55
|
"typedApiDefaultErrorHandler": "handleErrors"
|
|
50
56
|
}
|
|
@@ -62,19 +68,22 @@ Configuration can be supplied in the `config` section of your `package.json`, wi
|
|
|
62
68
|
| `apiOutput` | `src/api` | Directory where the generated API files are written. |
|
|
63
69
|
| `typedApiUseTypeOnlyImports` | `false` | Generates `import type` statements for imports that are only used as types. |
|
|
64
70
|
| `typedApiUseFilterFormValues` | `false` | Generates paginated methods that accept `FilterFormValues[]`, page values, and sort values instead of a prebuilt query object. |
|
|
65
|
-
| `typedApiDefaultFunctionsPath` |
|
|
71
|
+
| `typedApiDefaultFunctionsPath` | `src/defaultApiFunctions` | Import path used by generated method files for shared success and error handlers. |
|
|
66
72
|
| `typedApiDefaultSuccessHandler` | `handleGoodResult` | Name of the default success handler function imported into generated methods. |
|
|
67
73
|
| `typedApiDefaultErrorHandler` | `handleErrors` | Name of the default error handler function imported into generated methods. |
|
|
68
74
|
|
|
69
75
|
### Environment variables
|
|
70
76
|
|
|
71
|
-
| Environment variable
|
|
72
|
-
|
|
|
73
|
-
| `SWAGGER_URL`
|
|
74
|
-
| `SWAGGER_FILE`
|
|
75
|
-
| `API_OUTPUT`
|
|
76
|
-
| `TYPED_API_USE_TYPE_ONLY_IMPORTS`
|
|
77
|
-
| `TYPED_API_USE_FILTER_FORM_VALUES`
|
|
77
|
+
| Environment variable | Description |
|
|
78
|
+
| ----------------------------------- | -------------------------------------------------- |
|
|
79
|
+
| `SWAGGER_URL` | URL of the OpenAPI/Swagger document. |
|
|
80
|
+
| `SWAGGER_FILE` | Local path to an OpenAPI/Swagger document. |
|
|
81
|
+
| `API_OUTPUT` | Directory where generated API files are written. |
|
|
82
|
+
| `TYPED_API_USE_TYPE_ONLY_IMPORTS` | Enables type-only imports when set to `true`. |
|
|
83
|
+
| `TYPED_API_USE_FILTER_FORM_VALUES` | Enables filter-form based paginated methods. |
|
|
84
|
+
| `TYPED_API_DEFAULT_FUNCTIONS_PATH` | Path to the shared default callback handler file. |
|
|
85
|
+
| `TYPED_API_DEFAULT_SUCCESS_HANDLER` | Export name of the shared default success handler. |
|
|
86
|
+
| `TYPED_API_DEFAULT_ERROR_HANDLER` | Export name of the shared default error handler. |
|
|
78
87
|
|
|
79
88
|
Example:
|
|
80
89
|
|
|
@@ -88,6 +97,7 @@ SWAGGER_URL=https://localhost:7000/swagger/v1/swagger.json npm run generate:api
|
|
|
88
97
|
npm run generate:api --swagger-url=https://localhost:7000/swagger/v1/swagger.json
|
|
89
98
|
npm run generate:api --swagger-file=swagger/openapi.json
|
|
90
99
|
npm run generate:api --api-output=src/api
|
|
100
|
+
npm run generate:api --typed-api-use-filter-form-values=true
|
|
91
101
|
```
|
|
92
102
|
|
|
93
103
|
## Generated structure
|
|
@@ -136,6 +146,39 @@ if (result.ok) {
|
|
|
136
146
|
|
|
137
147
|
Checking `result.ok` narrows the result automatically.
|
|
138
148
|
|
|
149
|
+
## Generated method argument order
|
|
150
|
+
|
|
151
|
+
Generated wrappers preserve the original generated API arguments and then add optional wrapper-only arguments at the end.
|
|
152
|
+
|
|
153
|
+
For query methods:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
await getSuppliers(query, onSuccess, onError, params);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
For non-query methods:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
await updateSupplier(id, body, onSuccess, onError, params);
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
For filter-form paginated methods when `typedApiUseFilterFormValues` is enabled:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
await getSuppliers(
|
|
169
|
+
filters,
|
|
170
|
+
page,
|
|
171
|
+
pageSize,
|
|
172
|
+
sortBy,
|
|
173
|
+
sortDirection,
|
|
174
|
+
onSuccess,
|
|
175
|
+
onError,
|
|
176
|
+
params,
|
|
177
|
+
);
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
The final `params` object is forwarded to the generated HTTP client and can contain values such as headers or other request options.
|
|
181
|
+
|
|
139
182
|
## Success and error callbacks
|
|
140
183
|
|
|
141
184
|
Callbacks receive the full result object, not just the raw response or error.
|
|
@@ -268,15 +311,16 @@ Arrays are appended as repeated fields, files keep their filename, primitives be
|
|
|
268
311
|
|
|
269
312
|
### Utility functions
|
|
270
313
|
|
|
271
|
-
| Export | Description
|
|
272
|
-
| ---------------------------------- |
|
|
273
|
-
| `buildQuery<TQuery, TSortModel>()` | Builds a query object from filter form values, page values, and sort values.
|
|
274
|
-
| `extractArgsCallbacksAndParams()` | Splits wrapper arguments into original API arguments, success callback, error callback, and request params.
|
|
275
|
-
| `extractArgsToastsAndParams()` | Backwards-compatible
|
|
276
|
-
| `handleApiResponse()` | Executes a generated API call and converts the response or thrown error into `ApiResult<T>`.
|
|
277
|
-
| `
|
|
278
|
-
| `
|
|
279
|
-
| `
|
|
314
|
+
| Export | Description |
|
|
315
|
+
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------- |
|
|
316
|
+
| `buildQuery<TQuery, TSortModel>()` | Builds a query object from filter form values, page values, and sort values. |
|
|
317
|
+
| `extractArgsCallbacksAndParams()` | Splits wrapper arguments into original API arguments, success callback, error callback, and request params. |
|
|
318
|
+
| `extractArgsToastsAndParams()` | Backwards-compatible extractor for wrapper arguments with success/error handlers and request params. |
|
|
319
|
+
| `handleApiResponse()` | Executes a generated API call and converts the response or thrown error into `ApiResult<T>`. |
|
|
320
|
+
| `HandleApiResponseOptions<T>` | Options object for passing success and error callbacks to `handleApiResponse`. |
|
|
321
|
+
| `getSortTypeFromSortDirection()` | Converts an API sort direction into a UI `SortType`. |
|
|
322
|
+
| `getSortDirectionFromSortType()` | Converts a UI `SortType` into an API sort direction. |
|
|
323
|
+
| `toFormData()` | Converts an object payload into `FormData`. |
|
|
280
324
|
|
|
281
325
|
### Default callback functions
|
|
282
326
|
|
package/dist/index.d.cts
CHANGED
|
@@ -207,6 +207,8 @@ declare function buildQuery<TQuery, TSortModel = keyof TQuery>(filters: FilterFo
|
|
|
207
207
|
/**
|
|
208
208
|
* Extracts wrapper-only arguments in this order:
|
|
209
209
|
* original API arguments, onSuccess, onError, request params.
|
|
210
|
+
*
|
|
211
|
+
* Kept for backwards compatibility with generated files that used toast-oriented naming.
|
|
210
212
|
*/
|
|
211
213
|
declare function extractArgsToastsAndParams<TArgs extends readonly unknown[], TResponse>(argsWithCallbacks: [
|
|
212
214
|
...TArgs,
|
package/dist/index.d.ts
CHANGED
|
@@ -207,6 +207,8 @@ declare function buildQuery<TQuery, TSortModel = keyof TQuery>(filters: FilterFo
|
|
|
207
207
|
/**
|
|
208
208
|
* Extracts wrapper-only arguments in this order:
|
|
209
209
|
* original API arguments, onSuccess, onError, request params.
|
|
210
|
+
*
|
|
211
|
+
* Kept for backwards compatibility with generated files that used toast-oriented naming.
|
|
210
212
|
*/
|
|
211
213
|
declare function extractArgsToastsAndParams<TArgs extends readonly unknown[], TResponse>(argsWithCallbacks: [
|
|
212
214
|
...TArgs,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typedapi-client-helpers",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"description": "Reusable typed OpenAPI client helpers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
},
|
|
19
19
|
"homepage": "https://github.com/Stef-Buurman/TypedApi/tree/main/npm/typed-api-client#readme",
|
|
20
20
|
"config": {
|
|
21
|
-
"swaggerFile": "swagger/TestSwagger.json",
|
|
22
21
|
"apiOutput": "src/api",
|
|
23
22
|
"typedApiDefaultFunctionsPath": "src/defaultApiFunctions",
|
|
24
23
|
"typedApiDefaultSuccessHandler": "handleGoodResult",
|
|
@@ -122,6 +122,16 @@ async function writeFormattedFile(filePath, content) {
|
|
|
122
122
|
fs.writeFileSync(filePath, content);
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
function removeGeneratedRouteFiles() {
|
|
126
|
+
const files = fs.readdirSync(generatedDir);
|
|
127
|
+
|
|
128
|
+
for (const file of files) {
|
|
129
|
+
if (/route\.ts$/i.test(file)) {
|
|
130
|
+
fs.rmSync(path.join(generatedDir, file), { force: true });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
125
135
|
async function generateOpenApiClient() {
|
|
126
136
|
cleanDirectory(generatedDir);
|
|
127
137
|
cleanDirectory(methodsDir);
|
|
@@ -147,6 +157,7 @@ async function generateOpenApiClient() {
|
|
|
147
157
|
}
|
|
148
158
|
|
|
149
159
|
await generateApi(generateOptions);
|
|
160
|
+
removeGeneratedRouteFiles();
|
|
150
161
|
}
|
|
151
162
|
|
|
152
163
|
function createImportStatement({ names, from, typeOnly = false }) {
|
|
@@ -471,7 +482,7 @@ async function createMethodFiles() {
|
|
|
471
482
|
file.endsWith(".ts") &&
|
|
472
483
|
file !== "http-client.ts" &&
|
|
473
484
|
file !== "data-contracts.ts" &&
|
|
474
|
-
|
|
485
|
+
!/route\.ts$/i.test(file) &&
|
|
475
486
|
!file.includes(".api"),
|
|
476
487
|
);
|
|
477
488
|
|