UNPKG

@novo-learning/novo-sdk

Version:

SDK for the Novolanguage Speech Analysis API

121 lines (109 loc) 4.01 kB
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; import { ExerciseResultMapper } from '../../mappers/exercise-result.mapper'; import { AttemptResponseDtoV1, ReplaceAttemptDtoV1 } from './dto'; import { Configuration, FilesApi, AttemptResponseDtoV1 as GeneratedAttemptResponseDtoV1, AttemptsApi as GeneratedAttemptsApi, } from './generated'; export class DataApi { private readonly filesApi: FilesApi; private readonly attemptsApi: GeneratedAttemptsApi; private readonly exerciseResultMapper: ExerciseResultMapper; constructor(configuration?: Configuration | undefined, basePath?: string, axios?: AxiosInstance) { this.attemptsApi = new GeneratedAttemptsApi(configuration, basePath, axios); this.filesApi = new FilesApi(configuration, basePath, axios); this.exerciseResultMapper = new ExerciseResultMapper(); } private mapGeneratedExerciseResponse( dto: AxiosResponse<GeneratedAttemptResponseDtoV1>, ): AxiosResponse<AttemptResponseDtoV1> { return { ...dto, data: { ...dto.data, exerciseResult: this.exerciseResultMapper.mapFromGenerated(dto.data.exerciseResult), }, }; } private mapGeneratedExerciseResponses( dto: AxiosResponse<Array<GeneratedAttemptResponseDtoV1>>, ): AxiosResponse<Array<AttemptResponseDtoV1>> { return { ...dto, data: dto.data.map((response) => ({ ...response, exerciseResult: this.exerciseResultMapper.mapFromGenerated(response.exerciseResult), })), }; } public async createAttempt( attempt?: string, audio?: unknown, options?: AxiosRequestConfig<unknown> | undefined, ): Promise<AxiosResponse<AttemptResponseDtoV1>> { const response = await this.attemptsApi.createAttempt(attempt, audio, options); return this.mapGeneratedExerciseResponse(response); } public async getAttempt( id: string, options?: AxiosRequestConfig<unknown> | undefined, ): Promise<AxiosResponse<AttemptResponseDtoV1>> { const response = await this.attemptsApi.getAttempt(id, options); return this.mapGeneratedExerciseResponse(response); } public getAttempts( options?: AxiosRequestConfig<unknown> | undefined, ): Promise<AxiosResponse<Array<AttemptResponseDtoV1>>> { return ( this.attemptsApi // No filter option for sdk users .getAttempts(undefined, undefined, undefined, false, options) .then((response) => this.mapGeneratedExerciseResponses(response)) ); } public removeAttempt(id: string, options?: AxiosRequestConfig<unknown>): Promise<AxiosResponse<void>> { return this.attemptsApi.removeAttempt(id, options); } public async updateAttempt( id: string, replaceAttemptDtoV1: ReplaceAttemptDtoV1, options?: AxiosRequestConfig<unknown> | undefined, ): Promise<AxiosResponse<AttemptResponseDtoV1>> { const response = await this.attemptsApi.updateAttempt( id, { ...replaceAttemptDtoV1, exerciseResult: { ...replaceAttemptDtoV1.exerciseResult, respondedAt: replaceAttemptDtoV1.exerciseResult.respondedAt.toISOString(), }, }, options, ); return this.mapGeneratedExerciseResponse(response); } public async getAudio(audioKey: string, options?: AxiosRequestConfig<unknown> | undefined): Promise<ArrayBuffer> { const response = await this.filesApi.downloadFile(audioKey, { ...options, responseType: 'arraybuffer' }); return response.data as ArrayBuffer; } public async downloadAttempts( publisherId: string, offset: number, limit: number, filter: Record<string, unknown>, options?: AxiosRequestConfig<unknown> | undefined, ): Promise<AxiosResponse<Array<AttemptResponseDtoV1>>> { const response = await this.attemptsApi.downloadAttempts( { publisherId, offset, limit, ...filter, }, options, ); return this.mapGeneratedExerciseResponses(response); } }