typedapi-client-helpers 0.1.13 → 0.1.15

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/dist/index.cjs CHANGED
@@ -180,7 +180,11 @@ async function readResponseBody(response) {
180
180
  }
181
181
  const contentType = response.headers.get("content-type");
182
182
  if (contentType?.includes("application/json")) {
183
- return JSON.parse(text);
183
+ try {
184
+ return JSON.parse(text);
185
+ } catch {
186
+ return text;
187
+ }
184
188
  }
185
189
  return text;
186
190
  }
@@ -198,19 +202,29 @@ async function handleApiResponse(call, options) {
198
202
  await options?.onSuccess?.(result2);
199
203
  return result2;
200
204
  }
201
- const errorData = data;
202
205
  const result = {
203
206
  ok: false,
204
207
  status: response.status,
205
- response: data,
206
- error: errorData
208
+ response: void 0,
209
+ error: data
207
210
  };
208
211
  await options?.onError?.(result);
209
212
  return result;
210
213
  } catch (error) {
214
+ if (error instanceof Response) {
215
+ const errorData = await readResponseBody(error.clone());
216
+ const result2 = {
217
+ ok: false,
218
+ status: error.status,
219
+ response: void 0,
220
+ error: errorData
221
+ };
222
+ await options?.onError?.(result2);
223
+ return result2;
224
+ }
211
225
  const result = {
212
226
  ok: false,
213
- status: error instanceof Response ? error.status : response?.status ?? 0,
227
+ status: response?.status ?? 0,
214
228
  response: void 0,
215
229
  error
216
230
  };
package/dist/index.d.cts CHANGED
@@ -189,9 +189,9 @@ type ApiMethodCallbacks<TResponse> = {
189
189
  params?: RequestParams;
190
190
  };
191
191
 
192
- type ApiMethodOptions<TMethodReturn, TRequestParams = RuntimeRequestParams> = {
193
- onSuccess?: ApiSuccessHandler<ExtractResponse<TMethodReturn>>;
194
- onError?: ApiErrorHandler<ExtractError<TMethodReturn>>;
192
+ type ApiMethodOptions<TResponse, TError = unknown, TRequestParams = RuntimeRequestParams> = {
193
+ onSuccess?: ApiSuccessHandler<TResponse>;
194
+ onError?: ApiErrorHandler<TError>;
195
195
  params?: TRequestParams;
196
196
  };
197
197
 
@@ -267,7 +267,7 @@ type HandleApiResponseOptions<TResponse> = {
267
267
  *
268
268
  * Successful responses return `{ ok: true, status, response }`. Failed responses and thrown errors return `{ ok: false, status, error }`.
269
269
  */
270
- declare function handleApiResponse<TResponse, TError>(call: () => Promise<HttpResponse<TResponse, TError>>, options?: HandleApiResponseOptions<TResponse>): Promise<ApiResult<TResponse>>;
270
+ declare function handleApiResponse<TResponse, TError = unknown>(call: () => Promise<HttpResponse<TResponse, TError>>, options?: HandleApiResponseOptions<TResponse>): Promise<ApiResult<TResponse>>;
271
271
 
272
272
  /** Values supported by the generated API SortDirection enum. */
273
273
  type ApiSortDirection = "Default" | "Ascending" | "Descending";
package/dist/index.d.ts CHANGED
@@ -189,9 +189,9 @@ type ApiMethodCallbacks<TResponse> = {
189
189
  params?: RequestParams;
190
190
  };
191
191
 
192
- type ApiMethodOptions<TMethodReturn, TRequestParams = RuntimeRequestParams> = {
193
- onSuccess?: ApiSuccessHandler<ExtractResponse<TMethodReturn>>;
194
- onError?: ApiErrorHandler<ExtractError<TMethodReturn>>;
192
+ type ApiMethodOptions<TResponse, TError = unknown, TRequestParams = RuntimeRequestParams> = {
193
+ onSuccess?: ApiSuccessHandler<TResponse>;
194
+ onError?: ApiErrorHandler<TError>;
195
195
  params?: TRequestParams;
196
196
  };
197
197
 
@@ -267,7 +267,7 @@ type HandleApiResponseOptions<TResponse> = {
267
267
  *
268
268
  * Successful responses return `{ ok: true, status, response }`. Failed responses and thrown errors return `{ ok: false, status, error }`.
269
269
  */
270
- declare function handleApiResponse<TResponse, TError>(call: () => Promise<HttpResponse<TResponse, TError>>, options?: HandleApiResponseOptions<TResponse>): Promise<ApiResult<TResponse>>;
270
+ declare function handleApiResponse<TResponse, TError = unknown>(call: () => Promise<HttpResponse<TResponse, TError>>, options?: HandleApiResponseOptions<TResponse>): Promise<ApiResult<TResponse>>;
271
271
 
272
272
  /** Values supported by the generated API SortDirection enum. */
273
273
  type ApiSortDirection = "Default" | "Ascending" | "Descending";
package/dist/index.js CHANGED
@@ -147,7 +147,11 @@ async function readResponseBody(response) {
147
147
  }
148
148
  const contentType = response.headers.get("content-type");
149
149
  if (contentType?.includes("application/json")) {
150
- return JSON.parse(text);
150
+ try {
151
+ return JSON.parse(text);
152
+ } catch {
153
+ return text;
154
+ }
151
155
  }
152
156
  return text;
153
157
  }
@@ -165,19 +169,29 @@ async function handleApiResponse(call, options) {
165
169
  await options?.onSuccess?.(result2);
166
170
  return result2;
167
171
  }
168
- const errorData = data;
169
172
  const result = {
170
173
  ok: false,
171
174
  status: response.status,
172
- response: data,
173
- error: errorData
175
+ response: void 0,
176
+ error: data
174
177
  };
175
178
  await options?.onError?.(result);
176
179
  return result;
177
180
  } catch (error) {
181
+ if (error instanceof Response) {
182
+ const errorData = await readResponseBody(error.clone());
183
+ const result2 = {
184
+ ok: false,
185
+ status: error.status,
186
+ response: void 0,
187
+ error: errorData
188
+ };
189
+ await options?.onError?.(result2);
190
+ return result2;
191
+ }
178
192
  const result = {
179
193
  ok: false,
180
- status: error instanceof Response ? error.status : response?.status ?? 0,
194
+ status: response?.status ?? 0,
181
195
  response: void 0,
182
196
  error
183
197
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typedapi-client-helpers",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "Reusable typed OpenAPI client helpers.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -575,7 +575,8 @@ export async function ${methodName}(
575
575
  > | null = null,
576
576
  sortDirection?: SortDirection,
577
577
  options: ApiMethodOptions<
578
- ReturnType<${className}["${methodName}"]>,
578
+ ExtractResponse<ReturnType<${className}["${methodName}"]>>,
579
+ ExtractError<ReturnType<${className}["${methodName}"]>>,
579
580
  RequestParams
580
581
  > = {}
581
582
  ): Promise<ApiResult<ExtractResponse<ReturnType<${className}["${methodName}"]>>>> {
@@ -604,7 +605,8 @@ export async function ${methodName}(
604
605
  export async function ${methodName}(
605
606
  query?: ${toQueryName(methodName)},
606
607
  options: ApiMethodOptions<
607
- ReturnType<${className}["${methodName}"]>,
608
+ ExtractResponse<ReturnType<${className}["${methodName}"]>>,
609
+ ExtractError<ReturnType<${className}["${methodName}"]>>,
608
610
  RequestParams
609
611
  > = {}
610
612
  ): Promise<ApiResult<ExtractResponse<ReturnType<${className}["${methodName}"]>>>> {
@@ -629,7 +631,8 @@ export async function ${methodName}(
629
631
  export async function ${methodName}(
630
632
  query?: ${toQueryName(methodName)},
631
633
  options: ApiMethodOptions<
632
- ReturnType<${className}["${methodName}"]>,
634
+ ExtractResponse<ReturnType<${className}["${methodName}"]>>,
635
+ ExtractError<ReturnType<${className}["${methodName}"]>>,
633
636
  RequestParams
634
637
  > = {}
635
638
  ): Promise<
@@ -665,7 +668,8 @@ export async function ${methodName}(
665
668
  ${className}["${methodName}"]
666
669
  >[0],
667
670
  options: ApiMethodOptions<
668
- ReturnType<${className}["${methodName}"]>,
671
+ ExtractResponse<ReturnType<${className}["${methodName}"]>>,
672
+ ExtractError<ReturnType<${className}["${methodName}"]>>,
669
673
  RequestParams
670
674
  > = {}
671
675
  ): Promise<
@@ -702,7 +706,8 @@ export async function ${methodName}(
702
706
  RequestParams
703
707
  >,
704
708
  ApiMethodOptions<
705
- ReturnType<${className}["${methodName}"]>,
709
+ ExtractResponse<ReturnType<${className}["${methodName}"]>>,
710
+ ExtractError<ReturnType<${className}["${methodName}"]>>,
706
711
  RequestParams
707
712
  >?
708
713
  ]
@@ -720,9 +725,10 @@ export async function ${methodName}(
720
725
  const options =
721
726
  args.length > ${methodArgumentCount}
722
727
  ? (args.pop() as ApiMethodOptions<
723
- ReturnType<${className}["${methodName}"]>,
724
- RequestParams
725
- >)
728
+ ExtractResponse<ReturnType<${className}["${methodName}"]>>,
729
+ ExtractError<ReturnType<${className}["${methodName}"]>>,
730
+ RequestParams
731
+ >)
726
732
  : {};
727
733
 
728
734
  const { onSuccess, onError, params } = options ?? {};
@@ -757,6 +763,7 @@ export async function ${methodName}(
757
763
  "ApiMethodOptions",
758
764
  ...(hasRegularNonQueryMethods ? ["ApiMethodArguments"] : []),
759
765
  "ExtractResponse",
766
+ "ExtractError",
760
767
 
761
768
  ...(hasPaginatedMethods && useFilterFormValues
762
769
  ? [