Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions packages/contentchef-node/src/services/Channel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T extends object>(params: interfaces.GetContentByIdOnlineConfig): Promise<interfaces.IMethodResponse<interfaces.IGetContentResponse<T>>> => {
const searchParams: URLSearchParams = createGetContentByIdRequestURLSearchParams(params, undefined);
return executeFetchRequest(config, url, searchParams);
};
}

/**
* @param {string} spaceId
* @param {string} channel
Expand All @@ -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 <T extends object>(params: interfaces.GetContentByIdPreviewConfig): Promise<interfaces.IMethodResponse<interfaces.IGetContentByIdResponse<T>>> => {
const targetDate = await targetDateResolver.getTargetDate();

const searchParams: URLSearchParams = createGetContentByIdRequestURLSearchParams(params, targetDate);
return executeFetchRequest(config, url, searchParams);
};
}

/**
* @param {string} spaceId
* @param {string} channel
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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}` : ''}`;
}

/**
Expand All @@ -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';
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -12,6 +12,11 @@ export interface IGetContentConfig {
publicId: string;
}

export interface IGetContentByIdConfig {
legacyMetadata?: boolean;
publishedContentId: number;
}

export interface IRequestContext {
publishingChannel: string;
targetDate: Date;
Expand All @@ -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;
Expand All @@ -34,9 +41,13 @@ export interface IMethodResponse<T> {
}

export interface IGetContentResponse<T = any> extends IResponse<T> { }
export type IGetContentByIdResponse<T = any> = Omit<IResponse<T>, 'requestContext'> & {
requestContext: Omit<IRequestContext, 'publishingChannel'> & {publishingChannel?: IRequestContext['publishingChannel']},
};

export interface IChannelMethods {
content<T extends object>(params: IGetContentConfig): Promise<IMethodResponse<IGetContentResponse<T>>>;
contentById<T extends object>(params: IGetContentByIdConfig): Promise<IMethodResponse<IGetContentByIdResponse<T>>>;
search<T extends object>(params: ISearchConfig): Promise<IMethodResponse<IPaginatedResponse<IResponse<T>>>>;
localizedContent<T extends object>(params: IGetContentConfig): Promise<IMethodResponse<IGetContentResponse<T>>>;
localizedSearch<T extends object>(params: ISearchConfig): Promise<IMethodResponse<IPaginatedResponse<IResponse<T>>>>;
Expand Down