@thisisagile/easy-service
Version:
Straightforward library for building domain-driven microservice architectures
225 lines (215 loc) • 8.25 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __decorateClass = (decorators, target, key, kind) => {
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
for (var i = decorators.length - 1, decorator; i >= 0; i--)
if (decorator = decorators[i])
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
if (kind && result)
__defProp(target, key, result);
return result;
};
// src/index.ts
var src_exports = {};
__export(src_exports, {
HealthResource: () => HealthResource,
HealthUri: () => HealthUri,
Jwt: () => Jwt,
OriginatedError: () => OriginatedError,
Requires: () => Requires,
Service: () => Service,
del: () => del,
get: () => get,
isOriginatedError: () => isOriginatedError,
patch: () => patch,
post: () => post,
put: () => put,
requires: () => requires,
route: () => route,
routes: () => routes,
search: () => search,
stream: () => stream,
toOriginatedError: () => toOriginatedError,
toVerbOptions: () => toVerbOptions
});
module.exports = __toCommonJS(src_exports);
// src/health/HealthResource.ts
var import_easy4 = require("@thisisagile/easy");
// src/health/HealthUri.ts
var import_easy = require("@thisisagile/easy");
var HealthUri = class _HealthUri extends import_easy.EasyUri {
static health = import_easy.uri.segment("health");
static Health = new _HealthUri([_HealthUri.health]);
};
// src/resources/Route.ts
var import_easy2 = require("@thisisagile/easy");
var route = (uri2) => (subject) => {
(0, import_easy2.meta)(subject).set("route", uri2);
};
var toRoute = (endpoint, requires2, verb, middleware) => (0, import_easy2.tryTo)(verb).is.defined().map((verb2) => ({ verb: verb2, endpoint, requires: requires2, middleware: middleware ?? [] })).orElse();
var Router = class {
constructor(resource) {
this.resource = resource;
}
get route() {
return (0, import_easy2.meta)(this.resource).get("route");
}
get middleware() {
return (0, import_easy2.meta)(this.resource).get("middleware") ?? [];
}
get endpoints() {
return (0, import_easy2.meta)(this.resource).properties("verb").mapDefined(
(v) => toRoute(
this.resource[v.property],
{
labCoat: v.get("labCoat") ?? false,
token: v.get("token") ?? false,
scope: v.get("scope"),
uc: v.get("uc")
},
v.get("verb"),
v.get("middleware")
)
);
}
};
var routes = (resource) => new Router(resource);
// src/http/Verb.ts
var import_easy3 = require("@thisisagile/easy");
var toVerbOptions = (options) => ({
onOk: options?.onOk ?? import_easy3.HttpStatus.Ok,
onNotFound: options?.onNotFound ?? import_easy3.HttpStatus.NotFound,
onError: options?.onError ?? import_easy3.HttpStatus.BadRequest,
type: options?.type ?? import_easy3.ContentType.Json,
cache: options?.cache ?? import_easy3.CacheControl.disabled()
});
var toVerb = (verb, options) => (subject, property) => {
(0, import_easy3.meta)(subject).property(property).set("verb", { verb, options });
};
var get = (options) => toVerb(import_easy3.HttpVerb.Get, options);
var search = (options) => toVerb(import_easy3.HttpVerb.Get, { onNotFound: import_easy3.HttpStatus.Ok, ...options });
var put = (options) => toVerb(import_easy3.HttpVerb.Put, options);
var patch = (options) => toVerb(import_easy3.HttpVerb.Patch, options);
var post = (options) => toVerb(import_easy3.HttpVerb.Post, { onOk: import_easy3.HttpStatus.Created, ...options });
var del = (options) => toVerb(import_easy3.HttpVerb.Delete, { onOk: import_easy3.HttpStatus.NoContent, ...options });
var stream = (options) => toVerb(import_easy3.HttpVerb.Get, { type: import_easy3.ContentType.Stream, ...options });
// src/health/HealthResource.ts
var HealthResource = class {
ok = () => (0, import_easy4.resolve)({ state: "Service is healthy." });
};
__decorateClass([
get()
], HealthResource.prototype, "ok", 2);
HealthResource = __decorateClass([
route(HealthUri.Health)
], HealthResource);
// src/http/OriginatedError.ts
var import_easy5 = require("@thisisagile/easy");
var OriginatedError = class extends Error {
constructor(origin, options) {
super();
this.origin = origin;
this.options = options;
if ((0, import_easy5.isError)(origin))
this.stack = origin.stack;
}
};
var isOriginatedError = (e) => (0, import_easy5.isError)(e) && e instanceof OriginatedError;
var toOriginatedError = (e, options) => isOriginatedError(e) ? e : new OriginatedError(e, options);
// src/resources/Requires.ts
var import_easy6 = require("@thisisagile/easy");
var Requires = class {
labCoat = () => (subject, property) => {
(0, import_easy6.meta)(subject).property(property).set("labCoat", true);
};
token = () => (subject, property) => {
(0, import_easy6.meta)(subject).property(property).set("token", true);
};
scope = (scope) => (subject, property) => {
(0, import_easy6.meta)(subject).property(property).set("token", true);
(0, import_easy6.meta)(subject).property(property).set("scope", scope);
};
useCase = (uc) => (subject, property) => {
(0, import_easy6.meta)(subject).property(property).set("token", true);
(0, import_easy6.meta)(subject).property(property).set("uc", uc);
};
};
var requires = new Requires();
// src/resources/Service.ts
var import_easy7 = require("@thisisagile/easy");
var Service = class extends import_easy7.Enum {
constructor(name, app, resources = (0, import_easy7.toList)()) {
super(name);
this.name = name;
this.app = app;
this.resources = resources;
}
port = 8080;
pre = () => [];
post = () => [];
with(...resources) {
return (0, import_easy7.tryTo)(this).accept((t) => t.resources.add(resources.map((r) => new r()))).value;
}
atPort(port) {
return (0, import_easy7.tryTo)(this).accept((t) => t.port = port).value;
}
start(message = `Service ${this.name} listening on port ${this.port} with ${this.resources.length} resources.`) {
(0, import_easy7.tryTo)(this).accept((t) => t.pre().forEach((h) => this.app.use(h))).accept((t) => t.resources.forEach((r) => this.app.route(this, r))).accept((t) => t.post().forEach((h) => this.app.use(h))).accept((t) => t.app.listen(this.port, message));
}
};
// src/security/Jwt.ts
var import_jsonwebtoken = require("jsonwebtoken");
var import_easy8 = require("@thisisagile/easy");
var Jwt = class _Jwt extends import_easy8.Jwt {
static of = (a) => new _Jwt(a.jwt);
get isValid() {
return (0, import_easy8.tryTo)(() => import_easy8.ctx.env.get("tokenPublicKey") ?? "").map((key) => (0, import_jsonwebtoken.verify)(this.value, key)).map(() => true).orElse() ?? false;
}
static sign = (token, options) => (0, import_easy8.tryTo)(() => import_easy8.ctx.env.get("tokenPrivateKey") ?? "").is.not.empty().map(
(key) => (0, import_jsonwebtoken.sign)(token, key, {
...options,
expiresIn: import_easy8.ctx.env.get("tokenExpiresIn") ?? "1h",
keyid: import_easy8.ctx.env.get("tokenKeyid") ?? "easy",
algorithm: import_easy8.ctx.env.get("tokenAlgorithm", "RS256")
})
).map((s) => new _Jwt(s)).value;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
HealthResource,
HealthUri,
Jwt,
OriginatedError,
Requires,
Service,
del,
get,
isOriginatedError,
patch,
post,
put,
requires,
route,
routes,
search,
stream,
toOriginatedError,
toVerbOptions
});
//# sourceMappingURL=index.js.map