UNPKG

@digital-blueprint/formalize-app

Version:

[GitHub Repository](https://github.com/digital-blueprint/formalize-app) | [npmjs package](https://www.npmjs.com/package/@digital-blueprint/formalize-app) | [Unpkg CDN](https://unpkg.com/browse/@digital-blueprint/formalize-app/) | [Formalize Bundle](https:

182 lines (165 loc) • 5.79 kB
import DBPLitElement from '@dbp-toolkit/common/dbp-lit-element'; import {createInstance} from './i18n'; import {getStackTrace} from '@dbp-toolkit/common/src/error.js'; import {AuthMixin, LangMixin, sendNotification} from '@dbp-toolkit/common'; import {setOverridesByGlobalCache} from '@dbp-toolkit/common/i18next.js'; export default class DBPFormalizeLitElement extends LangMixin( AuthMixin(DBPLitElement), createInstance, ) { constructor() { super(); this._initialized = false; this.entryPointUrl = ''; this.basePath = ''; this.langDir = ''; } static get properties() { return { ...super.properties, entryPointUrl: {type: String, attribute: 'entry-point-url'}, basePath: {type: String, attribute: 'base-path'}, langDir: {type: String, attribute: 'lang-dir'}, }; } connectedCallback() { super.connectedCallback(); if (this.langDir) { void setOverridesByGlobalCache(this._i18n, this); } } loginCallback() { if (!this._initialized) { this.initialize(); this._initialized = true; } } initialize() {} update(changedProperties) { changedProperties.forEach((oldValue, propName) => { if ((propName === 'langDir' || propName === 'lang') && this.langDir) { void setOverridesByGlobalCache(this._i18n, this); } }); super.update(changedProperties); } /** * Returns true if a person has successfully logged in * @returns {boolean} true or false */ isLoading() { return this.isAuthPending(); } getToken() { if (!this.auth?.token) { throw new Error('Authentication token is not available'); } return this.auth.token; } getUserId() { if (!this.auth?.['user-id']) { throw new Error('User ID is not available'); } return this.auth['user-id']; } /** * @param {Response} response * @param {string} [targetNotificationId] */ handleErrorResponse(response, targetNotificationId = undefined) { switch (response.status) { case 401: sendNotification({ summary: this._i18n.t('errors.unauthorized-title'), body: this._i18n.t('errors.unauthorized-body'), type: 'danger', timeout: 0, targetNotificationId, }); break; case 403: sendNotification({ summary: this._i18n.t('errors.forbidden-title'), body: this._i18n.t('errors.forbidden-body'), type: 'danger', timeout: 0, targetNotificationId, }); break; case 404: sendNotification({ summary: this._i18n.t('errors.notfound-title'), body: this._i18n.t('errors.notfound-body'), type: 'danger', timeout: 0, targetNotificationId, }); break; case 422: // unprocessable entity sendNotification({ summary: this._i18n.t('errors.unprocessable_entity-title'), body: this._i18n.t('errors.unprocessable_entity-body'), type: 'danger', timeout: 0, targetNotificationId, }); break; default: sendNotification({ summary: this._i18n.t('errors.other-title'), body: this._i18n.t('errors.other-body'), type: 'danger', timeout: 0, targetNotificationId, }); } //throw new Error(response); } /** * Sends an analytics error event * * @param category * @param action * @param information * @param responseData */ async sendErrorAnalyticsEvent(category, action, information, responseData = {}) { let responseBody; // Use a clone of responseData to prevent "Failed to execute 'json' on 'Response': body stream already read" // after this function, but still a TypeError will occur if .json() was already called before this function try { responseBody = await responseData.clone().json(); } catch { responseBody = responseData; // got already decoded data } const data = { status: responseData.status || '', url: responseData.url || '', description: responseBody['hydra:description'] || '', errorDetails: responseBody['relay:errorDetails'] || '', information: information, // get 5 items from the stack trace stack: getStackTrace().slice(1, 6), }; this.sendSetPropertyEvent('analytics-event', { category: category, action: action, name: JSON.stringify(data), }); } /** * Gets user details from API * @param {string} userIdentifier * @returns {Promise<Response>} response */ async apiGetUserDetails(userIdentifier) { const options = { method: 'GET', headers: { 'Content-Type': 'application/ld+json', Authorization: 'Bearer ' + this.getToken(), }, }; return await fetch(this.entryPointUrl + `/base/people/${userIdentifier}`, options); } }