From d165e3819c9673f88d4b4ca53ab74c814c40addb Mon Sep 17 00:00:00 2001 From: Aaron Couch Date: Fri, 13 Sep 2024 15:55:17 -0400 Subject: [PATCH] Update BaseAPI to make it generic --- .../src/app/[locale]/opportunity/[id]/page.tsx | 6 +++--- frontend/src/app/api/BaseApi.ts | 8 ++++---- frontend/src/app/api/OpportunityListingAPI.ts | 12 +++++++----- .../search/searchfetcher/APISearchFetcher.ts | 4 +++- frontend/src/types/apiResponseTypes.ts | 17 +++++++++++++++++ .../opportunity/opportunityResponseTypes.ts | 2 +- .../src/types/search/searchResponseTypes.ts | 10 +--------- 7 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 frontend/src/types/apiResponseTypes.ts diff --git a/frontend/src/app/[locale]/opportunity/[id]/page.tsx b/frontend/src/app/[locale]/opportunity/[id]/page.tsx index c0d6e29f0..80e2c918a 100644 --- a/frontend/src/app/[locale]/opportunity/[id]/page.tsx +++ b/frontend/src/app/[locale]/opportunity/[id]/page.tsx @@ -1,7 +1,7 @@ import { - ApiResponse, + OpportunityApiResponse, Opportunity, -} from "../../../../types/opportunity/opportunityResponseTypes"; +} from "src/types/opportunity/opportunityResponseTypes"; import BetaAlert from "src/components/BetaAlert"; import Breadcrumbs from "src/components/Breadcrumbs"; @@ -40,7 +40,7 @@ export default async function OpportunityListing({ } const api = new OpportunityListingAPI(); - let opportunity: ApiResponse; + let opportunity: OpportunityApiResponse; try { opportunity = await api.getOpportunityById(id); } catch (error) { diff --git a/frontend/src/app/api/BaseApi.ts b/frontend/src/app/api/BaseApi.ts index bca7b683c..0243b99f2 100644 --- a/frontend/src/app/api/BaseApi.ts +++ b/frontend/src/app/api/BaseApi.ts @@ -17,7 +17,7 @@ import { compact, isEmpty } from "lodash"; import { QueryParamData } from "src/services/search/searchfetcher/SearchFetcher"; // TODO (#1682): replace search specific references (since this is a generic API file that any // future page or different namespace could use) -import { SearchAPIResponse } from "../../types/search/searchResponseTypes"; +import { APIResponse } from "src/types/apiResponseTypes"; export type ApiMethod = "DELETE" | "GET" | "PATCH" | "POST" | "PUT"; export interface JSONRequestBody { @@ -108,10 +108,10 @@ export default abstract class BaseApi { queryParamData?: QueryParamData, ) { let response: Response; - let responseBody: SearchAPIResponse; + let responseBody: APIResponse; try { response = await fetch(url, fetchOptions); - responseBody = (await response.json()) as SearchAPIResponse; + responseBody = (await response.json()) as APIResponse; } catch (error) { // API most likely down, but also possibly an error setting up or sending a request // or parsing the response. @@ -196,7 +196,7 @@ export function fetchErrorToNetworkError( } function handleNotOkResponse( - response: SearchAPIResponse, + response: APIResponse, message: string, status_code: number, searchInputs?: QueryParamData, diff --git a/frontend/src/app/api/OpportunityListingAPI.ts b/frontend/src/app/api/OpportunityListingAPI.ts index 8f3628a1f..9ac4e671b 100644 --- a/frontend/src/app/api/OpportunityListingAPI.ts +++ b/frontend/src/app/api/OpportunityListingAPI.ts @@ -1,6 +1,6 @@ import "server-only"; -import { ApiResponse } from "../../types/opportunity/opportunityResponseTypes"; +import { OpportunityApiResponse } from "src/types/opportunity/opportunityResponseTypes"; import BaseApi from "./BaseApi"; export default class OpportunityListingAPI extends BaseApi { @@ -16,14 +16,16 @@ export default class OpportunityListingAPI extends BaseApi { return "opportunities"; } - async getOpportunityById(opportunityId: number): Promise { + async getOpportunityById( + opportunityId: number, + ): Promise { const subPath = `${opportunityId}`; - const response = await this.request( + const response = (await this.request( "GET", this.basePath, this.namespace, subPath, - ); - return response as ApiResponse; + )) as OpportunityApiResponse; + return response; } } diff --git a/frontend/src/services/search/searchfetcher/APISearchFetcher.ts b/frontend/src/services/search/searchfetcher/APISearchFetcher.ts index 6887ee57d..6611cc8a1 100644 --- a/frontend/src/services/search/searchfetcher/APISearchFetcher.ts +++ b/frontend/src/services/search/searchfetcher/APISearchFetcher.ts @@ -21,7 +21,9 @@ export class APISearchFetcher extends SearchFetcher { // await new Promise((resolve) => setTimeout(resolve, 13250)); const response: SearchAPIResponse = - await this.searchApi.searchOpportunities(searchInputs); + (await this.searchApi.searchOpportunities( + searchInputs, + )) as SearchAPIResponse; response.actionType = searchInputs.actionType; response.fieldChanged = searchInputs.fieldChanged; diff --git a/frontend/src/types/apiResponseTypes.ts b/frontend/src/types/apiResponseTypes.ts new file mode 100644 index 000000000..860d2bcea --- /dev/null +++ b/frontend/src/types/apiResponseTypes.ts @@ -0,0 +1,17 @@ +export interface PaginationInfo { + order_by: string; + page_offset: number; + page_size: number; + sort_direction: string; + total_pages: number; + total_records: number; +} + +export interface APIResponse { + data: unknown[] | object; + message: string; + status_code: number; + pagination_info?: PaginationInfo; + warnings?: unknown[] | null | undefined; + errors?: unknown[] | null | undefined; +} diff --git a/frontend/src/types/opportunity/opportunityResponseTypes.ts b/frontend/src/types/opportunity/opportunityResponseTypes.ts index 9061f1051..eba9f404d 100644 --- a/frontend/src/types/opportunity/opportunityResponseTypes.ts +++ b/frontend/src/types/opportunity/opportunityResponseTypes.ts @@ -50,7 +50,7 @@ export interface Opportunity { updated_at: string; } -export interface ApiResponse { +export interface OpportunityApiResponse { data: Opportunity; message: string; status_code: number; diff --git a/frontend/src/types/search/searchResponseTypes.ts b/frontend/src/types/search/searchResponseTypes.ts index a7b51f7e7..240088744 100644 --- a/frontend/src/types/search/searchResponseTypes.ts +++ b/frontend/src/types/search/searchResponseTypes.ts @@ -1,4 +1,5 @@ import { SearchFetcherActionType } from "./searchRequestTypes"; +import { PaginationInfo } from "src/types/apiResponseTypes"; export interface AssistanceListing { assistance_listing_number: string; @@ -52,15 +53,6 @@ export interface Opportunity { updated_at: string; } -export interface PaginationInfo { - order_by: string; - page_offset: number; - page_size: number; - sort_direction: string; - total_pages: number; - total_records: number; -} - export interface SearchAPIResponse { data: Opportunity[]; message: string;