msw#RequestHandler TypeScript Examples

The following examples show how to use msw#RequestHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From frontend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
export function mockAPI (url: string, method: HTTPMethods): RequestHandler {
  return rest[method](url, (req, res, ctx) =>
    loadFixture(req.url.pathname, req.method).then(data => res(
      ctx.status(200),
      ctx.json(data)
    ))
  )
}
Example #2
Source File: BitbucketDiscoveryProcessor.test.ts    From backstage with Apache License 2.0 5 votes vote down vote up
function setupStubs(
  projects: any[],
  bitbucketBaseUrl = `https://bitbucket.mycompany.com`,
) {
  function pagedResponse(values: any): PagedResponse<any> {
    return {
      values: values,
      isLastPage: true,
    } as PagedResponse<any>;
  }

  function stubbedProject(
    project: string,
    repos: string[],
  ): RequestHandler<any, any> {
    return rest.get(
      `${bitbucketBaseUrl}/api/rest/1.0/projects/${project}/repos`,
      (_, res, ctx) => {
        const response = [];
        for (const repo of repos) {
          response.push({
            slug: repo,
            links: {
              self: [
                {
                  href: `${bitbucketBaseUrl}/projects/${project}/repos/${repo}/browse`,
                },
              ],
            },
          });
        }
        return res(ctx.json(pagedResponse(response)));
      },
    );
  }

  server.use(
    rest.get(`${bitbucketBaseUrl}/api/rest/1.0/projects`, (_, res, ctx) => {
      return res(
        ctx.json(
          pagedResponse(
            projects.map(p => {
              return { key: p.key };
            }),
          ),
        ),
      );
    }),
  );

  for (const project of projects) {
    server.use(stubbedProject(project.key, project.repos));
  }
}