-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow referrer header in request options
- Loading branch information
1 parent
49a094b
commit 70ea915
Showing
9 changed files
with
94 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,38 @@ | ||
import 'cross-fetch/polyfill'; | ||
|
||
// Define a default request options and allow modification using getters, setters | ||
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request | ||
const defaultFetchOpts: RequestInit = { | ||
// By default referrer value will be client:origin: above reference link | ||
referrerPolicy: 'origin', // Use origin value for referrer policy | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy | ||
}; | ||
/* | ||
* Get fetch options | ||
* @return fetchOptions | ||
*/ | ||
export const getFetchOptions = () => { | ||
return defaultFetchOpts; | ||
}; | ||
/* | ||
* Set fetch options | ||
* Users can change default referrer as well as other options when fetch is used internally by stacks.js libraries or from server side | ||
* @example | ||
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request | ||
* setFetchOptions({ referrer: 'no-referrer', referrerPolicy: 'no-referrer', ... other options as per above reference }); | ||
* Now all the subsequent fetchPrivate will use above options | ||
* @return fetchOptions | ||
*/ | ||
export const setFetchOptions = (ops: RequestInit) => { | ||
return Object.assign(defaultFetchOpts, ops); | ||
}; | ||
|
||
/** @ignore */ | ||
export async function fetchPrivate(input: RequestInfo, init?: RequestInit): Promise<Response> { | ||
const defaultFetchOpts: RequestInit = { | ||
referrer: 'no-referrer', | ||
referrerPolicy: 'no-referrer', | ||
}; | ||
const fetchOpts = Object.assign(defaultFetchOpts, init); | ||
const fetchOpts = {}; | ||
// Use the provided options in request options along with default or user provided values | ||
Object.assign(fetchOpts, init, defaultFetchOpts); | ||
|
||
const fetchResult = await fetch(input, fetchOpts); | ||
return fetchResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { fetchPrivate, getFetchOptions, setFetchOptions } from '../src' | ||
import fetchMock from "jest-fetch-mock"; | ||
|
||
test('Verify fetch private options', async () => { | ||
const defaultOptioins = getFetchOptions(); | ||
|
||
expect(defaultOptioins).toEqual({ referrerPolicy: 'origin' }); | ||
|
||
// Override default options when fetchPrivate is called internally by other stacks.js libraries like transactions or from server side | ||
// This is for developers as they cannot directly pass options directly in fetchPrivate | ||
const modifiedOptions: RequestInit= { referrer: 'http://test.com', referrerPolicy: 'same-origin' }; | ||
|
||
// Developers can set fetch options globally one time specifically when fetchPrivate is used internally by stacks.js libraries | ||
setFetchOptions(modifiedOptions); | ||
|
||
expect(getFetchOptions()).toEqual(modifiedOptions); | ||
|
||
// Browser will replace about:client with actual url but it will not be visible in test case | ||
fetchMock.mockOnce(`{ status: 'success'}`, { headers: modifiedOptions as any }); | ||
|
||
const result = await fetchPrivate('https://example.com'); | ||
|
||
// Verify the request options | ||
expect(result.status).toEqual(200); | ||
expect(result.headers.get('referrer')).toEqual(modifiedOptions.referrer); | ||
expect(result.headers.get('referrerPolicy')).toEqual(modifiedOptions.referrerPolicy); | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70ea915
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
stacks-js – ./
stacks-js-git-master-blockstack.vercel.app
stacks-js.vercel.app
stacks.js.org
stacks-js-blockstack.vercel.app