@aep-foundation/fastify 0.1.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # @aep-foundation/fastify
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#1](https://github.com/aep-foundation/aep-node/pull/1) [`3d4713e`](https://github.com/aep-foundation/aep-node/commit/3d4713e2a9cf85478a415192c7c0abc90c374073) Thanks [@nkavian](https://github.com/nkavian)! - Add the initial AEP Node SDK packages, framework adapters, and conformance helpers.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`3d4713e`](https://github.com/aep-foundation/aep-node/commit/3d4713e2a9cf85478a415192c7c0abc90c374073)]:
12
+ - @aep-foundation/core@0.1.0
13
+ - @aep-foundation/service@0.1.0
14
+
15
+ ## 0.0.0
16
+
17
+ Initial development version.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AEP Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @aep-foundation/fastify
2
+
3
+ Thin Fastify adapter for `@aep-foundation/service`.
4
+
5
+ `createFastifyAepPlugin()` registers only the Inspect route. Use
6
+ `createFastifyAepRoutesPlugin()` to register Inspect plus Enroll, Status, Grant,
7
+ and Revoke command routes from the Service Inspect metadata.
package/dist/index.cjs ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AEP_FASTIFY_MEDIA_TYPE: () => AEP_FASTIFY_MEDIA_TYPE,
24
+ AEP_FASTIFY_WELL_KNOWN_PATH: () => AEP_FASTIFY_WELL_KNOWN_PATH,
25
+ createFastifyAepHandler: () => createFastifyAepHandler,
26
+ createFastifyAepHandlers: () => createFastifyAepHandlers,
27
+ createFastifyAepPlugin: () => createFastifyAepPlugin,
28
+ createFastifyAepRoutesPlugin: () => createFastifyAepRoutesPlugin,
29
+ packageName: () => packageName
30
+ });
31
+ module.exports = __toCommonJS(index_exports);
32
+ var import_core = require("@aep-foundation/core");
33
+ var import_service = require("@aep-foundation/service");
34
+ var packageName = "@aep-foundation/fastify";
35
+ var AEP_FASTIFY_WELL_KNOWN_PATH = "/.well-known/aep";
36
+ var AEP_FASTIFY_MEDIA_TYPE = "application/aep+json";
37
+ function createFastifyAepHandler(input) {
38
+ const service = resolveService(input);
39
+ return (_request, reply) => {
40
+ reply.status?.(200);
41
+ return reply.type(AEP_FASTIFY_MEDIA_TYPE).send(service.inspectDocument());
42
+ };
43
+ }
44
+ function createFastifyAepHandlers(input) {
45
+ const service = resolveService(input);
46
+ return {
47
+ enroll: createFastifyCommandHandler(service, "enroll"),
48
+ grant: createFastifyCommandHandler(service, "grant"),
49
+ inspect: createFastifyAepHandler(service),
50
+ revoke: createFastifyCommandHandler(service, "revoke"),
51
+ status: createFastifyCommandHandler(service, "status")
52
+ };
53
+ }
54
+ function createFastifyAepPlugin(input, path = AEP_FASTIFY_WELL_KNOWN_PATH) {
55
+ return (fastify) => {
56
+ fastify.get(path, createFastifyAepHandler(input));
57
+ return Promise.resolve();
58
+ };
59
+ }
60
+ function createFastifyAepRoutesPlugin(input, inspectPath = AEP_FASTIFY_WELL_KNOWN_PATH) {
61
+ return (fastify) => {
62
+ const service = resolveService(input);
63
+ const handlers = createFastifyAepHandlers(service);
64
+ const inspect = service.inspectDocument();
65
+ fastify.get(inspectPath, handlers.inspect);
66
+ fastify.post((0, import_core.commandPathFromInspect)(inspect, "enroll"), handlers.enroll);
67
+ fastify.get((0, import_core.commandPathFromInspect)(inspect, "status"), handlers.status);
68
+ fastify.post((0, import_core.commandPathFromInspect)(inspect, "grant"), handlers.grant);
69
+ fastify.post((0, import_core.commandPathFromInspect)(inspect, "revoke"), handlers.revoke);
70
+ return Promise.resolve();
71
+ };
72
+ }
73
+ function createFastifyCommandHandler(service, command) {
74
+ return async (request, reply) => {
75
+ const clientAssertion = parseAepAuthorization(headerValue(request.headers, "authorization"));
76
+ const idempotencyKey = headerValue(request.headers, "idempotency-key");
77
+ const commandOptions = authenticatedOptions(clientAssertion, idempotencyKey);
78
+ const result = command === "enroll" ? await service.enroll(request.body, commandOptions) : command === "status" ? await service.status({ clientAssertion }) : command === "grant" ? await service.grant(request.body, commandOptions) : await service.revoke(request.body, commandOptions);
79
+ reply.status?.(result.status);
80
+ return reply.type(result.contentType).send(result.body);
81
+ };
82
+ }
83
+ function authenticatedOptions(clientAssertion, idempotencyKey) {
84
+ return {
85
+ clientAssertion,
86
+ ...idempotencyKey === void 0 ? {} : { idempotencyKey }
87
+ };
88
+ }
89
+ function headerValue(headers, name) {
90
+ const value = headers?.[name] ?? headers?.[name.toLowerCase()];
91
+ return Array.isArray(value) ? value[0] : value;
92
+ }
93
+ function parseAepAuthorization(authorization) {
94
+ const prefix = "AEP ";
95
+ if (authorization?.startsWith(prefix)) {
96
+ return authorization.slice(prefix.length);
97
+ }
98
+ return "";
99
+ }
100
+ function resolveService(input) {
101
+ return "inspectDocument" in input ? input : (0, import_service.createAepService)(input);
102
+ }
103
+ // Annotate the CommonJS export names for ESM import in node:
104
+ 0 && (module.exports = {
105
+ AEP_FASTIFY_MEDIA_TYPE,
106
+ AEP_FASTIFY_WELL_KNOWN_PATH,
107
+ createFastifyAepHandler,
108
+ createFastifyAepHandlers,
109
+ createFastifyAepPlugin,
110
+ createFastifyAepRoutesPlugin,
111
+ packageName
112
+ });
113
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { commandPathFromInspect } from \"@aep-foundation/core\";\nimport { createAepService } from \"@aep-foundation/service\";\nimport type {\n AepAuthenticatedServiceOptions,\n AepService,\n AepServiceOptions\n} from \"@aep-foundation/service\";\n\nexport const packageName = \"@aep-foundation/fastify\";\nexport const AEP_FASTIFY_WELL_KNOWN_PATH = \"/.well-known/aep\";\nexport const AEP_FASTIFY_MEDIA_TYPE = \"application/aep+json\";\n\nexport interface FastifyAepReply {\n send(body: unknown): unknown;\n status?(code: number): FastifyAepReply;\n type(contentType: string): FastifyAepReply;\n}\n\nexport interface FastifyAepRequest {\n body?: unknown;\n headers?: Record<string, string | string[] | undefined>;\n}\n\nexport type FastifyAepHandler = (request: FastifyAepRequest, reply: FastifyAepReply) => unknown;\n\nexport interface FastifyAepInspectInstance {\n get(path: string, handler: FastifyAepHandler): unknown;\n}\n\nexport interface FastifyAepInstance extends FastifyAepInspectInstance {\n post(path: string, handler: FastifyAepHandler): unknown;\n}\n\nexport type FastifyAepInspectPlugin = (fastify: FastifyAepInspectInstance) => Promise<void>;\nexport type FastifyAepPlugin = (fastify: FastifyAepInstance) => Promise<void>;\nexport type FastifyAepServiceInput = AepService | AepServiceOptions;\n\nexport interface FastifyAepRouteHandlers {\n enroll: FastifyAepHandler;\n grant: FastifyAepHandler;\n inspect: FastifyAepHandler;\n revoke: FastifyAepHandler;\n status: FastifyAepHandler;\n}\n\nexport function createFastifyAepHandler(input: FastifyAepServiceInput): FastifyAepHandler {\n const service = resolveService(input);\n\n return (_request, reply) => {\n reply.status?.(200);\n return reply.type(AEP_FASTIFY_MEDIA_TYPE).send(service.inspectDocument());\n };\n}\n\nexport function createFastifyAepHandlers(input: FastifyAepServiceInput): FastifyAepRouteHandlers {\n const service = resolveService(input);\n\n return {\n enroll: createFastifyCommandHandler(service, \"enroll\"),\n grant: createFastifyCommandHandler(service, \"grant\"),\n inspect: createFastifyAepHandler(service),\n revoke: createFastifyCommandHandler(service, \"revoke\"),\n status: createFastifyCommandHandler(service, \"status\")\n };\n}\n\nexport function createFastifyAepPlugin(\n input: FastifyAepServiceInput,\n path = AEP_FASTIFY_WELL_KNOWN_PATH\n): FastifyAepInspectPlugin {\n return (fastify) => {\n fastify.get(path, createFastifyAepHandler(input));\n return Promise.resolve();\n };\n}\n\nexport function createFastifyAepRoutesPlugin(\n input: FastifyAepServiceInput,\n inspectPath = AEP_FASTIFY_WELL_KNOWN_PATH\n): FastifyAepPlugin {\n return (fastify) => {\n const service = resolveService(input);\n const handlers = createFastifyAepHandlers(service);\n const inspect = service.inspectDocument();\n\n fastify.get(inspectPath, handlers.inspect);\n fastify.post(commandPathFromInspect(inspect, \"enroll\"), handlers.enroll);\n fastify.get(commandPathFromInspect(inspect, \"status\"), handlers.status);\n fastify.post(commandPathFromInspect(inspect, \"grant\"), handlers.grant);\n fastify.post(commandPathFromInspect(inspect, \"revoke\"), handlers.revoke);\n return Promise.resolve();\n };\n}\n\nfunction createFastifyCommandHandler(\n service: AepService,\n command: \"enroll\" | \"grant\" | \"revoke\" | \"status\"\n): FastifyAepHandler {\n return async (request, reply) => {\n const clientAssertion = parseAepAuthorization(headerValue(request.headers, \"authorization\"));\n const idempotencyKey = headerValue(request.headers, \"idempotency-key\");\n const commandOptions = authenticatedOptions(clientAssertion, idempotencyKey);\n const result =\n command === \"enroll\"\n ? await service.enroll(request.body, commandOptions)\n : command === \"status\"\n ? await service.status({ clientAssertion })\n : command === \"grant\"\n ? await service.grant(request.body, commandOptions)\n : await service.revoke(request.body, commandOptions);\n\n reply.status?.(result.status);\n return reply.type(result.contentType).send(result.body);\n };\n}\n\nfunction authenticatedOptions(\n clientAssertion: string,\n idempotencyKey: string | undefined\n): AepAuthenticatedServiceOptions {\n return {\n clientAssertion,\n ...(idempotencyKey === undefined ? {} : { idempotencyKey })\n };\n}\n\nfunction headerValue(\n headers: Record<string, string | string[] | undefined> | undefined,\n name: string\n): string | undefined {\n const value = headers?.[name] ?? headers?.[name.toLowerCase()];\n\n return Array.isArray(value) ? value[0] : value;\n}\n\nfunction parseAepAuthorization(authorization: string | undefined): string {\n const prefix = \"AEP \";\n\n if (authorization?.startsWith(prefix)) {\n return authorization.slice(prefix.length);\n }\n\n return \"\";\n}\n\nfunction resolveService(input: FastifyAepServiceInput): AepService {\n return \"inspectDocument\" in input ? input : createAepService(input);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kB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AS,UAAU;AAC/B,UAAM,kBAAkB,sBAAsB,YAAY,QAAQ,SAAS,eAAe,CAAC;AAC3F,UAAM,iBAAiB,YAAY,QAAQ,SAAS,iBAAiB;AACrE,UAAM,iBAAiB,qBAAqB,iBAAiB,cAAc;AAC3E,UAAM,SACJ,YAAY,WACR,MAAM,QAAQ,OAAO,QAAQ,MAAM,cAAc,IACjD,YAAY,WACV,MAAM,QAAQ,OAAO,EAAE,gBAAgB,CAAC,IACxC,YAAY,UACV,MAAM,QAAQ,MAAM,QAAQ,MAAM,cAAc,IAChD,MAAM,QAAQ,OAAO,QAAQ,MAAM,cAAc;AAE3D,UAAM,SAAS,OAAO,MAAM;AAC5B,WAAO,MAAM,KAAK,OAAO,WAAW,EAAE,KAAK,OAAO,IAAI;AAAA,EACxD;AACF;AAEA,SAAS,qBACP,iBACA,gBACgC;AAChC,SAAO;AAAA,IACL;AAAA,IACA,GAAI,mBAAmB,SAAY,CAAC,IAAI,EAAE,eAAe;AAAA,EAC3D;AACF;AAEA,SAAS,YACP,SACA,MACoB;AACpB,QAAM,QAAQ,UAAU,IAAI,KAAK,UAAU,KAAK,YAAY,CAAC;AAE7D,SAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,CAAC,IAAI;AAC3C;AAEA,SAAS,sBAAsB,eAA2C;AACxE,QAAM,SAAS;AAEf,MAAI,eAAe,WAAW,MAAM,GAAG;AACrC,WAAO,cAAc,MAAM,OAAO,MAAM;AAAA,EAC1C;AAEA,SAAO;AACT;AAEA,SAAS,eAAe,OAA2C;AACjE,SAAO,qBAAqB,QAAQ,YAAQ,iCAAiB,KAAK;AACpE;","names":[]}
@@ -0,0 +1,37 @@
1
+ import { AepService, AepServiceOptions } from '@aep-foundation/service';
2
+
3
+ declare const packageName = "@aep-foundation/fastify";
4
+ declare const AEP_FASTIFY_WELL_KNOWN_PATH = "/.well-known/aep";
5
+ declare const AEP_FASTIFY_MEDIA_TYPE = "application/aep+json";
6
+ interface FastifyAepReply {
7
+ send(body: unknown): unknown;
8
+ status?(code: number): FastifyAepReply;
9
+ type(contentType: string): FastifyAepReply;
10
+ }
11
+ interface FastifyAepRequest {
12
+ body?: unknown;
13
+ headers?: Record<string, string | string[] | undefined>;
14
+ }
15
+ type FastifyAepHandler = (request: FastifyAepRequest, reply: FastifyAepReply) => unknown;
16
+ interface FastifyAepInspectInstance {
17
+ get(path: string, handler: FastifyAepHandler): unknown;
18
+ }
19
+ interface FastifyAepInstance extends FastifyAepInspectInstance {
20
+ post(path: string, handler: FastifyAepHandler): unknown;
21
+ }
22
+ type FastifyAepInspectPlugin = (fastify: FastifyAepInspectInstance) => Promise<void>;
23
+ type FastifyAepPlugin = (fastify: FastifyAepInstance) => Promise<void>;
24
+ type FastifyAepServiceInput = AepService | AepServiceOptions;
25
+ interface FastifyAepRouteHandlers {
26
+ enroll: FastifyAepHandler;
27
+ grant: FastifyAepHandler;
28
+ inspect: FastifyAepHandler;
29
+ revoke: FastifyAepHandler;
30
+ status: FastifyAepHandler;
31
+ }
32
+ declare function createFastifyAepHandler(input: FastifyAepServiceInput): FastifyAepHandler;
33
+ declare function createFastifyAepHandlers(input: FastifyAepServiceInput): FastifyAepRouteHandlers;
34
+ declare function createFastifyAepPlugin(input: FastifyAepServiceInput, path?: string): FastifyAepInspectPlugin;
35
+ declare function createFastifyAepRoutesPlugin(input: FastifyAepServiceInput, inspectPath?: string): FastifyAepPlugin;
36
+
37
+ export { AEP_FASTIFY_MEDIA_TYPE, AEP_FASTIFY_WELL_KNOWN_PATH, type FastifyAepHandler, type FastifyAepInspectInstance, type FastifyAepInspectPlugin, type FastifyAepInstance, type FastifyAepPlugin, type FastifyAepReply, type FastifyAepRequest, type FastifyAepRouteHandlers, type FastifyAepServiceInput, createFastifyAepHandler, createFastifyAepHandlers, createFastifyAepPlugin, createFastifyAepRoutesPlugin, packageName };
@@ -0,0 +1,37 @@
1
+ import { AepService, AepServiceOptions } from '@aep-foundation/service';
2
+
3
+ declare const packageName = "@aep-foundation/fastify";
4
+ declare const AEP_FASTIFY_WELL_KNOWN_PATH = "/.well-known/aep";
5
+ declare const AEP_FASTIFY_MEDIA_TYPE = "application/aep+json";
6
+ interface FastifyAepReply {
7
+ send(body: unknown): unknown;
8
+ status?(code: number): FastifyAepReply;
9
+ type(contentType: string): FastifyAepReply;
10
+ }
11
+ interface FastifyAepRequest {
12
+ body?: unknown;
13
+ headers?: Record<string, string | string[] | undefined>;
14
+ }
15
+ type FastifyAepHandler = (request: FastifyAepRequest, reply: FastifyAepReply) => unknown;
16
+ interface FastifyAepInspectInstance {
17
+ get(path: string, handler: FastifyAepHandler): unknown;
18
+ }
19
+ interface FastifyAepInstance extends FastifyAepInspectInstance {
20
+ post(path: string, handler: FastifyAepHandler): unknown;
21
+ }
22
+ type FastifyAepInspectPlugin = (fastify: FastifyAepInspectInstance) => Promise<void>;
23
+ type FastifyAepPlugin = (fastify: FastifyAepInstance) => Promise<void>;
24
+ type FastifyAepServiceInput = AepService | AepServiceOptions;
25
+ interface FastifyAepRouteHandlers {
26
+ enroll: FastifyAepHandler;
27
+ grant: FastifyAepHandler;
28
+ inspect: FastifyAepHandler;
29
+ revoke: FastifyAepHandler;
30
+ status: FastifyAepHandler;
31
+ }
32
+ declare function createFastifyAepHandler(input: FastifyAepServiceInput): FastifyAepHandler;
33
+ declare function createFastifyAepHandlers(input: FastifyAepServiceInput): FastifyAepRouteHandlers;
34
+ declare function createFastifyAepPlugin(input: FastifyAepServiceInput, path?: string): FastifyAepInspectPlugin;
35
+ declare function createFastifyAepRoutesPlugin(input: FastifyAepServiceInput, inspectPath?: string): FastifyAepPlugin;
36
+
37
+ export { AEP_FASTIFY_MEDIA_TYPE, AEP_FASTIFY_WELL_KNOWN_PATH, type FastifyAepHandler, type FastifyAepInspectInstance, type FastifyAepInspectPlugin, type FastifyAepInstance, type FastifyAepPlugin, type FastifyAepReply, type FastifyAepRequest, type FastifyAepRouteHandlers, type FastifyAepServiceInput, createFastifyAepHandler, createFastifyAepHandlers, createFastifyAepPlugin, createFastifyAepRoutesPlugin, packageName };
package/dist/index.js ADDED
@@ -0,0 +1,82 @@
1
+ // src/index.ts
2
+ import { commandPathFromInspect } from "@aep-foundation/core";
3
+ import { createAepService } from "@aep-foundation/service";
4
+ var packageName = "@aep-foundation/fastify";
5
+ var AEP_FASTIFY_WELL_KNOWN_PATH = "/.well-known/aep";
6
+ var AEP_FASTIFY_MEDIA_TYPE = "application/aep+json";
7
+ function createFastifyAepHandler(input) {
8
+ const service = resolveService(input);
9
+ return (_request, reply) => {
10
+ reply.status?.(200);
11
+ return reply.type(AEP_FASTIFY_MEDIA_TYPE).send(service.inspectDocument());
12
+ };
13
+ }
14
+ function createFastifyAepHandlers(input) {
15
+ const service = resolveService(input);
16
+ return {
17
+ enroll: createFastifyCommandHandler(service, "enroll"),
18
+ grant: createFastifyCommandHandler(service, "grant"),
19
+ inspect: createFastifyAepHandler(service),
20
+ revoke: createFastifyCommandHandler(service, "revoke"),
21
+ status: createFastifyCommandHandler(service, "status")
22
+ };
23
+ }
24
+ function createFastifyAepPlugin(input, path = AEP_FASTIFY_WELL_KNOWN_PATH) {
25
+ return (fastify) => {
26
+ fastify.get(path, createFastifyAepHandler(input));
27
+ return Promise.resolve();
28
+ };
29
+ }
30
+ function createFastifyAepRoutesPlugin(input, inspectPath = AEP_FASTIFY_WELL_KNOWN_PATH) {
31
+ return (fastify) => {
32
+ const service = resolveService(input);
33
+ const handlers = createFastifyAepHandlers(service);
34
+ const inspect = service.inspectDocument();
35
+ fastify.get(inspectPath, handlers.inspect);
36
+ fastify.post(commandPathFromInspect(inspect, "enroll"), handlers.enroll);
37
+ fastify.get(commandPathFromInspect(inspect, "status"), handlers.status);
38
+ fastify.post(commandPathFromInspect(inspect, "grant"), handlers.grant);
39
+ fastify.post(commandPathFromInspect(inspect, "revoke"), handlers.revoke);
40
+ return Promise.resolve();
41
+ };
42
+ }
43
+ function createFastifyCommandHandler(service, command) {
44
+ return async (request, reply) => {
45
+ const clientAssertion = parseAepAuthorization(headerValue(request.headers, "authorization"));
46
+ const idempotencyKey = headerValue(request.headers, "idempotency-key");
47
+ const commandOptions = authenticatedOptions(clientAssertion, idempotencyKey);
48
+ const result = command === "enroll" ? await service.enroll(request.body, commandOptions) : command === "status" ? await service.status({ clientAssertion }) : command === "grant" ? await service.grant(request.body, commandOptions) : await service.revoke(request.body, commandOptions);
49
+ reply.status?.(result.status);
50
+ return reply.type(result.contentType).send(result.body);
51
+ };
52
+ }
53
+ function authenticatedOptions(clientAssertion, idempotencyKey) {
54
+ return {
55
+ clientAssertion,
56
+ ...idempotencyKey === void 0 ? {} : { idempotencyKey }
57
+ };
58
+ }
59
+ function headerValue(headers, name) {
60
+ const value = headers?.[name] ?? headers?.[name.toLowerCase()];
61
+ return Array.isArray(value) ? value[0] : value;
62
+ }
63
+ function parseAepAuthorization(authorization) {
64
+ const prefix = "AEP ";
65
+ if (authorization?.startsWith(prefix)) {
66
+ return authorization.slice(prefix.length);
67
+ }
68
+ return "";
69
+ }
70
+ function resolveService(input) {
71
+ return "inspectDocument" in input ? input : createAepService(input);
72
+ }
73
+ export {
74
+ AEP_FASTIFY_MEDIA_TYPE,
75
+ AEP_FASTIFY_WELL_KNOWN_PATH,
76
+ createFastifyAepHandler,
77
+ createFastifyAepHandlers,
78
+ createFastifyAepPlugin,
79
+ createFastifyAepRoutesPlugin,
80
+ packageName
81
+ };
82
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { commandPathFromInspect } from \"@aep-foundation/core\";\nimport { createAepService } from \"@aep-foundation/service\";\nimport type {\n AepAuthenticatedServiceOptions,\n AepService,\n AepServiceOptions\n} from \"@aep-foundation/service\";\n\nexport const packageName = \"@aep-foundation/fastify\";\nexport const AEP_FASTIFY_WELL_KNOWN_PATH = \"/.well-known/aep\";\nexport const AEP_FASTIFY_MEDIA_TYPE = \"application/aep+json\";\n\nexport interface FastifyAepReply {\n send(body: unknown): unknown;\n status?(code: number): FastifyAepReply;\n type(contentType: string): FastifyAepReply;\n}\n\nexport interface FastifyAepRequest {\n body?: unknown;\n headers?: Record<string, string | string[] | undefined>;\n}\n\nexport type FastifyAepHandler = (request: FastifyAepRequest, reply: FastifyAepReply) => unknown;\n\nexport interface FastifyAepInspectInstance {\n get(path: string, handler: FastifyAepHandler): unknown;\n}\n\nexport interface FastifyAepInstance extends FastifyAepInspectInstance {\n post(path: string, handler: FastifyAepHandler): unknown;\n}\n\nexport type FastifyAepInspectPlugin = (fastify: FastifyAepInspectInstance) => Promise<void>;\nexport type FastifyAepPlugin = (fastify: FastifyAepInstance) => Promise<void>;\nexport type FastifyAepServiceInput = AepService | AepServiceOptions;\n\nexport interface FastifyAepRouteHandlers {\n enroll: FastifyAepHandler;\n grant: FastifyAepHandler;\n inspect: FastifyAepHandler;\n revoke: FastifyAepHandler;\n status: FastifyAepHandler;\n}\n\nexport function createFastifyAepHandler(input: FastifyAepServiceInput): FastifyAepHandler {\n const service = resolveService(input);\n\n return (_request, reply) => {\n reply.status?.(200);\n return reply.type(AEP_FASTIFY_MEDIA_TYPE).send(service.inspectDocument());\n };\n}\n\nexport function createFastifyAepHandlers(input: FastifyAepServiceInput): FastifyAepRouteHandlers {\n const service = resolveService(input);\n\n return {\n enroll: createFastifyCommandHandler(service, \"enroll\"),\n grant: createFastifyCommandHandler(service, \"grant\"),\n inspect: createFastifyAepHandler(service),\n revoke: createFastifyCommandHandler(service, \"revoke\"),\n status: createFastifyCommandHandler(service, \"status\")\n };\n}\n\nexport function createFastifyAepPlugin(\n input: FastifyAepServiceInput,\n path = AEP_FASTIFY_WELL_KNOWN_PATH\n): FastifyAepInspectPlugin {\n return (fastify) => {\n fastify.get(path, createFastifyAepHandler(input));\n return Promise.resolve();\n };\n}\n\nexport function createFastifyAepRoutesPlugin(\n input: FastifyAepServiceInput,\n inspectPath = AEP_FASTIFY_WELL_KNOWN_PATH\n): FastifyAepPlugin {\n return (fastify) => {\n const service = resolveService(input);\n const handlers = createFastifyAepHandlers(service);\n const inspect = service.inspectDocument();\n\n fastify.get(inspectPath, handlers.inspect);\n fastify.post(commandPathFromInspect(inspect, \"enroll\"), handlers.enroll);\n fastify.get(commandPathFromInspect(inspect, \"status\"), handlers.status);\n fastify.post(commandPathFromInspect(inspect, \"grant\"), handlers.grant);\n fastify.post(commandPathFromInspect(inspect, \"revoke\"), handlers.revoke);\n return Promise.resolve();\n };\n}\n\nfunction createFastifyCommandHandler(\n service: AepService,\n command: \"enroll\" | \"grant\" | \"revoke\" | \"status\"\n): FastifyAepHandler {\n return async (request, reply) => {\n const clientAssertion = parseAepAuthorization(headerValue(request.headers, \"authorization\"));\n const idempotencyKey = headerValue(request.headers, \"idempotency-key\");\n const commandOptions = authenticatedOptions(clientAssertion, idempotencyKey);\n const result =\n command === \"enroll\"\n ? await service.enroll(request.body, commandOptions)\n : command === \"status\"\n ? await service.status({ clientAssertion })\n : command === \"grant\"\n ? await service.grant(request.body, commandOptions)\n : await service.revoke(request.body, commandOptions);\n\n reply.status?.(result.status);\n return reply.type(result.contentType).send(result.body);\n };\n}\n\nfunction authenticatedOptions(\n clientAssertion: string,\n idempotencyKey: string | undefined\n): AepAuthenticatedServiceOptions {\n return {\n clientAssertion,\n ...(idempotencyKey === undefined ? {} : { idempotencyKey })\n };\n}\n\nfunction headerValue(\n headers: Record<string, string | string[] | undefined> | undefined,\n name: string\n): string | undefined {\n const value = headers?.[name] ?? headers?.[name.toLowerCase()];\n\n return Array.isArray(value) ? value[0] : value;\n}\n\nfunction parseAepAuthorization(authorization: string | undefined): string {\n const prefix = \"AEP \";\n\n if (authorization?.startsWith(prefix)) {\n return authorization.slice(prefix.length);\n }\n\n return \"\";\n}\n\nfunction resolveService(input: FastifyAepServiceInput): AepService {\n return \"inspectDocument\" in input ? input : createAepService(input);\n}\n"],"mappings":";AAAA,SAAS,8BAA8B;AACvC,SAAS,wBAAwB;AAO1B,IAAM,cAAc;AACpB,IAAM,8BAA8B;AACpC,IAAM,yB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3F,UAAM,iBAAiB,YAAY,QAAQ,SAAS,iBAAiB;AACrE,UAAM,iBAAiB,qBAAqB,iBAAiB,cAAc;AAC3E,UAAM,SACJ,YAAY,WACR,MAAM,QAAQ,OAAO,QAAQ,MAAM,cAAc,IACjD,YAAY,WACV,MAAM,QAAQ,OAAO,EAAE,gBAAgB,CAAC,IACxC,YAAY,UACV,MAAM,QAAQ,MAAM,QAAQ,MAAM,cAAc,IAChD,MAAM,QAAQ,OAAO,QAAQ,MAAM,cAAc;AAE3D,UAAM,SAAS,OAAO,MAAM;AAC5B,WAAO,MAAM,KAAK,OAAO,WAAW,EAAE,KAAK,OAAO,IAAI;AAAA,EACxD;AACF;AAEA,SAAS,qBACP,iBACA,gBACgC;AAChC,SAAO;AAAA,IACL;AAAA,IACA,GAAI,mBAAmB,SAAY,CAAC,IAAI,EAAE,eAAe;AAAA,EAC3D;AACF;AAEA,SAAS,YACP,SACA,MACoB;AACpB,QAAM,QAAQ,UAAU,IAAI,KAAK,UAAU,KAAK,YAAY,CAAC;AAE7D,SAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,CAAC,IAAI;AAC3C;AAEA,SAAS,sBAAsB,eAA2C;AACxE,QAAM,SAAS;AAEf,MAAI,eAAe,WAAW,MAAM,GAAG;AACrC,WAAO,cAAc,MAAM,OAAO,MAAM;AAAA,EAC1C;AAEA,SAAO;AACT;AAEA,SAAS,eAAe,OAA2C;AACjE,SAAO,qBAAqB,QAAQ,QAAQ,iBAAiB,KAAK;AACpE;","names":[]}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@aep-foundation/fastify",
3
+ "version": "0.1.0",
4
+ "description": "Fastify adapter for AEP Service handlers.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "sideEffects": false,
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "node": {
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.cjs"
16
+ },
17
+ "default": "./dist/index.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "CHANGELOG.md",
24
+ "README.md",
25
+ "LICENSE"
26
+ ],
27
+ "engines": {
28
+ "node": ">=22.0.0"
29
+ },
30
+ "license": "MIT",
31
+ "dependencies": {
32
+ "@aep-foundation/core": "^0.1.0",
33
+ "@aep-foundation/service": "^0.1.0"
34
+ },
35
+ "peerDependencies": {
36
+ "fastify": ">=5"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "provenance": true
41
+ },
42
+ "devDependencies": {
43
+ "fastify": "^5.8.5"
44
+ },
45
+ "scripts": {
46
+ "build": "tsup",
47
+ "dev": "tsup --watch",
48
+ "test": "vitest run --coverage",
49
+ "test:watch": "vitest",
50
+ "lint": "eslint src test --max-warnings 0",
51
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
52
+ "clean": "rm -rf dist .turbo coverage"
53
+ }
54
+ }