| import * as Schema from "../../Schema.js"; | |
| import * as HttpApiEndpoint from "../httpapi/HttpApiEndpoint.js"; | |
| import * as HttpApiGroup from "../httpapi/HttpApiGroup.js"; | |
| import * as Rpc from "../rpc/Rpc.js"; | |
| import * as RpcGroup from "../rpc/RpcGroup.js"; | |
| /** | |
| * Derives an `RpcGroup` from a list of workflows. | |
| * | |
| * **Example** (Deriving RPC endpoints from workflows) | |
| * | |
| * ```ts | |
| * import { Layer, Schema } from "effect" | |
| * import { RpcServer } from "effect/unstable/rpc" | |
| * import { Workflow, WorkflowProxy, WorkflowProxyServer } from "effect/unstable/workflow" | |
| * | |
| * const EmailWorkflow = Workflow.make("EmailWorkflow", { | |
| * payload: { | |
| * id: Schema.String, | |
| * to: Schema.String | |
| * }, | |
| * idempotencyKey: ({ id }) => id | |
| * }) | |
| * | |
| * const myWorkflows = [EmailWorkflow] as const | |
| * | |
| * // Use WorkflowProxy.toRpcGroup to create a `RpcGroup` from the | |
| * // workflows | |
| * class MyRpcs extends WorkflowProxy.toRpcGroup(myWorkflows) {} | |
| * | |
| * // Use WorkflowProxyServer.layerRpcHandlers to create a layer that implements | |
| * // the rpc handlers | |
| * const ApiLayer = RpcServer.layer(MyRpcs).pipe( | |
| * Layer.provide(WorkflowProxyServer.layerRpcHandlers(myWorkflows)) | |
| * ) | |
| * ``` | |
| * | |
| * @category constructors | |
| * @since 4.0.0 | |
| */ | |
| export const toRpcGroup = (workflows, options) => { | |
| const prefix = options?.prefix ?? ""; | |
| const rpcs = []; | |
| for (const workflow_ of workflows) { | |
| const workflow = workflow_; | |
| rpcs.push(Rpc.make(`${prefix}${workflow._tag}`, { | |
| payload: workflow.payloadSchema, | |
| error: workflow.errorSchema, | |
| success: workflow.successSchema | |
| }).annotateMerge(workflow.annotations), Rpc.make(`${prefix}${workflow._tag}Discard`, { | |
| payload: workflow.payloadSchema | |
| }).annotateMerge(workflow.annotations), Rpc.make(`${prefix}${workflow._tag}Resume`, { | |
| payload: ResumePayload | |
| }).annotateMerge(workflow.annotations)); | |
| } | |
| return RpcGroup.make(...rpcs); | |
| }; | |
| /** | |
| * Derives an `HttpApiGroup` from a list of workflows. | |
| * | |
| * **Example** (Deriving HTTP API endpoints from workflows) | |
| * | |
| * ```ts | |
| * import { Layer, Schema } from "effect" | |
| * import { HttpApi, HttpApiBuilder } from "effect/unstable/httpapi" | |
| * import { Workflow, WorkflowProxy, WorkflowProxyServer } from "effect/unstable/workflow" | |
| * | |
| * const EmailWorkflow = Workflow.make("EmailWorkflow", { | |
| * payload: { | |
| * id: Schema.String, | |
| * to: Schema.String | |
| * }, | |
| * idempotencyKey: ({ id }) => id | |
| * }) | |
| * | |
| * const myWorkflows = [EmailWorkflow] as const | |
| * | |
| * // Use WorkflowProxy.toHttpApiGroup to create a `HttpApiGroup` from the | |
| * // workflows | |
| * class MyApi extends HttpApi.make("api") | |
| * .add(WorkflowProxy.toHttpApiGroup("workflows", myWorkflows)) | |
| * {} | |
| * | |
| * // Use WorkflowProxyServer.layerHttpApi to create a layer that implements the | |
| * // workflows HttpApiGroup | |
| * const ApiLayer = HttpApiBuilder.layer(MyApi).pipe( | |
| * Layer.provide( | |
| * WorkflowProxyServer.layerHttpApi(MyApi, "workflows", myWorkflows) | |
| * ) | |
| * ) | |
| * ``` | |
| * | |
| * @category constructors | |
| * @since 4.0.0 | |
| */ | |
| export const toHttpApiGroup = (name, workflows) => { | |
| let group = HttpApiGroup.make(name); | |
| for (const workflow_ of workflows) { | |
| const workflow = workflow_; | |
| const path = `/${tagToPath(workflow._tag)}`; | |
| group = group.add(HttpApiEndpoint.post(workflow._tag, path, { | |
| payload: workflow.payloadSchema, | |
| success: workflow.successSchema, | |
| error: workflow.errorSchema | |
| }).annotateMerge(workflow.annotations), HttpApiEndpoint.post(workflow._tag + "Discard", `${path}/discard`, { | |
| payload: workflow.payloadSchema | |
| }).annotateMerge(workflow.annotations), HttpApiEndpoint.post(workflow._tag + "Resume", `${path}/resume`, { | |
| payload: ResumePayload | |
| }).annotateMerge(workflow.annotations)); | |
| } | |
| return group; | |
| }; | |
| const tagToPath = tag => tag | |
| // .replace(/[^a-zA-Z0-9]+/g, "-") // Replace non-alphanumeric characters with hyphen | |
| // .replace(/([a-z])([A-Z])/g, "$1-$2") // Insert hyphen before uppercase letters | |
| .toLowerCase(); | |
| const ResumePayload = /*#__PURE__*/Schema.Struct({ | |
| executionId: Schema.String | |
| }); | |
| //# sourceMappingURL=WorkflowProxy.js.map |
Xet Storage Details
- Size:
- 4.1 kB
- Xet hash:
- 3b9b954ad19f4fe06ce2f0dd935a7580c6ec899a6cc4e3c6e53d628f9f2d4f21
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.