From c9d30b4cc808fd2937c5bd72bc4ddfb48151efa2 Mon Sep 17 00:00:00 2001 From: hanoak20 <97229656+hanoak20@users.noreply.github.com> Date: Tue, 4 Jun 2024 18:12:06 +0530 Subject: [PATCH] Contentstack's destination enhancement. (#2062) * 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. --- .../src/destinations/contentstack/constants.ts | 3 --- .../contentstack/customAttributesSync/index.ts | 17 +++++++++++------ .../contentstack/customAttributesSync/utils.ts | 9 ++++----- .../contentstack/generated-types.ts | 8 ++++++++ .../src/destinations/contentstack/index.ts | 15 +++++++++++++-- 5 files changed, 36 insertions(+), 16 deletions(-) delete mode 100644 packages/destination-actions/src/destinations/contentstack/constants.ts diff --git a/packages/destination-actions/src/destinations/contentstack/constants.ts b/packages/destination-actions/src/destinations/contentstack/constants.ts deleted file mode 100644 index b7ab3a5f34..0000000000 --- a/packages/destination-actions/src/destinations/contentstack/constants.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const PERSONALIZE_API_BASE_URL = 'https://personalization-api.contentstack.com' -export const PERSONALIZE_EDGE_API_URL = 'https://personalization-edge.contentstack.com' -export const ACCESS_TOKEN_URL = 'https://developerhub-api.contentstack.com/apps/token' diff --git a/packages/destination-actions/src/destinations/contentstack/customAttributesSync/index.ts b/packages/destination-actions/src/destinations/contentstack/customAttributesSync/index.ts index bc7eada79b..89f2cf4c8f 100644 --- a/packages/destination-actions/src/destinations/contentstack/customAttributesSync/index.ts +++ b/packages/destination-actions/src/destinations/contentstack/customAttributesSync/index.ts @@ -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 = { title: 'Custom Attributes Sync', @@ -25,8 +24,8 @@ const action: ActionDefinition = { 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 ) @@ -35,14 +34,20 @@ const action: ActionDefinition = { ) 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: { diff --git a/packages/destination-actions/src/destinations/contentstack/customAttributesSync/utils.ts b/packages/destination-actions/src/destinations/contentstack/customAttributesSync/utils.ts index 08f273f3ff..4ede26329e 100644 --- a/packages/destination-actions/src/destinations/contentstack/customAttributesSync/utils.ts +++ b/packages/destination-actions/src/destinations/contentstack/customAttributesSync/utils.ts @@ -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, @@ -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' }) diff --git a/packages/destination-actions/src/destinations/contentstack/generated-types.ts b/packages/destination-actions/src/destinations/contentstack/generated-types.ts index 31ac2e6f20..97dcf1ad1f 100644 --- a/packages/destination-actions/src/destinations/contentstack/generated-types.ts +++ b/packages/destination-actions/src/destinations/contentstack/generated-types.ts @@ -9,4 +9,12 @@ export interface Settings { * Your Personalize project ID to which Segment's data should be synced. */ personalizeProjectId: string + /** + * Your region-based personalize API base URL. + */ + personalizeApiBaseUrl: string + /** + * Your region-based personalize-edge API base URL. + */ + personalizeEdgeApiBaseUrl: string } diff --git a/packages/destination-actions/src/destinations/contentstack/index.ts b/packages/destination-actions/src/destinations/contentstack/index.ts index 72ac74b19d..b81cb8c26d 100644 --- a/packages/destination-actions/src/destinations/contentstack/index.ts +++ b/packages/destination-actions/src/destinations/contentstack/index.ts @@ -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' @@ -23,10 +22,22 @@ const destination: DestinationDefinition = { 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(ACCESS_TOKEN_URL, { + const res = await request(auth.refreshTokenUrl || '', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded'