1- import { ToastAndroid } from 'react-native' ;
2- import { Credentials } from '../store/settings' ;
1+ import { Buffer } from 'buffer' ;
2+ import { ToastAndroid } from 'react-native' ;
3+ import { Credentials } from '../store/settings' ;
4+
5+ export const authorizationHeader : ( credentials : Credentials | null ) => {
6+ Authorization ?: string ;
7+ } = credentials =>
8+ credentials !== null
9+ ? {
10+ Authorization : `Basic ${ Buffer . from (
11+ `${ credentials . username } :${ credentials . password } ` ,
12+ ) . toString ( 'base64' ) } `,
13+ }
14+ : { } ;
315
416export const get = async < T > (
517 endpoint : string ,
@@ -12,7 +24,7 @@ export const get = async <T>(
1224 `${ endpoint } ${ queryParams ? `?${ new URLSearchParams ( queryParams ) } ` : '' } ` ,
1325 {
1426 headers : {
15- Authorization : credentials !== null ? `Basic ${ Buffer . from ( ` ${ credentials . username } : ${ credentials . password } ` ) . toString ( 'base64' ) } ` : undefined ,
27+ ... authorizationHeader ( credentials ) ,
1628 } ,
1729 } ,
1830 ) . then ( res => ( json === false ? res . text ( ) : res . json ( ) ) ) ;
@@ -35,7 +47,7 @@ export const post = async <T>(
3547 {
3648 method : 'post' ,
3749 headers : {
38- Authorization : credentials !== null ? `Basic ${ Buffer . from ( ` ${ credentials . username } : ${ credentials . password } ` ) . toString ( 'base64' ) } ` : undefined ,
50+ ... authorizationHeader ( credentials ) ,
3951 } ,
4052 } ,
4153 ) . then ( res => ( json === false ? res . text ( ) : res . json ( ) ) ) ;
@@ -58,7 +70,7 @@ export const del = async <T>(
5870 {
5971 method : 'delete' ,
6072 headers : {
61- Authorization : credentials !== null ? `Basic ${ Buffer . from ( ` ${ credentials . username } : ${ credentials . password } ` ) . toString ( 'base64' ) } ` : undefined ,
73+ ... authorizationHeader ( credentials ) ,
6274 } ,
6375 } ,
6476 ) . then ( res => ( json === false ? res . text ( ) : res . json ( ) ) ) ;
0 commit comments