@azure-utils/durable-functions
Version:
Utilities for Azure Durable functions.
1 lines • 7.49 kB
Source Map (JSON)
{"version":3,"file":"durable-entities-api-dPDbDq65.cjs","names":["options: SignalAzureFunctionsEntityOptions","requestOptions: RequestAzureFunctionsDurableTaskWebhookOptions","requestAzureFunctionsDurableTaskWebhookOrThrow","options: GetAzureFunctionsEntityOptions","options: ListAzureFunctionsEntitiesOptions"],"sources":["../src/durable-entities-api.ts"],"sourcesContent":["/**\n * @module\n * Helper utils to interact with Azure Functions Entities.\n */\n\nimport {\n type RequestAzureFunctionsDurableTaskWebhookOptions,\n requestAzureFunctionsDurableTaskWebhookOrThrow,\n} from \"./durable-webhook.js\";\n\n/**\n * An object that contains all the functions for interacting with Azure Functions Durable Entity.\n *\n * @throws Each method can throw an error if the operation fails.\n */\nexport const durableEntities = {\n /** Query and list Azure Functions Entity Instances. */\n list: listAzureFunctionsEntitiesOrThrow,\n /** Gets the state of the specified entity. */\n getState: getAzureFunctionsEntityStateOrThrow,\n /** Sends a one-way operation message to a Durable Entity. */\n signal: signalAzureFunctionsEntityOrThrow,\n};\n\n/**\n * Options for the `signalAzureFunctionsEntityOrThrow` function.\n */\nexport type SignalAzureFunctionsEntityOptions = {\n /** The name (type) of the entity. */\n entityName: string;\n /** The key (unique ID) of the entity. */\n entityKey: string;\n /** Optional. The name of the user-defined operation to invoke. */\n operationName?: string;\n /** The JSON-formatted event payload. */\n operationInput?: unknown;\n};\n/**\n * Sends a one-way operation message to a Durable Entity. If the entity doesn't exist, it will be created automatically.\n */\nexport async function signalAzureFunctionsEntityOrThrow(\n options: SignalAzureFunctionsEntityOptions,\n requestOptions: RequestAzureFunctionsDurableTaskWebhookOptions\n): Promise<void> {\n const { entityKey, entityName, operationInput, operationName } = options;\n\n const searchParams = new URLSearchParams();\n if (operationName) {\n searchParams.set(\"op\", operationName);\n }\n\n await requestAzureFunctionsDurableTaskWebhookOrThrow(\n `entities/${entityName}/${entityKey}`,\n {\n ...requestOptions,\n searchParams,\n body: JSON.stringify(operationInput),\n method: \"POST\",\n }\n );\n\n return;\n}\n\n/**\n * Options for the `getAzureFunctionsEntityOrThrow` function.\n */\nexport type GetAzureFunctionsEntityOptions = {\n /** The name (type) of the entity. */\n entityName: string;\n /** The key (unique ID) of the entity. */\n entityKey: string;\n};\n\n/**\n * Gets the state of the specified entity.\n */\nexport async function getAzureFunctionsEntityStateOrThrow<T = unknown>(\n options: GetAzureFunctionsEntityOptions,\n requestOptions: RequestAzureFunctionsDurableTaskWebhookOptions\n): Promise<T> {\n const { entityKey, entityName } = options;\n\n const response = await requestAzureFunctionsDurableTaskWebhookOrThrow(\n `entities/${entityName}/${entityKey}`,\n requestOptions\n );\n\n return response.json() as T;\n}\n\n/**\n * Options for the `listAzureFunctionsEntitiesOrThrow` function.\n */\nexport type ListAzureFunctionsEntitiesOptions = {\n /** Optional. When specified, filters the list of returned entities by their entity name (case-insensitive). */\n entityName?: string;\n /** Optional parameter. If set to true, the entity state will be included in the response payload. */\n fetchState?: boolean;\n /** Optional parameter. When specified, filters the list of returned entities that processed operations after the given ISO8601 timestamp. */\n lastOperationTimeFrom?: Date;\n /** Optional parameter. When specified, filters the list of returned entities that processed operations before the given ISO8601 timestamp. */\n lastOperationTimeTo?: Date;\n /** Optional parameter. When specified, limits the number of entities returned by the query. */\n top?: number;\n /**\n * Optional parameter. When specified, the continuation token for the next page of results.\n * This token is returned in the response headers of the previous page of results.\n */\n continuationToken?: string;\n};\n\n/**\n * The result of listing Azure Functions Orchestration Instances.\n */\nexport type ListAzureFunctionsEntitiesResult<T> = {\n /** The number of instances returned by the query. */\n top: number | null;\n /**\n * The continuation token for the next page of results.\n * This token is returned in the response headers of the previous page of results.\n */\n continuationToken: string | null;\n /** The list of Azure Functions Entity Instances.*/\n instances: AzureFunctionsEntityInstance<T>[];\n};\n/**\n * Query and list Azure Functions Entity Instances.\n */\nexport async function listAzureFunctionsEntitiesOrThrow<T = unknown>(\n options: ListAzureFunctionsEntitiesOptions,\n requestOptions: RequestAzureFunctionsDurableTaskWebhookOptions\n): Promise<ListAzureFunctionsEntitiesResult<T>> {\n const {\n entityName = \"\",\n fetchState,\n lastOperationTimeFrom,\n lastOperationTimeTo,\n top,\n continuationToken,\n } = options;\n\n const searchParams = new URLSearchParams();\n if (fetchState) {\n searchParams.set(\"fetchState\", \"true\");\n }\n if (lastOperationTimeFrom) {\n searchParams.set(\n \"lastOperationTimeFrom\",\n lastOperationTimeFrom.toISOString()\n );\n }\n if (lastOperationTimeTo) {\n searchParams.set(\"lastOperationTimeTo\", lastOperationTimeTo.toISOString());\n }\n if (top) {\n searchParams.set(\"top\", top.toString());\n }\n\n const continuationTokenHeader = \"x-ms-continuation-token\";\n const headers = new Headers();\n if (continuationToken) {\n headers.set(continuationTokenHeader, continuationToken);\n }\n\n const response = await requestAzureFunctionsDurableTaskWebhookOrThrow(\n `entities/${entityName}`,\n { ...requestOptions, searchParams, headers }\n );\n\n const instances =\n (await response.json()) as AzureFunctionsEntityInstance<T>[];\n\n return {\n instances,\n top: top ?? null,\n continuationToken: response.headers.get(continuationTokenHeader),\n };\n}\n\n/**\n * The Azure Functions Entity Instance.\n */\nexport type AzureFunctionsEntityInstance<T = unknown> = {\n entityId: { name: string; key: string };\n lastOperationTime: string;\n state?: T;\n};\n"],"mappings":";;;;;;;;AAeA,MAAa,kBAAkB;CAE7B,MAAM;CAEN,UAAU;CAEV,QAAQ;AACT;;;;AAkBD,eAAsB,kCACpBA,SACAC,gBACe;CACf,MAAM,EAAE,WAAW,YAAY,gBAAgB,eAAe,GAAG;CAEjE,MAAM,eAAe,IAAI;AACzB,KAAI,eACF,aAAa,IAAI,MAAM,cAAc;CAGvC,MAAMC,uEACJ,CAAC,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,EACrC;EACE,GAAG;EACH;EACA,MAAM,KAAK,UAAU,eAAe;EACpC,QAAQ;CACT,EACF;AAED;AACD;;;;AAeD,eAAsB,oCACpBC,SACAF,gBACY;CACZ,MAAM,EAAE,WAAW,YA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