UNPKG

@thisisagile/easy

Version:

Straightforward library for building domain-driven microservice architectures

105 lines (104 loc) 3.53 kB
import { ctx, entries, meta, toList, tryTo } from "../chunk-AVHYDITZ.mjs"; import "../chunk-EBIF6AMC.mjs"; import { toArray } from "../chunk-KCY4RPFR.mjs"; import "../chunk-SSROJBD5.mjs"; import "../chunk-S3NSPQ7M.mjs"; import { asString } from "../chunk-NNA77YYC.mjs"; import "../chunk-SJGQU3OG.mjs"; import "../chunk-D5IYAIMK.mjs"; import { toName } from "../chunk-PF7HDF6B.mjs"; import "../chunk-IXK47WKF.mjs"; import "../chunk-PIRWVOO2.mjs"; import { isBoolean, isNotEmpty, isTrue } from "../chunk-AAND4MKF.mjs"; import "../chunk-4N72FQFX.mjs"; // src/types/Uri.ts var toSegment = (key, { segment, query } = {}) => ({ key, segment, query, toString: () => asString(key) }); var uri = { host: (key) => toSegment(key, { segment: key ?? ctx.env.host ?? "$host" }), resource: (resource) => toSegment(toName(resource, "Uri"), { segment: toName(resource, "Uri") }), segment: (key) => toSegment(key, { segment: key }), path: (key) => toSegment(key, { segment: `:${key}` }), query: (key) => toSegment(key, { query: (value) => tryTo(value).is.defined().map((v) => encodeURIComponent(asString(v))).map((v) => `${key}=${v}`).orElse("") ?? "" }), boolean: (key) => toSegment(key, { query: (value) => isTrue(value) ? `${key}` : "" }) }; var toRoute = (...segments) => toList(segments).mapDefined((s) => s.segment).join("/"); var EasyUri = class _EasyUri { constructor(segments = []) { this.segments = segments; } static id = uri.path("id"); static ids = uri.query("ids"); static query = uri.query("q"); static sort = uri.query("s"); static skip = uri.query("skip"); static take = uri.query("take"); host = uri.host(); resource = uri.resource(this); state = {}; get path() { return toRoute(uri.segment(""), this.resource, ...this.segments); } get complete() { return toRoute(this.host, this.resource, ...this.segments); } get isInternal() { return toRoute(this.host) === (ctx.env.host ?? "$host"); } get props() { return meta(this.state).values(); } route = (resource = this.resource.key) => toRoute(uri.segment(""), uri.segment(resource?.toLowerCase()), ...this.segments); set = (segment, value) => { tryTo(value).is.defined().accept((value2) => this.state[segment.key ?? ""] = { segment, value: value2 }); return this; }; toString() { return tryTo(() => this.props).map((ps) => ps.filter((p) => p.segment?.segment)).map((ps) => ps.reduce((r, p) => r.replace(asString(p.segment.segment), asString(p.value)), this.complete)).map((route) => ({ route, query: this.props.mapDefined((p) => p.segment?.query ? p.segment?.query(p.value) : void 0)?.join("&") })).map(({ route, query }) => isNotEmpty(query) ? `${route}?${query}` : route).value; } id = (id) => this.set(_EasyUri.id, id); ids = (ids) => this.set(_EasyUri.ids, toArray(ids).join(",")); query = (q) => this.set(_EasyUri.query, q); sort = (s) => this.set(_EasyUri.sort, asString(s)); skip = (index) => this.set(_EasyUri.skip, index); take = (items) => this.set(_EasyUri.take, items); expand(props) { return entries(props).filter(([_, v]) => isNotEmpty(v)).reduce((u, [k, v]) => isBoolean(v) ? u.set(uri.boolean(k), v) : u.set(uri.query(k), toArray(v).join(",")), this); } }; var clipUri = (uri2) => tryTo(() => asString(uri2)).map((uri3) => uri3.replace(/^(https?:\/\/)?(www\.)?/, "")).map((uri3) => uri3.replace(/\/$/, "")).value; export { EasyUri, clipUri, toSegment, uri }; //# sourceMappingURL=Uri.mjs.map