contentful-management
Version:
Client for Contentful's Content Management API
53 lines (50 loc) • 1.63 kB
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
import { freezeSys, toPlainObject } from 'contentful-sdk-core';
import copy from 'fast-copy';
import { wrapCollection } from '../common-utils';
import { pollAsyncActionStatus } from '../methods/action';
import enhanceWithMethods from '../enhance-with-methods';
/** The object returned by the Releases API */
/**
* @private
*/
function createReleaseActionApi(makeRequest) {
const getParams = self => {
const action = self.toPlainObject();
return {
spaceId: action.sys.space.sys.id,
environmentId: action.sys.environment.sys.id,
releaseId: action.sys.release.sys.id,
actionId: action.sys.id
};
};
return {
async get() {
const params = getParams(this);
return makeRequest({
entityType: 'ReleaseAction',
action: 'get',
params
}).then(releaseAction => wrapReleaseAction(makeRequest, releaseAction));
},
/** Waits for a Release Action to complete */
async waitProcessing(options) {
return pollAsyncActionStatus(async () => this.get(), options);
}
};
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw Release data
* @return Wrapped Release data
*/
export function wrapReleaseAction(makeRequest, data) {
const releaseAction = toPlainObject(copy(data));
const releaseActionWithApiMethods = enhanceWithMethods(releaseAction, createReleaseActionApi(makeRequest));
return freezeSys(releaseActionWithApiMethods);
}
/**
* @private
*/
export const wrapReleaseActionCollection = wrapCollection(wrapReleaseAction);