@yusufstar07/sdk-client 1.0.0 → 1.1.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/README.md +1 -3
- package/dist/client.d.ts +4 -20
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +170 -183
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/types.d.ts +0 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +3 -21
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -64
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -78,7 +78,6 @@ import { NextResponse } from "next/server";
|
|
|
78
78
|
const sdk = createSDKClient({
|
|
79
79
|
apiKey: process.env.API_KEY!,
|
|
80
80
|
baseUrl: process.env.API_URL!,
|
|
81
|
-
fetch: fetch, // Use native fetch for SSR
|
|
82
81
|
});
|
|
83
82
|
|
|
84
83
|
export async function GET() {
|
|
@@ -217,7 +216,6 @@ interface SDKClientConfig {
|
|
|
217
216
|
baseUrl: string; // Required: API base URL
|
|
218
217
|
cookieDomain?: string; // Optional: Cookie domain
|
|
219
218
|
useCookies?: boolean; // Optional: Use cookies for token storage (default: true)
|
|
220
|
-
fetch?: typeof fetch; // Optional: Custom fetch implementation (required for SSR)
|
|
221
219
|
}
|
|
222
220
|
```
|
|
223
221
|
|
|
@@ -240,7 +238,7 @@ The SDK is fully compatible with Next.js App Router and Server Components:
|
|
|
240
238
|
|
|
241
239
|
1. **Server Components**: Use `getAccessTokenFromCookieString()` to get tokens from cookies
|
|
242
240
|
2. **API Routes**: Use `cookies()` from `next/headers` to get cookie string
|
|
243
|
-
3. **
|
|
241
|
+
3. **Axios**: SDK uses axios internally, which works seamlessly in both client and server environments
|
|
244
242
|
|
|
245
243
|
## TypeScript Support
|
|
246
244
|
|
package/dist/client.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import type { SDKClientConfig, RegisterRequest, RegisterResponse, LoginRequest, LoginResponse, RefreshResponse, LogoutResponse, MeResponse } from "./types";
|
|
2
2
|
export declare class SDKClient {
|
|
3
3
|
private config;
|
|
4
|
-
private
|
|
4
|
+
private axiosInstance;
|
|
5
5
|
private refreshPromise;
|
|
6
6
|
constructor(config: SDKClientConfig);
|
|
7
|
-
/**
|
|
8
|
-
* Make authenticated request with automatic token refresh and retry
|
|
9
|
-
*/
|
|
10
|
-
private request;
|
|
11
7
|
/**
|
|
12
8
|
* Internal refresh token method (prevents concurrent refresh requests)
|
|
13
9
|
*/
|
|
@@ -17,19 +13,15 @@ export declare class SDKClient {
|
|
|
17
13
|
*/
|
|
18
14
|
private performRefresh;
|
|
19
15
|
/**
|
|
20
|
-
* Get access token from cookie
|
|
16
|
+
* Get access token from cookie (for client-side only, axios handles cookies automatically)
|
|
21
17
|
*/
|
|
22
18
|
private getAccessToken;
|
|
23
19
|
/**
|
|
24
|
-
* Get refresh token from cookie
|
|
20
|
+
* Get refresh token from cookie (for client-side only, axios handles cookies automatically)
|
|
25
21
|
*/
|
|
26
22
|
private getRefreshToken;
|
|
27
23
|
/**
|
|
28
|
-
*
|
|
29
|
-
*/
|
|
30
|
-
private storeTokens;
|
|
31
|
-
/**
|
|
32
|
-
* Clear tokens from cookies
|
|
24
|
+
* Clear tokens - axios will handle cookie clearing via backend response
|
|
33
25
|
*/
|
|
34
26
|
private clearTokens;
|
|
35
27
|
/**
|
|
@@ -65,12 +57,4 @@ export declare class SDKClient {
|
|
|
65
57
|
* Create SDK client instance
|
|
66
58
|
*/
|
|
67
59
|
export declare function createSDKClient(config: SDKClientConfig): SDKClient;
|
|
68
|
-
/**
|
|
69
|
-
* Get access token from cookie string (for SSR)
|
|
70
|
-
*/
|
|
71
|
-
export declare function getAccessTokenFromCookieString(cookieString?: string): string | null;
|
|
72
|
-
/**
|
|
73
|
-
* Get refresh token from cookie string (for SSR)
|
|
74
|
-
*/
|
|
75
|
-
export declare function getRefreshTokenFromCookieString(cookieString?: string): string | null;
|
|
76
60
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACR,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,cAAc,EACd,UAAU,EAEb,MAAM,SAAS,CAAC;AAEjB,qBAAa,SAAS;IAClB,OAAO,CAAC,MAAM,CAKZ;IACF,OAAO,CAAC,aAAa,CAAgB;IACrC,OAAO,CAAC,cAAc,CAAyC;gBAEnD,MAAM,EAAE,eAAe;IA6FnC;;OAEG;YACW,oBAAoB;IAkBlC;;OAEG;YACW,cAAc;IAwC5B;;OAEG;IACH,OAAO,CAAC,cAAc;IAgBtB;;OAEG;IACH,OAAO,CAAC,eAAe;IAevB;;OAEG;IACH,OAAO,CAAC,WAAW;IASnB;;OAEG;IACG,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAchE;;OAEG;IACG,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAkBvD;;OAEG;IACG,OAAO,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;IAwC9D;;OAEG;IACG,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAwC3D;;OAEG;IACG,EAAE,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IA2BnD;;OAEG;IACH,QAAQ,IAAI,MAAM,GAAG,IAAI;IAIzB;;OAEG;IACH,eAAe,IAAI,OAAO;CAG7B;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,eAAe,GAAG,SAAS,CAElE"}
|
package/dist/client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import axios from "axios";
|
|
2
2
|
export class SDKClient {
|
|
3
3
|
constructor(config) {
|
|
4
4
|
this.refreshPromise = null; // Prevent concurrent refresh requests
|
|
@@ -8,67 +8,73 @@ export class SDKClient {
|
|
|
8
8
|
if (!config.baseUrl) {
|
|
9
9
|
throw new Error("Base URL is required");
|
|
10
10
|
}
|
|
11
|
-
const fetchImpl = config.fetch ?? (typeof fetch !== "undefined" ? fetch : undefined);
|
|
12
|
-
if (!fetchImpl) {
|
|
13
|
-
throw new Error("Fetch is not available. Provide a custom fetch implementation for SSR.");
|
|
14
|
-
}
|
|
15
11
|
this.config = {
|
|
16
12
|
apiKey: config.apiKey,
|
|
17
13
|
baseUrl: config.baseUrl.replace(/\/$/, ""), // Remove trailing slash
|
|
18
14
|
cookieDomain: config.cookieDomain,
|
|
19
15
|
useCookies: config.useCookies ?? true,
|
|
20
16
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"Content-Type": "application/json",
|
|
30
|
-
"x-api-key": this.config.apiKey,
|
|
31
|
-
...options.headers,
|
|
32
|
-
};
|
|
33
|
-
// Add access token if needed
|
|
34
|
-
if (useAuth) {
|
|
35
|
-
const accessToken = this.getAccessToken();
|
|
36
|
-
if (accessToken) {
|
|
37
|
-
headers["Authorization"] = `Bearer ${accessToken}`;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const response = await this.fetchImpl(url, {
|
|
41
|
-
...options,
|
|
42
|
-
headers,
|
|
43
|
-
credentials: "include", // Include cookies for SSR
|
|
17
|
+
// Create axios instance
|
|
18
|
+
this.axiosInstance = axios.create({
|
|
19
|
+
baseURL: this.config.baseUrl,
|
|
20
|
+
headers: {
|
|
21
|
+
"Content-Type": "application/json",
|
|
22
|
+
"x-api-key": this.config.apiKey,
|
|
23
|
+
},
|
|
24
|
+
withCredentials: true, // Include cookies automatically
|
|
44
25
|
});
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
26
|
+
// Add request interceptor for API key and auth token
|
|
27
|
+
this.axiosInstance.interceptors.request.use((config) => {
|
|
28
|
+
// Always add API key
|
|
29
|
+
if (config.headers) {
|
|
30
|
+
config.headers["x-api-key"] = this.config.apiKey;
|
|
31
|
+
// Add access token if available and not already set
|
|
32
|
+
if (!config.headers["Authorization"]) {
|
|
33
|
+
const accessToken = this.getAccessToken();
|
|
34
|
+
if (accessToken) {
|
|
35
|
+
config.headers["Authorization"] = `Bearer ${accessToken}`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
53
38
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
39
|
+
return config;
|
|
40
|
+
}, (error) => {
|
|
41
|
+
return Promise.reject(error);
|
|
42
|
+
});
|
|
43
|
+
// Add response interceptor for automatic token refresh
|
|
44
|
+
this.axiosInstance.interceptors.response.use((response) => response, async (error) => {
|
|
45
|
+
const originalRequest = error.config;
|
|
46
|
+
// Handle 401 Unauthorized - try to refresh token and retry
|
|
47
|
+
if (error.response?.status === 401 && originalRequest && !originalRequest._retry) {
|
|
48
|
+
originalRequest._retry = true;
|
|
49
|
+
try {
|
|
50
|
+
// Attempt to refresh token
|
|
51
|
+
await this.refreshTokenInternal();
|
|
52
|
+
// Retry the original request with new token
|
|
53
|
+
const accessToken = this.getAccessToken();
|
|
54
|
+
if (accessToken) {
|
|
55
|
+
originalRequest.headers["Authorization"] = `Bearer ${accessToken}`;
|
|
56
|
+
}
|
|
57
|
+
return this.axiosInstance(originalRequest);
|
|
58
|
+
}
|
|
59
|
+
catch (refreshError) {
|
|
60
|
+
// Refresh failed - clear tokens and throw original 401 error
|
|
61
|
+
this.clearTokens();
|
|
62
|
+
const errorData = error.response?.data;
|
|
63
|
+
const sdkError = {
|
|
64
|
+
error: errorData?.error || "Unauthorized",
|
|
65
|
+
status: 401,
|
|
66
|
+
};
|
|
67
|
+
return Promise.reject(sdkError);
|
|
68
|
+
}
|
|
62
69
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
error:
|
|
67
|
-
status: response
|
|
70
|
+
// Handle other errors
|
|
71
|
+
const errorData = error.response?.data;
|
|
72
|
+
const sdkError = {
|
|
73
|
+
error: errorData?.error || error.message || "Request failed",
|
|
74
|
+
status: error.response?.status,
|
|
68
75
|
};
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
return data;
|
|
76
|
+
return Promise.reject(sdkError);
|
|
77
|
+
});
|
|
72
78
|
}
|
|
73
79
|
/**
|
|
74
80
|
* Internal refresh token method (prevents concurrent refresh requests)
|
|
@@ -100,119 +106,109 @@ export class SDKClient {
|
|
|
100
106
|
status: 401,
|
|
101
107
|
};
|
|
102
108
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
// Refresh failed - clear tokens
|
|
109
|
+
try {
|
|
110
|
+
const response = await this.axiosInstance.post("/api/sdk/refresh", {}, {
|
|
111
|
+
headers: {
|
|
112
|
+
Authorization: `Bearer ${refreshToken}`,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
const refreshResponse = response.data;
|
|
116
|
+
// Axios automatically handles cookies from Set-Cookie headers
|
|
117
|
+
// No manual cookie setting needed
|
|
118
|
+
return refreshResponse;
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
// Refresh failed - clear tokens
|
|
116
122
|
this.clearTokens();
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
123
|
+
const errorData = error.response?.data || error;
|
|
124
|
+
const sdkError = {
|
|
125
|
+
error: errorData?.error || "Failed to refresh token",
|
|
126
|
+
status: error.response?.status || 401,
|
|
120
127
|
};
|
|
121
|
-
throw
|
|
122
|
-
}
|
|
123
|
-
const refreshResponse = data;
|
|
124
|
-
// Update access token in cookies
|
|
125
|
-
if (this.config.useCookies && refreshResponse.accessToken) {
|
|
126
|
-
setCookie("sdk_access_token", refreshResponse.accessToken, {
|
|
127
|
-
domain: this.config.cookieDomain,
|
|
128
|
-
maxAge: refreshResponse.expiresIn || 15 * 60, // Use expiresIn from response or default 15 minutes
|
|
129
|
-
path: "/",
|
|
130
|
-
secure: true,
|
|
131
|
-
sameSite: "lax",
|
|
132
|
-
});
|
|
128
|
+
throw sdkError;
|
|
133
129
|
}
|
|
134
|
-
return refreshResponse;
|
|
135
130
|
}
|
|
136
131
|
/**
|
|
137
|
-
* Get access token from cookie
|
|
132
|
+
* Get access token from cookie (for client-side only, axios handles cookies automatically)
|
|
138
133
|
*/
|
|
139
134
|
getAccessToken() {
|
|
135
|
+
// Axios automatically manages cookies with withCredentials: true
|
|
136
|
+
// We only need to read from cookies for manual token passing (SSR scenarios)
|
|
140
137
|
if (this.config.useCookies && typeof document !== "undefined") {
|
|
141
|
-
|
|
138
|
+
const cookies = document.cookie.split(";").reduce((acc, cookie) => {
|
|
139
|
+
const [key, value] = cookie.trim().split("=");
|
|
140
|
+
if (key && value) {
|
|
141
|
+
acc[key] = decodeURIComponent(value);
|
|
142
|
+
}
|
|
143
|
+
return acc;
|
|
144
|
+
}, {});
|
|
145
|
+
return cookies["sdk_access_token"] || null;
|
|
142
146
|
}
|
|
143
147
|
return null;
|
|
144
148
|
}
|
|
145
149
|
/**
|
|
146
|
-
* Get refresh token from cookie
|
|
150
|
+
* Get refresh token from cookie (for client-side only, axios handles cookies automatically)
|
|
147
151
|
*/
|
|
148
152
|
getRefreshToken() {
|
|
153
|
+
// Axios automatically manages cookies with withCredentials: true
|
|
149
154
|
if (this.config.useCookies && typeof document !== "undefined") {
|
|
150
|
-
|
|
155
|
+
const cookies = document.cookie.split(";").reduce((acc, cookie) => {
|
|
156
|
+
const [key, value] = cookie.trim().split("=");
|
|
157
|
+
if (key && value) {
|
|
158
|
+
acc[key] = decodeURIComponent(value);
|
|
159
|
+
}
|
|
160
|
+
return acc;
|
|
161
|
+
}, {});
|
|
162
|
+
return cookies["sdk_refresh_token"] || null;
|
|
151
163
|
}
|
|
152
164
|
return null;
|
|
153
165
|
}
|
|
154
166
|
/**
|
|
155
|
-
*
|
|
156
|
-
*/
|
|
157
|
-
storeTokens(accessToken, refreshToken) {
|
|
158
|
-
if (!this.config.useCookies || typeof document === "undefined") {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
// Access token expires in 15 minutes
|
|
162
|
-
setCookie("sdk_access_token", accessToken, {
|
|
163
|
-
domain: this.config.cookieDomain,
|
|
164
|
-
maxAge: 15 * 60, // 15 minutes
|
|
165
|
-
path: "/",
|
|
166
|
-
secure: true,
|
|
167
|
-
sameSite: "lax",
|
|
168
|
-
});
|
|
169
|
-
// Refresh token expires in 7 days
|
|
170
|
-
setCookie("sdk_refresh_token", refreshToken, {
|
|
171
|
-
domain: this.config.cookieDomain,
|
|
172
|
-
maxAge: 7 * 24 * 60 * 60, // 7 days
|
|
173
|
-
path: "/",
|
|
174
|
-
secure: true,
|
|
175
|
-
sameSite: "lax",
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Clear tokens from cookies
|
|
167
|
+
* Clear tokens - axios will handle cookie clearing via backend response
|
|
180
168
|
*/
|
|
181
169
|
clearTokens() {
|
|
182
|
-
|
|
183
|
-
|
|
170
|
+
// Axios automatically handles cookies, but we can clear them manually if needed
|
|
171
|
+
if (typeof document !== "undefined") {
|
|
172
|
+
// Clear cookies by setting them to expire
|
|
173
|
+
document.cookie = `sdk_access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; ${this.config.cookieDomain ? `domain=${this.config.cookieDomain};` : ""}`;
|
|
174
|
+
document.cookie = `sdk_refresh_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; ${this.config.cookieDomain ? `domain=${this.config.cookieDomain};` : ""}`;
|
|
184
175
|
}
|
|
185
|
-
deleteCookie("sdk_access_token", {
|
|
186
|
-
domain: this.config.cookieDomain,
|
|
187
|
-
path: "/",
|
|
188
|
-
});
|
|
189
|
-
deleteCookie("sdk_refresh_token", {
|
|
190
|
-
domain: this.config.cookieDomain,
|
|
191
|
-
path: "/",
|
|
192
|
-
});
|
|
193
176
|
}
|
|
194
177
|
/**
|
|
195
178
|
* Register a new user
|
|
196
179
|
*/
|
|
197
180
|
async register(data) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
181
|
+
try {
|
|
182
|
+
const response = await this.axiosInstance.post("/api/sdk/register", data);
|
|
183
|
+
return response.data;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
const errorData = error.response?.data || error;
|
|
187
|
+
const sdkError = {
|
|
188
|
+
error: errorData?.error || "Failed to register",
|
|
189
|
+
status: error.response?.status,
|
|
190
|
+
};
|
|
191
|
+
throw sdkError;
|
|
192
|
+
}
|
|
202
193
|
}
|
|
203
194
|
/**
|
|
204
195
|
* Login user
|
|
205
196
|
*/
|
|
206
197
|
async login(data) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
198
|
+
try {
|
|
199
|
+
const response = await this.axiosInstance.post("/api/sdk/login", data);
|
|
200
|
+
// Axios automatically handles cookies from Set-Cookie headers
|
|
201
|
+
// No manual cookie setting needed
|
|
202
|
+
return response.data;
|
|
203
|
+
}
|
|
204
|
+
catch (error) {
|
|
205
|
+
const errorData = error.response?.data || error;
|
|
206
|
+
const sdkError = {
|
|
207
|
+
error: errorData?.error || "Failed to login",
|
|
208
|
+
status: error.response?.status,
|
|
209
|
+
};
|
|
210
|
+
throw sdkError;
|
|
214
211
|
}
|
|
215
|
-
return response;
|
|
216
212
|
}
|
|
217
213
|
/**
|
|
218
214
|
* Refresh access token (manual refresh)
|
|
@@ -225,38 +221,27 @@ export class SDKClient {
|
|
|
225
221
|
status: 401,
|
|
226
222
|
};
|
|
227
223
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
224
|
+
try {
|
|
225
|
+
const response = await this.axiosInstance.post("/api/sdk/refresh", {}, {
|
|
226
|
+
headers: {
|
|
227
|
+
Authorization: `Bearer ${token}`,
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
const refreshResponse = response.data;
|
|
231
|
+
// Axios automatically handles cookies from Set-Cookie headers
|
|
232
|
+
// No manual cookie setting needed
|
|
233
|
+
return refreshResponse;
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
240
236
|
// Refresh failed - clear tokens
|
|
241
237
|
this.clearTokens();
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
238
|
+
const errorData = error.response?.data || error;
|
|
239
|
+
const sdkError = {
|
|
240
|
+
error: errorData?.error || "Failed to refresh token",
|
|
241
|
+
status: error.response?.status || 401,
|
|
245
242
|
};
|
|
246
|
-
throw
|
|
247
|
-
}
|
|
248
|
-
const refreshResponse = data;
|
|
249
|
-
// Update access token in cookies
|
|
250
|
-
if (this.config.useCookies && refreshResponse.accessToken) {
|
|
251
|
-
setCookie("sdk_access_token", refreshResponse.accessToken, {
|
|
252
|
-
domain: this.config.cookieDomain,
|
|
253
|
-
maxAge: refreshResponse.expiresIn || 15 * 60,
|
|
254
|
-
path: "/",
|
|
255
|
-
secure: true,
|
|
256
|
-
sameSite: "lax",
|
|
257
|
-
});
|
|
243
|
+
throw sdkError;
|
|
258
244
|
}
|
|
259
|
-
return refreshResponse;
|
|
260
245
|
}
|
|
261
246
|
/**
|
|
262
247
|
* Logout user
|
|
@@ -272,20 +257,24 @@ export class SDKClient {
|
|
|
272
257
|
};
|
|
273
258
|
}
|
|
274
259
|
try {
|
|
275
|
-
const response = await this.
|
|
276
|
-
method: "POST",
|
|
260
|
+
const response = await this.axiosInstance.post("/api/sdk/logout", {}, {
|
|
277
261
|
headers: {
|
|
278
262
|
Authorization: `Bearer ${token}`,
|
|
279
263
|
},
|
|
280
|
-
}
|
|
264
|
+
});
|
|
281
265
|
// Clear tokens from cookies
|
|
282
266
|
this.clearTokens();
|
|
283
|
-
return response;
|
|
267
|
+
return response.data;
|
|
284
268
|
}
|
|
285
269
|
catch (error) {
|
|
286
270
|
// Clear cookies even if request fails
|
|
287
271
|
this.clearTokens();
|
|
288
|
-
|
|
272
|
+
const errorData = error.response?.data || error;
|
|
273
|
+
const sdkError = {
|
|
274
|
+
error: errorData?.error || "Failed to logout",
|
|
275
|
+
status: error.response?.status,
|
|
276
|
+
};
|
|
277
|
+
throw sdkError;
|
|
289
278
|
}
|
|
290
279
|
}
|
|
291
280
|
/**
|
|
@@ -299,12 +288,22 @@ export class SDKClient {
|
|
|
299
288
|
status: 401,
|
|
300
289
|
};
|
|
301
290
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
291
|
+
try {
|
|
292
|
+
const response = await this.axiosInstance.get("/api/sdk/me", {
|
|
293
|
+
headers: {
|
|
294
|
+
Authorization: `Bearer ${token}`,
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
return response.data;
|
|
298
|
+
}
|
|
299
|
+
catch (error) {
|
|
300
|
+
const errorData = error.response?.data || error;
|
|
301
|
+
const sdkError = {
|
|
302
|
+
error: errorData?.error || "Failed to get user",
|
|
303
|
+
status: error.response?.status,
|
|
304
|
+
};
|
|
305
|
+
throw sdkError;
|
|
306
|
+
}
|
|
308
307
|
}
|
|
309
308
|
/**
|
|
310
309
|
* Get access token (for manual token management)
|
|
@@ -325,15 +324,3 @@ export class SDKClient {
|
|
|
325
324
|
export function createSDKClient(config) {
|
|
326
325
|
return new SDKClient(config);
|
|
327
326
|
}
|
|
328
|
-
/**
|
|
329
|
-
* Get access token from cookie string (for SSR)
|
|
330
|
-
*/
|
|
331
|
-
export function getAccessTokenFromCookieString(cookieString) {
|
|
332
|
-
return getAccessTokenFromCookies(cookieString);
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Get refresh token from cookie string (for SSR)
|
|
336
|
-
*/
|
|
337
|
-
export function getRefreshTokenFromCookieString(cookieString) {
|
|
338
|
-
return getRefreshTokenFromCookies(cookieString);
|
|
339
|
-
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Multi-Auth Platform SDK Client
|
|
3
3
|
*
|
|
4
|
-
* A
|
|
5
|
-
*
|
|
4
|
+
* A client-side authentication client for Next.js and React applications.
|
|
5
|
+
* Uses axios with automatic cookie management.
|
|
6
6
|
*/
|
|
7
|
-
export { SDKClient, createSDKClient
|
|
7
|
+
export { SDKClient, createSDKClient } from "./client";
|
|
8
8
|
export type { SDKUser, RegisterRequest, RegisterResponse, LoginRequest, LoginResponse, RefreshResponse, LogoutResponse, MeResponse, SDKError, SDKClientConfig, } from "./types";
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AACtD,YAAY,EACR,OAAO,EACP,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,cAAc,EACd,UAAU,EACV,QAAQ,EACR,eAAe,GAClB,MAAM,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Multi-Auth Platform SDK Client
|
|
3
3
|
*
|
|
4
|
-
* A
|
|
5
|
-
*
|
|
4
|
+
* A client-side authentication client for Next.js and React applications.
|
|
5
|
+
* Uses axios with automatic cookie management.
|
|
6
6
|
*/
|
|
7
|
-
export { SDKClient, createSDKClient
|
|
7
|
+
export { SDKClient, createSDKClient } from "./client";
|
package/dist/types.d.ts
CHANGED
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,OAAO;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,OAAO,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,gBAAgB;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,QAAQ;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,eAAe;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,OAAO;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,OAAO,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,gBAAgB;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,QAAQ;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,eAAe;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACxB"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,27 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Utility functions for SDK Client
|
|
3
|
+
*
|
|
4
|
+
* Note: Axios automatically handles cookies with withCredentials: true
|
|
5
|
+
* These utilities are only for SSR scenarios where we need to read cookies manually
|
|
3
6
|
*/
|
|
4
|
-
/**
|
|
5
|
-
* Get cookie value by name
|
|
6
|
-
*/
|
|
7
|
-
export declare function getCookie(name: string): string | null;
|
|
8
|
-
/**
|
|
9
|
-
* Set cookie
|
|
10
|
-
*/
|
|
11
|
-
export declare function setCookie(name: string, value: string, options?: {
|
|
12
|
-
domain?: string;
|
|
13
|
-
maxAge?: number;
|
|
14
|
-
path?: string;
|
|
15
|
-
secure?: boolean;
|
|
16
|
-
sameSite?: "strict" | "lax" | "none";
|
|
17
|
-
}): void;
|
|
18
|
-
/**
|
|
19
|
-
* Delete cookie
|
|
20
|
-
*/
|
|
21
|
-
export declare function deleteCookie(name: string, options?: {
|
|
22
|
-
domain?: string;
|
|
23
|
-
path?: string;
|
|
24
|
-
}): void;
|
|
25
7
|
/**
|
|
26
8
|
* Get cookies from cookie string (for SSR)
|
|
27
9
|
*/
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,wBAAgB,YAAY,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAY1E;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG9E;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG/E"}
|
package/dist/utils.js
CHANGED
|
@@ -1,70 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Utility functions for SDK Client
|
|
3
|
+
*
|
|
4
|
+
* Note: Axios automatically handles cookies with withCredentials: true
|
|
5
|
+
* These utilities are only for SSR scenarios where we need to read cookies manually
|
|
3
6
|
*/
|
|
4
|
-
/**
|
|
5
|
-
* Get cookie value by name
|
|
6
|
-
*/
|
|
7
|
-
export function getCookie(name) {
|
|
8
|
-
if (typeof document === "undefined") {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
const value = `; ${document.cookie}`;
|
|
12
|
-
const parts = value.split(`; ${name}=`);
|
|
13
|
-
if (parts.length === 2) {
|
|
14
|
-
return parts.pop()?.split(";").shift() || null;
|
|
15
|
-
}
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Set cookie
|
|
20
|
-
*/
|
|
21
|
-
export function setCookie(name, value, options = {}) {
|
|
22
|
-
if (typeof document === "undefined") {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
let cookieString = `${name}=${encodeURIComponent(value)}`;
|
|
26
|
-
if (options.maxAge) {
|
|
27
|
-
cookieString += `; max-age=${options.maxAge}`;
|
|
28
|
-
}
|
|
29
|
-
if (options.path) {
|
|
30
|
-
cookieString += `; path=${options.path}`;
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
cookieString += `; path=/`;
|
|
34
|
-
}
|
|
35
|
-
if (options.domain) {
|
|
36
|
-
cookieString += `; domain=${options.domain}`;
|
|
37
|
-
}
|
|
38
|
-
if (options.secure) {
|
|
39
|
-
cookieString += `; secure`;
|
|
40
|
-
}
|
|
41
|
-
if (options.sameSite) {
|
|
42
|
-
cookieString += `; samesite=${options.sameSite}`;
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
cookieString += `; samesite=lax`;
|
|
46
|
-
}
|
|
47
|
-
document.cookie = cookieString;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Delete cookie
|
|
51
|
-
*/
|
|
52
|
-
export function deleteCookie(name, options = {}) {
|
|
53
|
-
if (typeof document === "undefined") {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
let cookieString = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC`;
|
|
57
|
-
if (options.path) {
|
|
58
|
-
cookieString += `; path=${options.path}`;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
cookieString += `; path=/`;
|
|
62
|
-
}
|
|
63
|
-
if (options.domain) {
|
|
64
|
-
cookieString += `; domain=${options.domain}`;
|
|
65
|
-
}
|
|
66
|
-
document.cookie = cookieString;
|
|
67
|
-
}
|
|
68
7
|
/**
|
|
69
8
|
* Get cookies from cookie string (for SSR)
|
|
70
9
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yusufstar07/sdk-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "SDK client for Multi-Auth Platform authentication",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -23,16 +23,19 @@
|
|
|
23
23
|
"dist",
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"axios": "^1.13.2"
|
|
28
|
+
},
|
|
26
29
|
"devDependencies": {
|
|
27
|
-
"@types/node": "^
|
|
28
|
-
"typescript": "^5.
|
|
30
|
+
"@types/node": "^25.0.3",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
29
32
|
},
|
|
30
33
|
"peerDependencies": {
|
|
31
|
-
"react": "
|
|
34
|
+
"react": "19.2.3"
|
|
32
35
|
},
|
|
33
36
|
"peerDependenciesMeta": {
|
|
34
37
|
"react": {
|
|
35
38
|
"optional": true
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
|
-
}
|
|
41
|
+
}
|