Skip to content

Commit

Permalink
Contentstack's destination enhancement. (#2062)
Browse files Browse the repository at this point in the history
* feat: added Contentstack's destination-action.

* chore: segregated the types of destination, and other improvements.

* fix: Action schema changed to oauth-managed.

* refactor: used 'extendRequest' function to add headers automatically.

* refactor: removed yarn PackageManager config.

* refactor: simplified the perform's data and its usage.

* chore: personalize urls are now configurable.
  • Loading branch information
hanoak20 authored Jun 4, 2024
1 parent e99e29f commit c9d30b4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { createCustomAttrbute, fetchAllAttributes } from './utils'
import { PersonalizeAttributes } from './types'
import { PERSONALIZE_EDGE_API_URL } from '../constants'

const action: ActionDefinition<Settings, Payload> = {
title: 'Custom Attributes Sync',
Expand All @@ -25,8 +24,8 @@ const action: ActionDefinition<Settings, Payload> = {
required: false
}
},
perform: async (request, { payload }) => {
const personalizeAttributesData = (await fetchAllAttributes(request)).map(
perform: async (request, { payload, settings }) => {
const personalizeAttributesData = (await fetchAllAttributes(request, settings.personalizeApiBaseUrl)).map(
(attribute: PersonalizeAttributes) => attribute?.key
)

Expand All @@ -35,14 +34,20 @@ const action: ActionDefinition<Settings, Payload> = {
)

if (attributesToCreate?.length) {
const firstAttributeRes = await createCustomAttrbute(request, attributesToCreate[0])
const firstAttributeRes = await createCustomAttrbute(
request,
attributesToCreate[0],
settings.personalizeApiBaseUrl
)
if (firstAttributeRes.status === 401) return firstAttributeRes

const otherAttributes = attributesToCreate.slice(1)

await Promise.allSettled(otherAttributes.map((trait: string) => createCustomAttrbute(request, trait)))
await Promise.allSettled(
otherAttributes.map((trait: string) => createCustomAttrbute(request, trait, settings.personalizeApiBaseUrl))
)

return request(`${PERSONALIZE_EDGE_API_URL}/user-attributes`, {
return request(`${settings.personalizeEdgeApiBaseUrl}/user-attributes`, {
method: 'patch',
json: payload.traits,
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { RequestClient } from '@segment/actions-core'
import { PersonalizeAttributes } from './types'
import { PERSONALIZE_API_BASE_URL } from '../constants'

export const createCustomAttrbute = async (request: RequestClient, name: string) =>
request(`${PERSONALIZE_API_BASE_URL}/attributes`, {
export const createCustomAttrbute = async (request: RequestClient, name: string, url: string) =>
request(`${url}/attributes`, {
method: 'post',
json: {
name,
Expand All @@ -12,8 +11,8 @@ export const createCustomAttrbute = async (request: RequestClient, name: string)
}
})

export const fetchAllAttributes = async (request: RequestClient) => {
const res = await request(`${PERSONALIZE_API_BASE_URL}/attributes`, {
export const fetchAllAttributes = async (request: RequestClient, url: string) => {
const res = await request(`${url}/attributes`, {
method: 'get'
})

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { DestinationDefinition } from '@segment/actions-core'
import type { Settings } from './generated-types'
import { ACCESS_TOKEN_URL } from './constants'
import customAttributesSync from './customAttributesSync'
import { RefreshTokenResponse } from './types'

Expand All @@ -23,10 +22,22 @@ const destination: DestinationDefinition<Settings> = {
type: 'string',
required: true,
description: "Your Personalize project ID to which Segment's data should be synced."
},
personalizeApiBaseUrl: {
label: 'Personalize API base URL',
type: 'string',
required: true,
description: 'Your region-based personalize API base URL.'
},
personalizeEdgeApiBaseUrl: {
label: 'Personalize Edge API base URL',
type: 'string',
required: true,
description: 'Your region-based personalize-edge API base URL.'
}
},
refreshAccessToken: async (request, { auth }) => {
const res = await request<RefreshTokenResponse>(ACCESS_TOKEN_URL, {
const res = await request<RefreshTokenResponse>(auth.refreshTokenUrl || '', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
Expand Down

0 comments on commit c9d30b4

Please sign in to comment.