@entifix/service-shell 0.12.1 → 0.14.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/README.md +7 -7
- package/dist/auth/failures.d.ts +0 -12
- package/dist/auth/failures.d.ts.map +1 -1
- package/dist/auth/failures.js +0 -10
- package/dist/auth/failures.js.map +1 -1
- package/dist/auth/index.d.ts +0 -1
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +0 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/require-principal.d.ts +0 -16
- package/dist/auth/require-principal.d.ts.map +1 -1
- package/dist/auth/require-principal.js +1 -38
- package/dist/auth/require-principal.js.map +1 -1
- package/dist/auth/require-service-crossing.d.ts +2 -73
- package/dist/auth/require-service-crossing.d.ts.map +1 -1
- package/dist/auth/require-service-crossing.js +2 -117
- package/dist/auth/require-service-crossing.js.map +1 -1
- package/dist/auth/shared-token.d.ts +0 -15
- package/dist/auth/shared-token.d.ts.map +1 -1
- package/dist/auth/shared-token.js +0 -25
- package/dist/auth/shared-token.js.map +1 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -3
- package/dist/index.js.map +1 -1
- package/dist/load-remote-configuration.d.ts +4 -4
- package/dist/load-remote-configuration.d.ts.map +1 -1
- package/dist/load-remote-configuration.js +7 -14
- package/dist/load-remote-configuration.js.map +1 -1
- package/dist/observability.d.ts +2 -2
- package/dist/observability.d.ts.map +1 -1
- package/dist/observability.js +1 -1
- package/dist/observability.js.map +1 -1
- package/dist/push/index.d.ts +9 -0
- package/dist/push/index.d.ts.map +1 -0
- package/dist/push/index.js +9 -0
- package/dist/push/index.js.map +1 -0
- package/dist/push/server-push.d.ts +82 -0
- package/dist/push/server-push.d.ts.map +1 -0
- package/dist/push/server-push.js +48 -0
- package/dist/push/server-push.js.map +1 -0
- package/dist/push/sse.d.ts +62 -0
- package/dist/push/sse.d.ts.map +1 -0
- package/dist/push/sse.js +120 -0
- package/dist/push/sse.js.map +1 -0
- package/dist/respond.d.ts +1 -7
- package/dist/respond.d.ts.map +1 -1
- package/dist/respond.js +1 -8
- package/dist/respond.js.map +1 -1
- package/package.json +23 -13
- package/dist/auth/require-organization.d.ts +0 -32
- package/dist/auth/require-organization.d.ts.map +0 -1
- package/dist/auth/require-organization.js +0 -52
- package/dist/auth/require-organization.js.map +0 -1
- package/dist/entity-facet-routes.d.ts +0 -44
- package/dist/entity-facet-routes.d.ts.map +0 -1
- package/dist/entity-facet-routes.js +0 -70
- package/dist/entity-facet-routes.js.map +0 -1
- package/dist/entity-metadata-route.d.ts +0 -25
- package/dist/entity-metadata-route.d.ts.map +0 -1
- package/dist/entity-metadata-route.js +0 -102
- package/dist/entity-metadata-route.js.map +0 -1
- package/dist/entity-route-guard.d.ts +0 -62
- package/dist/entity-route-guard.d.ts.map +0 -1
- package/dist/entity-route-guard.js +0 -35
- package/dist/entity-route-guard.js.map +0 -1
- package/dist/entity-routes.d.ts +0 -76
- package/dist/entity-routes.d.ts.map +0 -1
- package/dist/entity-routes.js +0 -267
- package/dist/entity-routes.js.map +0 -1
- package/dist/route-responses.d.ts +0 -44
- package/dist/route-responses.d.ts.map +0 -1
- package/dist/route-responses.js +0 -104
- package/dist/route-responses.js.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/entity-facet-routes.ts"],"sourcesContent":["import type { Narrowing, NarrowingRules } from '@entifix/business';\nimport {\n EntityFacetCounterFactoryTag,\n EntityFacetCounterTag,\n facetCountsUCFactory,\n} from '@entifix/business';\nimport type { EntifixFailure } from '@entifix/core';\nimport {\n EntifixBuildError,\n type Entity,\n type EntityConstructor,\n type EntityFacetCounts,\n type EntityFacetQuery,\n envelopeEntityName,\n makeEntityFacetsEnvelope,\n parseFacetQueryParams,\n} from '@entifix/core';\nimport { Effect } from 'effect';\nimport { HttpRouter, HttpServerRequest, HttpServerResponse } from 'effect/http';\n\nimport {\n type EntityReadScope,\n type EntityRouteContext,\n type EntityRouteGuard,\n guardRoute,\n type GuardServices,\n} from './entity-route-guard.js';\nimport { narrowingFor } from './narrowing.js';\nimport {\n defaultFailurePolicy,\n type FailurePolicy,\n respond,\n} from './respond.js';\nimport { legacyAnswer } from './route-responses.js';\n\nexport interface EntityFacetRoutesOptions<\n T extends Entity,\n R,\n G extends EntityRouteGuard = EntityRouteGuard,\n> {\n /** Who may count: the guard the entity's `list` has, as a rule. */\n readonly guard: G;\n /** Defaults to `/api/<entity key>`; the route is `<basePath>/$facets`. */\n readonly basePath?: `/${string}`;\n /**\n * Narrows the counts to the records this caller may see — the same scope\n * the entity's `list` takes, so a count never reveals a record the list\n * would not.\n */\n readonly scope?: (\n context: EntityRouteContext,\n ) => Effect.Effect<EntityReadScope<T>, never, R>;\n /**\n * The same narrowing the entity's `list` takes, as resolver rules — so a\n * count equals the narrowed list. Replaces `scope`; naming both throws.\n */\n readonly narrowing?: NarrowingRules<T>;\n /** How the count's failures are answered and logged. */\n readonly policy?: FailurePolicy;\n}\n\n/**\n * `GET <base>/$facets` — how many of the entity's records hold each value of\n * each `facet` member, each under its own `rsql.<member>` filtering — bound to\n * storage through the host's {@link EntityFacetCounterFactoryTag}.\n *\n * A route of its own beside `makeEntityRoutes` rather than a sixth verb, so it\n * is the one route set that requires a counter: a host that mounts it without\n * binding one fails to compile, and a host that never counts binds nothing.\n *\n * The query is parsed against the entity's metadata, so only a `filterable`\n * member is counted and every malformed request is a `400`. A scope is\n * **conjoined into every facet's filtering**, never substituted, so no query\n * string can count past it; `'none'` answers every member empty.\n */\nexport const makeEntityFacetRoutes = <\n T extends Entity,\n R = never,\n // `never` for the reason `makeEntityRoutes` gives: a caller naming `R`\n // explicitly must not widen the guard to every kind there is.\n const G extends EntityRouteGuard = never,\n>(\n entityConstructor: EntityConstructor<T>,\n options: EntityFacetRoutesOptions<T, R, G>,\n) => {\n const base: `/${string}` =\n options.basePath ?? `/api/${envelopeEntityName(entityConstructor)}`;\n\n const readQueries = Effect.gen(function* () {\n const request = yield* HttpServerRequest.HttpServerRequest;\n const search = new URL(request.url, 'http://localhost').searchParams;\n return yield* Effect.try({\n try: () => parseFacetQueryParams(entityConstructor, search),\n catch: error => error as EntifixBuildError,\n });\n });\n\n const narrowed = (\n queries: EntityFacetQuery<T>[],\n narrowing: Exclude<Narrowing<T>, 'nothing'>,\n ): EntityFacetQuery<T>[] =>\n narrowing === 'unscoped'\n ? queries\n : queries.map(query => ({\n ...query,\n filtering: [...(query.filtering ?? []), narrowing],\n }));\n\n if (options.narrowing !== undefined && options.scope !== undefined)\n throw new Error(\n `the facet route for '${envelopeEntityName(entityConstructor)}' names both ` +\n '`narrowing` and `scope`; `narrowing` replaces `scope`, so name one.',\n );\n\n /** A scope, as the narrowing this route applies. */\n const asNarrowing = (scope: EntityReadScope<T>): Narrowing<T> =>\n scope === 'all' ? 'unscoped' : scope === 'none' ? 'nothing' : scope.filter;\n\n const count = (context: EntityRouteContext) =>\n Effect.gen(function* () {\n const queries = yield* readQueries;\n const narrowing =\n options.narrowing !== undefined && context.verified !== undefined\n ? narrowingFor(options.narrowing, context.verified)\n : asNarrowing(\n options.scope ? yield* options.scope(context) : ('all' as const),\n );\n if (narrowing === 'nothing') {\n // Still parsed: whether a query is well-formed must not depend on who\n // asked.\n const empty: EntityFacetCounts = Object.fromEntries(\n queries.map(({ member }) => [member, []]),\n );\n return yield* HttpServerResponse.json(\n makeEntityFacetsEnvelope(entityConstructor, empty),\n );\n }\n const factory = yield* EntityFacetCounterFactoryTag;\n const counter = yield* factory.for(entityConstructor, {\n ...(context.organizationId === undefined\n ? {}\n : { organizationId: context.organizationId }),\n });\n const counts = yield* facetCountsUCFactory<T>(\n narrowed(queries, narrowing),\n ).pipe(Effect.provideService(EntityFacetCounterTag, counter));\n return yield* HttpServerResponse.json(\n makeEntityFacetsEnvelope(entityConstructor, counts),\n );\n }).pipe(Effect.catch(legacyAnswer('invalidQuery')));\n\n const handler = respond(\n guardRoute(\n entityConstructor,\n options.guard,\n 'read',\n count,\n ) as Effect.Effect<\n HttpServerResponse.HttpServerResponse,\n EntifixFailure,\n | R\n | EntityFacetCounterFactoryTag\n | GuardServices<G>\n | HttpServerRequest.HttpServerRequest\n | HttpRouter.RouteContext\n >,\n options.policy ?? defaultFailurePolicy,\n );\n\n return HttpRouter.addAll([\n HttpRouter.route('GET', `${base}/$facets`, handler),\n ]);\n};\n"],"names":["EntityFacetCounterFactoryTag","EntityFacetCounterTag","facetCountsUCFactory","envelopeEntityName","makeEntityFacetsEnvelope","parseFacetQueryParams","Effect","HttpRouter","HttpServerRequest","HttpServerResponse","guardRoute","narrowingFor","defaultFailurePolicy","respond","legacyAnswer","makeEntityFacetRoutes","entityConstructor","options","base","basePath","readQueries","gen","request","search","URL","url","searchParams","try","catch","error","narrowed","queries","narrowing","map","query","filtering","undefined","scope","Error","asNarrowing","filter","count","context","verified","empty","Object","fromEntries","member","json","factory","counter","for","organizationId","counts","pipe","provideService","handler","guard","policy","addAll","route"],"mappings":";AACA,SACEA,4BAA4B,EAC5BC,qBAAqB,EACrBC,oBAAoB,QACf,oBAAoB;AAE3B,SAMEC,kBAAkB,EAClBC,wBAAwB,EACxBC,qBAAqB,QAChB,gBAAgB;AACvB,SAASC,MAAM,QAAQ,SAAS;AAChC,SAASC,UAAU,EAAEC,iBAAiB,EAAEC,kBAAkB,QAAQ,cAAc;AAEhF,SAIEC,UAAU,QAEL,0BAA0B;AACjC,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SACEC,oBAAoB,EAEpBC,OAAO,QACF,eAAe;AACtB,SAASC,YAAY,QAAQ,uBAAuB;AA4BpD;;;;;;;;;;;;;CAaC,GACD,OAAO,MAAMC,wBAAwB,CAOnCC,mBACAC;QAGEA,mBAgFAA;IAjFF,MAAMC,QACJD,oBAAAA,QAAQE,QAAQ,YAAhBF,oBAAoB,CAAC,KAAK,EAAEd,mBAAmBa,oBAAoB;IAErE,MAAMI,cAAcd,OAAOe,GAAG,CAAC;Q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MkD,UAAU,OAAOD,QAAQE,GAAG,CAACnC,mBAAmB,aAChD0B,QAAQU,cAAc,KAAKhB,YAC3B,CAAC,IACD;gBAAEgB,gBAAgBV,QAAQU,cAAc;YAAC;YAE/C,MAAMC,SAAS,OAAOnD,qBACpB4B,SAASC,SAASC,YAClBsB,IAAI,CAAChD,OAAOiD,cAAc,CAACtD,uBAAuBiD;YACpD,OAAO,OAAOzC,mBAAmBuC,IAAI,CACnC5C,yBAAyBY,mBAAmBqC;QAEhD,GAAGC,IAAI,CAAChD,OAAOsB,KAAK,CAACd,aAAa;IAEpC,MAAM0C,UAAU3C,QACdH,WACEM,mBACAC,QAAQwC,KAAK,EACb,QACAhB,SAUFxB,kBAAAA,QAAQyC,MAAM,YAAdzC,kBAAkBL;IAGpB,OAAOL,WAAWoD,MAAM,CAAC;QACvBpD,WAAWqD,KAAK,CAAC,OAAO,GAAG1C,KAAK,QAAQ,CAAC,EAAEsC;KAC5C;AACH,EAAE"}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { PolicyDecisionTag } from '@entifix/authz';
|
|
2
|
-
import { type Entity, type EntityConstructor } from '@entifix/core';
|
|
3
|
-
import { Effect } from 'effect';
|
|
4
|
-
import { HttpServerRequest, HttpServerResponse } from 'effect/http';
|
|
5
|
-
import { type MetadataCompositions } from './metadata-composition.js';
|
|
6
|
-
/**
|
|
7
|
-
* `GET /api/<entity>/$metadata` — what this caller may do with one entity.
|
|
8
|
-
*
|
|
9
|
-
* Mounted **per entity, at a literal path**, not as a `/api/:entity/$metadata`
|
|
10
|
-
* wildcard. `HttpRouter` resolves through `find-my-way-ts`, where a static
|
|
11
|
-
* segment beats a parametric one and there is no backtracking once the
|
|
12
|
-
* parametric branch has matched: alongside an existing `/api/<entity>/:id`, a
|
|
13
|
-
* wildcard `$metadata` route never runs at all — the by-id handler wins with
|
|
14
|
-
* `id === "$metadata"`, misses, and answers its own `404`. The endpoint would
|
|
15
|
-
* read as "this entity has no metadata" while appearing mounted. A literal wins
|
|
16
|
-
* regardless of registration order, which is why the base exports a route for
|
|
17
|
-
* one entity and each service registers it for each of its own.
|
|
18
|
-
*
|
|
19
|
-
* Deliberately **not** behind `requireOrganization`, even on a tenant-plane
|
|
20
|
-
* service: the document describes the model, not tenant data, and resolving a
|
|
21
|
-
* database handle here would leave a vendor with no active organization unable
|
|
22
|
-
* to see their own affordances.
|
|
23
|
-
*/
|
|
24
|
-
export declare const entityMetadataRoute: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, compositions?: MetadataCompositions) => Effect.Effect<HttpServerResponse.HttpServerResponse, never, import("@entifix/business").TokenServiceTag | PolicyDecisionTag | HttpServerRequest.HttpServerRequest>;
|
|
25
|
-
//# sourceMappingURL=entity-metadata-route.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"entity-metadata-route.d.ts","sourceRoot":"","sources":["../src/entity-metadata-route.ts"],"names":[],"mappings":"AAEA,OAAO,EAKL,iBAAiB,EAClB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAEL,KAAK,MAAM,EAEX,KAAK,iBAAiB,EAKvB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGpE,OAAO,EAGL,KAAK,oBAAoB,EAC1B,MAAM,2BAA2B,CAAC;AAGnC;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,mBAAmB,GAAI,OAAO,SAAS,MAAM,EACxD,mBAAmB,iBAAiB,CAAC,OAAO,CAAC,EAC7C,eAAc,oBAAyB,uKAqFxC,CAAC"}
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
-
import { createHash } from 'node:crypto';
|
|
3
|
-
import { parsePermission, permissionForEntity, permissionForUseCase, PolicyDecisionTag } from '@entifix/authz';
|
|
4
|
-
import { describeEntityUseCases, ENTITY_ACTIONS, extractMetaEntity, makeEntityMetadataEnvelope, NotFound } from '@entifix/core';
|
|
5
|
-
import { Effect } from 'effect';
|
|
6
|
-
import { HttpServerRequest, HttpServerResponse } from 'effect/http';
|
|
7
|
-
import { requirePrincipal, withVerifiedContext } from './auth/index.js';
|
|
8
|
-
import { assertCompositionsDeclared, compositionFor } from './metadata-composition.js';
|
|
9
|
-
import { respond } from './respond.js';
|
|
10
|
-
/**
|
|
11
|
-
* `GET /api/<entity>/$metadata` — what this caller may do with one entity.
|
|
12
|
-
*
|
|
13
|
-
* Mounted **per entity, at a literal path**, not as a `/api/:entity/$metadata`
|
|
14
|
-
* wildcard. `HttpRouter` resolves through `find-my-way-ts`, where a static
|
|
15
|
-
* segment beats a parametric one and there is no backtracking once the
|
|
16
|
-
* parametric branch has matched: alongside an existing `/api/<entity>/:id`, a
|
|
17
|
-
* wildcard `$metadata` route never runs at all — the by-id handler wins with
|
|
18
|
-
* `id === "$metadata"`, misses, and answers its own `404`. The endpoint would
|
|
19
|
-
* read as "this entity has no metadata" while appearing mounted. A literal wins
|
|
20
|
-
* regardless of registration order, which is why the base exports a route for
|
|
21
|
-
* one entity and each service registers it for each of its own.
|
|
22
|
-
*
|
|
23
|
-
* Deliberately **not** behind `requireOrganization`, even on a tenant-plane
|
|
24
|
-
* service: the document describes the model, not tenant data, and resolving a
|
|
25
|
-
* database handle here would leave a vendor with no active organization unable
|
|
26
|
-
* to see their own affordances.
|
|
27
|
-
*/ export const entityMetadataRoute = (entityConstructor, compositions = {})=>{
|
|
28
|
-
assertCompositionsDeclared(describeEntityUseCases(entityConstructor), compositions);
|
|
29
|
-
return respond(requirePrincipal((principal)=>Effect.gen(function*() {
|
|
30
|
-
const policy = yield* PolicyDecisionTag;
|
|
31
|
-
const allows = (permission)=>{
|
|
32
|
-
const { resource, action } = parsePermission(permission);
|
|
33
|
-
return policy.decide({
|
|
34
|
-
subject: {
|
|
35
|
-
roles: principal.roles
|
|
36
|
-
},
|
|
37
|
-
resource,
|
|
38
|
-
action
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
// SECURITY BOUNDARY. The document names domains, entity keys and verbs —
|
|
42
|
-
// it is a map of the model — so an entity the caller may not read answers
|
|
43
|
-
// exactly as one this service does not host. A `403` here would make the
|
|
44
|
-
// endpoint an oracle: walk it and learn the shape of everything you are
|
|
45
|
-
// not allowed to see. Same category as `redactConfiguration`, not
|
|
46
|
-
// diagnostics polish.
|
|
47
|
-
if (!allows(permissionForEntity(entityConstructor, 'read'))) {
|
|
48
|
-
return yield* notFound(entityConstructor);
|
|
49
|
-
}
|
|
50
|
-
const document = {
|
|
51
|
-
actions: ENTITY_ACTIONS.filter((action)=>allows(permissionForEntity(entityConstructor, action))),
|
|
52
|
-
useCases: yield* withVerifiedContext(principal, (verified)=>Effect.succeed(describeEntityUseCases(entityConstructor).filter((descriptor)=>allows(permissionForUseCase(entityConstructor, descriptor.key)))// A verb the caller resolves to nothing is omitted, like one
|
|
53
|
-
// they may not invoke; the rest carry what they resolve to.
|
|
54
|
-
.flatMap((descriptor)=>{
|
|
55
|
-
const composition = compositionFor(descriptor, verified, compositions);
|
|
56
|
-
if (composition === 'unreachable') return [];
|
|
57
|
-
return composition === undefined ? [
|
|
58
|
-
descriptor
|
|
59
|
-
] : [
|
|
60
|
-
_extends({}, descriptor, {
|
|
61
|
-
composition
|
|
62
|
-
})
|
|
63
|
-
];
|
|
64
|
-
})))
|
|
65
|
-
};
|
|
66
|
-
// The ETag hashes the *computed* document, not the descriptor set the
|
|
67
|
-
// deployment declares. Two principals with different grants get different
|
|
68
|
-
// documents from the same model, so a hash of the model would `304` one
|
|
69
|
-
// caller onto another's affordances. Hashing the permission set instead
|
|
70
|
-
// would read `ROLE_PERMISSIONS` directly and go around `PolicyDecisionTag`,
|
|
71
|
-
// so an injected ABAC policy could change affordances without changing the
|
|
72
|
-
// tag. Computing first costs a metadata read and a handful of pure
|
|
73
|
-
// `decide` calls — no IO.
|
|
74
|
-
const body = JSON.stringify(makeEntityMetadataEnvelope(entityConstructor, document));
|
|
75
|
-
const etag = `"${createHash('sha256').update(body).digest('hex')}"`;
|
|
76
|
-
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
77
|
-
if (request.headers['if-none-match'] === etag) {
|
|
78
|
-
return HttpServerResponse.empty({
|
|
79
|
-
status: 304
|
|
80
|
-
}).pipe(HttpServerResponse.setHeaders(cacheHeaders(etag)));
|
|
81
|
-
}
|
|
82
|
-
return HttpServerResponse.text(body, {
|
|
83
|
-
contentType: 'application/json',
|
|
84
|
-
headers: cacheHeaders(etag)
|
|
85
|
-
});
|
|
86
|
-
})));
|
|
87
|
-
};
|
|
88
|
-
/**
|
|
89
|
-
* The response varies by principal, so it must never land in a shared cache.
|
|
90
|
-
* `Vary` names **both** credential carriers because the shell reads the access
|
|
91
|
-
* token from the `entifix_at` cookie *or* an `Authorization: Bearer` header, and a
|
|
92
|
-
* cache keyed on only one of them would serve across the other.
|
|
93
|
-
*/ const cacheHeaders = (etag)=>({
|
|
94
|
-
etag,
|
|
95
|
-
'cache-control': 'private, no-cache',
|
|
96
|
-
vary: 'Cookie, Authorization'
|
|
97
|
-
});
|
|
98
|
-
/** Indistinguishable from the `404` a service gives for an entity it does not host. */ const notFound = (entityConstructor)=>Effect.fail(new NotFound({
|
|
99
|
-
entity: extractMetaEntity(entityConstructor).key
|
|
100
|
-
}));
|
|
101
|
-
|
|
102
|
-
//# sourceMappingURL=entity-metadata-route.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/entity-metadata-route.ts"],"sourcesContent":["import { createHash } from 'node:crypto';\n\nimport {\n parsePermission,\n type Permission,\n permissionForEntity,\n permissionForUseCase,\n PolicyDecisionTag,\n} from '@entifix/authz';\nimport {\n describeEntityUseCases,\n type Entity,\n ENTITY_ACTIONS,\n type EntityConstructor,\n type EntityMetadataDocument,\n extractMetaEntity,\n makeEntityMetadataEnvelope,\n NotFound,\n} from '@entifix/core';\nimport { Effect } from 'effect';\nimport { HttpServerRequest, HttpServerResponse } from 'effect/http';\n\nimport { requirePrincipal, withVerifiedContext } from './auth/index.js';\nimport {\n assertCompositionsDeclared,\n compositionFor,\n type MetadataCompositions,\n} from './metadata-composition.js';\nimport { respond } from './respond.js';\n\n/**\n * `GET /api/<entity>/$metadata` — what this caller may do with one entity.\n *\n * Mounted **per entity, at a literal path**, not as a `/api/:entity/$metadata`\n * wildcard. `HttpRouter` resolves through `find-my-way-ts`, where a static\n * segment beats a parametric one and there is no backtracking once the\n * parametric branch has matched: alongside an existing `/api/<entity>/:id`, a\n * wildcard `$metadata` route never runs at all — the by-id handler wins with\n * `id === \"$metadata\"`, misses, and answers its own `404`. The endpoint would\n * read as \"this entity has no metadata\" while appearing mounted. A literal wins\n * regardless of registration order, which is why the base exports a route for\n * one entity and each service registers it for each of its own.\n *\n * Deliberately **not** behind `requireOrganization`, even on a tenant-plane\n * service: the document describes the model, not tenant data, and resolving a\n * database handle here would leave a vendor with no active organization unable\n * to see their own affordances.\n */\nexport const entityMetadataRoute = <TEntity extends Entity>(\n entityConstructor: EntityConstructor<TEntity>,\n compositions: MetadataCompositions = {},\n) => {\n assertCompositionsDeclared(\n describeEntityUseCases(entityConstructor),\n compositions,\n );\n return respond(\n requirePrincipal(principal =>\n Effect.gen(function* () {\n const policy = yield* PolicyDecisionTag;\n const allows = (permission: Permission): boolean => {\n const { resource, action } = parsePermission(permission);\n return policy.decide({\n subject: { roles: principal.roles },\n resource,\n action,\n });\n };\n\n // SECURITY BOUNDARY. The document names domains, entity keys and verbs —\n // it is a map of the model — so an entity the caller may not read answers\n // exactly as one this service does not host. A `403` here would make the\n // endpoint an oracle: walk it and learn the shape of everything you are\n // not allowed to see. Same category as `redactConfiguration`, not\n // diagnostics polish.\n if (!allows(permissionForEntity(entityConstructor, 'read'))) {\n return yield* notFound(entityConstructor);\n }\n\n const document: EntityMetadataDocument = {\n actions: ENTITY_ACTIONS.filter(action =>\n allows(permissionForEntity(entityConstructor, action)),\n ),\n useCases: yield* withVerifiedContext(principal, verified =>\n Effect.succeed(\n describeEntityUseCases(entityConstructor)\n .filter(descriptor =>\n allows(\n permissionForUseCase(entityConstructor, descriptor.key),\n ),\n )\n // A verb the caller resolves to nothing is omitted, like one\n // they may not invoke; the rest carry what they resolve to.\n .flatMap(descriptor => {\n const composition = compositionFor(\n descriptor,\n verified,\n compositions,\n );\n if (composition === 'unreachable') return [];\n return composition === undefined\n ? [descriptor]\n : [{ ...descriptor, composition }];\n }),\n ),\n ),\n };\n\n // The ETag hashes the *computed* document, not the descriptor set the\n // deployment declares. Two principals with different grants get different\n // documents from the same model, so a hash of the model would `304` one\n // caller onto another's affordances. Hashing the permission set instead\n // would read `ROLE_PERMISSIONS` directly and go around `PolicyDecisionTag`,\n // so an injected ABAC policy could change affordances without changing the\n // tag. Computing first costs a metadata read and a handful of pure\n // `decide` calls — no IO.\n const body = JSON.stringify(\n makeEntityMetadataEnvelope(entityConstructor, document),\n );\n const etag = `\"${createHash('sha256').update(body).digest('hex')}\"`;\n\n const request = yield* HttpServerRequest.HttpServerRequest;\n if (request.headers['if-none-match'] === etag) {\n return HttpServerResponse.empty({ status: 304 }).pipe(\n HttpServerResponse.setHeaders(cacheHeaders(etag)),\n );\n }\n\n return HttpServerResponse.text(body, {\n contentType: 'application/json',\n headers: cacheHeaders(etag),\n });\n }),\n ),\n );\n};\n\n/**\n * The response varies by principal, so it must never land in a shared cache.\n * `Vary` names **both** credential carriers because the shell reads the access\n * token from the `entifix_at` cookie *or* an `Authorization: Bearer` header, and a\n * cache keyed on only one of them would serve across the other.\n */\nconst cacheHeaders = (etag: string) => ({\n etag,\n 'cache-control': 'private, no-cache',\n vary: 'Cookie, Authorization',\n});\n\n/** Indistinguishable from the `404` a service gives for an entity it does not host. */\nconst notFound = <TEntity extends Entity>(\n entityConstructor: EntityConstructor<TEntity>,\n) =>\n Effect.fail(\n new NotFound({ entity: extractMetaEntity(entityConstructor).key }),\n );\n"],"names":["createHash","parsePermission","permissionForEntity","permissionForUseCase","PolicyDecisionTag","describeEntityUseCases","ENTITY_ACTIONS","extractMetaEntity","makeEntityMetadataEnvelope","NotFound","Effect","HttpServerRequest","HttpServerResponse","requirePrincipal","withVerifiedContext","assertCompositionsDeclared","compositionFor","respond","entityMetadataRoute","entityConstructor","compositions","principal","gen","policy","allows","permission","resource","action","decide","subject","roles","notFound","document","actions","filter","useCases","verified","succeed","descriptor","key","flatMap","composition","undefined","body","JSON","stringify","etag","update","digest","request","headers","empty","status","pipe","setHeaders","cacheHeaders","text","contentType","vary","fail","entity"],"mappings":";AAAA,SAASA,UAAU,QAAQ,cAAc;AAEzC,SACEC,eAAe,EAEfC,mBAAmB,EACnBC,oBAAoB,EACpBC,iBAAiB,QACZ,iBAAiB;AACxB,SACEC,sBAAsB,EAEtBC,cAAc,EAGdC,iBAAiB,EACjBC,0BAA0B,EAC1BC,QAAQ,QACH,gBAAgB;AACvB,SAASC,MAAM,QAAQ,SAAS;AAChC,SAASC,iBAAiB,EAAEC,kBAAkB,QAAQ,cAAc;AAEpE,SAASC,gBAAgB,EAAEC,mBAAmB,QAAQ,kBAAkB;AACxE,SACEC,0BAA0B,EAC1BC,cAAc,QAET,4BAA4B;AACnC,SAASC,OAAO,QAAQ,eAAe;AAEvC;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,MAAMC,sBAAsB,CACjCC,mBACAC,eAAqC,CAAC,CAAC;IAEvCL,2BACEV,uBAAuBc,oBACvBC;IAEF,OAAOH,QACLJ,iBAAiBQ,CAAAA,YACfX,OAAOY,GAAG,CAAC;YACT,MAAMC,SAAS,OAAOnB;YACtB,MAAMoB,SAAS,CAACC;gBACd,MAAM,EAAEC,QAAQ,EAAEC,MAAM,EAAE,GAAG1B,gBAAgBwB;gBAC7C,OAAOF,OAAOK,MAAM,CAAC;oBACnBC,SAAS;wBAAEC,OAAOT,UAAUS,KAAK;oBAAC;oBAClCJ;oBACAC;gBACF;YACF;YA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,MAAM;gBACnCc,aAAa;gBACbP,SAASK,aAAaT;YACxB;QACF;AAGN,EAAE;AAEF;;;;;CAKC,GACD,MAAMS,eAAe,CAACT,OAAkB,CAAA;QACtCA;QACA,iBAAiB;QACjBY,MAAM;IACR,CAAA;AAEA,qFAAqF,GACrF,MAAM3B,WAAW,CACfZ,oBAEAT,OAAOiD,IAAI,CACT,IAAIlD,SAAS;QAAEmD,QAAQrD,kBAAkBY,mBAAmBoB,GAAG;IAAC"}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { type Action, type PolicyDecisionTag, type ServiceCrossingPolicyTag } from '@entifix/authz';
|
|
2
|
-
import type { RepositoryScope, RequestPrincipal, TokenServiceTag, VerifiedContext } from '@entifix/business';
|
|
3
|
-
import type { Entity, EntityConstructor, FilterGroup } from '@entifix/core';
|
|
4
|
-
import type { Effect } from 'effect';
|
|
5
|
-
import { type ServiceCrossingTokenTag } from './auth/index.js';
|
|
6
|
-
/**
|
|
7
|
-
* Who may reach a route, and where the organization a per-organization store
|
|
8
|
-
* needs comes from.
|
|
9
|
-
*
|
|
10
|
-
* - `public` — anybody; nothing is checked and no organization is known.
|
|
11
|
-
* - `permission` — a verified session holding the permission the entity
|
|
12
|
-
* derives for the verb.
|
|
13
|
-
* - `organization` — the same, plus the session's active organization, which
|
|
14
|
-
* is what a per-organization store opens.
|
|
15
|
-
* - `crossing` — the fleet's crossing token and no session at all. With
|
|
16
|
-
* `organization: true` the caller names the organization in a header, which
|
|
17
|
-
* only a caller that has proved it is the fleet may do.
|
|
18
|
-
*/
|
|
19
|
-
export type EntityRouteGuard = {
|
|
20
|
-
readonly kind: 'public';
|
|
21
|
-
} | {
|
|
22
|
-
readonly kind: 'permission';
|
|
23
|
-
} | {
|
|
24
|
-
readonly kind: 'organization';
|
|
25
|
-
} | {
|
|
26
|
-
readonly kind: 'crossing';
|
|
27
|
-
readonly organization: boolean;
|
|
28
|
-
};
|
|
29
|
-
/** What a route learned about its caller from its guard. */
|
|
30
|
-
export interface EntityRouteContext extends RepositoryScope {
|
|
31
|
-
readonly principal?: RequestPrincipal;
|
|
32
|
-
/** The caller's verified context, built by the guard from the principal. */
|
|
33
|
-
readonly verified?: VerifiedContext;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Which records of an entity a caller may see.
|
|
37
|
-
*
|
|
38
|
-
* `filter` narrows a list and `includes` decides a by-id read — both are
|
|
39
|
-
* needed, because `id` is the one member no query can name. `'none'` is a
|
|
40
|
-
* caller who holds the read grant and owns nothing: an empty page and a `404`,
|
|
41
|
-
* never a `403`, because owning no records is not an error.
|
|
42
|
-
*/
|
|
43
|
-
export type EntityReadScope<T extends Entity> = 'all' | 'none' | {
|
|
44
|
-
readonly filter: FilterGroup<T>;
|
|
45
|
-
readonly includes: (entity: T) => boolean;
|
|
46
|
-
};
|
|
47
|
-
/**
|
|
48
|
-
* What a route needs from the host for its guard: a session's services always
|
|
49
|
-
* (a `$metadata` read and a session guard both read the caller), the fleet's
|
|
50
|
-
* crossing services only when a guard is a crossing.
|
|
51
|
-
*/
|
|
52
|
-
export type GuardServices<G extends EntityRouteGuard> = TokenServiceTag | PolicyDecisionTag | (G extends {
|
|
53
|
-
readonly kind: 'crossing';
|
|
54
|
-
} ? ServiceCrossingTokenTag | ServiceCrossingPolicyTag : never);
|
|
55
|
-
/**
|
|
56
|
-
* Runs `use` behind `guard`, checking the permission the entity derives for
|
|
57
|
-
* `action` — the one place a derived route turns a stated guard into the
|
|
58
|
-
* check that enforces it, so the entity routes and the facet route cannot
|
|
59
|
-
* enforce the same guard differently.
|
|
60
|
-
*/
|
|
61
|
-
export declare const guardRoute: <T extends Entity, A, E, R>(entityConstructor: EntityConstructor<T>, guard: EntityRouteGuard, action: Action, use: (context: EntityRouteContext) => Effect.Effect<A, E, R>) => Effect.Effect<import("effect/http/HttpServerResponse").HttpServerResponse | A, E, TokenServiceTag | PolicyDecisionTag | import("effect/http/HttpServerRequest").HttpServerRequest | Exclude<R, import("@entifix/business").VerifiedContextTag>> | Effect.Effect<import("effect/http/HttpServerResponse").HttpServerResponse | A, E, ServiceCrossingPolicyTag | import("effect/http/HttpServerRequest").HttpServerRequest | ServiceCrossingTokenTag | Exclude<R, import("@entifix/business").VerifiedContextTag>>;
|
|
62
|
-
//# sourceMappingURL=entity-route-guard.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"entity-route-guard.d.ts","sourceRoot":"","sources":["../src/entity-route-guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EAEX,KAAK,iBAAiB,EACtB,KAAK,wBAAwB,EAC9B,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EACV,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,EAKL,KAAK,uBAAuB,EAE7B,MAAM,iBAAiB,CAAC;AAEzB;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,gBAAgB,GACxB;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAA;CAAE,GAC3B;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAA;CAAE,GAC/B;IAAE,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAA;CAAE,GACjC;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAA;CAAE,CAAC;AAElE,4DAA4D;AAC5D,MAAM,WAAW,kBAAmB,SAAQ,eAAe;IACzD,QAAQ,CAAC,SAAS,CAAC,EAAE,gBAAgB,CAAC;IACtC,4EAA4E;IAC5E,QAAQ,CAAC,QAAQ,CAAC,EAAE,eAAe,CAAC;CACrC;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,MAAM,IACxC,KAAK,GACL,MAAM,GACN;IACE,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAChC,QAAQ,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC;CAC3C,CAAC;AAEN;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,gBAAgB,IAChD,eAAe,GACf,iBAAiB,GACjB,CAAC,CAAC,SAAS;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;CAAE,GACpC,uBAAuB,GAAG,wBAAwB,GAClD,KAAK,CAAC,CAAC;AAEf;;;;;GAKG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAClD,mBAAmB,iBAAiB,CAAC,CAAC,CAAC,EACvC,OAAO,gBAAgB,EACvB,QAAQ,MAAM,EACd,KAAK,CAAC,OAAO,EAAE,kBAAkB,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,qfAyB7D,CAAC"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
-
import { permissionForEntity } from '@entifix/authz';
|
|
3
|
-
import { requireCrossing, requireOrganization, requirePermission, requireServiceCrossing, withVerifiedContext } from './auth/index.js';
|
|
4
|
-
/**
|
|
5
|
-
* Runs `use` behind `guard`, checking the permission the entity derives for
|
|
6
|
-
* `action` — the one place a derived route turns a stated guard into the
|
|
7
|
-
* check that enforces it, so the entity routes and the facet route cannot
|
|
8
|
-
* enforce the same guard differently.
|
|
9
|
-
*/ export const guardRoute = (entityConstructor, guard, action, use)=>{
|
|
10
|
-
const permission = permissionForEntity(entityConstructor, action);
|
|
11
|
-
// Every guard ends here: the context is built once, from what the guard
|
|
12
|
-
// verified, and provided to everything the route runs.
|
|
13
|
-
const verify = (context)=>withVerifiedContext(context.principal, (verified)=>use(_extends({}, context, {
|
|
14
|
-
verified
|
|
15
|
-
})));
|
|
16
|
-
switch(guard.kind){
|
|
17
|
-
case 'public':
|
|
18
|
-
return verify({});
|
|
19
|
-
case 'permission':
|
|
20
|
-
return requirePermission(permission)((principal)=>verify({
|
|
21
|
-
principal
|
|
22
|
-
}));
|
|
23
|
-
case 'organization':
|
|
24
|
-
return requireOrganization(permission)((organizationId, principal)=>verify({
|
|
25
|
-
organizationId,
|
|
26
|
-
principal
|
|
27
|
-
}));
|
|
28
|
-
case 'crossing':
|
|
29
|
-
return guard.organization ? requireServiceCrossing(permission)((organizationId)=>verify({
|
|
30
|
-
organizationId
|
|
31
|
-
})) : requireCrossing(permission)(verify({}));
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
//# sourceMappingURL=entity-route-guard.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/entity-route-guard.ts"],"sourcesContent":["import {\n type Action,\n permissionForEntity,\n type PolicyDecisionTag,\n type ServiceCrossingPolicyTag,\n} from '@entifix/authz';\nimport type {\n RepositoryScope,\n RequestPrincipal,\n TokenServiceTag,\n VerifiedContext,\n} from '@entifix/business';\nimport type { Entity, EntityConstructor, FilterGroup } from '@entifix/core';\nimport type { Effect } from 'effect';\n\nimport {\n requireCrossing,\n requireOrganization,\n requirePermission,\n requireServiceCrossing,\n type ServiceCrossingTokenTag,\n withVerifiedContext,\n} from './auth/index.js';\n\n/**\n * Who may reach a route, and where the organization a per-organization store\n * needs comes from.\n *\n * - `public` — anybody; nothing is checked and no organization is known.\n * - `permission` — a verified session holding the permission the entity\n * derives for the verb.\n * - `organization` — the same, plus the session's active organization, which\n * is what a per-organization store opens.\n * - `crossing` — the fleet's crossing token and no session at all. With\n * `organization: true` the caller names the organization in a header, which\n * only a caller that has proved it is the fleet may do.\n */\nexport type EntityRouteGuard =\n | { readonly kind: 'public' }\n | { readonly kind: 'permission' }\n | { readonly kind: 'organization' }\n | { readonly kind: 'crossing'; readonly organization: boolean };\n\n/** What a route learned about its caller from its guard. */\nexport interface EntityRouteContext extends RepositoryScope {\n readonly principal?: RequestPrincipal;\n /** The caller's verified context, built by the guard from the principal. */\n readonly verified?: VerifiedContext;\n}\n\n/**\n * Which records of an entity a caller may see.\n *\n * `filter` narrows a list and `includes` decides a by-id read — both are\n * needed, because `id` is the one member no query can name. `'none'` is a\n * caller who holds the read grant and owns nothing: an empty page and a `404`,\n * never a `403`, because owning no records is not an error.\n */\nexport type EntityReadScope<T extends Entity> =\n | 'all'\n | 'none'\n | {\n readonly filter: FilterGroup<T>;\n readonly includes: (entity: T) => boolean;\n };\n\n/**\n * What a route needs from the host for its guard: a session's services always\n * (a `$metadata` read and a session guard both read the caller), the fleet's\n * crossing services only when a guard is a crossing.\n */\nexport type GuardServices<G extends EntityRouteGuard> =\n | TokenServiceTag\n | PolicyDecisionTag\n | (G extends { readonly kind: 'crossing' }\n ? ServiceCrossingTokenTag | ServiceCrossingPolicyTag\n : never);\n\n/**\n * Runs `use` behind `guard`, checking the permission the entity derives for\n * `action` — the one place a derived route turns a stated guard into the\n * check that enforces it, so the entity routes and the facet route cannot\n * enforce the same guard differently.\n */\nexport const guardRoute = <T extends Entity, A, E, R>(\n entityConstructor: EntityConstructor<T>,\n guard: EntityRouteGuard,\n action: Action,\n use: (context: EntityRouteContext) => Effect.Effect<A, E, R>,\n) => {\n const permission = permissionForEntity(entityConstructor, action);\n // Every guard ends here: the context is built once, from what the guard\n // verified, and provided to everything the route runs.\n const verify = (context: EntityRouteContext) =>\n withVerifiedContext(context.principal, verified =>\n use({ ...context, verified }),\n );\n switch (guard.kind) {\n case 'public':\n return verify({});\n case 'permission':\n return requirePermission(permission)(principal => verify({ principal }));\n case 'organization':\n return requireOrganization(permission)((organizationId, principal) =>\n verify({ organizationId, principal }),\n );\n case 'crossing':\n return guard.organization\n ? requireServiceCrossing(permission)(organizationId =>\n verify({ organizationId }),\n )\n : requireCrossing(permission)(verify({}));\n }\n};\n"],"names":["permissionForEntity","requireCrossing","requireOrganization","requirePermission","requireServiceCrossing","withVerifiedContext","guardRoute","entityConstructor","guard","action","use","permission","verify","context","principal","verified","kind","organizationId","organization"],"mappings":";AAAA,SAEEA,mBAAmB,QAGd,iBAAiB;AAUxB,SACEC,eAAe,EACfC,mBAAmB,EACnBC,iBAAiB,EACjBC,sBAAsB,EAEtBC,mBAAmB,QACd,kBAAkB;AAwDzB;;;;;CAKC,GACD,OAAO,MAAMC,aAAa,CACxBC,mBACAC,OACAC,QACAC;IAEA,MAAMC,aAAaX,oBAAoBO,mBAAmBE;IAC1D,wEAAwE;IACxE,uDAAuD;IACvD,MAAMG,SAAS,CAACC,UACdR,oBAAoBQ,QAAQC,SAAS,EAAEC,CAAAA,WACrCL,IAAI,aAAKG;gBAASE;;IAEtB,OAAQP,MAAMQ,IAAI;QAChB,KAAK;YACH,OAAOJ,OAAO,CAAC;QACjB,KAAK;YACH,OAAOT,kBAAkBQ,YAAYG,CAAAA,YAAaF,OAAO;oBAAEE;gBAAU;QACvE,KAAK;YACH,OAAOZ,oBAAoBS,YAAY,CAACM,gBAAgBH,YACtDF,OAAO;oBAAEK;oBAAgBH;gBAAU;QAEvC,KAAK;YACH,OAAON,MAAMU,YAAY,GACrBd,uBAAuBO,YAAYM,CAAAA,iBACjCL,OAAO;oBAAEK;gBAAe,MAE1BhB,gBAAgBU,YAAYC,OAAO,CAAC;IAC5C;AACF,EAAE"}
|
package/dist/entity-routes.d.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import type { NarrowingRules } from '@entifix/business';
|
|
2
|
-
import { EntityRepositoryFactoryTag } from '@entifix/business';
|
|
3
|
-
import { type EntifixBuildError, type EntifixEnvelopeLink, type Entity, type EntityConstructor, type EntityId } from '@entifix/core';
|
|
4
|
-
import { Effect } from 'effect';
|
|
5
|
-
import { HttpRouter, HttpServerResponse } from 'effect/http';
|
|
6
|
-
import { type EntityReadScope, type EntityRouteContext, type EntityRouteGuard } from './entity-route-guard.js';
|
|
7
|
-
import type { MetadataCompositions } from './metadata-composition.js';
|
|
8
|
-
import { type FailurePolicy } from './respond.js';
|
|
9
|
-
import { type EntityRouteRefusal, routeFailure } from './route-responses.js';
|
|
10
|
-
/** The five generic verbs a route set can mount. */
|
|
11
|
-
export type EntityVerb = 'list' | 'get' | 'create' | 'update' | 'delete';
|
|
12
|
-
export { type EntityReadScope, type EntityRouteContext, type EntityRouteGuard, } from './entity-route-guard.js';
|
|
13
|
-
export { EntityRouteRefusal } from './route-responses.js';
|
|
14
|
-
/** A hand-written handler for one verb, mounted under the derived guard. */
|
|
15
|
-
export type EntityRouteOverride<R> = (context: EntityRouteContext, links: EntityRouteLinks) => Effect.Effect<HttpServerResponse.HttpServerResponse, unknown, R>;
|
|
16
|
-
/** The affordances a route set derives from the verbs it mounts. */
|
|
17
|
-
export interface EntityRouteLinks {
|
|
18
|
-
readonly record: (id: EntityId) => EntifixEnvelopeLink[];
|
|
19
|
-
readonly collection: () => EntifixEnvelopeLink[];
|
|
20
|
-
}
|
|
21
|
-
export interface EntityRoutesOptions<T extends Entity, R, G extends EntityRouteGuard = EntityRouteGuard> {
|
|
22
|
-
readonly verbs: readonly EntityVerb[];
|
|
23
|
-
/** One guard for every verb, or one for the reads and one for the writes. */
|
|
24
|
-
readonly guard: G | {
|
|
25
|
-
readonly read: G;
|
|
26
|
-
readonly write: G;
|
|
27
|
-
};
|
|
28
|
-
/** Defaults to `/api/<entity key>`. */
|
|
29
|
-
readonly basePath?: `/${string}`;
|
|
30
|
-
/**
|
|
31
|
-
* Narrows the reads to what this caller may see, as resolver rules over the
|
|
32
|
-
* verified context. Replaces `scope`; a route set naming both throws.
|
|
33
|
-
*/
|
|
34
|
-
readonly narrowing?: NarrowingRules<T>;
|
|
35
|
-
/**
|
|
36
|
-
* The variant rules, recipes and catalog the entity's `@useCase()` verbs
|
|
37
|
-
* name, so `$metadata` can report what each resolves to for the caller.
|
|
38
|
-
*/
|
|
39
|
-
readonly compositions?: MetadataCompositions;
|
|
40
|
-
/** Narrows the reads to what this caller may see. Superseded by `narrowing`. */
|
|
41
|
-
readonly scope?: (context: EntityRouteContext) => Effect.Effect<EntityReadScope<T>, never, R>;
|
|
42
|
-
/**
|
|
43
|
-
* Runs on every create and update, after the body is read and before the
|
|
44
|
-
* save — and may change what is saved, or refuse it.
|
|
45
|
-
*/
|
|
46
|
-
readonly prepare?: (entity: T, context: EntityRouteContext & {
|
|
47
|
-
readonly verb: 'create' | 'update';
|
|
48
|
-
}) => Effect.Effect<T, EntityRouteRefusal | EntifixBuildError, R>;
|
|
49
|
-
/**
|
|
50
|
-
* A hand-written handler in place of the generic one for a verb. It keeps
|
|
51
|
-
* the derived guard and receives the derived links, so the escape hatch
|
|
52
|
-
* cannot drift from the rest of the set.
|
|
53
|
-
*/
|
|
54
|
-
readonly override?: Partial<Record<EntityVerb, EntityRouteOverride<R>>>;
|
|
55
|
-
/**
|
|
56
|
-
* How every verb's failures are answered and logged. Defaults to
|
|
57
|
-
* {@link defaultFailurePolicy}; a host passes its fleet policy.
|
|
58
|
-
*/
|
|
59
|
-
readonly policy?: FailurePolicy;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* {@link deriveEntityRoutes}, with a curried form for a caller that has to
|
|
63
|
-
* name the services its `override`, `scope` or `prepare` handlers need:
|
|
64
|
-
* `makeEntityRoutes.for<R>()(Entity, options)` names `R` and still infers the
|
|
65
|
-
* entity and the guard from the value, which a single call with explicit type
|
|
66
|
-
* arguments cannot do.
|
|
67
|
-
*/
|
|
68
|
-
export { routeFailure };
|
|
69
|
-
export declare const makeEntityRoutes: (<T extends Entity, R = never, const G extends EntityRouteGuard = never>(entityConstructor: EntityConstructor<T>, options: EntityRoutesOptions<T, R, G>) => import("effect/Layer").Layer<never, never, HttpRouter.HttpRouter | HttpRouter.Request<"Requires", import("@entifix/business").TokenServiceTag> | HttpRouter.Request<"Requires", import("@entifix/authz").PolicyDecisionTag> | HttpRouter.Request<"Requires", EntityRepositoryFactoryTag> | HttpRouter.Request.From<"Requires", Exclude<Exclude<R, HttpRouter.Provided>, HttpRouter.Provided>> | HttpRouter.Request.From<"Requires", Exclude<Exclude<G extends {
|
|
70
|
-
readonly kind: "crossing";
|
|
71
|
-
} ? import("@entifix/authz").ServiceCrossingPolicyTag | import("./index.js").ServiceCrossingTokenTag : never, HttpRouter.Provided>, HttpRouter.Provided>>>) & {
|
|
72
|
-
for: <R>() => <T extends Entity, const G extends EntityRouteGuard>(entityConstructor: EntityConstructor<T>, options: EntityRoutesOptions<T, R, G>) => import("effect/Layer").Layer<never, never, HttpRouter.HttpRouter | HttpRouter.Request<"Requires", import("@entifix/business").TokenServiceTag> | HttpRouter.Request<"Requires", import("@entifix/authz").PolicyDecisionTag> | HttpRouter.Request<"Requires", EntityRepositoryFactoryTag> | HttpRouter.Request.From<"Requires", Exclude<Exclude<R, HttpRouter.Provided>, HttpRouter.Provided>> | HttpRouter.Request.From<"Requires", Exclude<Exclude<G extends {
|
|
73
|
-
readonly kind: "crossing";
|
|
74
|
-
} ? import("@entifix/authz").ServiceCrossingPolicyTag | import("./index.js").ServiceCrossingTokenTag : never, HttpRouter.Provided>, HttpRouter.Provided>>>;
|
|
75
|
-
};
|
|
76
|
-
//# sourceMappingURL=entity-routes.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"entity-routes.d.ts","sourceRoot":"","sources":["../src/entity-routes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAIL,0BAA0B,EAM3B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EACxB,KAAK,MAAM,EACX,KAAK,iBAAiB,EACtB,KAAK,QAAQ,EASd,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,MAAM,EAAE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