import type {
RawResponse,
WebsocAPIResponse,
} from "peterportal-api-next-types";
import { isErrorResponse } from "peterportal-api-next-types";
try {
const res = await fetch(
"https://api-next.peterportal.org/v1/rest/websoc" +
new URLSearchParams({
year: "2023",
quarter: "Spring",
department: "COMPSCI",
}),
{
headers: {
Referer: "https://docs.api-next.peterportal.org",
},
},
);
const json: RawResponse<WebsocAPIResponse> = await res.json();
// These fields are always available regardless of whether the request
// succeeded or failed.
console.log(json.timestamp);
console.log(json.requestId);
console.log(json.statusCode);
if (isErrorResponse(json)) {
// If the request failed, the error message and details will be logged to
// standard error.
console.error(json.error);
console.error(json.message);
} else {
// If the request was successful, all courses in the COMPSCI department for
// Spring 2023 will be logged to standard output.
// If you are using an editor that supports intelligent code completion,
// typing json.payload.s in this block should show the `schools` array of
// the WebSoc API response type.
console.log(json.payload);
}
} catch (e) {
// If fetch itself failed, the error message will be logged to standard error.
console.error(e);
}