Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
theosanderson committed Nov 1, 2024
1 parent 6a613c6 commit c0d3660
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
100 changes: 100 additions & 0 deletions unknown-target
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));
}
10 changes: 10 additions & 0 deletions website/src/services/backendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ const infoEndpoint = makeEndpoint({
response: info,
});

const getProcessedSequencesCountEndpoint = makeEndpoint({
method: 'get',
path: '/debug/processed-sequences-count',
alias: 'getProcessedSequencesCount',
parameters: [authorizationHeader],
response: z.record(z.string(), z.number()),
errors: [notAuthorizedError],
});

export const backendApi = makeApi([
submitEndpoint,
reviseEndpoint,
Expand All @@ -264,4 +273,5 @@ export const backendApi = makeApi([
getDataUseTermsHistoryEndpoint,
setDataUseTerms,
infoEndpoint,
getProcessedSequencesCountEndpoint,
]);
6 changes: 6 additions & 0 deletions website/src/services/backendClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ export class BackendClient extends ZodiosWrapperClient<typeof backendApi> {
() => false,
);
}

public getProcessedSequencesCount(token: string) {
return this.call('getProcessedSequencesCount', {
headers: createAuthorizationHeader(token),
});
}
}
12 changes: 11 additions & 1 deletion website/src/services/serviceHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ import { fastaEntries } from '../utils/parseFasta.ts';
import { isAlignedSequence, isUnalignedSequence, type SequenceType } from '../utils/sequenceTypeHelpers.ts';

export function backendClientHooks(clientConfig: ClientConfig) {
return new ZodiosHooks('loculus', new Zodios(clientConfig.backendUrl, backendApi));
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) {
Expand Down
4 changes: 4 additions & 0 deletions website/src/services/zodiosWrapperClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ export class ZodiosWrapperClient<Api extends ZodiosEndpointDefinitions> {
instance: method,
};
}

public async getProcessedSequencesCount(): Promise<Result<Record<string, number>, ProblemDetail>> {
return this.call('getProcessedSequencesCount');
}
}

0 comments on commit c0d3660

Please sign in to comment.