@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
88 lines • 3.38 kB
JavaScript
import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import { cli } from '../../../../cli/cli.js';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
export const options = z.strictObject({
...globalOptionsZod.shape,
id: z.string().optional(),
name: z.string().optional(),
force: z.boolean().optional().alias('f')
});
class ExternalConnectionRemoveCommand extends GraphCommand {
get name() {
return commands.CONNECTION_REMOVE;
}
get description() {
return 'Allows the administrator to remove a specific external connection';
}
alias() {
return [commands.EXTERNALCONNECTION_REMOVE];
}
get schema() {
return options;
}
getRefinedSchema(schema) {
return schema
.refine(options => [options.id, options.name].filter(x => x !== undefined).length === 1, {
message: `Specify either 'id' or 'name', but not both.`,
params: {
customCode: 'optionSet',
options: ['id', 'name']
}
});
}
async getExternalConnectionId(args) {
if (args.options.id) {
return args.options.id;
}
const requestOptions = {
url: `${this.resource}/v1.0/external/connections?$filter=name eq '${formatting.encodeQueryParameter(args.options.name)}'&$select=id`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
const res = await request.get(requestOptions);
if (res.value.length === 1) {
return res.value[0].id;
}
if (res.value.length === 0) {
throw `The specified connection does not exist`;
}
const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', res.value);
const result = await cli.handleMultipleResultsFound(`Multiple external connections with name ${args.options.name} found.`, resultAsKeyValuePair);
return result.id;
}
async commandAction(logger, args) {
if (args.options.force) {
await this.removeExternalConnection(args);
}
else {
const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove the external connection '${args.options.id || args.options.name}'?` });
if (result) {
await this.removeExternalConnection(args);
}
}
}
async removeExternalConnection(args) {
try {
const externalConnectionId = await this.getExternalConnectionId(args);
const requestOptions = {
url: `${this.resource}/v1.0/external/connections/${formatting.encodeQueryParameter(externalConnectionId)}`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
await request.delete(requestOptions);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
}
export default new ExternalConnectionRemoveCommand();
//# sourceMappingURL=connection-remove.js.map