UNPKG

@starsched/sdk

Version:

ABA clinic control and management service API SDK

208 lines (207 loc) 10.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.StarSchedAPI = void 0; const client_1 = require("./http/client"); const index_1 = require("./services/account/index"); const index_2 = require("./services/appointment/index"); const index_3 = require("./services/authentication/index"); const index_4 = require("./services/companies/index"); const index_5 = require("./services/company-invites/index"); const index_6 = require("./services/company-members/index"); const index_7 = require("./services/company-patient/index"); const index_8 = require("./services/company-plan/index"); const index_9 = require("./services/company-professional/index"); const index_10 = require("./services/profiles/index"); __exportStar(require("./services/account/protocols/company-sign-up-request"), exports); __exportStar(require("./services/account/protocols/finalize-company-sign-up-request.protocols"), exports); __exportStar(require("./services/account/protocols/get-company-sign-up-request.protocols"), exports); __exportStar(require("./services/account/protocols/request-company-sign-up.protocols"), exports); __exportStar(require("./services/appointment/protocols/appointment"), exports); __exportStar(require("./services/appointment/protocols/list-by-patient"), exports); __exportStar(require("./services/authentication/protocols/sign-in-with-email-and-password.protocols"), exports); __exportStar(require("./services/company-plan/protocols/company-plan"), exports); __exportStar(require("./services/company-plan/protocols/get-plan.protocols"), exports); __exportStar(require("./services/companies/protocols/company"), exports); __exportStar(require("./services/companies/protocols/create-company.protocols"), exports); __exportStar(require("./services/companies/protocols/get-my-companies.protocols"), exports); __exportStar(require("./services/companies/protocols/update-company.protocols"), exports); __exportStar(require("./services/company-invites/protocols/company-invite"), exports); __exportStar(require("./services/company-invites/protocols/create.protocols"), exports); __exportStar(require("./services/company-invites/protocols/delete.protocols"), exports); __exportStar(require("./services/company-invites/protocols/list.protocols"), exports); __exportStar(require("./services/company-invites/protocols/resend.protocols"), exports); __exportStar(require("./services/company-invites/protocols/update-role.protocols"), exports); __exportStar(require("./services/company-members/protocols/company-member"), exports); __exportStar(require("./services/company-members/protocols/list.protocols"), exports); __exportStar(require("./services/company-members/protocols/update-role.protocols"), exports); __exportStar(require("./services/company-members/protocols/update-access.protocols"), exports); __exportStar(require("./services/company-patient/protocols/company-patient"), exports); __exportStar(require("./services/company-patient/protocols/create.protocols"), exports); __exportStar(require("./services/company-patient/protocols/list.protocols"), exports); __exportStar(require("./services/company-professional/protocols/company-professional"), exports); __exportStar(require("./services/company-professional/protocols/list.protocols"), exports); __exportStar(require("./services/profiles/protocols/get-my-profile.protocols"), exports); __exportStar(require("./services/profiles/protocols/profile"), exports); class StarSchedAPI { options; isRefreshingAccessToken = false; failedRequestsQueue = []; accessToken = null; refreshToken = null; httpClient; authentication; profiles; accounts; companies; companiesPlan; companyMembers; companyProfessionals; companyInvites; companyPatients; appointments; constructor(options) { this.options = options; this.httpClient = new client_1.HttpClient({ baseURL: options.baseURL, requestInterceptors: [this.authenticatedRoutesInterceptor.bind(this)], responseInterceptors: [ this.refreshAuthenticationInterceptor.bind(this), this.storeAuthenticationOutputInterceptor.bind(this) ] }); this.authentication = new index_3.Authentication(this.httpClient); this.profiles = new index_10.Profiles(this.httpClient); this.accounts = new index_1.Accounts(this.httpClient); this.companies = new index_4.Companies(this.httpClient); this.companiesPlan = new index_8.CompaniesPlan(this.httpClient); this.companyMembers = new index_6.CompanyMembers(this.httpClient); this.companyProfessionals = new index_9.Companyprofessionals(this.httpClient); this.companyInvites = new index_5.CompanyInvites(this.httpClient); this.companyPatients = new index_7.CompanyPatients(this.httpClient); this.appointments = new index_2.Appointments(this.httpClient); } async authenticatedRoutesInterceptor(config) { if (this.options.logRequests) { console.log(`${config.method} ${config.url} ${this.accessToken ? '(with auth)' : ''}`); } const notAuthenticatedRoutes = [ '/v1/authentication/email', '/v1/authentication/refresh' ]; const routeRequiresAuthentication = !notAuthenticatedRoutes.includes(config.url); if (routeRequiresAuthentication) { if (this.options.authenticationStore) { const storedAuthentication = await this.options.authenticationStore.get(); if (storedAuthentication) { this.accessToken = storedAuthentication.access_token; this.refreshToken = storedAuthentication.refresh_token; } } if (this.accessToken) { config.headers.set('Authorization', `Bearer ${this.accessToken}`); } } return config; } async refreshAuthenticationInterceptor(config, response) { if (response.ok) { return response; } const errorResponse = response; const authenticationExpired = errorResponse.statusCode === 401 && errorResponse.body?.code === 'token.expired'; if (!authenticationExpired) { return response; } if (!this.refreshToken) { return response; } if (!this.isRefreshingAccessToken) { this.isRefreshingAccessToken = true; this.refreshAccessToken() .then((refreshTokenResponse) => { if (!refreshTokenResponse.ok) { throw refreshTokenResponse; } return refreshTokenResponse.body; }) .then(async (refreshTokenResponse) => { // biome-ignore lint/style/noNonNullAssertion: <explanation> const authentication = refreshTokenResponse; this.accessToken = authentication.access_token; this.refreshToken = authentication.refresh_token; if (this.options.authenticationStore) { await this.options.authenticationStore.store(authentication); } // biome-ignore lint/complexity/noForEach: <explanation> this.failedRequestsQueue.forEach((request) => { request.onSuccess(authentication.access_token); }); this.failedRequestsQueue = []; }) .catch(async (refreshTokenError) => { if (this.options.authenticationStore) { await this.options.authenticationStore.remove(); } // biome-ignore lint/complexity/noForEach: <explanation> this.failedRequestsQueue.forEach((request) => { request.onFailure(refreshTokenError); }); this.failedRequestsQueue = []; if (this.options.onLogout) { this.options.onLogout(); } }) .finally(() => { this.isRefreshingAccessToken = false; }); return new Promise((resolve, reject) => { this.failedRequestsQueue.push({ onSuccess: (updatedAccessToken) => { config.headers.set('Authorization', `Bearer ${updatedAccessToken}`); resolve(this.httpClient.request(config)); }, onFailure: reject }); }); } return errorResponse; } async storeAuthenticationOutputInterceptor(config, response) { if (!response.ok) { return response; } const authenticationRoutes = [ '/v1/authentication/email', '/v1/authentication/refresh' ]; const isAuthenticationResponse = authenticationRoutes.includes(config.url); if (isAuthenticationResponse) { const body = response.body; if (this.options.authenticationStore) { await this.options.authenticationStore.store(body); } this.accessToken = body.access_token; this.refreshToken = body.refresh_token; } return response; } async refreshAccessToken() { return this.httpClient.post('/v1/authentication/refresh', { refresh_token: this.refreshToken }); } } exports.StarSchedAPI = StarSchedAPI;