UNPKG

@thisisagile/easy

Version:

Straightforward library for building domain-driven microservice architectures

88 lines (86 loc) 2.47 kB
import { HttpHeader } from "./chunk-SW2OCUH3.mjs"; import { toUuid } from "./chunk-6FUVIZYN.mjs"; import { ContentType } from "./chunk-JMAWKYXN.mjs"; import { Enum, ctx } from "./chunk-AVHYDITZ.mjs"; import { asString } from "./chunk-NNA77YYC.mjs"; import { on } from "./chunk-PF7HDF6B.mjs"; import { isDefined, isNotEmpty } from "./chunk-AAND4MKF.mjs"; // src/http/RequestOptions.ts var toPageOptions = (options) => options instanceof RequestOptions ? options.pageOptions : options; var RequestOptions = class _RequestOptions extends Enum { constructor(type = ContentType.Json, headers = {}, pageOptions) { super(type.name); this.type = type; this.headers = headers; this.pageOptions = pageOptions; this.headers["Content-Type"] = type.id; this.headers[HttpHeader.Correlation] = ctx.request.correlationId ?? toUuid(); } requestOptions = {}; static get Form() { return new _RequestOptions(ContentType.Form); } static get Json() { return new _RequestOptions(ContentType.Json); } static get Stream() { return new _RequestOptions(ContentType.Stream); } static get Text() { return new _RequestOptions(ContentType.Text); } static get Xml() { return new _RequestOptions(ContentType.Xml); } page = (options) => { this.pageOptions = options; return this; }; authorization = (auth) => this.setHeader("Authorization", auth); apiKey = (apiKey) => this.setHeader("apiKey", apiKey); setHeader = (key, value) => on(this, (t) => t.headers[asString(key)] = value); setHeaderUnlessPresent = (key, value) => value ? this.setHeader(key, this.headers[key] ?? value) : this; accept = (type) => this.setHeader("Accept", type.id); bearer = (jwt) => { return isNotEmpty(jwt) ? this.authorization(`Bearer ${jwt}`) : this; }; basic = (username, password) => { const basicAuth = Buffer.from(`${username}:${password}`, "utf8").toString("base64"); return this.authorization(`Basic ${basicAuth}`); }; maxRedirects = (max) => { this.requestOptions.maxRedirects = max; return this; }; validateStatus = (validate) => { this.requestOptions.validateStatus = validate; return this; }; timeout = (t) => { this.requestOptions.timeout = t; return this; }; }; var isRequestOptions = (o) => isDefined(o) && o instanceof RequestOptions; export { toPageOptions, RequestOptions, isRequestOptions }; //# sourceMappingURL=chunk-QIBUS2GL.mjs.map