Skip to content

Commit

Permalink
feat: adding api/variantValidator (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe authored Jan 30, 2024
1 parent 3ad1082 commit d9bfe61
Show file tree
Hide file tree
Showing 9 changed files with 2,087 additions and 0 deletions.
436 changes: 436 additions & 0 deletions src/api/variantValidator/__snapshots__/client.spec.ts.snap

Large diffs are not rendered by default.

524 changes: 524 additions & 0 deletions src/api/variantValidator/__snapshots__/types.spec.ts.snap

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/api/variantValidator/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from 'fs'
import path from 'path'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import createFetchMock from 'vitest-fetch-mock'

import { SeqvarImpl } from '../../lib/genomicVars'
import { VariantValidatorClient } from './client'
import { Response } from './types'

Check warning on line 8 in src/api/variantValidator/client.spec.ts

View workflow job for this annotation

GitHub Actions / Lint

'Response' is defined but never used. Allowed unused vars must match /^_/u

/** Fixture Seqvar */
const seqvar = new SeqvarImpl('grch37', '6', 24302274, 'T', 'C')

/** Fixture with response from API. */
const responseManeBrca1Json = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './fixture.maneResponse.BRCA1.json'), 'utf8')
)

/** Initialize mock for `fetch()`. */
const fetchMocker = createFetchMock(vi)

describe.concurrent('VariantValidator', () => {
beforeEach(() => {
fetchMocker.enableMocks()
fetchMocker.resetMocks()
})

it('handles response correctly', async () => {
// arrange:
fetchMocker.mockResponseOnce(JSON.stringify(responseManeBrca1Json))

// act:
const client = new VariantValidatorClient()
const result = await client.fetchVvResults(seqvar)

// assert:
expect(result).toMatchSnapshot()
})

it('throws in case of fetching problems', async () => {
// arrange:
fetchMocker.mockResponse(() => {
return Promise.reject(new Error('failed to fetch from VariantValidator'))
})

// act:
const client = new VariantValidatorClient()
// (with guard)
await expect(async () => await client.fetchVvResults(seqvar)).rejects.toThrow(
'failed to fetch from VariantValidator'
)

// assert:
})
})
36 changes: 36 additions & 0 deletions src/api/variantValidator/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Seqvar } from '../../lib/genomicVars'

/** Base URL for VariantValidator. */
const API_BASE_URL = `/remote/variantvalidator`

/**
* Client for the VariantValidator API.
*/
export class VariantValidatorClient {
private apiBaseUrl: string

constructor(apiBaseUrl?: string) {
this.apiBaseUrl = apiBaseUrl ?? API_BASE_URL
}

/**
* Call variant validator API via proxy.
*
* @param seqvar The `Seqvar` object to be validated.
* @returns The response from the API.
* @throws Error if the API call fails.
*/
async fetchVvResults(seqvar: Seqvar): Promise<any> {
const { genomeBuild, chrom, pos, del, ins } = seqvar
const release = genomeBuild === 'grch37' ? 'hg19' : 'hg38'
const url =
`${this.apiBaseUrl}/${release}/` +
`${chrom}-${pos}-${del}-${ins}/mane?content-type=application%2Fjson`

const response = await fetch(url, { method: 'GET' })
if (!response.ok) {
throw new Error(`Failed to fetch ACMG rating for ${seqvar.userRepr}`)
}
return await response.json()
}
}
Loading

0 comments on commit d9bfe61

Please sign in to comment.