Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix usage of cross-fetch #8

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build": "rimraf dist && webpack --mode=production",
"commitmsg": "commitlint -e $GIT_PARAMS",
"coverage": "tap ./test/{unit,integration}/*.js --coverage --coverage-report=lcov",
"prepare": "husky install && npm run build",
"prepare": "husky install && webpack --mode=production",
"semantic-release": "semantic-release",
"test": "npm run test:lint && jest \"test[\\\\/](unit|integration)\"",
"test:clearCache": "jest --clearCache",
Expand Down
2 changes: 1 addition & 1 deletion src/FetchWorkerTool.worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env worker */

const crossFetch = require('cross-fetch').default;
const crossFetch = require('cross-fetch');

let jobsActive = 0;
const complete = [];
Expand Down
15 changes: 8 additions & 7 deletions src/scratchFetch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const crossFetch = require('cross-fetch');
const fetch = require('cross-fetch').fetch;
const Headers = require('cross-fetch').Headers;

/**
* Metadata header names
Expand All @@ -16,7 +17,7 @@ const RequestMetadata = {
* Metadata headers for requests
* @type {Headers}
*/
const metadata = new crossFetch.Headers();
const metadata = new Headers();

/**
* Check if there is any metadata to apply.
Expand Down Expand Up @@ -54,13 +55,13 @@ const hasMetadata = () => {
const applyMetadata = options => {
if (hasMetadata()) {
const augmentedOptions = Object.assign({}, options);
augmentedOptions.headers = new crossFetch.Headers(metadata);
augmentedOptions.headers = new Headers(metadata);
if (options && options.headers) {
// the Fetch spec says options.headers could be:
// "A Headers object, an object literal, or an array of two-item arrays to set request's headers."
// turn it into a Headers object to be sure of how to interact with it
const overrideHeaders = options.headers instanceof crossFetch.Headers ?
options.headers : new crossFetch.Headers(options.headers);
const overrideHeaders = options.headers instanceof Headers ?
options.headers : new Headers(options.headers);
for (const [name, value] of overrideHeaders.entries()) {
augmentedOptions.headers.set(name, value);
}
Expand All @@ -80,7 +81,7 @@ const applyMetadata = options => {
*/
const scratchFetch = (resource, options) => {
const augmentedOptions = applyMetadata(options);
return crossFetch.fetch(resource, augmentedOptions);
return fetch(resource, augmentedOptions);
};

/**
Expand Down Expand Up @@ -111,7 +112,7 @@ const unsetMetadata = name => {
const getMetadata = name => metadata.get(name);

module.exports = {
Headers: crossFetch.Headers,
Headers: Headers,
RequestMetadata,
applyMetadata,
scratchFetch,
Expand Down
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const baseConfig = new ScratchWebpackConfigBuilder(
rootPath: path.resolve(__dirname),
enableReact: false,
enableTs: true,
shouldSplitChunks: false
shouldSplitChunks: false,
publicPath: ''
})
.setTarget('browserslist')
.merge({
Expand All @@ -32,6 +33,7 @@ const webConfig = baseConfig.clone()
type: 'umd2'
},
path: path.resolve(__dirname, 'dist', 'web'),
publicPath: 'auto',
clean: false
}
});
Expand Down