@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
90 lines (88 loc) • 2.51 kB
JavaScript
import {
ContentType
} from "./chunk-RDLH6MXX.mjs";
import {
HttpHeader
} from "./chunk-SW2OCUH3.mjs";
import {
toUuid
} from "./chunk-LXQQ5N6S.mjs";
import {
Enum
} from "./chunk-JVDKV5HE.mjs";
import {
ctx
} from "./chunk-NDCIZJXB.mjs";
import {
asString
} from "./chunk-BDA5LB4S.mjs";
import {
on
} from "./chunk-ZPNFXK7Y.mjs";
import {
isDefined,
isNotEmpty
} from "./chunk-DEJ7A5PY.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-NYFC5ZUU.mjs.map