Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function createHandlerWithError(
serviceName: string,
statusCode: number
): HandlerType {
return rest.post(createMask(serviceName), (_, res, ctx) => {
return res(
ctx.status(200),
ctx.set({
"content-length": "0",
"content-type": "application/grpc-web+proto",
"grpc-message": "Error Message",
"grpc-status": `${statusCode}`,
})
);
});
}
export function createHandler(
serviceName: string,
getResponseMessage: (requestData: Uint8Array) => T
): HandlerType {
return rest.post(
createMask(serviceName),
(req, res, ctx) => {
const arr: Uint8Array =
typeof req.body === "string" ? encoder.encode(req.body) : req.body;
const message = getResponseMessage(arr.slice(5));
const data = serialize(message.serializeBinary());
return res(
ctx.status(200),
ctx.set("Content-Type", "application/grpc-web+proto"),
ctx.body(data)
);
}
);
}
import { rest } from "msw";
import { serialize } from "./serializer";
import { createMask } from "./utils";
const DummyHandler = rest.post("", (_0, res, ctx) => {
return res(ctx.status(200));
});
type HandlerType = typeof DummyHandler;
const encoder = new TextEncoder();
export function createHandler(
serviceName: string,
getResponseMessage: (requestData: Uint8Array) => T
): HandlerType {
return rest.post(
createMask(serviceName),
(req, res, ctx) => {
const arr: Uint8Array =
typeof req.body === "string" ? encoder.encode(req.body) : req.body;
import { composeMocks, rest } from 'msw'
import userMocks from './user'
const { start } = composeMocks(
...userMocks,
rest.get('https://api.github.com/users/:username', (req, res, { json }) => {
const { username } = req.params
return res(
json({
avatar_url: 'https://i.imgflip.com/wnv7r.jpg',
login: username,
name: 'John Maverick',
public_repos: Math.ceil(Math.random() * 100),
}),
)
}),
)
start()
import { msw } from 'msw'
msw.get('https://api.github.com/users/:username', (req, res, { json }) => {
const { username } = req.params
return res(
json({
avatar_url: 'https://i.imgflip.com/wnv7r.jpg',
login: username,
name: 'John Maverick',
public_repos: Math.ceil(Math.random() * 100),
}),
)
})
msw.start()
import { msw } from 'msw'
msw.get('https://api.github.com/users/:username', (req, res, { json }) => {
const { username } = req.params
return res(
json({
avatar_url: 'https://i.imgflip.com/wnv7r.jpg',
login: username,
name: 'John Maverick',
public_repos: Math.ceil(Math.random() * 100),
}),
)
})
msw.start()
import { composeMocks, rest } from 'msw'
import userMocks from './user'
const { start } = composeMocks(
...userMocks,
rest.get('https://api.github.com/users/:username', (req, res, { json }) => {
const { username } = req.params
return res(
json({
avatar_url: 'https://i.imgflip.com/wnv7r.jpg',
login: username,
name: 'John Maverick',
public_repos: Math.ceil(Math.random() * 100),
}),
)
}),
)
start()