From d854af4ce78d9a9a6ab865001e0d2f4a0e4722c9 Mon Sep 17 00:00:00 2001 From: PierpaoloIannone Date: Wed, 15 Feb 2023 11:34:58 +0100 Subject: [PATCH] feat: add content-by-id methods --- .../src/services/Channel/index.ts | 56 +++++++++++++++++-- .../src/services/Channel/interfaces/index.ts | 13 ++++- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/packages/contentchef-node/src/services/Channel/index.ts b/packages/contentchef-node/src/services/Channel/index.ts index 3ea152c..0105d71 100644 --- a/packages/contentchef-node/src/services/Channel/index.ts +++ b/packages/contentchef-node/src/services/Channel/index.ts @@ -49,6 +49,19 @@ export function createOnlineContentRequest(spaceId: string, channel: string, con }; } +/** + * @param {string} spaceId + * @param {ISDKConfiguration} config + */ +export function createOnlineContentByIdRequest(spaceId: string, config: IChannelConfiguration) { + const url = getOnlineEndpoint(spaceId, 'content-by-id'); + + return async (params: interfaces.GetContentByIdOnlineConfig): Promise>> => { + const searchParams: URLSearchParams = createGetContentByIdRequestURLSearchParams(params, undefined); + return executeFetchRequest(config, url, searchParams); + }; +} + /** * @param {string} spaceId * @param {string} channel @@ -67,6 +80,23 @@ export function createPreviewContentRequest(spaceId: string, channel: string, st }; } +/** + * @param {string} spaceId + * @param {PublishingStatus} state + * @param {ITargetDateResolver} targetDateResolver + * @param {ISDKConfiguration} config + */ +export function createPreviewContentByIdRequest(spaceId: string, state: PublishingStatus, config: IChannelConfiguration, targetDateResolver: ITargetDateResolver) { + const url = getPreviewEndpoint(spaceId, 'content-by-id', state); + + return async (params: interfaces.GetContentByIdPreviewConfig): Promise>> => { + const targetDate = await targetDateResolver.getTargetDate(); + + const searchParams: URLSearchParams = createGetContentByIdRequestURLSearchParams(params, targetDate); + return executeFetchRequest(config, url, searchParams); + }; +} + /** * @param {string} spaceId * @param {string} channel @@ -135,6 +165,20 @@ function createSearchRequestURLSearchParams(params: interfaces.SearchPreviewConf return createdParams; } +function createGetContentByIdRequestURLSearchParams(params: interfaces.GetContentByIdPreviewConfig | interfaces.GetContentByIdOnlineConfig , targetDate?: string) { + + const { legacyMetadata, publishedContentId } = params; + + const createdParams = new URLSearchParams({ + }); + + maybeAddToURLSearchParams(createdParams, 'legacyMetadata', '' + legacyMetadata); + maybeAddToURLSearchParams(createdParams, 'publishedContentId', publishedContentId.toString()); + maybeAddToURLSearchParams(createdParams, 'targetDate', targetDate); + + return createdParams; +} + function createGetContentRequestURLSearchParams(params: interfaces.GetContentPreviewConfig | interfaces.GetContentOnlineConfig , targetDate?: string) { const { legacyMetadata, publicId } = params; @@ -201,11 +245,13 @@ export function getPreviewChannelMethods( } const content = createPreviewContentRequest(spaceId, channel, state, config, targetDateResolver); + const contentById = createPreviewContentByIdRequest(spaceId, state, config, targetDateResolver); const search = createPreviewSearchRequest(spaceId, channel, state, config, targetDateResolver); const localizedContent = createPreviewContentRequest(spaceId, channel, state, config, targetDateResolver, config.locale); const localizedSearch = createPreviewSearchRequest(spaceId, channel, state, config, targetDateResolver, config.locale); return { content, + contentById, localizedContent, localizedSearch, search, @@ -236,12 +282,14 @@ export function getOnlineChannelMethods(spaceId: string, channel: string, config } const content = createOnlineContentRequest(spaceId, channel, config); + const contentById = createOnlineContentByIdRequest(spaceId, config); const search = createOnlineSearchRequest(spaceId, channel, config); const localizedContent = createOnlineContentRequest(spaceId, channel, config, config.locale); const localizedSearch = createOnlineSearchRequest(spaceId, channel, config, config.locale); return { content, + contentById, localizedContent, localizedSearch, search, @@ -254,8 +302,8 @@ export function getOnlineChannelMethods(spaceId: string, channel: string, config * @param {string} channel * @returns */ -export function getOnlineEndpoint(spaceId: string, method: interfaces.ContentRequestMethod, channel: string, locale?: string) { - return `/space/${spaceId}/online/${method}/${channel}${locale ? `/${locale}` : ''}`; +export function getOnlineEndpoint(spaceId: string, method: interfaces.ContentRequestMethod, channel?: string, locale?: string) { + return `/space/${spaceId}/online/${method}${channel ? `/${channel}` : ''}${locale ? `/${locale}` : ''}`; } /** @@ -265,8 +313,8 @@ export function getOnlineEndpoint(spaceId: string, method: interfaces.ContentReq * @param {string} channel * @returns */ -export function getPreviewEndpoint(spaceId: string, method: interfaces.ContentRequestMethod, state: PublishingStatus, channel: string, locale?: string) { - return `/space/${spaceId}/preview/${state}/${method}/${channel}${locale ? `/${locale}` : ''}`; +export function getPreviewEndpoint(spaceId: string, method: interfaces.ContentRequestMethod, state: PublishingStatus, channel?: string, locale?: string) { + return `/space/${spaceId}/preview/${state}/${method}${channel ? `/${channel}` : ''}${locale ? `/${locale}` : ''}`; } export * from './interfaces'; diff --git a/packages/contentchef-node/src/services/Channel/interfaces/index.ts b/packages/contentchef-node/src/services/Channel/interfaces/index.ts index 5104bd2..aea5b23 100644 --- a/packages/contentchef-node/src/services/Channel/interfaces/index.ts +++ b/packages/contentchef-node/src/services/Channel/interfaces/index.ts @@ -1,6 +1,6 @@ import { PublishingStatus } from '../index'; -export type ContentRequestMethod = 'content' | `search/v2`; +export type ContentRequestMethod = 'content' | 'content-by-id' | `search/v2`; export type ContentState = 'staging' | 'live'; @@ -12,6 +12,11 @@ export interface IGetContentConfig { publicId: string; } +export interface IGetContentByIdConfig { + legacyMetadata?: boolean; + publishedContentId: number; +} + export interface IRequestContext { publishingChannel: string; targetDate: Date; @@ -21,6 +26,8 @@ export interface IRequestContext { export type GetContentOnlineConfig = IGetContentConfig; export type GetContentPreviewConfig = IGetContentConfig; +export type GetContentByIdOnlineConfig = IGetContentByIdConfig; +export type GetContentByIdPreviewConfig = IGetContentByIdConfig; export type SearchOnlineConfig = ISearchConfig; export type SearchPreviewConfig = ISearchConfig; @@ -34,9 +41,13 @@ export interface IMethodResponse { } export interface IGetContentResponse extends IResponse { } +export type IGetContentByIdResponse = Omit, 'requestContext'> & { + requestContext: Omit & {publishingChannel?: IRequestContext['publishingChannel']}, +}; export interface IChannelMethods { content(params: IGetContentConfig): Promise>>; + contentById(params: IGetContentByIdConfig): Promise>>; search(params: ISearchConfig): Promise>>>; localizedContent(params: IGetContentConfig): Promise>>; localizedSearch(params: ISearchConfig): Promise>>>;