Skip to content

Commit

Permalink
feat: client options can now be provided through environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Carr committed May 21, 2020
1 parent 1d47c2e commit 492459a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ import got, { Got } from "got";

export interface ClientOptions {
/**
* The base URL of the Moodle site to send API requests to.
* The base URL of the Moodle site to send API requests to. If this value
* is not provided, then the client falls back to the `JOODLE_BASE_URL`
* environment variable.
*/
baseURL: string;
baseURL?: string;

/**
* The token used to authenticate with the Moodle site's Web Services API.
* If this value is not provided, then the client falls back to the
* `JOODLE_TOKEN` environment variable.
*/
token: string;
token?: string;
}

/**
* A client that can send HTTP requests to a Moodle site's Web Services API.
*/
export abstract class Client {
private baseURL: string;
private baseURL?: string;

private token: string;
private token?: string;

public got: Got;

Expand All @@ -28,9 +32,15 @@ export abstract class Client {
*
* @param options The client's configuration options.
*/
public constructor(options: ClientOptions) {
this.baseURL = options.baseURL;
this.token = options.token;
public constructor(options?: ClientOptions) {
this.baseURL = (options && options.baseURL) || process.env.JOODLE_BASE_URL;
this.token = (options && options.token) || process.env.JOODLE_TOKEN;

if (this.baseURL === undefined) {
throw new Error("`baseURL` cannot be undefined!");
} else if (this.token === undefined) {
throw new Error("`token` cannot be undefined!");
}

this.got = got.extend({
prefixUrl: this.baseURL,
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ 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.
*/
public constructor(options: ClientOptions) {
public constructor(options?: ClientOptions) {
super(options);

this.auth = new AuthModule(this);
this.core = new CoreModule(this);
}
Expand Down

0 comments on commit 492459a

Please sign in to comment.