Skip to content

Commit 21338bb

Browse files
adding multistatus response
1 parent bfd7efc commit 21338bb

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

packages/destination-actions/src/destinations/taguchi/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const destination: DestinationDefinition<Settings> = {
2929
required: true
3030
}
3131
},
32-
testAuthentication: (request) => {
32+
testAuthentication: (request, {settings}) => {
3333
// Return a request that tests/validates the user's credentials.
3434
// If you do not have a way to validate the authentication fields safely,
3535
// you can remove the `testAuthentication` function, though discouraged.

packages/destination-actions/src/destinations/taguchi/syncAudience/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ const action: ActionDefinition<Settings, Payload> = {
154154
default: { '@path': '$.timestamp' }
155155
}
156156
},
157-
perform: (request, {payload, settings}) => {
158-
send(request, [payload], settings)
157+
perform: async (request, {payload, settings}) => {
158+
await send(request, [payload], settings, false)
159159
},
160-
performBatch: (request, { payload, settings }) => {
161-
send(request, payload, settings)
160+
performBatch: async (request, { payload, settings }) => {
161+
await send(request, payload, settings, true)
162162
}
163163
}
164164

packages/destination-actions/src/destinations/taguchi/syncAudience/utils.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Payload } from './generated-types'
22
import { Settings } from '../generated-types'
3-
import { RequestClient, PayloadValidationError } from '@segment/actions-core'
3+
import { RequestClient, PayloadValidationError, MultiStatusResponse, JSONLikeObject } from '@segment/actions-core'
44
import { JSON, JSONItem, ResponseJSON } from './types'
55

66
export function validate(payloads: Payload[]): Payload[]{
@@ -13,8 +13,10 @@ export function validate(payloads: Payload[]): Payload[]{
1313
return payloads
1414
}
1515

16-
export async function send(request: RequestClient, payloads: Payload[], settings: Settings) {
16+
export async function send(request: RequestClient, payloads: Payload[], settings: Settings, isBatch: boolean) {
17+
1718
validate(payloads)
19+
1820
const json: JSON = payloads.map(payload => buildJSON(payload, settings.organizationId))
1921

2022
const response = await request<ResponseJSON>(`${settings.integrationURL}/subscriber`, {
@@ -25,6 +27,29 @@ export async function send(request: RequestClient, payloads: Payload[], settings
2527
'Authorization': `Bearer ${settings.apiKey}`
2628
}
2729
})
30+
31+
if(isBatch){
32+
const multiStatusResponse = new MultiStatusResponse()
33+
response.data.forEach((res, index) => {
34+
if (res.code >= 200 && res.code < 300) {
35+
multiStatusResponse.setSuccessResponseAtIndex(index, {
36+
status: res.code,
37+
sent: json[index] as unknown as JSONLikeObject,
38+
body: res as unknown as JSONLikeObject
39+
});
40+
} else {
41+
multiStatusResponse.setErrorResponseAtIndex(index, {
42+
status: res.code,
43+
sent: json[index] as unknown as JSONLikeObject,
44+
body: res as unknown as JSONLikeObject,
45+
errormessage: res.description
46+
})
47+
}
48+
})
49+
return multiStatusResponse
50+
}
51+
52+
return response
2853
}
2954

3055
function buildJSON(payload: Payload, organizationId: string): JSONItem {

0 commit comments

Comments
 (0)