@next-feature/client 0.1.2-5 → 0.1.3-0
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/{error-CN4QrYXy.js → error-DRDfrCQN.js} +1 -1
- package/index.js +23 -42
- package/lib/actions/with-api.d.ts +2 -3
- package/lib/types/client.d.ts +2 -2
- package/package.json +3 -3
- package/server.js +27 -20
package/index.js
CHANGED
|
@@ -1,46 +1,31 @@
|
|
|
1
|
-
import { z as i, a, h as
|
|
2
|
-
import { A as
|
|
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
|
|
9
|
-
|
|
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:
|
|
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 =
|
|
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(
|
|
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(
|
|
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,
|
|
101
|
-
this.pendingRequests.push({ resolve: s, reject:
|
|
102
|
-
}).then((s) => (t.headers.Authorization = `Bearer ${s}`, this.instance(t))).catch((s) => Promise.reject(
|
|
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(
|
|
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
|
-
|
|
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
|
|
144
|
-
return
|
|
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
|
|
@@ -186,10 +167,10 @@ class R {
|
|
|
186
167
|
}
|
|
187
168
|
}
|
|
188
169
|
export {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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,7 @@
|
|
|
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
|
|
3
|
+
fallbackData?: T;
|
|
5
4
|
successMessage?: string;
|
|
6
5
|
}
|
|
7
|
-
export declare
|
|
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>>>>;
|
|
8
7
|
export {};
|
package/lib/types/client.d.ts
CHANGED
|
@@ -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
|
|
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
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next-feature/client",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"type": "module",
|
|
3
|
+
"version": "0.1.3-0",
|
|
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,29 @@
|
|
|
1
|
-
import { A as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import { A as o } from "./error-DRDfrCQN.js";
|
|
2
|
+
function u(r, c) {
|
|
3
|
+
const e = {
|
|
4
|
+
fallbackData: {},
|
|
5
|
+
successMessage: "success",
|
|
6
|
+
...c
|
|
7
|
+
};
|
|
8
|
+
return async (...t) => {
|
|
9
|
+
try {
|
|
10
|
+
const s = await r(...t);
|
|
11
|
+
return {
|
|
12
|
+
success: !0,
|
|
13
|
+
message: e.successMessage,
|
|
14
|
+
data: s
|
|
15
|
+
};
|
|
16
|
+
} catch (s) {
|
|
17
|
+
const a = o.of(s);
|
|
18
|
+
return {
|
|
19
|
+
data: e.fallbackData,
|
|
20
|
+
error: a.body,
|
|
21
|
+
message: a.message,
|
|
22
|
+
success: !1
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
20
27
|
export {
|
|
21
|
-
|
|
28
|
+
u as withApi
|
|
22
29
|
};
|