@@ -7,28 +7,36 @@ const defaultFetchOpts: RequestInit = {
7
7
referrerPolicy : 'origin' , // Use origin value for referrer policy
8
8
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
9
9
} ;
10
- /*
10
+
11
+ /**
11
12
* Get fetch options
12
- * @return fetchOptions
13
+ * @category Network
13
14
*/
14
15
export const getFetchOptions = ( ) => {
15
16
return defaultFetchOpts ;
16
17
} ;
17
- /*
18
- * Set fetch options
19
- * Users can change default referrer as well as other options when fetch is used internally by stacks.js libraries or from server side
18
+
19
+ /**
20
+ * Sets global fetch options for stacks.js network calls.
21
+ *
20
22
* @example
21
- * Reference: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
22
- * setFetchOptions({ referrer: 'no-referrer', referrerPolicy: 'no-referrer', ... other options as per above reference });
23
- * Now all the subsequent fetchPrivate will use above options
24
- * @return fetchOptions
23
+ * Users can change the default referrer as well as other options when fetch is used internally by stacks.js:
24
+ * ```
25
+ * setFetchOptions({ referrer: 'no-referrer', referrerPolicy: 'no-referrer', ...otherRequestOptions });
26
+ * ```
27
+ * After calling {@link setFetchOptions} all subsequent network calls will use the specified options above.
28
+ *
29
+ * @see MDN Request: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
30
+ * @returns global fetch options after merging with previous options (or defaults)
31
+ * @category Network
32
+ * @related {@link getFetchOptions }
25
33
*/
26
- export const setFetchOptions = ( ops : RequestInit ) => {
34
+ export const setFetchOptions = ( ops : RequestInit ) : RequestInit => {
27
35
return Object . assign ( defaultFetchOpts , ops ) ;
28
36
} ;
29
37
30
- /** @ignore */
31
- export async function fetchPrivate ( input : RequestInfo , init ?: RequestInit ) : Promise < Response > {
38
+ /** @internal */
39
+ export async function fetchWrapper ( input : RequestInfo , init ?: RequestInit ) : Promise < Response > {
32
40
const fetchOpts = { } ;
33
41
// Use the provided options in request options along with default or user provided values
34
42
Object . assign ( fetchOpts , init , defaultFetchOpts ) ;
@@ -76,6 +84,14 @@ export function hostMatches(host: string, pattern: string | RegExp) {
76
84
return ( pattern as RegExp ) . exec ( host ) ;
77
85
}
78
86
87
+ /**
88
+ * Creates a new middleware from an API key.
89
+ * @example
90
+ * ```
91
+ * const apiMiddleware = createApiKeyMiddleware("example_e8e044a3_41d8b0fe_3dd3988ef302");
92
+ * ```
93
+ * @category Network
94
+ */
79
95
export function createApiKeyMiddleware ( {
80
96
apiKey,
81
97
host = / ( .* ) a p i ( .* ) \. s t a c k s \. c o $ / i,
@@ -104,9 +120,8 @@ function createDefaultMiddleware(): FetchMiddleware[] {
104
120
return [ setOriginMiddleware ] ;
105
121
}
106
122
107
- // Argument helper function for {createFetchFn}
108
123
function argsForCreateFetchFn ( args : any [ ] ) : { middlewares : FetchMiddleware [ ] ; fetchLib : FetchFn } {
109
- let fetchLib : FetchFn = fetch ;
124
+ let fetchLib : FetchFn = fetchWrapper ;
110
125
let middlewares : FetchMiddleware [ ] = [ ] ;
111
126
if ( args . length > 0 && typeof args [ 0 ] === 'function' ) {
112
127
fetchLib = args . shift ( ) ;
@@ -117,6 +132,16 @@ function argsForCreateFetchFn(args: any[]): { middlewares: FetchMiddleware[]; fe
117
132
return { middlewares, fetchLib } ;
118
133
}
119
134
135
+ /**
136
+ * Creates a new network fetching function, which combines an optional fetch-compatible library with optional middlware.
137
+ * @example
138
+ * ```
139
+ * const customFetch = createFetchFn(someMiddleware)
140
+ * const customFetch = createFetchFn(fetch, someMiddleware)
141
+ * const customFetch = createFetchFn(fetch, middlewareA, middlewareB)
142
+ * ```
143
+ * @category Network
144
+ */
120
145
export function createFetchFn ( fetchLib : FetchFn , ...middleware : FetchMiddleware [ ] ) : FetchFn ;
121
146
export function createFetchFn ( ...middleware : FetchMiddleware [ ] ) : FetchFn ;
122
147
export function createFetchFn ( ...args : any [ ] ) : FetchFn {
@@ -125,6 +150,7 @@ export function createFetchFn(...args: any[]): FetchFn {
125
150
126
151
const fetchFn = async ( url : string , init ?: RequestInit | undefined ) : Promise < Response > => {
127
152
let fetchParams = { url, init : init ?? { } } ;
153
+
128
154
for ( const middleware of middlewares ) {
129
155
if ( typeof middleware . pre !== 'function' ) continue ;
130
156
const result = await Promise . resolve (
0 commit comments