@usearazzo/runner 1.0.1-alpha.27 → 1.0.1-alpha.29

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.
@@ -1,4 +1,5 @@
1
1
  import { toValue } from '@speclynx/apidom-core';
2
+ import { isPlainObject } from 'ramda-adjunct';
2
3
  import { isArrayElement, isObjectElement, isStringElement } from '@speclynx/apidom-datamodel';
3
4
  import { isFailureActionElement, isParameterElement, isStepElement, isSuccessActionElement } from '@speclynx/apidom-ns-arazzo-1';
4
5
  import RuntimeExpressionEvaluator from "../expression/RuntimeExpressionEvaluator.mjs";
@@ -14,7 +15,7 @@ import WorkflowCallStack from "./WorkflowCallStack.mjs";
14
15
  import StepTransitionInterpreter, { actionTargets } from "./StepTransitionInterpreter.mjs";
15
16
  import ExecutionError from "../errors/ExecutionError.mjs";
16
17
  import ResolverError from "../errors/ResolverError.mjs";
17
- import { readAbortSignal, throwIfAborted } from "./abort.mjs";
18
+ import { throwIfAborted } from "./abort.mjs";
18
19
  /**
19
20
  * Options for the WorkflowExecutor.
20
21
  * @public
@@ -149,7 +150,7 @@ class WorkflowExecutor {
149
150
  #maxWorkflowDepth;
150
151
  #now;
151
152
  #extractor = new ArazzoWorkflowExtractor();
152
- #normalizer = new ArazzoWorkflowNormalizer();
153
+ #normalizer;
153
154
  #outputResolver = new OutputResolver();
154
155
  #parameterResolver = new WorkflowParameterResolver();
155
156
  #stepExecutor;
@@ -160,6 +161,7 @@ class WorkflowExecutor {
160
161
  this.#document = options.document;
161
162
  this.#registry = options.registry;
162
163
  this.#workflowLocatorNormalizer = new ArazzoWorkflowLocatorNormalizer(options.registry);
164
+ this.#normalizer = options.workflowNormalizer ?? new ArazzoWorkflowNormalizer();
163
165
  this.#maxSteps = options.maxSteps ?? WorkflowExecutor.#DEFAULT_MAX_STEPS;
164
166
  this.#maxWorkflowDepth = options.maxWorkflowDepth ?? WorkflowExecutor.#DEFAULT_MAX_WORKFLOW_DEPTH;
165
167
  this.#now = options.now ?? WorkflowExecutor.#DEFAULT_NOW;
@@ -182,23 +184,21 @@ class WorkflowExecutor {
182
184
  */
183
185
  async execute(workflowId, options = {}) {
184
186
  this.#rejectUnknownOptions(options);
185
-
186
- // a signal in the opaque bag was the only channel before this option existed,
187
- // and it still reaches the transport. Absorbing it here is what makes it a
188
- // whole cancellation rather than half of one: read only at dispatch, it would
189
- // abort requests while the loop walked on through the remaining steps.
190
- const signal = options.signal ?? readAbortSignal(options.executeOptions ?? {});
187
+ const {
188
+ signal
189
+ } = options;
190
+ const executeOptions = this.#executeOptionsByDocument(options.executeOptions ?? {});
191
191
  const scope = {
192
- // the first-class signal is also spread into the opaque bag, because that
193
- // bag is how anything reaches the transport: the executor's own checks
194
- // stop the run at the next boundary, and this is what cancels the request
195
- // already in flight. Spread last, so it wins over one passed in the bag.
196
- executeOptions: {
197
- ...options.executeOptions,
198
- ...(signal === undefined ? {} : {
199
- signal
200
- })
201
- },
192
+ // the signal is put into every step's opaque bag, because that bag is
193
+ // how anything reaches the transport: the executor's own checks stop the
194
+ // run at the next boundary, and this is what cancels the request already
195
+ // in flight. Set even when absent, so a `signal` in an entry never
196
+ // stands in for it: that one would cancel the steps of a single API and
197
+ // leave the run walking on.
198
+ executeOptions: locator => ({
199
+ ...executeOptions.get(locator.document.uri),
200
+ signal
201
+ }),
202
202
  dependencyInputs: options.dependencyInputs ?? {},
203
203
  runDependencies: options.runDependencies ?? true,
204
204
  signal,
@@ -217,6 +217,41 @@ class WorkflowExecutor {
217
217
  }), 'root');
218
218
  }
219
219
 
220
+ /**
221
+ * Resolves the `executeOptions` entries to the documents they configure,
222
+ * keyed by canonical document URI.
223
+ *
224
+ * Every way an entry can fail to name exactly one OpenAPI document throws
225
+ * here, before the run starts: sent to no API or to two, its options would
226
+ * be missing or misdirected with nothing to show for it until a request
227
+ * failed.
228
+ */
229
+ #executeOptionsByDocument(executeOptions) {
230
+ const byDocument = new Map();
231
+ const fail = (name, problem, reason) => {
232
+ throw new ExecutionError(`executeOptions is keyed by source description name, and "${name}" ${problem}`, {
233
+ reason
234
+ });
235
+ };
236
+ for (const [name, options] of Object.entries(executeOptions)) {
237
+ const uri = this.#document.resolveSourceDescriptionURI(name);
238
+ if (uri === undefined) {
239
+ return fail(name, `names no source description with a url in the Arazzo document at "${this.#document.uri}"`, ExecutionError.SourceDescriptionNotFound);
240
+ }
241
+ if (this.#document.resolveSourceDescriptionField(name, 'type') === 'arazzo') {
242
+ fail(name, 'names an Arazzo document', ExecutionError.SourceDescriptionNotOpenAPI);
243
+ }
244
+ if (!isPlainObject(options)) {
245
+ fail(name, 'is not given an object of options', ExecutionError.UnknownExecuteOption);
246
+ }
247
+ if (byDocument.has(uri)) {
248
+ fail(name, `points at "${uri}", which another entry already configures`, ExecutionError.AmbiguousExecuteOptions);
249
+ }
250
+ byDocument.set(uri, options);
251
+ }
252
+ return byDocument;
253
+ }
254
+
220
255
  /**
221
256
  * Rejects an options bag carrying keys this method does not recognize.
222
257
  *
package/types/runner.d.ts CHANGED
@@ -223,9 +223,14 @@ export declare class ArazzoRunnerError extends ApiDOMStructuredError {
223
223
  *
224
224
  * Every option is a seam of a building block the runner wires: `registry` is
225
225
  * the {@link DocumentRegistry} the documents load into (a fresh one with the
226
- * default providers when omitted), `httpClient` and `requestInterceptor` go to
226
+ * default providers when omitted), `reference` goes to everything the runner
227
+ * builds that retrieves documents, `httpClient` and `requestInterceptor` go to
227
228
  * the {@link OpenAPIOperationExecutor}, and the rest go to the
228
229
  * {@link WorkflowExecutor}.
230
+ *
231
+ * Retrieving documents and calling APIs are separate on purpose: `reference`
232
+ * never applies to API calls, and `httpClient` / `requestInterceptor` never
233
+ * apply to document retrieval, so credentials for one do not reach the other.
229
234
  * @public
230
235
  */
231
236
  export declare interface ArazzoRunnerLoadOptions extends Pick<OpenAPIOperationExecutorOptions, 'httpClient' | 'requestInterceptor'>, Pick<WorkflowExecutorOptions, 'maxSteps' | 'maxWorkflowDepth' | 'sleep' | 'now'> {
@@ -234,6 +239,20 @@ export declare interface ArazzoRunnerLoadOptions extends Pick<OpenAPIOperationEx
234
239
  * document: loading a different one into it throws.
235
240
  */
236
241
  readonly registry?: DocumentRegistry;
242
+ /**
243
+ * Options of ApiDOM reference, which retrieves the documents: the entry
244
+ * document, its source descriptions and every externally referenced
245
+ * document. Its `resolve` options say how, e.g. `resolverOpts.axiosConfig`
246
+ * for request headers or a TLS agent, or `resolvers` for a resolver list of
247
+ * your own.
248
+ *
249
+ * The runner gives them to the default registry providers and to the
250
+ * normalizers, which fetch external references lazily. A `registry` passed
251
+ * alongside is already configured through its own providers, so `reference`
252
+ * then reaches the normalizers only: give that registry's providers the
253
+ * same options.
254
+ */
255
+ readonly reference?: ArazzoRunnerReferenceOptions;
237
256
  }
238
257
 
239
258
  /**
@@ -251,6 +270,15 @@ export declare interface ArazzoRunnerOptions extends Omit<ArazzoRunnerLoadOption
251
270
  readonly registry: DocumentRegistry;
252
271
  }
253
272
 
273
+ /**
274
+ * The options of ApiDOM reference the runner accepts: its `resolve` options,
275
+ * which mean the same for every building block that retrieves documents.
276
+ * `parse` and `dereference` options differ per building block, so the runner
277
+ * passes none of them on; set those on the building blocks themselves.
278
+ * @public
279
+ */
280
+ export declare type ArazzoRunnerReferenceOptions = Pick<DocumentRegistryProviderOptions, 'resolve'>;
281
+
254
282
  /**
255
283
  * Index mapping a source description name to its element and canonical URI in
256
284
  * an Arazzo document.
@@ -788,6 +816,8 @@ export declare type DocumentRegistryProviderOptions = PartialDeep<ApiDOMReferenc
788
816
  export declare class ExecutionError extends ArazzoRunnerError {
789
817
  /** The run was cancelled through its `AbortSignal`; the signal's reason is the `cause`. */
790
818
  static readonly Aborted: "aborted";
819
+ /** Two `executeOptions` entries name source descriptions that point at the same document. */
820
+ static readonly AmbiguousExecuteOptions: "ambiguous-execute-options";
791
821
  /** A step or a `goto`/`retry` action declares more than one mutually exclusive target. */
792
822
  static readonly AmbiguousTarget: "ambiguous-target";
793
823
  /** The `dependsOn` edges alone form a cycle between workflows. */
@@ -822,7 +852,7 @@ export declare class ExecutionError extends ArazzoRunnerError {
822
852
  static readonly StepBudget: "step-budget";
823
853
  /** An action declares a `type` other than `end`, `goto` or `retry`. */
824
854
  static readonly UnknownActionType: "unknown-action-type";
825
- /** `execute` received an option it does not know. */
855
+ /** `execute` received an option it does not know, or an `executeOptions` entry that is not an object. */
826
856
  static readonly UnknownExecuteOption: "unknown-execute-option";
827
857
  /** A step produced a transition the workflow executor does not recognize. */
828
858
  static readonly UnknownTransitionKind: "unknown-transition-kind";
@@ -1122,11 +1152,14 @@ export declare type OpenAPIOperationElement = OperationElement | OperationElemen
1122
1152
  * building the request and sending it.
1123
1153
  *
1124
1154
  * Typed here are the options the runner itself drives and the ones callers
1125
- * routinely reach for. The rest of `buildRequest`'s surface (`securities`,
1126
- * `scheme`, `parameterBuilders`, `attachContentTypeForEmptyPayload`, …) is
1127
- * deliberately left untyped and passes through the bag's open tail unchanged —
1128
- * see {@link OpenAPIOperationExecuteOptionsBag}. Authentication in particular
1129
- * is not modeled: Arazzo is auth-agnostic, so credentials are applied with a
1155
+ * routinely reach for. The rest of `buildRequest`'s surface (`scheme`,
1156
+ * `parameterBuilders`, `attachContentTypeForEmptyPayload`, …) is deliberately
1157
+ * left untyped and passes through the bag's open tail unchanged — see
1158
+ * {@link OpenAPIOperationExecuteOptionsBag}.
1159
+ *
1160
+ * Arazzo is auth-agnostic, so credentials come from outside the workflow:
1161
+ * static ones for the security schemes the OpenAPI document declares go in
1162
+ * `securities`, and anything computed per request is applied with a
1130
1163
  * {@link RequestInterceptor}.
1131
1164
  * @public
1132
1165
  */
@@ -1171,6 +1204,13 @@ export declare interface OpenAPIOperationExecuteOptions {
1171
1204
  * a declared server — an override carries no declared variables.
1172
1205
  */
1173
1206
  readonly serverVariables?: Record<string, string>;
1207
+ /**
1208
+ * Credentials for the security schemes of the operation's OpenAPI document.
1209
+ * Only the schemes the operation requires are applied, each where its
1210
+ * scheme says (header, query or cookie); an operation with an empty
1211
+ * `security` gets none.
1212
+ */
1213
+ readonly securities?: OpenAPIOperationSecurities;
1174
1214
  readonly signal?: AbortSignal;
1175
1215
  readonly requestInterceptor?: RequestInterceptor;
1176
1216
  }
@@ -1183,11 +1223,9 @@ export declare interface OpenAPIOperationExecuteOptions {
1183
1223
  * the rest of `buildRequest`'s surface (`scheme`, `parameterBuilders`,
1184
1224
  * `attachContentTypeForEmptyPayload`, …) passes through untouched.
1185
1225
  *
1186
- * Nothing routine belongs in the tail. Credentials go through a
1187
- * {@link RequestInterceptor} and `server` covers every case that once needed a
1188
- * `contextUrl`, so reaching for either of those here means an edge case: tests
1189
- * standing in for a document fetched over HTTP, or credentials applied from the
1190
- * document's own security schemes.
1226
+ * Nothing routine belongs in the tail. `server` covers every case that once
1227
+ * needed a `contextUrl`, so reaching for that here means an edge case: tests
1228
+ * standing in for a document fetched over HTTP.
1191
1229
  *
1192
1230
  * Two kinds of entry are inert rather than honored, since the executor owns
1193
1231
  * what they used to control. The operation target always comes from the
@@ -1581,6 +1619,33 @@ export declare class OpenAPIOperationResponseNormalizer {
1581
1619
  normalize(response: Response, request?: OpenAPIOperationRequestInfo): Promise<OpenAPIOperationResponse>;
1582
1620
  }
1583
1621
 
1622
+ /**
1623
+ * Credentials for the security schemes of an OpenAPI document, keyed by the
1624
+ * scheme name under `components.securitySchemes` (`securityDefinitions` in
1625
+ * OpenAPI 2.0).
1626
+ * @public
1627
+ */
1628
+ export declare interface OpenAPIOperationSecurities {
1629
+ readonly authorized: Readonly<Record<string, OpenAPIOperationSecurityCredential>>;
1630
+ }
1631
+
1632
+ /**
1633
+ * The credential for one security scheme, in the form the vendored
1634
+ * swagger-client applies it: the key or token itself (or `{ value }`) for an
1635
+ * `apiKey` or an `http` bearer scheme, `{ username, password }` for an `http`
1636
+ * basic scheme, and `{ token: { access_token, token_type } }` for an `oauth2`
1637
+ * or `openIdConnect` scheme.
1638
+ * @public
1639
+ */
1640
+ export declare type OpenAPIOperationSecurityCredential = string | {
1641
+ readonly value: string;
1642
+ } | {
1643
+ readonly username: string;
1644
+ readonly password: string;
1645
+ } | {
1646
+ readonly token: Readonly<Record<string, string>>;
1647
+ };
1648
+
1584
1649
  /**
1585
1650
  * Union of PathItemElement across all supported OpenAPI versions.
1586
1651
  * @public
@@ -2146,6 +2211,14 @@ export declare abstract class SourceDescriptionsLocatorNormalizer {
2146
2211
  } | undefined;
2147
2212
  }
2148
2213
 
2214
+ /**
2215
+ * The execute options of a step: the opaque bag forwarded to the operation
2216
+ * executor, or a function returning it for the operation the step was located
2217
+ * to.
2218
+ * @public
2219
+ */
2220
+ export declare type StepExecuteOptions = Record<string, unknown> | ((locator: OpenAPIOperationLocator) => Record<string, unknown>);
2221
+
2149
2222
  /**
2150
2223
  * The outcome of executing a single step.
2151
2224
  * @public
@@ -2232,7 +2305,9 @@ export declare class StepExecutor {
2232
2305
  * `executeOptions` is an opaque bag passed through to the operation executor
2233
2306
  * (e.g. a `server` to run the step against, or an `AbortSignal`). The
2234
2307
  * Arazzo-derived options (`operationId`, `parameters`, `requestBody`) always
2235
- * take precedence over it.
2308
+ * take precedence over it. A function is called with the operation the step
2309
+ * was located to and returns the bag, for options that depend on which API
2310
+ * the step calls.
2236
2311
  *
2237
2312
  * A `signal` in that bag is honored here and not only at the transport: an
2238
2313
  * already-aborted signal throws with reason `ExecutionError.Aborted` instead of issuing a
@@ -2244,7 +2319,7 @@ export declare class StepExecutor {
2244
2319
  * here then carries it alongside the `stepId`, since step ids are only
2245
2320
  * unique within a workflow. The workflow executor always passes it.
2246
2321
  */
2247
- execute(step: StepElement, state: ContextSource, executeOptions?: Record<string, unknown>, workflowId?: string): Promise<StepExecutionResult>;
2322
+ execute(step: StepElement, state: ContextSource, executeOptions?: StepExecuteOptions, workflowId?: string): Promise<StepExecutionResult>;
2248
2323
  /**
2249
2324
  * Throws {@link ResolverError} unless the step's `successCriteria` is absent
2250
2325
  * or a list of Criterion Objects: `malformed-criteria` for one that is
@@ -2477,10 +2552,23 @@ export declare interface WorkflowExecuteOptions {
2477
2552
  */
2478
2553
  readonly inputs?: Record<string, unknown>;
2479
2554
  /**
2480
- * The opaque client-specific bag forwarded verbatim to every
2481
- * {@link StepExecutor.execute} (e.g. a `server` to run against).
2555
+ * Execute options addressed to an API: keyed by the `name` of a source
2556
+ * description of the entry document, each entry the opaque client-specific
2557
+ * bag forwarded verbatim to {@link StepExecutor.execute} for the steps that
2558
+ * call an operation of the OpenAPI document that name points at (e.g. a
2559
+ * `server` to run against, or the credentials of its security schemes).
2560
+ *
2561
+ * An entry configures a document, not a name: a step of another Arazzo
2562
+ * document calling the same OpenAPI document gets the entry whatever name it
2563
+ * knows the document by, and one calling a different document under the same
2564
+ * name gets nothing. Steps of an API without an entry get no options.
2565
+ *
2566
+ * A key naming no source description, one naming a source description that
2567
+ * has no `url` or is not an OpenAPI document, two keys pointing at the same
2568
+ * document, and an entry that is not an object all throw before the first
2569
+ * request.
2482
2570
  */
2483
- readonly executeOptions?: Record<string, unknown>;
2571
+ readonly executeOptions?: Record<string, Record<string, unknown>>;
2484
2572
  /**
2485
2573
  * Inputs for the workflows run implicitly to satisfy `dependsOn`, keyed by
2486
2574
  * the `dependsOn` entry as written — the bare workflowId for a
@@ -2523,11 +2611,10 @@ export declare interface WorkflowExecuteOptions {
2523
2611
  * is not something the steps did, and the steps that never ran were not
2524
2612
  * decided against.
2525
2613
  *
2526
- * The signal is also forwarded to the transport (through the same
2527
- * `executeOptions` bag it may be passed in), so the request in flight when the
2528
- * abort lands is cancelled rather than merely awaited. A `signal` passed in
2529
- * that bag instead is observed here too — it cancels the run just as this
2530
- * option does — and this option wins when both are given.
2614
+ * The signal is also forwarded to the transport with every step's execute
2615
+ * options, so the request in flight when the abort lands is cancelled rather
2616
+ * than merely awaited. This option is the only way to cancel a run: a
2617
+ * `signal` in an `executeOptions` entry is dropped.
2531
2618
  */
2532
2619
  readonly signal?: AbortSignal;
2533
2620
  }
@@ -2754,6 +2841,12 @@ export declare interface WorkflowExecutorOptions {
2754
2841
  * executor is agnostic to how a step reaches the live API.
2755
2842
  */
2756
2843
  readonly stepExecutor: StepExecutor;
2844
+ /**
2845
+ * Normalizes each workflow before it runs: dereferences it, which may fetch
2846
+ * externally referenced documents, and inherits its parameters and actions
2847
+ * into its steps. Defaults to a fresh {@link ArazzoWorkflowNormalizer}.
2848
+ */
2849
+ readonly workflowNormalizer?: ArazzoWorkflowNormalizer;
2757
2850
  /**
2758
2851
  * Upper bound on the number of step attempts in a single run — every attempt
2759
2852
  * of every step counts, including each `retry` and each entry into a