import { BugsnagApi } from './BugsnagApi' ; import { BugsnagError , Organisation , Project } from './types' ; import { DiscoveryApi } from '@backstage/core-plugin-api' ; const DEFAULT_PROXY_PATH = '/bugsnag/api' ; export type Options = { discoveryApi : DiscoveryApi ; proxyPath ? : string ; } ; export class BugsnagClient implements BugsnagApi { private readonly discoveryApi : DiscoveryApi ; private readonly proxyPath : string ; constructor ( options ) { this . discoveryApi = options . discoveryApi ; this . proxyPath = options . proxyPath ?? DEFAULT_PROXY_PATH ; } private async getApiUrl ( ) { const proxyUrl = await this . discoveryApi . getBaseUrl ( 'proxy' ) ; return proxyUrl + this . proxyPath ; } async fetchOrganisations ( ) { const response = await fetch ( `${ await this . getApiUrl ( ) }/user/organizations` ) ; const payload = await response . json ( ) ; if ( ! response . ok ) { throw new Error ( payload . errors [ 0 ] ) ; } return payload ; } async fetchProjects ( organisationId ) { const response = await fetch ( `${ await this . getApiUrl ( ) }/organizations/${ organisationId }/projects` ) ; const payload = await response . json ( ) ; if ( ! response . ok ) { throw new Error ( payload . errors [ 0 ] ) ; } return await payload ; } async fetchErrors ( projectId ) { const response = await fetch ( `${ await this . getApiUrl ( ) }/projects/${ projectId }/errors` ) ; const payload = await response . json ( ) ; if ( ! response . ok ) { throw new Error ( payload . errors [ 0 ] ) ; } return await payload ; } }