import { createHttpApiClient } from '../../httpApi/index.js'; import { Authentication } from './Authentication.js'; import { SpecifyError, specifyErrors } from '../../errors/index.js'; /* v8 ignore start */ export class HttpResources { #authentication: Authentication; constructor(authentication: Authentication) { this.#authentication = authentication; } /* ------------------------------------------ Authenticated routes --------------------------------------------- */ /* ------------------------------------------ — Using current user and organization --------------------------------------------- */ async login(personalAccessToken: string) { const currentUserResponse = await createHttpApiClient(personalAccessToken).getMe(); if (currentUserResponse.payload.organizations.length !== 1) { throw new SpecifyError({ errorKey: specifyErrors.AUTHENTICATION_NOT_MATCHING_ORGANIZATIONS_LENGTH.errorKey, publicMessage: 'The user is not in exactly one organization', }); } this.#authentication.set(personalAccessToken, currentUserResponse.payload); } getMe() { return createHttpApiClient(this.#authentication.personalAccessToken).getMe(); } getMyRepositories() { return this.getRepositoriesByOwner( this.#authentication.credentials.currentOrganization.namespace, ); } getMyRepositoryByName(name: string) { return this.getRepositoryByOwnerAndName( this.#authentication.credentials.currentOrganization.namespace, name, ); } getMyRepositoryTokenTreeByName(name: string) { return this.getRepositoryTokenTreeByOwnerAndName( this.#authentication.credentials.currentOrganization.namespace, name, ); } /* ------------------------------------------ — Generic routes --------------------------------------------- */ getRepositoriesByOwner(owner: string) { return createHttpApiClient(this.#authentication.personalAccessToken).getRepositoriesByOwner( owner, ); } getRepositoryByOwnerAndName(owner: string, name: string) { return createHttpApiClient( this.#authentication.personalAccessToken, ).getRepositoryByOwnerAndName(owner, name); } getRepositoryTokenTreeByOwnerAndName(owner: string, name: string) { return createHttpApiClient( this.#authentication.personalAccessToken, ).getRepositoryTokenTreeByOwnerAndName(owner, name); } } /* v8 ignore stop */