Skip to content

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

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

Open
2 tasks done
felixmosh opened this issue Feb 6, 2025 · 1 comment
Open
2 tasks done

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

felixmosh opened this issue Feb 6, 2025 · 1 comment

Comments

@felixmosh
Copy link

felixmosh commented Feb 6, 2025

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 assigning 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.
@mcollina
Copy link
Member

Something that would be interesting for this module to support is:

const inject = bindInject(app, { headers: { authorization: 'Bearer ${payload.accessToken}' } });

const addAdminResponse = inject({method: 'post', url: '/admin/admins/add', body: { ... }}); // this request requires auth token header.

It's a useful feature

This same functionality could also exposed in Fastify too as:

const inject = app.bindInject({ headers: { authorization: 'Bearer ${payload.accessToken}' } });

const addAdminResponse = 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

2 participants