@thisisagile/easy-service 18.17.0 → 19.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-4D3CGIDW.mjs +22 -0
- package/dist/chunk-4D3CGIDW.mjs.map +1 -0
- package/dist/chunk-BTPC4SVP.mjs +1 -0
- package/dist/chunk-BTPC4SVP.mjs.map +1 -0
- package/dist/chunk-KHAYZJCB.mjs +29 -0
- package/dist/chunk-KHAYZJCB.mjs.map +1 -0
- package/dist/chunk-OQUQPJ6S.mjs +25 -0
- package/dist/chunk-OQUQPJ6S.mjs.map +1 -0
- package/dist/chunk-RJWBTUKZ.mjs +27 -0
- package/dist/chunk-RJWBTUKZ.mjs.map +1 -0
- package/dist/chunk-VXGHOHGY.mjs +1 -0
- package/dist/chunk-VXGHOHGY.mjs.map +1 -0
- package/dist/chunk-YB7NDS5U.mjs +20 -0
- package/dist/chunk-YB7NDS5U.mjs.map +1 -0
- package/dist/health/HealthResource.mjs +6 -23
- package/dist/health/HealthResource.mjs.map +1 -1
- package/dist/http/OriginatedError.mjs +5 -14
- package/dist/http/OriginatedError.mjs.map +1 -1
- package/dist/index.js +13 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +59 -10
- package/dist/index.mjs.map +1 -1
- package/dist/resources/AppProvider.mjs +1 -0
- package/dist/resources/Requires.mjs +4 -20
- package/dist/resources/Requires.mjs.map +1 -1
- package/dist/resources/Resource.mjs +1 -0
- package/dist/resources/Service.mjs +3 -23
- package/dist/resources/Service.mjs.map +1 -1
- package/dist/security/Jwt.mjs +3 -18
- package/dist/security/Jwt.mjs.map +1 -1
- package/package.json +54 -4
- package/src/security/Jwt.ts +5 -4
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/security/Jwt.ts
|
|
2
|
+
import jwt from "jsonwebtoken";
|
|
3
|
+
import { ctx, tryTo, Jwt as JwtBase } from "@thisisagile/easy";
|
|
4
|
+
var Jwt = class _Jwt extends JwtBase {
|
|
5
|
+
static of = (a) => new _Jwt(a.jwt);
|
|
6
|
+
get isValid() {
|
|
7
|
+
return tryTo(() => ctx.env.get("tokenPublicKey") ?? "").map((key) => jwt.verify(this.value, key)).map(() => true).orElse() ?? false;
|
|
8
|
+
}
|
|
9
|
+
static sign = (token, options) => tryTo(() => ctx.env.get("tokenPrivateKey") ?? "").is.not.empty().map(
|
|
10
|
+
(key) => jwt.sign(token, key, {
|
|
11
|
+
...options,
|
|
12
|
+
expiresIn: ctx.env.get("tokenExpiresIn") ?? "1h",
|
|
13
|
+
keyid: ctx.env.get("tokenKeyid") ?? "easy",
|
|
14
|
+
algorithm: ctx.env.get("tokenAlgorithm", "RS256")
|
|
15
|
+
})
|
|
16
|
+
).map((s) => new _Jwt(s)).value;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
Jwt
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=chunk-4D3CGIDW.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/security/Jwt.ts"],"sourcesContent":["import jwt from 'jsonwebtoken';\nimport type { Algorithm } from 'jsonwebtoken';\nimport { ctx, Json, OneOrMore, Optional, tryTo, Validatable, Jwt as JwtBase } from '@thisisagile/easy';\n\ninterface SignOptions {\n audience?: Optional<OneOrMore<string>>;\n issuer?: Optional<string>;\n}\nexport class Jwt extends JwtBase implements Validatable {\n static of = (a: { jwt: string }): Jwt => new Jwt(a.jwt);\n\n get isValid(): boolean {\n return (\n tryTo(() => ctx.env.get('tokenPublicKey') ?? '')\n .map(key => jwt.verify(this.value, key))\n .map(() => true)\n .orElse() ?? false\n );\n }\n\n static sign = (token: Json, options?: SignOptions): Jwt =>\n tryTo(() => ctx.env.get('tokenPrivateKey') ?? '')\n .is.not.empty()\n .map(key =>\n jwt.sign(token, key, {\n ...options,\n expiresIn: (ctx.env.get('tokenExpiresIn') as any) ?? '1h',\n keyid: ctx.env.get('tokenKeyid') ?? 'easy',\n algorithm: ctx.env.get('tokenAlgorithm', 'RS256') as Algorithm,\n })\n )\n .map(s => new Jwt(s)).value;\n}\n"],"mappings":";AAAA,OAAO,SAAS;AAEhB,SAAS,KAAgC,OAAoB,OAAO,eAAe;AAM5E,IAAM,MAAN,MAAM,aAAY,QAA+B;AAAA,EACtD,OAAO,KAAK,CAAC,MAA4B,IAAI,KAAI,EAAE,GAAG;AAAA,EAEtD,IAAI,UAAmB;AACrB,WACE,MAAM,MAAM,IAAI,IAAI,IAAI,gBAAgB,KAAK,EAAE,EAC5C,IAAI,SAAO,IAAI,OAAO,KAAK,OAAO,GAAG,CAAC,EACtC,IAAI,MAAM,IAAI,EACd,OAAO,KAAK;AAAA,EAEnB;AAAA,EAEA,OAAO,OAAO,CAAC,OAAa,YAC1B,MAAM,MAAM,IAAI,IAAI,IAAI,iBAAiB,KAAK,EAAE,EAC7C,GAAG,IAAI,MAAM,EACb;AAAA,IAAI,SACH,IAAI,KAAK,OAAO,KAAK;AAAA,MACnB,GAAG;AAAA,MACH,WAAY,IAAI,IAAI,IAAI,gBAAgB,KAAa;AAAA,MACrD,OAAO,IAAI,IAAI,IAAI,YAAY,KAAK;AAAA,MACpC,WAAW,IAAI,IAAI,IAAI,kBAAkB,OAAO;AAAA,IAClD,CAAC;AAAA,EACH,EACC,IAAI,OAAK,IAAI,KAAI,CAAC,CAAC,EAAE;AAC5B;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-BTPC4SVP.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
route
|
|
3
|
+
} from "./chunk-B37TXK4C.mjs";
|
|
4
|
+
import {
|
|
5
|
+
HealthUri
|
|
6
|
+
} from "./chunk-2XMYNSTI.mjs";
|
|
7
|
+
import {
|
|
8
|
+
get
|
|
9
|
+
} from "./chunk-W62PBGRF.mjs";
|
|
10
|
+
import {
|
|
11
|
+
__decorateClass
|
|
12
|
+
} from "./chunk-4N72FQFX.mjs";
|
|
13
|
+
|
|
14
|
+
// src/health/HealthResource.ts
|
|
15
|
+
import { resolve } from "@thisisagile/easy";
|
|
16
|
+
var HealthResource = class {
|
|
17
|
+
ok = () => resolve({ state: "Service is healthy." });
|
|
18
|
+
};
|
|
19
|
+
__decorateClass([
|
|
20
|
+
get()
|
|
21
|
+
], HealthResource.prototype, "ok", 2);
|
|
22
|
+
HealthResource = __decorateClass([
|
|
23
|
+
route(HealthUri.Health)
|
|
24
|
+
], HealthResource);
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
HealthResource
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=chunk-KHAYZJCB.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/health/HealthResource.ts"],"sourcesContent":["import { resolve, Json } from '@thisisagile/easy';\nimport { HealthUri } from './HealthUri';\nimport { Resource } from '../resources/Resource';\nimport { route } from '../resources/Route';\nimport { get } from '../http/Verb';\n\n@route(HealthUri.Health)\nexport class HealthResource implements Resource {\n @get()\n ok = (): Promise<Json> => resolve({ state: 'Service is healthy.' });\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,eAAqB;AAOvB,IAAM,iBAAN,MAAyC;AAAA,EAE9C,KAAK,MAAqB,QAAQ,EAAE,OAAO,sBAAsB,CAAC;AACpE;AADE;AAAA,EADC,IAAI;AAAA,GADM,eAEX;AAFW,iBAAN;AAAA,EADN,MAAM,UAAU,MAAM;AAAA,GACV;","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/resources/Requires.ts
|
|
2
|
+
import { meta } from "@thisisagile/easy";
|
|
3
|
+
var Requires = class {
|
|
4
|
+
labCoat = () => (subject, property) => {
|
|
5
|
+
meta(subject).property(property).set("labCoat", true);
|
|
6
|
+
};
|
|
7
|
+
token = () => (subject, property) => {
|
|
8
|
+
meta(subject).property(property).set("token", true);
|
|
9
|
+
};
|
|
10
|
+
scope = (scope) => (subject, property) => {
|
|
11
|
+
meta(subject).property(property).set("token", true);
|
|
12
|
+
meta(subject).property(property).set("scope", scope);
|
|
13
|
+
};
|
|
14
|
+
useCase = (uc) => (subject, property) => {
|
|
15
|
+
meta(subject).property(property).set("token", true);
|
|
16
|
+
meta(subject).property(property).set("uc", uc);
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
var requires = new Requires();
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
Requires,
|
|
23
|
+
requires
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=chunk-OQUQPJ6S.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/resources/Requires.ts"],"sourcesContent":["import { meta } from '@thisisagile/easy';\nimport { Scope, UseCase } from '@thisisagile/easy';\n\nexport class Requires {\n readonly labCoat =\n (): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('labCoat', true);\n };\n\n readonly token =\n (): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n };\n\n readonly scope =\n (scope: Scope): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n meta(subject).property(property).set('scope', scope);\n };\n\n readonly useCase =\n (uc: UseCase): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n meta(subject).property(property).set('uc', uc);\n };\n}\n\nexport const requires = new Requires();\n"],"mappings":";AAAA,SAAS,YAAY;AAGd,IAAM,WAAN,MAAe;AAAA,EACX,UACP,MACA,CAAC,SAAkB,aAAoC;AACrD,SAAK,OAAO,EAAE,SAAS,QAAQ,EAAE,IAAI,WAAW,IAAI;AAAA,EACtD;AAAA,EAEO,QACP,MACA,CAAC,SAAkB,aAAoC;AACrD,SAAK,OAAO,EAAE,SAAS,QAAQ,EAAE,IAAI,SAAS,IAAI;AAAA,EACpD;AAAA,EAEO,QACP,CAAC,UACD,CAAC,SAAkB,aAAoC;AACrD,SAAK,OAAO,EAAE,SAAS,QAAQ,EAAE,IAAI,SAAS,IAAI;AAClD,SAAK,OAAO,EAAE,SAAS,QAAQ,EAAE,IAAI,SAAS,KAAK;AAAA,EACrD;AAAA,EAEO,UACP,CAAC,OACD,CAAC,SAAkB,aAAoC;AACrD,SAAK,OAAO,EAAE,SAAS,QAAQ,EAAE,IAAI,SAAS,IAAI;AAClD,SAAK,OAAO,EAAE,SAAS,QAAQ,EAAE,IAAI,MAAM,EAAE;AAAA,EAC/C;AACJ;AAEO,IAAM,WAAW,IAAI,SAAS;","names":[]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/resources/Service.ts
|
|
2
|
+
import { Enum, toList, tryTo } from "@thisisagile/easy";
|
|
3
|
+
var Service = class extends Enum {
|
|
4
|
+
constructor(name, app, resources = toList()) {
|
|
5
|
+
super(name);
|
|
6
|
+
this.name = name;
|
|
7
|
+
this.app = app;
|
|
8
|
+
this.resources = resources;
|
|
9
|
+
}
|
|
10
|
+
port = 8080;
|
|
11
|
+
pre = () => [];
|
|
12
|
+
post = () => [];
|
|
13
|
+
with(...resources) {
|
|
14
|
+
return tryTo(this).accept((t) => t.resources.add(resources.map((r) => new r()))).value;
|
|
15
|
+
}
|
|
16
|
+
atPort(port) {
|
|
17
|
+
return tryTo(this).accept((t) => t.port = port).value;
|
|
18
|
+
}
|
|
19
|
+
start(message = `Service ${this.name} listening on port ${this.port} with ${this.resources.length} resources.`) {
|
|
20
|
+
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));
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
Service
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=chunk-RJWBTUKZ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/resources/Service.ts"],"sourcesContent":["import { AppProvider, Handler } from './AppProvider';\nimport { Constructor, Enum, List, toList, tryTo } from '@thisisagile/easy';\nimport { Resource } from './Resource';\n\nexport class Service extends Enum {\n protected port = 8080;\n\n constructor(\n readonly name: string,\n protected app: AppProvider,\n protected resources: List<Resource> = toList()\n ) {\n super(name);\n }\n\n pre = (): Handler[] => [];\n post = (): Handler[] => [];\n\n with(...resources: Constructor<Resource>[]): this {\n return tryTo(this).accept(t => t.resources.add(resources.map(r => new r()))).value;\n }\n\n atPort(port: number): this {\n return tryTo(this).accept(t => (t.port = port)).value;\n }\n\n start(message = `Service ${this.name} listening on port ${this.port} with ${this.resources.length} resources.`): void {\n tryTo(this)\n .accept(t => t.pre().forEach(h => this.app.use(h)))\n .accept(t => t.resources.forEach(r => this.app.route(this, r)))\n .accept(t => t.post().forEach(h => this.app.use(h)))\n .accept(t => t.app.listen(this.port, message));\n }\n}\n"],"mappings":";AACA,SAAsB,MAAY,QAAQ,aAAa;AAGhD,IAAM,UAAN,cAAsB,KAAK;AAAA,EAGhC,YACW,MACC,KACA,YAA4B,OAAO,GAC7C;AACA,UAAM,IAAI;AAJD;AACC;AACA;AAAA,EAGZ;AAAA,EARU,OAAO;AAAA,EAUjB,MAAM,MAAiB,CAAC;AAAA,EACxB,OAAO,MAAiB,CAAC;AAAA,EAEzB,QAAQ,WAA0C;AAChD,WAAO,MAAM,IAAI,EAAE,OAAO,OAAK,EAAE,UAAU,IAAI,UAAU,IAAI,OAAK,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE;AAAA,EAC/E;AAAA,EAEA,OAAO,MAAoB;AACzB,WAAO,MAAM,IAAI,EAAE,OAAO,OAAM,EAAE,OAAO,IAAK,EAAE;AAAA,EAClD;AAAA,EAEA,MAAM,UAAU,WAAW,KAAK,IAAI,sBAAsB,KAAK,IAAI,SAAS,KAAK,UAAU,MAAM,eAAqB;AACpH,UAAM,IAAI,EACP,OAAO,OAAK,EAAE,IAAI,EAAE,QAAQ,OAAK,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,EACjD,OAAO,OAAK,EAAE,UAAU,QAAQ,OAAK,KAAK,IAAI,MAAM,MAAM,CAAC,CAAC,CAAC,EAC7D,OAAO,OAAK,EAAE,KAAK,EAAE,QAAQ,OAAK,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,EAClD,OAAO,OAAK,EAAE,IAAI,OAAO,KAAK,MAAM,OAAO,CAAC;AAAA,EACjD;AACF;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-VXGHOHGY.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/http/OriginatedError.ts
|
|
2
|
+
import { isError } from "@thisisagile/easy";
|
|
3
|
+
var OriginatedError = class extends Error {
|
|
4
|
+
constructor(origin, options) {
|
|
5
|
+
super();
|
|
6
|
+
this.origin = origin;
|
|
7
|
+
this.options = options;
|
|
8
|
+
if (isError(origin))
|
|
9
|
+
this.stack = origin.stack;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var isOriginatedError = (e) => isError(e) && e instanceof OriginatedError;
|
|
13
|
+
var toOriginatedError = (e, options) => isOriginatedError(e) ? e : new OriginatedError(e, options);
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
OriginatedError,
|
|
17
|
+
isOriginatedError,
|
|
18
|
+
toOriginatedError
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=chunk-YB7NDS5U.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/http/OriginatedError.ts"],"sourcesContent":["import { ErrorOrigin, isError } from '@thisisagile/easy';\nimport { VerbOptions } from './Verb';\n\nexport class OriginatedError extends Error {\n constructor(\n readonly origin: ErrorOrigin,\n readonly options?: VerbOptions\n ) {\n super();\n if (isError(origin)) this.stack = origin.stack;\n }\n}\n\nexport const isOriginatedError = (e?: unknown): e is OriginatedError => isError(e) && e instanceof OriginatedError;\n\nexport const toOriginatedError = (e: unknown, options?: VerbOptions): OriginatedError =>\n isOriginatedError(e) ? e : new OriginatedError(e as ErrorOrigin, options);\n"],"mappings":";AAAA,SAAsB,eAAe;AAG9B,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzC,YACW,QACA,SACT;AACA,UAAM;AAHG;AACA;AAGT,QAAI,QAAQ,MAAM;AAAG,WAAK,QAAQ,OAAO;AAAA,EAC3C;AACF;AAEO,IAAM,oBAAoB,CAAC,MAAsC,QAAQ,CAAC,KAAK,aAAa;AAE5F,IAAM,oBAAoB,CAAC,GAAY,YAC5C,kBAAkB,CAAC,IAAI,IAAI,IAAI,gBAAgB,GAAkB,OAAO;","names":[]}
|
|
@@ -1,27 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
route
|
|
9
|
-
} from "../chunk-B37TXK4C.mjs";
|
|
10
|
-
import {
|
|
11
|
-
__decorateClass
|
|
12
|
-
} from "../chunk-4N72FQFX.mjs";
|
|
13
|
-
|
|
14
|
-
// src/health/HealthResource.ts
|
|
15
|
-
import { resolve } from "@thisisagile/easy";
|
|
16
|
-
var HealthResource = class {
|
|
17
|
-
ok = () => resolve({ state: "Service is healthy." });
|
|
18
|
-
};
|
|
19
|
-
__decorateClass([
|
|
20
|
-
get()
|
|
21
|
-
], HealthResource.prototype, "ok", 2);
|
|
22
|
-
HealthResource = __decorateClass([
|
|
23
|
-
route(HealthUri.Health)
|
|
24
|
-
], HealthResource);
|
|
2
|
+
HealthResource
|
|
3
|
+
} from "../chunk-KHAYZJCB.mjs";
|
|
4
|
+
import "../chunk-B37TXK4C.mjs";
|
|
5
|
+
import "../chunk-2XMYNSTI.mjs";
|
|
6
|
+
import "../chunk-W62PBGRF.mjs";
|
|
7
|
+
import "../chunk-4N72FQFX.mjs";
|
|
25
8
|
export {
|
|
26
9
|
HealthResource
|
|
27
10
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,18 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OriginatedError,
|
|
3
|
+
isOriginatedError,
|
|
4
|
+
toOriginatedError
|
|
5
|
+
} from "../chunk-YB7NDS5U.mjs";
|
|
1
6
|
import "../chunk-4N72FQFX.mjs";
|
|
2
|
-
|
|
3
|
-
// src/http/OriginatedError.ts
|
|
4
|
-
import { isError } from "@thisisagile/easy";
|
|
5
|
-
var OriginatedError = class extends Error {
|
|
6
|
-
constructor(origin, options) {
|
|
7
|
-
super();
|
|
8
|
-
this.origin = origin;
|
|
9
|
-
this.options = options;
|
|
10
|
-
if (isError(origin))
|
|
11
|
-
this.stack = origin.stack;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
var isOriginatedError = (e) => isError(e) && e instanceof OriginatedError;
|
|
15
|
-
var toOriginatedError = (e, options) => isOriginatedError(e) ? e : new OriginatedError(e, options);
|
|
16
7
|
export {
|
|
17
8
|
OriginatedError,
|
|
18
9
|
isOriginatedError,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
var __decorateClass = (decorators, target, key, kind) => {
|
|
20
30
|
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
@@ -185,15 +195,15 @@ var Service = class extends import_easy7.Enum {
|
|
|
185
195
|
};
|
|
186
196
|
|
|
187
197
|
// src/security/Jwt.ts
|
|
188
|
-
var import_jsonwebtoken = require("jsonwebtoken");
|
|
198
|
+
var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
|
|
189
199
|
var import_easy8 = require("@thisisagile/easy");
|
|
190
200
|
var Jwt = class _Jwt extends import_easy8.Jwt {
|
|
191
201
|
static of = (a) => new _Jwt(a.jwt);
|
|
192
202
|
get isValid() {
|
|
193
|
-
return (0, import_easy8.tryTo)(() => import_easy8.ctx.env.get("tokenPublicKey") ?? "").map((key) =>
|
|
203
|
+
return (0, import_easy8.tryTo)(() => import_easy8.ctx.env.get("tokenPublicKey") ?? "").map((key) => import_jsonwebtoken.default.verify(this.value, key)).map(() => true).orElse() ?? false;
|
|
194
204
|
}
|
|
195
205
|
static sign = (token, options) => (0, import_easy8.tryTo)(() => import_easy8.ctx.env.get("tokenPrivateKey") ?? "").is.not.empty().map(
|
|
196
|
-
(key) =>
|
|
206
|
+
(key) => import_jsonwebtoken.default.sign(token, key, {
|
|
197
207
|
...options,
|
|
198
208
|
expiresIn: import_easy8.ctx.env.get("tokenExpiresIn") ?? "1h",
|
|
199
209
|
keyid: import_easy8.ctx.env.get("tokenKeyid") ?? "easy",
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/health/HealthResource.ts","../src/health/HealthUri.ts","../src/resources/Route.ts","../src/http/Verb.ts","../src/http/OriginatedError.ts","../src/resources/Requires.ts","../src/resources/Service.ts","../src/security/Jwt.ts"],"sourcesContent":["export * from './health/HealthResource';\nexport * from './health/HealthUri';\nexport * from './http/OriginatedError';\nexport * from './http/Verb';\nexport * from './resources/AppProvider';\nexport * from './resources/Requires';\nexport * from './resources/Resource';\nexport * from './resources/Route';\nexport * from './resources/Service';\nexport * from './security/Jwt';\n","import { resolve, Json } from '@thisisagile/easy';\nimport { HealthUri } from './HealthUri';\nimport { Resource } from '../resources/Resource';\nimport { route } from '../resources/Route';\nimport { get } from '../http/Verb';\n\n@route(HealthUri.Health)\nexport class HealthResource implements Resource {\n @get()\n ok = (): Promise<Json> => resolve({ state: 'Service is healthy.' });\n}\n","import { EasyUri, uri } from '@thisisagile/easy';\n\nexport class HealthUri extends EasyUri {\n static readonly health = uri.segment('health');\n static readonly Health = new HealthUri([HealthUri.health]);\n}\n","import { List, meta, Optional, tryTo, Uri, Scope, UseCase, Req } from '@thisisagile/easy';\nimport { Resource } from './Resource';\nimport { RequestHandler } from 'express';\nimport { Verb } from '../http/Verb';\n\nexport const route =\n (uri: Uri): ClassDecorator =>\n (subject: unknown): void => {\n meta(subject).set('route', uri);\n };\n\nexport type Endpoint<T = unknown> = (re: Req) => Promise<T | List<T>>;\nexport type RouteRequires = { token: boolean; labCoat: boolean; scope?: Scope; uc?: UseCase };\nexport type Route = { verb: Verb; name: string, endpoint: Endpoint; requires: RouteRequires; middleware: RequestHandler[] };\nexport type Routes = { route: Uri; middleware: RequestHandler[]; endpoints: List<Route> };\n\nconst toRoute = (endpoint: Endpoint, requires: RouteRequires, verb?: Verb, middleware?: RequestHandler[], name = ''): Optional<Route> =>\n tryTo(verb)\n .is.defined()\n .map(verb => ({ verb, name, endpoint, requires, middleware: middleware ?? [] }) as Route)\n .orElse();\n\nclass Router implements Routes {\n constructor(readonly resource: Resource) {}\n\n get route(): Uri {\n return meta(this.resource).get('route');\n }\n\n get middleware(): RequestHandler[] {\n return meta(this.resource).get<RequestHandler[]>('middleware') ?? [];\n }\n\n get endpoints(): List<Route> {\n return meta(this.resource)\n .properties('verb')\n .mapDefined(v =>\n toRoute(\n this.resource[v.property],\n {\n labCoat: v.get<boolean>('labCoat') ?? false,\n token: v.get<boolean>('token') ?? false,\n scope: v.get<Scope>('scope'),\n uc: v.get<UseCase>('uc'),\n },\n v.get<Verb>('verb'),\n v.get<RequestHandler[]>('middleware'),\n v.property.toString(),\n )\n );\n }\n}\n\nexport const routes = (resource: Resource): Routes => new Router(resource);\n","import { meta, CacheControl, ContentType, HttpStatus, HttpVerb } from '@thisisagile/easy';\n\nexport type VerbOptions = { onOk?: HttpStatus; onNotFound?: HttpStatus; onError?: HttpStatus; type?: ContentType; cache?: CacheControl };\nexport type Verb = { verb: HttpVerb; options: VerbOptions };\n\nexport const toVerbOptions = (options?: VerbOptions): Required<VerbOptions> => ({\n onOk: options?.onOk ?? HttpStatus.Ok,\n onNotFound: options?.onNotFound ?? HttpStatus.NotFound,\n onError: options?.onError ?? HttpStatus.BadRequest,\n type: options?.type ?? ContentType.Json,\n cache: options?.cache ?? CacheControl.disabled(),\n});\n\nconst toVerb =\n <T>(verb: HttpVerb, options?: VerbOptions): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('verb', { verb, options });\n };\n\nexport const get = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Get, options);\n\nexport const search = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Get, { onNotFound: HttpStatus.Ok, ...options });\n\nexport const put = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Put, options);\n\nexport const patch = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Patch, options);\n\nexport const post = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Post, { onOk: HttpStatus.Created, ...options });\n\nexport const del = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Delete, { onOk: HttpStatus.NoContent, ...options });\n\nexport const stream = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Get, { type: ContentType.Stream, ...options });\n","import { ErrorOrigin, isError } from '@thisisagile/easy';\nimport { VerbOptions } from './Verb';\n\nexport class OriginatedError extends Error {\n constructor(\n readonly origin: ErrorOrigin,\n readonly options?: VerbOptions\n ) {\n super();\n if (isError(origin)) this.stack = origin.stack;\n }\n}\n\nexport const isOriginatedError = (e?: unknown): e is OriginatedError => isError(e) && e instanceof OriginatedError;\n\nexport const toOriginatedError = (e: unknown, options?: VerbOptions): OriginatedError =>\n isOriginatedError(e) ? e : new OriginatedError(e as ErrorOrigin, options);\n","import { meta } from '@thisisagile/easy';\nimport { Scope, UseCase } from '@thisisagile/easy';\n\nexport class Requires {\n readonly labCoat =\n (): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('labCoat', true);\n };\n\n readonly token =\n (): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n };\n\n readonly scope =\n (scope: Scope): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n meta(subject).property(property).set('scope', scope);\n };\n\n readonly useCase =\n (uc: UseCase): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n meta(subject).property(property).set('uc', uc);\n };\n}\n\nexport const requires = new Requires();\n","import { AppProvider, Handler } from './AppProvider';\nimport { Constructor, Enum, List, toList, tryTo } from '@thisisagile/easy';\nimport { Resource } from './Resource';\n\nexport class Service extends Enum {\n protected port = 8080;\n\n constructor(\n readonly name: string,\n protected app: AppProvider,\n protected resources: List<Resource> = toList()\n ) {\n super(name);\n }\n\n pre = (): Handler[] => [];\n post = (): Handler[] => [];\n\n with(...resources: Constructor<Resource>[]): this {\n return tryTo(this).accept(t => t.resources.add(resources.map(r => new r()))).value;\n }\n\n atPort(port: number): this {\n return tryTo(this).accept(t => (t.port = port)).value;\n }\n\n start(message = `Service ${this.name} listening on port ${this.port} with ${this.resources.length} resources.`): void {\n tryTo(this)\n .accept(t => t.pre().forEach(h => this.app.use(h)))\n .accept(t => t.resources.forEach(r => this.app.route(this, r)))\n .accept(t => t.post().forEach(h => this.app.use(h)))\n .accept(t => t.app.listen(this.port, message));\n }\n}\n","import { Algorithm, sign, verify } from 'jsonwebtoken';\nimport { ctx, Json, OneOrMore, Optional, tryTo, Validatable, Jwt as JwtBase } from '@thisisagile/easy';\n\ninterface SignOptions {\n audience?: Optional<OneOrMore<string>>;\n issuer?: Optional<string>;\n}\nexport class Jwt extends JwtBase implements Validatable {\n static of = (a: { jwt: string }): Jwt => new Jwt(a.jwt);\n\n get isValid(): boolean {\n return (\n tryTo(() => ctx.env.get('tokenPublicKey') ?? '')\n .map(key => verify(this.value, key))\n .map(() => true)\n .orElse() ?? false\n );\n }\n\n static sign = (token: Json, options?: SignOptions): Jwt =>\n tryTo(() => ctx.env.get('tokenPrivateKey') ?? '')\n .is.not.empty()\n .map(key =>\n sign(token, key, {\n ...options,\n expiresIn: ctx.env.get('tokenExpiresIn') as any ?? '1h',\n keyid: ctx.env.get('tokenKeyid') ?? 'easy',\n algorithm: ctx.env.get('tokenAlgorithm', 'RS256') as Algorithm,\n })\n )\n .map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,KAAK;AAAA,EAEnB;AAAA,EAEA,OAAO,OAAO,CAAC,OAAa,gBAC1B,oBAAM,MAAM,iBAAI,IAAI,IAAI,iBAAiB,KAAK,EAAE,EAC7C,GAAG,IAAI,MAAM,EACb;AAAA,IAAI,aACH,0BAAK,OAAO,KAAK;AAAA,MACf,GAAG;AAAA,MACH,WAAW,iBAAI,IAAI,IAAI,gBAAgB,KAAY;AAAA,MACnD,OAAO,iBAAI,IAAI,IAAI,YAAY,KAAK;AAAA,MACpC,WAAW,iBAAI,IAAI,IAAI,kBAAkB,OAAO;AAAA,IAClD,CAAC;AAAA,EACH,EACC,IAAI,OAAK,IAAI,KAAI,CAAC,CAAC,EAAE;AAC5B;","names":["import_easy","import_easy","uri","requires","verb","import_easy","import_easy","import_easy","import_easy","import_easy","JwtBase"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/health/HealthResource.ts","../src/health/HealthUri.ts","../src/resources/Route.ts","../src/http/Verb.ts","../src/http/OriginatedError.ts","../src/resources/Requires.ts","../src/resources/Service.ts","../src/security/Jwt.ts"],"sourcesContent":["export * from './health/HealthResource';\nexport * from './health/HealthUri';\nexport * from './http/OriginatedError';\nexport * from './http/Verb';\nexport * from './resources/AppProvider';\nexport * from './resources/Requires';\nexport * from './resources/Resource';\nexport * from './resources/Route';\nexport * from './resources/Service';\nexport * from './security/Jwt';\n","import { resolve, Json } from '@thisisagile/easy';\nimport { HealthUri } from './HealthUri';\nimport { Resource } from '../resources/Resource';\nimport { route } from '../resources/Route';\nimport { get } from '../http/Verb';\n\n@route(HealthUri.Health)\nexport class HealthResource implements Resource {\n @get()\n ok = (): Promise<Json> => resolve({ state: 'Service is healthy.' });\n}\n","import { EasyUri, uri } from '@thisisagile/easy';\n\nexport class HealthUri extends EasyUri {\n static readonly health = uri.segment('health');\n static readonly Health = new HealthUri([HealthUri.health]);\n}\n","import { List, meta, Optional, tryTo, Uri, Scope, UseCase, Req } from '@thisisagile/easy';\nimport { Resource } from './Resource';\nimport { RequestHandler } from 'express';\nimport { Verb } from '../http/Verb';\n\nexport const route =\n (uri: Uri): ClassDecorator =>\n (subject: unknown): void => {\n meta(subject).set('route', uri);\n };\n\nexport type Endpoint<T = unknown> = (re: Req) => Promise<T | List<T>>;\nexport type RouteRequires = { token: boolean; labCoat: boolean; scope?: Scope; uc?: UseCase };\nexport type Route = { verb: Verb; name: string, endpoint: Endpoint; requires: RouteRequires; middleware: RequestHandler[] };\nexport type Routes = { route: Uri; middleware: RequestHandler[]; endpoints: List<Route> };\n\nconst toRoute = (endpoint: Endpoint, requires: RouteRequires, verb?: Verb, middleware?: RequestHandler[], name = ''): Optional<Route> =>\n tryTo(verb)\n .is.defined()\n .map(verb => ({ verb, name, endpoint, requires, middleware: middleware ?? [] }) as Route)\n .orElse();\n\nclass Router implements Routes {\n constructor(readonly resource: Resource) {}\n\n get route(): Uri {\n return meta(this.resource).get('route');\n }\n\n get middleware(): RequestHandler[] {\n return meta(this.resource).get<RequestHandler[]>('middleware') ?? [];\n }\n\n get endpoints(): List<Route> {\n return meta(this.resource)\n .properties('verb')\n .mapDefined(v =>\n toRoute(\n this.resource[v.property],\n {\n labCoat: v.get<boolean>('labCoat') ?? false,\n token: v.get<boolean>('token') ?? false,\n scope: v.get<Scope>('scope'),\n uc: v.get<UseCase>('uc'),\n },\n v.get<Verb>('verb'),\n v.get<RequestHandler[]>('middleware'),\n v.property.toString(),\n )\n );\n }\n}\n\nexport const routes = (resource: Resource): Routes => new Router(resource);\n","import { meta, CacheControl, ContentType, HttpStatus, HttpVerb } from '@thisisagile/easy';\n\nexport type VerbOptions = { onOk?: HttpStatus; onNotFound?: HttpStatus; onError?: HttpStatus; type?: ContentType; cache?: CacheControl };\nexport type Verb = { verb: HttpVerb; options: VerbOptions };\n\nexport const toVerbOptions = (options?: VerbOptions): Required<VerbOptions> => ({\n onOk: options?.onOk ?? HttpStatus.Ok,\n onNotFound: options?.onNotFound ?? HttpStatus.NotFound,\n onError: options?.onError ?? HttpStatus.BadRequest,\n type: options?.type ?? ContentType.Json,\n cache: options?.cache ?? CacheControl.disabled(),\n});\n\nconst toVerb =\n <T>(verb: HttpVerb, options?: VerbOptions): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('verb', { verb, options });\n };\n\nexport const get = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Get, options);\n\nexport const search = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Get, { onNotFound: HttpStatus.Ok, ...options });\n\nexport const put = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Put, options);\n\nexport const patch = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Patch, options);\n\nexport const post = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Post, { onOk: HttpStatus.Created, ...options });\n\nexport const del = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Delete, { onOk: HttpStatus.NoContent, ...options });\n\nexport const stream = (options?: VerbOptions): PropertyDecorator => toVerb(HttpVerb.Get, { type: ContentType.Stream, ...options });\n","import { ErrorOrigin, isError } from '@thisisagile/easy';\nimport { VerbOptions } from './Verb';\n\nexport class OriginatedError extends Error {\n constructor(\n readonly origin: ErrorOrigin,\n readonly options?: VerbOptions\n ) {\n super();\n if (isError(origin)) this.stack = origin.stack;\n }\n}\n\nexport const isOriginatedError = (e?: unknown): e is OriginatedError => isError(e) && e instanceof OriginatedError;\n\nexport const toOriginatedError = (e: unknown, options?: VerbOptions): OriginatedError =>\n isOriginatedError(e) ? e : new OriginatedError(e as ErrorOrigin, options);\n","import { meta } from '@thisisagile/easy';\nimport { Scope, UseCase } from '@thisisagile/easy';\n\nexport class Requires {\n readonly labCoat =\n (): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('labCoat', true);\n };\n\n readonly token =\n (): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n };\n\n readonly scope =\n (scope: Scope): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n meta(subject).property(property).set('scope', scope);\n };\n\n readonly useCase =\n (uc: UseCase): PropertyDecorator =>\n (subject: unknown, property: string | symbol): void => {\n meta(subject).property(property).set('token', true);\n meta(subject).property(property).set('uc', uc);\n };\n}\n\nexport const requires = new Requires();\n","import { AppProvider, Handler } from './AppProvider';\nimport { Constructor, Enum, List, toList, tryTo } from '@thisisagile/easy';\nimport { Resource } from './Resource';\n\nexport class Service extends Enum {\n protected port = 8080;\n\n constructor(\n readonly name: string,\n protected app: AppProvider,\n protected resources: List<Resource> = toList()\n ) {\n super(name);\n }\n\n pre = (): Handler[] => [];\n post = (): Handler[] => [];\n\n with(...resources: Constructor<Resource>[]): this {\n return tryTo(this).accept(t => t.resources.add(resources.map(r => new r()))).value;\n }\n\n atPort(port: number): this {\n return tryTo(this).accept(t => (t.port = port)).value;\n }\n\n start(message = `Service ${this.name} listening on port ${this.port} with ${this.resources.length} resources.`): void {\n tryTo(this)\n .accept(t => t.pre().forEach(h => this.app.use(h)))\n .accept(t => t.resources.forEach(r => this.app.route(this, r)))\n .accept(t => t.post().forEach(h => this.app.use(h)))\n .accept(t => t.app.listen(this.port, message));\n }\n}\n","import jwt from 'jsonwebtoken';\nimport type { Algorithm } from 'jsonwebtoken';\nimport { ctx, Json, OneOrMore, Optional, tryTo, Validatable, Jwt as JwtBase } from '@thisisagile/easy';\n\ninterface SignOptions {\n audience?: Optional<OneOrMore<string>>;\n issuer?: Optional<string>;\n}\nexport class Jwt extends JwtBase implements Validatable {\n static of = (a: { jwt: string }): Jwt => new Jwt(a.jwt);\n\n get isValid(): boolean {\n return (\n tryTo(() => ctx.env.get('tokenPublicKey') ?? '')\n .map(key => jwt.verify(this.value, key))\n .map(() => true)\n .orElse() ?? false\n );\n }\n\n static sign = (token: Json, options?: SignOptions): Jwt =>\n tryTo(() => ctx.env.get('tokenPrivateKey') ?? '')\n .is.not.empty()\n .map(key =>\n jwt.sign(token, key, {\n ...options,\n expiresIn: (ctx.env.get('tokenExpiresIn') as any) ?? '1h',\n keyid: ctx.env.get('tokenKeyid') ?? 'easy',\n algorithm: ctx.env.get('tokenAlgorithm', 'RS256') as Algorithm,\n })\n )\n .map(s => new Jwt(s)).value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,eAA8B;;;ACA9B,kBAA6B;AAEtB,IAAM,YAAN,MAAM,mBAAkB,oBAAQ;AAAA,EACrC,OAAgB,SAAS,gBAAI,QAAQ,QAAQ;AAAA,EAC7C,OAAgB,SAAS,IAAI,WAAU,CAAC,WAAU,MAAM,CAAC;AAC3D;;;ACLA,IAAAC,eAAsE;AAK/D,IAAM,QACX,CAACC,SACD,CAAC,YAA2B;AAC1B,yBAAK,OAAO,EAAE,IAAI,SAASA,IAAG;AAChC;AAOF,IAAM,UAAU,CAAC,UAAoBC,WAAyB,MAAa,YAA+B,OAAO,WAC/G,oBAAM,IAAI,EACP,GAAG,QAAQ,EACX,IAAI,CAAAC,WAAS,EAAE,MAAAA,OAAM,MAAM,UAAU,UAAAD,WAAU,YAAY,cAAc,CAAC,EAAE,EAAW,EACvF,OAAO;AAEZ,IAAM,SAAN,MAA+B;AAAA,EAC7B,YAAqB,UAAoB;AAApB;AAAA,EAAqB;AAAA,EAE1C,IAAI,QAAa;AACf,eAAO,mBAAK,KAAK,QAAQ,EAAE,IAAI,OAAO;AAAA,EACxC;AAAA,EAEA,IAAI,aAA+B;AACjC,eAAO,mBAAK,KAAK,QAAQ,EAAE,IAAsB,YAAY,KAAK,CAAC;AAAA,EACrE;AAAA,EAEA,IAAI,YAAyB;AAC3B,eAAO,mBAAK,KAAK,QAAQ,EACtB,WAAW,MAA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,EAEtD,IAAI,UAAmB;AACrB,eACE,oBAAM,MAAM,iBAAI,IAAI,IAAI,gBAAgB,KAAK,EAAE,EAC5C,IAAI,SAAO,oBAAAC,QAAI,OAAO,KAAK,OAAO,GAAG,CAAC,EACtC,IAAI,MAAM,IAAI,EACd,OAAO,KAAK;AAAA,EAEnB;AAAA,EAEA,OAAO,OAAO,CAAC,OAAa,gBAC1B,oBAAM,MAAM,iBAAI,IAAI,IAAI,iBAAiB,KAAK,EAAE,EAC7C,GAAG,IAAI,MAAM,EACb;AAAA,IAAI,SACH,oBAAAA,QAAI,KAAK,OAAO,KAAK;AAAA,MACnB,GAAG;AAAA,MACH,WAAY,iBAAI,IAAI,IAAI,gBAAgB,KAAa;AAAA,MACrD,OAAO,iBAAI,IAAI,IAAI,YAAY,KAAK;AAAA,MACpC,WAAW,iBAAI,IAAI,IAAI,kBAAkB,OAAO;AAAA,IAClD,CAAC;AAAA,EACH,EACC,IAAI,OAAK,IAAI,KAAI,CAAC,CAAC,EAAE;AAC5B;","names":["import_easy","import_easy","uri","requires","verb","import_easy","import_easy","import_easy","import_easy","import_easy","JwtBase","jwt"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import {
|
|
2
|
+
Service
|
|
3
|
+
} from "./chunk-RJWBTUKZ.mjs";
|
|
4
|
+
import {
|
|
5
|
+
Jwt
|
|
6
|
+
} from "./chunk-4D3CGIDW.mjs";
|
|
7
|
+
import {
|
|
8
|
+
HealthResource
|
|
9
|
+
} from "./chunk-KHAYZJCB.mjs";
|
|
10
|
+
import {
|
|
11
|
+
route,
|
|
12
|
+
routes
|
|
13
|
+
} from "./chunk-B37TXK4C.mjs";
|
|
14
|
+
import {
|
|
15
|
+
HealthUri
|
|
16
|
+
} from "./chunk-2XMYNSTI.mjs";
|
|
17
|
+
import {
|
|
18
|
+
OriginatedError,
|
|
19
|
+
isOriginatedError,
|
|
20
|
+
toOriginatedError
|
|
21
|
+
} from "./chunk-YB7NDS5U.mjs";
|
|
22
|
+
import {
|
|
23
|
+
del,
|
|
24
|
+
get,
|
|
25
|
+
patch,
|
|
26
|
+
post,
|
|
27
|
+
put,
|
|
28
|
+
search,
|
|
29
|
+
stream,
|
|
30
|
+
toVerbOptions
|
|
31
|
+
} from "./chunk-W62PBGRF.mjs";
|
|
32
|
+
import "./chunk-VXGHOHGY.mjs";
|
|
33
|
+
import {
|
|
34
|
+
Requires,
|
|
35
|
+
requires
|
|
36
|
+
} from "./chunk-OQUQPJ6S.mjs";
|
|
37
|
+
import "./chunk-4N72FQFX.mjs";
|
|
38
|
+
import "./chunk-BTPC4SVP.mjs";
|
|
39
|
+
export {
|
|
40
|
+
HealthResource,
|
|
41
|
+
HealthUri,
|
|
42
|
+
Jwt,
|
|
43
|
+
OriginatedError,
|
|
44
|
+
Requires,
|
|
45
|
+
Service,
|
|
46
|
+
del,
|
|
47
|
+
get,
|
|
48
|
+
isOriginatedError,
|
|
49
|
+
patch,
|
|
50
|
+
post,
|
|
51
|
+
put,
|
|
52
|
+
requires,
|
|
53
|
+
route,
|
|
54
|
+
routes,
|
|
55
|
+
search,
|
|
56
|
+
stream,
|
|
57
|
+
toOriginatedError,
|
|
58
|
+
toVerbOptions
|
|
59
|
+
};
|
|
11
60
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,24 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Requires,
|
|
3
|
+
requires
|
|
4
|
+
} from "../chunk-OQUQPJ6S.mjs";
|
|
1
5
|
import "../chunk-4N72FQFX.mjs";
|
|
2
|
-
|
|
3
|
-
// src/resources/Requires.ts
|
|
4
|
-
import { meta } from "@thisisagile/easy";
|
|
5
|
-
var Requires = class {
|
|
6
|
-
labCoat = () => (subject, property) => {
|
|
7
|
-
meta(subject).property(property).set("labCoat", true);
|
|
8
|
-
};
|
|
9
|
-
token = () => (subject, property) => {
|
|
10
|
-
meta(subject).property(property).set("token", true);
|
|
11
|
-
};
|
|
12
|
-
scope = (scope) => (subject, property) => {
|
|
13
|
-
meta(subject).property(property).set("token", true);
|
|
14
|
-
meta(subject).property(property).set("scope", scope);
|
|
15
|
-
};
|
|
16
|
-
useCase = (uc) => (subject, property) => {
|
|
17
|
-
meta(subject).property(property).set("token", true);
|
|
18
|
-
meta(subject).property(property).set("uc", uc);
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
var requires = new Requires();
|
|
22
6
|
export {
|
|
23
7
|
Requires,
|
|
24
8
|
requires
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,27 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Service
|
|
3
|
+
} from "../chunk-RJWBTUKZ.mjs";
|
|
1
4
|
import "../chunk-4N72FQFX.mjs";
|
|
2
|
-
|
|
3
|
-
// src/resources/Service.ts
|
|
4
|
-
import { Enum, toList, tryTo } from "@thisisagile/easy";
|
|
5
|
-
var Service = class extends Enum {
|
|
6
|
-
constructor(name, app, resources = toList()) {
|
|
7
|
-
super(name);
|
|
8
|
-
this.name = name;
|
|
9
|
-
this.app = app;
|
|
10
|
-
this.resources = resources;
|
|
11
|
-
}
|
|
12
|
-
port = 8080;
|
|
13
|
-
pre = () => [];
|
|
14
|
-
post = () => [];
|
|
15
|
-
with(...resources) {
|
|
16
|
-
return tryTo(this).accept((t) => t.resources.add(resources.map((r) => new r()))).value;
|
|
17
|
-
}
|
|
18
|
-
atPort(port) {
|
|
19
|
-
return tryTo(this).accept((t) => t.port = port).value;
|
|
20
|
-
}
|
|
21
|
-
start(message = `Service ${this.name} listening on port ${this.port} with ${this.resources.length} resources.`) {
|
|
22
|
-
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));
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
5
|
export {
|
|
26
6
|
Service
|
|
27
7
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/security/Jwt.mjs
CHANGED
|
@@ -1,22 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Jwt
|
|
3
|
+
} from "../chunk-4D3CGIDW.mjs";
|
|
1
4
|
import "../chunk-4N72FQFX.mjs";
|
|
2
|
-
|
|
3
|
-
// src/security/Jwt.ts
|
|
4
|
-
import { sign, verify } from "jsonwebtoken";
|
|
5
|
-
import { ctx, tryTo, Jwt as JwtBase } from "@thisisagile/easy";
|
|
6
|
-
var Jwt = class _Jwt extends JwtBase {
|
|
7
|
-
static of = (a) => new _Jwt(a.jwt);
|
|
8
|
-
get isValid() {
|
|
9
|
-
return tryTo(() => ctx.env.get("tokenPublicKey") ?? "").map((key) => verify(this.value, key)).map(() => true).orElse() ?? false;
|
|
10
|
-
}
|
|
11
|
-
static sign = (token, options) => tryTo(() => ctx.env.get("tokenPrivateKey") ?? "").is.not.empty().map(
|
|
12
|
-
(key) => sign(token, key, {
|
|
13
|
-
...options,
|
|
14
|
-
expiresIn: ctx.env.get("tokenExpiresIn") ?? "1h",
|
|
15
|
-
keyid: ctx.env.get("tokenKeyid") ?? "easy",
|
|
16
|
-
algorithm: ctx.env.get("tokenAlgorithm", "RS256")
|
|
17
|
-
})
|
|
18
|
-
).map((s) => new _Jwt(s)).value;
|
|
19
|
-
};
|
|
20
5
|
export {
|
|
21
6
|
Jwt
|
|
22
7
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thisisagile/easy-service",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "19.0.1",
|
|
4
4
|
"description": "Straightforward library for building domain-driven microservice architectures",
|
|
5
5
|
"author": "Sander Hoogendoorn",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"lint": "yarn g:eslint . --ext .js,.jsx,.ts,.tsx --fix",
|
|
22
22
|
"format": "yarn g:prettier --check --write src test *.json",
|
|
23
|
-
"build": "yarn
|
|
24
|
-
"build:tsup": "yarn g:tsup
|
|
23
|
+
"build": "yarn build:tsup",
|
|
24
|
+
"build:tsup": "yarn g:tsup && yarn g:tsc --emitDeclarationOnly --declaration",
|
|
25
25
|
"test": "yarn g:jest --coverage",
|
|
26
26
|
"prepack": "yarn g:copy-readme"
|
|
27
27
|
},
|
|
@@ -37,7 +37,57 @@
|
|
|
37
37
|
"@thisisagile/easy-test": "*"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@thisisagile/easy": "^
|
|
40
|
+
"@thisisagile/easy": "^19.0.1",
|
|
41
41
|
"jsonwebtoken": "^9.0.2"
|
|
42
|
+
},
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"types": "./dist/index.d.ts",
|
|
46
|
+
"import": "./dist/index.mjs",
|
|
47
|
+
"require": "./dist/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./health/HealthResource": {
|
|
50
|
+
"types": "./dist/health/HealthResource.d.ts",
|
|
51
|
+
"import": "./dist/health/HealthResource.mjs"
|
|
52
|
+
},
|
|
53
|
+
"./health/HealthUri": {
|
|
54
|
+
"types": "./dist/health/HealthUri.d.ts",
|
|
55
|
+
"import": "./dist/health/HealthUri.mjs"
|
|
56
|
+
},
|
|
57
|
+
"./http/OriginatedError": {
|
|
58
|
+
"types": "./dist/http/OriginatedError.d.ts",
|
|
59
|
+
"import": "./dist/http/OriginatedError.mjs"
|
|
60
|
+
},
|
|
61
|
+
"./http/Verb": {
|
|
62
|
+
"types": "./dist/http/Verb.d.ts",
|
|
63
|
+
"import": "./dist/http/Verb.mjs"
|
|
64
|
+
},
|
|
65
|
+
"./resources/AppProvider": {
|
|
66
|
+
"types": "./dist/resources/AppProvider.d.ts",
|
|
67
|
+
"import": "./dist/resources/AppProvider.mjs"
|
|
68
|
+
},
|
|
69
|
+
"./resources/Requires": {
|
|
70
|
+
"types": "./dist/resources/Requires.d.ts",
|
|
71
|
+
"import": "./dist/resources/Requires.mjs"
|
|
72
|
+
},
|
|
73
|
+
"./resources/Resource": {
|
|
74
|
+
"types": "./dist/resources/Resource.d.ts",
|
|
75
|
+
"import": "./dist/resources/Resource.mjs"
|
|
76
|
+
},
|
|
77
|
+
"./resources/Route": {
|
|
78
|
+
"types": "./dist/resources/Route.d.ts",
|
|
79
|
+
"import": "./dist/resources/Route.mjs"
|
|
80
|
+
},
|
|
81
|
+
"./resources/Service": {
|
|
82
|
+
"types": "./dist/resources/Service.d.ts",
|
|
83
|
+
"import": "./dist/resources/Service.mjs"
|
|
84
|
+
},
|
|
85
|
+
"./security/Jwt": {
|
|
86
|
+
"types": "./dist/security/Jwt.d.ts",
|
|
87
|
+
"import": "./dist/security/Jwt.mjs"
|
|
88
|
+
},
|
|
89
|
+
"./src/*": "./src/*",
|
|
90
|
+
"./dist/*": "./dist/*",
|
|
91
|
+
"./package.json": "./package.json"
|
|
42
92
|
}
|
|
43
93
|
}
|
package/src/security/Jwt.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
import type { Algorithm } from 'jsonwebtoken';
|
|
2
3
|
import { ctx, Json, OneOrMore, Optional, tryTo, Validatable, Jwt as JwtBase } from '@thisisagile/easy';
|
|
3
4
|
|
|
4
5
|
interface SignOptions {
|
|
@@ -11,7 +12,7 @@ export class Jwt extends JwtBase implements Validatable {
|
|
|
11
12
|
get isValid(): boolean {
|
|
12
13
|
return (
|
|
13
14
|
tryTo(() => ctx.env.get('tokenPublicKey') ?? '')
|
|
14
|
-
.map(key => verify(this.value, key))
|
|
15
|
+
.map(key => jwt.verify(this.value, key))
|
|
15
16
|
.map(() => true)
|
|
16
17
|
.orElse() ?? false
|
|
17
18
|
);
|
|
@@ -21,9 +22,9 @@ export class Jwt extends JwtBase implements Validatable {
|
|
|
21
22
|
tryTo(() => ctx.env.get('tokenPrivateKey') ?? '')
|
|
22
23
|
.is.not.empty()
|
|
23
24
|
.map(key =>
|
|
24
|
-
sign(token, key, {
|
|
25
|
+
jwt.sign(token, key, {
|
|
25
26
|
...options,
|
|
26
|
-
expiresIn: ctx.env.get('tokenExpiresIn') as any ?? '1h',
|
|
27
|
+
expiresIn: (ctx.env.get('tokenExpiresIn') as any) ?? '1h',
|
|
27
28
|
keyid: ctx.env.get('tokenKeyid') ?? 'easy',
|
|
28
29
|
algorithm: ctx.env.get('tokenAlgorithm', 'RS256') as Algorithm,
|
|
29
30
|
})
|