Skip to content

feat(client): add spotCloseDataStream#953

Merged
carlosmiei merged 2 commits into
masterfrom
close-stream-data
Sep 6, 2025
Merged

feat(client): add spotCloseDataStream#953
carlosmiei merged 2 commits into
masterfrom
close-stream-data

Conversation

@carlosmiei

Copy link
Copy Markdown
Collaborator

@carlosmiei carlosmiei self-assigned this Sep 6, 2025
@carlosmiei carlosmiei requested a review from Copilot September 6, 2025 17:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds functionality to manage spot user data streams by implementing three new methods for creating, keeping alive, and closing data streams. The changes also update the signed request handling to properly support listen key endpoints that don't require signatures.

  • Added three new methods: spotGetDataStream, spotKeepDataStream, and spotCloseDataStream
  • Modified signedRequest method to handle listen key endpoints that don't require signatures
  • Updated URL encoding logic to conditionally append query parameters and signatures

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/node-binance-api.ts
*/
async signedRequest(url: string, data: Dict = {}, method: HttpMethod = 'GET', noDataInSignature = false) {
this.requireApiSecret('signedRequest');
const isListenKeyEndpoint = url.includes('v3/userDataStream');

Copilot AI Sep 6, 2025

Copy link

Choose a reason for hiding this comment

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

Hard-coded URL path detection is fragile. Consider using a more robust method like checking against a constant or using URL parsing to identify listen key endpoints.

Copilot uses AI. Check for mistakes.
Comment thread src/node-binance-api.ts
const query = method === 'POST' && noDataInSignature ? '' : this.makeQueryString(data);

const signature = this.generateSignature(query);
let signature = undefined;

Copilot AI Sep 6, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] Initialize signature as undefined is redundant since variables declared with let without initialization are automatically undefined. Consider declaring it only when needed or initialize with a more meaningful default.

Suggested change
let signature = undefined;
let signature;

Copilot uses AI. Check for mistakes.
Comment thread src/node-binance-api.ts
Comment on lines +3934 to +3943
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'PUT');
}

async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'DELETE');

Copilot AI Sep 6, 2025

Copy link

Choose a reason for hiding this comment

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

The error handling logic for missing listenKey is duplicated across both methods. Consider extracting this validation into a private helper method to reduce code duplication.

Suggested change
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'PUT');
}
async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'DELETE');
/**
* Helper to validate and retrieve a listenKey from argument or options.
* Throws if neither is present.
*/
private _getValidListenKey(listenKey?: string): string {
const key = listenKey || this.Options.listenKey;
if (!key) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return key;
}
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
const validListenKey = this._getValidListenKey(listenKey);
return await this.privateSpotRequest('v3/userDataStream', { listenKey: validListenKey, ...params }, 'PUT');
}
async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
const validListenKey = this._getValidListenKey(listenKey);
return await this.privateSpotRequest('v3/userDataStream', { listenKey: validListenKey, ...params }, 'DELETE');

Copilot uses AI. Check for mistakes.
Comment thread src/node-binance-api.ts
Comment on lines +3934 to +3943
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'PUT');
}

async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'DELETE');

Copilot AI Sep 6, 2025

Copy link

Choose a reason for hiding this comment

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

The error handling logic for missing listenKey is duplicated across both methods. Consider extracting this validation into a private helper method to reduce code duplication.

Suggested change
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'PUT');
}
async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'DELETE');
/**
* Helper to get listenKey or throw error if missing
*/
private _getListenKeyOrThrow(listenKey?: string): string {
const key = listenKey || this.Options.listenKey;
if (!key) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return key;
}
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
const key = this._getListenKeyOrThrow(listenKey);
return await this.privateSpotRequest('v3/userDataStream', { listenKey: key, ...params }, 'PUT');
}
async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
const key = this._getListenKeyOrThrow(listenKey);
return await this.privateSpotRequest('v3/userDataStream', { listenKey: key, ...params }, 'DELETE');

Copilot uses AI. Check for mistakes.
@carlosmiei carlosmiei merged commit adaeba9 into master Sep 6, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants