-
Notifications
You must be signed in to change notification settings - Fork 14
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(utils): Add support for fetching remote JSON objects. #178
Changes from 8 commits
8b955cb
56c6f94
8f1ba6c
5f2c317
bff0feb
34d8496
48ca6d2
22bfd34
7c29dd4
cf71755
ff6c68c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -30,6 +30,29 @@ const convertAxiosError = (e: AxiosError): Error => { | |||||||||||||||||
); | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Retrieves an object that is stored as JSON in a remote location. | ||||||||||||||||||
* If the response is not an object, the response is returned as a string. | ||||||||||||||||||
* | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix inconsistency in JSDoc documentation. The JSDoc comment states "If the response is not an object, the response is returned as a string", but the function signature Apply this diff to fix the documentation: /**
* Retrieves an object that is stored as JSON in a remote location.
- * If the response is not an object, the response is returned as a string.
+ * The response is parsed as JSON and cast to type T. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||
* @param remoteUrl | ||||||||||||||||||
* @return The parsed JSON object. | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the original docstring is more clear There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it returns a string, it is not an object. While JSON is more like json value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure. how about: @ return The parsed response. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
* @throws {Error} if the download fails. | ||||||||||||||||||
*/ | ||||||||||||||||||
const getJsonObjectFrom = async <T>(remoteUrl: string) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you try and replace type T, with Something like. Did this not work?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, this is to supply type information for the
instead of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better as JsonValue. It restricts the type more appropriately than T. Also you dont really know if string[], until validated. I think you can write
Or add a type predicate isStringArray() if you really want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, that works |
||||||||||||||||||
: Promise<T> => { | ||||||||||||||||||
try { | ||||||||||||||||||
const {data} = await axios.get<T>(remoteUrl, { | ||||||||||||||||||
responseType: "json", | ||||||||||||||||||
}); | ||||||||||||||||||
|
||||||||||||||||||
return data; | ||||||||||||||||||
} catch (e) { | ||||||||||||||||||
throw (e instanceof AxiosError) ? | ||||||||||||||||||
convertAxiosError(e) : | ||||||||||||||||||
e; | ||||||||||||||||||
} | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Downloads (bypassing any caching) a file as a Uint8Array. | ||||||||||||||||||
* | ||||||||||||||||||
|
@@ -57,5 +80,7 @@ const getUint8ArrayFrom = async (fileUrl: string) | |||||||||||||||||
} | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
export {getUint8ArrayFrom}; | ||||||||||||||||||
export { | ||||||||||||||||||
getJsonObjectFrom, | ||||||||||||||||||
getUint8ArrayFrom, | ||||||||||||||||||
}; |
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.
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.
I do think it's worth mentioning a string would be returned for any non JSON response body, rather than anything thrown (especially now we don't have test cases to show the behavior). Shall we keep the last sentence?
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.
Okay we can leave it.
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.
How about:
Downloads and parses JSON from remote URL. If the HTTP response body is not JSON, the body is gracefully returned as a string.
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.
Does it really parse? or does it just return a massive string?
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.
Anyways I think your comment is fine, if it returns a js object
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.
I look at your later PR, it looks like it parses. I think your comment is fine