@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
78 lines • 3.28 kB
JavaScript
import commands from '../../commands.js';
import SpoCommand from '../../../base/SpoCommand.js';
import { globalOptionsZod } from '../../../../Command.js';
import { z } from 'zod';
import { zod } from '../../../../utils/zod.js';
import { validation } from '../../../../utils/validation.js';
import { urlUtil } from '../../../../utils/urlUtil.js';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
import { odata } from '../../../../utils/odata.js';
export const options = globalOptionsZod
.extend({
webUrl: zod.alias('u', z.string()
.refine(url => validation.isValidSharePointUrl(url) === true, url => ({
message: `'${url}' is not a valid SharePoint Online site URL.`
}))),
fileUrl: z.string().optional(),
fileId: zod.alias('i', z.string()
.refine(id => validation.isValidGuid(id), id => ({
message: `'${id}' is not a valid GUID.`
})).optional()),
label: z.string()
})
.strict();
class SpoFileVersionKeepCommand extends SpoCommand {
get name() {
return commands.FILE_VERSION_KEEP;
}
get description() {
return 'Ensure that a specific file version will never expire';
}
get schema() {
return options;
}
getRefinedSchema(schema) {
return schema
.refine(options => [options.fileUrl, options.fileId].filter(o => o !== undefined).length === 1, {
message: `Specify 'fileUrl' or 'fileId', but not both.`
});
}
async commandAction(logger, args) {
if (this.verbose) {
await logger.logToStderr(`Ensuring version '${args.options.label}' of file '${args.options.fileUrl || args.options.fileId}' at site '${args.options.webUrl}' will never expire...`);
}
try {
const baseApiUrl = this.getBaseApiUrl(args.options.webUrl, args.options.fileUrl, args.options.fileId);
const response = await odata.getAllItems(`${baseApiUrl}/versions?$filter=VersionLabel eq '${formatting.encodeQueryParameter(args.options.label)}'&$select=ID`);
if (response.length === 0) {
throw `Version with label '${args.options.label}' not found.`;
}
const requestExpirationOptions = {
url: `${baseApiUrl}/versions(${response[0].ID})/SetExpirationDate()`,
headers: {
accept: 'application/json;odata=nometadata',
'content-type': 'application/json'
},
responseType: 'json'
};
await request.post(requestExpirationOptions);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
getBaseApiUrl(webUrl, fileUrl, fileId) {
let requestUrl;
if (fileUrl) {
const serverRelUrl = urlUtil.getServerRelativePath(webUrl, fileUrl);
requestUrl = `${webUrl}/_api/web/GetFileByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter(serverRelUrl)}')`;
}
else {
requestUrl = `${webUrl}/_api/web/GetFileById('${fileId}')`;
}
return requestUrl;
}
}
export default new SpoFileVersionKeepCommand();
//# sourceMappingURL=file-version-keep.js.map