scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
183 lines • 4.91 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var request_exports = {};
__export(request_exports, {
MockRequest: () => MockRequest
});
module.exports = __toCommonJS(request_exports);
var import_scriptable_abstract = require("scriptable-abstract");
var import_data = require("../data");
const DEFAULT_STATE = {
url: "",
method: "GET",
headers: {},
body: "",
timeoutInterval: 60,
allowInsecureRequest: false,
onRedirect: (request) => request,
response: {}
};
class MockRequest extends import_scriptable_abstract.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 import_data.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");
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MockRequest
});
//# sourceMappingURL=request.js.map