|
| 1 | +import { temporal } from '@temporalio/proto'; |
| 2 | +import { PayloadConverter } from './converter/payload-converter'; |
| 3 | +import { LoadedDataConverter } from './converter/data-converter'; |
| 4 | +import { encodeOptionalToPayload, decodeOptionalSinglePayload, encodeOptionalSingle } from './internal-non-workflow'; |
| 5 | + |
| 6 | +/** |
| 7 | + * User metadata that can be attached to workflow commands. |
| 8 | + */ |
| 9 | +export interface UserMetadata { |
| 10 | + /** @experimental A fixed, single line summary of the command's purpose */ |
| 11 | + staticSummary?: string; |
| 12 | + /** @experimental Fixed additional details about the command for longer-text description, can span multiple lines */ |
| 13 | + staticDetails?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export function userMetadataToPayload( |
| 17 | + payloadConverter: PayloadConverter, |
| 18 | + staticSummary: string | undefined, |
| 19 | + staticDetails: string | undefined |
| 20 | +): temporal.api.sdk.v1.IUserMetadata | undefined { |
| 21 | + if (staticSummary == null && staticDetails == null) return undefined; |
| 22 | + |
| 23 | + const summary = encodeOptionalToPayload(payloadConverter, staticSummary); |
| 24 | + const details = encodeOptionalToPayload(payloadConverter, staticDetails); |
| 25 | + |
| 26 | + if (summary == null && details == null) return undefined; |
| 27 | + |
| 28 | + return { summary, details }; |
| 29 | +} |
| 30 | + |
| 31 | +export async function encodeUserMetadata( |
| 32 | + dataConverter: LoadedDataConverter, |
| 33 | + staticSummary: string | undefined, |
| 34 | + staticDetails: string | undefined |
| 35 | +): Promise<temporal.api.sdk.v1.IUserMetadata | undefined> { |
| 36 | + if (staticSummary == null && staticDetails == null) return undefined; |
| 37 | + |
| 38 | + const { payloadConverter, payloadCodecs } = dataConverter; |
| 39 | + const summary = await encodeOptionalSingle( |
| 40 | + payloadCodecs, |
| 41 | + await encodeOptionalToPayload(payloadConverter, staticSummary) |
| 42 | + ); |
| 43 | + const details = await encodeOptionalSingle( |
| 44 | + payloadCodecs, |
| 45 | + await encodeOptionalToPayload(payloadConverter, staticDetails) |
| 46 | + ); |
| 47 | + |
| 48 | + if (summary == null && details == null) return undefined; |
| 49 | + |
| 50 | + return { summary, details }; |
| 51 | +} |
| 52 | + |
| 53 | +export async function decodeUserMetadata( |
| 54 | + dataConverter: LoadedDataConverter, |
| 55 | + metadata: temporal.api.sdk.v1.IUserMetadata | undefined | null |
| 56 | +): Promise<UserMetadata> { |
| 57 | + const res = { staticSummary: undefined, staticDetails: undefined }; |
| 58 | + if (metadata == null) return res; |
| 59 | + |
| 60 | + const staticSummary = (await decodeOptionalSinglePayload<string>(dataConverter, metadata.summary)) ?? undefined; |
| 61 | + const staticDetails = (await decodeOptionalSinglePayload<string>(dataConverter, metadata.details)) ?? undefined; |
| 62 | + |
| 63 | + if (staticSummary == null && staticDetails == null) return res; |
| 64 | + |
| 65 | + return { staticSummary, staticDetails }; |
| 66 | +} |
0 commit comments