Skip to content

Commit

Permalink
feat(client): exposed several HTTP options for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Carr committed May 21, 2020
1 parent 78badb8 commit a45e941
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 34 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;

Expand All @@ -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,
});
}
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
Expand Down
15 changes: 14 additions & 1 deletion tests/joodle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a45e941

Please sign in to comment.