aspida / aspida

TypeScript friendly HTTP client wrapper for the browser and node.js.

Home Page:https://github.com/aspida/aspida

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to pass headers (e.g. Authorization)

memen45 opened this issue · comments

const client = api(aspida())
const {data, error} = useAspidaSWRV(client.V1.products, {
        cache: new LocalStorageCache('swrv'),
        shouldRetryOnError: false
})

How can a custom header be passed to the request (ideally without invalidation of cache)?

I have a Swagger 2.0 specification of the API. The openapi2aspida script works perfectly. I added a securityDefinition:

"securityDefinitions":{
    "bearerAuth":{
        "name":"Authorization",
        "in":"header",
        "type":"apiKey",
        "description":"Authorization using the Bearer token from admin/integration/token"
    }
},
"security": [{
    "bearerAuth":[]
}],

However, I do not notice any change with or without this definition. How could it be implemented correctly?

Okay, apparently, simply adding the headers: {} key works. Except Visual Studio Code intellisense does not like it and reports a type error on it Argument of type {...} is not assignable to parameter of type {...}

const client = api(aspida())
const {data, error} = useAspidaSWRV(client.V1.products, {
        headers: { 'Authorization': 'Bearer ' + token,
        cache: new LocalStorageCache('swrv'),
        shouldRetryOnError: false
})

Is there a way to manage authorization headers (swagger securityDefinitions) on API level, or would that make this issue a feature request?