Skip to content

Commit

Permalink
chore(chainweb-node-client): use cross fetch only if native fetch is …
Browse files Browse the repository at this point in the history
…not available (#2136)

* chore(chainweb-node-client): removed cross-fetch since we only support node >=18

* chore: format

* refactor(chainweb-node-client): use node-fetch-native

* fix(chainweb-node-client)

* chore: format

* fix(chainweb-node-client): try node-fetch-native

* cleanup

* test something else

* fix test

* trying something else
  • Loading branch information
javadkh2 committed Jun 3, 2024
1 parent ee3ca96 commit 9c4145c
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-fishes-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kadena/chainweb-node-client": patch
---

Remove cross-fetch since we only support node >=18
2 changes: 1 addition & 1 deletion packages/libs/chainweb-node-client/src/listen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'cross-fetch';
import type { ICommandResult, IListenRequestBody } from './interfaces/PactAPI';
import { parseResponse } from './parseResponse';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';

/**
* Blocking request for single command result.
Expand Down
2 changes: 1 addition & 1 deletion packages/libs/chainweb-node-client/src/local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ensureSignedCommand } from '@kadena/pactjs';
import type { ICommand, ISignatureJson, IUnsignedCommand } from '@kadena/types';
import fetch from 'cross-fetch';
import type {
ICommandResult,
ILocalCommandResult,
Expand All @@ -9,6 +8,7 @@ import type {
} from './interfaces/PactAPI';
import { parsePreflight, parseResponse } from './parseResponse';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';

/**
* @alpha
Expand Down
1 change: 0 additions & 1 deletion packages/libs/chainweb-node-client/src/parseResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Response } from 'cross-fetch';
import type { ILocalCommandResult, ILocalResult } from './interfaces/PactAPI';
/**
* Parses raw `fetch` response into a typed JSON value.
Expand Down
2 changes: 0 additions & 2 deletions packages/libs/chainweb-node-client/src/parseResponseTEXT.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Response } from 'cross-fetch';

/**
* Parses raw `fetch` response into text.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/libs/chainweb-node-client/src/poll.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'cross-fetch';
import type { IPollRequestBody, IPollResponse } from './interfaces/PactAPI';
import { parseResponse } from './parseResponse';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';
/**
* Allows polling for one or more transaction results by request key.
* Returns an Array of the transaction results we polled for.
Expand Down
2 changes: 1 addition & 1 deletion packages/libs/chainweb-node-client/src/send.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'cross-fetch';
import type { ISendRequestBody, SendResponse } from './interfaces/PactAPI';
import { parseResponse } from './parseResponse';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';
/**
* Asynchronous submission of one or more public (unencrypted) commands to the blockchain for execution.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/libs/chainweb-node-client/src/spv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'cross-fetch';
import type { ISPVRequestBody, SPVResponse } from './interfaces/PactAPI';
import { parseResponseTEXT } from './parseResponseTEXT';
import { stringifyAndMakePOSTRequest } from './stringifyAndMakePOSTRequest';
import { fetch } from './utils/fetch';

/**
* Blocking request to fetch spv proof of a cross chain transaction.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Response } from 'cross-fetch';
import { expect, test } from 'vitest';
import { parseResponse } from '../parseResponse';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Response } from 'cross-fetch';
import { expect, test } from 'vitest';
import { parseResponseTEXT } from '../parseResponseTEXT';

Expand Down
22 changes: 22 additions & 0 deletions packages/libs/chainweb-node-client/src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const fetch: typeof globalThis.fetch = ((
...args: Parameters<typeof globalThis.fetch>
) => {
if (
typeof globalThis !== 'undefined' &&
typeof globalThis.fetch !== 'undefined'
) {
return globalThis.fetch(...args);
}

if (typeof window !== 'undefined' && typeof window.fetch !== 'undefined') {
return window.fetch(...args);
}

if (typeof global !== 'undefined' && typeof global.fetch !== 'undefined') {
return global.fetch(...args);
}

return import('cross-fetch').then(({ fetch }) => {
return fetch(...args);
});
}) as unknown as typeof globalThis.fetch;
6 changes: 3 additions & 3 deletions packages/libs/chainweb-node-client/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const localConfig = defineConfig({
coverage: {
provider: 'v8',
thresholds: {
lines: 98.29,
lines: 97,
functions: 100,
branches: 84.37,
statements: 98.29,
branches: 81,
statements: 97,
},
},
},
Expand Down

0 comments on commit 9c4145c

Please sign in to comment.