|
| 1 | +/** |
| 2 | + * Test example demonstrating context passing from performBatch to poll methods |
| 3 | + * using stateContext pattern inspired by Braze cohorts destination |
| 4 | + */ |
| 5 | + |
| 6 | +import { ActionDefinition, ExecuteInput } from '../' |
| 7 | +import type { RequestClient } from '../../create-request-client' |
| 8 | + |
| 9 | +interface Settings { |
| 10 | + apiKey: string |
| 11 | + region: string |
| 12 | +} |
| 13 | + |
| 14 | +interface Payload { |
| 15 | + userId: string |
| 16 | + attributes: Record<string, unknown> |
| 17 | +} |
| 18 | + |
| 19 | +const testAction: ActionDefinition<Settings, Payload> = { |
| 20 | + title: 'Context Passing Test Action', |
| 21 | + description: 'Test action to verify context passing between performBatch and poll methods', |
| 22 | + fields: { |
| 23 | + userId: { |
| 24 | + label: 'User ID', |
| 25 | + description: 'Unique identifier for the user', |
| 26 | + type: 'string', |
| 27 | + required: true |
| 28 | + }, |
| 29 | + attributes: { |
| 30 | + label: 'User Attributes', |
| 31 | + description: 'Additional attributes for the user', |
| 32 | + type: 'object', |
| 33 | + required: false |
| 34 | + } |
| 35 | + }, |
| 36 | + |
| 37 | + async perform(request: RequestClient, data: ExecuteInput<Settings, Payload>) { |
| 38 | + // Single record operation (not async) |
| 39 | + const { payload, settings } = data |
| 40 | + |
| 41 | + const response = await request('https://api.example.com/single', { |
| 42 | + method: 'POST', |
| 43 | + json: { |
| 44 | + apiKey: settings.apiKey, |
| 45 | + region: settings.region, |
| 46 | + user: payload |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + return await response.json() |
| 51 | + }, |
| 52 | + |
| 53 | + async performBatch(request: RequestClient, data: ExecuteInput<Settings, Payload[]>) { |
| 54 | + const { payload, settings, stateContext } = data |
| 55 | + |
| 56 | + // Simulate making an API call that returns an operation ID |
| 57 | + const response = await request('https://api.example.com/batch', { |
| 58 | + method: 'POST', |
| 59 | + json: { |
| 60 | + apiKey: settings.apiKey, |
| 61 | + region: settings.region, |
| 62 | + users: payload |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + const responseData = await response.json() |
| 67 | + |
| 68 | + // Store additional context that poll method will need |
| 69 | + // This follows the pattern from Braze cohorts destination |
| 70 | + stateContext?.setResponseContext( |
| 71 | + 'batchMetadata', |
| 72 | + JSON.stringify({ |
| 73 | + submittedAt: new Date().toISOString(), |
| 74 | + totalRecords: payload.length, |
| 75 | + region: settings.region, |
| 76 | + requestId: responseData.requestId |
| 77 | + }), |
| 78 | + {} |
| 79 | + ) |
| 80 | + |
| 81 | + stateContext?.setResponseContext( |
| 82 | + 'apiCredentials', |
| 83 | + JSON.stringify({ |
| 84 | + apiKey: settings.apiKey, |
| 85 | + endpoint: 'https://api.example.com' |
| 86 | + }), |
| 87 | + {} |
| 88 | + ) |
| 89 | + |
| 90 | + return { |
| 91 | + operationId: responseData.operationId, |
| 92 | + status: 'pending' as const, |
| 93 | + message: `Batch operation initiated for ${payload.length} records` |
| 94 | + } |
| 95 | + }, |
| 96 | + |
| 97 | + async poll(request: RequestClient, data, context) { |
| 98 | + const { operationId, settings } = data |
| 99 | + const { stateContext } = context |
| 100 | + |
| 101 | + // Retrieve context set by performBatch method |
| 102 | + const batchMetadataStr = stateContext?.getRequestContext('batchMetadata') |
| 103 | + const apiCredentialsStr = stateContext?.getRequestContext('apiCredentials') |
| 104 | + |
| 105 | + const batchMetadata = batchMetadataStr && typeof batchMetadataStr === 'string' ? JSON.parse(batchMetadataStr) : null |
| 106 | + const apiCredentials = |
| 107 | + apiCredentialsStr && typeof apiCredentialsStr === 'string' ? JSON.parse(apiCredentialsStr) : null |
| 108 | + |
| 109 | + // Use the context data in the poll request |
| 110 | + const response = await request(`${apiCredentials?.endpoint || 'https://api.example.com'}/status/${operationId}`, { |
| 111 | + method: 'GET', |
| 112 | + headers: { |
| 113 | + Authorization: `Bearer ${apiCredentials?.apiKey || settings.apiKey}`, |
| 114 | + 'X-Request-ID': batchMetadata?.requestId || 'unknown' |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + const statusData = await response.json() |
| 119 | + |
| 120 | + return { |
| 121 | + status: (statusData.completed ? 'completed' : 'pending') as 'pending' | 'completed' | 'failed', |
| 122 | + message: batchMetadata |
| 123 | + ? `Operation for ${batchMetadata.totalRecords} records (submitted at ${batchMetadata.submittedAt}): ${statusData.message}` |
| 124 | + : statusData.message |
| 125 | + } |
| 126 | + }, |
| 127 | + |
| 128 | + async pollMultiple(request: RequestClient, data, context) { |
| 129 | + const { operationIds, settings } = data |
| 130 | + const { stateContext } = context |
| 131 | + |
| 132 | + // Retrieve context set by performBatch method |
| 133 | + const batchMetadataStr = stateContext?.getRequestContext('batchMetadata') |
| 134 | + const apiCredentialsStr = stateContext?.getRequestContext('apiCredentials') |
| 135 | + |
| 136 | + const batchMetadata = batchMetadataStr && typeof batchMetadataStr === 'string' ? JSON.parse(batchMetadataStr) : null |
| 137 | + const apiCredentials = |
| 138 | + apiCredentialsStr && typeof apiCredentialsStr === 'string' ? JSON.parse(apiCredentialsStr) : null |
| 139 | + |
| 140 | + // Poll multiple operations using context data |
| 141 | + const pollPromises = operationIds.map(async (operationId) => { |
| 142 | + const response = await request(`${apiCredentials?.endpoint || 'https://api.example.com'}/status/${operationId}`, { |
| 143 | + method: 'GET', |
| 144 | + headers: { |
| 145 | + Authorization: `Bearer ${apiCredentials?.apiKey || settings.apiKey}`, |
| 146 | + 'X-Request-ID': batchMetadata?.requestId || 'unknown' |
| 147 | + } |
| 148 | + }) |
| 149 | + |
| 150 | + const statusData = await response.json() |
| 151 | + |
| 152 | + return { |
| 153 | + status: (statusData.completed ? 'completed' : 'pending') as 'pending' | 'completed' | 'failed', |
| 154 | + message: batchMetadata |
| 155 | + ? `Operation ${operationId} for batch submitted at ${batchMetadata.submittedAt}: ${statusData.message}` |
| 156 | + : statusData.message |
| 157 | + } |
| 158 | + }) |
| 159 | + |
| 160 | + return Promise.all(pollPromises) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +export default testAction |
0 commit comments