mswjs / data

Data modeling and relation library for testing JavaScript applications.

Home Page:https://npm.im/@mswjs/data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

toHandlers method does not use the full baseUrl argument

oscles opened this issue · comments

//database.js
export const db = factory({
  banner: {
   ...
  },
  contact: {
   ....
  },
});

// server-handlers.js
import { db } from 'test/database.js;
const baseUrl = 'http://localhost:8000/api';

const bannerHandlers = db.banner.toHandlers('rest', urlApi);
const contactHandlers = db.contact.toHandlers('rest', urlApi);

export const handlers = [...bannerHandlers, ...contactHandlers];

// test-server.js
import { handlers } from './server-handlers';
import { setupServer } from 'msw/node';

const server = setupServer(...handlers);

export * from 'msw';
export { server };

Throw the following alert:
image

This is the console output when printing console.log(db.banner.toHandlers('rest', urlApi))
image

does not add api segment to baseUrl

Hey, @oscles.

You are missing a trailing slash in your base URL in order for it to be taken fully when composing request handlers URL. Compare:

// http://localhost:8000/user/:id
new URL('user/:id', 'http://localhost:8000/api')

// http://localhost:8000/api/user/:id
new URL('user/:id', 'http://localhost:8000/api/')

The solution:

-const baseUrl = 'http://localhost:8000/api';
+const baseUrl = 'http://localhost:8000/api/';