|
1 | 1 | import dayjs from '../../lib/dayjs' |
2 | 2 | import isPlainObject from 'lodash/isPlainObject' |
3 | 3 | import { fullFormats } from 'ajv-formats/dist/formats' |
| 4 | +import { ErrorCodes, MultiStatusResponse, type RequestClient } from '@segment/actions-core' |
4 | 5 | import { CUSTOMERIO_TRACK_API_VERSION } from './versioning-info' |
5 | 6 |
|
6 | 7 | const isEmail = (value: string): boolean => { |
@@ -196,23 +197,144 @@ export const resolveIdentifiers = ({ |
196 | 197 | } |
197 | 198 | } |
198 | 199 |
|
199 | | -export const sendBatch = <Payload extends BasePayload>(request: Function, options: RequestPayload<Payload>[]) => { |
| 200 | +export const sendBatch = async <Payload extends BasePayload>( |
| 201 | + request: RequestClient, |
| 202 | + options: RequestPayload<Payload>[] |
| 203 | +) => { |
200 | 204 | if (!options?.length) { |
201 | 205 | return |
202 | 206 | } |
203 | 207 |
|
204 | 208 | const [{ settings }] = options |
205 | 209 | const batch = options.map((opts) => buildPayload(opts)) |
206 | 210 |
|
207 | | - return request(`${trackApiEndpoint(settings)}/api/${CUSTOMERIO_TRACK_API_VERSION}/batch`, { |
| 211 | + const response = await request(`${trackApiEndpoint(settings)}/api/${CUSTOMERIO_TRACK_API_VERSION}/batch`, { |
208 | 212 | method: 'post', |
209 | 213 | json: { |
210 | 214 | batch |
211 | 215 | } |
212 | 216 | }) |
| 217 | + |
| 218 | + const responseBody = getResponseBody(response) |
| 219 | + |
| 220 | + if (response?.status === 207 && responseBody) { |
| 221 | + const parsedResults = parseTrackApiMultiStatusResponse(responseBody, batch.length) |
| 222 | + if (parsedResults) { |
| 223 | + return parsedResults |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + if (response?.status === 200 && responseBody) { |
| 228 | + const parsedResults = parseTrackApiMultiStatusResponse(responseBody, batch.length) |
| 229 | + if (parsedResults) { |
| 230 | + return parsedResults |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + return response |
| 235 | +} |
| 236 | + |
| 237 | +interface TrackApiError { |
| 238 | + batch_index?: number |
| 239 | + reason?: string |
| 240 | + field?: string |
| 241 | + message?: string |
| 242 | +} |
| 243 | + |
| 244 | +interface TrackApiResponse { |
| 245 | + errors?: TrackApiError[] |
| 246 | +} |
| 247 | + |
| 248 | +interface RequestResponse { |
| 249 | + status?: number |
| 250 | + data?: unknown |
| 251 | + content?: unknown |
| 252 | + body?: unknown |
| 253 | +} |
| 254 | + |
| 255 | +function mapTrackApiReasonToErrorCode(reason: string | undefined) { |
| 256 | + if (!reason) { |
| 257 | + return undefined |
| 258 | + } |
| 259 | + |
| 260 | + switch (reason.toLowerCase()) { |
| 261 | + case 'invalid': |
| 262 | + case 'required': |
| 263 | + return ErrorCodes.PAYLOAD_VALIDATION_FAILED |
| 264 | + default: |
| 265 | + return undefined |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +function getResponseBody(response: RequestResponse): unknown { |
| 270 | + const body = response.data ?? response.content ?? response.body |
| 271 | + |
| 272 | + if (typeof body !== 'string') { |
| 273 | + return body |
| 274 | + } |
| 275 | + |
| 276 | + try { |
| 277 | + return JSON.parse(body) |
| 278 | + } catch { |
| 279 | + try { |
| 280 | + const decoded = Buffer.from(body, 'base64').toString('utf-8') |
| 281 | + return JSON.parse(decoded) |
| 282 | + } catch { |
| 283 | + return body |
| 284 | + } |
| 285 | + } |
| 286 | +} |
| 287 | + |
| 288 | +export function parseTrackApiErrors(errors: TrackApiError[], totalItems: number): MultiStatusResponse { |
| 289 | + const multiStatusResponse = new MultiStatusResponse() |
| 290 | + const errorMap = new Map<number, TrackApiError>() |
| 291 | + |
| 292 | + for (const error of errors) { |
| 293 | + if (typeof error.batch_index === 'number') { |
| 294 | + errorMap.set(error.batch_index, error) |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + for (let i = 0; i < totalItems; i++) { |
| 299 | + const error = errorMap.get(i) |
| 300 | + |
| 301 | + if (!error) { |
| 302 | + multiStatusResponse.setSuccessResponseAtIndex(i, { |
| 303 | + status: 200, |
| 304 | + body: {}, |
| 305 | + sent: {} |
| 306 | + }) |
| 307 | + continue |
| 308 | + } |
| 309 | + |
| 310 | + multiStatusResponse.setErrorResponseAtIndex(i, { |
| 311 | + status: 400, |
| 312 | + errormessage: error.message || `${error.reason || 'ERROR'}: ${error.field || 'unknown field'}`, |
| 313 | + errortype: mapTrackApiReasonToErrorCode(error.reason), |
| 314 | + body: error |
| 315 | + }) |
| 316 | + } |
| 317 | + |
| 318 | + return multiStatusResponse |
| 319 | +} |
| 320 | + |
| 321 | +export function parseTrackApiMultiStatusResponse( |
| 322 | + responseBody: unknown, |
| 323 | + totalItems: number |
| 324 | +): MultiStatusResponse | null { |
| 325 | + if (!isRecord(responseBody)) { |
| 326 | + return null |
| 327 | + } |
| 328 | + |
| 329 | + const { errors } = responseBody as TrackApiResponse |
| 330 | + if (!Array.isArray(errors) || errors.length === 0) { |
| 331 | + return null |
| 332 | + } |
| 333 | + |
| 334 | + return parseTrackApiErrors(errors, totalItems) |
213 | 335 | } |
214 | 336 |
|
215 | | -export const sendSingle = <Payload extends BasePayload>(request: Function, options: RequestPayload<Payload>) => { |
| 337 | +export const sendSingle = <Payload extends BasePayload>(request: RequestClient, options: RequestPayload<Payload>) => { |
216 | 338 | const json = buildPayload(options) |
217 | 339 | return request(`${trackApiEndpoint(options.settings)}/api/${CUSTOMERIO_TRACK_API_VERSION}/entity`, { |
218 | 340 | method: 'post', |
|
0 commit comments