@@ -7,28 +7,36 @@ const defaultFetchOpts: RequestInit = {
77 referrerPolicy : 'origin' , // Use origin value for referrer policy
88 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
99} ;
10- /*
10+
11+ /**
1112 * Get fetch options
12- * @return fetchOptions
13+ * @category Network
1314 */
1415export const getFetchOptions = ( ) => {
1516 return defaultFetchOpts ;
1617} ;
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+ *
2022 * @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 }
2533 */
26- export const setFetchOptions = ( ops : RequestInit ) => {
34+ export const setFetchOptions = ( ops : RequestInit ) : RequestInit => {
2735 return Object . assign ( defaultFetchOpts , ops ) ;
2836} ;
2937
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 > {
3240 const fetchOpts = { } ;
3341 // Use the provided options in request options along with default or user provided values
3442 Object . assign ( fetchOpts , init , defaultFetchOpts ) ;
@@ -76,6 +84,14 @@ export function hostMatches(host: string, pattern: string | RegExp) {
7684 return ( pattern as RegExp ) . exec ( host ) ;
7785}
7886
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+ */
7995export function createApiKeyMiddleware ( {
8096 apiKey,
8197 host = / ( .* ) a p i ( .* ) \. s t a c k s \. c o $ / i,
@@ -104,9 +120,8 @@ function createDefaultMiddleware(): FetchMiddleware[] {
104120 return [ setOriginMiddleware ] ;
105121}
106122
107- // Argument helper function for {createFetchFn}
108123function argsForCreateFetchFn ( args : any [ ] ) : { middlewares : FetchMiddleware [ ] ; fetchLib : FetchFn } {
109- let fetchLib : FetchFn = fetch ;
124+ let fetchLib : FetchFn = fetchWrapper ;
110125 let middlewares : FetchMiddleware [ ] = [ ] ;
111126 if ( args . length > 0 && typeof args [ 0 ] === 'function' ) {
112127 fetchLib = args . shift ( ) ;
@@ -117,6 +132,16 @@ function argsForCreateFetchFn(args: any[]): { middlewares: FetchMiddleware[]; fe
117132 return { middlewares, fetchLib } ;
118133}
119134
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+ */
120145export function createFetchFn ( fetchLib : FetchFn , ...middleware : FetchMiddleware [ ] ) : FetchFn ;
121146export function createFetchFn ( ...middleware : FetchMiddleware [ ] ) : FetchFn ;
122147export function createFetchFn ( ...args : any [ ] ) : FetchFn {
@@ -125,6 +150,7 @@ export function createFetchFn(...args: any[]): FetchFn {
125150
126151 const fetchFn = async ( url : string , init ?: RequestInit | undefined ) : Promise < Response > => {
127152 let fetchParams = { url, init : init ?? { } } ;
153+
128154 for ( const middleware of middlewares ) {
129155 if ( typeof middleware . pre !== 'function' ) continue ;
130156 const result = await Promise . resolve (
0 commit comments