Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generic wrapper for JSON APIs #70

Draft
wants to merge 36 commits into
base: staging
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
759758a
add fetchData
ExampleWasTaken Jun 25, 2024
c15b315
add vatsim-event endpoint types
ExampleWasTaken Jun 25, 2024
211579d
add util type guards
ExampleWasTaken Jun 25, 2024
684ca62
migrate vatsim events to the new api wrapper
ExampleWasTaken Jun 25, 2024
7ab2383
add vatsim data endpoint types
ExampleWasTaken Jun 25, 2024
bd67559
add zod library
ExampleWasTaken Jul 1, 2024
bd0b412
partial vatsim port to zod
ExampleWasTaken Jul 1, 2024
0f7ec61
migrate vatsim observer to zod
ExampleWasTaken Jul 2, 2024
992c700
add missing flight plan type export
ExampleWasTaken Jul 2, 2024
81b9045
migrate vatsim pilot to zod
ExampleWasTaken Jul 2, 2024
7b37d6e
migrate vatsim stats to zod
ExampleWasTaken Jul 2, 2024
7062fc0
fix lint in vatsim
ExampleWasTaken Jul 2, 2024
5e2a3b7
migrate vatsimEvents
ExampleWasTaken Jul 2, 2024
33e3219
improve var naming
ExampleWasTaken Jul 2, 2024
7a0c89b
refactor fetchData
ExampleWasTaken Jul 2, 2024
31c2ea3
remove any ewww
ExampleWasTaken Jul 2, 2024
540557a
migrate taf cmd to zod
ExampleWasTaken Jul 3, 2024
0bffea0
migrate metar cmd to zod
ExampleWasTaken Jul 3, 2024
5c1299b
add disclaimer comments to taf and metar schemas
ExampleWasTaken Jul 3, 2024
f24dd5c
migrate simbrief cmd to zod
ExampleWasTaken Jul 3, 2024
50d08dc
migrate wolfram alpha cmd to zod
ExampleWasTaken Jul 3, 2024
0044d3c
fix zod error being thrown incorrectly while fetching data
ExampleWasTaken Jul 3, 2024
d1a8704
add folder structure to zod schema directory
ExampleWasTaken Jul 3, 2024
c841fea
migrate station cmd to zod
ExampleWasTaken Jul 4, 2024
698074e
migrate live-flight cmd to zod
ExampleWasTaken Jul 4, 2024
eae9c22
rename fetchData to fetchForeignAPI
ExampleWasTaken Jul 4, 2024
03adb0d
improve fetchForeignAPI zod error logging
ExampleWasTaken Jul 4, 2024
c01ad7a
allow string and url when fetching from foreign apis
ExampleWasTaken Jul 4, 2024
6647032
use string instead of request object for foreign apis
ExampleWasTaken Jul 4, 2024
0b2de9e
add changelog
ExampleWasTaken Jul 28, 2024
a5b363e
self review
ExampleWasTaken Jul 4, 2024
aa84fa9
update simbrief zod schema
ExampleWasTaken Aug 8, 2024
7e46e6b
remove console
ExampleWasTaken Aug 26, 2024
e4eca0d
Merge branch 'staging' into feat/generic-api-wrapper
ExampleWasTaken Oct 27, 2024
256b6db
update changelog
ExampleWasTaken Oct 27, 2024
777c5b6
Merge branch 'staging' into feat/generic-api-wrapper
ExampleWasTaken Nov 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
allow string and url when fetching from foreign apis
ExampleWasTaken committed Aug 8, 2024

Verified

This commit was signed with the committer’s verified signature.
ExampleWasTaken ExampleWasTaken
commit c01ad7a0af2ccd0d6e80c826746b83fced8a7aa3
14 changes: 8 additions & 6 deletions src/lib/apis/fetchForeignAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch, { Request, Response } from 'node-fetch';
import fetch, { Request, RequestInfo, Response } from 'node-fetch';
import { ZodSchema } from 'zod';
import { Logger } from '../logger';

@@ -9,12 +9,14 @@ import { Logger } from '../logger';
* @param zodSchema The [Zod](https://github.com/colinhacks/zod) schema that the returned data conforms to. The promise will reject if the returned data does not conform to the schema provided.
* @returns A promise that resolves to the expected type or rejects with an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error).
*/
export const fetchForeignAPI = async <ReturnType = unknown>(request: Request, zodSchema: ZodSchema<ReturnType>, debug?: boolean): Promise<ReturnType> => {
export const fetchForeignAPI = async <ReturnType = unknown>(request: RequestInfo, zodSchema: ZodSchema<ReturnType>, debug?: boolean): Promise<ReturnType> => {
const req = new Request(request);

let response: Response;
try {
response = await fetch(request);
response = await fetch(req);
} catch (e) {
throw new Error(`An error occured while fetching data from ${request.url}: ${String(e)}`);
throw new Error(`An error occured while fetching data from ${req.url}: ${String(e)}`);
}

if (!response.ok) {
@@ -25,14 +27,14 @@ export const fetchForeignAPI = async <ReturnType = unknown>(request: Request, zo
try {
data = await response.json();
} catch (e) {
throw new Error(`Could not parse JSON. Make sure the endpoint at ${request.url} returns valid JSON. Error: ${String(e)}`);
throw new Error(`Could not parse JSON. Make sure the endpoint at ${req.url} returns valid JSON. Error: ${String(e)}`);
}

const result = zodSchema.safeParse(data);

if (!result.success) {
Logger.error("[zod] Data validation failed! Pass the 'debug' flag to 'fetchForeignAPI()' to print the retrieved data to the console.");
Logger.error(`Endpoint location: ${request.url}.`);
Logger.error(`Endpoint location: ${req.url}.`);
if (debug) {
// winston doesn't usefully log object at the moment
// eslint-disable-next-line no-console