UNPKG

@pnp/spfx-controls-react

Version:

Reusable React controls for SharePoint Framework solutions

107 lines 5.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSpAPI = void 0; var tslib_1 = require("tslib"); /* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-for-in-array */ /* eslint-disable @typescript-eslint/no-explicit-any */ var React = tslib_1.__importStar(require("react")); var sp_http_1 = require("@microsoft/sp-http"); var useGrapAPI_1 = require("./useGrapAPI"); var useSpAPI = function (context) { var getDriveItemDownloadUrl = (0, useGrapAPI_1.useGraphAPI)(context).getDriveItemDownloadUrl; var getFileFromBlob = React.useCallback(function (blob, fileName) { var file = new File([blob], fileName, { type: blob.type }); return file; }, []); var getADAcesstoken = React.useCallback(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var token1, token, getSiteLists, data; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, context.aadTokenProviderFactory.getTokenProvider()]; case 1: token1 = _a.sent(); return [4 /*yield*/, token1.getToken('https://microsoft.sharepoint.com')]; case 2: token = _a.sent(); return [4 /*yield*/, fetch('https://ysz3l-my.sharepoint.com/personal/jmendes_spteckint_onmicrosoft_com/_api/web/lists', { method: 'GET', headers: { 'Authorization': "Bearer ".concat(token) } })]; case 3: getSiteLists = _a.sent(); return [4 /*yield*/, getSiteLists.json()]; case 4: data = _a.sent(); console.log(data); return [2 /*return*/]; } }); }); }, []); var downloadBingContent = function (absoluteFileUrl, fileName) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var fileDownloadResult, blob, err_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); return [4 /*yield*/, context.httpClient.get(absoluteFileUrl, sp_http_1.SPHttpClient.configurations.v1, { method: "GET", mode: "cors" })]; case 1: fileDownloadResult = _a.sent(); if (!fileDownloadResult || !fileDownloadResult.ok) { throw new Error("Something went wrong when downloading the file. Status='".concat(fileDownloadResult.status, "'")); } return [4 /*yield*/, fileDownloadResult.blob()]; case 2: blob = _a.sent(); return [2 /*return*/, getFileFromBlob(blob, fileName)]; case 3: err_1 = _a.sent(); console.error("[DownloadBingContent] Err='".concat(err_1.message, "'")); return [2 /*return*/, null]; case 4: return [2 /*return*/]; } }); }); }; var downLoadSpOrOneDriveContent = function (driveId, itemId, fileName) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var fileDownloadUrl, fileDownloadResult, blob, err_2; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 4, , 5]); return [4 /*yield*/, getDriveItemDownloadUrl(driveId, itemId)]; case 1: fileDownloadUrl = _a.sent(); return [4 /*yield*/, context.httpClient.get(fileDownloadUrl, sp_http_1.SPHttpClient.configurations.v1, { method: "GET", mode: "cors" })]; case 2: fileDownloadResult = _a.sent(); if (!fileDownloadResult || !fileDownloadResult.ok) { throw new Error("Something went wrong when downloading the file. Status='".concat(fileDownloadResult.status, "'")); } return [4 /*yield*/, fileDownloadResult.blob()]; case 3: blob = _a.sent(); return [2 /*return*/, getFileFromBlob(blob, fileName)]; case 4: err_2 = _a.sent(); console.error("[DownloadBingContent] Err='".concat(err_2.message, "'")); return [2 /*return*/, null]; case 5: return [2 /*return*/]; } }); }); }; return { getADAcesstoken: getADAcesstoken, downloadBingContent: downloadBingContent, downLoadSpOrOneDriveContent: downLoadSpOrOneDriveContent }; }; exports.useSpAPI = useSpAPI; //# sourceMappingURL=useSpAPI.js.map