-
Notifications
You must be signed in to change notification settings - Fork 775
feat(client): add spotCloseDataStream #953
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
Conversation
carlosmiei
commented
Sep 6, 2025
- relates to spotCloseDataStream #950 (comment)
There was a problem hiding this comment.
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, andspotCloseDataStream - Modified
signedRequestmethod 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.
| */ | ||
| 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
There was a problem hiding this comment.
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.
| const query = method === 'POST' && noDataInSignature ? '' : this.makeQueryString(data); | ||
|
|
||
| const signature = this.generateSignature(query); | ||
| let signature = undefined; |
Copilot
AI
Sep 6, 2025
There was a problem hiding this comment.
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.
| let signature = undefined; | |
| let signature; |
| 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
There was a problem hiding this comment.
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.
| 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'); |
| 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
There was a problem hiding this comment.
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.
| 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'); |