-
Notifications
You must be signed in to change notification settings - Fork 7
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
Open
ExampleWasTaken
wants to merge
33
commits into
flybywiresim:staging
Choose a base branch
from
ExampleWasTaken:feat/generic-api-wrapper
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: generic wrapper for JSON APIs #70
ExampleWasTaken
wants to merge
33
commits into
flybywiresim:staging
from
ExampleWasTaken:feat/generic-api-wrapper
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I like this in general, but needs a deeper review than I can do right now :) |
benw202
approved these changes
Jul 18, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, will wait on a second review from @pdellaert on this one
ExampleWasTaken
force-pushed
the
feat/generic-api-wrapper
branch
from
July 28, 2024 15:47
bca310f
to
01e424d
Compare
pdellaert
reviewed
Jul 28, 2024
ExampleWasTaken
force-pushed
the
feat/generic-api-wrapper
branch
from
August 7, 2024 17:06
01e424d
to
dec8382
Compare
# Conflicts: # .github/CHANGELOG.md
ExampleWasTaken
force-pushed
the
feat/generic-api-wrapper
branch
from
August 8, 2024 17:22
dec8382
to
aa84fa9
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Using 3rd-party APIs in statically typed languages poses a challenge as you need some way to validate the type of the returned data to avoid type errors at runtime. So far, the codebase used TypeScript's
any
type on responses from 3rd-party APIs to access values on the returned data. Asany
basically means opting out of all type checking, it's best practice to avoid its usage wherever possible. Therefore, the upcoming lint overhaul will disallowany
completely.Refactoring all currently used API implementations to avoid using
any
is too complex to be included in the lint overhaul.This PR is a mix of
feat
andrefactor
: It introduces a new generic API wrapper and migrates all existing 3rd-party APIs to use it.Description
The API wrapper uses the 3rd-party library Zod to validate the returned data against statically declared schemas. It leverages TypeScript's ability to implicitly infer types thereby enabling type checking on the returned data. Code completion on the returned data is another QoL feature added by this PR/library.
Basic usage
Details
Using the wrapper is as simple as declaring a Zod schema for the returned data:
Then using the
fetchForeignAPI<ReturnType>(request, schema)
method:For simplicity you can also just pass a
URL
object orstring
tofetchForeignAPI()
.Testing
A list of all modified commands can be found below.
While testing make sure to use improbable inputs as well as more typical inputs. E.g. requesting station/taf/metar information from small airports, airports with almost no traffic etc.
Consider any case where the command doesn't succeed as 'failed'. The goal is to get correct typing, not limit the data we can show/use.
Affected Commands
/vatsim data <all_subcommands>
/vatsim events
/station
/taf
/metar
/live-flights
/simbrief-data retrieve
/wolframalpha
Additional Information
The
/taf
embed has been slightly modified to be more consistent. Instead of decoding the first part of the TAF only, it now only provides the raw string and links to our docs and https://e6bx.com/weather/{ICAO}/?showDecoded=1&focuspoint=tafdecoder which decodes the TAF for the queried airport.Discord Username
examplewastaken