import { P as PackageDeclaration, _ as Plan, a as AgentOptions, c as Route, d as Context, n as Plugin, s as Middleware, u as Payload } from "./plugin-DYhXH7JX.mjs"; /** The action a handler answers, as the platform sends it: its id, slug, name, type, status and the action specification it is an instance of. */ interface Action { id: string; slug: string; name: string; /** A default action's type (`create`, `update`, `delete`), else the action specification's, else `custom`. */ type: string; status: string; specification: { id: string; slug: string; }; } /** An entity an action is of (its service, or its link), as the platform sends it, with its attributes. */ interface Entity> extends Record { id: string; slug: string; /** Its attributes as stored: what its actions' results wrote. */ attributes: Attributes; } /** Who asked for the action. */ interface User extends Record { id: string; } /** The package the service is bound to, as the platform sends it: `{ id, slug, name, revision: { id, version, artifacts: [{ name, type, meta }] } }`, the revision the service runs or is moving to. */ interface BoundPackage extends Record { id: string; slug: string; } /** What a specification's action handler gets as its input: its parameters typed `Parameters`, its service's attributes `Attributes`. */ interface ServiceInput, Attributes = Record> { /** The action's parameters. */ parameters: Parameters; /** The action, as the platform names it. */ action: Action; /** The service the action is of, its attributes the service's. */ service: Entity; /** Who asked for the action, when the platform says. */ user?: User; /** The package the service is bound to, when it is bound to one. */ package?: BoundPackage; /** The payload's tags, as sent, when it carries them. */ tags?: Record; /** The NRN of the entity the action is of, as sent, when the payload carries one. */ entity_nrn?: string; /** Whether the platform manages the action, as sent, when the payload says. */ managed?: boolean; /** The trace the action continues, as sent, when the payload carries one: the tracing SDK's carrier, `||`. */ trace_context?: string; } /** What a link's action handler gets as its input: a specification's action input, with the link the action is of and its attributes `LinkAttributes`. */ interface LinkInput, Attributes = Record, LinkAttributes = Record> extends ServiceInput { link: Entity; } /** An action's handler: a function of the input its route's middleware builds, and of the run's context. */ type ActionHandler = (input: Input, context: Context) => unknown; /** An action as its author declares it beside its handler: what the trace tells of it, and a custom one's specification. */ interface ActionDeclaration { handler: ActionHandler; /** The run's name on the trace; the action's key, made readable, unless given. Never published. */ title?: string; /** What the action does: the run's narrative until the handler says more. Never published. */ description?: string; /** The steps the action is expected to run, as the trace's job declares them; `context.trace.step` names them by key. Never published. */ plan?: Plan; /** A custom action's action_specification body, as the API takes it; a default action's is the platform's own. */ specification?: Record; } /** * The values a schema declares, as TypeScript reads a schema literal: what a * handler's context and its return are typed by. A definition's schemas are * inferred as literals (a `const` type parameter), so `{ type: "object", * required: ["region"], properties: { region: { type: "string" } } }` is * `{ region: string } & { [key: string]: unknown }`. * * - An object's properties are its `properties`, each required when `required` * names it, else optional; a key it does not declare is allowed, `unknown`, * unless `additionalProperties` is `false`. A `required` that is no literal * (a schema written without `as const`) names nothing the type can know, so * it requires nothing. * - A property is its `enum`'s members, else its `const`, else an object's * values, an array's items' (`items`, one schema), else its `type`'s * (`string`, `number` and `integer`, `boolean`, `null`; each of a list of * them). * - What it does not read falls back: a schema with no `properties` (one that * is no literal, one that combines others with `oneOf`, `anyOf` or `allOf`, * one that points elsewhere with `$ref`, one that declares its keys by * `patternProperties`) is `Record`; a property with none of * the keywords above is `unknown`, an array of no single `items` schema * `unknown[]`. * * The specification's table types each action by them: a default action * (create, update, delete) gets its entity's attributes as its parameters, * required at every depth as the platform keeps them on it * (`DefaultParametersOf`), and returns `Partial` of them as written, the * platform's results schema being the attributes' unprocessed; a custom action * its own `parameters` and `results` schemas. The platform's own: main-service-api * `src/services/ActionSpecificationService.js`, `buildActionFromReferenceSchema` * and `#processSchemaRequiredFields`. */ /** The value a JSON Schema `type` names; each of a list of them. */ type TypeValue = Type extends readonly (infer Member)[] ? TypeValue : Type extends "string" ? string : Type extends "integer" | "number" ? number : Type extends "boolean" ? boolean : Type extends "null" ? null : Type extends "array" ? unknown[] : Type extends "object" ? Record : unknown; /** The value of a property whose schema is `Schema`, its objects' `required` as the default action `Action` keeps it (`RequiredOn`). */ type PropertyValue = Schema extends { enum: readonly (infer Member)[]; } ? Member : Schema extends { const: infer Constant; } ? Constant : Schema extends { properties: object; } ? ObjectValues : Schema extends { type: "array"; items: infer Item; } ? PropertyValue[] : Schema extends { type: infer Type; } ? TypeValue : unknown; /** The names `Schema` requires; none when its `required` is no literal (a schema written without `as const`), whose names the type cannot know. */ type RequiredNames = Schema extends { required: readonly (infer Name)[]; } ? (string extends Name ? never : Name) : never; /** `Name`, when the platform keeps it required on the default action `Action`: its property has no `visibleOn`, or is visible on `Action`. */ type KeptName = Name extends keyof Properties ? Properties[Name] extends { visibleOn: readonly (infer Role)[]; } ? Action extends Role ? Name : never : Name : never; /** * The names `Schema` requires on the default action `Action`, as the platform * processes them at every depth (main-service-api * `src/services/ActionSpecificationService.js`, `#processSchemaRequiredFields`, * which recurses into each property's `properties` and, for one without them, * its `items`): every one when no action reads it (`undefined`: a schema * read as written), none on delete, else those it keeps (`KeptName`). */ type RequiredOn = [Action] extends [undefined] ? RequiredNames : Action extends "delete" ? never : Schema extends { properties: infer Properties; } ? KeptName, Properties, Action> : never; /** The keys an object of `Schema` holds beside its properties: none when `additionalProperties` is `false`, else any, each `unknown`. */ type AdditionalValues = Schema extends { additionalProperties: false; } ? unknown : { [key: string]: unknown; }; /** `Value`'s keys as one object type: what an intersection of objects reads as, its index signature kept. */ type Flattened = { [Key in keyof Value]: Value[Key]; }; /** The values an object schema `Schema` declares on the default action `Action` (`undefined`: as written): its required properties, its optional ones, and the keys it allows beside them; `Record` for a schema with no `properties`. */ type ObjectValues = Schema extends { properties: infer Properties; } ? Flattened<{ -readonly [Key in keyof Properties as Key extends RequiredOn ? Key : never]: PropertyValue; } & { -readonly [Key in keyof Properties as Key extends RequiredOn ? never : Key]?: PropertyValue; } & AdditionalValues> : Record; /** The values an object schema `Schema` declares, as written: its required properties, its optional ones, and the keys it allows beside them; `Record` for a schema with no `properties`. */ type ValuesOf = ObjectValues; /** What a default action `Action` (`create`, `update`, `delete`) gets as its parameters: the attributes `Schema` declares, each required, at every depth, that the platform keeps required on it; none on delete. */ type DefaultParametersOf = ObjectValues; /** The schema of the attributes a specification body `Body` declares; none when it declares none. */ type AttributesSchemaOf = Body extends { attributes: { schema: infer Schema; }; } ? Schema : undefined; /** The schema an action specification body `Body` declares under `Part` (`parameters`, `results`); none when it declares none. */ type PartSchemaOf = Body extends { [Key in Part]: { schema: infer Schema; }; } ? Schema : undefined; /** What a custom action whose specification body is `Body` returns: the values its `results` schema declares; `unknown` when it declares none. */ type CustomResultOf = PartSchemaOf extends undefined ? unknown : ValuesOf>; /** The platform's default actions, by the key each is bound to (`[ServiceActions.CREATE]: …`): the contract's `default_actions`. */ declare const ServiceActions: { readonly CREATE: "create"; readonly UPDATE: "update"; readonly DELETE: "delete"; }; /** A default action's key. */ type DefaultActionKey = (typeof ServiceActions)[keyof typeof ServiceActions]; /** Whether the action `Key` of the specification whose body is `Body` is a default one: a default action's key, unless the body uses none (`use_default_actions: false`). */ type IsDefault = Key extends DefaultActionKey ? (Body extends { use_default_actions: false; } ? false : true) : false; /** The attributes a specification whose body is `Body` stores, each optional: what its entity's `attributes` hold; any, untyped, when it declares no schema. */ type StoredAttributesOf = AttributesSchemaOf extends undefined ? Record : Partial>>; /** The parameters the action `Key` of the specification whose body is `Body` gets, its own specification `Declared`: a default action's its entity's attributes, as the platform requires them on it; a custom action's its `parameters` schema's. */ type ActionParametersOf = IsDefault extends true ? DefaultParametersOf, Key> : ValuesOf>; /** What the action `Key` of the specification whose body is `Body` returns, its own specification `Declared`: a default action `Partial` of its entity's attributes; a custom action its `results` schema's values, `unknown` without one. */ type ActionResultOf = IsDefault extends true ? StoredAttributesOf : CustomResultOf; /** * What the service's action `Key` gets as its input, the service's body * `Body`, the action's own specification `Declared` (a custom one's): the * parameters its schemas declare and the service's attributes. A handler * written apart from its definition is typed by it: * * const create = async (input: ActionInputOf<"create", typeof specification>, context: Context): Promise> => ({}); */ interface ActionInputOf extends ServiceInput, StoredAttributesOf> {} /** What the action `Key` of a link whose body is `LinkBody`, of the service whose body is `Body`, gets as its input: its parameters the link's, the service's attributes and the link's. */ interface LinkActionInputOf extends LinkInput, StoredAttributesOf, StoredAttributesOf> {} /** A handler of the input `Input`, and the run's context, that returns `Result`: the return is checked against it, never inferred from it. */ type TypedHandler = (input: Input, context: Context) => NoInfer | Promise>; /** An action's entry: its handler, or its handler with what it declares beside it, its specification `Declared`. */ type TypedEntry = TypedHandler | (Omit, "handler" | "specification"> & { specification?: Declared; handler: TypedHandler; }); /** * The actions a definition declares when TypeScript infers none of their keys: * every entry is a handler that reads its input, which TypeScript types only * once their keys are fixed. They are the default ones, each optional. */ type DefaultSpecs = Partial>; /** The service's actions, by key, each typed by its schemas: its own specification `Specs[Key]`, its service's body `Body`. */ type ServiceEntries = { [Key in keyof Specs]: TypedEntry, ActionResultOf, Specs[Key]>; }; /** * A handler of the open interface, of the input `Input`. It is declared as a * method, whose parameters TypeScript checks both ways, so each default key's * own handler type fits it beside them under the same index. */ type OpenHandler = { handle(input: Input, context: Context): unknown; }["handle"]; /** An entry of the open interface: its handler, or its handler with what it declares beside it. */ type OpenEntry = OpenHandler | (Omit, "handler"> & { handler: OpenHandler; }); /** * A link's actions, by key: each default one typed by the link's schemas and * its service's, every other the open interface, an entry of the link's * input (`LinkInput`) whose schemas the type does not read: TypeScript * infers no custom link action's specification, so its handler is annotated by * hand, and a value that is no entry does not typecheck. */ type LinkEntries = { [Key in DefaultActionKey]?: TypedEntry, ActionResultOf>; } & { [key: string]: OpenEntry | undefined; }; /** What a link's middleware is handed: the input of any action of it, its parameters untyped. */ type LinkMiddlewareInput = LinkInput, StoredAttributesOf, StoredAttributesOf>; /** A link of the service whose body is `Body`, by its key under `links`, its own body `LinkBody`. */ interface LinkDefinition, Body = Record> { /** The link_specification body, as the API takes it. */ specification?: LinkBody; /** What the link answers, by key: its default actions, while it uses them, and its custom ones. */ actions: LinkEntries; /** Around each of its actions' handlers, outermost first, after the kind's own middleware: it is handed the link's action's input, its parameters untyped, which the route builds again right before the handler. */ use?: Middleware>[]; } /** What every specification's define takes beside its specification and its actions, its middleware typed by its actions' input `Input`. */ interface SpecificationOptions { /** The package it belongs to: its name, version and description (the slug is the name's unless given), the project's own. */ package: PackageDeclaration; /** How the package's agent channel reaches the worker. */ agent?: AgentOptions; /** Around the handlers of the kind's actions whose input is `Input`, outermost first, after the kind's own middleware (the action reported, its input built): it is handed that input, as those handlers are. Never on a specification's last route, nor on the author's own routes, which run their own. */ use?: Middleware[]; /** What else it answers, after its actions, each with its own middleware (`route.use`). */ routes?: Route[]; } /** The links a definition declares when TypeScript infers none of their names (none gives its specification, and every handler reads its input): any, each of an untyped body. */ type UntypedLinks = Record>; /** What a service's middleware is handed: the input of any of its own actions, its parameters untyped, which the route builds again right before the handler. */ type ServiceMiddlewareInput = ServiceInput, StoredAttributesOf>; /** What a service is defined by: its body `Body`, each custom action's specification `Specs[key]`, each link's body `Links[key]`. */ interface ServiceDefinition, Specs = Record, Links = UntypedLinks> extends SpecificationOptions> { /** The service_specification body, as the API takes it; named by the package's slug. */ specification: Body; /** What it answers, by key: its default actions, while it uses them, and its custom ones, each typed by its schemas. */ actions: ServiceEntries; /** Its links, by key. */ links?: { [Name in keyof Links]: LinkDefinition; }; /** Around each of the service's own actions' handlers, after the kind's own middleware: it is handed the service's action's input, its parameters untyped. Never on a link's action, which runs its link's own. */ use?: Middleware>[]; } /** * A service of its package: its specification, its actions and its links * published, a route per action. A definition with a problem is refused, * naming every one at once: the specification's (its body, its actions, its * links), each link's (its body, its actions), then the definition's own. */ declare function defineService, const Specs extends Record = DefaultSpecs, const Links extends Record = UntypedLinks>(definition: ServiceDefinition): Plugin; export { ServiceDefinition as a, TypedEntry as c, PartSchemaOf as d, ValuesOf as f, ServiceActions as i, defineService as l, ServiceInput as m, ActionResultOf as n, SpecificationOptions as o, LinkInput as p, LinkActionInputOf as r, StoredAttributesOf as s, ActionInputOf as t, CustomResultOf as u }; //# sourceMappingURL=define-DwE2AZUZ.d.mts.map