typedapi-client-helpers 0.1.12 → 0.1.13

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.d.cts CHANGED
@@ -189,6 +189,17 @@ 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>>;
195
+ params?: TRequestParams;
196
+ };
197
+
198
+ type ApiMethodArguments<TMethod extends (...args: any[]) => unknown, TRequestParams = RuntimeRequestParams> = Parameters<TMethod> extends [
199
+ ...infer Arguments,
200
+ TRequestParams?
201
+ ] ? Arguments : Parameters<TMethod>;
202
+
192
203
  /**
193
204
  * Sort direction value accepted by generated query objects.
194
205
  *
@@ -287,4 +298,4 @@ declare function getSortDirectionFromSortType(sortType: SortType | null | undefi
287
298
  */
288
299
  declare function toFormData<T extends object>(payload: T): FormData;
289
300
 
290
- export { type ApiErrorHandler, type ApiErrorResult, type ApiMethodCallbacks, type ApiResult, type ApiSortDirection, type ApiSuccessHandler, type ApiSuccessResult, type ExtractDataIfPaginated, type ExtractError, type ExtractResponse, type FilterFormValues, type FilterType, type HandleApiResponseOptions, type HttpResponse, type OptionValue, type RuntimeRequestParams, type SortDirection, type SortType, type SortableKeys, type UnwrapArray, type WithoutRequestParams, buildQuery, extractArgsCallbacksAndParams, extractArgsToastsAndParams, getSortDirectionFromSortType, getSortTypeFromSortDirection, handleApiResponse, sortTypes, toFormData };
301
+ export { type ApiErrorHandler, type ApiErrorResult, type ApiMethodArguments, type ApiMethodCallbacks, type ApiMethodOptions, type ApiResult, type ApiSortDirection, type ApiSuccessHandler, type ApiSuccessResult, type ExtractDataIfPaginated, type ExtractError, type ExtractResponse, type FilterFormValues, type FilterType, type HandleApiResponseOptions, type HttpResponse, type OptionValue, type RuntimeRequestParams, type SortDirection, type SortType, type SortableKeys, type UnwrapArray, type WithoutRequestParams, buildQuery, extractArgsCallbacksAndParams, extractArgsToastsAndParams, getSortDirectionFromSortType, getSortTypeFromSortDirection, handleApiResponse, sortTypes, toFormData };
package/dist/index.d.ts CHANGED
@@ -189,6 +189,17 @@ 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>>;
195
+ params?: TRequestParams;
196
+ };
197
+
198
+ type ApiMethodArguments<TMethod extends (...args: any[]) => unknown, TRequestParams = RuntimeRequestParams> = Parameters<TMethod> extends [
199
+ ...infer Arguments,
200
+ TRequestParams?
201
+ ] ? Arguments : Parameters<TMethod>;
202
+
192
203
  /**
193
204
  * Sort direction value accepted by generated query objects.
194
205
  *
@@ -287,4 +298,4 @@ declare function getSortDirectionFromSortType(sortType: SortType | null | undefi
287
298
  */
288
299
  declare function toFormData<T extends object>(payload: T): FormData;
289
300
 
290
- export { type ApiErrorHandler, type ApiErrorResult, type ApiMethodCallbacks, type ApiResult, type ApiSortDirection, type ApiSuccessHandler, type ApiSuccessResult, type ExtractDataIfPaginated, type ExtractError, type ExtractResponse, type FilterFormValues, type FilterType, type HandleApiResponseOptions, type HttpResponse, type OptionValue, type RuntimeRequestParams, type SortDirection, type SortType, type SortableKeys, type UnwrapArray, type WithoutRequestParams, buildQuery, extractArgsCallbacksAndParams, extractArgsToastsAndParams, getSortDirectionFromSortType, getSortTypeFromSortDirection, handleApiResponse, sortTypes, toFormData };
301
+ export { type ApiErrorHandler, type ApiErrorResult, type ApiMethodArguments, type ApiMethodCallbacks, type ApiMethodOptions, type ApiResult, type ApiSortDirection, type ApiSuccessHandler, type ApiSuccessResult, type ExtractDataIfPaginated, type ExtractError, type ExtractResponse, type FilterFormValues, type FilterType, type HandleApiResponseOptions, type HttpResponse, type OptionValue, type RuntimeRequestParams, type SortDirection, type SortType, type SortableKeys, type UnwrapArray, type WithoutRequestParams, buildQuery, extractArgsCallbacksAndParams, extractArgsToastsAndParams, getSortDirectionFromSortType, getSortTypeFromSortDirection, handleApiResponse, sortTypes, toFormData };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typedapi-client-helpers",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "Reusable typed OpenAPI client helpers.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -33,16 +33,16 @@ const config = {
33
33
  const swaggerFile =
34
34
  process.env.SWAGGER_FILE ??
35
35
  process.env.npm_config_swagger_file ??
36
- config.swaggerFile ??
37
- "swagger/swagger.json";
36
+ config.swaggerFile;
38
37
 
39
38
  const swaggerUrl =
40
39
  process.env.SWAGGER_URL ??
41
40
  process.env.npm_config_swagger_url ??
42
- config.swaggerUrl ??
43
- "https://localhost:7000/swagger/v1/swagger.json";
41
+ config.swaggerUrl;
44
42
 
45
- const swaggerInput = swaggerFile ? path.resolve(cwd, swaggerFile) : swaggerUrl;
43
+ const swaggerInput = swaggerFile
44
+ ? path.resolve(cwd, swaggerFile)
45
+ : (swaggerUrl ?? path.resolve(cwd, "swagger/swagger.json"));
46
46
 
47
47
  const apiRoot = path.resolve(
48
48
  cwd,
@@ -112,10 +112,6 @@ function toQueryName(methodName) {
112
112
  return `${pascalCase(methodName)}Query`;
113
113
  }
114
114
 
115
- function toOptionsName(methodName) {
116
- return `${pascalCase(methodName)}Options`;
117
- }
118
-
119
115
  function cleanDirectory(directory) {
120
116
  if (fs.existsSync(directory)) {
121
117
  fs.rmSync(directory, { recursive: true, force: true });
@@ -552,22 +548,6 @@ export type ${toQueryName(methodName)} =
552
548
  NonNullable<
553
549
  Parameters<${className}["${methodName}"]>[0]
554
550
  >;
555
- `.trim(),
556
- )
557
- .join("\n\n");
558
-
559
- const methodOptionTypes = generatedMethods
560
- .map(({ methodName }) =>
561
- `
562
- export type ${toOptionsName(methodName)} = {
563
- onSuccess?: ApiSuccessHandler<
564
- ExtractResponse<ReturnType<${className}["${methodName}"]>>
565
- >;
566
- onError?: ApiErrorHandler<
567
- ExtractError<ReturnType<${className}["${methodName}"]>>
568
- >;
569
- params?: RequestParams;
570
- };
571
551
  `.trim(),
572
552
  )
573
553
  .join("\n\n");
@@ -594,7 +574,10 @@ export async function ${methodName}(
594
574
  ExtractResponse<ReturnType<${className}["${methodName}"]>>
595
575
  > | null = null,
596
576
  sortDirection?: SortDirection,
597
- options: ${toOptionsName(methodName)} = {}
577
+ options: ApiMethodOptions<
578
+ ReturnType<${className}["${methodName}"]>,
579
+ RequestParams
580
+ > = {}
598
581
  ): Promise<ApiResult<ExtractResponse<ReturnType<${className}["${methodName}"]>>>> {
599
582
  const { onSuccess, onError, params } = options;
600
583
 
@@ -620,7 +603,10 @@ export async function ${methodName}(
620
603
  return `
621
604
  export async function ${methodName}(
622
605
  query?: ${toQueryName(methodName)},
623
- options: ${toOptionsName(methodName)} = {}
606
+ options: ApiMethodOptions<
607
+ ReturnType<${className}["${methodName}"]>,
608
+ RequestParams
609
+ > = {}
624
610
  ): Promise<ApiResult<ExtractResponse<ReturnType<${className}["${methodName}"]>>>> {
625
611
  const { onSuccess, onError, params } = options;
626
612
 
@@ -642,7 +628,10 @@ export async function ${methodName}(
642
628
  `
643
629
  export async function ${methodName}(
644
630
  query?: ${toQueryName(methodName)},
645
- options: ${toOptionsName(methodName)} = {}
631
+ options: ApiMethodOptions<
632
+ ReturnType<${className}["${methodName}"]>,
633
+ RequestParams
634
+ > = {}
646
635
  ): Promise<
647
636
  ApiResult<
648
637
  ExtractResponse<
@@ -675,7 +664,10 @@ export async function ${methodName}(
675
664
  data: Parameters<
676
665
  ${className}["${methodName}"]
677
666
  >[0],
678
- options: ${toOptionsName(methodName)} = {}
667
+ options: ApiMethodOptions<
668
+ ReturnType<${className}["${methodName}"]>,
669
+ RequestParams
670
+ > = {}
679
671
  ): Promise<
680
672
  ApiResult<
681
673
  ExtractResponse<
@@ -706,9 +698,13 @@ export async function ${methodName}(
706
698
  export async function ${methodName}(
707
699
  ...argsWithOptions: [
708
700
  ...ApiMethodArguments<
709
- ${className}["${methodName}"]
701
+ ${className}["${methodName}"],
702
+ RequestParams
710
703
  >,
711
- ${toOptionsName(methodName)}?
704
+ ApiMethodOptions<
705
+ ReturnType<${className}["${methodName}"]>,
706
+ RequestParams
707
+ >?
712
708
  ]
713
709
  ): Promise<
714
710
  ApiResult<
@@ -723,7 +719,10 @@ export async function ${methodName}(
723
719
 
724
720
  const options =
725
721
  args.length > ${methodArgumentCount}
726
- ? (args.pop() as ${toOptionsName(methodName)})
722
+ ? (args.pop() as ApiMethodOptions<
723
+ ReturnType<${className}["${methodName}"]>,
724
+ RequestParams
725
+ >)
727
726
  : {};
728
727
 
729
728
  const { onSuccess, onError, params } = options ?? {};
@@ -755,10 +754,9 @@ export async function ${methodName}(
755
754
 
756
755
  const runtimeTypeImportNames = [
757
756
  "ApiResult",
758
- "ApiSuccessHandler",
759
- "ApiErrorHandler",
757
+ "ApiMethodOptions",
758
+ ...(hasRegularNonQueryMethods ? ["ApiMethodArguments"] : []),
760
759
  "ExtractResponse",
761
- "ExtractError",
762
760
 
763
761
  ...(hasPaginatedMethods && useFilterFormValues
764
762
  ? [
@@ -812,20 +810,6 @@ export async function ${methodName}(
812
810
  })
813
811
  : "";
814
812
 
815
- const apiMethodArgumentsType = hasRegularNonQueryMethods
816
- ? `
817
- type ApiMethodArguments<
818
- TMethod extends (...args: any[]) => unknown
819
- > =
820
- Parameters<TMethod> extends [
821
- ...infer Arguments,
822
- RequestParams?
823
- ]
824
- ? Arguments
825
- : Parameters<TMethod>;
826
- `.trim()
827
- : "";
828
-
829
813
  const content = `
830
814
  ${runtimeValueImports}
831
815
 
@@ -837,18 +821,11 @@ ${generatedValueImports}
837
821
 
838
822
  ${generatedTypeImports}
839
823
 
840
- ${apiMethodArgumentsType}
841
-
842
824
  /* =======================
843
825
  Query Types
844
826
  ======================= */
845
827
  ${queryTypes}
846
828
 
847
- /* =======================
848
- Method Option Types
849
- ======================= */
850
- ${methodOptionTypes}
851
-
852
829
  /* =======================
853
830
  API Instance
854
831
  ======================= */