1
- import axios , { AxiosRequestConfig } from 'axios'
2
- import { err , ok , Result } from 'rusty-result-ts'
3
- import { GetAPIBaseURLConfig } from '../config'
4
- import { ApiErrorBase } from './base.error'
5
- import FormData from 'form-data'
1
+ import axios , { AxiosRequestConfig , AxiosResponse } from 'axios' ;
2
+ import { err , ok , Result } from 'rusty-result-ts' ;
3
+ import { GetAPIBaseURLConfig } from '../config' ;
4
+ import { ApiErrorBase } from './base.error' ;
5
+ import FormData from 'form-data' ;
6
6
7
- const apiBase = GetAPIBaseURLConfig ( )
7
+ const apiBase = GetAPIBaseURLConfig ( ) ;
8
8
9
9
export class ApiServiceBase {
10
- protected urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase
10
+ protected urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase ;
11
11
12
12
/**
13
13
* Creates a new service instance.
14
14
* @param path A base path for all requests this service will make. Defaults to `/api`.
15
15
*/
16
16
public constructor ( ) {
17
- this . urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase
17
+ this . urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase ;
18
18
}
19
19
20
20
/**
21
21
* Returns a new instance of the base config for all requests this service makes.
22
22
* @protected
23
23
*/
24
- protected getConfig ( sessionToken ?: string , formDataHeaders ?: FormData . Headers ) : AxiosRequestConfig {
24
+ protected getConfig (
25
+ sessionToken ?: string ,
26
+ formDataHeaders ?: FormData . Headers ,
27
+ ) : AxiosRequestConfig {
25
28
const config : AxiosRequestConfig = {
26
29
headers : {
27
30
Accept : 'application/json' ,
28
31
'Content-Type' : 'application/json' ,
29
- 'Access-Control-Allow-Origin' : '*'
30
- }
31
- }
32
+ 'Access-Control-Allow-Origin' : '*' ,
33
+ } ,
34
+ } ;
32
35
33
36
if ( sessionToken && config . headers ) {
34
- config . headers . Authorization = `Bearer ${ sessionToken } `
37
+ config . headers . Authorization = `Bearer ${ sessionToken } ` ;
35
38
}
36
39
37
40
if ( formDataHeaders && config . headers ) {
38
41
config . headers = {
39
42
...config . headers ,
40
- ...formDataHeaders
41
- }
43
+ ...formDataHeaders ,
44
+ } ;
42
45
}
43
46
44
- return config
47
+ return config ;
45
48
}
46
49
47
50
/**
@@ -50,10 +53,13 @@ export class ApiServiceBase {
50
53
* @param configOverrides A config object to merge onto the base config.
51
54
* @protected
52
55
*/
53
- protected async get < T > ( path = '' , configOverrides : AxiosRequestConfig ) : Promise < Result < T | null , ApiErrorBase > > {
56
+ protected async get < T > (
57
+ path = '' ,
58
+ configOverrides : AxiosRequestConfig ,
59
+ ) : Promise < Result < T | null , ApiErrorBase > > {
54
60
return await this . requestResultWrapper < T > ( path , configOverrides , ( fullPath , config ) => {
55
- return axios . get ( fullPath , config )
56
- } )
61
+ return axios . get ( fullPath , config ) ;
62
+ } ) ;
57
63
}
58
64
59
65
/**
@@ -66,11 +72,11 @@ export class ApiServiceBase {
66
72
protected async post < T > (
67
73
path = '' ,
68
74
data : unknown = undefined ,
69
- configOverrides : AxiosRequestConfig
75
+ configOverrides : AxiosRequestConfig ,
70
76
) : Promise < Result < T | null , ApiErrorBase > > {
71
77
return await this . requestResultWrapper < T > ( path , configOverrides , ( fullPath , config ) => {
72
- return axios . post ( fullPath , data , config )
73
- } )
78
+ return axios . post ( fullPath , data , config ) ;
79
+ } ) ;
74
80
}
75
81
76
82
/**
@@ -83,11 +89,11 @@ export class ApiServiceBase {
83
89
protected async put < T > (
84
90
path = '' ,
85
91
data : unknown = undefined ,
86
- configOverrides : AxiosRequestConfig
92
+ configOverrides : AxiosRequestConfig ,
87
93
) : Promise < Result < T | null , ApiErrorBase > > {
88
94
return await this . requestResultWrapper < T > ( path , configOverrides , ( fullPath , config ) => {
89
- return axios . put ( fullPath , data , config )
90
- } )
95
+ return axios . put ( fullPath , data , config ) ;
96
+ } ) ;
91
97
}
92
98
93
99
/**
@@ -100,11 +106,11 @@ export class ApiServiceBase {
100
106
protected async patch < T > (
101
107
path = '' ,
102
108
data : unknown = undefined ,
103
- configOverrides : AxiosRequestConfig
109
+ configOverrides : AxiosRequestConfig ,
104
110
) : Promise < Result < T | null , ApiErrorBase > > {
105
111
return await this . requestResultWrapper < T > ( path , configOverrides , ( fullPath , config ) => {
106
- return axios . patch ( fullPath , data , config )
107
- } )
112
+ return axios . patch ( fullPath , data , config ) ;
113
+ } ) ;
108
114
}
109
115
110
116
/**
@@ -113,25 +119,38 @@ export class ApiServiceBase {
113
119
* @param configOverrides A config object to merge onto the base config.
114
120
* @protected
115
121
*/
116
- protected async delete < T > ( path = '' , configOverrides : AxiosRequestConfig ) : Promise < Result < T | null , ApiErrorBase > > {
122
+ protected async delete < T > (
123
+ path = '' ,
124
+ configOverrides : AxiosRequestConfig ,
125
+ ) : Promise < Result < T | null , ApiErrorBase > > {
117
126
return await this . requestResultWrapper < T > ( path , configOverrides , ( fullPath , config ) => {
118
- return axios . delete ( fullPath , config )
119
- } )
127
+ return axios . delete ( fullPath , config ) ;
128
+ } ) ;
120
129
}
121
130
122
131
private async requestResultWrapper < T > (
123
132
subPath : string ,
124
133
config : AxiosRequestConfig ,
125
- request : ( fullPath : string , config : AxiosRequestConfig | undefined ) => Promise < { data : unknown } | null >
134
+ request : (
135
+ fullPath : string ,
136
+ config : AxiosRequestConfig | undefined ,
137
+ ) => Promise < AxiosResponse < T > > ,
126
138
) : Promise < Result < T | null , ApiErrorBase > > {
127
139
if ( subPath . length > 0 && subPath [ 0 ] !== '/' ) {
128
- subPath = `/${ subPath } `
140
+ subPath = `/${ subPath } ` ;
129
141
}
130
142
try {
131
- const responseData : T | null = ( ( await request ( `${ this . urlBase } ${ subPath } ` , config ) ) ?. data as T ) ?? null
132
- return ok ( responseData )
143
+ const response = ( await request ( `${ this . urlBase } ${ subPath } ` , config ) ) ?? null ;
144
+ const status = response ?. status ;
145
+ const responseData = response . data as T ;
146
+ return ok ( {
147
+ ...responseData ,
148
+ httpConfig : {
149
+ status,
150
+ } ,
151
+ } ) ;
133
152
} catch ( e : unknown ) {
134
- return err ( new ApiErrorBase ( e ) )
153
+ return err ( new ApiErrorBase ( e ) ) ;
135
154
}
136
155
}
137
156
}
0 commit comments