@insforge/sdk 1.3.0-ssr.0 → 1.3.0-ssr.2
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 +14 -2
- package/dist/ssr.d.mts +20 -14
- package/dist/ssr.d.ts +20 -14
- package/dist/ssr.js +134 -58
- package/dist/ssr.js.map +1 -1
- package/dist/ssr.mjs +134 -58
- package/dist/ssr.mjs.map +1 -1
- package/package.json +1 -1
package/dist/ssr.mjs
CHANGED
|
@@ -2621,26 +2621,6 @@ function isJwtExpiredOrExpiring(token, leewaySeconds = 60) {
|
|
|
2621
2621
|
return expires.getTime() <= Date.now() + leewaySeconds * 1e3;
|
|
2622
2622
|
}
|
|
2623
2623
|
|
|
2624
|
-
// src/ssr/config.ts
|
|
2625
|
-
function env(name) {
|
|
2626
|
-
if (typeof process === "undefined") return void 0;
|
|
2627
|
-
return process.env[name];
|
|
2628
|
-
}
|
|
2629
|
-
function resolveBrowserConfig(config = {}) {
|
|
2630
|
-
return {
|
|
2631
|
-
...config,
|
|
2632
|
-
baseUrl: config.baseUrl ?? env("NEXT_PUBLIC_INSFORGE_URL"),
|
|
2633
|
-
anonKey: config.anonKey ?? env("NEXT_PUBLIC_INSFORGE_ANON_KEY")
|
|
2634
|
-
};
|
|
2635
|
-
}
|
|
2636
|
-
function resolveServerConfig(config = {}) {
|
|
2637
|
-
return {
|
|
2638
|
-
...config,
|
|
2639
|
-
baseUrl: config.baseUrl ?? env("INSFORGE_URL") ?? env("NEXT_PUBLIC_INSFORGE_URL"),
|
|
2640
|
-
anonKey: config.anonKey ?? env("INSFORGE_ANON_KEY") ?? env("NEXT_PUBLIC_INSFORGE_ANON_KEY")
|
|
2641
|
-
};
|
|
2642
|
-
}
|
|
2643
|
-
|
|
2644
2624
|
// src/ssr/browser-client.ts
|
|
2645
2625
|
import { ERROR_CODES as ERROR_CODES2 } from "@insforge/shared-schemas";
|
|
2646
2626
|
|
|
@@ -2717,11 +2697,11 @@ function setCookie(cookies, name, value, options) {
|
|
|
2717
2697
|
}
|
|
2718
2698
|
function deleteCookie(cookies, name, options) {
|
|
2719
2699
|
if (!cookies) return;
|
|
2720
|
-
if (cookies.
|
|
2721
|
-
cookies.
|
|
2700
|
+
if (cookies.set) {
|
|
2701
|
+
cookies.set(name, "", expiredCookieOptions(options));
|
|
2722
2702
|
return;
|
|
2723
2703
|
}
|
|
2724
|
-
cookies.
|
|
2704
|
+
cookies.delete?.(name);
|
|
2725
2705
|
}
|
|
2726
2706
|
function serializeCookie(name, value, options = {}) {
|
|
2727
2707
|
const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
|
|
@@ -2740,32 +2720,17 @@ function serializeCookie(name, value, options = {}) {
|
|
|
2740
2720
|
function appendSetCookie(headers, name, value, options) {
|
|
2741
2721
|
headers.append("Set-Cookie", serializeCookie(name, value, options));
|
|
2742
2722
|
}
|
|
2743
|
-
function setAuthCookies(
|
|
2723
|
+
function setAuthCookies(cookies, tokens, settings = {}) {
|
|
2744
2724
|
const accessName = getAccessTokenCookieName(settings.names);
|
|
2745
2725
|
const refreshName = getRefreshTokenCookieName(settings.names);
|
|
2746
2726
|
const accessOptions = accessTokenCookieOptions(
|
|
2747
2727
|
tokens.accessToken,
|
|
2748
2728
|
settings.options?.accessToken
|
|
2749
2729
|
);
|
|
2750
|
-
|
|
2751
|
-
appendSetCookie(target, accessName, tokens.accessToken, accessOptions);
|
|
2752
|
-
if (tokens.refreshToken) {
|
|
2753
|
-
appendSetCookie(
|
|
2754
|
-
target,
|
|
2755
|
-
refreshName,
|
|
2756
|
-
tokens.refreshToken,
|
|
2757
|
-
refreshTokenCookieOptions(
|
|
2758
|
-
tokens.refreshToken,
|
|
2759
|
-
settings.options?.refreshToken
|
|
2760
|
-
)
|
|
2761
|
-
);
|
|
2762
|
-
}
|
|
2763
|
-
return;
|
|
2764
|
-
}
|
|
2765
|
-
setCookie(target, accessName, tokens.accessToken, accessOptions);
|
|
2730
|
+
setCookie(cookies, accessName, tokens.accessToken, accessOptions);
|
|
2766
2731
|
if (tokens.refreshToken) {
|
|
2767
2732
|
setCookie(
|
|
2768
|
-
|
|
2733
|
+
cookies,
|
|
2769
2734
|
refreshName,
|
|
2770
2735
|
tokens.refreshToken,
|
|
2771
2736
|
refreshTokenCookieOptions(
|
|
@@ -2775,18 +2740,48 @@ function setAuthCookies(target, tokens, settings = {}) {
|
|
|
2775
2740
|
);
|
|
2776
2741
|
}
|
|
2777
2742
|
}
|
|
2778
|
-
function clearAuthCookies(
|
|
2743
|
+
function clearAuthCookies(cookies, settings = {}) {
|
|
2779
2744
|
const accessName = getAccessTokenCookieName(settings.names);
|
|
2780
2745
|
const refreshName = getRefreshTokenCookieName(settings.names);
|
|
2781
2746
|
const accessOptions = expiredCookieOptions(settings.options?.accessToken);
|
|
2782
2747
|
const refreshOptions = expiredCookieOptions(settings.options?.refreshToken);
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2748
|
+
deleteCookie(cookies, accessName, accessOptions);
|
|
2749
|
+
deleteCookie(cookies, refreshName, refreshOptions);
|
|
2750
|
+
}
|
|
2751
|
+
function setAuthCookieHeaders(headers, tokens, settings = {}) {
|
|
2752
|
+
const accessName = getAccessTokenCookieName(settings.names);
|
|
2753
|
+
const refreshName = getRefreshTokenCookieName(settings.names);
|
|
2754
|
+
appendSetCookie(
|
|
2755
|
+
headers,
|
|
2756
|
+
accessName,
|
|
2757
|
+
tokens.accessToken,
|
|
2758
|
+
accessTokenCookieOptions(tokens.accessToken, settings.options?.accessToken)
|
|
2759
|
+
);
|
|
2760
|
+
if (tokens.refreshToken) {
|
|
2761
|
+
appendSetCookie(
|
|
2762
|
+
headers,
|
|
2763
|
+
refreshName,
|
|
2764
|
+
tokens.refreshToken,
|
|
2765
|
+
refreshTokenCookieOptions(
|
|
2766
|
+
tokens.refreshToken,
|
|
2767
|
+
settings.options?.refreshToken
|
|
2768
|
+
)
|
|
2769
|
+
);
|
|
2787
2770
|
}
|
|
2788
|
-
|
|
2789
|
-
|
|
2771
|
+
}
|
|
2772
|
+
function clearAuthCookieHeaders(headers, settings = {}) {
|
|
2773
|
+
appendSetCookie(
|
|
2774
|
+
headers,
|
|
2775
|
+
getAccessTokenCookieName(settings.names),
|
|
2776
|
+
"",
|
|
2777
|
+
expiredCookieOptions(settings.options?.accessToken)
|
|
2778
|
+
);
|
|
2779
|
+
appendSetCookie(
|
|
2780
|
+
headers,
|
|
2781
|
+
getRefreshTokenCookieName(settings.names),
|
|
2782
|
+
"",
|
|
2783
|
+
expiredCookieOptions(settings.options?.refreshToken)
|
|
2784
|
+
);
|
|
2790
2785
|
}
|
|
2791
2786
|
|
|
2792
2787
|
// src/ssr/browser-client.ts
|
|
@@ -2835,6 +2830,17 @@ function withAuthHeader(init, token) {
|
|
|
2835
2830
|
};
|
|
2836
2831
|
}
|
|
2837
2832
|
function createBrowserClient(options = {}) {
|
|
2833
|
+
let { baseUrl, anonKey } = options;
|
|
2834
|
+
try {
|
|
2835
|
+
baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
|
|
2836
|
+
anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
|
|
2837
|
+
} catch {
|
|
2838
|
+
}
|
|
2839
|
+
if (!baseUrl || !anonKey) {
|
|
2840
|
+
throw new Error(
|
|
2841
|
+
"Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to createBrowserClient() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
|
|
2842
|
+
);
|
|
2843
|
+
}
|
|
2838
2844
|
let accessToken = getBrowserCookie(
|
|
2839
2845
|
getAccessTokenCookieName(options.names)
|
|
2840
2846
|
);
|
|
@@ -2911,7 +2917,9 @@ function createBrowserClient(options = {}) {
|
|
|
2911
2917
|
return fetchImpl(input, withAuthHeader(init, refreshed.accessToken));
|
|
2912
2918
|
};
|
|
2913
2919
|
client = new InsForgeClient({
|
|
2914
|
-
...
|
|
2920
|
+
...options,
|
|
2921
|
+
baseUrl,
|
|
2922
|
+
anonKey,
|
|
2915
2923
|
fetch: ssrFetch
|
|
2916
2924
|
});
|
|
2917
2925
|
const setAccessToken = client.setAccessToken.bind(client);
|
|
@@ -2930,12 +2938,25 @@ function createBrowserClient(options = {}) {
|
|
|
2930
2938
|
|
|
2931
2939
|
// src/ssr/server-client.ts
|
|
2932
2940
|
function createServerClient(options = {}) {
|
|
2941
|
+
let { baseUrl, anonKey } = options;
|
|
2942
|
+
try {
|
|
2943
|
+
baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
|
|
2944
|
+
anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
|
|
2945
|
+
} catch {
|
|
2946
|
+
}
|
|
2947
|
+
if (!baseUrl || !anonKey) {
|
|
2948
|
+
throw new Error(
|
|
2949
|
+
"Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to createServerClient() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
|
|
2950
|
+
);
|
|
2951
|
+
}
|
|
2933
2952
|
const accessToken = options.accessToken ?? getCookieValue(
|
|
2934
2953
|
options.cookies,
|
|
2935
2954
|
getAccessTokenCookieName(options.names)
|
|
2936
2955
|
);
|
|
2937
2956
|
return new InsForgeClient({
|
|
2938
|
-
...
|
|
2957
|
+
...options,
|
|
2958
|
+
baseUrl,
|
|
2959
|
+
anonKey,
|
|
2939
2960
|
isServerMode: true,
|
|
2940
2961
|
edgeFunctionToken: accessToken ?? void 0
|
|
2941
2962
|
});
|
|
@@ -2952,12 +2973,25 @@ function jsonResponse(body, init = {}, headers = new Headers(init.headers)) {
|
|
|
2952
2973
|
}
|
|
2953
2974
|
function normalizeError(error) {
|
|
2954
2975
|
if (error instanceof InsForgeError) return error;
|
|
2976
|
+
if (error && typeof error === "object") {
|
|
2977
|
+
const body = error;
|
|
2978
|
+
return new InsForgeError(
|
|
2979
|
+
typeof body.message === "string" ? body.message : "Failed to refresh auth session",
|
|
2980
|
+
typeof body.statusCode === "number" ? body.statusCode : 500,
|
|
2981
|
+
typeof body.error === "string" ? body.error : ERROR_CODES3.UNKNOWN_ERROR
|
|
2982
|
+
);
|
|
2983
|
+
}
|
|
2955
2984
|
return new InsForgeError(
|
|
2956
2985
|
error instanceof Error ? error.message : "Failed to refresh auth session",
|
|
2957
2986
|
500,
|
|
2958
2987
|
ERROR_CODES3.UNKNOWN_ERROR
|
|
2959
2988
|
);
|
|
2960
2989
|
}
|
|
2990
|
+
async function readJson(response) {
|
|
2991
|
+
const contentType = response.headers.get("content-type");
|
|
2992
|
+
if (!contentType?.includes("json")) return null;
|
|
2993
|
+
return response.json();
|
|
2994
|
+
}
|
|
2961
2995
|
function readRefreshToken(options) {
|
|
2962
2996
|
if (options.refreshToken) return options.refreshToken;
|
|
2963
2997
|
const refreshCookieName = getRefreshTokenCookieName(options.names);
|
|
@@ -2972,7 +3006,7 @@ async function refreshAuth(options = {}) {
|
|
|
2972
3006
|
const headers = new Headers();
|
|
2973
3007
|
const refreshToken = readRefreshToken(options);
|
|
2974
3008
|
if (!refreshToken) {
|
|
2975
|
-
|
|
3009
|
+
clearAuthCookieHeaders(headers, options);
|
|
2976
3010
|
const error2 = new InsForgeError(
|
|
2977
3011
|
"Refresh token cookie is missing",
|
|
2978
3012
|
401,
|
|
@@ -2994,13 +3028,55 @@ async function refreshAuth(options = {}) {
|
|
|
2994
3028
|
error: error2
|
|
2995
3029
|
};
|
|
2996
3030
|
}
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3031
|
+
let { baseUrl, anonKey } = options;
|
|
3032
|
+
try {
|
|
3033
|
+
baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
|
|
3034
|
+
anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
|
|
3035
|
+
} catch {
|
|
3036
|
+
}
|
|
3037
|
+
if (!baseUrl || !anonKey) {
|
|
3038
|
+
throw new Error(
|
|
3039
|
+
"Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to refreshAuth() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
|
|
3040
|
+
);
|
|
3041
|
+
}
|
|
3042
|
+
const fetchImpl = options.fetch ?? (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
|
|
3043
|
+
if (!fetchImpl) {
|
|
3044
|
+
throw new Error(
|
|
3045
|
+
"Fetch is not available. Please provide a fetch implementation."
|
|
3046
|
+
);
|
|
3047
|
+
}
|
|
3048
|
+
const requestHeaders = new Headers(options.headers);
|
|
3049
|
+
requestHeaders.set("Authorization", `Bearer ${anonKey}`);
|
|
3050
|
+
requestHeaders.set("Content-Type", "application/json");
|
|
3051
|
+
requestHeaders.set("Accept", "application/json");
|
|
3052
|
+
let data = null;
|
|
3053
|
+
let error = null;
|
|
3054
|
+
try {
|
|
3055
|
+
const response = await fetchImpl(
|
|
3056
|
+
new URL("/api/auth/refresh?client_type=mobile", baseUrl).toString(),
|
|
3057
|
+
{
|
|
3058
|
+
method: "POST",
|
|
3059
|
+
headers: requestHeaders,
|
|
3060
|
+
body: JSON.stringify({ refresh_token: refreshToken })
|
|
3061
|
+
}
|
|
3062
|
+
);
|
|
3063
|
+
const body = await readJson(response);
|
|
3064
|
+
if (!response.ok) {
|
|
3065
|
+
error = normalizeError(
|
|
3066
|
+
body ?? {
|
|
3067
|
+
message: "Failed to refresh auth session",
|
|
3068
|
+
statusCode: response.status,
|
|
3069
|
+
error: ERROR_CODES3.UNKNOWN_ERROR
|
|
3070
|
+
}
|
|
3071
|
+
);
|
|
3072
|
+
} else {
|
|
3073
|
+
data = body;
|
|
3074
|
+
}
|
|
3075
|
+
} catch (caught) {
|
|
3076
|
+
error = normalizeError(caught);
|
|
3077
|
+
}
|
|
3002
3078
|
if (error || !data?.accessToken) {
|
|
3003
|
-
|
|
3079
|
+
clearAuthCookieHeaders(headers, options);
|
|
3004
3080
|
const normalized = normalizeError(error);
|
|
3005
3081
|
return {
|
|
3006
3082
|
response: jsonResponse(
|
|
@@ -3019,7 +3095,7 @@ async function refreshAuth(options = {}) {
|
|
|
3019
3095
|
};
|
|
3020
3096
|
}
|
|
3021
3097
|
const nextRefreshToken = data.refreshToken ?? refreshToken;
|
|
3022
|
-
|
|
3098
|
+
setAuthCookieHeaders(
|
|
3023
3099
|
headers,
|
|
3024
3100
|
{
|
|
3025
3101
|
accessToken: data.accessToken,
|