auth()
함수 시그니처
auth: (token: string, type?: DenchAuthType) => R
관련 Header 속성
기본 인증 방식:
{
Authorization: `Bearer ${token}`
}
인증 방식을 지정한 경우:
{
Authorization: `${type} ${token}`
}
설명
현재 요청의 Authorization 헤더를 설정합니다. type을 생략하면 DenchAuthType.BEARER가 사용됩니다.
지원되는 DenchAuthType 값은 다음과 같습니다.
DenchAuthType.ETC
DenchAuthType.BASIC
DenchAuthType.BEARER
DenchAuthType.DIGEST
DenchAuthType.NEGOTIATE
DenchAuthType.AWS4_HMAC_SHA256
DenchAuthType.API_KEY
DenchAuthType.TOKEN
DenchAuthType.HMAC
예제
import { dench } from 'dench-fetch';
const api = dench('https://api.example.com');
const profile = await api
.get('/profile')
.auth('access-token')
.toJson();
실제 RequestInit과 Header는 다음과 같습니다.
{
method: 'GET',
headers: {
Authorization: 'Bearer access-token',
},
}
다른 인증 방식을 지정할 수도 있습니다.
import { DenchAuthType, dench } from 'dench-fetch';
const request = dench('https://api.example.com')
.get('/secure')
.auth('encoded-credentials', DenchAuthType.BASIC);
{
method: 'GET',
headers: {
Authorization: 'Basic encoded-credentials',
},
}
주의사항
DenchAuthType.ETC를 사용하면 접두사를 붙이지 않고token을 그대로Authorization헤더 값으로 사용합니다.- 기존
Authorization헤더가 있으면 새 값으로 덮어씁니다. auth()는 토큰을 인코딩하거나 인증 서명을 생성하지 않습니다.