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

Remove unfetch #1450

Open
wants to merge 4 commits into
base: master
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
7 changes: 7 additions & 0 deletions .changeset/mean-hotels-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@near-js/accounts": patch
"@near-js/client": patch
"@near-js/providers": patch
---

drop unfetch polyfill
2 changes: 0 additions & 2 deletions packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"borsh": "1.0.0",
"depd": "2.0.0",
"is-my-json-valid": "^2.20.6",
"isomorphic-unfetch": "^3.1.0",
"lru_map": "0.4.1",
"near-abi": "0.1.1"
},
Expand All @@ -40,7 +39,6 @@
"jest": "29.7.0",
"near-hello": "0.5.1",
"near-workspaces": "3.5.0",
"node-fetch": "2.6.7",
"semver": "7.1.1",
"ts-jest": "29.1.5",
"tsconfig": "workspace:*",
Expand Down
3 changes: 1 addition & 2 deletions packages/accounts/src/account_2fa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { PublicKey } from '@near-js/crypto';
import { FinalExecutionOutcome, TypedError, FunctionCallPermissionView } from '@near-js/types';
import { actionCreators } from '@near-js/transactions';
import { Logger } from '@near-js/utils'
import unfetch from 'isomorphic-unfetch';

import { SignAndSendTransactionOptions } from './account';
import { AccountMultisig } from './account_multisig';
Expand Down Expand Up @@ -332,7 +331,7 @@ export class Account2FA extends AccountMultisig {
* @returns {Promise<any>} - A promise that resolves to the response from the helper.
*/
async postSignedJson(path, body) {
return await unfetch(this.helperUrl + path, {
return await fetch(this.helperUrl + path, {
body: JSON.stringify({
...body,
...(await this.signatureFor()),
Expand Down
3 changes: 1 addition & 2 deletions packages/accounts/src/account_creator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PublicKey } from '@near-js/crypto';
import unfetch from 'isomorphic-unfetch';

import { Connection } from './connection';
import { Account } from './account';
Expand Down Expand Up @@ -50,7 +49,7 @@ export class UrlAccountCreator extends AccountCreator {
* @returns {Promise<void>}
*/
async createAccount(newAccountId: string, publicKey: PublicKey): Promise<void> {
await unfetch(`${this.helperUrl}/account`, {
await fetch(`${this.helperUrl}/account`, {
body: JSON.stringify({ newAccountId, newAccountPublicKey: publicKey.toString() }),
method: 'POST',
});
Expand Down
3 changes: 1 addition & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"@near-js/transactions": "workspace:*",
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"@noble/hashes": "1.3.3",
"isomorphic-fetch": "3.0.0"
"@noble/hashes": "1.3.3"
},
"keywords": [],
"author": "",
Expand Down
1 change: 0 additions & 1 deletion packages/client/src/funded_account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { FinalExecutionOutcome } from '@near-js/types';
import fetch from 'isomorphic-fetch';

import { KITWALLET_FUNDED_TESTNET_ACCOUNT_ENDPOINT } from './constants';
import { NewAccountParams } from './interfaces';
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"borsh": "1.0.0",
"exponential-backoff": "^3.1.1",
"isomorphic-unfetch": "^3.1.0"
"exponential-backoff": "^3.1.1"
},
"devDependencies": {
"@fetch-mock/jest": "^0.2.7",
"@jest/globals": "^29.7.0",
"@types/node": "20.0.0",
"build": "workspace:*",
Expand Down
3 changes: 1 addition & 2 deletions packages/providers/src/fetch_json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TypedError } from '@near-js/types';
import { backOff } from 'exponential-backoff';
import unfetch from 'isomorphic-unfetch';

const BACKOFF_MULTIPLIER = 1.5;
const RETRY_NUMBER = 10;
Expand Down Expand Up @@ -56,7 +55,7 @@ interface JsonRpcRequest {
*/
export async function fetchJsonRpc(url: string, json: JsonRpcRequest, headers: object, retryConfig: object): Promise<any> {
const response = await backOff(async () => {
const res = await unfetch(url, {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify(json),
headers: { ...headers, 'Content-Type': 'application/json' }
Expand Down
38 changes: 6 additions & 32 deletions packages/providers/test/fetch_json_error.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { describe, expect, test, jest, afterEach } from '@jest/globals';
import { describe, expect, test } from '@jest/globals';
import { fetchJsonRpc, retryConfig } from '../src/fetch_json';
import unfetch from 'isomorphic-unfetch';
import { ProviderError } from '../src/fetch_json';

jest.mock('isomorphic-unfetch');
import fetchMock from '@fetch-mock/jest';

describe('fetchJsonError', () => {
const RPC_URL = 'https://rpc.testnet.near.org';
Expand All @@ -14,30 +12,16 @@ describe('fetchJsonError', () => {
params: [],
};

afterEach(() => {
jest.clearAllMocks();
});

test('handles 500 Internal Server Error', async () => {
(unfetch as jest.Mock).mockReturnValue({
ok: false,
status: 500,
text: '',
json: {},
});
fetchMock.once(RPC_URL, 500, '');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for whatever reason typescript says that fetchMock once is not defined, even though it is. I suspsect this is something to do with Jest or incorrectly configured typescript compiler (probably the first)

 FAIL  test/fetch_json_error.test.ts
  ● Test suite failed to run

    test/fetch_json_error.test.ts:16:19 - error TS2339: Property 'once' does not exist on type 'FetchMockJest'.

    16         fetchMock.once(RPC_URL, 500, '');
                         ~~~~
    test/fetch_json_error.test.ts:24:19 - error TS2339: Property 'once' does not exist on type 'FetchMockJest'.

    24         fetchMock.once(RPC_URL, 408, '');
                         ~~~~
    test/fetch_json_error.test.ts:33:19 - error TS2339: Property 'once' does not exist on type 'FetchMockJest'.

    33         fetchMock.once(RPC_URL, 400, '');
                         ~~~~
    test/fetch_json_error.test.ts:41:19 - error TS2339: Property 'once' does not exist on type 'FetchMockJest'.

    41         fetchMock.once(RPC_URL, 503, '');
                         ~~~~


// @ts-expect-error test input
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined, retryConfig()))
.rejects
.toThrowError(new ProviderError('Internal server error', { cause: 500 }));
});
test('handles 408 Timeout Error', async () => {
(unfetch as jest.Mock).mockReturnValue({
ok: false,
status: 408,
text: '',
json: {},
});
fetchMock.once(RPC_URL, 408, '');
// @ts-expect-error test input
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined, retryConfig()))
.rejects
Expand All @@ -46,25 +30,15 @@ describe('fetchJsonError', () => {
// });

test('handles 400 Request Validation Error', async () => {
(unfetch as jest.Mock).mockReturnValue({
ok: false,
status: 400,
text: '',
json: {},
});
fetchMock.once(RPC_URL, 400, '');
// @ts-expect-error test input
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined, retryConfig()))
.rejects
.toThrowError(new ProviderError('Request validation error', { cause: 400 }));
});

test('handles 503 Service Unavailable', async () => {
(unfetch as jest.Mock).mockReturnValue({
ok: false,
status: 503,
text: '',
json: {},
});
fetchMock.once(RPC_URL, 503, '');

// @ts-expect-error test input
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined, retryConfig()))
Expand Down
Loading