@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
103 lines • 3.76 kB
JavaScript
import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { validation } from '../../../../utils/validation.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
export const options = z.strictObject({
...globalOptionsZod.shape,
id: z.string().alias('i'),
description: z.string().optional().alias('d'),
owner: z.string(),
targetTypes: z.string().alias('t'),
properties: z.string().alias('p')
});
class GraphSchemaExtensionAddCommand extends GraphCommand {
get name() {
return commands.SCHEMAEXTENSION_ADD;
}
get description() {
return 'Creates a Microsoft Graph schema extension';
}
get schema() {
return options;
}
getRefinedSchema(schema) {
return schema
.refine(options => validation.isValidGuid(options.owner), {
error: e => `The specified owner '${e.input.owner}' is not a valid App Id`,
path: ['owner']
})
.refine(options => {
return this.validateProperties(options.properties) === true;
}, {
error: e => `${this.validateProperties(e.input.properties)}`,
path: ['properties']
});
}
async commandAction(logger, args) {
if (this.verbose) {
await logger.logToStderr(`Adding schema extension with id '${args.options.id}'...`);
}
const targetTypes = args.options.targetTypes.split(',').map(t => t.trim());
const properties = JSON.parse(args.options.properties);
const requestOptions = {
url: `${this.resource}/v1.0/schemaExtensions`,
headers: {
accept: 'application/json;odata.metadata=none',
'content-type': 'application/json'
},
data: {
id: args.options.id,
description: args.options.description,
owner: args.options.owner,
targetTypes,
properties
},
responseType: 'json'
};
try {
const res = await request.post(requestOptions);
await logger.log(res);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
validateProperties(propertiesString) {
let result = false;
try {
const properties = JSON.parse(propertiesString);
// If the properties object is not an array
if (properties.length === undefined) {
result = 'The specified JSON string is not an array';
}
else {
for (let i = 0; i < properties.length; i++) {
const property = properties[i];
if (!property.name) {
result = `Property ${JSON.stringify(property)} misses name`;
}
if (!this.isValidPropertyType(property.type)) {
result = `${property.type} is not a valid property type. Valid types are: Binary, Boolean, DateTime, Integer and String`;
}
}
if (typeof result !== "string") {
result = true;
}
}
}
catch (e) {
result = e;
}
return result;
}
isValidPropertyType(propertyType) {
if (!propertyType) {
return false;
}
return ['Binary', 'Boolean', 'DateTime', 'Integer', 'String'].indexOf(propertyType) > -1;
}
}
export default new GraphSchemaExtensionAddCommand();
//# sourceMappingURL=schemaextension-add.js.map