You can define custom handlers for your custom endpoints or for existing endpoints (aka OCC Endpoints) of spartacus-mock.
Create a handlers file src/mock-server/handlers.ts
Create a folder src/mock-server/mock-data where all your mock-data lives
In the handlers file you can define all your custom handlers like in this example:
// src/mock-server/handlers.tsimport { getDefaultRoutes } from'@valantic/spartacus-mock';import { HttpHandler, HttpResponse, PathParams, StrictRequest, http,} from'msw';import { environment } from'../environments/environment';import { countryList } from'./mock-data/countries';// default routes defined in the spartacus-mock library// for custom routes, use your getRoutes function explained aboveconstdefaultRoutes=getDefaultRoutes(environment);exportconsthandlers= ():RestHandler[] => {return [http.get<{}, { foo:number; bar:string }>(defaultRoutes.languages, ({ request, params }) => {returnHttpResponse.json(languageList()); } ), ];};
Defining a custom handler for an existing route (aka OCC Endpoint) will override the default handler for this route.
Create a file src/mock-server/mock-data/languages.ts
Add a languageList function to return the mock-data for the languages endpoint:
// src/mock-server/mock-data/languages.tsimport { createLanguage } from'@valantic/spartacus-mock';import { Occ } from'@spartacus/core';// you can use the `createLanguage` function from our library or create your ownexportconstlanguageList= ():Occ.LanguageList=> {return { languages: [createLanguage({ isocode:'en', name:'English', nativeName:'English', }),createLanguage({ isocode:'de', name:'German', nativeName:'Deutsch', }), ], };};
Append your handlers to the mockConfig in your main.ts file