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 Annoying things with this Package. #131

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
"url": "git+https://github.com/SalesforceCommerceCloud/commerce-sdk-isomorphic.git"
},
"license": "BSD-3-Clause",
"main": "lib/index.cjs.js",
"module": "lib/index.esm.js",
"exports": {
".": {
"require": "./lib/index.cjs",
"import": "./lib/index.mjs",
"types": "./lib/index.d.ts"
}
},
"main": "./lib/index.cjs",
"style": "lib/default.css",
"files": [
"lib",
Expand Down Expand Up @@ -97,7 +103,7 @@
},
"dependencies": {
"nanoid": "^3.3.4",
"node-fetch": "2.6.12"
"node-fetch": "3.3.2"
},
"devDependencies": {
"@babel/cli": "7.18.6",
Expand All @@ -115,7 +121,6 @@
"@testing-library/react": "10.4.9",
"@types/fs-extra": "^9.0.13",
"@types/handlebars-helpers": "^0.5.3",
"@types/node-fetch": "^2.6.2",
"@types/react-dom": "^16.9.16",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
Expand Down Expand Up @@ -147,7 +152,7 @@
"react-dom": "^16.14.0",
"react-scripts": "4.0.3",
"rollup": "2.77.0",
"rollup-plugin-filesize": "9.1.2",
"rollup-plugin-filesize": "10.0.0",
"rollup-plugin-includepaths": "0.2.4",
"rollup-plugin-peer-deps-external": "2.2.4",
"rollup-plugin-postcss": "3.1.8",
Expand All @@ -165,12 +170,9 @@
"typedoc-plugin-nojekyll": "^1.0.1",
"typescript": "^4.4.4"
},
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
},
"packageManager": "[email protected]",
"engines": {
"node": ">=10"
"node": ">=12"
},
"bundlesize": [
{
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const outputs = [
format: 'umd',
},
{
file: process.env.REACT_APP_PKG_MODULE || pkg.module,
file: process.env.REACT_APP_PKG_MODULE || pkg.exports['.'].import,
format: 'es',
},
];
Expand Down
17 changes: 12 additions & 5 deletions src/static/clientConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type BrowserRequestInit = RequestInit;
*/
export type FetchOptions = NodeRequestInit & BrowserRequestInit;

export type FetchFunction = (
input: RequestInfo,
init?: FetchOptions | undefined
) => Promise<Response>;

export type FetchArgs = [input: RequestInfo, init?: FetchOptions | undefined];
/**
* Base options that can be passed to the `ClientConfig` class.
*/
Expand All @@ -27,18 +33,14 @@ export interface ClientConfigInit<Params extends BaseUriParameters> {
headers?: {[key: string]: string};
parameters: Params;
fetchOptions?: FetchOptions;
fetchFunction?: FetchFunction;
transformRequest?: (
data: unknown,
headers: {[key: string]: string}
) => Required<FetchOptions>['body'];
throwOnBadResponse?: boolean;
}

export type FetchFunction = (
input: RequestInfo,
init?: FetchOptions | undefined
) => Promise<Response>;

/**
* Configuration parameters common to Commerce SDK clients
*/
Expand All @@ -55,6 +57,8 @@ export default class ClientConfig<Params extends BaseUriParameters>

public fetchOptions: FetchOptions;

public fetchFunction?: FetchFunction;

public transformRequest: NonNullable<
ClientConfigInit<Params>['transformRequest']
>;
Expand Down Expand Up @@ -82,6 +86,9 @@ export default class ClientConfig<Params extends BaseUriParameters>
if (config.proxy) {
this.proxy = config.proxy;
}
if (config.fetchFunction) {
this.fetchFunction = config.fetchFunction;
}
this.throwOnBadResponse = !!config.throwOnBadResponse;
}

Expand Down
15 changes: 9 additions & 6 deletions src/static/helpers/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import type {FetchFunction} from '../clientConfig';

import {RequestInfo} from 'node-fetch';
import type {FetchArgs, FetchFunction, FetchOptions} from '../clientConfig';
/*
* Copyright (c) 2022, Salesforce, Inc.
* All rights reserved.
Expand All @@ -27,15 +27,18 @@ export const hasFetchAvailable = typeof globalObject.fetch === 'function';
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
export const fetch: FetchFunction = (() => {
if (isNode) {
// .default is added because the newer versions of babel doesn't get the default export automatically for require().
// eslint-disable-next-line global-require, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
return require('node-fetch').default;
return (...args: FetchArgs) =>
import('node-fetch').then(({default: nodeFetch}) =>
nodeFetch(
...(args as [input: RequestInfo, init?: FetchOptions | undefined])
)
) as Promise<Response>;
}

if (!hasFetchAvailable)
throw new Error(
'Bad environment: it is not a node environment but fetch is not defined'
);

return globalObject.fetch;
return globalObject.fetch as FetchFunction;
})();
3 changes: 2 additions & 1 deletion templates/operations.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@
method: "{{loud method}}"
};

const response = await fetch(url.toString(), requestOptions);
const fetchFunction = this.clientConfig?.fetchFunction ?? fetch;
const response = await fetchFunction(url.toString(), requestOptions);
if (rawResponse) {
return response;
} else if (this.clientConfig.throwOnBadResponse && !response.ok && response.status !== 304) {
Expand Down
Loading