UNPKG

@react-mvi/http

Version:

Http IO module for React MVI.

64 lines (63 loc) 2.28 kB
"use strict"; /** * @fileoverview IOのモッククラス定義 * @author Taketoshi Aono */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var http_handler_1 = require("./http-handler"); /** * Mock class for HttpRequest. */ var HttpHandlerMock = /** @class */ (function (_super) { __extends(HttpHandlerMock, _super); /** * @param methods Definitions of each method return value. */ function HttpHandlerMock(methods, advices) { var _this = _super.call(this, advices) || this; _this.methods = methods; _this.fetchFunction = function (url, request) { return new Promise(function (resolve, reject) { setTimeout(function () { var method = _this.methods[(request.method || "get").toLowerCase()]; if (typeof method === "function") { var fn = method; try { return resolve(fn(url, request)); } catch (e) { return reject(e); } } resolve(new Response(request.body, { status: 200, statusText: "OK" })); }, 100); }); }; return _this; } Object.defineProperty(HttpHandlerMock.prototype, "fetch", { /** * Return whatwgFetch function mock. */ get: function () { return this.fetchFunction; }, enumerable: true, configurable: true }); return HttpHandlerMock; }(http_handler_1.HttpHandler)); exports.HttpHandlerMock = HttpHandlerMock;