UNPKG

robert

Version:

A generic shitty http client in nod.js

124 lines (123 loc) 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const robert_util_1 = require("robert-util"); const response_1 = require("./response"); function default_1(method, url, options) { let body; return { full() { options.full = !options.full; return this; }, format(format) { options.format = format; return this; }, port(port) { options.port = port; return this; }, redirects(redirects) { options.redirects = redirects; return this; }, size(size) { options.size = (0, robert_util_1.parseSize)(size); return this; }, timeout(time) { options.timeout = (0, robert_util_1.parseTime)(time); return this; }, query(key, value) { // @ts-ignore still the same fucking issue options.query.append(key, value); return this; }, setQuery(query) { // @ts-ignore yeah options.query = new URLSearchParams(query); return this; }, addQuery(query) { // @ts-ignore h const entries = new URLSearchParams(query).entries(); for (const [key, value] of entries) options.query.append(key, value); return this; }, delQuery(key) { // @ts-ignore fucking options.query.delete(key); return this; }, header(key, value) { options.headers[key] = value; return this; }, setHeaders(headers) { options.headers = headers; return this; }, addHeaders(headers) { Object.assign(options.headers, headers); return this; }, delHeader(key) { delete options.headers[key]; return this; }, auth(value) { this.header("authorization", value); return this; }, agent(value) { this.header("user-agent", value); return this; }, contentType(value) { this.header("content-type", value); return this; }, contentLength(value = this.body.length) { this.header("content-length", value); return this; }, formData(formData) { Object.assign(options.headers, formData.getHeaders()); body = formData; return this; }, stream(stream) { body = stream; return this; }, buffer(buffer) { body = buffer; return this; }, text(text) { this.contentType("text/plain"); body = text; return this; }, json(json) { this.contentType("application/json"); body = JSON.stringify(json); return this; }, form(form) { this.contentType("application/x-www-form-urlencoded"); // @ts-ignore h body = new URLSearchParams(form).toString(); return this; }, send(format = options.format, full = options.full) { options.format = format; options.full = full; url.search = options.query.toString(); return (0, response_1.default)(method, url, body, options); }, }; } exports.default = default_1;