@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
119 lines (118 loc) • 3.88 kB
JavaScript
import {
tryTo
} from "../chunk-XAIHCZT4.mjs";
import "../chunk-OFGI5FLG.mjs";
import "../chunk-NCEWAKOZ.mjs";
import "../chunk-Q3WMGUO2.mjs";
import "../chunk-5HENJTR6.mjs";
import "../chunk-FKZ4MOTX.mjs";
import "../chunk-WSBULPUZ.mjs";
import "../chunk-JVDKV5HE.mjs";
import {
meta
} from "../chunk-WTFW6DLP.mjs";
import {
ctx
} from "../chunk-NDCIZJXB.mjs";
import "../chunk-MCCIBDEH.mjs";
import {
entries
} from "../chunk-H2762RTS.mjs";
import {
toList
} from "../chunk-A7C3XND3.mjs";
import "../chunk-JSON7A4X.mjs";
import {
toArray
} from "../chunk-CO2AFYVD.mjs";
import {
asString
} from "../chunk-BDA5LB4S.mjs";
import "../chunk-SJGQU3OG.mjs";
import "../chunk-ZHXKBOK2.mjs";
import {
toName
} from "../chunk-ZPNFXK7Y.mjs";
import "../chunk-DXQSIBC7.mjs";
import "../chunk-PIRWVOO2.mjs";
import {
isBoolean,
isNotEmpty,
isTrue
} from "../chunk-DEJ7A5PY.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