Skip to content

Commit

Permalink
feat: header options for WS and REST (#250)
Browse files Browse the repository at this point in the history
* feat: header options for WS and REST

* Create cool-wolves-sniff.md

---------

Co-authored-by: Nico <nico.03727+github@gmail.com>
  • Loading branch information
IdkGoodName and zaida04 authored Apr 21, 2024
1 parent c21df21 commit 7158c2c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/cool-wolves-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@guildedjs/api": patch
"guilded.js": patch
---

feat: header options for WS and REST
3 changes: 3 additions & 0 deletions packages/api/lib/rest/RestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ export class RestManager {
"User-Agent": `@guildedjs-rest/${packageDetails.version} Node.js ${process.version}`,
// Spread the other headers like authentication.
...headers,
// User supplided. This will be after Guilded.js default headers to allow modifying, but before request's specific one
// to still allow per-request modifications
...this.options.headers,
// Spread any additional headers passed from the method.
...data.headers,
},
Expand Down
4 changes: 4 additions & 0 deletions packages/api/lib/rest/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ export type RestOptions = {
* The version of the API to be used for making requests. By default, this will use the latest version that the library supports.
*/
version?: 1;
/**
* The additional headers that will be added to each request done by RestManager.
*/
headers?: Record<string, string>;
};
6 changes: 6 additions & 0 deletions packages/api/lib/ws/WebSocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ export class WebSocketManager {

connect(): void {
this._debug(`Connecting to Guilded WS Gateway at url ${this.wsURL}.`);
// Since rest supplies custom headers after authorization header, WS will be the same in that regard
const headers: Record<string, string> = {
Authorization: `Bearer ${this.token}`,
...this.options.headers,
};

if (this.shouldRequestMissedEvents) {
Expand Down Expand Up @@ -287,6 +289,10 @@ export type WebSocketOptions = {
* The version of the websocket to connect to.
*/
version?: 1;
/**
* The additional headers that will be added to WebSocket request upon initial connection.
*/
headers?: Record<string, string>;
};

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
Expand Down
20 changes: 20 additions & 0 deletions packages/guilded.js/lib/structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
* The websocket connection.
*/
ws = new WebSocketManager({
...this.options.ws,
token: this.options.token,
});

Expand Down Expand Up @@ -292,6 +293,25 @@ export type ClientOptions = {
* @remarks If you want to use a custom API url, you can set this property to your custom url.
*/
proxyURL?: string;

/**
* The headers that will be supplied in each request that the client, or more precisely, RestManager, will send.
*
* @remarks It's generally only recommended to supply headers that may be required for Guilded experiments, as vital headers will be supplied by Guilded.js.
*/
headers?: Record<string, string>;
};

/**
* Options that will be given directly to WebSocket.
*/
ws?: {
/**
* The headers that will be supplied when WebSocket is being initialized and its request is made.
*
* @remarks It's generally only recommended to supply headers that may be required for Guilded experiments, as vital headers will be supplied by Guilded.js.
*/
headers?: Record<string, string>;
};

/**
Expand Down

0 comments on commit 7158c2c

Please sign in to comment.