Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Update BaseAPI to make it generic
Browse files Browse the repository at this point in the history
  • Loading branch information
acouch committed Sep 13, 2024
1 parent 95d1232 commit d165e38
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
6 changes: 3 additions & 3 deletions frontend/src/app/[locale]/opportunity/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/app/api/BaseApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -196,7 +196,7 @@ export function fetchErrorToNetworkError(
}

function handleNotOkResponse(
response: SearchAPIResponse,
response: APIResponse,
message: string,
status_code: number,
searchInputs?: QueryParamData,
Expand Down
12 changes: 7 additions & 5 deletions frontend/src/app/api/OpportunityListingAPI.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,14 +16,16 @@ export default class OpportunityListingAPI extends BaseApi {
return "opportunities";
}

async getOpportunityById(opportunityId: number): Promise<ApiResponse> {
async getOpportunityById(
opportunityId: number,
): Promise<OpportunityApiResponse> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
17 changes: 17 additions & 0 deletions frontend/src/types/apiResponseTypes.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion frontend/src/types/opportunity/opportunityResponseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface Opportunity {
updated_at: string;
}

export interface ApiResponse {
export interface OpportunityApiResponse {
data: Opportunity;
message: string;
status_code: number;
Expand Down
10 changes: 1 addition & 9 deletions frontend/src/types/search/searchResponseTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SearchFetcherActionType } from "./searchRequestTypes";
import { PaginationInfo } from "src/types/apiResponseTypes";

export interface AssistanceListing {
assistance_listing_number: string;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d165e38

Please sign in to comment.