@next-feature/client 0.1.2-5 → 0.1.3-1

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.
@@ -4699,7 +4699,7 @@ class de extends Error {
4699
4699
  return new Qe(null);
4700
4700
  }
4701
4701
  static of(e) {
4702
- return console.error("ApiError#of", e), pi(e);
4702
+ return pi(e);
4703
4703
  }
4704
4704
  }
4705
4705
  class Qe {
package/index.js CHANGED
@@ -1,46 +1,31 @@
1
- import { z as i, a, h as r } from "./error-CN4QrYXy.js";
2
- import { A as m, g as E, b as k, c as w, i as A } from "./error-CN4QrYXy.js";
1
+ import { z as i, a as o, h as n } from "./error-DRDfrCQN.js";
2
+ import { A as p, g as l, b as R, c as y, i as g } from "./error-DRDfrCQN.js";
3
3
  i.object({
4
4
  NODE_ENV: i.enum(["development", "production"]),
5
5
  CLIENT_API_URL: i.string().url()
6
6
  });
7
7
  process.env.NODE_ENV;
8
- const h = process.env.CLIENT_API_URL, c = "@next-feature/client", u = "0.0.1", p = {
9
- name: c,
10
- version: u
11
- }, { name: o, version: d } = p;
12
- class R {
8
+ const a = process.env.CLIENT_API_URL;
9
+ class c {
13
10
  constructor(e) {
14
11
  this.isRefreshing = !1, this.pendingRequests = [], this.config = {
15
- baseURL: h,
12
+ baseURL: a,
16
13
  timeout: 3e4,
17
14
  enableRefreshToken: !1,
18
15
  maxRetries: 1,
19
16
  retryDelay: 1e3,
20
17
  skipRefreshPaths: [],
21
18
  onUnauthorized: async () => {
22
- console.log(`[${o}] Unauthorized`);
23
19
  },
24
20
  onRefreshTokenExpired: async () => {
25
- console.log("Refresh token expired");
26
21
  },
27
22
  onAuthenticated: async (t) => {
28
- console.log(
29
- `[${o}]`,
30
- t.method?.toUpperCase(),
31
- t.url,
32
- t.data ?? ""
33
- );
34
23
  },
35
- onRefreshToken: async () => "",
24
+ onRefreshToken: async (t) => "",
36
25
  ...e
37
- }, this.instance = a.create({
26
+ }, this.instance = o.create({
38
27
  baseURL: this.config.baseURL,
39
- timeout: this.config.timeout,
40
- headers: {
41
- "Content-Type": "application/json",
42
- "User-Agent": `${o}:${d}`
43
- }
28
+ timeout: this.config.timeout
44
29
  }), this.setupInterceptors();
45
30
  }
46
31
  /**
@@ -88,24 +73,24 @@ class R {
88
73
  */
89
74
  async handleResponseRejected(e) {
90
75
  const t = e.config;
91
- return t ? e.response?.status === 401 && this.config.enableRefreshToken && !this.shouldSkipRefresh(t.url) ? this.handleUnauthorizedError(e, t) : this.shouldRetry(e, t) ? this.retryRequest(t) : Promise.reject(r(e)) : Promise.reject(r(e));
76
+ return t ? e.response?.status === 401 && this.config.enableRefreshToken && !this.shouldSkipRefresh(t.url) ? this.handleUnauthorizedError(e, t) : this.shouldRetry(e, t) ? this.retryRequest(t) : Promise.reject(n(e)) : Promise.reject(n(e));
92
77
  }
93
78
  /**
94
79
  * Handle 401 errors with token refresh
95
80
  */
96
81
  async handleUnauthorizedError(e, t) {
97
82
  if (t._retry)
98
- return this.config.onRefreshTokenExpired && await this.config.onRefreshTokenExpired(), Promise.reject(r(e));
83
+ return this.config.onRefreshTokenExpired && await this.config.onRefreshTokenExpired(), Promise.reject(n(e));
99
84
  if (t._retry = !0, this.isRefreshing)
100
- return new Promise((s, n) => {
101
- this.pendingRequests.push({ resolve: s, reject: n });
102
- }).then((s) => (t.headers.Authorization = `Bearer ${s}`, this.instance(t))).catch((s) => Promise.reject(r(s)));
85
+ return new Promise((s, r) => {
86
+ this.pendingRequests.push({ resolve: s, reject: r });
87
+ }).then((s) => (t.headers.Authorization = `Bearer ${s}`, this.instance(t))).catch((s) => Promise.reject(n(s)));
103
88
  this.isRefreshing = !0;
104
89
  try {
105
90
  const s = await this.refreshToken(t);
106
91
  return t.headers.Authorization = `Bearer ${s}`, this.processPendingRequests(null, s), this.instance(t);
107
92
  } catch (s) {
108
- return this.processPendingRequests(s, null), this.config.onUnauthorized && await this.config.onUnauthorized(), Promise.reject(r(e));
93
+ return this.processPendingRequests(s, null), this.config.onUnauthorized && await this.config.onUnauthorized(), Promise.reject(n(e));
109
94
  } finally {
110
95
  this.isRefreshing = !1;
111
96
  }
@@ -115,11 +100,7 @@ class R {
115
100
  */
116
101
  async refreshToken(e) {
117
102
  try {
118
- const t = await this.config.onRefreshToken();
119
- return await this.post("/api/auth/refresh", {
120
- refreshToken: t,
121
- headers: e.headers
122
- });
103
+ return this.config.onRefreshToken(e);
123
104
  } catch (t) {
124
105
  throw console.error("Token refresh failed:", t), t;
125
106
  }
@@ -140,8 +121,8 @@ class R {
140
121
  return !1;
141
122
  if (!e.response)
142
123
  return !0;
143
- const n = e.response.status;
144
- return n >= 500 && n !== 501 || n === 429;
124
+ const r = e.response.status;
125
+ return r >= 500 && r !== 501 || r === 429;
145
126
  }
146
127
  /**
147
128
  * Retry failed request with exponential backoff
@@ -181,15 +162,15 @@ class R {
181
162
  /**
182
163
  * Get the underlying Axios instance for advanced usage
183
164
  */
184
- getAxiosInstance() {
165
+ get axios() {
185
166
  return this.instance;
186
167
  }
187
168
  }
188
169
  export {
189
- R as ApiClient,
190
- m as ApiError,
191
- E as getErrorMessage,
192
- k as handleAxiosError,
193
- w as handleZodError,
194
- A as isHttpStatus
170
+ c as ApiClient,
171
+ p as ApiError,
172
+ l as getErrorMessage,
173
+ R as handleAxiosError,
174
+ y as handleZodError,
175
+ g as isHttpStatus
195
176
  };
@@ -1,8 +1,8 @@
1
1
  import { ApiResponse } from '../types';
2
- type WrapperFn<T, F extends (...args: unknown[]) => Promise<T> | T> = (...args: Parameters<F>) => Promise<ApiResponse<T>>;
3
2
  interface WithApiOptions<T> {
4
- fallbackData?: T | null;
3
+ fallbackData?: T;
5
4
  successMessage?: string;
6
5
  }
7
- export declare const withApi: <T, F extends (...args: any[]) => Promise<T>>(fn: F, options?: WithApiOptions<T>) => WrapperFn<T, F>;
6
+ export declare function withApi<F extends (...args: any[]) => Promise<any>>(fn: F, options?: WithApiOptions<Awaited<ReturnType<F>>>): (...args: Parameters<F>) => Promise<ApiResponse<Awaited<ReturnType<F>>>>;
7
+ export declare function withForm<State>(fn: (formData: FormData) => Promise<ApiResponse<State>>): (prevState: Awaited<ApiResponse<State>>, formData: FormData) => Promise<ApiResponse<State>>;
8
8
  export {};
package/lib/client.d.ts CHANGED
@@ -68,5 +68,5 @@ export declare class ApiClient {
68
68
  /**
69
69
  * Get the underlying Axios instance for advanced usage
70
70
  */
71
- getAxiosInstance(): AxiosInstance;
71
+ get axios(): AxiosInstance;
72
72
  }
@@ -7,7 +7,7 @@ import { InternalAxiosRequestConfig } from 'axios';
7
7
  * January 11th 2026, 9:00:22 pm
8
8
  */
9
9
  export interface ApiClientConfig {
10
- baseURL: string;
10
+ baseURL?: string;
11
11
  timeout?: number;
12
12
  enableRefreshToken?: boolean;
13
13
  maxRetries?: number;
@@ -20,5 +20,5 @@ export interface ApiClientConfig {
20
20
  onAuthenticated?: (config: InternalAxiosRequestConfig) => void | Promise<void>;
21
21
  onUnauthorized?: () => void | Promise<void>;
22
22
  onRefreshTokenExpired?: () => void | Promise<void>;
23
- onRefreshToken?: () => string | Promise<string>;
23
+ onRefreshToken?: (originalRequest: InternalAxiosRequestConfig) => string | Promise<string>;
24
24
  }
@@ -1,4 +1,5 @@
1
1
  import { HttpStatusCode } from 'axios';
2
+ import { ApiError } from '../error';
2
3
  /**
3
4
  * [api-response]
4
5
  * next-feature@0.0.11-beta
@@ -7,7 +8,7 @@ import { HttpStatusCode } from 'axios';
7
8
  export interface ApiResponse<Response> {
8
9
  success?: boolean;
9
10
  message?: string;
10
- error?: ProblemDetail;
11
+ error?: ApiError;
11
12
  data: Response;
12
13
  }
13
14
  /**
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "@next-feature/client",
3
- "version": "0.1.2-5",
4
- "type": "module",
3
+ "version": "0.1.3-1",
5
4
  "main": "./index.js",
6
5
  "types": "./index.d.ts",
7
6
  "exports": {
@@ -16,5 +15,6 @@
16
15
  "default": "./server.js"
17
16
  },
18
17
  "./package.json": "./package.json"
19
- }
18
+ },
19
+ "type": "module"
20
20
  }
package/server.js CHANGED
@@ -1,22 +1,36 @@
1
- import { A as t } from "./error-CN4QrYXy.js";
2
- const l = (r, s) => (s.fallbackData ??= null, s.successMessage ??= "success", async (...c) => {
3
- try {
4
- const e = await r(c);
5
- return {
6
- success: !0,
7
- message: s.successMessage,
8
- data: e
9
- };
10
- } catch (e) {
11
- const a = t.of(e);
12
- return {
13
- data: s.fallbackData,
14
- error: a.body,
15
- message: a.message,
16
- success: !1
17
- };
18
- }
19
- });
1
+ import { A as o } from "./error-DRDfrCQN.js";
2
+ function u(e, r) {
3
+ const a = {
4
+ fallbackData: {},
5
+ successMessage: "success",
6
+ ...r
7
+ };
8
+ return async (...s) => {
9
+ try {
10
+ const t = await e(...s);
11
+ return {
12
+ success: !0,
13
+ message: a.successMessage,
14
+ data: t
15
+ };
16
+ } catch (t) {
17
+ const c = o.of(t);
18
+ return {
19
+ data: a.fallbackData,
20
+ error: c,
21
+ message: c.message,
22
+ success: !1
23
+ };
24
+ }
25
+ };
26
+ }
27
+ function i(e) {
28
+ return async (r, a) => {
29
+ const s = await e(a);
30
+ return s.error || !s.success ? { ...s, data: r.data } : s;
31
+ };
32
+ }
20
33
  export {
21
- l as withApi
34
+ u as withApi,
35
+ i as withForm
22
36
  };