UNPKG

@backstage/plugin-microsoft-calendar

Version:
79 lines (75 loc) 2.46 kB
import { createApiRef, createPlugin, createApiFactory, microsoftAuthApiRef, fetchApiRef, createComponentExtension } from '@backstage/core-plugin-api'; import { ResponseError } from '@backstage/errors'; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; const microsoftCalendarApiRef = createApiRef( { id: "plugin.microsoft-calendar.service" } ); class MicrosoftCalendarApiClient { constructor(options) { __publicField(this, "authApi"); __publicField(this, "fetchApi"); this.authApi = options.authApi; this.fetchApi = options.fetchApi; } async get(path, params = {}, headers) { const query = new URLSearchParams(params); const url = new URL( `${path}?${query.toString()}`, "https://graph.microsoft.com" ); const token = await this.authApi.getAccessToken(); let temp = {}; if (headers && typeof headers === "object") { temp = { ...headers }; } if (token) { temp.Authorization = `Bearer ${token}`; } const response = await this.fetchApi.fetch(url.toString(), { headers: temp }); if (!response.ok) { throw await ResponseError.fromResponse(response); } return response.json(); } async getCalendars() { const data = await this.get("v1.0/me/calendars"); return data.value; } async getEvents(calendarId, params, headers) { const data = await this.get(`v1.0/me/calendars/${calendarId}/calendarview`, params, headers); return data.value; } } const microsoftCalendarPlugin = createPlugin({ id: "microsoft-calendar", apis: [ createApiFactory({ api: microsoftCalendarApiRef, deps: { authApi: microsoftAuthApiRef, fetchApi: fetchApiRef }, factory(deps) { return new MicrosoftCalendarApiClient(deps); } }) ] }); const MicrosoftCalendarCard = microsoftCalendarPlugin.provide( createComponentExtension({ name: "MicrosoftCalendarCard", component: { lazy: () => import('./esm/index-DGcR6krL.esm.js').then((m) => m.MicrosoftCalendar) } }) ); export { MicrosoftCalendarApiClient, MicrosoftCalendarCard, microsoftCalendarApiRef }; //# sourceMappingURL=index.esm.js.map