-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding api/variantValidator (#38)
- Loading branch information
Showing
9 changed files
with
2,087 additions
and
0 deletions.
There are no files selected for viewing
436 changes: 436 additions & 0 deletions
436
src/api/variantValidator/__snapshots__/client.spec.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
524 changes: 524 additions & 0 deletions
524
src/api/variantValidator/__snapshots__/types.spec.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
||
/** 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: | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.