-
Notifications
You must be signed in to change notification settings - Fork 186
[jslib] Refactored Acknowledgements with Enhanced HTTP Utilities #9999
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
base: main
Are you sure you want to change the base?
Conversation
$factory->config(), | ||
[ | ||
__DIR__ . "/../project/", | ||
__DIR__ . "/../project/modules", |
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.
needed to get things to load...
fallback: { | ||
fs: false, | ||
path: false, | ||
stream: require.resolve("stream-browserify"), |
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.
npm compile was giving errors without this
"jstat": "^1.9.5", | ||
"jszip": "^3.10.1", | ||
"papaparse": "^5.3.0", | ||
"pbkdf2": "^3.1.3", |
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.
npm compile was giving errors without these
jslib/core/http/Client.ts
Outdated
export class Client<T> { | ||
protected baseUrl: string; | ||
protected subEndpoint?: string; | ||
public getCustomMessage: ( |
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.
this is to allow each Client concrete class to define it's own errors across all the error types, not sure if it's the best implementation, but it seems to work — maybe it just needs a better name. return type be something like Record<string, string>
@driusan @ridz1208 would love your thoughts on this! Hoping to extend this to more parts of loris after and add more entities. I'm also curious what you think of the directory structure I've started to build out, as it will likely get expanded. Should everything live in jslib? I'm thinking the top level components that currently live in jsx/ should also get moved into what ever the top level javascript directory is and live under a |
jslib/core/errors/BaseError.ts
Outdated
* Base class for all custom API-related errors. | ||
*/ | ||
export class BaseError extends Error { | ||
public name: 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.
need to delete this since name is already a property of Error
@kongtiaowang could you help me with these errors?
|
@HenriRabalais I will handle it. |
I made some changes in 3b59957 |
Thanks @kongtiaowang ! @driusan do you have time to take a look at this? Otherwise, can I assign it to someone else? |
@skarya22 @marandmart I assigned the both of you here because its a JSX heavy PR but also brongs some nice additions to what we already have. This is not exactly a critical priority but definitely a nice have so if you ca take a bit of time here and there to review it it would be perfect. We always talk baout improving our react side and this is doing just that. |
@@ -0,0 +1,14 @@ | |||
import {Acknowledgement} from '../../'; | |||
import {Http} from 'jsx/../jslib/core'; |
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.
If there isn't a specific reason for this import being done like this, change to /jslib/core
|
||
// 1. Handle HTTP status errors (e.g., 404, 500) | ||
if (!response.ok) { | ||
const message = this.getMessage(response.status, request, response); |
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.
Shouldn't the first value being passed here be 'ApiResponseError' instead of response.status? The response object is already being passed as the third parameter, therefore it would be more useful to have the type of error like the examples bellow, I think
/** | ||
* Function to retrieve a custom error message for a given error context. | ||
*/ | ||
public getMessage: ( |
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.
Since this method handles error messages, I think its name should make that purpose more immediately clear
* @param baseUrl The base URL for the API requests. | ||
*/ | ||
constructor(baseUrl: string) { | ||
this.baseUrl = loris.BaseURL+'/'+baseUrl+'/'; |
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.
Instead of concatenating strings to build the URL, I recommend using the URL object for cleaner and more reliable construction
case Operator.GreaterThan: return '>'; | ||
case Operator.LessThanOrEqual: return '<='; | ||
case Operator.GreaterThanOrEqual: return '>='; | ||
case Operator.Like: return '_like'; |
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.
The Operator.Includes case is missing
// Handle responses with no content | ||
const contentType = response.headers.get('content-type'); | ||
if (!contentType || !contentType.includes('application/json')) { | ||
return null as U; |
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.
Instead of typing null as U, return null directly and include it in the return type. This way, it is required to handle the null case instead of always expecting an object of type U
protected async fetchJSON<U>(url: string, options: RequestInit): Promise<U> { | ||
const request = new Request(url, options); | ||
try { | ||
const response = await fetch(url, options); |
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.
Doesn't really change much, but it looks cleaner to use the request const from line 121 here instead of passing url and options again
This PR refactors the acknowledgements page to use a new, more robust and reusable API client and a suite of custom error classes. This change modernizes our data fetching approach by moving away from raw fetch requests and adopting a structured, type-safe methodology.
Key Changes
Introduced a new Core Library: Added a new directory at jslib/core containing:
Introduced a Entities Library: Added a new directory at jslib/entities containing:
Updated Acknowledgements Module
Misc