Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

feat: expose https agent options #82

Merged
merged 1 commit into from
Nov 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/rest/src/lib/REST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { EventEmitter } from 'node:events';
import { CDN } from './CDN';
import { InternalRequest, RequestData, RequestManager, RequestMethod, RouteLike } from './RequestManager';
import { DefaultRestOptions, RESTEvents } from './utils/constants';
import type { AgentOptions } from 'node:https';

/**
* Options to be passed when creating the REST instance
*/
export interface RESTOptions {
/**
* HTTPS Agent options
* @default {}
*/
agent: Omit<AgentOptions, 'keepAlive'>;
/**
* The base api path, without version
* @default 'https://discord.com/api'
Expand Down
4 changes: 3 additions & 1 deletion packages/rest/src/lib/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SequentialHandler } from './handlers/SequentialHandler';
import type { RESTOptions } from './REST';
import { DefaultRestOptions, DefaultUserAgent } from './utils/constants';

const agent = new Agent({ keepAlive: true });
let agent: Agent | null = null;

/**
* Represents an attachment to be added to the request
Expand Down Expand Up @@ -198,6 +198,8 @@ export class RequestManager extends EventEmitter {
private resolveRequest(request: InternalRequest): { url: string; fetchOptions: RequestInit } {
const { options } = this;

agent ??= new Agent({ ...options.agent, keepAlive: true });

let query = '';

// If a query option is passed, use it
Expand Down
1 change: 1 addition & 0 deletions packages/rest/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Package = require('../../../package.json');
export const DefaultUserAgent = `DiscordBot (${Package.homepage}, ${Package.version})`;

export const DefaultRestOptions: Required<RESTOptions> = {
agent: {},
api: 'https://discord.com/api',
cdn: 'https://cdn.discordapp.com',
headers: {},
Expand Down