diff --git a/apps/api/src/routes/v1/graphql/lib.ts b/apps/api/src/routes/v1/graphql/lib.ts index a643400a..a73f6e2a 100644 --- a/apps/api/src/routes/v1/graphql/lib.ts +++ b/apps/api/src/routes/v1/graphql/lib.ts @@ -1,6 +1,5 @@ import type { BaseContext, HTTPGraphQLResponse } from "@apollo/server"; import type { IFieldResolver } from "@graphql-tools/utils"; -import { isErrorResponse } from "@peterportal-api/types"; import type { RawResponse } from "@peterportal-api/types"; import { GraphQLError } from "graphql/error"; @@ -58,7 +57,7 @@ export const proxyRestApi = }; }); - if (isErrorResponse(data)) { + if (!data.success) { throw new GraphQLError(data.message, { extensions: { code: data.error.toUpperCase().replace(" ", "_"), diff --git a/libs/lambda/src/response.ts b/libs/lambda/src/response.ts index a4e90d62..3038f91d 100644 --- a/libs/lambda/src/response.ts +++ b/libs/lambda/src/response.ts @@ -47,7 +47,7 @@ export function createOKResult( ): APIGatewayProxyResult { const statusCode = 200; const timestamp = createTimestamp(); - const response: Response = { statusCode, timestamp, requestId, payload }; + const response: Response = { statusCode, timestamp, requestId, payload, success: true }; const headers = { ...responseHeaders }; try { @@ -89,12 +89,12 @@ export function createErrorResult( e instanceof Error ? `${e.name}: ${e.message}` : typeof e === "string" - ? e - : "An unknown error has occurred. Please try again."; + ? e + : "An unknown error has occurred. Please try again."; const error = httpErrorCodes[statusCode as keyof typeof httpErrorCodes]; - const body: ErrorResponse = { timestamp, requestId, statusCode, error, message }; + const body: ErrorResponse = { timestamp, requestId, statusCode, error, message, success: false }; logger.error(`${body.statusCode} ${body.error}: ${body.message}`); diff --git a/package.json b/package.json index 6f07a567..99c02082 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ }, "packageManager": "pnpm@8.14.3", "engines": { - "node": "20", "pnpm": "8" } } diff --git a/packages/types/types/response.ts b/packages/types/types/response.ts index 21814103..7cbf0df5 100644 --- a/packages/types/types/response.ts +++ b/packages/types/types/response.ts @@ -25,6 +25,8 @@ export type Response = BaseResponse & { * The payload returned by the REST API. */ payload: T; + + success: true; }; /** @@ -39,6 +41,8 @@ export type ErrorResponse = BaseResponse & { * The detailed error message. */ message: string; + + success: false; }; /** @@ -54,4 +58,4 @@ export type RawResponse = Response | ErrorResponse; * ``ErrorResponse`` or a ``Response``. * @param r The object to test. */ -export const isErrorResponse = (r: RawResponse): r is ErrorResponse => "error" in r; +export const isErrorResponse = (r: RawResponse): r is ErrorResponse => r.success == false; diff --git a/packages/websoc-fuzzy-search/setup.ts b/packages/websoc-fuzzy-search/setup.ts index 358782dc..991204c7 100644 --- a/packages/websoc-fuzzy-search/setup.ts +++ b/packages/websoc-fuzzy-search/setup.ts @@ -2,7 +2,6 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { normalize } from "node:path"; import { gzipSync } from "node:zlib"; -import { isErrorResponse } from "@peterportal-api/types"; import type { Course, Instructor, RawResponse } from "@peterportal-api/types"; import fetch from "cross-fetch"; import pluralize from "pluralize"; @@ -219,7 +218,7 @@ async function main() { console.time("Data fetched in"); const coursesRes = await fetch("https://api-next.peterportal.org/v1/rest/courses/all"); const coursesJson: RawResponse = await coursesRes.json(); - if (isErrorResponse(coursesJson)) throw new Error("Could not fetch courses from API."); + if (!coursesJson.success) throw new Error("Could not fetch courses from API."); coursesJson.payload.forEach( ({ id, department, departmentName, courseNumber, geList, courseLevel, school, title }) => { d.courses[id] = { @@ -235,7 +234,7 @@ async function main() { ); const instructorsRes = await fetch("https://api-next.peterportal.org/v1/rest/instructors/all"); const instructorsJson: RawResponse = await instructorsRes.json(); - if (isErrorResponse(instructorsJson)) throw new Error("Could not fetch instructors from API."); + if (!instructorsJson.success) throw new Error("Could not fetch instructors from API."); instructorsJson.payload.forEach(({ ucinetid, shortenedName, name, schools, department }) => { d.instructors[ucinetid] = { ucinetid,