UNPKG

@panyam/tsutils

Version:

Some basic TS utils for personal use

83 lines 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Response = exports.Request = exports.URLBuilder = void 0; class URLBuilder { constructor(baseUrl) { this.params = []; this.baseUrl = baseUrl; } appendPath(path) { if (!this.baseUrl.endsWith("/") && !path.startsWith("/")) { this.baseUrl += "/"; } this.baseUrl += path; return this; } addParam(key, value) { this.params.push([key, value]); return this; } addParams(params) { for (const key in params) { this.addParam(key, params[key]); } return this; } build() { const url = this.baseUrl; const path = this.path; const params = this.params; const paramString = params .map((item, _index) => { const [key, value] = item; return encodeURIComponent(key) + "=" + encodeURIComponent(value); }) .join("&"); if (paramString.length > 0) { return url + (url.indexOf("?") >= 0 ? "&" : "?") + paramString; } else { return url; } } } exports.URLBuilder = URLBuilder; class Request { constructor(url, options) { this.url = url; this.options = options || {}; this.options.headers = this.options.headers || {}; this.options.body = this.options.body || null; } get method() { return this.options.method || "GET"; } set method(method) { this.options.method = method; } get body() { return this.options.body; } set body(body) { this.options.body = body; } get headers() { return this.options.headers; } get contentType() { return this.options.contentType; } } exports.Request = Request; class Response { constructor(status = 200, statusText = "", data = null) { this.headers = {}; this.data = null; this.error = null; this.status = status; this.statusText = statusText; this.data = data; } } exports.Response = Response; //# sourceMappingURL=net.js.map