|
| 1 | +import Long from 'long'; |
| 2 | +import { Link as NexusLink } from 'nexus-rpc'; |
| 3 | +import { temporal } from '@temporalio/proto'; |
| 4 | + |
| 5 | +const { EventType } = temporal.api.enums.v1; |
| 6 | +type WorkflowEventLink = temporal.api.common.v1.Link.IWorkflowEvent; |
| 7 | +type EventReference = temporal.api.common.v1.Link.WorkflowEvent.IEventReference; |
| 8 | +type RequestIdReference = temporal.api.common.v1.Link.WorkflowEvent.IRequestIdReference; |
| 9 | + |
| 10 | +const LINK_EVENT_ID_PARAM = 'eventID'; |
| 11 | +const LINK_EVENT_TYPE_PARAM = 'eventType'; |
| 12 | +const LINK_REQUEST_ID_PARAM = 'requestID'; |
| 13 | + |
| 14 | +const EVENT_REFERENCE_TYPE = 'EventReference'; |
| 15 | +const REQUEST_ID_REFERENCE_TYPE = 'RequestIdReference'; |
| 16 | + |
| 17 | +// fullName isn't part of the generated typed unfortunately. |
| 18 | +const WORKFLOW_EVENT_TYPE: string = (temporal.api.common.v1.Link.WorkflowEvent as any).fullName.slice(1); |
| 19 | + |
| 20 | +function pascalCaseToConstantCase(s: string) { |
| 21 | + return s.replace(/[^\b][A-Z]/g, (m) => `${m[0]}_${m[1]}`).toUpperCase(); |
| 22 | +} |
| 23 | + |
| 24 | +function constantCaseToPascalCase(s: string) { |
| 25 | + return s.replace(/[A-Z]+_?/g, (m) => `${m[0]}${m.slice(1).toLocaleLowerCase()}`.replace(/_/, '')); |
| 26 | +} |
| 27 | + |
| 28 | +function normalizeEnumValue(value: string, prefix: string) { |
| 29 | + value = pascalCaseToConstantCase(value); |
| 30 | + if (!value.startsWith(prefix)) { |
| 31 | + value = `${prefix}_${value}`; |
| 32 | + } |
| 33 | + return value; |
| 34 | +} |
| 35 | + |
| 36 | +export function convertWorkflowEventLinkToNexusLink(we: WorkflowEventLink): NexusLink { |
| 37 | + if (!we.namespace || !we.workflowId || !we.runId) { |
| 38 | + throw new TypeError('Missing required fields: namespace, workflowId, or runId'); |
| 39 | + } |
| 40 | + const url = new URL( |
| 41 | + `temporal:///namespaces/${encodeURIComponent(we.namespace)}/workflows/${encodeURIComponent( |
| 42 | + we.workflowId |
| 43 | + )}/${encodeURIComponent(we.runId)}/history` |
| 44 | + ); |
| 45 | + |
| 46 | + if (we.eventRef != null) { |
| 47 | + url.search = convertLinkWorkflowEventEventReferenceToURLQuery(we.eventRef); |
| 48 | + } else if (we.requestIdRef != null) { |
| 49 | + url.search = convertLinkWorkflowEventRequestIdReferenceToURLQuery(we.requestIdRef); |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + url, |
| 54 | + type: WORKFLOW_EVENT_TYPE, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +export function convertNexusLinkToWorkflowEventLink(link: NexusLink): WorkflowEventLink { |
| 59 | + if (link.url.protocol !== 'temporal:') { |
| 60 | + throw new TypeError(`Invalid URL scheme: ${link.url}, expected 'temporal:', got '${link.url.protocol}'`); |
| 61 | + } |
| 62 | + |
| 63 | + // /namespaces/:namespace/workflows/:workflowId/:runId/history |
| 64 | + const parts = link.url.pathname.split('/'); |
| 65 | + if (parts.length !== 7 || parts[1] !== 'namespaces' || parts[3] !== 'workflows' || parts[6] !== 'history') { |
| 66 | + throw new TypeError(`Invalid URL path: ${link.url}`); |
| 67 | + } |
| 68 | + const namespace = decodeURIComponent(parts[2]); |
| 69 | + const workflowId = decodeURIComponent(parts[4]); |
| 70 | + const runId = decodeURIComponent(parts[5]); |
| 71 | + |
| 72 | + const query = link.url.searchParams; |
| 73 | + const refType = query.get('referenceType'); |
| 74 | + |
| 75 | + const workflowEventLink: WorkflowEventLink = { |
| 76 | + namespace, |
| 77 | + workflowId, |
| 78 | + runId, |
| 79 | + }; |
| 80 | + |
| 81 | + switch (refType) { |
| 82 | + case EVENT_REFERENCE_TYPE: |
| 83 | + workflowEventLink.eventRef = convertURLQueryToLinkWorkflowEventEventReference(query); |
| 84 | + break; |
| 85 | + case REQUEST_ID_REFERENCE_TYPE: |
| 86 | + workflowEventLink.requestIdRef = convertURLQueryToLinkWorkflowEventRequestIdReference(query); |
| 87 | + break; |
| 88 | + default: |
| 89 | + throw new TypeError(`Unknown reference type: ${refType}`); |
| 90 | + } |
| 91 | + return workflowEventLink; |
| 92 | +} |
| 93 | + |
| 94 | +function convertLinkWorkflowEventEventReferenceToURLQuery(eventRef: EventReference): string { |
| 95 | + const params = new URLSearchParams(); |
| 96 | + params.set('referenceType', EVENT_REFERENCE_TYPE); |
| 97 | + if (eventRef.eventId != null) { |
| 98 | + const eventId = eventRef.eventId.toNumber(); |
| 99 | + if (eventId > 0) { |
| 100 | + params.set(LINK_EVENT_ID_PARAM, `${eventId}`); |
| 101 | + } |
| 102 | + } |
| 103 | + if (eventRef.eventType != null) { |
| 104 | + const eventType = constantCaseToPascalCase(EventType[eventRef.eventType].replace('EVENT_TYPE_', '')); |
| 105 | + params.set(LINK_EVENT_TYPE_PARAM, eventType); |
| 106 | + } |
| 107 | + return params.toString(); |
| 108 | +} |
| 109 | + |
| 110 | +function convertURLQueryToLinkWorkflowEventEventReference(query: URLSearchParams): EventReference { |
| 111 | + let eventId = 0; |
| 112 | + const eventIdParam = query.get(LINK_EVENT_ID_PARAM); |
| 113 | + if (eventIdParam && /^\d+$/.test(eventIdParam)) { |
| 114 | + eventId = parseInt(eventIdParam, 10); |
| 115 | + } |
| 116 | + const eventTypeParam = query.get(LINK_EVENT_TYPE_PARAM); |
| 117 | + if (!eventTypeParam) { |
| 118 | + throw new TypeError(`Missing eventType parameter`); |
| 119 | + } |
| 120 | + const eventType = EventType[normalizeEnumValue(eventTypeParam, 'EVENT_TYPE') as keyof typeof EventType]; |
| 121 | + if (eventType == null) { |
| 122 | + throw new TypeError(`Unknown eventType parameter: ${eventTypeParam}`); |
| 123 | + } |
| 124 | + return { eventId: Long.fromNumber(eventId), eventType }; |
| 125 | +} |
| 126 | + |
| 127 | +function convertLinkWorkflowEventRequestIdReferenceToURLQuery(requestIdRef: RequestIdReference): string { |
| 128 | + const params = new URLSearchParams(); |
| 129 | + params.set('referenceType', REQUEST_ID_REFERENCE_TYPE); |
| 130 | + if (requestIdRef.requestId != null) { |
| 131 | + params.set(LINK_REQUEST_ID_PARAM, requestIdRef.requestId); |
| 132 | + } |
| 133 | + if (requestIdRef.eventType != null) { |
| 134 | + const eventType = constantCaseToPascalCase(EventType[requestIdRef.eventType].replace('EVENT_TYPE_', '')); |
| 135 | + params.set(LINK_EVENT_TYPE_PARAM, eventType); |
| 136 | + } |
| 137 | + return params.toString(); |
| 138 | +} |
| 139 | + |
| 140 | +function convertURLQueryToLinkWorkflowEventRequestIdReference(query: URLSearchParams): RequestIdReference { |
| 141 | + const requestId = query.get(LINK_REQUEST_ID_PARAM); |
| 142 | + const eventTypeParam = query.get(LINK_EVENT_TYPE_PARAM); |
| 143 | + if (!eventTypeParam) { |
| 144 | + throw new TypeError(`Missing eventType parameter`); |
| 145 | + } |
| 146 | + const eventType = EventType[normalizeEnumValue(eventTypeParam, 'EVENT_TYPE') as keyof typeof EventType]; |
| 147 | + if (eventType == null) { |
| 148 | + throw new TypeError(`Unknown eventType parameter: ${eventTypeParam}`); |
| 149 | + } |
| 150 | + return { requestId, eventType }; |
| 151 | +} |
0 commit comments