@commercengine/storefront-sdk-nextjs 0.1.0-alpha.0 → 1.0.0-alpha.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 +515 -139
- package/dist/client.cjs +405 -0
- package/dist/client.d.cts +121 -0
- package/dist/client.d.ts +121 -0
- package/dist/client.js +378 -0
- package/dist/index.cjs +229 -140
- package/dist/index.d.cts +27 -102
- package/dist/index.d.ts +27 -102
- package/dist/index.js +230 -132
- package/dist/middleware.cjs +66 -0
- package/dist/middleware.d.cts +38 -0
- package/dist/middleware.d.ts +38 -0
- package/dist/middleware.js +39 -0
- package/dist/server-ByBPoXJG.d.cts +182 -0
- package/dist/server-ByBPoXJG.d.ts +182 -0
- package/dist/server-CwxgXezP.d.cts +115 -0
- package/dist/server-CwxgXezP.d.ts +115 -0
- package/dist/server-D-pFrx8J.d.cts +105 -0
- package/dist/server-DaDfTjsO.d.cts +103 -0
- package/dist/server-DaDfTjsO.d.ts +103 -0
- package/dist/server-DjlQVC11.d.ts +105 -0
- package/dist/server.cjs +385 -0
- package/dist/server.d.cts +3 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +358 -0
- package/dist/storefront.cjs +474 -0
- package/dist/storefront.d.cts +2 -0
- package/dist/storefront.d.ts +2 -0
- package/dist/storefront.js +448 -0
- package/package.json +18 -8
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal cookie interface compatible with Next.js middleware cookies API.
|
|
3
|
+
*/
|
|
4
|
+
interface NextCookiesLike {
|
|
5
|
+
get: (name: string) => {
|
|
6
|
+
value: string;
|
|
7
|
+
} | undefined;
|
|
8
|
+
set: (name: string, value: string, options?: {
|
|
9
|
+
maxAge?: number;
|
|
10
|
+
path?: string;
|
|
11
|
+
domain?: string;
|
|
12
|
+
secure?: boolean;
|
|
13
|
+
sameSite?: "strict" | "lax" | "none" | (string & {});
|
|
14
|
+
httpOnly?: boolean;
|
|
15
|
+
}) => any;
|
|
16
|
+
}
|
|
17
|
+
interface EnsureTokensOptions {
|
|
18
|
+
baseUrl: string;
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
cookiePrefix?: string;
|
|
21
|
+
cookieOptions?: {
|
|
22
|
+
maxAge?: number;
|
|
23
|
+
path?: string;
|
|
24
|
+
domain?: string;
|
|
25
|
+
secure?: boolean;
|
|
26
|
+
sameSite?: "strict" | "lax" | "none";
|
|
27
|
+
httpOnly?: boolean;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Pre-seed anonymous tokens in Next.js middleware when missing.
|
|
32
|
+
* Call this early to ensure SSR and client share the same session.
|
|
33
|
+
*
|
|
34
|
+
* Returns true if tokens were set on the response, false otherwise.
|
|
35
|
+
*/
|
|
36
|
+
declare function ensureStorefrontTokens(reqCookies: NextCookiesLike, resCookies: NextCookiesLike, opts: EnsureTokensOptions): Promise<boolean>;
|
|
37
|
+
|
|
38
|
+
export { type EnsureTokensOptions, type NextCookiesLike, ensureStorefrontTokens };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/middleware-helper.ts
|
|
2
|
+
async function ensureStorefrontTokens(reqCookies, resCookies, opts) {
|
|
3
|
+
const prefix = opts.cookiePrefix ?? "ce_";
|
|
4
|
+
const ACCESS = `${prefix}access_token`;
|
|
5
|
+
const existingAccess = reqCookies.get(ACCESS)?.value;
|
|
6
|
+
if (existingAccess) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const resp = await fetch(`${opts.baseUrl}/auth/anonymous`, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: {
|
|
13
|
+
"Content-Type": "application/json",
|
|
14
|
+
...opts.apiKey ? { "X-Api-Key": opts.apiKey } : {}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
if (!resp.ok) return false;
|
|
18
|
+
const data = await resp.json();
|
|
19
|
+
const content = data?.content;
|
|
20
|
+
if (!content?.access_token || !content?.refresh_token) return false;
|
|
21
|
+
const cookieDefaults = {
|
|
22
|
+
path: "/",
|
|
23
|
+
httpOnly: false,
|
|
24
|
+
sameSite: "lax",
|
|
25
|
+
secure: opts.cookieOptions?.secure ?? false,
|
|
26
|
+
maxAge: 30 * 24 * 60 * 60
|
|
27
|
+
// 30 days
|
|
28
|
+
};
|
|
29
|
+
const finalOpts = { ...cookieDefaults, ...opts.cookieOptions || {} };
|
|
30
|
+
resCookies.set(ACCESS, content.access_token, finalOpts);
|
|
31
|
+
resCookies.set(`${prefix}refresh_token`, content.refresh_token, finalOpts);
|
|
32
|
+
return true;
|
|
33
|
+
} catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
ensureStorefrontTokens
|
|
39
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { TokenStorage, StorefrontSDK, Environment, SupportedDefaultHeaders, DebugLoggerFn, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for NextJSTokenStorage
|
|
5
|
+
*/
|
|
6
|
+
interface NextJSTokenStorageOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Prefix for cookie names (default: "ce_")
|
|
9
|
+
*/
|
|
10
|
+
prefix?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum age of cookies in seconds (default: 30 days)
|
|
13
|
+
*/
|
|
14
|
+
maxAge?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Cookie path (default: "/")
|
|
17
|
+
*/
|
|
18
|
+
path?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Cookie domain (default: current domain)
|
|
21
|
+
*/
|
|
22
|
+
domain?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
25
|
+
*/
|
|
26
|
+
secure?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* SameSite cookie attribute (default: "Lax")
|
|
29
|
+
*/
|
|
30
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Client-side token storage that uses document.cookie
|
|
34
|
+
*/
|
|
35
|
+
declare class ClientTokenStorage implements TokenStorage {
|
|
36
|
+
private accessTokenKey;
|
|
37
|
+
private refreshTokenKey;
|
|
38
|
+
private options;
|
|
39
|
+
constructor(options?: NextJSTokenStorageOptions);
|
|
40
|
+
getAccessToken(): Promise<string | null>;
|
|
41
|
+
setAccessToken(token: string): Promise<void>;
|
|
42
|
+
getRefreshToken(): Promise<string | null>;
|
|
43
|
+
setRefreshToken(token: string): Promise<void>;
|
|
44
|
+
clearTokens(): Promise<void>;
|
|
45
|
+
private getCookie;
|
|
46
|
+
private setCookie;
|
|
47
|
+
private deleteCookie;
|
|
48
|
+
}
|
|
49
|
+
type NextCookieStore$2 = {
|
|
50
|
+
get: (name: string) => {
|
|
51
|
+
value: string;
|
|
52
|
+
} | undefined;
|
|
53
|
+
set: (name: string, value: string, options?: any) => void;
|
|
54
|
+
delete: (name: string) => void;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Server-side token storage that uses Next.js cookies API
|
|
58
|
+
*/
|
|
59
|
+
declare class ServerTokenStorage implements TokenStorage {
|
|
60
|
+
private accessTokenKey;
|
|
61
|
+
private refreshTokenKey;
|
|
62
|
+
private options;
|
|
63
|
+
private cookieStore;
|
|
64
|
+
constructor(cookieStore: NextCookieStore$2, options?: NextJSTokenStorageOptions);
|
|
65
|
+
getAccessToken(): Promise<string | null>;
|
|
66
|
+
setAccessToken(token: string): Promise<void>;
|
|
67
|
+
getRefreshToken(): Promise<string | null>;
|
|
68
|
+
setRefreshToken(token: string): Promise<void>;
|
|
69
|
+
clearTokens(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Configuration for the NextJS SDK wrapper
|
|
74
|
+
*/
|
|
75
|
+
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
76
|
+
/**
|
|
77
|
+
* Token storage configuration options
|
|
78
|
+
*/
|
|
79
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Runtime configuration overrides that can be passed to storefront() function
|
|
83
|
+
* These override environment variables for that specific request
|
|
84
|
+
*/
|
|
85
|
+
interface StorefrontRuntimeConfig {
|
|
86
|
+
/**
|
|
87
|
+
* Override environment variables
|
|
88
|
+
*/
|
|
89
|
+
storeId?: string;
|
|
90
|
+
apiKey?: string;
|
|
91
|
+
environment?: Environment;
|
|
92
|
+
baseUrl?: string;
|
|
93
|
+
timeout?: number;
|
|
94
|
+
debug?: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Advanced options not available via environment variables
|
|
97
|
+
*/
|
|
98
|
+
accessToken?: string;
|
|
99
|
+
refreshToken?: string;
|
|
100
|
+
defaultHeaders?: SupportedDefaultHeaders;
|
|
101
|
+
logger?: DebugLoggerFn;
|
|
102
|
+
onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
|
|
103
|
+
onTokensCleared?: () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Token storage configuration
|
|
106
|
+
*/
|
|
107
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
108
|
+
}
|
|
109
|
+
type NextCookieStore$1 = {
|
|
110
|
+
get: (name: string) => {
|
|
111
|
+
value: string;
|
|
112
|
+
} | undefined;
|
|
113
|
+
set: (name: string, value: string, options?: any) => void;
|
|
114
|
+
delete: (name: string) => void;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Smart SDK getter that automatically detects environment
|
|
118
|
+
*
|
|
119
|
+
* Usage:
|
|
120
|
+
* - Client-side: getStorefrontSDK()
|
|
121
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
122
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
123
|
+
*/
|
|
124
|
+
declare function getStorefrontSDK(): StorefrontSDK;
|
|
125
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore$1): StorefrontSDK;
|
|
126
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore$1, requestConfig?: StorefrontRuntimeConfig): StorefrontSDK;
|
|
127
|
+
/**
|
|
128
|
+
* Set global storefront configuration that applies to all SDK instances
|
|
129
|
+
* This gets bundled into both client and server contexts automatically
|
|
130
|
+
*/
|
|
131
|
+
declare function setGlobalStorefrontConfig(config: StorefrontRuntimeConfig | null): void;
|
|
132
|
+
/**
|
|
133
|
+
* Get the current global storefront configuration
|
|
134
|
+
*/
|
|
135
|
+
declare function getGlobalStorefrontConfig(): StorefrontRuntimeConfig | null;
|
|
136
|
+
/**
|
|
137
|
+
* Initialize the SDK (internal use)
|
|
138
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
139
|
+
*/
|
|
140
|
+
declare function initializeStorefrontSDK(): void;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Universal storefront SDK export
|
|
144
|
+
*
|
|
145
|
+
* This provides a simple unified interface that works in all Next.js contexts:
|
|
146
|
+
* - Client components: automatically uses client token storage
|
|
147
|
+
* - Server components: requires cookies() to be passed
|
|
148
|
+
* - SSG/ISR: automatically uses memory storage with optional build caching
|
|
149
|
+
*
|
|
150
|
+
* Usage:
|
|
151
|
+
* ```typescript
|
|
152
|
+
* // Client-side
|
|
153
|
+
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
154
|
+
* const products = await storefront().catalog.listProducts()
|
|
155
|
+
*
|
|
156
|
+
* // Server-side
|
|
157
|
+
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
158
|
+
* import { cookies } from 'next/headers'
|
|
159
|
+
* const products = await storefront(cookies()).catalog.listProducts()
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
type NextCookieStore = {
|
|
164
|
+
get: (name: string) => {
|
|
165
|
+
value: string;
|
|
166
|
+
} | undefined;
|
|
167
|
+
set: (name: string, value: string, options?: any) => void;
|
|
168
|
+
delete: (name: string) => void;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* Universal storefront SDK accessor
|
|
172
|
+
*
|
|
173
|
+
* @param cookieStore - Next.js cookie store (required on server-side)
|
|
174
|
+
* @param config - Optional per-request configuration overrides
|
|
175
|
+
* @returns StorefrontSDK instance configured for the current environment
|
|
176
|
+
*/
|
|
177
|
+
declare function storefront(): StorefrontSDK;
|
|
178
|
+
declare function storefront(cookieStore: NextCookieStore): StorefrontSDK;
|
|
179
|
+
declare function storefront(config: StorefrontRuntimeConfig): StorefrontSDK;
|
|
180
|
+
declare function storefront(cookieStore: NextCookieStore, config: StorefrontRuntimeConfig): StorefrontSDK;
|
|
181
|
+
|
|
182
|
+
export { ClientTokenStorage as C, type NextJSSDKConfig as N, type StorefrontRuntimeConfig as S, getGlobalStorefrontConfig as a, storefront as b, ServerTokenStorage as c, type NextJSTokenStorageOptions as d, getStorefrontSDK as g, initializeStorefrontSDK as i, setGlobalStorefrontConfig as s };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { TokenStorage, StorefrontSDK, Environment, SupportedDefaultHeaders, DebugLoggerFn, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for NextJSTokenStorage
|
|
5
|
+
*/
|
|
6
|
+
interface NextJSTokenStorageOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Prefix for cookie names (default: "ce_")
|
|
9
|
+
*/
|
|
10
|
+
prefix?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum age of cookies in seconds (default: 30 days)
|
|
13
|
+
*/
|
|
14
|
+
maxAge?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Cookie path (default: "/")
|
|
17
|
+
*/
|
|
18
|
+
path?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Cookie domain (default: current domain)
|
|
21
|
+
*/
|
|
22
|
+
domain?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
25
|
+
*/
|
|
26
|
+
secure?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* SameSite cookie attribute (default: "Lax")
|
|
29
|
+
*/
|
|
30
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Client-side token storage that uses document.cookie
|
|
34
|
+
*/
|
|
35
|
+
declare class ClientTokenStorage implements TokenStorage {
|
|
36
|
+
private accessTokenKey;
|
|
37
|
+
private refreshTokenKey;
|
|
38
|
+
private options;
|
|
39
|
+
constructor(options?: NextJSTokenStorageOptions);
|
|
40
|
+
getAccessToken(): Promise<string | null>;
|
|
41
|
+
setAccessToken(token: string): Promise<void>;
|
|
42
|
+
getRefreshToken(): Promise<string | null>;
|
|
43
|
+
setRefreshToken(token: string): Promise<void>;
|
|
44
|
+
clearTokens(): Promise<void>;
|
|
45
|
+
private getCookie;
|
|
46
|
+
private setCookie;
|
|
47
|
+
private deleteCookie;
|
|
48
|
+
}
|
|
49
|
+
type NextCookieStore$2 = {
|
|
50
|
+
get: (name: string) => {
|
|
51
|
+
value: string;
|
|
52
|
+
} | undefined;
|
|
53
|
+
set: (name: string, value: string, options?: any) => void;
|
|
54
|
+
delete: (name: string) => void;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Server-side token storage that uses Next.js cookies API
|
|
58
|
+
*/
|
|
59
|
+
declare class ServerTokenStorage implements TokenStorage {
|
|
60
|
+
private accessTokenKey;
|
|
61
|
+
private refreshTokenKey;
|
|
62
|
+
private options;
|
|
63
|
+
private cookieStore;
|
|
64
|
+
constructor(cookieStore: NextCookieStore$2, options?: NextJSTokenStorageOptions);
|
|
65
|
+
getAccessToken(): Promise<string | null>;
|
|
66
|
+
setAccessToken(token: string): Promise<void>;
|
|
67
|
+
getRefreshToken(): Promise<string | null>;
|
|
68
|
+
setRefreshToken(token: string): Promise<void>;
|
|
69
|
+
clearTokens(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Configuration for the NextJS SDK wrapper
|
|
74
|
+
*/
|
|
75
|
+
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
76
|
+
/**
|
|
77
|
+
* Token storage configuration options
|
|
78
|
+
*/
|
|
79
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Runtime configuration overrides that can be passed to storefront() function
|
|
83
|
+
* These override environment variables for that specific request
|
|
84
|
+
*/
|
|
85
|
+
interface StorefrontRuntimeConfig {
|
|
86
|
+
/**
|
|
87
|
+
* Override environment variables
|
|
88
|
+
*/
|
|
89
|
+
storeId?: string;
|
|
90
|
+
apiKey?: string;
|
|
91
|
+
environment?: Environment;
|
|
92
|
+
baseUrl?: string;
|
|
93
|
+
timeout?: number;
|
|
94
|
+
debug?: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Advanced options not available via environment variables
|
|
97
|
+
*/
|
|
98
|
+
accessToken?: string;
|
|
99
|
+
refreshToken?: string;
|
|
100
|
+
defaultHeaders?: SupportedDefaultHeaders;
|
|
101
|
+
logger?: DebugLoggerFn;
|
|
102
|
+
onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
|
|
103
|
+
onTokensCleared?: () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Token storage configuration
|
|
106
|
+
*/
|
|
107
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
108
|
+
}
|
|
109
|
+
type NextCookieStore$1 = {
|
|
110
|
+
get: (name: string) => {
|
|
111
|
+
value: string;
|
|
112
|
+
} | undefined;
|
|
113
|
+
set: (name: string, value: string, options?: any) => void;
|
|
114
|
+
delete: (name: string) => void;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Smart SDK getter that automatically detects environment
|
|
118
|
+
*
|
|
119
|
+
* Usage:
|
|
120
|
+
* - Client-side: getStorefrontSDK()
|
|
121
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
122
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
123
|
+
*/
|
|
124
|
+
declare function getStorefrontSDK(): StorefrontSDK;
|
|
125
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore$1): StorefrontSDK;
|
|
126
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore$1, requestConfig?: StorefrontRuntimeConfig): StorefrontSDK;
|
|
127
|
+
/**
|
|
128
|
+
* Set global storefront configuration that applies to all SDK instances
|
|
129
|
+
* This gets bundled into both client and server contexts automatically
|
|
130
|
+
*/
|
|
131
|
+
declare function setGlobalStorefrontConfig(config: StorefrontRuntimeConfig | null): void;
|
|
132
|
+
/**
|
|
133
|
+
* Get the current global storefront configuration
|
|
134
|
+
*/
|
|
135
|
+
declare function getGlobalStorefrontConfig(): StorefrontRuntimeConfig | null;
|
|
136
|
+
/**
|
|
137
|
+
* Initialize the SDK (internal use)
|
|
138
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
139
|
+
*/
|
|
140
|
+
declare function initializeStorefrontSDK(): void;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Universal storefront SDK export
|
|
144
|
+
*
|
|
145
|
+
* This provides a simple unified interface that works in all Next.js contexts:
|
|
146
|
+
* - Client components: automatically uses client token storage
|
|
147
|
+
* - Server components: requires cookies() to be passed
|
|
148
|
+
* - SSG/ISR: automatically uses memory storage with optional build caching
|
|
149
|
+
*
|
|
150
|
+
* Usage:
|
|
151
|
+
* ```typescript
|
|
152
|
+
* // Client-side
|
|
153
|
+
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
154
|
+
* const products = await storefront().catalog.listProducts()
|
|
155
|
+
*
|
|
156
|
+
* // Server-side
|
|
157
|
+
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
158
|
+
* import { cookies } from 'next/headers'
|
|
159
|
+
* const products = await storefront(cookies()).catalog.listProducts()
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
type NextCookieStore = {
|
|
164
|
+
get: (name: string) => {
|
|
165
|
+
value: string;
|
|
166
|
+
} | undefined;
|
|
167
|
+
set: (name: string, value: string, options?: any) => void;
|
|
168
|
+
delete: (name: string) => void;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* Universal storefront SDK accessor
|
|
172
|
+
*
|
|
173
|
+
* @param cookieStore - Next.js cookie store (required on server-side)
|
|
174
|
+
* @param config - Optional per-request configuration overrides
|
|
175
|
+
* @returns StorefrontSDK instance configured for the current environment
|
|
176
|
+
*/
|
|
177
|
+
declare function storefront(): StorefrontSDK;
|
|
178
|
+
declare function storefront(cookieStore: NextCookieStore): StorefrontSDK;
|
|
179
|
+
declare function storefront(config: StorefrontRuntimeConfig): StorefrontSDK;
|
|
180
|
+
declare function storefront(cookieStore: NextCookieStore, config: StorefrontRuntimeConfig): StorefrontSDK;
|
|
181
|
+
|
|
182
|
+
export { ClientTokenStorage as C, type NextJSSDKConfig as N, type StorefrontRuntimeConfig as S, getGlobalStorefrontConfig as a, storefront as b, ServerTokenStorage as c, type NextJSTokenStorageOptions as d, getStorefrontSDK as g, initializeStorefrontSDK as i, setGlobalStorefrontConfig as s };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { TokenStorage, Environment, SupportedDefaultHeaders, DebugLoggerFn, StorefrontSDK, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for NextJSTokenStorage
|
|
5
|
+
*/
|
|
6
|
+
interface NextJSTokenStorageOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Prefix for cookie names (default: "ce_")
|
|
9
|
+
*/
|
|
10
|
+
prefix?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum age of cookies in seconds (default: 30 days)
|
|
13
|
+
*/
|
|
14
|
+
maxAge?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Cookie path (default: "/")
|
|
17
|
+
*/
|
|
18
|
+
path?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Cookie domain (default: current domain)
|
|
21
|
+
*/
|
|
22
|
+
domain?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
25
|
+
*/
|
|
26
|
+
secure?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* SameSite cookie attribute (default: "Lax")
|
|
29
|
+
*/
|
|
30
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
31
|
+
}
|
|
32
|
+
type NextCookieStore$1 = {
|
|
33
|
+
get: (name: string) => {
|
|
34
|
+
value: string;
|
|
35
|
+
} | undefined;
|
|
36
|
+
set: (name: string, value: string, options?: any) => void;
|
|
37
|
+
delete: (name: string) => void;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Server-side token storage that uses Next.js cookies API
|
|
41
|
+
*/
|
|
42
|
+
declare class ServerTokenStorage implements TokenStorage {
|
|
43
|
+
private accessTokenKey;
|
|
44
|
+
private refreshTokenKey;
|
|
45
|
+
private options;
|
|
46
|
+
private cookieStore;
|
|
47
|
+
constructor(cookieStore: NextCookieStore$1, options?: NextJSTokenStorageOptions);
|
|
48
|
+
getAccessToken(): Promise<string | null>;
|
|
49
|
+
setAccessToken(token: string): Promise<void>;
|
|
50
|
+
getRefreshToken(): Promise<string | null>;
|
|
51
|
+
setRefreshToken(token: string): Promise<void>;
|
|
52
|
+
clearTokens(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Configuration for the NextJS SDK wrapper
|
|
57
|
+
*/
|
|
58
|
+
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
59
|
+
/**
|
|
60
|
+
* Token storage configuration options
|
|
61
|
+
*/
|
|
62
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Runtime configuration overrides that can be passed to storefront() function
|
|
66
|
+
* These override environment variables for that specific request
|
|
67
|
+
*/
|
|
68
|
+
interface StorefrontRuntimeConfig {
|
|
69
|
+
/**
|
|
70
|
+
* Override environment variables
|
|
71
|
+
*/
|
|
72
|
+
storeId?: string;
|
|
73
|
+
apiKey?: string;
|
|
74
|
+
environment?: Environment;
|
|
75
|
+
baseUrl?: string;
|
|
76
|
+
timeout?: number;
|
|
77
|
+
debug?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Advanced options not available via environment variables
|
|
80
|
+
*/
|
|
81
|
+
accessToken?: string;
|
|
82
|
+
refreshToken?: string;
|
|
83
|
+
defaultHeaders?: SupportedDefaultHeaders;
|
|
84
|
+
logger?: DebugLoggerFn;
|
|
85
|
+
onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
|
|
86
|
+
onTokensCleared?: () => void;
|
|
87
|
+
/**
|
|
88
|
+
* Token storage configuration
|
|
89
|
+
*/
|
|
90
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
91
|
+
}
|
|
92
|
+
type NextCookieStore = {
|
|
93
|
+
get: (name: string) => {
|
|
94
|
+
value: string;
|
|
95
|
+
} | undefined;
|
|
96
|
+
set: (name: string, value: string, options?: any) => void;
|
|
97
|
+
delete: (name: string) => void;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Smart SDK getter that automatically detects environment
|
|
101
|
+
*
|
|
102
|
+
* Usage:
|
|
103
|
+
* - Client-side: getStorefrontSDK()
|
|
104
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
105
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
106
|
+
*/
|
|
107
|
+
declare function getStorefrontSDK(): StorefrontSDK;
|
|
108
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore): StorefrontSDK;
|
|
109
|
+
/**
|
|
110
|
+
* Initialize the SDK (internal use)
|
|
111
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
112
|
+
*/
|
|
113
|
+
declare function initializeStorefrontSDK(): void;
|
|
114
|
+
|
|
115
|
+
export { type NextJSSDKConfig as N, type StorefrontRuntimeConfig as S, ServerTokenStorage as a, type NextJSTokenStorageOptions as b, getStorefrontSDK as g, initializeStorefrontSDK as i };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { TokenStorage, Environment, SupportedDefaultHeaders, DebugLoggerFn, StorefrontSDK, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for NextJSTokenStorage
|
|
5
|
+
*/
|
|
6
|
+
interface NextJSTokenStorageOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Prefix for cookie names (default: "ce_")
|
|
9
|
+
*/
|
|
10
|
+
prefix?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum age of cookies in seconds (default: 30 days)
|
|
13
|
+
*/
|
|
14
|
+
maxAge?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Cookie path (default: "/")
|
|
17
|
+
*/
|
|
18
|
+
path?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Cookie domain (default: current domain)
|
|
21
|
+
*/
|
|
22
|
+
domain?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
25
|
+
*/
|
|
26
|
+
secure?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* SameSite cookie attribute (default: "Lax")
|
|
29
|
+
*/
|
|
30
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
31
|
+
}
|
|
32
|
+
type NextCookieStore$1 = {
|
|
33
|
+
get: (name: string) => {
|
|
34
|
+
value: string;
|
|
35
|
+
} | undefined;
|
|
36
|
+
set: (name: string, value: string, options?: any) => void;
|
|
37
|
+
delete: (name: string) => void;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Server-side token storage that uses Next.js cookies API
|
|
41
|
+
*/
|
|
42
|
+
declare class ServerTokenStorage implements TokenStorage {
|
|
43
|
+
private accessTokenKey;
|
|
44
|
+
private refreshTokenKey;
|
|
45
|
+
private options;
|
|
46
|
+
private cookieStore;
|
|
47
|
+
constructor(cookieStore: NextCookieStore$1, options?: NextJSTokenStorageOptions);
|
|
48
|
+
getAccessToken(): Promise<string | null>;
|
|
49
|
+
setAccessToken(token: string): Promise<void>;
|
|
50
|
+
getRefreshToken(): Promise<string | null>;
|
|
51
|
+
setRefreshToken(token: string): Promise<void>;
|
|
52
|
+
clearTokens(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Configuration for the NextJS SDK wrapper
|
|
57
|
+
*/
|
|
58
|
+
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
59
|
+
/**
|
|
60
|
+
* Token storage configuration options
|
|
61
|
+
*/
|
|
62
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Runtime configuration overrides that can be passed to storefront() function
|
|
66
|
+
* These override environment variables for that specific request
|
|
67
|
+
*/
|
|
68
|
+
interface StorefrontRuntimeConfig {
|
|
69
|
+
/**
|
|
70
|
+
* Override environment variables
|
|
71
|
+
*/
|
|
72
|
+
storeId?: string;
|
|
73
|
+
apiKey?: string;
|
|
74
|
+
environment?: Environment;
|
|
75
|
+
baseUrl?: string;
|
|
76
|
+
timeout?: number;
|
|
77
|
+
debug?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Advanced options not available via environment variables
|
|
80
|
+
*/
|
|
81
|
+
accessToken?: string;
|
|
82
|
+
refreshToken?: string;
|
|
83
|
+
defaultHeaders?: SupportedDefaultHeaders;
|
|
84
|
+
logger?: DebugLoggerFn;
|
|
85
|
+
onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
|
|
86
|
+
onTokensCleared?: () => void;
|
|
87
|
+
/**
|
|
88
|
+
* Token storage configuration
|
|
89
|
+
*/
|
|
90
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
91
|
+
}
|
|
92
|
+
type NextCookieStore = {
|
|
93
|
+
get: (name: string) => {
|
|
94
|
+
value: string;
|
|
95
|
+
} | undefined;
|
|
96
|
+
set: (name: string, value: string, options?: any) => void;
|
|
97
|
+
delete: (name: string) => void;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Smart SDK getter that automatically detects environment
|
|
101
|
+
*
|
|
102
|
+
* Usage:
|
|
103
|
+
* - Client-side: getStorefrontSDK()
|
|
104
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
105
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
106
|
+
*/
|
|
107
|
+
declare function getStorefrontSDK(): StorefrontSDK;
|
|
108
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore): StorefrontSDK;
|
|
109
|
+
/**
|
|
110
|
+
* Initialize the SDK (internal use)
|
|
111
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
112
|
+
*/
|
|
113
|
+
declare function initializeStorefrontSDK(): void;
|
|
114
|
+
|
|
115
|
+
export { type NextJSSDKConfig as N, type StorefrontRuntimeConfig as S, ServerTokenStorage as a, type NextJSTokenStorageOptions as b, getStorefrontSDK as g, initializeStorefrontSDK as i };
|