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

feat: header options for WS and REST #250

Merged
merged 2 commits into from
Apr 21, 2024
Merged
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
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
Loading