ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.58k stars 1.33k forks source link

[feat] Configuring request header before specifying the method #1765

Open Brunoaqu opened 1 year ago

Brunoaqu commented 1 year ago

Configuring request header before specifying the method, or creating agent instances that keep memory of what we configured him with.

How I would like Superagent to be:

const superagent = require('superagent')

const agent = superagent(app).createAgent()

agent.set({
    accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
    refreshToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
})

agent.get('/currentUser')

Checklist

ghost commented 1 year ago

i belive you can do something like this:

const superagent = require('superagent')

const request = superagent
  .agent(app)
  .set({ Accept: 'applicaion/json', 'x-custom': 'custom' });

const response = await request.get('/my/url');