mui-spfx-controls
Version:
SPFx component library built with MUI
57 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserService = void 0;
var tslib_1 = require("tslib");
var pnp_config_1 = require("../config/pnp.config");
/**
* Service class for User profiles
*/
var UserService = /** @class */ (function () {
/**
* Initializes the ProfileService instance.
* @param {WebPartContext} context - The SharePoint WebPart context.
*/
function UserService(context) {
this.graph = (0, pnp_config_1.getGraph)(context);
}
/**
* Fetches current user details from Microsoft Graph.
* @returns {Promise<User>} A promise resolving to current user profile details.
* @throws {Error} - Throws an error if the request fails due to insufficient permissions.
* @example
* const user = await profileService.getUser('john.doe@example.com');
* console.log(user.displayName, user.mail);
*/
UserService.prototype.getCurrentUser = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.graph.me()];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Fetches user details from Microsoft Graph using either a user ID or email.
* @param upn - The user's upn
* @returns {Promise<User>} A promise resolving to the user profile details.
* @throws {Error} - Throws an error if the request fails due to insufficient permissions.
* @example
* const user = await profileService.getUser('john.doe@example.com');
* console.log(user.displayName, user.mail);
*/
UserService.prototype.getUser = function (upn) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.graph.users.getById(upn)()];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
return UserService;
}());
exports.UserService = UserService;
//# sourceMappingURL=UserService.js.map