@@ -2,98 +2,6 @@ import {ClientBuilder, ClientBuilderOptions, Headers, PureRoute} from "../types/
2
2
import { Client } from "../types/client" ;
3
3
import { BaseRequest , BaseResult , Request , Result } from "../types/http" ;
4
4
5
- function createRequest ( base : BaseRequest ) : Request {
6
- return {
7
- ...base ,
8
- addHeaders ( headers : Headers ) {
9
- this . headers = {
10
- ...this . headers ,
11
- ...headers
12
- }
13
- } ,
14
- setHeaders ( headers : Headers ) {
15
- this . headers = headers
16
- } ,
17
- addQueryParameters ( queryParameters : { [ key : string ] : string | number | boolean } ) {
18
- this . queryParameters = {
19
- ...this . queryParameters ,
20
- ...queryParameters
21
- }
22
- } ,
23
- setQueryParameters ( queryParameters : { [ key : string ] : string | number | boolean } ) {
24
- this . queryParameters = queryParameters
25
- } ,
26
- setBody ( body : any ) {
27
- this . body = body
28
- } ,
29
- setEncoder ( encoder : ( body : any ) => string ) {
30
- this . encoder = encoder
31
- } ,
32
- setDecoder ( decoder : ( body : string ) => any ) {
33
- this . decoder = decoder
34
- } ,
35
- setPath ( path : string ) {
36
- this . path = path
37
- } ,
38
- setMethod ( method : "GET" | "POST" | "PUT" | "DELETE" ) {
39
- this . method = method
40
- } ,
41
- setBaseUrl ( baseUrl : string ) {
42
- this . baseUrl = baseUrl
43
- } ,
44
- setTimeout ( timeout : number ) {
45
- this . timeout = timeout
46
- } ,
47
- setAbortSignal ( signal : AbortSignal ) {
48
- this . abortSignal = signal
49
- } ,
50
- merge ( request : Partial < Request > ) : Request {
51
- const newRequest = {
52
- ...this ,
53
- ...request
54
- }
55
-
56
- // Properly merge the array properties since they should be merged
57
- // instead of being replaced entirely, like new properties be added.
58
- if ( request . headers ) {
59
- newRequest . headers = {
60
- ...this . headers ,
61
- ...request . headers
62
- }
63
- }
64
- if ( request . hooks ) {
65
- newRequest . hooks = {
66
- ...this . hooks ,
67
- ...request . hooks
68
- }
69
- }
70
-
71
- return newRequest
72
- }
73
- }
74
- }
75
-
76
- function createResult < Type > ( base : BaseResult < Type > ) : Result < Type > {
77
- return {
78
- ...base ,
79
- merge ( result : Partial < Result < Type > > ) : Result < Type > {
80
- const newResult = {
81
- ...this ,
82
- ...result
83
- }
84
- // Properly merge the array properties since they should be merged
85
- // instead of being replaced entirely, like new properties be added.
86
- if ( result . headers ) {
87
- newResult . headers = {
88
- ...this . headers ,
89
- ...result . headers
90
- }
91
- }
92
- return newResult
93
- }
94
- }
95
- }
96
-
97
5
export function createClient < C extends ClientBuilder > ( baseUrl : string , config : C , options ?: ClientBuilderOptions ) : Client < C > {
98
6
const client = { } as Client < C > ;
99
7
const global = {
@@ -224,3 +132,75 @@ export function createClient<C extends ClientBuilder>(baseUrl: string, config: C
224
132
225
133
return client ;
226
134
}
135
+
136
+ function mergeObjects < A , B > ( a : A , b : B ) : A {
137
+ return {
138
+ ...a ,
139
+ ...b
140
+ }
141
+ }
142
+
143
+ function createRequest ( base : BaseRequest ) : Request {
144
+ return {
145
+ ...base ,
146
+ addHeaders ( headers : Headers ) {
147
+ this . headers = mergeObjects ( this . headers , headers )
148
+ } ,
149
+ setHeaders ( headers : Headers ) {
150
+ this . headers = headers
151
+ } ,
152
+ addQueryParameters ( queryParameters : { [ key : string ] : string | number | boolean } ) {
153
+ this . queryParameters = mergeObjects ( this . queryParameters , queryParameters )
154
+ } ,
155
+ setQueryParameters ( queryParameters : { [ key : string ] : string | number | boolean } ) {
156
+ this . queryParameters = queryParameters
157
+ } ,
158
+ setBody ( body : any ) {
159
+ this . body = body
160
+ } ,
161
+ setEncoder ( encoder : ( body : any ) => string ) {
162
+ this . encoder = encoder
163
+ } ,
164
+ setDecoder ( decoder : ( body : string ) => any ) {
165
+ this . decoder = decoder
166
+ } ,
167
+ setPath ( path : string ) {
168
+ this . path = path
169
+ } ,
170
+ setMethod ( method : "GET" | "POST" | "PUT" | "DELETE" ) {
171
+ this . method = method
172
+ } ,
173
+ setBaseUrl ( baseUrl : string ) {
174
+ this . baseUrl = baseUrl
175
+ } ,
176
+ setTimeout ( timeout : number ) {
177
+ this . timeout = timeout
178
+ } ,
179
+ setAbortSignal ( signal : AbortSignal ) {
180
+ this . abortSignal = signal
181
+ } ,
182
+ merge ( request : Partial < Request > ) : Request {
183
+ const newRequest = mergeObjects ( this , request )
184
+ if ( request . headers ) {
185
+ newRequest . headers = mergeObjects ( this . headers , request . headers )
186
+ }
187
+ if ( request . hooks ) {
188
+ newRequest . hooks = mergeObjects ( this . hooks , request . hooks )
189
+ }
190
+ return newRequest
191
+ }
192
+ }
193
+ }
194
+
195
+ function createResult < Type > ( base : BaseResult < Type > ) : Result < Type > {
196
+ return {
197
+ ...base ,
198
+ merge ( result : Partial < Result < Type > > ) : Result < Type > {
199
+ const newResult = mergeObjects ( this , result )
200
+ if ( result . headers ) {
201
+ newResult . headers = mergeObjects ( this . headers , result . headers )
202
+ }
203
+ return newResult
204
+ }
205
+ }
206
+ }
0 commit comments