11import axios , { AxiosError , AxiosInstance , AxiosResponse } from "axios" ;
2- import { AuthStrategy } from "./types " ;
2+ import { prepare_client , FaableClientConfig } from "./client " ;
33export interface FaableApp {
44 id : string ;
55 name : string ;
@@ -20,7 +20,11 @@ const wrap_error = async <T>(prom: Promise<AxiosResponse<T>>): Promise<T> => {
2020 const e : AxiosError < { message : string } > = error ;
2121 if ( e . isAxiosError ) {
2222 const res = e . response ;
23- throw new Error ( `API Error: ${ res . data . message } ` ) ;
23+ if ( res ) {
24+ throw new Error ( `API Error ${ res . status } : ${ res ?. data . message } ` ) ;
25+ } else {
26+ throw new Error ( `API Error:${ e . message } ` ) ;
27+ }
2428 }
2529 throw error ;
2630 }
@@ -35,37 +39,17 @@ const paginate = async <Q extends Promise<Page<T>>, T>(
3539 return items ;
3640} ;
3741
38- type FaableApiConfig < T = any > = {
39- authStrategy ?: AuthStrategy < T > ;
40- auth ?: T ;
41- } ;
42- export class FaableAppsApi {
42+ type FaableApiConfig < T > = { } & FaableClientConfig < T > ;
43+
44+ export class FaableApi < T = any > {
4345 client : AxiosInstance ;
4446
45- constructor ( config : FaableApiConfig ) {
46- const { authStrategy, auth } = config ;
47- const strategy = authStrategy && authStrategy ( auth ) ;
48- this . client = axios . create ( {
49- baseURL : "https://api.faable.com" ,
50- } ) ;
51- this . client . interceptors . request . use (
52- async function ( config ) {
53- // Do something before request is sent
54- const headers = strategy ? await strategy . headers ( ) : { } ;
55- config . headers . set ( headers ) ;
56- // console.log("all headers");
57- // console.log(headers);
58- return config ;
59- } ,
60- function ( error ) {
61- // Do something with request error
62- return Promise . reject ( error ) ;
63- }
64- ) ;
47+ constructor ( config : FaableApiConfig < T > ) {
48+ this . client = prepare_client ( config ) ;
6549 }
6650
67- static create ( config : FaableApiConfig = { } ) {
68- return new FaableAppsApi ( config ) ;
51+ static create < T > ( config : FaableApiConfig < T > = { } ) {
52+ return new FaableApi ( config ) ;
6953 }
7054
7155 async list ( ) {
0 commit comments