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

Please allow for a callback in the $.request "header" function #301

Open
jcalfee opened this issue Jan 25, 2025 · 1 comment
Open

Please allow for a callback in the $.request "header" function #301

jcalfee opened this issue Jan 25, 2025 · 1 comment

Comments

@jcalfee
Copy link

jcalfee commented Jan 25, 2025

https://github.com/dsherret/dax/tree/6aed9b02ccf4debeeb0581f7f576468721631260?tab=readme-ov-file#making-requests

Would you consider accepting a callback for the header in the $.request API? If you promises? The example below would be ideal. The callback would allow me to return the request object for more manipulations without having to make the entire method async. It really only needs to be async when the call chain gets to some method that will make the request, like .json() or .text() which is when callbacks, like the one below, can get resolved.

export function myApiRequest(path: string) {
  return $.request(baseUrl + path).
    header("Authorization", async ()=> "Bearer " + await getAccessToken()). // please support [async] callback
    header('Content-Type', 'application/json')
}

It is not just convenience, the access token may have expired by the time the caller actually gets around to making the request. With the callback, getAccessToken can re-new the token without interrupting the work-flow.

@jcalfee
Copy link
Author

jcalfee commented Jan 25, 2025

Alternatively, this should cover more ground and require less API changes in dax:

export function myApiRequest(path: string) {
  return $.request(baseUrl + path).
    header('Content-Type', 'application/json').
    beforeRequest(async builder=> {
        return builder.header("Authorization", "Bearer " + await getAccessToken())
    })// returns RequestBuilder
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant