Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

SDK: Standardize `expand` parameter to accept only `string[]` (#13719)

Damian Legawiec1f0ca97a4d29
diff --git a/packages/next/CHANGELOG.md b/packages/next/CHANGELOG.md
index c1b691ae9a..58b02d9025 100644
--- a/packages/next/CHANGELOG.md
+++ b/packages/next/CHANGELOG.md
@@ -1,5 +1,11 @@
# @spree/next
+## 0.6.7
+
+### Patch Changes
+
+- Standardize `expand` parameter to accept only `string[]`
+
## 0.6.6
### Patch Changes
diff --git a/packages/next/README.md b/packages/next/README.md
index 00976d9b5f..5b5e0cece1 100644
--- a/packages/next/README.md
+++ b/packages/next/README.md
@@ -93,7 +93,7 @@ Plain async functions for reading data in Server Components. Wrap with `"use cac
```typescript
import { listProducts, getProduct, getProductFilters } from '@spree/next';
-const products = await listProducts({ limit: 25, expand: 'variants,images' });
+const products = await listProducts({ limit: 25, expand: ['variants', 'images'] });
const product = await getProduct('spree-tote');
const filters = await getProductFilters({ taxon_id: 'txn_123' });
```
@@ -108,7 +108,7 @@ const taxons = await listTaxons({ depth_eq: 1 });
const taxon = await getTaxon('categories/clothing');
const products = await listTaxonProducts('categories/clothing', { limit: 12 });
-const taxonomies = await listTaxonomies({ expand: 'taxons' });
+const taxonomies = await listTaxonomies({ expand: ['taxons'] });
const taxonomy = await getTaxonomy('tax_123');
```
diff --git a/packages/next/package.json b/packages/next/package.json
index 1beb459da8..b1e277fea6 100644
--- a/packages/next/package.json
+++ b/packages/next/package.json
@@ -1,6 +1,6 @@
{
"name": "@spree/next",
- "version": "0.6.6",
+ "version": "0.6.7",
"description": "Next.js integration for Spree Commerce — server actions, caching, and cookie-based auth",
"type": "module",
"main": "./dist/index.js",
@@ -62,7 +62,7 @@
"url": "https://github.com/spree/spree/issues"
},
"peerDependencies": {
- "@spree/sdk": ">=0.6.6",
+ "@spree/sdk": ">=0.6.7",
"next": ">=15.0.0",
"react": ">=19.0.0"
},
diff --git a/packages/next/src/actions/checkout.ts b/packages/next/src/actions/checkout.ts
index 9e33171fd0..fd55ad7cf3 100644
--- a/packages/next/src/actions/checkout.ts
+++ b/packages/next/src/actions/checkout.ts
@@ -21,7 +21,7 @@ export async function getCheckout(
const options = await getCheckoutOptions();
return getClient().store.orders.get(
orderId,
- { expand: 'line_items,shipments,ship_address,bill_address' },
+ { expand: ['line_items', 'shipments', 'ship_address', 'bill_address'] },
options
);
}
diff --git a/packages/next/src/data/countries.ts b/packages/next/src/data/countries.ts
index 0fb2a34479..cc5ddb75cc 100644
--- a/packages/next/src/data/countries.ts
+++ b/packages/next/src/data/countries.ts
@@ -17,11 +17,11 @@ export async function listCountries(
/**
* Get a country by ISO code.
* @param iso - ISO 3166-1 alpha-2 code (e.g., "US", "DE")
- * @param params - Optional params (e.g., { expand: 'states' } or { expand: 'market' })
+ * @param params - Optional params (e.g., { expand: ['states'] } or { expand: ['market'] })
*/
export async function getCountry(
iso: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: SpreeNextOptions
): Promise<StoreCountry> {
const resolved = options ?? await getLocaleOptions();
diff --git a/packages/next/src/data/markets.ts b/packages/next/src/data/markets.ts
index db617ad553..43af88fc00 100644
--- a/packages/next/src/data/markets.ts
+++ b/packages/next/src/data/markets.ts
@@ -58,7 +58,7 @@ export async function listMarketCountries(
export async function getMarketCountry(
marketId: string,
iso: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: SpreeNextOptions
): Promise<StoreCountry> {
const resolved = options ?? await getLocaleOptions();
diff --git a/packages/next/src/data/products.ts b/packages/next/src/data/products.ts
index fc78da72c5..eaaac5942a 100644
--- a/packages/next/src/data/products.ts
+++ b/packages/next/src/data/products.ts
@@ -21,7 +21,7 @@ export async function listProducts(
*/
export async function getProduct(
slugOrId: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: SpreeNextOptions
): Promise<StoreProduct> {
const resolved = options ?? await getLocaleOptions();
diff --git a/packages/next/tests/actions/checkout.test.ts b/packages/next/tests/actions/checkout.test.ts
index 10cc3f04d7..df44823f78 100644
--- a/packages/next/tests/actions/checkout.test.ts
+++ b/packages/next/tests/actions/checkout.test.ts
@@ -59,7 +59,7 @@ describe('checkout actions', () => {
expect(result).toEqual(mockOrder);
expect(mockClient.store.orders.get).toHaveBeenCalledWith(
'1',
- { expand: 'line_items,shipments,ship_address,bill_address' },
+ { expand: ['line_items', 'shipments', 'ship_address', 'bill_address'] },
{ orderToken: 'order_token_123', token: 'jwt_token_abc' }
);
});
diff --git a/packages/next/tests/actions/orders.test.ts b/packages/next/tests/actions/orders.test.ts
index 9ebb6d5277..0a95ced906 100644
--- a/packages/next/tests/actions/orders.test.ts
+++ b/packages/next/tests/actions/orders.test.ts
@@ -92,11 +92,11 @@ describe('order actions', () => {
const mockOrder = { id: '1', number: 'R001' };
mockClient.store.orders.get.mockResolvedValue(mockOrder);
- const result = await getOrder('R001', { expand: 'line_items' });
+ const result = await getOrder('R001', { expand: ['line_items'] });
expect(result).toEqual(mockOrder);
expect(mockClient.store.orders.get).toHaveBeenCalledWith(
'R001',
- { expand: 'line_items' },
+ { expand: ['line_items'] },
expect.objectContaining({ token: expect.any(String) })
);
});
diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md
index a81d2e4117..3ec7ef4cda 100644
--- a/packages/sdk/CHANGELOG.md
+++ b/packages/sdk/CHANGELOG.md
@@ -1,5 +1,11 @@
# @spree/sdk
+## 0.6.7
+
+### Patch Changes
+
+- Standardize `expand` parameter to accept only `string[]`
+
## 0.6.6
### Patch Changes
diff --git a/packages/sdk/README.md b/packages/sdk/README.md
index d1ead41782..6b3a42a0d0 100644
--- a/packages/sdk/README.md
+++ b/packages/sdk/README.md
@@ -27,7 +27,7 @@ const client = createSpreeClient({
// Browse products (Store API)
const products = await client.store.products.list({
limit: 10,
- expand: 'variants,images',
+ expand: ['variants', 'images'],
});
// Get a single product
@@ -150,12 +150,12 @@ const products = await client.store.products.list({
limit: 25,
name_cont: 'shirt',
sort: 'price asc',
- expand: 'variants,images,taxons',
+ expand: ['variants', 'images', 'taxons'],
});
// Get single product by ID or slug
const product = await client.store.products.get('spree-tote', {
- expand: 'variants,images',
+ expand: ['variants', 'images'],
});
// Get available filters (price range, availability, options, taxons)
@@ -169,12 +169,12 @@ const filters = await client.store.products.filters({
```typescript
// List taxonomies
const taxonomies = await client.store.taxonomies.list({
- expand: 'taxons',
+ expand: ['taxons'],
});
// Get taxonomy with taxons
const categories = await client.store.taxonomies.get('tax_123', {
- expand: 'root,taxons',
+ expand: ['root', 'taxons'],
});
// List taxons with filtering
@@ -185,14 +185,14 @@ const taxons = await client.store.taxons.list({
// Get single taxon by ID or permalink
const taxon = await client.store.taxons.get('categories/clothing', {
- expand: 'ancestors,children', // For breadcrumbs and subcategories
+ expand: ['ancestors', 'children'], // For breadcrumbs and subcategories
});
// List products in a category
const categoryProducts = await client.store.taxons.products.list('categories/clothing', {
page: 1,
limit: 12,
- expand: 'images,default_variant',
+ expand: ['images', 'default_variant'],
});
```
@@ -225,7 +225,7 @@ const options = { orderToken: cart.order_token };
// Get order by ID or number
const order = await client.store.orders.get('R123456789', {
- expand: 'line_items,shipments',
+ expand: ['line_items', 'shipments'],
}, options);
// Update order (email, addresses)
@@ -448,7 +448,7 @@ const { data: wishlists } = await client.store.wishlists.list({}, options);
// Get wishlist by ID
const wishlist = await client.store.wishlists.get('wl_xxx', {
- expand: 'wished_items',
+ expand: ['wished_items'],
}, options);
// Create wishlist
diff --git a/packages/sdk/package.json b/packages/sdk/package.json
index 6743222245..5685e46e4f 100644
--- a/packages/sdk/package.json
+++ b/packages/sdk/package.json
@@ -1,6 +1,6 @@
{
"name": "@spree/sdk",
- "version": "0.6.6",
+ "version": "0.6.7",
"description": "Official TypeScript SDK for Spree Commerce API",
"type": "module",
"main": "./dist/index.cjs",
diff --git a/packages/sdk/src/store-client.ts b/packages/sdk/src/store-client.ts
index 307fa96f48..8a3bc2ed4e 100644
--- a/packages/sdk/src/store-client.ts
+++ b/packages/sdk/src/store-client.ts
@@ -1,5 +1,11 @@
import type { RequestFn, RequestOptions } from './request';
import { transformListParams } from './params';
+
+/** Join expand array into comma-separated string for the API */
+function expandParams(params?: { expand?: string[] }): Record<string, string> | undefined {
+ if (!params?.expand?.length) return undefined;
+ return { expand: params.expand.join(',') };
+}
import type {
AuthTokens,
LoginCredentials,
@@ -108,12 +114,12 @@ export class StoreClient {
*/
get: (
idOrSlug: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: RequestOptions
): Promise<StoreProduct> =>
this.request<StoreProduct>('GET', `/products/${idOrSlug}`, {
...options,
- params,
+ params: expandParams(params),
}),
/**
@@ -152,12 +158,12 @@ export class StoreClient {
*/
get: (
id: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: RequestOptions
): Promise<StoreTaxonomy> =>
this.request<StoreTaxonomy>('GET', `/taxonomies/${id}`, {
...options,
- params,
+ params: expandParams(params),
}),
};
@@ -179,12 +185,12 @@ export class StoreClient {
*/
get: (
idOrPermalink: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: RequestOptions
): Promise<StoreTaxon> =>
this.request<StoreTaxon>('GET', `/taxons/${idOrPermalink}`, {
...options,
- params,
+ params: expandParams(params),
}),
/**
@@ -230,12 +236,12 @@ export class StoreClient {
*/
get: (
iso: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: RequestOptions
): Promise<StoreCountry> =>
this.request<StoreCountry>('GET', `/countries/${iso}`, {
...options,
- params,
+ params: expandParams(params),
}),
};
@@ -309,13 +315,13 @@ export class StoreClient {
get: (
marketId: string,
iso: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: RequestOptions
): Promise<StoreCountry> =>
this.request<StoreCountry>(
'GET',
`/markets/${marketId}/countries/${iso}`,
- { ...options, params }
+ { ...options, params: expandParams(params) }
),
},
};
@@ -361,12 +367,12 @@ export class StoreClient {
*/
get: (
idOrNumber: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: RequestOptions
): Promise<StoreOrder> =>
this.request<StoreOrder>('GET', `/orders/${idOrNumber}`, {
...options,
- params,
+ params: expandParams(params),
}),
/**
@@ -911,12 +917,12 @@ export class StoreClient {
*/
get: (
id: string,
- params?: { expand?: string },
+ params?: { expand?: string[] },
options?: RequestOptions
): Promise<StoreWishlist> =>
this.request<StoreWishlist>('GET', `/wishlists/${id}`, {
...options,
- params,
+ params: expandParams(params),
}),
/**
diff --git a/packages/sdk/src/types/index.ts b/packages/sdk/src/types/index.ts
index 08515b6a2a..d2e84b4e19 100644
--- a/packages/sdk/src/types/index.ts
+++ b/packages/sdk/src/types/index.ts
@@ -57,8 +57,8 @@ export interface RegisterParams {
export interface ListParams {
page?: number;
limit?: number;
- /** Associations to expand. Accepts array or comma-separated string. */
- expand?: string[] | string;
+ /** Associations to expand, e.g. ['variants', 'images'] */
+ expand?: string[];
}
export interface ProductListParams extends ListParams {
diff --git a/packages/sdk/tests/params.test.ts b/packages/sdk/tests/params.test.ts
index 410599ea2a..6e84e452d5 100644
--- a/packages/sdk/tests/params.test.ts
+++ b/packages/sdk/tests/params.test.ts
@@ -7,16 +7,21 @@ describe('transformListParams', () => {
expect(result).toEqual({ page: 2, limit: 10 });
});
- it('passes through expand string unchanged', () => {
- const result = transformListParams({ expand: 'variants,images' });
- expect(result).toEqual({ expand: 'variants,images' });
- });
-
it('joins expand array into comma-separated string', () => {
const result = transformListParams({ expand: ['variants', 'images'] });
expect(result).toEqual({ expand: 'variants,images' });
});
+ it('joins single-element expand array', () => {
+ const result = transformListParams({ expand: ['variants'] });
+ expect(result).toEqual({ expand: 'variants' });
+ });
+
+ it('handles empty expand array', () => {
+ const result = transformListParams({ expand: [] });
+ expect(result).toEqual({ expand: '' });
+ });
+
it('passes through sort param unchanged', () => {
const result = transformListParams({ sort: 'price asc' });
expect(result).toEqual({ sort: 'price asc' });
@@ -77,7 +82,7 @@ describe('transformListParams', () => {
const result = transformListParams({
page: 1,
limit: 12,
- expand: 'images,default_variant',
+ expand: ['images', 'default_variant'],
sort: 'created_at desc',
name_cont: 'shirt',
price_gte: 20,
diff --git a/packages/sdk/tests/products.test.ts b/packages/sdk/tests/products.test.ts
index 082853b9d7..08d17a7aac 100644
--- a/packages/sdk/tests/products.test.ts
+++ b/packages/sdk/tests/products.test.ts
@@ -42,6 +42,42 @@ describe('products', () => {
const result = await client.store.products.get('prod_1');
expect(result.id).toBe('prod_1');
});
+
+ it('sends expand array as comma-separated query param', async () => {
+ let capturedUrl = '';
+ const { createSpreeClient } = await import('../src');
+ const customClient = createSpreeClient({
+ baseUrl: 'https://demo.spreecommerce.org',
+ publishableKey: 'test-key',
+ fetch: async (url, init) => {
+ capturedUrl = url.toString();
+ return globalThis.fetch(url, init);
+ },
+ });
+
+ await customClient.store.products.get('test-product', {
+ expand: ['variants', 'images'],
+ });
+
+ expect(capturedUrl).toContain('expand=variants%2Cimages');
+ });
+
+ it('omits expand param when array is empty', async () => {
+ let capturedUrl = '';
+ const { createSpreeClient } = await import('../src');
+ const customClient = createSpreeClient({
+ baseUrl: 'https://demo.spreecommerce.org',
+ publishableKey: 'test-key',
+ fetch: async (url, init) => {
+ capturedUrl = url.toString();
+ return globalThis.fetch(url, init);
+ },
+ });
+
+ await customClient.store.products.get('test-product', { expand: [] });
+
+ expect(capturedUrl).not.toContain('expand');
+ });
});
describe('filters', () => {