@anker-in/lib 0.0.0-beta.1 → 0.0.0-beta.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/.gitkeep +0 -0
- package/.turbo/turbo-build.log +22 -0
- package/.turbo/turbo-test.log +5 -0
- package/dist/cjs/context/HeadlessProvider.js +1 -1
- package/dist/cjs/context/HeadlessProvider.js.map +2 -2
- package/dist/context/HeadlessProvider.d.ts.map +1 -1
- package/dist/esm/cart/const.js +2 -0
- package/dist/esm/cart/const.js.map +7 -0
- package/dist/esm/cart/hooks/basic/useBuyNow.js +2 -0
- package/dist/esm/cart/hooks/basic/useBuyNow.js.map +7 -0
- package/dist/esm/cart/index.js +2 -0
- package/dist/esm/cart/index.js.map +7 -0
- package/dist/esm/cart/track/fbq.js +2 -0
- package/dist/esm/cart/track/fbq.js.map +7 -0
- package/dist/esm/cart/types/index.js +1 -0
- package/dist/esm/cart/types/index.js.map +7 -0
- package/dist/esm/cart/utils/index.js +2 -0
- package/dist/esm/cart/utils/index.js.map +7 -0
- package/dist/esm/context/HeadlessProvider.js +2 -0
- package/dist/esm/context/HeadlessProvider.js.map +7 -0
- package/dist/esm/context/config.js +1 -0
- package/dist/esm/context/config.js.map +7 -0
- package/dist/esm/context/index.js +2 -0
- package/dist/esm/context/index.js.map +7 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/shopify/fetchers/add-cart-lines.js +2 -0
- package/dist/esm/shopify/fetchers/add-cart-lines.js.map +7 -0
- package/dist/esm/shopify/fetchers/create-cart.js +2 -0
- package/dist/esm/shopify/fetchers/create-cart.js.map +7 -0
- package/dist/esm/shopify/fetchers/get-cart.js +2 -0
- package/dist/esm/shopify/fetchers/get-cart.js.map +7 -0
- package/dist/esm/shopify/fetchers/get-products-by-handles.js +2 -0
- package/dist/esm/shopify/fetchers/get-products-by-handles.js.map +7 -0
- package/dist/esm/shopify/fetchers/index.js +2 -0
- package/dist/esm/shopify/fetchers/index.js.map +7 -0
- package/dist/esm/shopify/fetchers/remove-cart-lines.js +2 -0
- package/dist/esm/shopify/fetchers/remove-cart-lines.js.map +7 -0
- package/dist/esm/shopify/fetchers/update-cart-attributes.js +2 -0
- package/dist/esm/shopify/fetchers/update-cart-attributes.js.map +7 -0
- package/dist/esm/shopify/fetchers/update-cart-buyer-identity.js +2 -0
- package/dist/esm/shopify/fetchers/update-cart-buyer-identity.js.map +7 -0
- package/dist/esm/shopify/fetchers/update-cart-codes.js +2 -0
- package/dist/esm/shopify/fetchers/update-cart-codes.js.map +7 -0
- package/dist/esm/shopify/fetchers/update-cart-lines.js +2 -0
- package/dist/esm/shopify/fetchers/update-cart-lines.js.map +7 -0
- package/dist/esm/shopify/fragments/article.js +50 -0
- package/dist/esm/shopify/fragments/article.js.map +7 -0
- package/dist/esm/shopify/fragments/blog.js +11 -0
- package/dist/esm/shopify/fragments/blog.js.map +7 -0
- package/dist/esm/shopify/fragments/cart.js +189 -0
- package/dist/esm/shopify/fragments/cart.js.map +7 -0
- package/dist/esm/shopify/fragments/collection.js +16 -0
- package/dist/esm/shopify/fragments/collection.js.map +7 -0
- package/dist/esm/shopify/fragments/image.js +9 -0
- package/dist/esm/shopify/fragments/image.js.map +7 -0
- package/dist/esm/shopify/fragments/index.js +2 -0
- package/dist/esm/shopify/fragments/index.js.map +7 -0
- package/dist/esm/shopify/fragments/metafields.js +16 -0
- package/dist/esm/shopify/fragments/metafields.js.map +7 -0
- package/dist/esm/shopify/fragments/page-info.js +9 -0
- package/dist/esm/shopify/fragments/page-info.js.map +7 -0
- package/dist/esm/shopify/fragments/page.js +13 -0
- package/dist/esm/shopify/fragments/page.js.map +7 -0
- package/dist/esm/shopify/fragments/product.js +72 -0
- package/dist/esm/shopify/fragments/product.js.map +7 -0
- package/dist/esm/shopify/fragments/seo.js +7 -0
- package/dist/esm/shopify/fragments/seo.js.map +7 -0
- package/dist/esm/shopify/fragments/variant.js +30 -0
- package/dist/esm/shopify/fragments/variant.js.map +7 -0
- package/dist/esm/shopify/gql/fragment-masking.js +2 -0
- package/dist/esm/shopify/gql/fragment-masking.js.map +7 -0
- package/dist/esm/shopify/gql/gql.js +2 -0
- package/dist/esm/shopify/gql/gql.js.map +7 -0
- package/dist/esm/shopify/gql/graphql.js +2 -0
- package/dist/esm/shopify/gql/graphql.js.map +7 -0
- package/dist/esm/shopify/gql/index.js +2 -0
- package/dist/esm/shopify/gql/index.js.map +7 -0
- package/dist/esm/shopify/hooks/index.js +2 -0
- package/dist/esm/shopify/hooks/index.js.map +7 -0
- package/dist/esm/shopify/hooks/useProductsByHandles.js +2 -0
- package/dist/esm/shopify/hooks/useProductsByHandles.js.map +7 -0
- package/dist/esm/shopify/index.js +2 -0
- package/dist/esm/shopify/index.js.map +7 -0
- package/dist/esm/shopify/mutations/add-cart-lines.js +15 -0
- package/dist/esm/shopify/mutations/add-cart-lines.js.map +7 -0
- package/dist/esm/shopify/mutations/create-cart.js +21 -0
- package/dist/esm/shopify/mutations/create-cart.js.map +7 -0
- package/dist/esm/shopify/mutations/remove-cart-items.js +15 -0
- package/dist/esm/shopify/mutations/remove-cart-items.js.map +7 -0
- package/dist/esm/shopify/mutations/update-cart-attributes.js +15 -0
- package/dist/esm/shopify/mutations/update-cart-attributes.js.map +7 -0
- package/dist/esm/shopify/mutations/update-cart-buyer-identity.js +15 -0
- package/dist/esm/shopify/mutations/update-cart-buyer-identity.js.map +7 -0
- package/dist/esm/shopify/mutations/update-cart-discount-code.js +15 -0
- package/dist/esm/shopify/mutations/update-cart-discount-code.js.map +7 -0
- package/dist/esm/shopify/mutations/update-cart-items.js +15 -0
- package/dist/esm/shopify/mutations/update-cart-items.js.map +7 -0
- package/dist/esm/shopify/queries/get-cart.js +13 -0
- package/dist/esm/shopify/queries/get-cart.js.map +7 -0
- package/dist/esm/shopify/queries/get-product-by-handles.js +29 -0
- package/dist/esm/shopify/queries/get-product-by-handles.js.map +7 -0
- package/dist/esm/shopify/queries/index.js +2 -0
- package/dist/esm/shopify/queries/index.js.map +7 -0
- package/dist/esm/shopify/types/article.js +1 -0
- package/dist/esm/shopify/types/article.js.map +7 -0
- package/dist/esm/shopify/types/cart.js +1 -0
- package/dist/esm/shopify/types/cart.js.map +7 -0
- package/dist/esm/shopify/types/checkout.js +1 -0
- package/dist/esm/shopify/types/checkout.js.map +7 -0
- package/dist/esm/shopify/types/collection.js +1 -0
- package/dist/esm/shopify/types/collection.js.map +7 -0
- package/dist/esm/shopify/types/common.js +1 -0
- package/dist/esm/shopify/types/common.js.map +7 -0
- package/dist/esm/shopify/types/config.js +1 -0
- package/dist/esm/shopify/types/config.js.map +7 -0
- package/dist/esm/shopify/types/customer.js +1 -0
- package/dist/esm/shopify/types/customer.js.map +7 -0
- package/dist/esm/shopify/types/fetcher.js +2 -0
- package/dist/esm/shopify/types/fetcher.js.map +7 -0
- package/dist/esm/shopify/types/index.js +2 -0
- package/dist/esm/shopify/types/index.js.map +7 -0
- package/dist/esm/shopify/types/page.js +1 -0
- package/dist/esm/shopify/types/page.js.map +7 -0
- package/dist/esm/shopify/types/product.js +1 -0
- package/dist/esm/shopify/types/product.js.map +7 -0
- package/dist/esm/shopify/types/search.js +1 -0
- package/dist/esm/shopify/types/search.js.map +7 -0
- package/dist/esm/shopify/types/site.js +1 -0
- package/dist/esm/shopify/types/site.js.map +7 -0
- package/dist/esm/shopify/types/type-helper.js +1 -0
- package/dist/esm/shopify/types/type-helper.js.map +7 -0
- package/dist/esm/shopify/utils/colors.js +2 -0
- package/dist/esm/shopify/utils/colors.js.map +7 -0
- package/dist/esm/shopify/utils/const.js +2 -0
- package/dist/esm/shopify/utils/const.js.map +7 -0
- package/dist/esm/shopify/utils/cookie.js +2 -0
- package/dist/esm/shopify/utils/cookie.js.map +7 -0
- package/dist/esm/shopify/utils/errors.js +3 -0
- package/dist/esm/shopify/utils/errors.js.map +7 -0
- package/dist/esm/shopify/utils/fetch-graphql-api.js +3 -0
- package/dist/esm/shopify/utils/fetch-graphql-api.js.map +7 -0
- package/dist/esm/shopify/utils/handle-fetch-response.js +2 -0
- package/dist/esm/shopify/utils/handle-fetch-response.js.map +7 -0
- package/dist/esm/shopify/utils/helper.js +2 -0
- package/dist/esm/shopify/utils/helper.js.map +7 -0
- package/dist/esm/shopify/utils/normalize/cart.js +2 -0
- package/dist/esm/shopify/utils/normalize/cart.js.map +7 -0
- package/dist/esm/shopify/utils/normalize/customer.js +2 -0
- package/dist/esm/shopify/utils/normalize/customer.js.map +7 -0
- package/dist/esm/shopify/utils/normalize/index.js +2 -0
- package/dist/esm/shopify/utils/normalize/index.js.map +7 -0
- package/dist/esm/shopify/utils/normalize/metafield.js +2 -0
- package/dist/esm/shopify/utils/normalize/metafield.js.map +7 -0
- package/dist/esm/shopify/utils/normalize/product.js +2 -0
- package/dist/esm/shopify/utils/normalize/product.js.map +7 -0
- package/dist/esm/shopify/utils/store.js +2 -0
- package/dist/esm/shopify/utils/store.js.map +7 -0
- package/dist/esm/shopify/utils/type-helper.js +1 -0
- package/dist/esm/shopify/utils/type-helper.js.map +7 -0
- package/esbuild-cjs.mjs +23 -0
- package/esbuild-esm.mjs +30 -0
- package/jest.config.ts +12 -0
- package/package.json +10 -13
- package/src/cart/const.ts +23 -0
- package/src/cart/hooks/basic/useBuyNow.ts +105 -0
- package/src/cart/index.ts +19 -0
- package/src/cart/track/fbq.ts +63 -0
- package/src/cart/types/index.ts +25 -0
- package/src/cart/utils/index.ts +22 -0
- package/src/context/HeadlessProvider.tsx +30 -0
- package/src/context/config.ts +10 -0
- package/src/context/index.ts +3 -0
- package/src/index.ts +22 -0
- package/src/shopify/fetchers/add-cart-lines.ts +53 -0
- package/src/shopify/fetchers/create-cart.ts +43 -0
- package/src/shopify/fetchers/get-cart.ts +48 -0
- package/src/shopify/fetchers/get-products-by-handles.ts +48 -0
- package/src/shopify/fetchers/index.ts +10 -0
- package/src/shopify/fetchers/remove-cart-lines.ts +31 -0
- package/src/shopify/fetchers/update-cart-attributes.ts +29 -0
- package/src/shopify/fetchers/update-cart-buyer-identity.ts +32 -0
- package/src/shopify/fetchers/update-cart-codes.ts +32 -0
- package/src/shopify/fetchers/update-cart-lines.ts +42 -0
- package/src/shopify/fragments/article.ts +51 -0
- package/src/shopify/fragments/blog.ts +10 -0
- package/src/shopify/fragments/cart.ts +193 -0
- package/src/shopify/fragments/collection.ts +15 -0
- package/src/shopify/fragments/image.ts +8 -0
- package/src/shopify/fragments/index.ts +11 -0
- package/src/shopify/fragments/metafields.ts +17 -0
- package/src/shopify/fragments/page-info.ts +8 -0
- package/src/shopify/fragments/page.ts +12 -0
- package/src/shopify/fragments/product.ts +71 -0
- package/src/shopify/fragments/seo.ts +6 -0
- package/src/shopify/fragments/variant.ts +29 -0
- package/src/shopify/gql/fragment-masking.ts +85 -0
- package/src/shopify/gql/gql.ts +110 -0
- package/src/shopify/gql/graphql.ts +11780 -0
- package/src/shopify/gql/index.ts +4 -0
- package/src/shopify/hooks/index.ts +1 -0
- package/src/shopify/hooks/useProductsByHandles.ts +24 -0
- package/src/shopify/index.ts +5 -0
- package/src/shopify/mutations/add-cart-lines.ts +16 -0
- package/src/shopify/mutations/create-cart.ts +22 -0
- package/src/shopify/mutations/remove-cart-items.ts +16 -0
- package/src/shopify/mutations/update-cart-attributes.ts +16 -0
- package/src/shopify/mutations/update-cart-buyer-identity.ts +16 -0
- package/src/shopify/mutations/update-cart-discount-code.ts +16 -0
- package/src/shopify/mutations/update-cart-items.ts +16 -0
- package/src/shopify/queries/get-cart.ts +14 -0
- package/src/shopify/queries/get-product-by-handles.ts +32 -0
- package/src/shopify/queries/index.ts +2 -0
- package/src/shopify/types/article.ts +46 -0
- package/src/shopify/types/cart.ts +204 -0
- package/src/shopify/types/checkout.ts +44 -0
- package/src/shopify/types/collection.ts +104 -0
- package/src/shopify/types/common.ts +53 -0
- package/src/shopify/types/config.ts +14 -0
- package/src/shopify/types/customer.ts +31 -0
- package/src/shopify/types/fetcher.ts +67 -0
- package/src/shopify/types/index.ts +11 -0
- package/src/shopify/types/page.ts +45 -0
- package/src/shopify/types/product.ts +176 -0
- package/src/shopify/types/search.ts +62 -0
- package/src/shopify/types/site.ts +38 -0
- package/src/shopify/types/type-helper.ts +5 -0
- package/src/shopify/utils/colors.ts +206 -0
- package/src/shopify/utils/const.ts +115 -0
- package/src/shopify/utils/cookie.ts +28 -0
- package/src/shopify/utils/errors.ts +65 -0
- package/src/shopify/utils/fetch-graphql-api.ts +67 -0
- package/src/shopify/utils/handle-fetch-response.ts +60 -0
- package/src/shopify/utils/helper.ts +89 -0
- package/src/shopify/utils/normalize/cart.ts +106 -0
- package/src/shopify/utils/normalize/customer.ts +18 -0
- package/src/shopify/utils/normalize/index.ts +2 -0
- package/src/shopify/utils/normalize/metafield.ts +69 -0
- package/src/shopify/utils/normalize/product.ts +193 -0
- package/src/shopify/utils/store.ts +5 -0
- package/src/shopify/utils/type-helper.ts +7 -0
- package/tsconfig.json +20 -0
- package/tsup.config.ts +17 -0
- package/dist/index.esm.js +0 -1669
- package/dist/index.js +0 -1679
- package/dist/index.mjs +0 -1669
package/src/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// ========================================
|
|
2
|
+
// Context (from shared)
|
|
3
|
+
// ========================================
|
|
4
|
+
export * from './context'
|
|
5
|
+
|
|
6
|
+
// ========================================
|
|
7
|
+
// Shopify Integration
|
|
8
|
+
// ========================================
|
|
9
|
+
export * from './shopify/fetchers'
|
|
10
|
+
export * from './shopify/hooks'
|
|
11
|
+
export * from './shopify/queries'
|
|
12
|
+
export * from './shopify/fragments'
|
|
13
|
+
export * from './shopify/types'
|
|
14
|
+
|
|
15
|
+
// ========================================
|
|
16
|
+
// Cart Utilities
|
|
17
|
+
// ========================================
|
|
18
|
+
export * from './cart/hooks/basic/useBuyNow'
|
|
19
|
+
export * from './cart/types'
|
|
20
|
+
export * from './cart/utils'
|
|
21
|
+
export * from './cart/const'
|
|
22
|
+
export * from './cart/track/fbq'
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { CartLinesAddPayload, MutationCartLinesAddArgs } from '../gql'
|
|
2
|
+
import { addCartLinesMutation } from '../mutations/add-cart-lines'
|
|
3
|
+
import type { Cart } from '../types/cart'
|
|
4
|
+
import { ShopifyFetcher } from '../types/fetcher'
|
|
5
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
6
|
+
import { normalizeCart } from '../utils/normalize'
|
|
7
|
+
import { ShopifyConfig } from '../types/config'
|
|
8
|
+
import { Optional } from '../types/type-helper'
|
|
9
|
+
/**
|
|
10
|
+
* Adds cart lines to the Shopify cart.
|
|
11
|
+
*
|
|
12
|
+
* @param options - The options for adding cart lines.
|
|
13
|
+
* @param config - The store configuration.
|
|
14
|
+
* @returns The updated cart or undefined if the cart could not be updated.
|
|
15
|
+
*/
|
|
16
|
+
export const addCartLines: ShopifyFetcher<Cart | undefined, MutationCartLinesAddArgs> = async function ({
|
|
17
|
+
options,
|
|
18
|
+
config,
|
|
19
|
+
}: {
|
|
20
|
+
options: MutationCartLinesAddArgs
|
|
21
|
+
config: ShopifyConfig
|
|
22
|
+
}) {
|
|
23
|
+
const { data } = await fetchGraphqlApi<
|
|
24
|
+
{ cartLinesAdd: CartLinesAddPayload },
|
|
25
|
+
Optional<MutationCartLinesAddArgs, 'cartId'>
|
|
26
|
+
>({
|
|
27
|
+
query: addCartLinesMutation,
|
|
28
|
+
variables: {
|
|
29
|
+
cartId: options.cartId,
|
|
30
|
+
lines: options.lines,
|
|
31
|
+
},
|
|
32
|
+
config,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
if (data?.cartLinesAdd?.userErrors?.length) {
|
|
36
|
+
const errors = data.cartLinesAdd.userErrors
|
|
37
|
+
const quantityError = errors.find(error => error.field?.includes('quantity'))
|
|
38
|
+
if (quantityError) {
|
|
39
|
+
// The quantity of the line item is invalid.
|
|
40
|
+
return Promise.reject(quantityError)
|
|
41
|
+
} else {
|
|
42
|
+
throw new Error(JSON.stringify(data?.cartLinesAdd?.userErrors))
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const cart = data?.cartLinesAdd?.cart
|
|
47
|
+
|
|
48
|
+
if (cart) {
|
|
49
|
+
return normalizeCart(cart, '')
|
|
50
|
+
} else {
|
|
51
|
+
return undefined
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { CartCreatePayload, CartInput } from '../gql'
|
|
2
|
+
import { createCartMutation } from '../mutations/create-cart'
|
|
3
|
+
import type { Cart } from '../types/cart'
|
|
4
|
+
import { ShopifyFetcher } from '../types/fetcher'
|
|
5
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
6
|
+
import { normalizeCart } from '../utils/normalize'
|
|
7
|
+
import { GetCartMetafieldQueryRoot } from './get-cart'
|
|
8
|
+
import { constructMetafieldIdentifiersQueryParams } from '../utils/helper'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a cart using the Shopify API.
|
|
12
|
+
*
|
|
13
|
+
* @param options - The options for the cart.
|
|
14
|
+
* @param config - The store configuration.
|
|
15
|
+
* @returns The created cart or undefined if the cart creation fails.
|
|
16
|
+
*/
|
|
17
|
+
export const createCart: ShopifyFetcher<Cart | undefined, CartInput, GetCartMetafieldQueryRoot> = async function ({
|
|
18
|
+
options,
|
|
19
|
+
config,
|
|
20
|
+
}) {
|
|
21
|
+
const { comboMetafieldsNamespace, metafieldIdentifiers, ...rest } = options
|
|
22
|
+
if (!rest.lines?.length) {
|
|
23
|
+
throw new Error('Invalid input used for this operation: Miss lines')
|
|
24
|
+
}
|
|
25
|
+
const { data } = await fetchGraphqlApi<{ cartCreate: CartCreatePayload }, CartInput>({
|
|
26
|
+
query: createCartMutation,
|
|
27
|
+
variables: {
|
|
28
|
+
...rest,
|
|
29
|
+
...(metafieldIdentifiers && {
|
|
30
|
+
...constructMetafieldIdentifiersQueryParams(metafieldIdentifiers, comboMetafieldsNamespace),
|
|
31
|
+
}),
|
|
32
|
+
},
|
|
33
|
+
config,
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const cart = data?.cartCreate?.cart
|
|
37
|
+
|
|
38
|
+
if (cart) {
|
|
39
|
+
return normalizeCart(cart, comboMetafieldsNamespace)
|
|
40
|
+
} else {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Cart, GetCartQueryVariables } from '../gql'
|
|
2
|
+
import type { Cart as ExportCart } from '../types/cart'
|
|
3
|
+
import {
|
|
4
|
+
HasMetafieldQueryRoot,
|
|
5
|
+
metafieldIdentifiersGroup,
|
|
6
|
+
ShopifyFetcher,
|
|
7
|
+
ShopifyFetcherVariables,
|
|
8
|
+
} from '../types/fetcher'
|
|
9
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
10
|
+
import { normalizeCart } from '../utils/normalize'
|
|
11
|
+
import { getCartQuery } from '../queries/get-cart'
|
|
12
|
+
import { constructMetafieldIdentifiersQueryParams } from '../utils/helper'
|
|
13
|
+
|
|
14
|
+
export type GetCartMetafieldQueryRoot = HasMetafieldQueryRoot.Product | HasMetafieldQueryRoot.Variant
|
|
15
|
+
|
|
16
|
+
export type GetCartReq = {
|
|
17
|
+
cartId: string
|
|
18
|
+
} & metafieldIdentifiersGroup<GetCartMetafieldQueryRoot>
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Fetches the cart from Shopify.
|
|
22
|
+
*
|
|
23
|
+
* @param options - The options for fetching the cart.
|
|
24
|
+
* @param config - The store configuration.
|
|
25
|
+
* @returns The fetched cart or undefined if not found.
|
|
26
|
+
*/
|
|
27
|
+
export const getCart: ShopifyFetcher<ExportCart | undefined, GetCartQueryVariables, GetCartMetafieldQueryRoot> =
|
|
28
|
+
async function ({ options, config }) {
|
|
29
|
+
const { comboMetafieldsNamespace: metafieldsNamespace, metafieldIdentifiers, cartId } = options
|
|
30
|
+
|
|
31
|
+
if (!cartId) {
|
|
32
|
+
throw new Error('Invalid input used for this operation: Miss cartId')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { data } = await fetchGraphqlApi<
|
|
36
|
+
{ cart: Cart },
|
|
37
|
+
ShopifyFetcherVariables<GetCartQueryVariables, GetCartMetafieldQueryRoot>
|
|
38
|
+
>({
|
|
39
|
+
query: getCartQuery,
|
|
40
|
+
variables: {
|
|
41
|
+
cartId,
|
|
42
|
+
...constructMetafieldIdentifiersQueryParams(metafieldIdentifiers, metafieldsNamespace),
|
|
43
|
+
},
|
|
44
|
+
config,
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return normalizeCart(data.cart, metafieldsNamespace)
|
|
48
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { getProductsByHandlesQuery } from '../queries/get-product-by-handles'
|
|
2
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
3
|
+
import { constructMetafieldIdentifiersQueryParams } from '../utils/helper'
|
|
4
|
+
import { normalizeProduct } from '../utils/normalize/product'
|
|
5
|
+
import { Product as ShopifyProduct } from '../gql'
|
|
6
|
+
import { HasMetafieldQueryRoot, ShopifyFetcher, ShopifyFetcherVariables } from '../types/fetcher'
|
|
7
|
+
import { Product } from '../types/product'
|
|
8
|
+
|
|
9
|
+
export type GetProductsMetafieldQueryRoot = HasMetafieldQueryRoot.Product | HasMetafieldQueryRoot.Variant
|
|
10
|
+
|
|
11
|
+
export type GetProductsByHandlesReq = {
|
|
12
|
+
handles: string[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Gets products by their handles.
|
|
17
|
+
*
|
|
18
|
+
* @param options - The options for getting products.
|
|
19
|
+
* @param config - The store configuration.
|
|
20
|
+
* @returns An array of products.
|
|
21
|
+
*/
|
|
22
|
+
export const getProductsByHandles: ShopifyFetcher<
|
|
23
|
+
Product[],
|
|
24
|
+
GetProductsByHandlesReq,
|
|
25
|
+
GetProductsMetafieldQueryRoot
|
|
26
|
+
> = async ({ options, config }) => {
|
|
27
|
+
const { handles, metafieldIdentifiers, comboMetafieldsNamespace } = options
|
|
28
|
+
|
|
29
|
+
if (!handles?.length) {
|
|
30
|
+
throw new Error('Handles are required')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const products = await fetchGraphqlApi<
|
|
34
|
+
Record<string, ShopifyProduct>,
|
|
35
|
+
ShopifyFetcherVariables<GetProductsByHandlesReq, GetProductsMetafieldQueryRoot>
|
|
36
|
+
>({
|
|
37
|
+
query: getProductsByHandlesQuery(handles),
|
|
38
|
+
variables: {
|
|
39
|
+
handles,
|
|
40
|
+
...constructMetafieldIdentifiersQueryParams(metafieldIdentifiers, comboMetafieldsNamespace),
|
|
41
|
+
},
|
|
42
|
+
config,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
return Object.values(products.data)
|
|
46
|
+
.filter(item => !!item)
|
|
47
|
+
.map(product => normalizeProduct(product, '')) as Product[]
|
|
48
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { getProductsByHandles } from './get-products-by-handles'
|
|
2
|
+
export { addCartLines } from './add-cart-lines'
|
|
3
|
+
export { createCart } from './create-cart'
|
|
4
|
+
export { removeCartLines } from './remove-cart-lines'
|
|
5
|
+
export { updateCartAttributes } from './update-cart-attributes'
|
|
6
|
+
export { updateCartLines } from './update-cart-lines'
|
|
7
|
+
export { getCart } from './get-cart'
|
|
8
|
+
|
|
9
|
+
export type { GetProductsByHandlesReq, GetProductsMetafieldQueryRoot } from './get-products-by-handles'
|
|
10
|
+
export type { GetCartReq, GetCartMetafieldQueryRoot } from './get-cart'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Cart, MutationCartLinesRemoveArgs } from '../gql'
|
|
2
|
+
import { removeCartItemsMutation } from '../mutations/remove-cart-items'
|
|
3
|
+
import type { Cart as ExportCart } from '../types/cart'
|
|
4
|
+
import { ShopifyFetcher } from '../types/fetcher'
|
|
5
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
6
|
+
import { normalizeCart } from '../utils/normalize'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Removes cart lines from the Shopify API.
|
|
10
|
+
*
|
|
11
|
+
* @param options - The options for removing cart lines.
|
|
12
|
+
* @param config - The store configuration.
|
|
13
|
+
* @returns The normalized cart after removing the cart lines, or undefined if the operation fails.
|
|
14
|
+
*/
|
|
15
|
+
export const removeCartLines: ShopifyFetcher<ExportCart | undefined, MutationCartLinesRemoveArgs> = async function ({
|
|
16
|
+
options,
|
|
17
|
+
config,
|
|
18
|
+
}) {
|
|
19
|
+
const { data } = await fetchGraphqlApi<{ cartLinesRemove: { cart: Cart } }, MutationCartLinesRemoveArgs>({
|
|
20
|
+
query: removeCartItemsMutation,
|
|
21
|
+
variables: options,
|
|
22
|
+
config,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
if (data?.cartLinesRemove?.cart) {
|
|
26
|
+
const normalizedCart = normalizeCart(data.cartLinesRemove.cart, '')
|
|
27
|
+
return normalizedCart
|
|
28
|
+
} else {
|
|
29
|
+
return undefined
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Cart, MutationCartAttributesUpdateArgs } from '../gql'
|
|
2
|
+
import type { Cart as ExportCart } from '../types/cart'
|
|
3
|
+
import { ShopifyFetcher } from '../types/fetcher'
|
|
4
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
5
|
+
import { normalizeCart } from '../utils/normalize'
|
|
6
|
+
import { updateCartAttributesMutation } from '../mutations/update-cart-attributes'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Updates the attributes of a cart.
|
|
10
|
+
*
|
|
11
|
+
* @param options - The options for updating the cart attributes.
|
|
12
|
+
* @param config - The store configuration.
|
|
13
|
+
* @returns The updated cart or undefined if the update was unsuccessful.
|
|
14
|
+
*/
|
|
15
|
+
export const updateCartAttributes: ShopifyFetcher<ExportCart | undefined, MutationCartAttributesUpdateArgs> =
|
|
16
|
+
async function ({ options, config }) {
|
|
17
|
+
const { data } = await fetchGraphqlApi<{ cartAttributesUpdate: { cart: Cart } }, MutationCartAttributesUpdateArgs>({
|
|
18
|
+
query: updateCartAttributesMutation,
|
|
19
|
+
variables: options,
|
|
20
|
+
config,
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
if (data?.cartAttributesUpdate?.cart) {
|
|
24
|
+
const normalizedCart = normalizeCart(data.cartAttributesUpdate.cart, '')
|
|
25
|
+
return normalizedCart
|
|
26
|
+
} else {
|
|
27
|
+
return undefined
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Cart, MutationCartBuyerIdentityUpdateArgs } from '../gql'
|
|
2
|
+
import type { Cart as ExportCart } from '../types/cart'
|
|
3
|
+
import { ShopifyFetcher } from '../types/fetcher'
|
|
4
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
5
|
+
import { normalizeCart } from '../utils/normalize'
|
|
6
|
+
import { updateCartBuyerIdentityMutation } from '../mutations/update-cart-buyer-identity'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Updates the buyer identity of a cart.
|
|
10
|
+
*
|
|
11
|
+
* @param options - The options for updating the cart buyer identity.
|
|
12
|
+
* @param config - The store configuration.
|
|
13
|
+
* @returns The updated cart or undefined if the update was unsuccessful.
|
|
14
|
+
*/
|
|
15
|
+
export const updateCartBuyerIdentity: ShopifyFetcher<ExportCart | undefined, MutationCartBuyerIdentityUpdateArgs> =
|
|
16
|
+
async function ({ options, config }) {
|
|
17
|
+
const { data } = await fetchGraphqlApi<
|
|
18
|
+
{ cartBuyerIdentityUpdate: { cart: Cart } },
|
|
19
|
+
MutationCartBuyerIdentityUpdateArgs
|
|
20
|
+
>({
|
|
21
|
+
query: updateCartBuyerIdentityMutation,
|
|
22
|
+
variables: options,
|
|
23
|
+
config,
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
if (data?.cartBuyerIdentityUpdate?.cart) {
|
|
27
|
+
const normalizedCart = normalizeCart(data.cartBuyerIdentityUpdate.cart, '')
|
|
28
|
+
return normalizedCart
|
|
29
|
+
} else {
|
|
30
|
+
return undefined
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Cart, MutationCartDiscountCodesUpdateArgs } from '../gql'
|
|
2
|
+
import type { Cart as ExportCart } from '../types/cart'
|
|
3
|
+
import { ShopifyFetcher } from '../types/fetcher'
|
|
4
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
5
|
+
import { normalizeCart } from '../utils/normalize'
|
|
6
|
+
import { updateCartDiscountCodeMutation } from '../mutations/update-cart-discount-code'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Updates the discount codes for a cart in the Shopify API.
|
|
10
|
+
*
|
|
11
|
+
* @param options - The options for updating the cart discount codes.
|
|
12
|
+
* @param config - The store configuration.
|
|
13
|
+
* @returns The updated cart or undefined if the update was unsuccessful.
|
|
14
|
+
*/
|
|
15
|
+
export const updateCartCodes: ShopifyFetcher<ExportCart | undefined, MutationCartDiscountCodesUpdateArgs> =
|
|
16
|
+
async function ({ options, config }) {
|
|
17
|
+
const { data } = await fetchGraphqlApi<
|
|
18
|
+
{
|
|
19
|
+
cartDiscountCodesUpdate: { cart: Cart }
|
|
20
|
+
},
|
|
21
|
+
MutationCartDiscountCodesUpdateArgs
|
|
22
|
+
>({
|
|
23
|
+
query: updateCartDiscountCodeMutation,
|
|
24
|
+
variables: options,
|
|
25
|
+
config,
|
|
26
|
+
})
|
|
27
|
+
if (data.cartDiscountCodesUpdate.cart) {
|
|
28
|
+
return normalizeCart(data.cartDiscountCodesUpdate.cart, '')
|
|
29
|
+
} else {
|
|
30
|
+
return undefined
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Cart, MutationCartLinesUpdateArgs } from '../gql'
|
|
2
|
+
import type { Cart as ExportCart } from '../types/cart'
|
|
3
|
+
import { ShopifyFetcher } from '../types/fetcher'
|
|
4
|
+
import { fetchGraphqlApi } from '../utils/fetch-graphql-api'
|
|
5
|
+
import { normalizeCart } from '../utils/normalize'
|
|
6
|
+
import { updateCartLinesMutation } from '../mutations/update-cart-items'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Updates the cart lines in the Shopify API.
|
|
10
|
+
*
|
|
11
|
+
* @param options - The options for updating the cart lines.
|
|
12
|
+
* @param config - The store configuration.
|
|
13
|
+
* @returns The updated cart or undefined if the update was unsuccessful.
|
|
14
|
+
*/
|
|
15
|
+
export const updateCartLines: ShopifyFetcher<ExportCart | undefined, MutationCartLinesUpdateArgs> = async function ({
|
|
16
|
+
options,
|
|
17
|
+
config,
|
|
18
|
+
}) {
|
|
19
|
+
const { data } = await fetchGraphqlApi<
|
|
20
|
+
{ cartLinesUpdate: { cart: Cart; userErrors?: any } },
|
|
21
|
+
MutationCartLinesUpdateArgs
|
|
22
|
+
>({
|
|
23
|
+
query: updateCartLinesMutation,
|
|
24
|
+
variables: options,
|
|
25
|
+
config,
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
if (data?.cartLinesUpdate?.userErrors?.length) {
|
|
29
|
+
const errors = data.cartLinesUpdate?.userErrors
|
|
30
|
+
const quantityError = errors.find((error: any) => error.field?.includes('quantity'))
|
|
31
|
+
if (quantityError) {
|
|
32
|
+
return Promise.reject(quantityError)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (data?.cartLinesUpdate?.cart) {
|
|
37
|
+
const normalizedCart = normalizeCart(data.cartLinesUpdate.cart, '')
|
|
38
|
+
return normalizedCart
|
|
39
|
+
} else {
|
|
40
|
+
return undefined
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export const articleFragment = /* GraphQL */ `
|
|
2
|
+
fragment article on Article {
|
|
3
|
+
id
|
|
4
|
+
handle
|
|
5
|
+
title
|
|
6
|
+
seo {
|
|
7
|
+
...seo
|
|
8
|
+
}
|
|
9
|
+
image {
|
|
10
|
+
...image
|
|
11
|
+
}
|
|
12
|
+
tags
|
|
13
|
+
publishedAt
|
|
14
|
+
content
|
|
15
|
+
contentHtml
|
|
16
|
+
excerpt
|
|
17
|
+
excerptHtml
|
|
18
|
+
authorV2 {
|
|
19
|
+
bio
|
|
20
|
+
email
|
|
21
|
+
firstName
|
|
22
|
+
lastName
|
|
23
|
+
name
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
`
|
|
27
|
+
|
|
28
|
+
export const articleWithoutContentFragment = /* GraphQL */ `
|
|
29
|
+
fragment article on Article {
|
|
30
|
+
id
|
|
31
|
+
handle
|
|
32
|
+
title
|
|
33
|
+
seo {
|
|
34
|
+
...seo
|
|
35
|
+
}
|
|
36
|
+
image {
|
|
37
|
+
...image
|
|
38
|
+
}
|
|
39
|
+
tags
|
|
40
|
+
publishedAt
|
|
41
|
+
excerpt
|
|
42
|
+
excerptHtml
|
|
43
|
+
authorV2 {
|
|
44
|
+
bio
|
|
45
|
+
email
|
|
46
|
+
firstName
|
|
47
|
+
lastName
|
|
48
|
+
name
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
`
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { imageFragment } from './image'
|
|
2
|
+
import { metafieldFragment } from './metafields'
|
|
3
|
+
import { productFragment } from './product'
|
|
4
|
+
import { seoFragment } from './seo'
|
|
5
|
+
|
|
6
|
+
export const cartFragment = /* GraphQL */ `
|
|
7
|
+
fragment cart on Cart {
|
|
8
|
+
id
|
|
9
|
+
totalQuantity
|
|
10
|
+
updatedAt
|
|
11
|
+
createdAt
|
|
12
|
+
checkoutUrl
|
|
13
|
+
note
|
|
14
|
+
attributes {
|
|
15
|
+
key
|
|
16
|
+
value
|
|
17
|
+
}
|
|
18
|
+
cost {
|
|
19
|
+
checkoutChargeAmount {
|
|
20
|
+
amount
|
|
21
|
+
currencyCode
|
|
22
|
+
}
|
|
23
|
+
subtotalAmount {
|
|
24
|
+
amount
|
|
25
|
+
currencyCode
|
|
26
|
+
}
|
|
27
|
+
totalAmount {
|
|
28
|
+
amount
|
|
29
|
+
currencyCode
|
|
30
|
+
}
|
|
31
|
+
totalTaxAmount {
|
|
32
|
+
amount
|
|
33
|
+
currencyCode
|
|
34
|
+
}
|
|
35
|
+
totalDutyAmount {
|
|
36
|
+
amount
|
|
37
|
+
currencyCode
|
|
38
|
+
}
|
|
39
|
+
totalAmountEstimated
|
|
40
|
+
totalTaxAmountEstimated
|
|
41
|
+
totalDutyAmountEstimated
|
|
42
|
+
subtotalAmountEstimated
|
|
43
|
+
}
|
|
44
|
+
buyerIdentity {
|
|
45
|
+
email
|
|
46
|
+
customer {
|
|
47
|
+
email
|
|
48
|
+
id
|
|
49
|
+
}
|
|
50
|
+
deliveryAddressPreferences {
|
|
51
|
+
... on MailingAddress {
|
|
52
|
+
address1
|
|
53
|
+
address2
|
|
54
|
+
city
|
|
55
|
+
country
|
|
56
|
+
zip
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
discountCodes {
|
|
61
|
+
applicable
|
|
62
|
+
code
|
|
63
|
+
}
|
|
64
|
+
discountAllocations {
|
|
65
|
+
... on CartCodeDiscountAllocation {
|
|
66
|
+
code
|
|
67
|
+
discountedAmount {
|
|
68
|
+
amount
|
|
69
|
+
currencyCode
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
... on CartAutomaticDiscountAllocation {
|
|
73
|
+
title
|
|
74
|
+
discountedAmount {
|
|
75
|
+
amount
|
|
76
|
+
currencyCode
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
... on CartCustomDiscountAllocation {
|
|
80
|
+
title
|
|
81
|
+
discountedAmount {
|
|
82
|
+
amount
|
|
83
|
+
currencyCode
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
deliveryGroups(first: 10) {
|
|
88
|
+
nodes {
|
|
89
|
+
deliveryAddress {
|
|
90
|
+
address1
|
|
91
|
+
address2
|
|
92
|
+
city
|
|
93
|
+
country
|
|
94
|
+
zip
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
lines(first: 100) {
|
|
99
|
+
edges {
|
|
100
|
+
node {
|
|
101
|
+
id
|
|
102
|
+
quantity
|
|
103
|
+
discountAllocations {
|
|
104
|
+
... on CartCodeDiscountAllocation {
|
|
105
|
+
code
|
|
106
|
+
discountedAmount {
|
|
107
|
+
amount
|
|
108
|
+
currencyCode
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
... on CartAutomaticDiscountAllocation {
|
|
112
|
+
title
|
|
113
|
+
discountedAmount {
|
|
114
|
+
amount
|
|
115
|
+
currencyCode
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
... on CartCustomDiscountAllocation {
|
|
119
|
+
title
|
|
120
|
+
discountedAmount {
|
|
121
|
+
amount
|
|
122
|
+
currencyCode
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
attributes {
|
|
127
|
+
key
|
|
128
|
+
value
|
|
129
|
+
}
|
|
130
|
+
cost {
|
|
131
|
+
amountPerQuantity {
|
|
132
|
+
amount
|
|
133
|
+
currencyCode
|
|
134
|
+
}
|
|
135
|
+
compareAtAmountPerQuantity {
|
|
136
|
+
amount
|
|
137
|
+
currencyCode
|
|
138
|
+
}
|
|
139
|
+
subtotalAmount {
|
|
140
|
+
amount
|
|
141
|
+
currencyCode
|
|
142
|
+
}
|
|
143
|
+
totalAmount {
|
|
144
|
+
amount
|
|
145
|
+
currencyCode
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
merchandise {
|
|
149
|
+
... on ProductVariant {
|
|
150
|
+
id
|
|
151
|
+
sku
|
|
152
|
+
title
|
|
153
|
+
quantityAvailable
|
|
154
|
+
currentlyNotInStock
|
|
155
|
+
availableForSale
|
|
156
|
+
barcode
|
|
157
|
+
weight
|
|
158
|
+
selectedOptions {
|
|
159
|
+
name
|
|
160
|
+
value
|
|
161
|
+
}
|
|
162
|
+
image {
|
|
163
|
+
...image
|
|
164
|
+
}
|
|
165
|
+
price {
|
|
166
|
+
amount
|
|
167
|
+
currencyCode
|
|
168
|
+
}
|
|
169
|
+
compareAtPrice {
|
|
170
|
+
amount
|
|
171
|
+
currencyCode
|
|
172
|
+
}
|
|
173
|
+
metafields(identifiers: $variantMetafieldIdentifiers) {
|
|
174
|
+
...metafield
|
|
175
|
+
}
|
|
176
|
+
product {
|
|
177
|
+
...product
|
|
178
|
+
metafields(identifiers: $productMetafieldIdentifiers) {
|
|
179
|
+
...metafield
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
totalQuantity
|
|
188
|
+
}
|
|
189
|
+
${productFragment}
|
|
190
|
+
${seoFragment}
|
|
191
|
+
${imageFragment}
|
|
192
|
+
${metafieldFragment}
|
|
193
|
+
`
|