typedapi-client-helpers 0.1.2 → 0.1.4

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,50 +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 [
44
- infer First,
45
- ...infer Rest
46
- ] ? First extends RequestParams ? Rest : [First, ...WithoutRequestParams<Rest>] : [];
47
-
48
1
  /**
49
2
  * Standard result wrapper for API calls.
50
3
  *
@@ -100,25 +53,62 @@ interface FilterFormValues<TQuery> {
100
53
  */
101
54
  type SortType = "Default" | "Neutral" | "Ascending" | "Descending";
102
55
 
103
- /**
104
- * Toast helper for showing consistent success, error, warning, and info messages.
105
- *
106
- * Wraps `react-toastify` with a shared title/message layout.
107
- */
108
- declare const toast: {
109
- success(message: string, title?: string): void;
110
- error(message: string, title?: string): void;
111
- warning(message: string, title?: string): void;
112
- info(message: string, title?: string): void;
113
- errorResponse(error: Response | unknown): Promise<void>;
114
- };
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
+ }
115
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>>;
116
96
  /**
117
- * 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:
118
101
  *
119
- * 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]
120
107
  */
121
- declare function TypedApiToastProvider(): react.JSX.Element;
108
+ type WithoutRequestParams<T extends readonly unknown[]> = T extends readonly [
109
+ ...infer Arguments,
110
+ RequestParams?
111
+ ] ? Arguments : T;
122
112
 
123
113
  /**
124
114
  * Extended fetch response returned by the generated HTTP client.
@@ -136,23 +126,12 @@ interface HttpResponse<D extends unknown, E extends unknown = unknown> extends R
136
126
  */
137
127
  type RuntimeRequestParams = Record<string, unknown>;
138
128
 
139
- /**
140
- * Toast message shown after an API action.
141
- *
142
- * `title` is optional and `message` contains the main notification text.
143
- */
144
- type ToastMessage = {
145
- title?: string;
146
- message: string;
147
- };
148
- /**
149
- * Optional toast messages for API success and error states.
150
- *
151
- * Use this to show consistent notifications after typed API requests.
152
- */
153
- type ToastOptions = {
154
- toastSuccess?: ToastMessage;
155
- 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;
156
135
  };
157
136
 
158
137
  type SortDirection = number | string;
@@ -166,32 +145,26 @@ type SortDirection = number | string;
166
145
  declare function buildQuery<TQuery, TSortModel = keyof TQuery>(filters: FilterFormValues<TQuery>[], page?: number, pageSize?: number, sortBy?: keyof TSortModel | null, sortDirection?: SortDirection): Partial<TQuery>;
167
146
 
168
147
  /**
169
- * Splits API wrapper arguments into actual API arguments, optional toast options,
170
- * and optional runtime request params.
171
- *
172
- * This allows generated wrapper methods to accept toast and request options
173
- * without changing the original API method arguments.
148
+ * Extracts wrapper-only arguments in this order:
149
+ * original API arguments, onSuccess, onError, request params.
174
150
  */
175
- 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
+ ]): {
176
157
  args: TArgs;
177
- toastOptions?: ToastOptions;
158
+ onSuccess?: ApiSuccessHandler<TSuccess>;
159
+ onError?: ApiErrorHandler<TError>;
178
160
  params?: RuntimeRequestParams;
179
161
  };
180
162
 
181
- /**
182
- * Resets the unauthorized handling lock.
183
- *
184
- * This allows future `401 Unauthorized` responses to trigger the shared handling again.
185
- */
186
- declare function resetUnauthorizedHandling(): void;
187
- /**
188
- * Executes an API call and converts the response into an `ApiResult`.
189
- *
190
- * Successful responses return `{ ok: true }`.
191
- * Failed responses return `{ ok: false }` and show either a custom error toast
192
- * or the response error message.
193
- */
194
- 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>>;
195
168
 
196
169
  /** Values supported by the generated API SortDirection enum. */
197
170
  type ApiSortDirection = "Default" | "Ascending" | "Descending";
@@ -222,4 +195,4 @@ declare function getSortDirectionFromSortType(sortType: SortType | null | undefi
222
195
  */
223
196
  declare function toFormData<T extends object>(payload: T): FormData;
224
197
 
225
- 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,50 +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 [
44
- infer First,
45
- ...infer Rest
46
- ] ? First extends RequestParams ? Rest : [First, ...WithoutRequestParams<Rest>] : [];
47
-
48
1
  /**
49
2
  * Standard result wrapper for API calls.
50
3
  *
@@ -100,25 +53,62 @@ interface FilterFormValues<TQuery> {
100
53
  */
101
54
  type SortType = "Default" | "Neutral" | "Ascending" | "Descending";
102
55
 
103
- /**
104
- * Toast helper for showing consistent success, error, warning, and info messages.
105
- *
106
- * Wraps `react-toastify` with a shared title/message layout.
107
- */
108
- declare const toast: {
109
- success(message: string, title?: string): void;
110
- error(message: string, title?: string): void;
111
- warning(message: string, title?: string): void;
112
- info(message: string, title?: string): void;
113
- errorResponse(error: Response | unknown): Promise<void>;
114
- };
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
+ }
115
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>>;
116
96
  /**
117
- * 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:
118
101
  *
119
- * 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]
120
107
  */
121
- declare function TypedApiToastProvider(): react.JSX.Element;
108
+ type WithoutRequestParams<T extends readonly unknown[]> = T extends readonly [
109
+ ...infer Arguments,
110
+ RequestParams?
111
+ ] ? Arguments : T;
122
112
 
123
113
  /**
124
114
  * Extended fetch response returned by the generated HTTP client.
@@ -136,23 +126,12 @@ interface HttpResponse<D extends unknown, E extends unknown = unknown> extends R
136
126
  */
137
127
  type RuntimeRequestParams = Record<string, unknown>;
138
128
 
139
- /**
140
- * Toast message shown after an API action.
141
- *
142
- * `title` is optional and `message` contains the main notification text.
143
- */
144
- type ToastMessage = {
145
- title?: string;
146
- message: string;
147
- };
148
- /**
149
- * Optional toast messages for API success and error states.
150
- *
151
- * Use this to show consistent notifications after typed API requests.
152
- */
153
- type ToastOptions = {
154
- toastSuccess?: ToastMessage;
155
- 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;
156
135
  };
157
136
 
158
137
  type SortDirection = number | string;
@@ -166,32 +145,26 @@ type SortDirection = number | string;
166
145
  declare function buildQuery<TQuery, TSortModel = keyof TQuery>(filters: FilterFormValues<TQuery>[], page?: number, pageSize?: number, sortBy?: keyof TSortModel | null, sortDirection?: SortDirection): Partial<TQuery>;
167
146
 
168
147
  /**
169
- * Splits API wrapper arguments into actual API arguments, optional toast options,
170
- * and optional runtime request params.
171
- *
172
- * This allows generated wrapper methods to accept toast and request options
173
- * without changing the original API method arguments.
148
+ * Extracts wrapper-only arguments in this order:
149
+ * original API arguments, onSuccess, onError, request params.
174
150
  */
175
- 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
+ ]): {
176
157
  args: TArgs;
177
- toastOptions?: ToastOptions;
158
+ onSuccess?: ApiSuccessHandler<TSuccess>;
159
+ onError?: ApiErrorHandler<TError>;
178
160
  params?: RuntimeRequestParams;
179
161
  };
180
162
 
181
- /**
182
- * Resets the unauthorized handling lock.
183
- *
184
- * This allows future `401 Unauthorized` responses to trigger the shared handling again.
185
- */
186
- declare function resetUnauthorizedHandling(): void;
187
- /**
188
- * Executes an API call and converts the response into an `ApiResult`.
189
- *
190
- * Successful responses return `{ ok: true }`.
191
- * Failed responses return `{ ok: false }` and show either a custom error toast
192
- * or the response error message.
193
- */
194
- 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>>;
195
168
 
196
169
  /** Values supported by the generated API SortDirection enum. */
197
170
  type ApiSortDirection = "Default" | "Ascending" | "Descending";
@@ -222,4 +195,4 @@ declare function getSortDirectionFromSortType(sortType: SortType | null | undefi
222
195
  */
223
196
  declare function toFormData<T extends object>(payload: T): FormData;
224
197
 
225
- 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 };