1
1
import { RequestContext } from "../models/models" ;
2
2
import { isMatch } from "../utils/match" ;
3
3
4
+ export type RequestOptions = {
5
+ headers ?: object ;
6
+ queryParams ?: string ;
7
+ interceptIfContainsIgnorePaths ?: boolean ;
8
+ } ;
9
+
4
10
// TODO: write test code
5
11
// TODO: more simply
6
12
export async function fetchFromApi (
7
13
endpoint : string ,
8
- queryParams : string = "" ,
9
- ctx : RequestContext | null ,
10
- options : {
11
- interceptIfContainsIgnorePaths : boolean ;
12
- } | null = null
14
+ {
15
+ headers = { } ,
16
+ queryParams = "" ,
17
+ interceptIfContainsIgnorePaths = false
18
+ } : RequestOptions = { }
13
19
) : Promise < Response > {
14
- // TODO: extract to function & write test code
15
- if ( options ) {
16
- if ( options . interceptIfContainsIgnorePaths ) {
17
- if ( ! endpoint || ( endpoint && isMatch ( endpoint ) ) ) {
18
- return new Response ( null , { status : 404 } ) ;
19
- }
20
+ if ( interceptIfContainsIgnorePaths ) {
21
+ if ( ! endpoint || ( endpoint && isMatch ( endpoint ) ) ) {
22
+ return new Response ( null , { status : 404 } ) ;
20
23
}
21
24
}
22
25
23
- const q = queryParams ?? "" ;
24
- const url = buildRequestUrl ( endpoint , q ) ;
25
- const header = ctx ? { header : requestHeaderFrom ( ctx ) } : { } ;
26
+ const url = buildRequestUrl ( endpoint , queryParams ) ;
27
+ const header = headers ? { header : headers } : { } ;
26
28
27
29
return fetch ( url , {
28
30
method : "GET" ,
@@ -37,7 +39,7 @@ export function buildRequestUrl(url: string, queryParams: string): string {
37
39
return new URL ( u ) . href ;
38
40
}
39
41
40
- function requestHeaderFrom ( rq : RequestContext ) : Object {
42
+ export function requestHeaderFrom ( rq : RequestContext ) : Object {
41
43
return {
42
44
"Content-Type" : "application/json" ,
43
45
"x-forwarded-for" : rq . ipAddress ,
0 commit comments