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

feature/add-fetch-client-to-options #619

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare module 'binance-api-node' {
wsBase?: string
wsFutures?: string
proxy?: string
fetchClient?: unknown
}): Binance

export type ErrorCodes_LT =
Expand Down
8 changes: 6 additions & 2 deletions src/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import JSONbig from 'json-bigint'

import 'isomorphic-fetch'

let fetchClient = fetch;
const BASE = 'https://api.binance.com'
const FUTURES = 'https://fapi.binance.com'
const COIN_FUTURES = 'https://dapi.binance.com'
Expand Down Expand Up @@ -121,7 +122,7 @@ const checkParams = (name, payload, requires = []) => {
*/
const publicCall = ({ proxy, endpoints }) => (path, data, method = 'GET', headers = {}) => {
return sendResult(
fetch(
fetchClient(
`${
path.includes('/fapi') || path.includes('/futures')
? endpoints.futures
Expand Down Expand Up @@ -194,7 +195,7 @@ const privateCall = ({
const newData = noExtra ? data : { ...data, timestamp, signature }

return sendResult(
fetch(
fetchClient(
`${
path.includes('/fapi') || path.includes('/futures')
? endpoints.futures
Expand Down Expand Up @@ -333,6 +334,9 @@ export default opts => {
delivery: (opts && opts.httpDelivery) || COIN_FUTURES,
}

// overriding the fetch client if available in options
fetchClient = opts.fetchClient || fetch;

const pubCall = publicCall({ ...opts, endpoints })
const deliveryPubCall = publicCall({ ...opts, endpoints: { futures: endpoints.delivery } })
const privCall = privateCall({ ...opts, endpoints, pubCall })
Expand Down