UNPKG

@react-mvi/http

Version:

Http IO module for React MVI.

41 lines (40 loc) 1.23 kB
/** * @fileoverview IOのモッククラス定義 * @author Taketoshi Aono */ import { HttpHandler } from "./http-handler"; /** * Mock class for HttpRequest. */ export class HttpHandlerMock extends HttpHandler { /** * @param methods Definitions of each method return value. */ constructor(methods, advices) { super(advices); this.methods = methods; this.fetchFunction = (url, request) => { return new Promise((resolve, reject) => { setTimeout(() => { const method = this.methods[(request.method || "get").toLowerCase()]; if (typeof method === "function") { const fn = method; try { return resolve(fn(url, request)); } catch (e) { return reject(e); } } resolve(new Response(request.body, { status: 200, statusText: "OK" })); }, 100); }); }; } /** * Return whatwgFetch function mock. */ get fetch() { return this.fetchFunction; } }