UNPKG

nyro

Version:

A simple and effective promise-based HTTP & HTTP/2 request library that supports all HTTP methods.

101 lines (100 loc) 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Headers { headers = {}; constructor(headers) { if (headers) { this.setFromObject(headers); } } toJSON() { return this.headers; } ; set(key, value) { if (!this.headers[key]) { this.headers[key] = value; } return this; } get(key) { return this.headers[key]; } delete(key) { delete this.headers[key]; return this; } all() { return this.headers; } clear() { this.headers = {}; return this; } has(key) { return this.headers.hasOwnProperty(key); } setFromObject(headers) { this.headers = { ...this.headers, ...headers }; return this; } setFromHeaders(headers) { this.setFromObject(headers.all()); return this; } setUserAgent(userAgent) { return this.set('User-Agent', userAgent); } setContentType(contentType) { return this.set('Content-Type', contentType); } setAccept(accept) { return this.set('Accept', accept); } setAuthorization(authorization) { return this.set('Authorization', authorization); } setBearerToken(token) { return this.setAuthorization(`Bearer ${token}`); } setBasicAuth(username, password) { return this.setAuthorization(`Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`); } setBearerAuth(token) { return this.setAuthorization(`Bearer ${token}`); } setReferer(referer) { return this.set('Referer', referer); } setOrigin(origin) { return this.set('Origin', origin); } setHost(host) { return this.set('Host', host); } setConnection(connection) { return this.set('Connection', connection); } setAcceptEncoding(acceptEncoding) { return this.set('Accept-Encoding', acceptEncoding); } setAcceptLanguage(acceptLanguage) { return this.set('Accept-Language', acceptLanguage); } setResponseType(responseType) { return this.set('Response-Type', responseType); } setCacheControl(cacheControl) { return this.set('Cache-Control', cacheControl); } setCookie(cookie) { return this.set('Cookie', cookie); } setDNT(dnt) { return this.set('DNT', dnt); } setPragma(pragma) { return this.set('Pragma', pragma); } } exports.default = Headers;