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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
import { SpreeError, type StoreUpdateParams } from '@spree/admin-sdk'
import {
adminClient,
STORE_QUERY_RESOURCE,
useResourceKey,
useResourceMutation,
} from '@spree/dashboard-core'
import { useQuery } from '@tanstack/react-query'
import { TRANSLATIONS_QUERY_RESOURCE } from './use-translations'
// Reads the same cache entry `<StoreProvider>` maintains — settings pages and
// the provider (nav badge, pickers, Getting Started) always agree.
export function useStoreSettings() {
return useQuery({
queryKey: useResourceKey(STORE_QUERY_RESOURCE),
queryFn: () => adminClient.store.get(),
})
}
/**
* The pricing and inventory engines this store can choose between. Static for
* the life of a deploy — the list changes only when a connector gem is
* installed or an integration is connected — so it is cached generously.
*/
export function useStoreDataSources() {
return useQuery({
queryKey: useResourceKey(STORE_QUERY_RESOURCE, 'data-sources'),
queryFn: () => adminClient.store.dataSources(),
staleTime: 5 * 60 * 1000,
})
}
/**
* How this marketplace can pay its sellers.
*
* Its own query rather than part of the data sources: a payout provider is
* not a source of catalog data, and choosing one changes what sellers must
* do before they can be paid.
*/
export function usePayoutProviders() {
return useQuery({
queryKey: useResourceKey(STORE_QUERY_RESOURCE, 'payout-providers'),
queryFn: () => adminClient.payoutProviders.list(),
staleTime: 5 * 60 * 1000,
})
}
export function useUpdateStoreSettings() {
return useResourceMutation<unknown, Error, StoreUpdateParams>({
mutationFn: (params) => adminClient.store.update(params),
invalidate: [[STORE_QUERY_RESOURCE], [TRANSLATIONS_QUERY_RESOURCE, 'store']],
successMessage: false,
errorMessage: false,
})
}
/**
* Saves the storefront URL and allows its origin in one step — the action
* that completes the `setup_storefront` task.
*/
export function useConnectStorefront() {
return useResourceMutation<string, Error, string>({
mutationFn: async (origin) => {
// Origin first: it's idempotent (a 422 means already allowed), and if
// it fails hard nothing has been persisted yet — whereas saving the URL
// first and failing here would complete the setup task without CORS
// access AND skip the invalidation of the already-changed store.
try {
await adminClient.allowedOrigins.create({ origin })
} catch (error) {
if (!(error instanceof SpreeError) || error.status !== 422) throw error
}
await adminClient.store.update({ storefront_url: origin })
return origin
},
invalidate: [[STORE_QUERY_RESOURCE], ['allowed-origins']],
successMessage: false,
errorMessage: false,
})
}