typedapi-client-helpers 0.1.3 → 0.1.5

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
@@ -1,47 +1,3 @@
1
- import * as react from 'react';
2
-
3
- type QueryParamsType = Record<string | number, any>;
4
- type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
5
- interface FullRequestParams extends Omit<RequestInit, "body"> {
6
- /** set parameter to `true` for call `securityWorker` for this request */
7
- secure?: boolean;
8
- /** request path */
9
- path: string;
10
- /** content type of request body */
11
- type?: ContentType;
12
- /** query params */
13
- query?: QueryParamsType;
14
- /** format of response (i.e. response.json() -> format: "json") */
15
- format?: ResponseFormat;
16
- /** request body */
17
- body?: unknown;
18
- /** base url */
19
- baseUrl?: string;
20
- /** request cancellation token */
21
- cancelToken?: CancelToken;
22
- }
23
- type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
24
- interface HttpResponse$1<D extends unknown, E extends unknown = unknown> extends Response {
25
- data: D;
26
- error: E;
27
- }
28
- type CancelToken = Symbol | string | number;
29
- declare enum ContentType {
30
- Json = "application/json",
31
- JsonApi = "application/vnd.api+json",
32
- FormData = "multipart/form-data",
33
- UrlEncoded = "application/x-www-form-urlencoded",
34
- Text = "text/plain"
35
- }
36
-
37
- type ExtractResponse<T> = T extends Promise<HttpResponse$1<infer R, any>> ? R : never;
38
- type UnwrapArray<T> = T extends (infer U)[] ? U : T;
39
- type ExtractDataIfPaginated<T> = T extends {
40
- data?: (infer U)[] | null;
41
- } ? U : T;
42
- type SortableKeys<T> = keyof UnwrapArray<ExtractDataIfPaginated<T>>;
43
- type WithoutRequestParams<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends RequestParams ? Rest : [First, ...WithoutRequestParams<Rest>] : [];
44
-
45
1
  /**
46
2
  * Standard result wrapper for API calls.
47
3
  *
@@ -97,25 +53,62 @@ interface FilterFormValues<TQuery> {
97
53
  */
98
54
  type SortType = "Default" | "Neutral" | "Ascending" | "Descending";
99
55
 
100
- /**
101
- * Toast helper for showing consistent success, error, warning, and info messages.
102
- *
103
- * Wraps `react-toastify` with a shared title/message layout.
104
- */
105
- declare const toast: {
106
- success(message: string, title?: string): void;
107
- error(message: string, title?: string): void;
108
- warning(message: string, title?: string): void;
109
- info(message: string, title?: string): void;
110
- errorResponse(error: Response | unknown): Promise<void>;
111
- };
56
+ type QueryParamsType = Record<string | number, any>;
57
+ type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
58
+ interface FullRequestParams extends Omit<RequestInit, "body"> {
59
+ /** set parameter to `true` for call `securityWorker` for this request */
60
+ secure?: boolean;
61
+ /** request path */
62
+ path: string;
63
+ /** content type of request body */
64
+ type?: ContentType;
65
+ /** query params */
66
+ query?: QueryParamsType;
67
+ /** format of response (i.e. response.json() -> format: "json") */
68
+ format?: ResponseFormat;
69
+ /** request body */
70
+ body?: unknown;
71
+ /** base url */
72
+ baseUrl?: string;
73
+ /** request cancellation token */
74
+ cancelToken?: CancelToken;
75
+ }
76
+ type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
77
+ interface HttpResponse$1<D extends unknown, E extends unknown = unknown> extends Response {
78
+ data: D;
79
+ error: E;
80
+ }
81
+ type CancelToken = Symbol | string | number;
82
+ declare enum ContentType {
83
+ Json = "application/json",
84
+ JsonApi = "application/vnd.api+json",
85
+ FormData = "multipart/form-data",
86
+ UrlEncoded = "application/x-www-form-urlencoded",
87
+ Text = "text/plain"
88
+ }
112
89
 
90
+ type ExtractResponse<T> = T extends Promise<HttpResponse$1<infer Response, unknown>> ? Response : never;
91
+ type UnwrapArray<T> = T extends readonly (infer Item)[] ? Item : T;
92
+ type ExtractDataIfPaginated<T> = T extends {
93
+ data?: readonly (infer Item)[] | null;
94
+ } ? Item : T;
95
+ type SortableKeys<T> = keyof UnwrapArray<ExtractDataIfPaginated<T>>;
113
96
  /**
114
- * Shared toast container for TypedApi UI notifications.
97
+ * Removes the optional RequestParams argument from the end
98
+ * of a generated API method parameter tuple.
99
+ *
100
+ * Example:
115
101
  *
116
- * Add this once near the root of the app to enable toast messages.
102
+ * [data: CreateSupplierParams, params?: RequestParams]
103
+ *
104
+ * becomes:
105
+ *
106
+ * [data: CreateSupplierParams]
117
107
  */
118
- declare function TypedApiToastProvider(): react.JSX.Element;
108
+ type WithoutRequestParams<T extends readonly unknown[]> = T extends readonly [
109
+ ...infer Arguments,
110
+ RequestParams?
111
+ ] ? Arguments : T;
119
112
 
120
113
  /**
121
114
  * Extended fetch response returned by the generated HTTP client.
@@ -133,23 +126,12 @@ interface HttpResponse<D extends unknown, E extends unknown = unknown> extends R
133
126
  */
134
127
  type RuntimeRequestParams = Record<string, unknown>;
135
128
 
136
- /**
137
- * Toast message shown after an API action.
138
- *
139
- * `title` is optional and `message` contains the main notification text.
140
- */
141
- type ToastMessage = {
142
- title?: string;
143
- message: string;
144
- };
145
- /**
146
- * Optional toast messages for API success and error states.
147
- *
148
- * Use this to show consistent notifications after typed API requests.
149
- */
150
- type ToastOptions = {
151
- toastSuccess?: ToastMessage;
152
- toastError?: ToastMessage;
129
+ type ApiSuccessHandler<TResponse> = (response: TResponse) => void | Promise<void>;
130
+ type ApiErrorHandler<TError = unknown> = (error: TError) => void | Promise<void>;
131
+ type ApiMethodCallbacks<TResponse, TError = unknown> = {
132
+ onSuccess?: ApiSuccessHandler<TResponse>;
133
+ onError?: ApiErrorHandler<TError>;
134
+ params?: RequestParams;
153
135
  };
154
136
 
155
137
  type SortDirection = number | string;
@@ -163,32 +145,26 @@ type SortDirection = number | string;
163
145
  declare function buildQuery<TQuery, TSortModel = keyof TQuery>(filters: FilterFormValues<TQuery>[], page?: number, pageSize?: number, sortBy?: keyof TSortModel | null, sortDirection?: SortDirection): Partial<TQuery>;
164
146
 
165
147
  /**
166
- * Splits API wrapper arguments into actual API arguments, optional toast options,
167
- * and optional runtime request params.
168
- *
169
- * This allows generated wrapper methods to accept toast and request options
170
- * without changing the original API method arguments.
148
+ * Extracts wrapper-only arguments in this order:
149
+ * original API arguments, onSuccess, onError, request params.
171
150
  */
172
- declare function extractArgsToastsAndParams<TArgs extends unknown[]>(argsWithToast: [...TArgs, ToastOptions?, RuntimeRequestParams?]): {
151
+ declare function extractArgsCallbacksAndParams<TArgs extends readonly unknown[], TSuccess, TError = unknown>(argsWithCallbacks: [
152
+ ...TArgs,
153
+ ApiSuccessHandler<TSuccess>?,
154
+ ApiErrorHandler<TError>?,
155
+ RuntimeRequestParams?
156
+ ]): {
173
157
  args: TArgs;
174
- toastOptions?: ToastOptions;
158
+ onSuccess?: ApiSuccessHandler<TSuccess>;
159
+ onError?: ApiErrorHandler<TError>;
175
160
  params?: RuntimeRequestParams;
176
161
  };
177
162
 
178
- /**
179
- * Resets the unauthorized handling lock.
180
- *
181
- * This allows future `401 Unauthorized` responses to trigger the shared handling again.
182
- */
183
- declare function resetUnauthorizedHandling(): void;
184
- /**
185
- * Executes an API call and converts the response into an `ApiResult`.
186
- *
187
- * Successful responses return `{ ok: true }`.
188
- * Failed responses return `{ ok: false }` and show either a custom error toast
189
- * or the response error message.
190
- */
191
- declare function handleApiResponse<T>(call: () => Promise<HttpResponse<T, unknown>>, toastOptions?: ToastOptions): Promise<ApiResult<T>>;
163
+ type HandleApiResponseOptions<TResponse, TError = unknown> = {
164
+ onSuccess?: ApiSuccessHandler<TResponse>;
165
+ onError?: ApiErrorHandler<TError>;
166
+ };
167
+ declare function handleApiResponse<TResponse, TError = unknown>(call: () => Promise<HttpResponse<TResponse, TError>>, options?: HandleApiResponseOptions<TResponse, TError>): Promise<ApiResult<TResponse>>;
192
168
 
193
169
  /** Values supported by the generated API SortDirection enum. */
194
170
  type ApiSortDirection = "Default" | "Ascending" | "Descending";
@@ -219,4 +195,4 @@ declare function getSortDirectionFromSortType(sortType: SortType | null | undefi
219
195
  */
220
196
  declare function toFormData<T extends object>(payload: T): FormData;
221
197
 
222
- export { type ApiResult, type ApiSortDirection, type ExtractDataIfPaginated, type ExtractResponse, type FilterFormValues, type FilterType, type HttpResponse, type OptionValue, type RuntimeRequestParams, type SortDirection, type SortType, type SortableKeys, type ToastMessage, type ToastOptions, TypedApiToastProvider, type UnwrapArray, type WithoutRequestParams, buildQuery, extractArgsToastsAndParams, getSortDirectionFromSortType, getSortTypeFromSortDirection, handleApiResponse, resetUnauthorizedHandling, sortTypes, toFormData, toast };
198
+ export { type ApiErrorHandler, type ApiMethodCallbacks, type ApiResult, type ApiSortDirection, type ApiSuccessHandler, type ExtractDataIfPaginated, 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, getSortDirectionFromSortType, getSortTypeFromSortDirection, handleApiResponse, sortTypes, toFormData };
package/dist/index.d.ts CHANGED
@@ -1,47 +1,3 @@
1
- import * as react from 'react';
2
-
3
- type QueryParamsType = Record<string | number, any>;
4
- type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
5
- interface FullRequestParams extends Omit<RequestInit, "body"> {
6
- /** set parameter to `true` for call `securityWorker` for this request */
7
- secure?: boolean;
8
- /** request path */
9
- path: string;
10
- /** content type of request body */
11
- type?: ContentType;
12
- /** query params */
13
- query?: QueryParamsType;
14
- /** format of response (i.e. response.json() -> format: "json") */
15
- format?: ResponseFormat;
16
- /** request body */
17
- body?: unknown;
18
- /** base url */
19
- baseUrl?: string;
20
- /** request cancellation token */
21
- cancelToken?: CancelToken;
22
- }
23
- type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
24
- interface HttpResponse$1<D extends unknown, E extends unknown = unknown> extends Response {
25
- data: D;
26
- error: E;
27
- }
28
- type CancelToken = Symbol | string | number;
29
- declare enum ContentType {
30
- Json = "application/json",
31
- JsonApi = "application/vnd.api+json",
32
- FormData = "multipart/form-data",
33
- UrlEncoded = "application/x-www-form-urlencoded",
34
- Text = "text/plain"
35
- }
36
-
37
- type ExtractResponse<T> = T extends Promise<HttpResponse$1<infer R, any>> ? R : never;
38
- type UnwrapArray<T> = T extends (infer U)[] ? U : T;
39
- type ExtractDataIfPaginated<T> = T extends {
40
- data?: (infer U)[] | null;
41
- } ? U : T;
42
- type SortableKeys<T> = keyof UnwrapArray<ExtractDataIfPaginated<T>>;
43
- type WithoutRequestParams<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends RequestParams ? Rest : [First, ...WithoutRequestParams<Rest>] : [];
44
-
45
1
  /**
46
2
  * Standard result wrapper for API calls.
47
3
  *
@@ -97,25 +53,62 @@ interface FilterFormValues<TQuery> {
97
53
  */
98
54
  type SortType = "Default" | "Neutral" | "Ascending" | "Descending";
99
55
 
100
- /**
101
- * Toast helper for showing consistent success, error, warning, and info messages.
102
- *
103
- * Wraps `react-toastify` with a shared title/message layout.
104
- */
105
- declare const toast: {
106
- success(message: string, title?: string): void;
107
- error(message: string, title?: string): void;
108
- warning(message: string, title?: string): void;
109
- info(message: string, title?: string): void;
110
- errorResponse(error: Response | unknown): Promise<void>;
111
- };
56
+ type QueryParamsType = Record<string | number, any>;
57
+ type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
58
+ interface FullRequestParams extends Omit<RequestInit, "body"> {
59
+ /** set parameter to `true` for call `securityWorker` for this request */
60
+ secure?: boolean;
61
+ /** request path */
62
+ path: string;
63
+ /** content type of request body */
64
+ type?: ContentType;
65
+ /** query params */
66
+ query?: QueryParamsType;
67
+ /** format of response (i.e. response.json() -> format: "json") */
68
+ format?: ResponseFormat;
69
+ /** request body */
70
+ body?: unknown;
71
+ /** base url */
72
+ baseUrl?: string;
73
+ /** request cancellation token */
74
+ cancelToken?: CancelToken;
75
+ }
76
+ type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
77
+ interface HttpResponse$1<D extends unknown, E extends unknown = unknown> extends Response {
78
+ data: D;
79
+ error: E;
80
+ }
81
+ type CancelToken = Symbol | string | number;
82
+ declare enum ContentType {
83
+ Json = "application/json",
84
+ JsonApi = "application/vnd.api+json",
85
+ FormData = "multipart/form-data",
86
+ UrlEncoded = "application/x-www-form-urlencoded",
87
+ Text = "text/plain"
88
+ }
112
89
 
90
+ type ExtractResponse<T> = T extends Promise<HttpResponse$1<infer Response, unknown>> ? Response : never;
91
+ type UnwrapArray<T> = T extends readonly (infer Item)[] ? Item : T;
92
+ type ExtractDataIfPaginated<T> = T extends {
93
+ data?: readonly (infer Item)[] | null;
94
+ } ? Item : T;
95
+ type SortableKeys<T> = keyof UnwrapArray<ExtractDataIfPaginated<T>>;
113
96
  /**
114
- * Shared toast container for TypedApi UI notifications.
97
+ * Removes the optional RequestParams argument from the end
98
+ * of a generated API method parameter tuple.
99
+ *
100
+ * Example:
115
101
  *
116
- * Add this once near the root of the app to enable toast messages.
102
+ * [data: CreateSupplierParams, params?: RequestParams]
103
+ *
104
+ * becomes:
105
+ *
106
+ * [data: CreateSupplierParams]
117
107
  */
118
- declare function TypedApiToastProvider(): react.JSX.Element;
108
+ type WithoutRequestParams<T extends readonly unknown[]> = T extends readonly [
109
+ ...infer Arguments,
110
+ RequestParams?
111
+ ] ? Arguments : T;
119
112
 
120
113
  /**
121
114
  * Extended fetch response returned by the generated HTTP client.
@@ -133,23 +126,12 @@ interface HttpResponse<D extends unknown, E extends unknown = unknown> extends R
133
126
  */
134
127
  type RuntimeRequestParams = Record<string, unknown>;
135
128
 
136
- /**
137
- * Toast message shown after an API action.
138
- *
139
- * `title` is optional and `message` contains the main notification text.
140
- */
141
- type ToastMessage = {
142
- title?: string;
143
- message: string;
144
- };
145
- /**
146
- * Optional toast messages for API success and error states.
147
- *
148
- * Use this to show consistent notifications after typed API requests.
149
- */
150
- type ToastOptions = {
151
- toastSuccess?: ToastMessage;
152
- toastError?: ToastMessage;
129
+ type ApiSuccessHandler<TResponse> = (response: TResponse) => void | Promise<void>;
130
+ type ApiErrorHandler<TError = unknown> = (error: TError) => void | Promise<void>;
131
+ type ApiMethodCallbacks<TResponse, TError = unknown> = {
132
+ onSuccess?: ApiSuccessHandler<TResponse>;
133
+ onError?: ApiErrorHandler<TError>;
134
+ params?: RequestParams;
153
135
  };
154
136
 
155
137
  type SortDirection = number | string;
@@ -163,32 +145,26 @@ type SortDirection = number | string;
163
145
  declare function buildQuery<TQuery, TSortModel = keyof TQuery>(filters: FilterFormValues<TQuery>[], page?: number, pageSize?: number, sortBy?: keyof TSortModel | null, sortDirection?: SortDirection): Partial<TQuery>;
164
146
 
165
147
  /**
166
- * Splits API wrapper arguments into actual API arguments, optional toast options,
167
- * and optional runtime request params.
168
- *
169
- * This allows generated wrapper methods to accept toast and request options
170
- * without changing the original API method arguments.
148
+ * Extracts wrapper-only arguments in this order:
149
+ * original API arguments, onSuccess, onError, request params.
171
150
  */
172
- declare function extractArgsToastsAndParams<TArgs extends unknown[]>(argsWithToast: [...TArgs, ToastOptions?, RuntimeRequestParams?]): {
151
+ declare function extractArgsCallbacksAndParams<TArgs extends readonly unknown[], TSuccess, TError = unknown>(argsWithCallbacks: [
152
+ ...TArgs,
153
+ ApiSuccessHandler<TSuccess>?,
154
+ ApiErrorHandler<TError>?,
155
+ RuntimeRequestParams?
156
+ ]): {
173
157
  args: TArgs;
174
- toastOptions?: ToastOptions;
158
+ onSuccess?: ApiSuccessHandler<TSuccess>;
159
+ onError?: ApiErrorHandler<TError>;
175
160
  params?: RuntimeRequestParams;
176
161
  };
177
162
 
178
- /**
179
- * Resets the unauthorized handling lock.
180
- *
181
- * This allows future `401 Unauthorized` responses to trigger the shared handling again.
182
- */
183
- declare function resetUnauthorizedHandling(): void;
184
- /**
185
- * Executes an API call and converts the response into an `ApiResult`.
186
- *
187
- * Successful responses return `{ ok: true }`.
188
- * Failed responses return `{ ok: false }` and show either a custom error toast
189
- * or the response error message.
190
- */
191
- declare function handleApiResponse<T>(call: () => Promise<HttpResponse<T, unknown>>, toastOptions?: ToastOptions): Promise<ApiResult<T>>;
163
+ type HandleApiResponseOptions<TResponse, TError = unknown> = {
164
+ onSuccess?: ApiSuccessHandler<TResponse>;
165
+ onError?: ApiErrorHandler<TError>;
166
+ };
167
+ declare function handleApiResponse<TResponse, TError = unknown>(call: () => Promise<HttpResponse<TResponse, TError>>, options?: HandleApiResponseOptions<TResponse, TError>): Promise<ApiResult<TResponse>>;
192
168
 
193
169
  /** Values supported by the generated API SortDirection enum. */
194
170
  type ApiSortDirection = "Default" | "Ascending" | "Descending";
@@ -219,4 +195,4 @@ declare function getSortDirectionFromSortType(sortType: SortType | null | undefi
219
195
  */
220
196
  declare function toFormData<T extends object>(payload: T): FormData;
221
197
 
222
- export { type ApiResult, type ApiSortDirection, type ExtractDataIfPaginated, type ExtractResponse, type FilterFormValues, type FilterType, type HttpResponse, type OptionValue, type RuntimeRequestParams, type SortDirection, type SortType, type SortableKeys, type ToastMessage, type ToastOptions, TypedApiToastProvider, type UnwrapArray, type WithoutRequestParams, buildQuery, extractArgsToastsAndParams, getSortDirectionFromSortType, getSortTypeFromSortDirection, handleApiResponse, resetUnauthorizedHandling, sortTypes, toFormData, toast };
198
+ export { type ApiErrorHandler, type ApiMethodCallbacks, type ApiResult, type ApiSortDirection, type ApiSuccessHandler, type ExtractDataIfPaginated, 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, getSortDirectionFromSortType, getSortTypeFromSortDirection, handleApiResponse, sortTypes, toFormData };