@@ -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 ) ;
@@ -81,11 +89,16 @@ export function hostMatches(host: string, pattern: string | RegExp) {
81
89
* @example
82
90
* ```
83
91
* const apiMiddleware = createApiKeyMiddleware("example_e8e044a3_41d8b0fe_3dd3988ef302");
92
+ <<<<<<< HEAD
84
93
* const fetchFn = createFetchFn(apiMiddleware);
85
94
* const network = new StacksMainnet({ fetchFn });
86
95
* ```
87
96
* @category Network
88
97
* @related {@link createFetchFn }, {@link StacksNetwork}
98
+ =======
99
+ * ```
100
+ * @category Network
101
+ >>>>>>> 48226115 (docs: optimize ts docs)
89
102
*/
90
103
export function createApiKeyMiddleware ( {
91
104
apiKey,
@@ -105,7 +118,7 @@ export function createApiKeyMiddleware({
105
118
}
106
119
107
120
function argsForCreateFetchFn ( args : any [ ] ) : { middlewares : FetchMiddleware [ ] ; fetchLib : FetchFn } {
108
- let fetchLib : FetchFn = fetch ;
121
+ let fetchLib : FetchFn = fetchWrapper ;
109
122
let middlewares : FetchMiddleware [ ] = [ ] ;
110
123
if ( args . length > 0 && typeof args [ 0 ] === 'function' ) {
111
124
fetchLib = args . shift ( ) ;
@@ -116,6 +129,16 @@ function argsForCreateFetchFn(args: any[]): { middlewares: FetchMiddleware[]; fe
116
129
return { middlewares, fetchLib } ;
117
130
}
118
131
132
+ /**
133
+ * Creates a new network fetching function, which combines an optional fetch-compatible library with optional middlware.
134
+ * @example
135
+ * ```
136
+ * const customFetch = createFetchFn(someMiddleware)
137
+ * const customFetch = createFetchFn(fetch, someMiddleware)
138
+ * const customFetch = createFetchFn(fetch, middlewareA, middlewareB)
139
+ * ```
140
+ * @category Network
141
+ */
119
142
export function createFetchFn ( fetchLib : FetchFn , ...middleware : FetchMiddleware [ ] ) : FetchFn ;
120
143
export function createFetchFn ( ...middleware : FetchMiddleware [ ] ) : FetchFn ;
121
144
export function createFetchFn ( ...args : any [ ] ) : FetchFn {
@@ -124,6 +147,7 @@ export function createFetchFn(...args: any[]): FetchFn {
124
147
125
148
const fetchFn = async ( url : string , init ?: RequestInit | undefined ) : Promise < Response > => {
126
149
let fetchParams = { url, init : init ?? { } } ;
150
+
127
151
for ( const middleware of middlewares ) {
128
152
if ( typeof middleware . pre !== 'function' ) continue ;
129
153
const result = await Promise . resolve (
0 commit comments