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

A way to set defaults of headers / query params #336

Open
2 tasks done
felixmosh opened this issue Feb 6, 2025 · 0 comments
Open
2 tasks done

A way to set defaults of headers / query params #336

felixmosh opened this issue Feb 6, 2025 · 0 comments

Comments

@felixmosh
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

app.inject().setDefaults({headers: {auth: 'Beaere MY_ACCESS_TOKEN'}})

Motivation

I want to simplify the following flow,

  1. login
  2. make auth required request

currenly I'm saving the accessToken from the login response, and then attaching it to each one of the following request.
If there was some method of assining a default header value, it can be used after login.

Example

const response = await app.inject({ method: 'post', url: '/admin/auth/login', body });
const { payload, errors } = response.json<{ payload: IAuthToken; errors: string[] }>();
expect(response.statusCode).toEqual(200);
expect(errors).toHaveLength(0);

if (payload) {
  app.inject().setDefaults({ headers: { authorization: 'Bearer ${payload.accessToken}' } });
}

const addAdminResponse = await app.inject({method: 'post', url: '/admin/admins/add', body: { ... }}); // this request requires auth token header.
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