-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a613c6
commit c0d3660
Showing
5 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
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,100 @@ | ||
import { Zodios } from '@zodios/core'; | ||
import { ZodiosHooks, type ZodiosHooksInstance } from '@zodios/react'; | ||
import { isAxiosError } from 'axios'; | ||
|
||
import { backendApi } from './backendApi.ts'; | ||
import { lapisApi } from './lapisApi.ts'; | ||
import { seqSetCitationApi } from './seqSetCitationApi.ts'; | ||
import { problemDetail } from '../types/backend.ts'; | ||
import type { SequenceRequest } from '../types/lapis.ts'; | ||
import type { ClientConfig } from '../types/runtimeConfig.ts'; | ||
import { fastaEntries } from '../utils/parseFasta.ts'; | ||
import { isAlignedSequence, isUnalignedSequence, type SequenceType } from '../utils/sequenceTypeHelpers.ts'; | ||
|
||
export function backendClientHooks(clientConfig: ClientConfig) { | ||
const zodiosHooks = new ZodiosHooks('loculus', new Zodios(clientConfig.backendUrl, backendApi)); | ||
return { | ||
zodiosHooks, | ||
utilityHooks: { | ||
useGetProcessedSequencesCount(token: string) { | ||
return zodiosHooks.useGetProcessedSequencesCount({ | ||
headers: { Authorization: `Bearer ${token}` }, | ||
}); | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export function lapisClientHooks(lapisUrl: string) { | ||
const zodiosHooks = new ZodiosHooks('lapis', new Zodios(lapisUrl, lapisApi, { transform: false })); | ||
return { | ||
zodiosHooks, | ||
utilityHooks: { | ||
useGetSequence(accessionVersion: string, sequenceType: SequenceType, isMultiSegmented: boolean) { | ||
const { data, error, isLoading } = getSequenceHook( | ||
zodiosHooks, | ||
{ | ||
accessionVersion, | ||
dataFormat: 'FASTA', | ||
}, | ||
sequenceType, | ||
isMultiSegmented, | ||
); | ||
|
||
if (data === undefined) { | ||
if (isAxiosError(error)) { | ||
const maybeProblemDetail = error.response?.data.error ?? error.response?.data; | ||
|
||
const problemDetailParseResult = problemDetail.safeParse(maybeProblemDetail); | ||
|
||
if (problemDetailParseResult.success) { | ||
return { data: null, error: problemDetailParseResult.data, isLoading }; | ||
} | ||
} | ||
|
||
return { data, error, isLoading }; | ||
} | ||
|
||
const parseResult = fastaEntries.safeParse(data); | ||
|
||
if (parseResult.success) { | ||
return { | ||
data: parseResult.data.length > 0 ? parseResult.data[0] : null, | ||
error, | ||
isLoading, | ||
}; | ||
} | ||
return { | ||
data: undefined, | ||
error: parseResult.error, | ||
isLoading, | ||
}; | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function getSequenceHook( | ||
hooks: ZodiosHooksInstance<typeof lapisApi>, | ||
request: SequenceRequest, | ||
sequenceType: SequenceType, | ||
isMultiSegmented: boolean, | ||
) { | ||
if (isUnalignedSequence(sequenceType)) { | ||
return isMultiSegmented | ||
? hooks.useUnalignedNucleotideSequencesMultiSegment(request, { params: { segment: sequenceType.name } }) | ||
: hooks.useUnalignedNucleotideSequences(request); | ||
} | ||
|
||
if (isAlignedSequence(sequenceType)) { | ||
return isMultiSegmented | ||
? hooks.useAlignedNucleotideSequencesMultiSegment(request, { params: { segment: sequenceType.name } }) | ||
: hooks.useAlignedNucleotideSequences(request); | ||
} | ||
|
||
return hooks.useAlignedAminoAcidSequences(request, { params: { gene: sequenceType.name } }); | ||
} | ||
|
||
export function seqSetCitationClientHooks(clientConfig: ClientConfig) { | ||
return new ZodiosHooks('loculus', new Zodios(clientConfig.backendUrl, seqSetCitationApi)); | ||
} |
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
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
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
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