@ankhorage/api 0.3.0 → 0.4.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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ankhorage/api
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 0581f54: Compose manifest-declared API operations with approved handlers, explicit app/project scope, and fail-closed authorization.
8
+
9
+ ## 0.3.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 683b338: Migrate API capabilities to the singular Contracts 26 declaration and the published standalone Capability ID validator.
14
+
3
15
  ## 0.3.0
4
16
 
5
17
  ### Minor Changes
package/README.md CHANGED
@@ -3,22 +3,62 @@
3
3
 
4
4
  # @ankhorage/api
5
5
 
6
- Framework-neutral executable API runtime for Ankhorage.
6
+ ![license: MIT](././paradox/badges/license.svg) ![npm: v0.4.0](././paradox/badges/npm.svg) ![runtime: bun](././paradox/badges/runtime.svg) ![typescript: strict](././paradox/badges/typescript.svg) ![eslint: checked](././paradox/badges/eslint.svg) ![prettier: checked](././paradox/badges/prettier.svg) ![build: checked](././paradox/badges/build.svg) ![tests: checked](././paradox/badges/tests.svg) ![paradox: canonical](././paradox/badges/docs.svg)
7
7
 
8
- It consumes portable API definitions from `@ankhorage/contracts`, resolves executable operation
9
- bindings, dispatches application handlers, and exposes a transport-adapter boundary without
10
- depending on Fastify, Next.js, React, or another server framework.
8
+ Framework-neutral executable API runtime for Ankhorage.
11
9
 
12
10
  ## Usage
13
11
 
12
+ ### CLI
13
+
14
+ Ankhorage packages expose their command-line interface through `ankh`. Use `ankh --help` to discover available package commands, or run a package command with `--help` for package-specific usage.
15
+
16
+ ```zsh
17
+ # Install the Ankhorage CLI
18
+ bun add --global @ankhorage/ankh
19
+
20
+ # Show usage information for api
21
+ ankh api --help
22
+ ```
23
+
14
24
  ### Basic Usage
15
25
 
16
26
  `@ankhorage/api` executes portable internal REST API definitions without coupling application
17
27
  handlers to Fastify, Next.js, or another HTTP framework. Framework packages adapt requests and
18
28
  responses at the transport edge while this runtime owns operation lookup and dispatch.
19
29
 
20
- See `examples/basic-usage/index.ts` for the executable example.
30
+ ```ts
31
+ const definition = {
32
+ id: "health-api",
33
+ origin: "internal",
34
+ protocol: "rest",
35
+ basePath: "/api",
36
+ endpoints: {
37
+ health: {
38
+ id: "health",
39
+ kind: "http",
40
+ operations: {
41
+ "health.read": {
42
+ id: "health.read",
43
+ protocol: "http",
44
+ intent: "read",
45
+ method: "GET",
46
+ path: "/health",
47
+ },
48
+ },
49
+ },
50
+ },
51
+ } satisfies InternalRestApiDefinition;
52
+ ```
21
53
 
22
54
  ## Generated documentation
23
55
 
24
- Generated Paradox documentation is published with package releases.
56
+ - [Interactive documentation app](././paradox/index.html)
57
+ - [Public API reference](././paradox/exports.md)
58
+ - [Component registry](././paradox/components.md)
59
+ - [Architecture overview](././paradox/diagrams/architecture-overview.mmd)
60
+ - [Module relationships](././paradox/diagrams/module-relationships.mmd)
61
+ - [Export graph](././paradox/diagrams/export-graph.mmd)
62
+ - [composeAuthorizedApiRuntime sequence](././paradox/diagrams/sequences/compose-authorized-api-runtime.mmd)
63
+ - [createApiRuntime sequence](././paradox/diagrams/sequences/create-api-runtime.mmd)
64
+ - [projectApiCapabilities sequence](././paradox/diagrams/sequences/project-api-capabilities.mmd)
package/dist/api.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  export { CAPABILITIES } from "./capabilities/index.js";
2
+ export { composeAuthorizedApiRuntime } from "./features/execution/composeAuthorizedApiRuntime.js";
2
3
  export { createApiRuntime } from "./features/execution/createApiRuntime.js";
3
4
  export { createApiTransportHandler } from "./features/execution/createApiTransportHandler.js";
4
5
  export { projectApiCapabilities } from "./features/execution/projectApiCapabilities.js";
5
6
  export { resolveApiOperationBindings } from "./features/execution/resolveApiOperationBindings.js";
6
- export type { ApiHandler, ApiHandlerContext, ApiHandlerRegistry, ApiHandlerResponse, ApiOperationBinding, ApiRequest, ApiResponse, ApiRuntime, ApiTransportAdapter, } from "./types/api.js";
7
+ export type { ApiAuthorizationPolicy, ApiAuthorizationRequest, ApiCompositionContext, ApiHandler, ApiHandlerContext, ApiHandlerRegistry, ApiHandlerResolutionRequest, ApiHandlerResolver, ApiHandlerResponse, ApiOperationBinding, ApiRequest, ApiResponse, ApiRuntime, ApiTransportAdapter, ComposeAuthorizedApiRuntimeOptions, } from "./types/api.js";
7
8
  //# sourceMappingURL=api.d.ts.map
package/dist/api.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,yBAAyB,EAAE,MAAM,mDAAmD,CAAC;AAC9F,OAAO,EAAE,sBAAsB,EAAE,MAAM,gDAAgD,CAAC;AACxF,OAAO,EAAE,2BAA2B,EAAE,MAAM,qDAAqD,CAAC;AAClG,YAAY,EACV,UAAU,EACV,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,UAAU,EACV,WAAW,EACX,UAAU,EACV,mBAAmB,GACpB,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,2BAA2B,EAAE,MAAM,qDAAqD,CAAC;AAClG,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,yBAAyB,EAAE,MAAM,mDAAmD,CAAC;AAC9F,OAAO,EAAE,sBAAsB,EAAE,MAAM,gDAAgD,CAAC;AACxF,OAAO,EAAE,2BAA2B,EAAE,MAAM,qDAAqD,CAAC;AAClG,YAAY,EACV,sBAAsB,EACtB,uBAAuB,EACvB,qBAAqB,EACrB,UAAU,EACV,iBAAiB,EACjB,kBAAkB,EAClB,2BAA2B,EAC3B,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,UAAU,EACV,WAAW,EACX,UAAU,EACV,mBAAmB,EACnB,kCAAkC,GACnC,MAAM,gBAAgB,CAAC"}
package/dist/api.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { CAPABILITIES } from "./capabilities/index.js";
2
+ export { composeAuthorizedApiRuntime } from "./features/execution/composeAuthorizedApiRuntime.js";
2
3
  export { createApiRuntime } from "./features/execution/createApiRuntime.js";
3
4
  export { createApiTransportHandler } from "./features/execution/createApiTransportHandler.js";
4
5
  export { projectApiCapabilities } from "./features/execution/projectApiCapabilities.js";
package/dist/api.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,yBAAyB,EAAE,MAAM,mDAAmD,CAAC;AAC9F,OAAO,EAAE,sBAAsB,EAAE,MAAM,gDAAgD,CAAC;AACxF,OAAO,EAAE,2BAA2B,EAAE,MAAM,qDAAqD,CAAC","sourcesContent":["export { CAPABILITIES } from \"./capabilities/index.js\";\nexport { createApiRuntime } from \"./features/execution/createApiRuntime.js\";\nexport { createApiTransportHandler } from \"./features/execution/createApiTransportHandler.js\";\nexport { projectApiCapabilities } from \"./features/execution/projectApiCapabilities.js\";\nexport { resolveApiOperationBindings } from \"./features/execution/resolveApiOperationBindings.js\";\nexport type {\n ApiHandler,\n ApiHandlerContext,\n ApiHandlerRegistry,\n ApiHandlerResponse,\n ApiOperationBinding,\n ApiRequest,\n ApiResponse,\n ApiRuntime,\n ApiTransportAdapter,\n} from \"./types/api.js\";\n"]}
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,2BAA2B,EAAE,MAAM,qDAAqD,CAAC;AAClG,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,yBAAyB,EAAE,MAAM,mDAAmD,CAAC;AAC9F,OAAO,EAAE,sBAAsB,EAAE,MAAM,gDAAgD,CAAC;AACxF,OAAO,EAAE,2BAA2B,EAAE,MAAM,qDAAqD,CAAC","sourcesContent":["export { CAPABILITIES } from \"./capabilities/index.js\";\nexport { composeAuthorizedApiRuntime } from \"./features/execution/composeAuthorizedApiRuntime.js\";\nexport { createApiRuntime } from \"./features/execution/createApiRuntime.js\";\nexport { createApiTransportHandler } from \"./features/execution/createApiTransportHandler.js\";\nexport { projectApiCapabilities } from \"./features/execution/projectApiCapabilities.js\";\nexport { resolveApiOperationBindings } from \"./features/execution/resolveApiOperationBindings.js\";\nexport type {\n ApiAuthorizationPolicy,\n ApiAuthorizationRequest,\n ApiCompositionContext,\n ApiHandler,\n ApiHandlerContext,\n ApiHandlerRegistry,\n ApiHandlerResolutionRequest,\n ApiHandlerResolver,\n ApiHandlerResponse,\n ApiOperationBinding,\n ApiRequest,\n ApiResponse,\n ApiRuntime,\n ApiTransportAdapter,\n ComposeAuthorizedApiRuntimeOptions,\n} from \"./types/api.js\";\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/capabilities/index.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,EAA2C,CAAC","sourcesContent":["import type { Capability } from \"@ankhorage/contracts/capabilities\";\n\n/**\n * Static API package capabilities.\n *\n * Executable API operations are dynamic and are projected from authored API definitions by\n * `projectApiCapabilities`; they are intentionally not duplicated in this static catalog.\n */\nexport const CAPABILITIES = [] as const satisfies readonly Capability[];\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/capabilities/index.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,EAA2C,CAAC","sourcesContent":["import type { Capability } from \"@ankhorage/contracts/capability\";\n\n/**\n * Static API package capabilities.\n *\n * Executable API operations are dynamic and are projected from authored API definitions by\n * `projectApiCapabilities`; they are intentionally not duplicated in this static catalog.\n */\nexport const CAPABILITIES = [] as const satisfies readonly Capability[];\n"]}
@@ -0,0 +1,6 @@
1
+ import type { ComposeAuthorizedApiRuntimeOptions } from "../../types/api.js";
2
+ /***
3
+ * Compose declared API operations with host-approved handlers and deny-by-default authorization.
4
+ */
5
+ export declare function composeAuthorizedApiRuntime(options: ComposeAuthorizedApiRuntimeOptions): import("../../api.js").ApiRuntime;
6
+ //# sourceMappingURL=composeAuthorizedApiRuntime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"composeAuthorizedApiRuntime.d.ts","sourceRoot":"","sources":["../../../src/features/execution/composeAuthorizedApiRuntime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,kCAAkC,EACnC,MAAM,oBAAoB,CAAC;AAI5B;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,OAAO,EAAE,kCAAkC,qCAM5C"}
@@ -0,0 +1,63 @@
1
+ import { createApiRuntime } from "./createApiRuntime.js";
2
+ import { resolveApiOperationBindings } from "./resolveApiOperationBindings.js";
3
+ /***
4
+ * Compose declared API operations with host-approved handlers and deny-by-default authorization.
5
+ */
6
+ export function composeAuthorizedApiRuntime(options) {
7
+ assertCompositionContext(options.context);
8
+ const handlers = resolveAuthorizedHandlers(options);
9
+ return createApiRuntime({ definition: options.definition, handlers });
10
+ }
11
+ /*** Resolve every declared operation before exposing the API runtime. */
12
+ function resolveAuthorizedHandlers(options) {
13
+ return Object.fromEntries(resolveApiOperationBindings(options.definition).map((binding) => {
14
+ const handler = options.resolveHandler({
15
+ binding,
16
+ context: options.context,
17
+ });
18
+ if (!handler) {
19
+ throw new Error(`No approved handler is registered for API operation ${binding.operationId}.`);
20
+ }
21
+ return [
22
+ binding.operationId,
23
+ createAuthorizedHandler(binding, handler, options),
24
+ ];
25
+ }));
26
+ }
27
+ /*** Deny a request unless the host authorization policy explicitly approves it. */
28
+ function createAuthorizedHandler(binding, handler, options) {
29
+ return async (request) => {
30
+ const authorized = await isAuthorizedAsync(binding, request, options);
31
+ if (!authorized)
32
+ return forbiddenResponse(binding.operationId);
33
+ return handler(request, { binding, composition: options.context });
34
+ };
35
+ }
36
+ /*** Treat a failing authorization policy exactly like an explicit denial. */
37
+ async function isAuthorizedAsync(binding, request, options) {
38
+ try {
39
+ return await options.authorizeAsync({
40
+ binding,
41
+ context: options.context,
42
+ request,
43
+ });
44
+ }
45
+ catch {
46
+ return false;
47
+ }
48
+ }
49
+ /*** Reject incomplete composition scope before any HTTP route can be registered. */
50
+ function assertCompositionContext(context) {
51
+ if (!context.appId.trim() || !context.projectId.trim()) {
52
+ throw new Error("Authorized API composition requires non-empty app and project ids.");
53
+ }
54
+ }
55
+ /*** Return a normalized authorization failure without invoking the selected handler. */
56
+ function forbiddenResponse(operationId) {
57
+ return {
58
+ status: 403,
59
+ headers: { "content-type": "application/json" },
60
+ body: { error: { code: "forbidden", operationId } },
61
+ };
62
+ }
63
+ //# sourceMappingURL=composeAuthorizedApiRuntime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"composeAuthorizedApiRuntime.js","sourceRoot":"","sources":["../../../src/features/execution/composeAuthorizedApiRuntime.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AAE/E;;GAEG;AACH,MAAM,UAAU,2BAA2B,CACzC,OAA2C;IAE3C,wBAAwB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,QAAQ,GAAG,yBAAyB,CAAC,OAAO,CAA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;YACP,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,oFAAoF;AACpF,SAAS,wBAAwB,CAC/B,OAAsD;IAEtD,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;QACvD,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAC;IACJ,CAAC;AACH,CAAC;AAED,wFAAwF;AACxF,SAAS,iBAAiB,CAAC,WAAmB;IAC5C,OAAO;QACL,MAAM,EAAE,GAAG;QACX,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;QAC/C,IAAI,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,EAAE;KACpD,CAAC;AACJ,CAAC","sourcesContent":["import type {\n ApiHandler,\n ApiHandlerRegistry,\n ApiOperationBinding,\n ApiResponse,\n ComposeAuthorizedApiRuntimeOptions,\n} from \"../../types/api.js\";\nimport { createApiRuntime } from \"./createApiRuntime.js\";\nimport { resolveApiOperationBindings } from \"./resolveApiOperationBindings.js\";\n\n/***\n * Compose declared API operations with host-approved handlers and deny-by-default authorization.\n */\nexport function composeAuthorizedApiRuntime(\n options: ComposeAuthorizedApiRuntimeOptions,\n) {\n assertCompositionContext(options.context);\n const handlers = resolveAuthorizedHandlers(options);\n\n return createApiRuntime({ definition: options.definition, handlers });\n}\n\n/*** Resolve every declared operation before exposing the API runtime. */\nfunction resolveAuthorizedHandlers(\n options: ComposeAuthorizedApiRuntimeOptions,\n): ApiHandlerRegistry {\n return Object.fromEntries(\n resolveApiOperationBindings(options.definition).map((binding) => {\n const handler = options.resolveHandler({\n binding,\n context: options.context,\n });\n if (!handler) {\n throw new Error(\n `No approved handler is registered for API operation ${binding.operationId}.`,\n );\n }\n\n return [\n binding.operationId,\n createAuthorizedHandler(binding, handler, options),\n ];\n }),\n );\n}\n\n/*** Deny a request unless the host authorization policy explicitly approves it. */\nfunction createAuthorizedHandler(\n binding: ApiOperationBinding,\n handler: ApiHandler,\n options: ComposeAuthorizedApiRuntimeOptions,\n): ApiHandler {\n return async (request) => {\n const authorized = await isAuthorizedAsync(binding, request, options);\n if (!authorized) return forbiddenResponse(binding.operationId);\n\n return handler(request, { binding, composition: options.context });\n };\n}\n\n/*** Treat a failing authorization policy exactly like an explicit denial. */\nasync function isAuthorizedAsync(\n binding: ApiOperationBinding,\n request: Parameters<ApiHandler>[0],\n options: ComposeAuthorizedApiRuntimeOptions,\n): Promise<boolean> {\n try {\n return await options.authorizeAsync({\n binding,\n context: options.context,\n request,\n });\n } catch {\n return false;\n }\n}\n\n/*** Reject incomplete composition scope before any HTTP route can be registered. */\nfunction assertCompositionContext(\n context: ComposeAuthorizedApiRuntimeOptions[\"context\"],\n): void {\n if (!context.appId.trim() || !context.projectId.trim()) {\n throw new Error(\n \"Authorized API composition requires non-empty app and project ids.\",\n );\n }\n}\n\n/*** Return a normalized authorization failure without invoking the selected handler. */\nfunction forbiddenResponse(operationId: string): ApiResponse {\n return {\n status: 403,\n headers: { \"content-type\": \"application/json\" },\n body: { error: { code: \"forbidden\", operationId } },\n };\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { type Capability } from "@ankhorage/contracts/capabilities";
1
+ import type { Capability } from "@ankhorage/contracts/capability";
2
2
  import type { InternalRestApiDefinition } from "@ankhorage/contracts/data";
3
3
  /*** Project one internal REST API definition into canonical invocable API capabilities. */
4
4
  export declare function projectApiCapabilities(definition: InternalRestApiDefinition): readonly Capability[];
@@ -1 +1 @@
1
- {"version":3,"file":"projectApiCapabilities.d.ts","sourceRoot":"","sources":["../../../src/features/execution/projectApiCapabilities.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EAEhB,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAMV,yBAAyB,EAC1B,MAAM,2BAA2B,CAAC;AAEnC,2FAA2F;AAC3F,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,yBAAyB,GACpC,SAAS,UAAU,EAAE,CAWvB"}
1
+ {"version":3,"file":"projectApiCapabilities.d.ts","sourceRoot":"","sources":["../../../src/features/execution/projectApiCapabilities.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,KAAK,EAMV,yBAAyB,EAC1B,MAAM,2BAA2B,CAAC;AAEnC,2FAA2F;AAC3F,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,yBAAyB,GACpC,SAAS,UAAU,EAAE,CAWvB"}
@@ -1,4 +1,4 @@
1
- import { isCapabilityId, } from "@ankhorage/contracts/capabilities";
1
+ import { isCapabilityId } from "@ankhorage/capability";
2
2
  /*** Project one internal REST API definition into canonical invocable API capabilities. */
3
3
  export function projectApiCapabilities(definition) {
4
4
  const capabilities = Object.values(definition.endpoints).flatMap((endpoint) => Object.values(endpoint.operations).map((operation) => projectApiOperationCapability(definition, operation)));
@@ -1 +1 @@
1
- {"version":3,"file":"projectApiCapabilities.js","sourceRoot":"","sources":["../../../src/features/execution/projectApiCapabilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,cAAc,GACf,MAAM,mCAAmC,CAAC;AAU3C,2FAA2F;AAC3F,MAAM,UAAU,sBAAsB,CACpC,UAAqC;IAErC,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAC5E,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CACnD,6BAA6B,CAAC,UAAU,EAAE,SAAS,CAAC,CACrD,CACF,CAAC;IAEF,yBAAyB,CAAC,YAAY,CAAC,CAAC;IACxC,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yB,CAAC,YAAmC;IACpE,MAAM,IAAI,GAAG,IAAI,GAAG,EAAoB,CAAC;IACzC,KAAK,MAAM,UAAU,IAAI,YAAY,EAAE,CAAC;QACtC,IAAI,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CACb,0CAA0C,UAAU,CAAC,EAAE,EAAE,CAC1D,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAC1B,CAAC;AACH,CAAC","sourcesContent":["import {\n type Capability,\n isCapabilityId,\n} from \"@ankhorage/contracts/capabilities\";\nimport type {\n DataOperationConfig,\n DataOperationParameter,\n DataOperationRequest,\n DataSchema,\n DataSchemaSlot,\n InternalRestApiDefinition,\n} from \"@ankhorage/contracts/data\";\n\n/*** Project one internal REST API definition into canonical invocable API capabilities. */\nexport function projectApiCapabilities(\n definition: InternalRestApiDefinition,\n): readonly Capability[] {\n const capabilities = Object.values(definition.endpoints).flatMap((endpoint) =>\n Object.values(endpoint.operations).map((operation) =>\n projectApiOperationCapability(definition, operation),\n ),\n );\n\n assertUniqueCapabilityIds(capabilities);\n return [...capabilities].sort((left, right) =>\n left.id.localeCompare(right.id),\n );\n}\n\n/*** Project one authored API operation into its stable semantic capability descriptor. */\nfunction projectApiOperationCapability(\n definition: InternalRestApiDefinition,\n operation: DataOperationConfig,\n): Capability {\n const id = `api.${definition.id}.${operation.id}`;\n if (!isCapabilityId(id)) {\n throw new Error(\n `API operation projects to an invalid capability id: ${definition.id} / ${operation.id}`,\n );\n }\n\n const input = projectOperationInput(operation.request);\n const output = projectSchemaSlot(operation.response);\n\n return {\n id,\n owner: \"@ankhorage/api\",\n access: [\"invoke\"],\n binding: { kind: \"api\", bindableAs: [\"target\"] },\n label: operation.name ?? operation.id,\n ...(operation.description === undefined\n ? {}\n : { description: operation.description }),\n ...(input === undefined ? {} : { input }),\n ...(output === undefined ? {} : { output }),\n };\n}\n\n/*** Project request parameters and request body into the flat operation input value space. */\nfunction projectOperationInput(\n request: DataOperationRequest | undefined,\n): DataSchemaSlot | undefined {\n if (request === undefined) return undefined;\n\n const bodySchema = projectSchemaSlotAsSchema(request);\n const parameterSchema = projectOperationParameters(request.parameters);\n\n if (bodySchema === undefined) {\n return parameterSchema === undefined\n ? undefined\n : { schema: parameterSchema };\n }\n if (parameterSchema === undefined) return projectSchemaSlot(request);\n\n return {\n schema: {\n allOf: [bodySchema, parameterSchema],\n },\n };\n}\n\n/*** Project transport parameters into the flat object consumed by Runtime operation bindings. */\nfunction projectOperationParameters(\n parameters: readonly DataOperationParameter[] | undefined,\n): DataSchema | undefined {\n if (parameters === undefined || parameters.length === 0) return undefined;\n\n const properties: Record<string, DataSchema> = {};\n const required: string[] = [];\n for (const parameter of parameters) {\n if (Object.hasOwn(properties, parameter.name)) {\n throw new Error(\n `Duplicate API operation input parameter: ${parameter.name}`,\n );\n }\n properties[parameter.name] = projectOperationParameterSchema(parameter);\n if (parameter.required === true) required.push(parameter.name);\n }\n\n return {\n type: \"object\",\n properties,\n ...(required.length === 0 ? {} : { required: [...required].sort() }),\n };\n}\n\n/*** Preserve one parameter's schema reference plus parameter-specific description/default metadata. */\nfunction projectOperationParameterSchema(\n parameter: DataOperationParameter,\n): DataSchema {\n const schema = projectSchemaSlotAsSchema(parameter) ?? {};\n return {\n ...schema,\n ...(parameter.description === undefined\n ? {}\n : { description: parameter.description }),\n ...(parameter.default === undefined ? {} : { default: parameter.default }),\n };\n}\n\n/*** Preserve exactly one portable schema slot when the operation already owns one. */\nfunction projectSchemaSlot(\n slot: DataSchemaSlot | undefined,\n): DataSchemaSlot | undefined {\n if (slot?.schema !== undefined) return { schema: slot.schema };\n if (slot?.schemaRef !== undefined) return { schemaRef: slot.schemaRef };\n return undefined;\n}\n\n/*** Convert a schema slot to an embeddable schema while preserving authored schema references. */\nfunction projectSchemaSlotAsSchema(\n slot: DataSchemaSlot | undefined,\n): DataSchema | undefined {\n if (slot?.schema !== undefined) return slot.schema;\n if (slot?.schemaRef !== undefined) return { ref: slot.schemaRef };\n return undefined;\n}\n\n/*** Reject API definitions whose endpoint operations collide in the projected capability namespace. */\nfunction assertUniqueCapabilityIds(capabilities: readonly Capability[]): void {\n const seen = new Set<Capability[\"id\"]>();\n for (const capability of capabilities) {\n if (seen.has(capability.id)) {\n throw new Error(\n `Duplicate projected API capability id: ${capability.id}`,\n );\n }\n seen.add(capability.id);\n }\n}\n"]}
1
+ {"version":3,"file":"projectApiCapabilities.js","sourceRoot":"","sources":["../../../src/features/execution/projectApiCapabilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAWvD,2FAA2F;AAC3F,MAAM,UAAU,sBAAsB,CACpC,UAAqC;IAErC,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAC5E,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CACnD,6BAA6B,CAAC,UAAU,EAAE,SAAS,CAAC,CACrD,CACF,CAAC;IAEF,yBAAyB,CAAC,YAAY,CAAC,CAAC;IACxC,OAAO,CAAC,GAAG,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAC5C,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC,CAChC,CAAC;AACJ,CAAC;AAED,0FAA0F;AAC1F,SAAS,6BAA6B,CACpC,UAAqC,EACrC,SAA8B;IAE9B,MAAM,EAAE,GAAG,OAAO,UAAU,CAAC,EAAE,IAAI,SAAS,CAAC,EAAE,EAAE,CAAC;IAClD,IAAI,CAAC,cAAc,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CACb,uDA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\"@ankhorage/contracts/capability\";\nimport type {\n DataOperationConfig,\n DataOperationParameter,\n DataOperationRequest,\n DataSchema,\n DataSchemaSlot,\n InternalRestApiDefinition,\n} from \"@ankhorage/contracts/data\";\n\n/*** Project one internal REST API definition into canonical invocable API capabilities. */\nexport function projectApiCapabilities(\n definition: InternalRestApiDefinition,\n): readonly Capability[] {\n const capabilities = Object.values(definition.endpoints).flatMap((endpoint) =>\n Object.values(endpoint.operations).map((operation) =>\n projectApiOperationCapability(definition, operation),\n ),\n );\n\n assertUniqueCapabilityIds(capabilities);\n return [...capabilities].sort((left, right) =>\n left.id.localeCompare(right.id),\n );\n}\n\n/*** Project one authored API operation into its stable semantic capability descriptor. */\nfunction projectApiOperationCapability(\n definition: InternalRestApiDefinition,\n operation: DataOperationConfig,\n): Capability {\n const id = `api.${definition.id}.${operation.id}`;\n if (!isCapabilityId(id)) {\n throw new Error(\n `API operation projects to an invalid capability id: ${definition.id} / ${operation.id}`,\n );\n }\n\n const input = projectOperationInput(operation.request);\n const output = projectSchemaSlot(operation.response);\n\n return {\n id,\n owner: \"@ankhorage/api\",\n access: [\"invoke\"],\n binding: { kind: \"api\", bindableAs: [\"target\"] },\n label: operation.name ?? operation.id,\n ...(operation.description === undefined\n ? {}\n : { description: operation.description }),\n ...(input === undefined ? {} : { input }),\n ...(output === undefined ? {} : { output }),\n };\n}\n\n/*** Project request parameters and request body into the flat operation input value space. */\nfunction projectOperationInput(\n request: DataOperationRequest | undefined,\n): DataSchemaSlot | undefined {\n if (request === undefined) return undefined;\n\n const bodySchema = projectSchemaSlotAsSchema(request);\n const parameterSchema = projectOperationParameters(request.parameters);\n\n if (bodySchema === undefined) {\n return parameterSchema === undefined\n ? undefined\n : { schema: parameterSchema };\n }\n if (parameterSchema === undefined) return projectSchemaSlot(request);\n\n return {\n schema: {\n allOf: [bodySchema, parameterSchema],\n },\n };\n}\n\n/*** Project transport parameters into the flat object consumed by Runtime operation bindings. */\nfunction projectOperationParameters(\n parameters: readonly DataOperationParameter[] | undefined,\n): DataSchema | undefined {\n if (parameters === undefined || parameters.length === 0) return undefined;\n\n const properties: Record<string, DataSchema> = {};\n const required: string[] = [];\n for (const parameter of parameters) {\n if (Object.hasOwn(properties, parameter.name)) {\n throw new Error(\n `Duplicate API operation input parameter: ${parameter.name}`,\n );\n }\n properties[parameter.name] = projectOperationParameterSchema(parameter);\n if (parameter.required === true) required.push(parameter.name);\n }\n\n return {\n type: \"object\",\n properties,\n ...(required.length === 0 ? {} : { required: [...required].sort() }),\n };\n}\n\n/*** Preserve one parameter's schema reference plus parameter-specific description/default metadata. */\nfunction projectOperationParameterSchema(\n parameter: DataOperationParameter,\n): DataSchema {\n const schema = projectSchemaSlotAsSchema(parameter) ?? {};\n return {\n ...schema,\n ...(parameter.description === undefined\n ? {}\n : { description: parameter.description }),\n ...(parameter.default === undefined ? {} : { default: parameter.default }),\n };\n}\n\n/*** Preserve exactly one portable schema slot when the operation already owns one. */\nfunction projectSchemaSlot(\n slot: DataSchemaSlot | undefined,\n): DataSchemaSlot | undefined {\n if (slot?.schema !== undefined) return { schema: slot.schema };\n if (slot?.schemaRef !== undefined) return { schemaRef: slot.schemaRef };\n return undefined;\n}\n\n/*** Convert a schema slot to an embeddable schema while preserving authored schema references. */\nfunction projectSchemaSlotAsSchema(\n slot: DataSchemaSlot | undefined,\n): DataSchema | undefined {\n if (slot?.schema !== undefined) return slot.schema;\n if (slot?.schemaRef !== undefined) return { ref: slot.schemaRef };\n return undefined;\n}\n\n/*** Reject API definitions whose endpoint operations collide in the projected capability namespace. */\nfunction assertUniqueCapabilityIds(capabilities: readonly Capability[]): void {\n const seen = new Set<Capability[\"id\"]>();\n for (const capability of capabilities) {\n if (seen.has(capability.id)) {\n throw new Error(\n `Duplicate projected API capability id: ${capability.id}`,\n );\n }\n seen.add(capability.id);\n }\n}\n"]}
@@ -27,9 +27,31 @@ export interface ApiResponse {
27
27
  }
28
28
  export interface ApiHandlerContext {
29
29
  readonly binding: ApiOperationBinding;
30
+ readonly composition?: ApiCompositionContext;
30
31
  }
31
32
  export type ApiHandler = (request: ApiRequest, context: ApiHandlerContext) => ApiHandlerResponse | Promise<ApiHandlerResponse>;
32
33
  export type ApiHandlerRegistry = Readonly<Record<string, ApiHandler>>;
34
+ export interface ApiCompositionContext {
35
+ readonly appId: string;
36
+ readonly projectId: string;
37
+ }
38
+ export interface ApiHandlerResolutionRequest {
39
+ readonly binding: ApiOperationBinding;
40
+ readonly context: ApiCompositionContext;
41
+ }
42
+ export type ApiHandlerResolver = (request: ApiHandlerResolutionRequest) => ApiHandler | undefined;
43
+ export interface ApiAuthorizationRequest {
44
+ readonly binding: ApiOperationBinding;
45
+ readonly context: ApiCompositionContext;
46
+ readonly request: ApiRequest;
47
+ }
48
+ export type ApiAuthorizationPolicy = (request: ApiAuthorizationRequest) => boolean | Promise<boolean>;
49
+ export interface ComposeAuthorizedApiRuntimeOptions {
50
+ readonly definition: InternalRestApiDefinition;
51
+ readonly context: ApiCompositionContext;
52
+ readonly resolveHandler: ApiHandlerResolver;
53
+ readonly authorizeAsync: ApiAuthorizationPolicy;
54
+ }
33
55
  export interface ApiRuntime {
34
56
  readonly definition: InternalRestApiDefinition;
35
57
  readonly bindings: readonly ApiOperationBinding[];
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/types/api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,yBAAyB,EAC1B,MAAM,2BAA2B,CAAC;AAEnC,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,UAAU,EAAE,yBAAyB,CAAC;IAC/C,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,CAAC;IACtC,QAAQ,CAAC,SAAS,EAAE,mBAAmB,CAAC;IACxC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;IACrC,QAAQ,CAAC,I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CAAC,SAAS,CAAC,CAAC;CACvB"}
@@ -1 +1 @@
1
- {"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/types/api.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n DataEndpointConfig,\n DataOperationConfig,\n DataOperationMethod,\n InternalRestApiDefinition,\n} from \"@ankhorage/contracts/data\";\n\nexport interface ApiOperationBinding {\n readonly definition: InternalRestApiDefinition;\n readonly endpoint: DataEndpointConfig;\n readonly operation: DataOperationConfig;\n readonly operationId: string;\n readonly method: DataOperationMethod;\n readonly path: string;\n}\n\nexport interface ApiRequest {\n readonly operationId: string;\n readonly method: DataOperationMethod;\n readonly params: Readonly<Record<string, string>>;\n readonly query: Readonly<Record<string, string | readonly string[]>>;\n readonly headers: Readonly<Record<string, string>>;\n readonly body?: unknown;\n}\n\nexport interface ApiHandlerResponse {\n readonly status?: number;\n readonly headers?: Readonly<Record<string, string>>;\n readonly body?: unknown;\n}\n\nexport interface ApiResponse {\n readonly status: number;\n readonly headers: Readonly<Record<string, string>>;\n readonly body?: unknown;\n}\n\nexport interface ApiHandlerContext {\n readonly binding: ApiOperationBinding;\n}\n\nexport type ApiHandler = (\n request: ApiRequest,\n context: ApiHandlerContext,\n) => ApiHandlerResponse | Promise<ApiHandlerResponse>;\n\nexport type ApiHandlerRegistry = Readonly<Record<string, ApiHandler>>;\n\nexport interface ApiRuntime {\n readonly definition: InternalRestApiDefinition;\n readonly bindings: readonly ApiOperationBinding[];\n dispatchAsync(request: ApiRequest): Promise<ApiResponse>;\n getBinding(operationId: string): ApiOperationBinding | undefined;\n}\n\nexport interface ApiTransportAdapter<TRequest, TResponse> {\n toApiRequestAsync(\n request: TRequest,\n binding: ApiOperationBinding,\n ): Promise<ApiRequest>;\n fromApiResponseAsync(\n response: ApiResponse,\n request: TRequest,\n binding: ApiOperationBinding,\n ): Promise<TResponse>;\n}\n"]}
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/types/api.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n DataEndpointConfig,\n DataOperationConfig,\n DataOperationMethod,\n InternalRestApiDefinition,\n} from \"@ankhorage/contracts/data\";\n\nexport interface ApiOperationBinding {\n readonly definition: InternalRestApiDefinition;\n readonly endpoint: DataEndpointConfig;\n readonly operation: DataOperationConfig;\n readonly operationId: string;\n readonly method: DataOperationMethod;\n readonly path: string;\n}\n\nexport interface ApiRequest {\n readonly operationId: string;\n readonly method: DataOperationMethod;\n readonly params: Readonly<Record<string, string>>;\n readonly query: Readonly<Record<string, string | readonly string[]>>;\n readonly headers: Readonly<Record<string, string>>;\n readonly body?: unknown;\n}\n\nexport interface ApiHandlerResponse {\n readonly status?: number;\n readonly headers?: Readonly<Record<string, string>>;\n readonly body?: unknown;\n}\n\nexport interface ApiResponse {\n readonly status: number;\n readonly headers: Readonly<Record<string, string>>;\n readonly body?: unknown;\n}\n\nexport interface ApiHandlerContext {\n readonly binding: ApiOperationBinding;\n readonly composition?: ApiCompositionContext;\n}\n\nexport type ApiHandler = (\n request: ApiRequest,\n context: ApiHandlerContext,\n) => ApiHandlerResponse | Promise<ApiHandlerResponse>;\n\nexport type ApiHandlerRegistry = Readonly<Record<string, ApiHandler>>;\n\nexport interface ApiCompositionContext {\n readonly appId: string;\n readonly projectId: string;\n}\n\nexport interface ApiHandlerResolutionRequest {\n readonly binding: ApiOperationBinding;\n readonly context: ApiCompositionContext;\n}\n\nexport type ApiHandlerResolver = (\n request: ApiHandlerResolutionRequest,\n) => ApiHandler | undefined;\n\nexport interface ApiAuthorizationRequest {\n readonly binding: ApiOperationBinding;\n readonly context: ApiCompositionContext;\n readonly request: ApiRequest;\n}\n\nexport type ApiAuthorizationPolicy = (\n request: ApiAuthorizationRequest,\n) => boolean | Promise<boolean>;\n\nexport interface ComposeAuthorizedApiRuntimeOptions {\n readonly definition: InternalRestApiDefinition;\n readonly context: ApiCompositionContext;\n readonly resolveHandler: ApiHandlerResolver;\n readonly authorizeAsync: ApiAuthorizationPolicy;\n}\n\nexport interface ApiRuntime {\n readonly definition: InternalRestApiDefinition;\n readonly bindings: readonly ApiOperationBinding[];\n dispatchAsync(request: ApiRequest): Promise<ApiResponse>;\n getBinding(operationId: string): ApiOperationBinding | undefined;\n}\n\nexport interface ApiTransportAdapter<TRequest, TResponse> {\n toApiRequestAsync(\n request: TRequest,\n binding: ApiOperationBinding,\n ): Promise<ApiRequest>;\n fromApiResponseAsync(\n response: ApiResponse,\n request: TRequest,\n binding: ApiOperationBinding,\n ): Promise<TResponse>;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ankhorage/api",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "Framework-neutral executable API runtime and transport contract for Ankhorage.",
6
6
  "license": "MIT",
@@ -62,7 +62,8 @@
62
62
  "docs": "paradox"
63
63
  },
64
64
  "dependencies": {
65
- "@ankhorage/contracts": "^24.1.0"
65
+ "@ankhorage/capability": "^0.2.1",
66
+ "@ankhorage/contracts": "^26.0.0"
66
67
  },
67
68
  "devDependencies": {
68
69
  "@ankhorage/devtools": "^2.1.11",