UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

160 lines 3.85 kB
import { AbsRequest } from "scriptable-abstract"; import { MockData } from "../data"; const DEFAULT_STATE = { url: "", method: "GET", headers: {}, body: "", timeoutInterval: 60, allowInsecureRequest: false, onRedirect: (request) => request, response: {} }; class MockRequest extends AbsRequest { constructor() { super(DEFAULT_STATE); } get url() { return this.state.url; } set url(value) { if (value === null) { throw new Error("URL cannot be null"); } if (typeof value !== "string") { throw new Error("URL must be a string"); } this.setState({ url: value }); } get method() { return this.state.method; } set method(value) { if (value === null) { throw new Error("Method cannot be null"); } const validMethods = ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"]; if (!validMethods.includes(value)) { throw new Error("Invalid HTTP method"); } this.setState({ method: value }); } get headers() { return { ...this.state.headers }; } set headers(value) { if (value === null) { throw new Error("Headers cannot be null"); } if (typeof value !== "object") { throw new Error("Headers must be an object"); } this.setState({ headers: { ...value } }); } get body() { return this.state.body; } set body(value) { if (value === null) { throw new Error("Body cannot be null"); } this.setState({ body: value }); } get timeoutInterval() { return this.state.timeoutInterval; } set timeoutInterval(value) { if (value < 0) { throw new Error("Timeout must be non-negative"); } if (Number.isNaN(value)) { throw new Error("Timeout must be a valid number"); } this.setState({ timeoutInterval: value }); } get allowInsecureRequest() { return this.state.allowInsecureRequest; } set allowInsecureRequest(value) { if (value === null) { throw new Error("allowInsecureRequest must be a boolean"); } this.setState({ allowInsecureRequest: value }); } get onRedirect() { return this.state.onRedirect; } set onRedirect(value) { this.setState({ onRedirect: value }); } get response() { return this.state.response; } addHeaderField(name, value) { if (name === null) { throw new Error("Header name cannot be null"); } if (name === "") { throw new Error("Header name cannot be empty"); } if (value === null) { throw new Error("Header value cannot be null"); } const headers = new Map(Object.entries(this.state.headers)); headers.set(name, value); this.setState({ headers: Object.fromEntries(headers) }); } async load() { if (!this.url || this.url === "invalid-url") { throw new Error("Invalid URL"); } if (this.url.includes("redirect.example.com")) { const redirectRequest = new MockRequest(); redirectRequest.url = "https://example.com/redirected"; return this.onRedirect(redirectRequest); } if (this.url.includes("/error")) { throw new Error("Server error"); } this.setState({ response: { statusCode: 200, headers: { "Content-Type": "text/plain" } } }); return MockData.fromString(""); } async loadString() { this.setState({ response: { statusCode: 200, headers: { "Content-Type": "text/plain" } } }); return ""; } async loadJSON() { this.setState({ response: { statusCode: 200, headers: { "Content-Type": "application/json" } } }); return {}; } async loadImage() { throw new Error("Not implemented"); } } export { MockRequest }; //# sourceMappingURL=request.js.map