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
Fix RESTful routes for Store API customer
Damian Legawiec62b35cf171d3
diff --git a/packages/sdk/src/store-client.ts b/packages/sdk/src/store-client.tsindex b11e2ba189..969d64479f 100644--- a/packages/sdk/src/store-client.ts+++ b/packages/sdk/src/store-client.ts@@ -715,7 +715,7 @@ export class StoreClient {): Promise<PaginatedResponse<Address>> =>this.request<PaginatedResponse<Address>>('GET',- '/customer/addresses',+ '/customers/me/addresses',{ ...options, params: transformListParams({ ...params }) }),@@ -723,7 +723,7 @@ export class StoreClient {* Get an address by ID*/get: (id: string, options?: RequestOptions): Promise<Address> =>- this.request<Address>('GET', `/customer/addresses/${id}`, options),+ this.request<Address>('GET', `/customers/me/addresses/${id}`, options),/*** Create an address@@ -732,7 +732,7 @@ export class StoreClient {params: AddressParams,options?: RequestOptions): Promise<Address> =>- this.request<Address>('POST', '/customer/addresses', {+ this.request<Address>('POST', '/customers/me/addresses', {...options,body: params,}),@@ -745,7 +745,7 @@ export class StoreClient {params: Partial<AddressParams>,options?: RequestOptions): Promise<Address> =>- this.request<Address>('PATCH', `/customer/addresses/${id}`, {+ this.request<Address>('PATCH', `/customers/me/addresses/${id}`, {...options,body: params,}),@@ -754,7 +754,6 @@ export class StoreClient {* Delete an address*/delete: (id: string, options?: RequestOptions): Promise<void> =>- this.request<void>('DELETE', `/customer/addresses/${id}`, options),/*** Mark an address as default billing or shipping@@ -768,6 +767,7 @@ export class StoreClient {...options,body: { kind },}),+ this.request<void>('DELETE', `/customers/me/addresses/${id}`, options),},/**@@ -783,7 +783,7 @@ export class StoreClient {): Promise<PaginatedResponse<CreditCard>> =>this.request<PaginatedResponse<CreditCard>>('GET',- '/customer/credit_cards',+ '/customers/me/credit_cards',{ ...options, params: transformListParams({ ...params }) }),@@ -791,13 +791,13 @@ export class StoreClient {* Get a credit card by ID*/get: (id: string, options?: RequestOptions): Promise<CreditCard> =>- this.request<CreditCard>('GET', `/customer/credit_cards/${id}`, options),+ this.request<CreditCard>('GET', `/customers/me/credit_cards/${id}`, options),/*** Delete a credit card*/delete: (id: string, options?: RequestOptions): Promise<void> =>- this.request<void>('DELETE', `/customer/credit_cards/${id}`, options),+ this.request<void>('DELETE', `/customers/me/credit_cards/${id}`, options),},/**@@ -814,7 +814,7 @@ export class StoreClient {): Promise<PaginatedResponse<GiftCard>> =>this.request<PaginatedResponse<GiftCard>>('GET',- '/customer/gift_cards',+ '/customers/me/gift_cards',{ ...options, params: transformListParams({ ...params }) }),@@ -822,7 +822,7 @@ export class StoreClient {* Get a gift card by ID*/get: (id: string, options?: RequestOptions): Promise<GiftCard> =>- this.request<GiftCard>('GET', `/customer/gift_cards/${id}`, options),+ this.request<GiftCard>('GET', `/customers/me/gift_cards/${id}`, options),},/**@@ -836,7 +836,7 @@ export class StoreClient {params?: OrderListParams,options?: RequestOptions): Promise<PaginatedResponse<Order>> =>- this.request<PaginatedResponse<Order>>('GET', '/customer/orders', {+ this.request<PaginatedResponse<Order>>('GET', '/customers/me/orders', {...options,params: transformListParams({ ...params }),}),@@ -849,7 +849,7 @@ export class StoreClient {params?: { expand?: string[]; fields?: string[] },options?: RequestOptions): Promise<Order> =>- this.request<Order>('GET', `/customer/orders/${id}`, {+ this.request<Order>('GET', `/customers/me/orders/${id}`, {...options,params: getParams(params),}),@@ -869,7 +869,7 @@ export class StoreClient {): Promise<PaymentSetupSession> =>this.request<PaymentSetupSession>('POST',- '/customer/payment_setup_sessions',+ '/customers/me/payment_setup_sessions',{ ...options, body: params }),@@ -877,7 +877,7 @@ export class StoreClient {* Get a payment setup session by ID*/get: (id: string, options?: RequestOptions): Promise<PaymentSetupSession> =>- this.request<PaymentSetupSession>('GET', `/customer/payment_setup_sessions/${id}`, options),+ this.request<PaymentSetupSession>('GET', `/customers/me/payment_setup_sessions/${id}`, options),/*** Complete a payment setup session@@ -890,7 +890,7 @@ export class StoreClient {): Promise<PaymentSetupSession> =>this.request<PaymentSetupSession>('PATCH',- `/customer/payment_setup_sessions/${id}/complete`,+ `/customers/me/payment_setup_sessions/${id}/complete`,{ ...options, body: params }),},@@ -904,7 +904,7 @@ export class StoreClient {* Always succeeds (202) to prevent email enumeration.*/create: (params: RequestPasswordResetParams): Promise<{ message: string }> =>- this.request<{ message: string }>('POST', '/customer/password_resets', {+ this.request<{ message: string }>('POST', '/customers/me/password_resets', {body: params,}),@@ -919,7 +919,7 @@ export class StoreClient {): Promise<AuthTokens> =>this.request<AuthTokens>('PATCH',- `/customer/password_resets/${token}`,+ `/customers/me/password_resets/${token}`,{ body: params }),},diff --git a/packages/sdk/tests/customer.test.ts b/packages/sdk/tests/customer.test.tsindex 352365f7a7..557ccc568e 100644--- a/packages/sdk/tests/customer.test.ts+++ b/packages/sdk/tests/customer.test.ts@@ -31,7 +31,7 @@ describe('customer', () => {it('sends phone, accepts_email_marketing, and metadata', async () => {let capturedBody: Record<string, unknown> = {};server.use(- http.patch(`${API_PREFIX}/customer`, async ({ request }) => {+ http.patch(`${API_PREFIX}/customers/me`, async ({ request }) => {capturedBody = await request.json() as Record<string, unknown>;return HttpResponse.json({ ...fixtures.user, phone: '+1234567890', accepts_email_marketing: true });})diff --git a/packages/sdk/tests/error-handling.test.ts b/packages/sdk/tests/error-handling.test.tsindex 7aa2cff857..2eb3b16731 100644--- a/packages/sdk/tests/error-handling.test.ts+++ b/packages/sdk/tests/error-handling.test.ts@@ -38,7 +38,7 @@ describe('error handling', () => {it('throws SpreeError with details on 422', async () => {server.use(- http.post(`${API_PREFIX}/customer/addresses`, () =>+ http.post(`${API_PREFIX}/customers/me/addresses`, () =>HttpResponse.json({error: {@@ -98,7 +98,7 @@ describe('error handling', () => {it('throws SpreeError on 401 unauthorized', async () => {server.use(- http.get(`${API_PREFIX}/customer`, () =>+ http.get(`${API_PREFIX}/customers/me`, () =>HttpResponse.json({error: {diff --git a/packages/sdk/tests/mocks/handlers.ts b/packages/sdk/tests/mocks/handlers.tsindex 14e2569b3e..aabab18204 100644--- a/packages/sdk/tests/mocks/handlers.ts+++ b/packages/sdk/tests/mocks/handlers.ts@@ -411,68 +411,68 @@ export const handlers = [),// Customer- http.get(`${API_PREFIX}/customer`, () =>+ http.get(`${API_PREFIX}/customers/me`, () =>HttpResponse.json(fixtures.user)),- http.patch(`${API_PREFIX}/customer`, () =>+ http.patch(`${API_PREFIX}/customers/me`, () =>HttpResponse.json({ ...fixtures.user, first_name: 'Updated' })),// Customer > Orders- http.get(`${API_PREFIX}/customer/orders`, () =>+ http.get(`${API_PREFIX}/customers/me/orders`, () =>HttpResponse.json({ data: [fixtures.order], meta: paginationMeta })),- http.get(`${API_PREFIX}/customer/orders/:id`, () =>+ http.get(`${API_PREFIX}/customers/me/orders/:id`, () =>HttpResponse.json(fixtures.order)),// Customer > Addresses- http.get(`${API_PREFIX}/customer/addresses`, () =>+ http.get(`${API_PREFIX}/customers/me/addresses`, () =>HttpResponse.json({ data: [fixtures.address], meta: paginationMeta })),- http.get(`${API_PREFIX}/customer/addresses/:id`, () =>+ http.get(`${API_PREFIX}/customers/me/addresses/:id`, () =>HttpResponse.json(fixtures.address)),- http.post(`${API_PREFIX}/customer/addresses`, () =>+ http.post(`${API_PREFIX}/customers/me/addresses`, () =>HttpResponse.json(fixtures.address)),- http.patch(`${API_PREFIX}/customer/addresses/:id`, () =>+ http.patch(`${API_PREFIX}/customers/me/addresses/:id`, () =>HttpResponse.json({ ...fixtures.address, city: 'Updated City' })),- http.delete(`${API_PREFIX}/customer/addresses/:id`, () =>+ http.delete(`${API_PREFIX}/customers/me/addresses/:id`, () =>new HttpResponse(null, { status: 204 })),// Customer > Credit Cards- http.get(`${API_PREFIX}/customer/credit_cards`, () =>+ http.get(`${API_PREFIX}/customers/me/credit_cards`, () =>HttpResponse.json({ data: [], meta: paginationMeta })),- http.get(`${API_PREFIX}/customer/credit_cards/:id`, () =>+ http.get(`${API_PREFIX}/customers/me/credit_cards/:id`, () =>HttpResponse.json({ id: 'cc_1', last4: '1234' })),- http.delete(`${API_PREFIX}/customer/credit_cards/:id`, () =>+ http.delete(`${API_PREFIX}/customers/me/credit_cards/:id`, () =>new HttpResponse(null, { status: 204 })),// Customer > Gift Cards- http.get(`${API_PREFIX}/customer/gift_cards`, () =>+ http.get(`${API_PREFIX}/customers/me/gift_cards`, () =>HttpResponse.json({ data: [], meta: paginationMeta })),- http.get(`${API_PREFIX}/customer/gift_cards/:id`, () =>+ http.get(`${API_PREFIX}/customers/me/gift_cards/:id`, () =>HttpResponse.json({ id: 'gc_1', code: 'GIFT123', balance: '50.00' })),// Customer > Payment Setup Sessions- http.post(`${API_PREFIX}/customer/payment_setup_sessions`, () =>+ http.post(`${API_PREFIX}/customers/me/payment_setup_sessions`, () =>HttpResponse.json({id: 'pss_1',status: 'pending',@@ -489,7 +489,7 @@ export const handlers = [}, { status: 201 })),- http.get(`${API_PREFIX}/customer/payment_setup_sessions/:id`, () =>+ http.get(`${API_PREFIX}/customers/me/payment_setup_sessions/:id`, () =>HttpResponse.json({id: 'pss_1',status: 'pending',@@ -506,7 +506,7 @@ export const handlers = [})),- http.patch(`${API_PREFIX}/customer/payment_setup_sessions/:id/complete`, () =>+ http.patch(`${API_PREFIX}/customers/me/payment_setup_sessions/:id/complete`, () =>HttpResponse.json({id: 'pss_1',status: 'completed',@@ -524,14 +524,14 @@ export const handlers = [),// Customer > Password Resets- http.post(`${API_PREFIX}/customer/password_resets`, () =>+ http.post(`${API_PREFIX}/customers/me/password_resets`, () =>HttpResponse.json({ message: 'If an account exists for that email, password reset instructions have been sent.' },{ status: 202 })),- http.patch(`${API_PREFIX}/customer/password_resets/:token`, () =>+ http.patch(`${API_PREFIX}/customers/me/password_resets/:token`, () =>HttpResponse.json({ token: 'new-jwt-token', user: fixtures.user })),diff --git a/packages/sdk/tests/password-resets.test.ts b/packages/sdk/tests/password-resets.test.tsindex 9fed95f6e8..3977a0d721 100644--- a/packages/sdk/tests/password-resets.test.ts+++ b/packages/sdk/tests/password-resets.test.ts@@ -19,7 +19,7 @@ describe('customer.passwordResets', () => {it('sends email in request body', async () => {let capturedBody: Record<string, unknown> = {};server.use(- http.post(`${API_PREFIX}/customer/password_resets`, async ({ request }) => {+ http.post(`${API_PREFIX}/customers/me/password_resets`, async ({ request }) => {capturedBody = await request.json() as Record<string, unknown>;return HttpResponse.json({ message: 'If an account exists for that email, password reset instructions have been sent.' },@@ -53,7 +53,7 @@ describe('customer.passwordResets', () => {it('sends token in URL path', async () => {let capturedUrl = '';server.use(- http.patch(`${API_PREFIX}/customer/password_resets/:token`, ({ request }) => {+ http.patch(`${API_PREFIX}/customers/me/password_resets/:token`, ({ request }) => {capturedUrl = request.url;return HttpResponse.json({ token: 'new-jwt-token', user: { id: 'user_1', email: '[email protected]', first_name: null, last_name: null } });})@@ -65,12 +65,12 @@ describe('customer.passwordResets', () => {password_confirmation: 'newsecurepassword',});- expect(capturedUrl).toContain('/customer/password_resets/my-reset-token');+ expect(capturedUrl).toContain('/customers/me/password_resets/my-reset-token');});it('throws SpreeError on invalid token', async () => {server.use(- http.patch(`${API_PREFIX}/customer/password_resets/:token`, () =>+ http.patch(`${API_PREFIX}/customers/me/password_resets/:token`, () =>HttpResponse.json({ error: { code: 'password_reset_token_invalid', message: 'Password reset token is invalid or has expired.' } },{ status: 422 }diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex f700e0b5f1..5a8ee97571 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -58,8 +58,11 @@ Spree::Core::Engine.add_routes doget 'customer', to: 'customers#show'patch 'customer', to: 'customers#update'- # Customer nested resources- namespace :customer, path: 'customer' do+ # Current customer profile and nested resources (/customers/me/...)+ namespace :customer, path: 'customers/me' do+ get '/', action: :show, controller: '/spree/api/v3/store/customers'+ patch '/', action: :update, controller: '/spree/api/v3/store/customers'+resources :password_resets, only: [:create, :update]resources :orders, only: [:index, :show]diff --git a/spree/api/spec/controllers/concerns/spree/api/v3/jwt_authentication_spec.rb b/spree/api/spec/controllers/concerns/spree/api/v3/jwt_authentication_spec.rbindex ae823c3786..e71c58ad13 100644--- a/spree/api/spec/controllers/concerns/spree/api/v3/jwt_authentication_spec.rb+++ b/spree/api/spec/controllers/concerns/spree/api/v3/jwt_authentication_spec.rb@@ -2,7 +2,7 @@ require 'spec_helper'# Test JWT authentication concerns.# Token generation tested via AuthController (login).-# Token verification/extraction tested via CustomersController (GET /customer requires JWT).+# Token verification/extraction tested via CustomersController (GET /customers/me requires JWT).RSpec.describe 'Spree::Api::V3::JwtAuthentication' dodescribe 'token generation' do# Use AuthController for testing token generation (login creates JWT)@@ -43,7 +43,7 @@ RSpec.describe 'Spree::Api::V3::JwtAuthentication' doit 'extracts token from Authorization Bearer header', type: :request dojwt = generate_jwt(user)- get '/api/v3/store/customer', headers: headers.merge('Authorization' => "Bearer #{jwt}")+ get '/api/v3/store/customers/me', headers: headers.merge('Authorization' => "Bearer #{jwt}")expect(response).to have_http_status(:ok)end@@ -54,7 +54,7 @@ RSpec.describe 'Spree::Api::V3::JwtAuthentication' doRails.application.secret_key_base,'HS256')- get '/api/v3/store/customer', headers: headers.merge('Authorization' => "Bearer #{bad_token}")+ get '/api/v3/store/customers/me', headers: headers.merge('Authorization' => "Bearer #{bad_token}")expect(response).to have_http_status(:unauthorized)end@@ -65,21 +65,21 @@ RSpec.describe 'Spree::Api::V3::JwtAuthentication' doRails.application.secret_key_base,'HS256')- get '/api/v3/store/customer', headers: headers.merge('Authorization' => "Bearer #{bad_token}")+ get '/api/v3/store/customers/me', headers: headers.merge('Authorization' => "Bearer #{bad_token}")expect(response).to have_http_status(:unauthorized)endit 'rejects expired tokens', type: :request doexpired_token = generate_jwt(user, expiration: -1.hour.to_i)- get '/api/v3/store/customer', headers: headers.merge('Authorization' => "Bearer #{expired_token}")+ get '/api/v3/store/customers/me', headers: headers.merge('Authorization' => "Bearer #{expired_token}")expect(response).to have_http_status(:unauthorized)endit 'does not extract token from query params on non-digitals controllers', type: :request dojwt = generate_jwt(user)- get '/api/v3/store/customer', params: { token: jwt }, headers: headers+ get '/api/v3/store/customers/me', params: { token: jwt }, headers: headersexpect(response).to have_http_status(:unauthorized)enddiff --git a/spree/api/spec/integration/spree/api/v3/store/addresses_spec.rb b/spree/api/spec/integration/spree/api/v3/store/addresses_spec.rbindex 6ca198a774..3e7f3c22be 100644--- a/spree/api/spec/integration/spree/api/v3/store/addresses_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/store/addresses_spec.rb@@ -9,7 +9,7 @@ RSpec.describe 'Addresses API', type: :request, swagger_doc: 'api-reference/storlet!(:state) { create(:state, country: country) }let!(:address) { create(:address, user: user, country: country, state: state) }- path '/api/v3/store/customer/addresses' do+ path '/api/v3/store/customers/me/addresses' doget 'List customer addresses' dotags 'Customers'produces 'application/json'@@ -136,7 +136,7 @@ RSpec.describe 'Addresses API', type: :request, swagger_doc: 'api-reference/storendend- path '/api/v3/store/customer/addresses/{id}' do+ path '/api/v3/store/customers/me/addresses/{id}' doget 'Get an address' dotags 'Customers'produces 'application/json'diff --git a/spree/api/spec/integration/spree/api/v3/store/credit_cards_spec.rb b/spree/api/spec/integration/spree/api/v3/store/credit_cards_spec.rbindex 63dedc9caf..041d7fc7ca 100644--- a/spree/api/spec/integration/spree/api/v3/store/credit_cards_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/store/credit_cards_spec.rb@@ -7,7 +7,7 @@ RSpec.describe 'Credit Cards API', type: :request, swagger_doc: 'api-reference/slet!(:credit_card) { create(:credit_card, user: user) }- path '/api/v3/store/customer/credit_cards' do+ path '/api/v3/store/customers/me/credit_cards' doget 'List saved credit cards' dotags 'Customers'produces 'application/json'@@ -55,7 +55,7 @@ RSpec.describe 'Credit Cards API', type: :request, swagger_doc: 'api-reference/sendend- path '/api/v3/store/customer/credit_cards/{id}' do+ path '/api/v3/store/customers/me/credit_cards/{id}' doget 'Get a credit card' dotags 'Customers'produces 'application/json'diff --git a/spree/api/spec/integration/spree/api/v3/store/customer_orders_spec.rb b/spree/api/spec/integration/spree/api/v3/store/customer_orders_spec.rbindex 8aca1d8943..0763b6b6e5 100644--- a/spree/api/spec/integration/spree/api/v3/store/customer_orders_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/store/customer_orders_spec.rb@@ -7,7 +7,7 @@ RSpec.describe 'Customer Orders API', type: :request, swagger_doc: 'api-referenclet(:user) { create(:user_with_addresses) }- path '/api/v3/store/customer/orders' do+ path '/api/v3/store/customers/me/orders' doget 'List orders' dotags 'Customer'produces 'application/json'@@ -48,7 +48,7 @@ RSpec.describe 'Customer Orders API', type: :request, swagger_doc: 'api-referencendend- path '/api/v3/store/customer/orders/{id}' do+ path '/api/v3/store/customers/me/orders/{id}' doget 'Get an order' dotags 'Customer'produces 'application/json'diff --git a/spree/api/spec/integration/spree/api/v3/store/customers_spec.rb b/spree/api/spec/integration/spree/api/v3/store/customers_spec.rbindex 45b69fe5ba..702bcf186a 100644--- a/spree/api/spec/integration/spree/api/v3/store/customers_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/store/customers_spec.rb@@ -94,7 +94,7 @@ RSpec.describe 'Customers API', type: :request, swagger_doc: 'api-reference/storendend- path '/api/v3/store/customer' do+ path '/api/v3/store/customers/me' doget 'Get current customer profile' dotags 'Customers'produces 'application/json'diff --git a/spree/api/spec/integration/spree/api/v3/store/gift_cards_spec.rb b/spree/api/spec/integration/spree/api/v3/store/gift_cards_spec.rbindex 89dd81c856..7a450acd48 100644--- a/spree/api/spec/integration/spree/api/v3/store/gift_cards_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/store/gift_cards_spec.rb@@ -7,7 +7,7 @@ RSpec.describe 'Gift Cards API', type: :request, swagger_doc: 'api-reference/stolet!(:gift_card) { create(:gift_card, user: user, store: store) }- path '/api/v3/store/customer/gift_cards' do+ path '/api/v3/store/customers/me/gift_cards' doget 'List gift cards' dotags 'Customers'produces 'application/json'@@ -55,7 +55,7 @@ RSpec.describe 'Gift Cards API', type: :request, swagger_doc: 'api-reference/stoendend- path '/api/v3/store/customer/gift_cards/{id}' do+ path '/api/v3/store/customers/me/gift_cards/{id}' doget 'Get a gift card' dotags 'Customers'produces 'application/json'diff --git a/spree/api/spec/integration/spree/api/v3/store/payment_setup_sessions_spec.rb b/spree/api/spec/integration/spree/api/v3/store/payment_setup_sessions_spec.rbindex 9390faee2a..9cce410ace 100644--- a/spree/api/spec/integration/spree/api/v3/store/payment_setup_sessions_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/store/payment_setup_sessions_spec.rb@@ -13,7 +13,7 @@ RSpec.describe 'Payment Setup Sessions API', type: :request, swagger_doc: 'api-rexternal_data: { 'client_secret' => 'secret_123' })end- path '/api/v3/store/customer/payment_setup_sessions' do+ path '/api/v3/store/customers/me/payment_setup_sessions' dopost 'Create payment setup session' dotags 'Customers'consumes 'application/json'@@ -75,7 +75,7 @@ RSpec.describe 'Payment Setup Sessions API', type: :request, swagger_doc: 'api-rendend- path '/api/v3/store/customer/payment_setup_sessions/{id}' do+ path '/api/v3/store/customers/me/payment_setup_sessions/{id}' doparameter name: 'x-spree-api-key', in: :header, type: :string, required: trueparameter name: 'Authorization', in: :header, type: :string, required: trueparameter name: :id, in: :path, type: :string, required: true,@@ -118,7 +118,7 @@ RSpec.describe 'Payment Setup Sessions API', type: :request, swagger_doc: 'api-rendend- path '/api/v3/store/customer/payment_setup_sessions/{id}/complete' do+ path '/api/v3/store/customers/me/payment_setup_sessions/{id}/complete' doparameter name: 'x-spree-api-key', in: :header, type: :string, required: trueparameter name: 'Authorization', in: :header, type: :string, required: trueparameter name: :id, in: :path, type: :string, required: true,