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.

Added an unchecked q to SDK list params for an app's own filters

Damian Legawiec8659851ad7e6
diff --git a/packages/sdk-core/src/params.ts b/packages/sdk-core/src/params.ts
index e064cf7af0..c42fd2de43 100644
--- a/packages/sdk-core/src/params.ts
+++ b/packages/sdk-core/src/params.ts
@@ -10,12 +10,16 @@ type ParamValue = string | number | boolean | (string | number)[] | undefined
*
* - `page`, `limit`, `expand`, `sort` pass through unchanged
* - Keys already in `q[...]` format pass through (backward compat)
+ * - The entries of `q` (filters the typed params do not list) are sent like
+ * any other filter; a filter given at the top level wins over the same key in `q`
* - All other keys are wrapped: `name_cont` → `q[name_cont]`
*/
export function transformListParams(params: object): Record<string, ParamValue> {
+ const { q, ...rest } = params as { q?: Record<string, unknown> }
+ const entries = [...Object.entries(q ?? {}), ...Object.entries(rest)]
const result: Record<string, ParamValue> = {}
- for (const [key, value] of Object.entries(params)) {
+ for (const [key, value] of entries) {
if (value === undefined) continue
if (PASSTHROUGH_KEYS.has(key)) {
diff --git a/packages/sdk-core/src/types.ts b/packages/sdk-core/src/types.ts
index d8bfa0c748..0de5b91bef 100644
--- a/packages/sdk-core/src/types.ts
+++ b/packages/sdk-core/src/types.ts
@@ -82,6 +82,11 @@ export type ListParams<
> = PageParams & {
/** Sort order. Prefix with - for descending, e.g. '-created_at', 'name'. Comma-separated for multiple fields. */
sort?: S
+ /**
+ * Filters the typed params do not list, such as ones your app adds, sent as
+ * given and not checked: `{ q: { erp_id_eq: 'ERP-1' } }`.
+ */
+ q?: Record<string, unknown>
} & F
// Address params
diff --git a/packages/sdk/tests/params.test.ts b/packages/sdk/tests/params.test.ts
index 657f24b81b..7ada72eed3 100644
--- a/packages/sdk/tests/params.test.ts
+++ b/packages/sdk/tests/params.test.ts
@@ -2,6 +2,23 @@ import { transformListParams } from '@spree/sdk-core'
import { describe, expect, it } from 'vitest'
describe('transformListParams', () => {
+ it('sends the entries of q as filters', () => {
+ const result = transformListParams({
+ name_cont: 'shirt',
+ q: { erp_id_eq: 'ERP-1', tags_in: ['a', 'b'] },
+ })
+ expect(result).toEqual({
+ 'q[erp_id_eq]': 'ERP-1',
+ 'q[tags_in][]': ['a', 'b'],
+ 'q[name_cont]': 'shirt',
+ })
+ })
+
+ it('prefers a typed filter over the same key in q', () => {
+ const result = transformListParams({ name_cont: 'typed', q: { name_cont: 'raw' } })
+ expect(result).toEqual({ 'q[name_cont]': 'typed' })
+ })
+
it('passes through pagination params unchanged', () => {
const result = transformListParams({ page: 2, limit: 10 })
expect(result).toEqual({ page: 2, limit: 10 })