Skip to content

Commit

Permalink
Update dynamic field types to support dynamic array of objects (segme…
Browse files Browse the repository at this point in the history
…ntio#2168)

* update dynamic field types to support dynamic array of objects

* more type fix

* minor
  • Loading branch information
pooyaj authored Jul 11, 2024
1 parent 15b4e43 commit f6748a9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
12 changes: 11 additions & 1 deletion packages/core/src/__tests__/destination-kit.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ActionDefinition } from '../destination-kit/action'
import {
StateContext,
Destination,
Expand Down Expand Up @@ -172,6 +173,15 @@ const destinationWithIdentifier: DestinationDefinition<JSONObject> = {
}
}

interface Payload {
testDynamicField: string
testUnstructuredObject: Record<string, string>
testStructuredObject: {
testDynamicSubfield: string
}
testObjectArrays: Array<{ testDynamicSubfield: string }>
}

const destinationWithDynamicFields: DestinationDefinition<JSONObject> = {
name: 'Actions Dynamic Fields',
mode: 'cloud',
Expand Down Expand Up @@ -271,7 +281,7 @@ const destinationWithDynamicFields: DestinationDefinition<JSONObject> = {
perform: (_request, { syncMode }) => {
return ['this is a test', syncMode]
}
}
} as ActionDefinition<JSONObject, Payload>
}
}

Expand Down
22 changes: 18 additions & 4 deletions packages/core/src/destination-kit/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type GenericActionHookBundle = {
}
}

// Utility type to check if T is an array
type IsArray<T> = T extends (infer U)[] ? U : never

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface ActionDefinition<
Settings,
Expand All @@ -82,16 +85,27 @@ export interface ActionDefinition<
* This is likely going to change as we productionalize the data model and definition object
*/
dynamicFields?: {
[K in keyof Payload]?: Payload[K] extends object
[K in keyof Payload]?: IsArray<Payload[K]> extends never
? Payload[K] extends object
? {
[ObjectProperty in keyof Payload[K] | '__keys__' | '__values__']?: RequestFn<
Settings,
Payload,
DynamicFieldResponse,
AudienceSettings
>
}
: RequestFn<Settings, Payload, DynamicFieldResponse, AudienceSettings>
: IsArray<Payload[K]> extends object
? {
[ObjectProperty in keyof Payload[K] | '__keys__' | '__values__']?: RequestFn<
[ObjectProperty in keyof IsArray<Payload[K]> | '__keys__' | '__values__']?: RequestFn<
Settings,
Payload[K],
Payload,
DynamicFieldResponse,
AudienceSettings
>
}
: RequestFn<Settings, Payload, DynamicFieldResponse, AudienceSettings>
: never
}

/** The operation to perform when this action is triggered */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { AudienceDestinationDefinition } from '@segment/actions-core'
import type { ActionDefinition, AudienceDestinationDefinition } from '@segment/actions-core'
import type { Settings, AudienceSettings } from './generated-types'

import send from '../webhook/send'
import { IntegrationError } from '@segment/actions-core'
import { createHmac } from 'crypto'
import { Payload } from './send.types'
const externalIdKey = 'externalId'
const audienceNameKey = 'audienceName'

Expand Down Expand Up @@ -157,7 +158,7 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
settings
})
}
}
} as ActionDefinition<Settings, Payload>
}
}

Expand Down

0 comments on commit f6748a9

Please sign in to comment.