@doswiftly/storefront-sdk 4.0.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 +430 -0
- package/dist/__tests__/unit/test-helpers.d.ts +46 -0
- package/dist/__tests__/unit/test-helpers.d.ts.map +1 -0
- package/dist/__tests__/unit/test-helpers.js +72 -0
- package/dist/core/auth/auth-client.d.ts +46 -0
- package/dist/core/auth/auth-client.d.ts.map +1 -0
- package/dist/core/auth/auth-client.js +82 -0
- package/dist/core/auth/cookie-config.d.ts +18 -0
- package/dist/core/auth/cookie-config.d.ts.map +1 -0
- package/dist/core/auth/cookie-config.js +18 -0
- package/dist/core/auth/handlers.d.ts +32 -0
- package/dist/core/auth/handlers.d.ts.map +1 -0
- package/dist/core/auth/handlers.js +127 -0
- package/dist/core/auth/routes.d.ts +21 -0
- package/dist/core/auth/routes.d.ts.map +1 -0
- package/dist/core/auth/routes.js +14 -0
- package/dist/core/auth/token-client.d.ts +26 -0
- package/dist/core/auth/token-client.d.ts.map +1 -0
- package/dist/core/auth/token-client.js +42 -0
- package/dist/core/auth/types.d.ts +53 -0
- package/dist/core/auth/types.d.ts.map +1 -0
- package/dist/core/auth/types.js +4 -0
- package/dist/core/cache.d.ts +54 -0
- package/dist/core/cache.d.ts.map +1 -0
- package/dist/core/cache.js +82 -0
- package/dist/core/cart/cart-client.d.ts +57 -0
- package/dist/core/cart/cart-client.d.ts.map +1 -0
- package/dist/core/cart/cart-client.js +89 -0
- package/dist/core/cart/types.d.ts +110 -0
- package/dist/core/cart/types.d.ts.map +1 -0
- package/dist/core/cart/types.js +6 -0
- package/dist/core/client/compose.d.ts +9 -0
- package/dist/core/client/compose.d.ts.map +1 -0
- package/dist/core/client/compose.js +9 -0
- package/dist/core/client/create-client.d.ts +15 -0
- package/dist/core/client/create-client.d.ts.map +1 -0
- package/dist/core/client/create-client.js +85 -0
- package/dist/core/client/dedupe.d.ts +7 -0
- package/dist/core/client/dedupe.d.ts.map +1 -0
- package/dist/core/client/dedupe.js +16 -0
- package/dist/core/client/execute.d.ts +20 -0
- package/dist/core/client/execute.d.ts.map +1 -0
- package/dist/core/client/execute.js +48 -0
- package/dist/core/client/hash.d.ts +7 -0
- package/dist/core/client/hash.d.ts.map +1 -0
- package/dist/core/client/hash.js +21 -0
- package/dist/core/client/operation-name.d.ts +7 -0
- package/dist/core/client/operation-name.d.ts.map +1 -0
- package/dist/core/client/operation-name.js +10 -0
- package/dist/core/client/types.d.ts +126 -0
- package/dist/core/client/types.d.ts.map +1 -0
- package/dist/core/client/types.js +26 -0
- package/dist/core/errors.d.ts +43 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +43 -0
- package/dist/core/format.d.ts +92 -0
- package/dist/core/format.d.ts.map +1 -0
- package/dist/core/format.js +216 -0
- package/dist/core/helpers/assert-no-user-errors.d.ts +10 -0
- package/dist/core/helpers/assert-no-user-errors.d.ts.map +1 -0
- package/dist/core/helpers/assert-no-user-errors.js +16 -0
- package/dist/core/helpers/normalize-connection.d.ts +36 -0
- package/dist/core/helpers/normalize-connection.d.ts.map +1 -0
- package/dist/core/helpers/normalize-connection.js +21 -0
- package/dist/core/helpers/sanitize-html.d.ts +8 -0
- package/dist/core/helpers/sanitize-html.d.ts.map +1 -0
- package/dist/core/helpers/sanitize-html.js +35 -0
- package/dist/core/index.d.ts +59 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +68 -0
- package/dist/core/middleware/auth.d.ts +16 -0
- package/dist/core/middleware/auth.d.ts.map +1 -0
- package/dist/core/middleware/auth.js +22 -0
- package/dist/core/middleware/currency.d.ts +15 -0
- package/dist/core/middleware/currency.d.ts.map +1 -0
- package/dist/core/middleware/currency.js +21 -0
- package/dist/core/middleware/errors.d.ts +24 -0
- package/dist/core/middleware/errors.d.ts.map +1 -0
- package/dist/core/middleware/errors.js +77 -0
- package/dist/core/middleware/retry.d.ts +22 -0
- package/dist/core/middleware/retry.d.ts.map +1 -0
- package/dist/core/middleware/retry.js +58 -0
- package/dist/core/middleware/timeout.d.ts +19 -0
- package/dist/core/middleware/timeout.d.ts.map +1 -0
- package/dist/core/middleware/timeout.js +51 -0
- package/dist/core/operations/auth.d.ts +11 -0
- package/dist/core/operations/auth.d.ts.map +1 -0
- package/dist/core/operations/auth.js +112 -0
- package/dist/core/operations/cart.d.ts +15 -0
- package/dist/core/operations/cart.d.ts.map +1 -0
- package/dist/core/operations/cart.js +169 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/react/cookies.d.ts +28 -0
- package/dist/react/cookies.d.ts.map +1 -0
- package/dist/react/cookies.js +49 -0
- package/dist/react/helpers/create-store-context.d.ts +37 -0
- package/dist/react/helpers/create-store-context.d.ts.map +1 -0
- package/dist/react/helpers/create-store-context.js +47 -0
- package/dist/react/hooks/use-auth.d.ts +65 -0
- package/dist/react/hooks/use-auth.d.ts.map +1 -0
- package/dist/react/hooks/use-auth.js +168 -0
- package/dist/react/hooks/use-cart-manager.d.ts +30 -0
- package/dist/react/hooks/use-cart-manager.d.ts.map +1 -0
- package/dist/react/hooks/use-cart-manager.js +223 -0
- package/dist/react/hooks/use-currency.d.ts +11 -0
- package/dist/react/hooks/use-currency.d.ts.map +1 -0
- package/dist/react/hooks/use-currency.js +19 -0
- package/dist/react/hooks/use-debounced-value.d.ts +15 -0
- package/dist/react/hooks/use-debounced-value.d.ts.map +1 -0
- package/dist/react/hooks/use-debounced-value.js +25 -0
- package/dist/react/hooks/use-hydrated.d.ts +9 -0
- package/dist/react/hooks/use-hydrated.d.ts.map +1 -0
- package/dist/react/hooks/use-hydrated.js +14 -0
- package/dist/react/hooks/use-storefront-client.d.ts +6 -0
- package/dist/react/hooks/use-storefront-client.d.ts.map +1 -0
- package/dist/react/hooks/use-storefront-client.js +8 -0
- package/dist/react/index.d.ts +30 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +34 -0
- package/dist/react/providers/currency-provider.d.ts +14 -0
- package/dist/react/providers/currency-provider.d.ts.map +1 -0
- package/dist/react/providers/currency-provider.js +20 -0
- package/dist/react/providers/storefront-client-provider.d.ts +33 -0
- package/dist/react/providers/storefront-client-provider.d.ts.map +1 -0
- package/dist/react/providers/storefront-client-provider.js +57 -0
- package/dist/react/providers/storefront-provider.d.ts +42 -0
- package/dist/react/providers/storefront-provider.d.ts.map +1 -0
- package/dist/react/providers/storefront-provider.js +40 -0
- package/dist/react/server/get-storefront-client.d.ts +42 -0
- package/dist/react/server/get-storefront-client.d.ts.map +1 -0
- package/dist/react/server/get-storefront-client.js +44 -0
- package/dist/react/server/index.d.ts +2 -0
- package/dist/react/server/index.d.ts.map +1 -0
- package/dist/react/server/index.js +1 -0
- package/dist/react/stores/auth.store.d.ts +48 -0
- package/dist/react/stores/auth.store.d.ts.map +1 -0
- package/dist/react/stores/auth.store.js +67 -0
- package/dist/react/stores/currency.store.d.ts +29 -0
- package/dist/react/stores/currency.store.d.ts.map +1 -0
- package/dist/react/stores/currency.store.js +76 -0
- package/dist/react/stores/index.d.ts +8 -0
- package/dist/react/stores/index.d.ts.map +1 -0
- package/dist/react/stores/index.js +10 -0
- package/dist/react/stores/store-context.d.ts +27 -0
- package/dist/react/stores/store-context.d.ts.map +1 -0
- package/dist/react/stores/store-context.js +62 -0
- package/package.json +71 -0
- package/src/__tests__/contract/storefront-api.contract.test.ts +450 -0
- package/src/__tests__/unit/auth-client.test.ts +210 -0
- package/src/__tests__/unit/cart-client.test.ts +233 -0
- package/src/__tests__/unit/create-client.test.ts +356 -0
- package/src/__tests__/unit/helpers.test.ts +377 -0
- package/src/__tests__/unit/middleware.test.ts +374 -0
- package/src/__tests__/unit/test-helpers.ts +103 -0
- package/src/core/auth/auth-client.ts +123 -0
- package/src/core/auth/cookie-config.ts +23 -0
- package/src/core/auth/handlers.ts +168 -0
- package/src/core/auth/routes.ts +26 -0
- package/src/core/auth/token-client.ts +51 -0
- package/src/core/auth/types.ts +54 -0
- package/src/core/cache.ts +102 -0
- package/src/core/cart/cart-client.ts +150 -0
- package/src/core/cart/types.ts +104 -0
- package/src/core/client/compose.ts +15 -0
- package/src/core/client/create-client.ts +129 -0
- package/src/core/client/dedupe.ts +19 -0
- package/src/core/client/execute.ts +70 -0
- package/src/core/client/hash.ts +21 -0
- package/src/core/client/operation-name.ts +12 -0
- package/src/core/client/types.ts +171 -0
- package/src/core/errors.ts +67 -0
- package/src/core/format.ts +254 -0
- package/src/core/helpers/assert-no-user-errors.ts +21 -0
- package/src/core/helpers/normalize-connection.ts +48 -0
- package/src/core/helpers/sanitize-html.ts +42 -0
- package/src/core/index.ts +148 -0
- package/src/core/middleware/auth.ts +27 -0
- package/src/core/middleware/currency.ts +26 -0
- package/src/core/middleware/errors.ts +86 -0
- package/src/core/middleware/retry.ts +75 -0
- package/src/core/middleware/timeout.ts +61 -0
- package/src/core/operations/auth.ts +123 -0
- package/src/core/operations/cart.ts +185 -0
- package/src/index.ts +25 -0
- package/src/react/cookies.ts +54 -0
- package/src/react/helpers/create-store-context.ts +56 -0
- package/src/react/hooks/use-auth.ts +218 -0
- package/src/react/hooks/use-cart-manager.ts +236 -0
- package/src/react/hooks/use-currency.ts +23 -0
- package/src/react/hooks/use-debounced-value.ts +30 -0
- package/src/react/hooks/use-hydrated.ts +20 -0
- package/src/react/hooks/use-storefront-client.ts +12 -0
- package/src/react/index.ts +45 -0
- package/src/react/providers/currency-provider.tsx +30 -0
- package/src/react/providers/storefront-client-provider.tsx +90 -0
- package/src/react/providers/storefront-provider.tsx +71 -0
- package/src/react/server/get-storefront-client.ts +60 -0
- package/src/react/server/index.ts +1 -0
- package/src/react/stores/auth.store.ts +112 -0
- package/src/react/stores/currency.store.ts +113 -0
- package/src/react/stores/index.ts +17 -0
- package/src/react/stores/store-context.tsx +82 -0
- package/tsconfig.json +20 -0
- package/vitest.config.ts +14 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry middleware — retries on network errors and 5xx responses.
|
|
3
|
+
*
|
|
4
|
+
* ONLY retries queries, NEVER mutations (to prevent double-charges, etc.).
|
|
5
|
+
* Uses exponential backoff with jitter.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { retryMiddleware } from '@doswiftly/storefront-sdk';
|
|
10
|
+
*
|
|
11
|
+
* client.use(retryMiddleware({ maxRetries: 2 }));
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { Middleware } from '../client/types';
|
|
16
|
+
import { StorefrontError, ErrorCodes } from '../errors';
|
|
17
|
+
|
|
18
|
+
export interface RetryOptions {
|
|
19
|
+
/** Maximum number of retries (default: 2) */
|
|
20
|
+
maxRetries?: number;
|
|
21
|
+
/** Initial delay in ms (default: 300) */
|
|
22
|
+
initialDelay?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function retryMiddleware(options: RetryOptions = {}): Middleware {
|
|
26
|
+
const { maxRetries = 2, initialDelay = 300 } = options;
|
|
27
|
+
|
|
28
|
+
return async (request, next) => {
|
|
29
|
+
// Never retry mutations
|
|
30
|
+
if (request.isMutation) {
|
|
31
|
+
return next(request);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let lastError: unknown;
|
|
35
|
+
|
|
36
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
37
|
+
try {
|
|
38
|
+
const response = await next(request);
|
|
39
|
+
|
|
40
|
+
// Retry on 5xx server errors
|
|
41
|
+
if (response.status >= 500 && attempt < maxRetries) {
|
|
42
|
+
await delay(initialDelay * Math.pow(2, attempt));
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return response;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
lastError = error;
|
|
49
|
+
|
|
50
|
+
// Don't retry on non-retriable errors
|
|
51
|
+
if (error instanceof StorefrontError) {
|
|
52
|
+
if (error.code === ErrorCodes.TIMEOUT) {
|
|
53
|
+
// Timeouts are retriable
|
|
54
|
+
} else if (error.status >= 400 && error.status < 500) {
|
|
55
|
+
// 4xx errors are not retriable
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (attempt < maxRetries) {
|
|
61
|
+
await delay(initialDelay * Math.pow(2, attempt));
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw lastError;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function delay(ms: number): Promise<void> {
|
|
72
|
+
// Add jitter: ±25%
|
|
73
|
+
const jitter = ms * 0.25 * (Math.random() * 2 - 1);
|
|
74
|
+
return new Promise(resolve => setTimeout(resolve, ms + jitter));
|
|
75
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timeout middleware — aborts request after timeout via AbortController.
|
|
3
|
+
*
|
|
4
|
+
* Edge-safe (no Node.js APIs). Default: 5 seconds.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { timeoutMiddleware } from '@doswiftly/storefront-sdk';
|
|
9
|
+
*
|
|
10
|
+
* client.use(timeoutMiddleware({ timeout: 10000 })); // 10s
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { Middleware } from '../client/types';
|
|
15
|
+
import { StorefrontError, ErrorCodes } from '../errors';
|
|
16
|
+
|
|
17
|
+
export interface TimeoutOptions {
|
|
18
|
+
/** Timeout in milliseconds (default: 5000) */
|
|
19
|
+
timeout?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function timeoutMiddleware(options: TimeoutOptions = {}): Middleware {
|
|
23
|
+
const { timeout = 5000 } = options;
|
|
24
|
+
|
|
25
|
+
return async (request, next) => {
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
|
|
28
|
+
// If request already has a signal, link them
|
|
29
|
+
if (request.signal) {
|
|
30
|
+
if (request.signal.aborted) {
|
|
31
|
+
controller.abort(request.signal.reason);
|
|
32
|
+
} else {
|
|
33
|
+
request.signal.addEventListener('abort', () => {
|
|
34
|
+
controller.abort(request.signal!.reason);
|
|
35
|
+
}, { once: true });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
request.signal = controller.signal;
|
|
40
|
+
|
|
41
|
+
const timer = setTimeout(() => {
|
|
42
|
+
controller.abort(new Error(`Request timed out after ${timeout}ms`));
|
|
43
|
+
}, timeout);
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const response = await next(request);
|
|
47
|
+
return response;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (controller.signal.aborted) {
|
|
50
|
+
throw new StorefrontError({
|
|
51
|
+
code: ErrorCodes.TIMEOUT,
|
|
52
|
+
message: `Request timed out after ${timeout}ms`,
|
|
53
|
+
cause: error,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
} finally {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth/Customer GraphQL operations — manual query strings (no codegen).
|
|
3
|
+
*
|
|
4
|
+
* These match the backend storefront-graphql schema exactly.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// Fragments
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
const CUSTOMER_ACCESS_TOKEN_FRAGMENT = `
|
|
12
|
+
fragment CustomerAccessTokenFields on CustomerAccessToken {
|
|
13
|
+
accessToken
|
|
14
|
+
expiresAt
|
|
15
|
+
}
|
|
16
|
+
`;
|
|
17
|
+
|
|
18
|
+
const USER_ERROR_FRAGMENT = `
|
|
19
|
+
fragment UserErrorFields on UserError {
|
|
20
|
+
message
|
|
21
|
+
field
|
|
22
|
+
code
|
|
23
|
+
}
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const CUSTOMER_FRAGMENT = `
|
|
27
|
+
fragment CustomerFields on Customer {
|
|
28
|
+
id
|
|
29
|
+
email
|
|
30
|
+
firstName
|
|
31
|
+
lastName
|
|
32
|
+
displayName
|
|
33
|
+
phone
|
|
34
|
+
emailVerified
|
|
35
|
+
emailMarketingState
|
|
36
|
+
defaultAddress {
|
|
37
|
+
id
|
|
38
|
+
address1
|
|
39
|
+
address2
|
|
40
|
+
city
|
|
41
|
+
company
|
|
42
|
+
country
|
|
43
|
+
countryCode
|
|
44
|
+
firstName
|
|
45
|
+
lastName
|
|
46
|
+
phone
|
|
47
|
+
province
|
|
48
|
+
provinceCode
|
|
49
|
+
zip
|
|
50
|
+
isDefault
|
|
51
|
+
}
|
|
52
|
+
ordersCount
|
|
53
|
+
totalSpent {
|
|
54
|
+
amount
|
|
55
|
+
currencyCode
|
|
56
|
+
}
|
|
57
|
+
createdAt
|
|
58
|
+
updatedAt
|
|
59
|
+
}
|
|
60
|
+
`;
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Mutations
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
export const CUSTOMER_LOGIN = `
|
|
67
|
+
mutation CustomerLogin($input: CustomerAccessTokenCreateInput!) {
|
|
68
|
+
customerAccessTokenCreate(input: $input) {
|
|
69
|
+
customerAccessToken { ...CustomerAccessTokenFields }
|
|
70
|
+
userErrors { ...UserErrorFields }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
${CUSTOMER_ACCESS_TOKEN_FRAGMENT}
|
|
74
|
+
${USER_ERROR_FRAGMENT}
|
|
75
|
+
`;
|
|
76
|
+
|
|
77
|
+
export const CUSTOMER_LOGOUT = `
|
|
78
|
+
mutation CustomerLogout($customerAccessToken: String!) {
|
|
79
|
+
customerAccessTokenDelete(customerAccessToken: $customerAccessToken) {
|
|
80
|
+
deletedAccessToken
|
|
81
|
+
deletedCustomerAccessTokenId
|
|
82
|
+
userErrors { ...UserErrorFields }
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
${USER_ERROR_FRAGMENT}
|
|
86
|
+
`;
|
|
87
|
+
|
|
88
|
+
export const CUSTOMER_TOKEN_RENEW = `
|
|
89
|
+
mutation CustomerTokenRenew($customerAccessToken: String!) {
|
|
90
|
+
customerAccessTokenRenew(customerAccessToken: $customerAccessToken) {
|
|
91
|
+
customerAccessToken { ...CustomerAccessTokenFields }
|
|
92
|
+
userErrors { ...UserErrorFields }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
${CUSTOMER_ACCESS_TOKEN_FRAGMENT}
|
|
96
|
+
${USER_ERROR_FRAGMENT}
|
|
97
|
+
`;
|
|
98
|
+
|
|
99
|
+
export const CUSTOMER_CREATE = `
|
|
100
|
+
mutation CustomerCreate($input: CustomerCreateInput!) {
|
|
101
|
+
customerCreate(input: $input) {
|
|
102
|
+
customer { ...CustomerFields }
|
|
103
|
+
customerAccessToken { ...CustomerAccessTokenFields }
|
|
104
|
+
userErrors { ...UserErrorFields }
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
${CUSTOMER_FRAGMENT}
|
|
108
|
+
${CUSTOMER_ACCESS_TOKEN_FRAGMENT}
|
|
109
|
+
${USER_ERROR_FRAGMENT}
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
// Queries
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
export const CUSTOMER_QUERY = `
|
|
117
|
+
query Customer($customerAccessToken: String!) {
|
|
118
|
+
customer(customerAccessToken: $customerAccessToken) {
|
|
119
|
+
...CustomerFields
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
${CUSTOMER_FRAGMENT}
|
|
123
|
+
`;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cart GraphQL operations — manual query strings (no codegen).
|
|
3
|
+
*
|
|
4
|
+
* These match the backend storefront-graphql schema exactly.
|
|
5
|
+
* Cart mutations always return full Cart + userErrors.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Fragments
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
const MONEY_FRAGMENT = `
|
|
13
|
+
fragment Money on Money {
|
|
14
|
+
amount
|
|
15
|
+
currencyCode
|
|
16
|
+
}
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
const CART_COST_FRAGMENT = `
|
|
20
|
+
fragment CartCost on CartCost {
|
|
21
|
+
totalAmount { ...Money }
|
|
22
|
+
subtotalAmount { ...Money }
|
|
23
|
+
totalTaxAmount { ...Money }
|
|
24
|
+
totalDutyAmount { ...Money }
|
|
25
|
+
}
|
|
26
|
+
`;
|
|
27
|
+
|
|
28
|
+
const CART_LINE_FRAGMENT = `
|
|
29
|
+
fragment CartLineFields on CartLine {
|
|
30
|
+
id
|
|
31
|
+
quantity
|
|
32
|
+
merchandise {
|
|
33
|
+
id
|
|
34
|
+
title
|
|
35
|
+
sku
|
|
36
|
+
image { url altText }
|
|
37
|
+
price { ...Money }
|
|
38
|
+
compareAtPrice { ...Money }
|
|
39
|
+
}
|
|
40
|
+
cost {
|
|
41
|
+
totalAmount { ...Money }
|
|
42
|
+
amountPerQuantity { ...Money }
|
|
43
|
+
compareAtAmountPerQuantity { ...Money }
|
|
44
|
+
}
|
|
45
|
+
attributes { key value }
|
|
46
|
+
productId
|
|
47
|
+
productTitle
|
|
48
|
+
productHandle
|
|
49
|
+
}
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
const CART_FRAGMENT = `
|
|
53
|
+
fragment CartFields on Cart {
|
|
54
|
+
id
|
|
55
|
+
checkoutUrl
|
|
56
|
+
totalQuantity
|
|
57
|
+
note
|
|
58
|
+
createdAt
|
|
59
|
+
updatedAt
|
|
60
|
+
cost { ...CartCost }
|
|
61
|
+
lines(first: 100) {
|
|
62
|
+
edges {
|
|
63
|
+
node { ...CartLineFields }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
buyerIdentity {
|
|
67
|
+
email
|
|
68
|
+
phone
|
|
69
|
+
countryCode
|
|
70
|
+
}
|
|
71
|
+
discountCodes {
|
|
72
|
+
code
|
|
73
|
+
applicable
|
|
74
|
+
}
|
|
75
|
+
discountAllocations {
|
|
76
|
+
discountedAmount { ...Money }
|
|
77
|
+
}
|
|
78
|
+
attributes { key value }
|
|
79
|
+
}
|
|
80
|
+
${MONEY_FRAGMENT}
|
|
81
|
+
${CART_COST_FRAGMENT}
|
|
82
|
+
${CART_LINE_FRAGMENT}
|
|
83
|
+
`;
|
|
84
|
+
|
|
85
|
+
const USER_ERROR_FRAGMENT = `
|
|
86
|
+
fragment UserErrorFields on UserError {
|
|
87
|
+
message
|
|
88
|
+
field
|
|
89
|
+
code
|
|
90
|
+
}
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// Queries
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
export const CART_QUERY = `
|
|
98
|
+
query Cart($id: ID!) {
|
|
99
|
+
cart(id: $id) {
|
|
100
|
+
...CartFields
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
${CART_FRAGMENT}
|
|
104
|
+
`;
|
|
105
|
+
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
// Mutations
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
export const CART_CREATE = `
|
|
111
|
+
mutation CartCreate($input: CartCreateInput) {
|
|
112
|
+
cartCreate(input: $input) {
|
|
113
|
+
cart { ...CartFields }
|
|
114
|
+
userErrors { ...UserErrorFields }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
${CART_FRAGMENT}
|
|
118
|
+
${USER_ERROR_FRAGMENT}
|
|
119
|
+
`;
|
|
120
|
+
|
|
121
|
+
export const CART_LINES_ADD = `
|
|
122
|
+
mutation CartLinesAdd($cartId: ID!, $lines: [CartLineInput!]!) {
|
|
123
|
+
cartLinesAdd(cartId: $cartId, lines: $lines) {
|
|
124
|
+
cart { ...CartFields }
|
|
125
|
+
userErrors { ...UserErrorFields }
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
${CART_FRAGMENT}
|
|
129
|
+
${USER_ERROR_FRAGMENT}
|
|
130
|
+
`;
|
|
131
|
+
|
|
132
|
+
export const CART_LINES_UPDATE = `
|
|
133
|
+
mutation CartLinesUpdate($cartId: ID!, $lines: [CartLineUpdateInput!]!) {
|
|
134
|
+
cartLinesUpdate(cartId: $cartId, lines: $lines) {
|
|
135
|
+
cart { ...CartFields }
|
|
136
|
+
userErrors { ...UserErrorFields }
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
${CART_FRAGMENT}
|
|
140
|
+
${USER_ERROR_FRAGMENT}
|
|
141
|
+
`;
|
|
142
|
+
|
|
143
|
+
export const CART_LINES_REMOVE = `
|
|
144
|
+
mutation CartLinesRemove($cartId: ID!, $lineIds: [ID!]!) {
|
|
145
|
+
cartLinesRemove(cartId: $cartId, lineIds: $lineIds) {
|
|
146
|
+
cart { ...CartFields }
|
|
147
|
+
userErrors { ...UserErrorFields }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
${CART_FRAGMENT}
|
|
151
|
+
${USER_ERROR_FRAGMENT}
|
|
152
|
+
`;
|
|
153
|
+
|
|
154
|
+
export const CART_DISCOUNT_CODES_UPDATE = `
|
|
155
|
+
mutation CartDiscountCodesUpdate($cartId: ID!, $discountCodes: [String!]!) {
|
|
156
|
+
cartDiscountCodesUpdate(cartId: $cartId, discountCodes: $discountCodes) {
|
|
157
|
+
cart { ...CartFields }
|
|
158
|
+
userErrors { ...UserErrorFields }
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
${CART_FRAGMENT}
|
|
162
|
+
${USER_ERROR_FRAGMENT}
|
|
163
|
+
`;
|
|
164
|
+
|
|
165
|
+
export const CART_NOTE_UPDATE = `
|
|
166
|
+
mutation CartNoteUpdate($cartId: ID!, $note: String!) {
|
|
167
|
+
cartNoteUpdate(cartId: $cartId, note: $note) {
|
|
168
|
+
cart { ...CartFields }
|
|
169
|
+
userErrors { ...UserErrorFields }
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
${CART_FRAGMENT}
|
|
173
|
+
${USER_ERROR_FRAGMENT}
|
|
174
|
+
`;
|
|
175
|
+
|
|
176
|
+
export const CART_BUYER_IDENTITY_UPDATE = `
|
|
177
|
+
mutation CartBuyerIdentityUpdate($cartId: ID!, $buyerIdentity: CartBuyerIdentityInput!) {
|
|
178
|
+
cartBuyerIdentityUpdate(cartId: $cartId, buyerIdentity: $buyerIdentity) {
|
|
179
|
+
cart { ...CartFields }
|
|
180
|
+
userErrors { ...UserErrorFields }
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
${CART_FRAGMENT}
|
|
184
|
+
${USER_ERROR_FRAGMENT}
|
|
185
|
+
`;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @doswiftly/storefront-sdk v4.0.0
|
|
3
|
+
*
|
|
4
|
+
* Layered SDK — Hydrogen-aligned architecture.
|
|
5
|
+
*
|
|
6
|
+
* Entry points:
|
|
7
|
+
* "." → Core (framework-agnostic, 0 deps)
|
|
8
|
+
* "./react" → React adapter (providers, stores, hooks)
|
|
9
|
+
* "./react/stores" → Zustand stores (direct import)
|
|
10
|
+
* "./react/server" → Server-side client factory
|
|
11
|
+
* "./cache" → Cache strategies
|
|
12
|
+
*
|
|
13
|
+
* @example Core (CLI, Edge, Remix, Astro — no React)
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { createStorefrontClient, CartClient, errorMiddleware } from '@doswiftly/storefront-sdk';
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example React
|
|
19
|
+
* ```tsx
|
|
20
|
+
* import { StorefrontProvider, useAuth, useCartManager } from '@doswiftly/storefront-sdk/react';
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
// Re-export everything from core
|
|
25
|
+
export * from './core';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cookie utilities for SDK consumers.
|
|
3
|
+
*
|
|
4
|
+
* Simple, framework-agnostic cookie read/write for client-side.
|
|
5
|
+
* For Next.js server-side cookies, use `cookies()` from next/headers.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get cookie value by name (client-side only).
|
|
10
|
+
*/
|
|
11
|
+
export function getCookie(name: string): string | null {
|
|
12
|
+
if (typeof document === 'undefined') return null;
|
|
13
|
+
const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${name}=([^;]*)`));
|
|
14
|
+
return match ? decodeURIComponent(match[1]) : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Set cookie (client-side only).
|
|
19
|
+
*/
|
|
20
|
+
export function setCookie(
|
|
21
|
+
name: string,
|
|
22
|
+
value: string,
|
|
23
|
+
options: { maxAge?: number; path?: string; sameSite?: string } = {},
|
|
24
|
+
): void {
|
|
25
|
+
if (typeof document === 'undefined') return;
|
|
26
|
+
const { maxAge = 365 * 24 * 60 * 60, path = '/', sameSite = 'lax' } = options;
|
|
27
|
+
document.cookie = `${name}=${encodeURIComponent(value)};max-age=${maxAge};path=${path};samesite=${sameSite}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Delete cookie (client-side only).
|
|
32
|
+
*/
|
|
33
|
+
export function deleteCookie(name: string, path = '/'): void {
|
|
34
|
+
if (typeof document === 'undefined') return;
|
|
35
|
+
document.cookie = `${name}=;max-age=0;path=${path}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Get preferred currency from cookie (async — works with Next.js cookies()).
|
|
40
|
+
* Falls back to document.cookie on client.
|
|
41
|
+
*/
|
|
42
|
+
export async function getCurrencyFromCookieAsync(): Promise<string | null> {
|
|
43
|
+
// Server-side: try Next.js cookies()
|
|
44
|
+
try {
|
|
45
|
+
const { cookies } = await import('next/headers');
|
|
46
|
+
const cookieStore = await cookies();
|
|
47
|
+
return cookieStore.get('preferred-currency')?.value ?? null;
|
|
48
|
+
} catch {
|
|
49
|
+
// Not in a server context or next not available
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Client-side fallback
|
|
53
|
+
return getCookie('preferred-currency');
|
|
54
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { createContext, useContext, createElement, type ReactNode } from 'react';
|
|
4
|
+
import { useStore, type StoreApi } from 'zustand';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a Context-based Zustand store pattern.
|
|
8
|
+
*
|
|
9
|
+
* Returns Provider, useStore hook, and useApi hook — eliminates
|
|
10
|
+
* module-level singleton boilerplate (Turbopack duplication bug).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const { Provider: CartProvider, useStore: useCartStore, useApi: useCartStoreApi } =
|
|
15
|
+
* createStoreContext<CartState>('CartStore');
|
|
16
|
+
*
|
|
17
|
+
* // In layout:
|
|
18
|
+
* const cartStore = useRef(createCartStore());
|
|
19
|
+
* <CartProvider store={cartStore.current}>{children}</CartProvider>
|
|
20
|
+
*
|
|
21
|
+
* // In components:
|
|
22
|
+
* const isOpen = useCartStore((s) => s.isOpen);
|
|
23
|
+
* const api = useCartStoreApi(); // for .getState() in callbacks
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function createStoreContext<T>(displayName: string) {
|
|
27
|
+
const StoreContext = createContext<StoreApi<T> | null>(null);
|
|
28
|
+
StoreContext.displayName = displayName;
|
|
29
|
+
|
|
30
|
+
function Provider({ store, children }: { store: StoreApi<T>; children: ReactNode }) {
|
|
31
|
+
return createElement(StoreContext.Provider, { value: store }, children);
|
|
32
|
+
}
|
|
33
|
+
Provider.displayName = `${displayName}Provider`;
|
|
34
|
+
|
|
35
|
+
// Overloads: no selector → full state, with selector → selected slice
|
|
36
|
+
function useStoreHook(): T;
|
|
37
|
+
function useStoreHook<U>(selector: (s: T) => U): U;
|
|
38
|
+
function useStoreHook<U>(selector?: (s: T) => U) {
|
|
39
|
+
const store = useContext(StoreContext);
|
|
40
|
+
if (!store) {
|
|
41
|
+
throw new Error(`${displayName}: useStore must be used within its Provider`);
|
|
42
|
+
}
|
|
43
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
44
|
+
return selector ? useStore(store, selector) : useStore(store);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function useApi(): StoreApi<T> {
|
|
48
|
+
const store = useContext(StoreContext);
|
|
49
|
+
if (!store) {
|
|
50
|
+
throw new Error(`${displayName}: useApi must be used within its Provider`);
|
|
51
|
+
}
|
|
52
|
+
return store;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { Provider, useStore: useStoreHook, useApi } as const;
|
|
56
|
+
}
|