Skip to content

Commit

Permalink
improve itis service types & address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mauberti-bc committed Aug 26, 2024
1 parent e700be8 commit 9183875
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
18 changes: 10 additions & 8 deletions api/src/paths/taxonomy/taxon/tsn/hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ const defaultLog = getLogger('paths/taxonomy/taxon/tsn/hierarchy');
export const GET: Operation = [getTaxonHierarchyByTSN()];

GET.apiDoc = {
description: 'Get taxon records by TSN ids.',
description: 'Get taxon hierarchy information by TSN ids.',
tags: ['taxon_id'],
security: [
{
Bearer: []
}
],
security: [],
parameters: [
{
description: 'Taxon TSN ids.',
Expand Down Expand Up @@ -44,12 +40,18 @@ GET.apiDoc = {
items: {
title: 'Species',
type: 'object',
description: 'Taxon hierarchy response object with an array of parent TSNs',
required: ['tsn', 'hierarchy'],
properties: {
tsn: {
type: 'integer'
},
hierarchy: { type: 'array', items: { type: 'integer' } }
hierarchy: {
type: 'array',
description:
'Array of parent TSNs in descending order, where the highest-ranking parent is first and the TSN for which the hierarchy was requested is last.',
items: { type: 'integer' }
}
},
additionalProperties: false
}
Expand Down Expand Up @@ -83,7 +85,7 @@ export function getTaxonHierarchyByTSN(): RequestHandler {

const connection = getAPIUserDBConnection();

const tsnIds: number[] = (req.query.tsn as (string | number)[]).map(Number);
const tsnIds: number[] = (req.query.tsn as string[]).map(Number);

try {
await connection.open();
Expand Down
15 changes: 12 additions & 3 deletions api/src/services/itis-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export type TSNWithHierarchy = {
hierarchy: number[];
};

/**
* Generic base type for the ITIS Solr service
*/
type ItisSolrResponseBase<T> = {
response: {
docs: T;
};
};

/**
* Service for retrieving and processing taxonomic data from the Integrated Taxonomic Information System (ITIS).
*
Expand All @@ -48,7 +57,7 @@ export class ItisService {

defaultLog.debug({ label: 'searchItisByTerm', message: 'url', url });

const response = await axios.get(url);
const response = await axios.get<ItisSolrResponseBase<ItisSolrSearchResponse[]>>(url);

if (!response.data || !response.data.response || !response.data.response.docs) {
return [];
Expand Down Expand Up @@ -76,7 +85,7 @@ export class ItisService {

defaultLog.debug({ label: 'searchItisByTSN', message: 'url', url });

const response = await axios.get(url);
const response = await axios.get<ItisSolrResponseBase<ItisSolrSearchResponse[]>>(url);

if (!response.data || !response.data.response || !response.data.response.docs) {
return [];
Expand All @@ -97,7 +106,7 @@ export class ItisService {

defaultLog.debug({ label: 'getHierarchyForTSNs', message: 'url', url });

const response = await axios.get(url);
const response = await axios.get<ItisSolrResponseBase<ItisSolrSearchResponseHierarchy[]>>(url);

if (!response.data || !response.data.response || !response.data.response.docs) {
return [];
Expand Down

0 comments on commit 9183875

Please sign in to comment.