Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spree Commerce Provider #484

Merged
merged 132 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 125 commits
Commits
Show all changes
132 commits
Select commit Hold shift + click to select a range
dd6ad75
Include @spree/storefront-api-v2-sdk
Jul 23, 2021
a3ef27f
Add basic Spree framework structure
Jul 23, 2021
c90fa7a
Add Spree as allowed Framework
Jul 23, 2021
14e7a4f
Fetch product images, standardize API fetch using Spree SDK
Jul 27, 2021
24e635b
Include slug and path in products
Jul 28, 2021
a7a75e7
Fetch single product during build time
Jul 28, 2021
c546d26
PLP with searching by category
Jul 28, 2021
2209731
Fetch Spree Categories and Brands
Jul 28, 2021
a86bcd1
Sort PLP
Jul 28, 2021
c173b47
Search products by name
Jul 28, 2021
cfa72b0
Fix option values collection
Jul 28, 2021
cc0da75
Fix hasNonMasterVariants
Jul 28, 2021
2c4e2e4
Sort Categories and Brands
Jul 28, 2021
744a8b9
Add configuration to show product options when there's one variant av…
Jul 29, 2021
a27996a
Enable text search for the Spree Framework
Aug 16, 2021
6ef9f4d
Allow removing line items
Aug 17, 2021
28ee2eb
Allow updating line item quantity
Aug 17, 2021
b38d15e
Add __typename to variant options to allow adding the selected varian…
Aug 17, 2021
30c29f0
Use fetch and Request from node-fetch in Spree SDK
Aug 18, 2021
3b3a181
Update Spree SDK fetcher
Aug 18, 2021
17f8d49
Show placeholder message for /chechout and adjust api fetcher type
Aug 18, 2021
c150a79
Use kebab case instead of camel case
Aug 18, 2021
718493a
Remove outdated comments
Aug 18, 2021
01c702a
Remove outdated comment
Aug 19, 2021
bf4adbd
Resolve isColorProductOption duplication
Aug 19, 2021
c932344
Type Spree variants and line items and temporarily remove height, wid…
Aug 19, 2021
8f95d76
Remove outdated comment
Aug 19, 2021
7d5a63b
Update comments about cart discounts
Aug 19, 2021
ed49ac8
Remove 'spree' prefix from isomorphicConfig and add lastUpdatedProduc…
Aug 19, 2021
a191e28
Implement getAllProductPaths to prerender some products during build …
Aug 19, 2021
3d2ee58
Adjust fetchers to the latest Spree SDK interface
Aug 20, 2021
eba8c28
Add types to Spree taxons mapping
Aug 20, 2021
464c8ca
Revert port change in package.json scripts
Aug 20, 2021
4a007ca
Add basic README describing Spree installation
Aug 20, 2021
f16b6b8
Expand README's installation section
Aug 20, 2021
df7e379
Upgrade Spree SDK to 4.7.0 and add node-fetch to dependencies
Aug 20, 2021
0540258
Order providers alphanumerically
tniezg Aug 23, 2021
57c176d
Sort products by available_on when using the Trending sorting in useS…
Aug 24, 2021
c3bd932
Change the default Spree port to 4000 and update README in sync with …
Aug 24, 2021
d98a755
Save primary variant's SKU when normalizing a product from Spree
Aug 24, 2021
5e743f0
Create a new cart if Spree can't find the current using a token
Aug 24, 2021
894f18f
Add separator to README
Aug 24, 2021
4481002
Add missing Error subclass
Aug 24, 2021
be0e246
Allow placeholder images for products and line items without images
Aug 25, 2021
0fa7d97
Add image
Aug 25, 2021
0ad4361
Merge remote-tracking branch 'origin-vercel/main' into spree-framewor…
Aug 25, 2021
25fd21e
Reset tsconfig.json paths to originla values
Aug 25, 2021
2f9e90e
Search taxonomies by permalinks instead of IDs
Aug 26, 2021
2953678
Upgrade Spree SDK to version 4.7.1
Aug 26, 2021
c869150
Merge branch 'main' into spree-framework-poc
tniezg Aug 30, 2021
490f94b
Remove references to @framework and use relative paths instead
tniezg Aug 30, 2021
320fc06
Generalize TypeScript and add typings to getPage
tniezg Aug 30, 2021
92bb7bc
Update fetcher to avoid parsing non-JSON responses
tniezg Aug 30, 2021
e9397bc
Use original product image by default instead of resized
tniezg Aug 31, 2021
b9f8ed6
Link to an online demo of the Spree integration in the README
tniezg Sep 1, 2021
60894d3
Flatten fetcher responses
tniezg Sep 3, 2021
1e37ce1
Merge branch 'main' into spree-framework-poc
tniezg Sep 9, 2021
c0f1a66
Include Spree in the list of supported ecommerce backends in README
tniezg Sep 9, 2021
632c2c6
Update README.md
damianlegawiec Sep 13, 2021
10408e3
Format Spree's README
tniezg Sep 13, 2021
964f04a
Add link to the Spree demo site in the main README
tniezg Sep 13, 2021
75165df
Update README.md
damianlegawiec Sep 13, 2021
6c3c94b
Update README.md
damianlegawiec Sep 13, 2021
5b241d0
Allow setting a taxon id for getAllProducts
tniezg Sep 13, 2021
c8ee3ef
Use Spree SDK's JSON:API helpers
tniezg Sep 13, 2021
fed88c8
Sort getAllProducts by -updated_at when using a taxonomy
tniezg Sep 13, 2021
76596fd
Remove slash '/' from line item's paths
tniezg Sep 13, 2021
ed7e110
Allow filtering variant images by option type
tniezg Sep 22, 2021
e2c2ade
Merge pull request #1 from spree/filter-variant-images-by-option-type
tniezg Sep 22, 2021
cab6b04
Merge branch 'main' into spree
tniezg Sep 24, 2021
73c5e9d
Upgrade checkout behavior in line with core NextJS Commerce changes
tniezg Sep 24, 2021
03295eb
Merge pull request #2 from spree/upgrade-checkout
tniezg Sep 24, 2021
20ffd5a
Merge branch 'main' into spree
tniezg Sep 24, 2021
0fc678b
Remove dummy submitCheckout function
tniezg Sep 24, 2021
a2560dc
Merge branch 'main' into spree
tniezg Sep 27, 2021
42318f4
[NX-24] Display PDP option types sorted by position from Spree
aplegatt Sep 29, 2021
da63989
Merge pull request #3 from spree/nx24-options-by-position
tniezg Oct 1, 2021
48dbcf1
Merge branch 'main' into spree
tniezg Oct 11, 2021
669cc8d
Supply Spree primary variant if a product has no option variants
tniezg Oct 11, 2021
68e9403
Merge pull request #4 from spree/fix-products-without-options-types
tniezg Oct 11, 2021
5288313
Do not throw an error if a product doesn't have NEXT_PUBLIC_SPREE_IMA…
tniezg Oct 11, 2021
f5ab965
Merge pull request #5 from spree/fix-images-option-filter
tniezg Oct 11, 2021
d12f1b1
[NX-43] Uses image transformations when fetching products images
aplegatt Oct 20, 2021
eeb003e
Merge pull request #6 from spree/nx-43-using-image-transformations
tniezg Oct 29, 2021
2536940
Merge branch 'main' into spree
tniezg Nov 9, 2021
a84baae
Merge branch 'main' into spree
tniezg Nov 16, 2021
311c495
Use bind to properly call Spree SDK methods and update SDK fetcher in…
tniezg Nov 16, 2021
c9eabf8
Fix ESLint issues in useHook
tniezg Nov 16, 2021
1ce4ea7
Support account sign up, login and logout
tniezg Nov 17, 2021
cdd4758
Fetch logged in user's cart after login or signup but associate guest…
tniezg Nov 18, 2021
160cf94
Support Spree default wishlist show, add and remove wished items oper…
tniezg Nov 19, 2021
1a0b5e2
Fetch Spree CMS Pages
tniezg Nov 19, 2021
55da90a
Fix login, handle critical token errors and fix WishlistCard
tniezg Nov 19, 2021
8250930
Fix variable name (#574)
isxwor Nov 18, 2021
8618a03
Update get-cart.ts (#474)
pfcodes Nov 19, 2021
ea9c126
Update normalize.ts (#475)
pfcodes Nov 19, 2021
3ff49fe
Update add-item.ts (#473)
pfcodes Nov 19, 2021
36ff343
fix typo (#572)
konradk Nov 19, 2021
578f61b
Fix authentication.refreshToken arguments
tniezg Nov 22, 2021
8c73220
Remove redundant comments and logs
tniezg Nov 22, 2021
e03b9f3
Fix createEmptyCart request to Spree and add option to disable auto l…
tniezg Nov 22, 2021
3be270e
Merge branch 'main' into spree
tniezg Nov 23, 2021
59762a3
Merge branch 'main' into spree
tniezg Nov 24, 2021
d007f5b
Merge branch 'main' into spree
tniezg Nov 24, 2021
eff469e
Merge branch 'main' into spree
tniezg Nov 24, 2021
d2e506f
Merge branch 'main' into spree
tniezg Nov 24, 2021
b3b8252
Merge branch 'main' into spree
tniezg Nov 25, 2021
9e6f80e
Fix formatting issues
tniezg Nov 25, 2021
e7fa124
Merge pull request #16 from spree/prettier-fix
tniezg Nov 25, 2021
8374ac5
Apply image transformation when fetching images for products in cart
tniezg Nov 25, 2021
fc18b12
Merge pull request #17 from spree/fix-cart-product-images
tniezg Nov 25, 2021
f22b76e
Merge branch 'main' into spree
tniezg Dec 1, 2021
f1377e2
Replace call to qs with Spree SDK built-in helper
tniezg Dec 1, 2021
282132d
Upgrade Spree SDK to 5.0.1
tniezg Dec 3, 2021
64550c5
Merge pull request #18 from spree/upgrade-spree-sdk
tniezg Dec 3, 2021
8706f50
Merge branch 'main' into spree
tniezg Dec 7, 2021
7436f6d
Rename zeitFetch import to vercelFetch
tniezg Dec 8, 2021
ff7923c
Merge pull request #19 from spree/rename-vercel-fetch-import
tniezg Dec 8, 2021
80c74ba
Abstract fetcher JSON Content-Type checking into separate function
tniezg Dec 8, 2021
fb688a4
Merge pull request #20 from spree/abstract-json-content-type-predicate
tniezg Dec 8, 2021
7f27ed6
Rename imageUrl to url
tniezg Dec 8, 2021
a32db0e
Merge pull request #21 from spree/shorten-variable-prefix
tniezg Dec 8, 2021
9d35c02
Remove return type for getProductPath
tniezg Dec 8, 2021
dc4a6fe
Merge pull request #22 from spree/remove-redundant-return-type
tniezg Dec 8, 2021
6b548cf
Merge branch 'main' into spree
tniezg Dec 9, 2021
f45d721
Merge branch 'main' into spree
tniezg Dec 9, 2021
8e71fed
Merge branch 'main' into spree
tniezg Dec 13, 2021
d160668
Change URL to Spree demo store in root README
tniezg Dec 13, 2021
acb4e89
Change label for link to Spree demo store in Spree's README
tniezg Dec 13, 2021
489023f
Change URL to Spree demo store in Spree's README
tniezg Dec 13, 2021
beac986
Use only relative paths to /framework/spree from itself
tniezg Dec 13, 2021
6e6da1f
Merge pull request #23 from spree/use-relative-imports
tniezg Dec 13, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Available providers: local, bigcommerce, shopify, swell, saleor
# Available providers: local, bigcommerce, shopify, swell, saleor, spree
COMMERCE_PROVIDER=

BIGCOMMERCE_STOREFRONT_API_URL=
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Demo live at: [demo.vercel.store](https://demo.vercel.store/)
- Vendure Demo: https://vendure.vercel.store
- Saleor Demo: https://saleor.vercel.store/
- Ordercloud Demo: https://ordercloud.vercel.store/
- Spree Demo: https://spree.vercel.app/
tniezg marked this conversation as resolved.
Show resolved Hide resolved

## Features

Expand All @@ -28,7 +29,7 @@ Demo live at: [demo.vercel.store](https://demo.vercel.store/)

## Integrations

Next.js Commerce integrates out-of-the-box with BigCommerce, Shopify, Swell, Saleor and Vendure. We plan to support all major ecommerce backends.
Next.js Commerce integrates out-of-the-box with BigCommerce, Shopify, Swell, Saleor, Vendure and Spree. We plan to support all major ecommerce backends.

## Considerations

Expand Down
1 change: 1 addition & 0 deletions framework/commerce/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const PROVIDERS = [
'swell',
'vendure',
'ordercloud',
'spree',
]

function getProviderName() {
Expand Down
25 changes: 25 additions & 0 deletions framework/spree/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Template to be used for creating .env* files (.env, .env.local etc.) in the project's root directory.

COMMERCE_PROVIDER=spree

{# - NEXT_PUBLIC_* are exposed to the web browser and the server #}
NEXT_PUBLIC_SPREE_API_HOST=http://localhost:4000
NEXT_PUBLIC_SPREE_DEFAULT_LOCALE=en-us
NEXT_PUBLIC_SPREE_CART_COOKIE_NAME=spree_cart_token
{# -- cookie expire in days #}
NEXT_PUBLIC_SPREE_CART_COOKIE_EXPIRE=7
NEXT_PUBLIC_SPREE_USER_COOKIE_NAME=spree_user_token
NEXT_PUBLIC_SPREE_USER_COOKIE_EXPIRE=7
NEXT_PUBLIC_SPREE_IMAGE_HOST=http://localhost:4000
NEXT_PUBLIC_SPREE_ALLOWED_IMAGE_DOMAIN=localhost
NEXT_PUBLIC_SPREE_CATEGORIES_TAXONOMY_PERMALINK=categories
NEXT_PUBLIC_SPREE_BRANDS_TAXONOMY_PERMALINK=brands
NEXT_PUBLIC_SPREE_ALL_PRODUCTS_TAXONOMY_ID=false
NEXT_PUBLIC_SPREE_SHOW_SINGLE_VARIANT_OPTIONS=false
NEXT_PUBLIC_SPREE_LAST_UPDATED_PRODUCTS_PRERENDER_COUNT=10
NEXT_PUBLIC_SPREE_PRODUCT_PLACEHOLDER_IMAGE_URL=/product-img-placeholder.svg
NEXT_PUBLIC_SPREE_LINE_ITEM_PLACEHOLDER_IMAGE_URL=/product-img-placeholder.svg
NEXT_PUBLIC_SPREE_IMAGES_OPTION_FILTER=false
NEXT_PUBLIC_SPREE_IMAGES_SIZE=1000x1000
NEXT_PUBLIC_SPREE_IMAGES_QUALITY=100
NEXT_PUBLIC_SPREE_LOGIN_AFTER_SIGNUP=true
Binary file added framework/spree/README-assets/screenshots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions framework/spree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# [Spree Commerce][1] Provider

![Screenshots of Spree Commerce and NextJS Commerce][5]

An integration of [Spree Commerce](https://spreecommerce.org/) within NextJS Commerce. It supports browsing and searching Spree products and adding products to the cart.

**Demo**: [https://spree.vercel.app/][6]
tniezg marked this conversation as resolved.
Show resolved Hide resolved

## Installation

1. Setup Spree - [follow the Getting Started guide](https://dev-docs.spreecommerce.org/getting-started/installation).

1. Setup Nextjs Commerce - [instructions for setting up NextJS Commerce][2].

1. Copy the `.env.template` file in this directory (`/framework/spree`) to `.env.local` in the main directory

```bash
cp framework/spree/.env.template .env.local
```

1. Set `NEXT_PUBLIC_SPREE_CATEGORIES_TAXONOMY_PERMALINK` and `NEXT_PUBLIC_SPREE_BRANDS_TAXONOMY_PERMALINK` environment variables:

- They rely on [taxonomies'](https://dev-docs.spreecommerce.org/internals/products#taxons-and-taxonomies) permalinks in Spree.
- Go to the Spree admin panel and create `Categories` and `Brands` taxonomies if they don't exist and copy their permalinks into `.env.local` in NextJS Commerce.

1. Finally, run `yarn dev` :tada:

[1]: https://spreecommerce.org/
[2]: https://github.com/vercel/commerce
[3]: https://github.com/spree/spree_starter
[4]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
[5]: ./README-assets/screenshots.png
[6]: https://spree.vercel.app/
tniezg marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/cart/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/catalog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/catalog/products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
44 changes: 44 additions & 0 deletions framework/spree/api/endpoints/checkout/get-checkout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { CheckoutEndpoint } from '.'

const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
req: _request,
res: response,
config: _config,
}) => {
try {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkout</title>
</head>
<body>
<div style='margin: 10rem auto; text-align: center; font-family: SansSerif, "Segoe UI", Helvetica; color: #888;'>
<svg xmlns="http://www.w3.org/2000/svg" style='height: 60px; width: 60px;' fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
</svg>
<h1>Checkout not yet implemented :(</h1>
<p>
See <a href='https://github.com/vercel/commerce/issues/64' target='_blank'>#64</a>
</p>
</div>
</body>
</html>
`

response.status(200)
response.setHeader('Content-Type', 'text/html')
response.write(html)
response.end()
} catch (error) {
console.error(error)

const message = 'An unexpected error ocurred'

response.status(500).json({ data: null, errors: [{ message }] })
}
}

export default getCheckout
22 changes: 22 additions & 0 deletions framework/spree/api/endpoints/checkout/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createEndpoint } from '@commerce/api'
import type { GetAPISchema, CommerceAPI } from '@commerce/api'
import checkoutEndpoint from '@commerce/api/endpoints/checkout'
import type { CheckoutSchema } from '@commerce/types/checkout'
import getCheckout from './get-checkout'
import type { SpreeApiProvider } from '../..'

export type CheckoutAPI = GetAPISchema<
CommerceAPI<SpreeApiProvider>,
CheckoutSchema
>

export type CheckoutEndpoint = CheckoutAPI['endpoint']

export const handlers: CheckoutEndpoint['handlers'] = { getCheckout }

const checkoutApi = createEndpoint<CheckoutAPI>({
handler: checkoutEndpoint,
handlers,
})

export default checkoutApi
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/customer/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/customer/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/customer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/login/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/logout/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/signup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
1 change: 1 addition & 0 deletions framework/spree/api/endpoints/wishlist/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}
45 changes: 45 additions & 0 deletions framework/spree/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { CommerceAPI, CommerceAPIConfig } from '@commerce/api'
import { getCommerceApi as commerceApi } from '@commerce/api'
import createApiFetch from './utils/create-api-fetch'

import getAllPages from './operations/get-all-pages'
import getPage from './operations/get-page'
import getSiteInfo from './operations/get-site-info'
import getCustomerWishlist from './operations/get-customer-wishlist'
import getAllProductPaths from './operations/get-all-product-paths'
import getAllProducts from './operations/get-all-products'
import getProduct from './operations/get-product'

export interface SpreeApiConfig extends CommerceAPIConfig {}

const config: SpreeApiConfig = {
commerceUrl: '',
apiToken: '',
cartCookie: '',
customerCookie: '',
cartCookieMaxAge: 2592000,
fetch: createApiFetch(() => getCommerceApi().getConfig()),
}

const operations = {
getAllPages,
getPage,
getSiteInfo,
getCustomerWishlist,
getAllProductPaths,
getAllProducts,
getProduct,
}

export const provider = { config, operations }

export type SpreeApiProvider = typeof provider

export type SpreeApi<P extends SpreeApiProvider = SpreeApiProvider> =
CommerceAPI<P>

export function getCommerceApi<P extends SpreeApiProvider>(
customProvider: P = provider as any
): SpreeApi<P> {
return commerceApi(customProvider)
}
82 changes: 82 additions & 0 deletions framework/spree/api/operations/get-all-pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type {
OperationContext,
OperationOptions,
} from '@commerce/api/operations'
import type { GetAllPagesOperation, Page } from '@commerce/types/page'
import { requireConfigValue } from '@framework/isomorphic-config'
tniezg marked this conversation as resolved.
Show resolved Hide resolved
import normalizePage from '@framework/utils/normalizations/normalize-page'
import type { IPages } from '@spree/storefront-api-v2-sdk/types/interfaces/Page'
import type { SpreeSdkVariables } from '../../types'
import type { SpreeApiConfig, SpreeApiProvider } from '../index'

export default function getAllPagesOperation({
commerce,
}: OperationContext<SpreeApiProvider>) {
async function getAllPages<T extends GetAllPagesOperation>(options?: {
config?: Partial<SpreeApiConfig>
preview?: boolean
}): Promise<T['data']>

async function getAllPages<T extends GetAllPagesOperation>(
opts: {
config?: Partial<SpreeApiConfig>
preview?: boolean
} & OperationOptions
): Promise<T['data']>

async function getAllPages<T extends GetAllPagesOperation>({
config: userConfig,
preview,
query,
url,
}: {
url?: string
config?: Partial<SpreeApiConfig>
preview?: boolean
query?: string
} = {}): Promise<T['data']> {
console.info(
'getAllPages called. Configuration: ',
'query: ',
query,
'userConfig: ',
userConfig,
'preview: ',
preview,
'url: ',
url
)

const config = commerce.getConfig(userConfig)
const { fetch: apiFetch } = config

const variables: SpreeSdkVariables = {
methodPath: 'pages.list',
arguments: [
{
per_page: 500,
filter: {
locale_eq:
config.locale || (requireConfigValue('defaultLocale') as string),
},
},
],
}

const { data: spreeSuccessResponse } = await apiFetch<
IPages,
SpreeSdkVariables
>('__UNUSED__', {
variables,
})

const normalizedPages: Page[] = spreeSuccessResponse.data.map<Page>(
(spreePage) =>
normalizePage(spreeSuccessResponse, spreePage, config.locales || [])
)

return { pages: normalizedPages }
}

return getAllPages
}
Loading