From a45e9419b33bf8c21f08d23dfac0d2d521890af1 Mon Sep 17 00:00:00 2001 From: Luke Carr Date: Thu, 21 May 2020 11:17:07 +0100 Subject: [PATCH] feat(client): exposed several HTTP options for configuration --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ src/client.ts | 36 ++++++++++++++++++++++++++++++++++-- src/index.ts | 9 +++++---- tests/joodle.test.ts | 15 ++++++++++++++- 4 files changed, 93 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 160b199..de801f3 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,46 @@ Setting the `JOODLE_BASE_URL` environment variable will emulate the `baseURL` cl Configuration options provided to the client constructor take priority over (override) any environment variables declared. +### HTTP Configuration + +Several HTTP configuration options from **got** (the HTTP library Joodle uses) are exposed so you can configure them to your liking. + +You can provide HTTP options as a second parameter when initializing the `Joodle` client. + +```js +const joodle = new Joodle( + { + ... + }, + { + /** + * The duration in milliseconds that the client should wait for a response + * before aborting the request. + * + * By default, there is no response timeout duration. + */ + timeout: 5000, // Timeout after 5 seconds (5000 milliseconds) + + /** + * How many retries should the client attempt to make on failure. + * + * By default, the client will attempt 2 retries if the first request fails. + */ + retries: 2, // A function call will giv eup after 2 failed retries + + /** + * Whether the client should reject invalid SSL certificates (true) or not + * (false). + * + * By default, the client will reject invalid SSL certificates. This option + * has security implications if set to true, and we only recommend you do + * so when connecting to a local Moodle instance. + */ + rejectInvalidSSL: false, // Accepts invalid SSL certs (useful for localhost) + } +); +``` + ## Contributing ### General Guidelines diff --git a/src/client.ts b/src/client.ts index 2686d12..22caf99 100644 --- a/src/client.ts +++ b/src/client.ts @@ -16,6 +16,33 @@ export interface ClientOptions { token?: string; } +export interface HttpOptions { + /** + * The duration in milliseconds that the client should wait for a response + * before aborting the request. + * + * By default, there is no response timeout duration. + */ + timeout?: number; + + /** + * How many retries should the client attempt to make on failure. + * + * By default, the client will attempt 2 retries if the first request fails. + */ + retries?: number; + + /** + * Whether the client should reject invalid SSL certificates (true) or not + * (false). + * + * By default, the client will reject invalid SSL certificates. This option + * has security implications if set to true, and we only recommend you do + * so when connecting to a local Moodle instance. + */ + rejectInvalidSSL?: boolean; +} + /** * A client that can send HTTP requests to a Moodle site's Web Services API. */ @@ -30,9 +57,10 @@ export abstract class Client { * Initializes the client as well as the client's `got` instance so HTTP * requests can be made. * - * @param options The client's configuration options. + * @param options The client's configuration options. + * @param httpOptions HTTP configuration options to pass along to `got`. */ - public constructor(options?: ClientOptions) { + public constructor(options?: ClientOptions, httpOptions?: HttpOptions) { this.baseURL = (options && options.baseURL) || process.env.JOODLE_BASE_URL; this.token = (options && options.token) || process.env.JOODLE_TOKEN; @@ -48,6 +76,10 @@ export abstract class Client { wstoken: this.token, moodlewsrestformat: "json", }, + // HTTP Options + timeout: (httpOptions && httpOptions.timeout) || undefined, + retry: (httpOptions && httpOptions.retries) || 2, + rejectUnauthorized: httpOptions && httpOptions.rejectInvalidSSL !== undefined ? httpOptions.rejectInvalidSSL : true, }); } } diff --git a/src/index.ts b/src/index.ts index 9401d34..0b920af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { Client, ClientOptions } from "./client"; +import { Client, ClientOptions, HttpOptions } from "./client"; import AuthModule from "./modules/auth"; import CoreModule from "./modules/core"; @@ -16,10 +16,11 @@ export class Joodle extends Client { * Initializes a new Joodle client instance for making API calls to Moodle's * Web Services API. * - * @param options The client's configuration options. + * @param options The client's configuration options. + * @param httpOptions HTTP configuration options to pass along to `got`. */ - public constructor(options?: ClientOptions) { - super(options); + public constructor(options?: ClientOptions, httpOptions?: HttpOptions) { + super(options, httpOptions); this.auth = new AuthModule(this); this.core = new CoreModule(this); diff --git a/tests/joodle.test.ts b/tests/joodle.test.ts index dada02c..196833e 100644 --- a/tests/joodle.test.ts +++ b/tests/joodle.test.ts @@ -4,12 +4,19 @@ import { Joodle } from "../src"; describe("The Joodle client class", () => { const baseURL = "https://moodle.example.com/"; const token = "abc123"; + const timeout = 5000; + const retries = 5; + const rejectInvalidSSL = false; let joodle: Joodle; beforeAll(() => { joodle = new Joodle({ baseURL, token, + }, { + timeout, + retries, + rejectInvalidSSL, }); nock(baseURL) @@ -62,7 +69,13 @@ describe("The Joodle client class", () => { delete process.env["JOODLE_BASE_URL"]; delete process.env["JOODLE_TOKEN"]; - }) + }); + + it("should allow HTTP options to be provided as a second constructor parameter", () => { + expect(joodle.got.defaults.options.timeout.request).toBe(timeout); + expect(joodle.got.defaults.options.retry.limit).toBe(retries); + expect(joodle.got.defaults.options.rejectUnauthorized).toBe(rejectInvalidSSL); + }); it("should throw an error if the baseURL is not provided", () => { expect(() => new Joodle({ baseURL })).toThrowError();