@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
77 lines • 3.04 kB
JavaScript
import { z } from 'zod';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { validation } from '../../../../utils/validation.js';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { calendar } from '../../../../utils/calendar.js';
import { formatting } from '../../../../utils/formatting.js';
export const options = z.strictObject({
...globalOptionsZod.shape,
id: z.string().alias('i'),
userId: z.string().refine(id => validation.isValidGuid(id), {
error: e => `'${e.input}' is not a valid GUID.`
}).optional(),
userName: z.string().refine(name => validation.isValidUserPrincipalName(name), {
error: e => `'${e.input}' is not a valid UPN.`
}).optional(),
calendarId: z.string().optional(),
calendarName: z.string().optional(),
timeZone: z.string().optional()
});
class OutlookEventGetCommand extends GraphCommand {
get name() {
return commands.EVENT_GET;
}
get description() {
return `Retrieve an event from a specific calendar of a user`;
}
get schema() {
return options;
}
getRefinedSchema(schema) {
return schema
.refine(options => [options.userId, options.userName].filter(x => x !== undefined).length === 1, {
error: 'Specify either userId or userName, but not both'
})
.refine(options => !(options.calendarId && options.calendarName), {
error: 'Specify either calendarId or calendarName, but not both.'
});
}
async commandAction(logger, args) {
const userIdentifier = args.options.userId ?? args.options.userName;
if (this.verbose) {
await logger.logToStderr(`Retrieving event ${args.options.id} for user ${userIdentifier}...`);
}
let calendarId = args.options.calendarId;
if (args.options.calendarName) {
calendarId = (await calendar.getUserCalendarByName(userIdentifier, args.options.calendarName)).id;
}
let requestUrl = `${this.resource}/v1.0/users('${formatting.encodeQueryParameter(userIdentifier)}')`;
if (calendarId) {
requestUrl += `/calendars/${calendarId}/events/${args.options.id}`;
}
else {
requestUrl += `/events/${args.options.id}`;
}
const requestOptions = {
url: requestUrl,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
if (args.options.timeZone) {
requestOptions.headers.Prefer = `outlook.timezone="${args.options.timeZone}"`;
}
try {
const result = await request.get(requestOptions);
await logger.log(result);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
}
export default new OutlookEventGetCommand();
//# sourceMappingURL=event-get.js.map