@foxpage/foxpage-manager
Version:
foxpage resource manager
384 lines (383 loc) • 12.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.foxpageDataService = exports.getFoxpageDataService = exports.createFoxpageDataService = exports.FoxpageDataService = void 0;
const request_1 = require("./request");
let foxpageDataService;
exports.foxpageDataService = foxpageDataService;
/**
* foxpage data service
*
* @export
* @class FoxpageDataService
*/
class FoxpageDataService {
constructor(opt) {
this.request = (0, request_1.createRequest)(opt);
this.option = opt;
}
/**
* update request instance
* @param opt
* @param cache if cache the option
*/
update(data = {}, cache = false) {
const _opt = Object.assign(Object.assign({}, this.option), data);
this.request = (0, request_1.createRequest)(_opt);
if (cache) {
this.option = _opt;
}
}
/**
* fetch app details via appIds
*
* @param {string[]} appIds
* @return {*}
*/
async fetchApps(appIds) {
return ((await this.request('post', '/applications/list', { applicationIds: appIds }, { throwError: true })) ||
[]);
}
/**
* fetch changes
*
* @param {string} appId
* @param {number} timestamp
* @return {*} {Promise<{
contents: ResourceUpdateInfo;
timestamp: number;
}>}
*/
async fetchContentChanges(appId, timestamp) {
return (await this.request('get', '/contents/changes', { applicationId: appId, timestamp }, { throwError: true }));
}
async fetchPackageLives(appId, type, opt) {
return await this.request('post', '/packages/live-versions', {
applicationId: appId,
componentIds: opt.packageIds,
loadOnIgnite: opt.strategy === 'loadOnIgnite' ? true : undefined,
semver: opt.semver,
type,
});
}
async fetchPackageInfos(appId, type, opt) {
return await this.request('post', '/packages/version-infos', {
applicationId: appId,
nameVersions: opt.nameVersions,
isCanary: opt.isCanary,
semver: opt.semver,
type,
});
}
/**
* fetch application live package
*
* @param {string} appId
* @param {{ packageIds: string[] }} packageIds
*/
async fetchPackages(appId, opt) {
return ((await this.fetchPackageLives(appId, ['component'], opt)) || []);
}
/**
* fetch packages by name and version
*
* @param {string} appId application id
* @param {{ nameVersions: PackageNamedVersion[] }} { nameVersions }
* @return {Promise<FPPackageResponse[]>}
*/
async fetchPackagesByNamedVersions(appId, opt) {
return ((await this.fetchPackageInfos(appId, ['component'], opt)) || []);
}
/**
* fetch application live plugin
*
* @param {string} appId
* @param {{ packageIds: string[] }} packageIds
* @return {Promise<FPPackage[]>}
*/
async fetchPlugins(appId, opt) {
return ((await this.fetchPackageLives(appId, ['plugin'], opt)) || []);
}
/**
* fetch plugins by name and version
*
* @param {string} appId application id
* @param {{ nameVersions: PackageNamedVersion[] }} { nameVersions }
* @return {Promise<FPPackageResponse[]>}
*/
async fetchPluginsByNamedVersions(appId, opt) {
return ((await this.fetchPackageInfos(appId, ['plugin'], opt)) || []);
}
/**
* fetch application live library
*
* @param {string} appId
* @param {{ packageIds: string[] }} libraryIds
* @return {Promise<FPPackage[]>}
*/
async fetchLibraries(appId, opt) {
return ((await this.fetchPackageLives(appId, ['library'], opt)) || []);
}
/**
* fetch libraries by name and version
*
* @param {string} appId application id
* @param {{ nameVersions: PackageNamedVersion[] }} { nameVersions }
* @return {Promise<FPPackageResponse[]>}
*/
async fetchLibrariesByNamedVersions(appId, opt) {
return ((await this.fetchPackageInfos(appId, ['library'], opt)) || []);
}
/**
* fetch application pages
*
* @param {string} appId
* @param {{ pageIds: string[] }} { page content ids }
* @return {Promise<Page[]>}
*/
async fetchPages(appId, { pageIds }) {
return ((await this.request('post', '/pages/lives', { applicationId: appId, ids: pageIds })) || []);
}
/**
* fetch application templates
*
* @param {string} appId
* @param {{ templateIds: string[] }} { template content ids }
* @return {Promise<Template[]>}
*/
async fetchTemplates(appId, { templateIds }) {
return ((await this.request('post', '/templates/lives', { applicationId: appId, ids: templateIds })) ||
[]);
}
/**
* fetch application blocks
*
* @param {string} appId
* @param {{ blockIds: string[] }} { template content ids }
* @return {Promise<Block[]>}
*/
async fetchBlocks(appId, { blockIds }) {
return ((await this.request('post', '/blocks/lives', { applicationId: appId, ids: blockIds })) || []);
}
/**
* fetch application content via tags
*
* @param {string} appId
* @param {Tag[]} tags tags
* @return {*} {(Promise<{
content: Content;
contentInfo: Relations;
}>)}
*/
async fetchContentInfoByTags(appId, fileId, pathname, tags) {
const result = (await this.request('post', '/content/tag-versions', {
applicationId: appId,
fileId,
pathname,
tags,
}));
return result[0];
}
/**
* fetch content by tag
*
* @param {string} appId
* @param {string} pathname
* @param {Tag[]} tags
* @return {*} {Promise<Content>}
*/
async fetchContentByTags(appId, fileId, pathname, tags) {
var _a;
const result = (await this.request('post', '/content/tag-contents', {
applicationId: appId,
fileId,
pathname,
tags,
}));
return (_a = result[0]) === null || _a === void 0 ? void 0 : _a.content;
}
/**
* fetch application content
* contain contentId and tags
* @param {string} appId
* @param {string[]} contentIds
* @return {*} {Promise<Content[]>}
*/
async fetchContents(appId, { contentIds }) {
return ((await this.request('post', '/contents', { applicationId: appId, contentIds })) || []);
}
/**
* fetch application functions
*
* @param {string} appId
* @param {{ functionIds: string[] }} { function content ids }
* @return {*} {Promise<FPFunction[]>}
*/
async fetchFunctions(appId, { functionIds }) {
return ((await this.request('post', '/functions/lives', { applicationId: appId, ids: functionIds })) ||
[]);
}
/**
* fetch application conditions
*
* @param {string} appId
* @param {{ conditionIds: string[] }} { condition content ids }
* @return {*} {Promise<Condition[]>}
*/
async fetchConditions(appId, { conditionIds }) {
return ((await this.request('post', '/conditions/lives', { applicationId: appId, ids: conditionIds })) ||
[]);
}
/**
* fetch application variables
*
* @param {string} appId
* @param {{ variableIds: string[] }} { variable content ids }
* @return {*} {(Promise<Variable[]>)}
*/
async fetchVariables(appId, { variableIds }) {
return ((await this.request('post', '/variables/lives', { applicationId: appId, ids: variableIds })) ||
[]);
}
/**
* fetch application materials
*
* @param {string} appId
* @param {{ materialIds: string[] }} { material content ids }
* @return {*} {(Promise<Variable[]>)}
*/
async fetchMaterials(appId, { items, materialIds }) {
return ((await this.request('post', '/materials/lives', { applicationId: appId, items, materialIds })) ||
[]);
}
/**
* fetch application mocks
*
* @param {string} appId
* @param {{ mockIds: string[] }} { mock content ids }
* @return {*} {(Promise<ContentRelationInfo[]>)}
*/
async fetchMocks(appId, { mockIds }) {
return ((await this.request('post', '/mocks/lives', { applicationId: appId, ids: mockIds })) ||
[]);
}
/**
* fetch application draft mocks
* @param appId
* @param params
* @returns
*/
async fetchDraftMocks(appId, { mockIds }) {
return ((await this.request('post', '/mocks/draft-infos', { applicationId: appId, ids: mockIds })) ||
[]);
}
/**
* fetch content & relations info
*
* @param {string} appId
* @param {{ contentIds: string[] }} { contentIds }
* @return {*}
*/
async fetchPageInfos(appId, { contentIds }) {
return ((await this.request('post', '/pages/live-infos', { applicationId: appId, ids: contentIds })) ||
[]);
}
/**
* fetch content & relations info
*
* @param {string} appId
* @param {{ contentIds: string[] }} { contentIds }
* @return {*}
*/
async fetchBlockRelationInfos(appId, { contentIds }) {
return ((await this.request('post', '/blocks/live-infos', { applicationId: appId, ids: contentIds })) ||
[]);
}
/**
* fetch content & relation info in draft status
*
* @param {string} appId
* @param {{ contentIds: string[] }} { contentIds }
* @return {*}
*/
async fetchDraftPageInfos(appId, { contentIds, locale }) {
return ((await this.request('post', '/pages/draft-infos', { applicationId: appId, ids: contentIds, locale })) ||
[]);
}
/**
* fetch preview version content & relation infos
* @param appId
* @param param
* @returns
*/
async fetchPreviewPageInfos(appId, { contentId, version, locale }) {
return ((await this.request('post', '/pages/version-infos', {
applicationId: appId,
id: contentId,
version: version,
locale,
})) || []);
}
/**
* fetch block content & relation info in draft status
*
* @param {string} appId
* @param {{ contentIds: string[] }} { contentIds }
* @return {*}
*/
async fetchDraftBlockRelationInfos(appId, { contentIds }) {
return ((await this.request('post', '/blocks/draft-infos', { applicationId: appId, ids: contentIds })) ||
[]);
}
/**
* fetch preview version block content & relation infos
* @param appId
* @param param
* @returns
*/
async fetchPreviewBlockRelationInfos(appId, { contentId, version }) {
return ((await this.request('post', '/blocks/version-infos', {
applicationId: appId,
id: contentId,
version: version,
})) || []);
}
/**
* fetch files
*
* @param {string} appId
* @param {{ fileIds: string[] }} { fileIds }
* @return {*}
*/
async fetchFiles(appId, { fileIds }) {
return ((await this.request('post', '/files', { applicationId: appId, ids: fileIds })) || []);
}
/**
* ticket check
* @param appId
* @param ticket
* @returns boolean
*/
async ticketCheck(ticket, data) {
return (await this.request('post', '/contents/encrypt-validate', { token: ticket, data }));
}
}
exports.FoxpageDataService = FoxpageDataService;
/**
* create data service
* @param opt data service options
*/
const createFoxpageDataService = (opt) => {
if (!foxpageDataService) {
exports.foxpageDataService = foxpageDataService = new FoxpageDataService(opt);
}
return foxpageDataService;
};
exports.createFoxpageDataService = createFoxpageDataService;
/**
* get foxpage dataService instance
* @returns {FoxpageDataService}
*/
const getFoxpageDataService = () => {
return foxpageDataService;
};
exports.getFoxpageDataService = getFoxpageDataService;