@progress/sitefinity-nextjs-sdk
Version:
Provides OOB widgets developed using the Next.js framework, which includes an abstraction layer for Sitefinity communication. Additionally, it offers an expanded API, typings, and tools for further development and integration.
76 lines (75 loc) • 3.02 kB
JavaScript
import { FilterConverterService } from '../rest-sdk/filters/filter-converter';
import { RestClient } from '../rest-sdk/rest-client';
import { ErrorCodeException } from '../rest-sdk/errors/error-code.exception';
export class RestClientForContext {
static async getItem(contentContext, externalArgs) {
return this.getItems(contentContext, {
...externalArgs,
type: externalArgs?.type,
traceContext: externalArgs?.traceContext
}).then((x) => {
if (x.Items.length === 0) {
const key = contentContext.ItemIdsOrdered?.length ? contentContext.ItemIdsOrdered[0] : null;
const errorMessage = key ? `The item with key '${key}' was not found` : 'The item was not found';
throw new ErrorCodeException('NotFound', errorMessage);
}
else {
return x.Items[0];
}
});
}
static async getItems(contentContext, externalArgs) {
if (!contentContext ||
!contentContext.Content ||
contentContext.Content.length === 0) {
return {
TotalCount: 0,
Items: []
};
}
const firstContent = contentContext.Content[0];
if (!firstContent || !firstContent.Variations || !firstContent.Variations[0]) {
return {
TotalCount: 0,
Items: []
};
}
const firstVariation = firstContent.Variations[0];
let args = {
...externalArgs,
type: firstContent.Type || externalArgs?.type,
provider: firstVariation.Source,
filter: FilterConverterService.getMainFilter(firstVariation),
traceContext: externalArgs?.traceContext
};
if (externalArgs?.orderBy && externalArgs?.orderBy.length > 0) {
args.orderBy = externalArgs.orderBy;
}
if (externalArgs?.culture) {
args.culture = externalArgs.culture;
}
const itemsForContext = await RestClient.getItems(args);
if (!externalArgs?.orderBy?.length && Array.isArray(contentContext.ItemIdsOrdered)
&& contentContext.ItemIdsOrdered.length > 0 && contentContext.ItemIdsOrdered.length === itemsForContext.Items.length) {
itemsForContext.Items = RestClientForContext.orderItemsManually(contentContext.ItemIdsOrdered, itemsForContext.Items);
}
return itemsForContext;
}
;
static orderItemsManually(orderedList, items) {
let orderedCollection = [];
orderedList.forEach((id) => {
const orderedItem = items.find((x) => x.Id === id);
if (orderedItem) {
orderedCollection.push(orderedItem);
}
});
if (orderedCollection.length === items.length) {
items = orderedCollection;
}
else {
orderedCollection = items;
}
return orderedCollection;
}
}