Skip to content

Commit

Permalink
feat(modules): added getHttpResponse() to get raw HTTP response
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Carr committed May 21, 2020
1 parent 989f82b commit b053696
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/functions/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Response } from "got";

/**
* The response returned by a function call
* to Moodle's Web Services API.
*/
export default interface FunctionResponse {
/**
* Returns the raw HTTP response returned
* from the API call for this function.
*/
getHttpResponse(): Response;
}
3 changes: 2 additions & 1 deletion src/modules/auth/email.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Module from "..";
import FunctionResponse from "../../functions/response";

export interface SignUpSettingsResponse {
export interface SignUpSettingsResponse extends FunctionResponse {
/**
* The fields that a user can provide during sign up to identify their name.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/modules/core/webservice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Module from "..";
import FunctionResponse from "../../functions/response";

interface WebServiceFunction {
/**
Expand All @@ -24,7 +25,7 @@ interface AdvancedFeature {
value: 1 | 0;
}

export interface SiteInfoResponse {
export interface SiteInfoResponse extends FunctionResponse {
/**
* The Moodle site's display name.
*/
Expand Down
18 changes: 11 additions & 7 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client } from "../client";
import FunctionResponse from "../functions/response";

/**
* Attempts to handle a JSON body returned by a call to Moodle's Web Services API.
Expand All @@ -7,11 +8,11 @@ import { Client } from "../client";
*
* @param body A JSON body returned by a Moodle API call.
*/
const handleResponse = async (body: unknown): Promise<unknown> => {
if ((body as any).exception) {
return Promise.reject(body);
const handleResponse = async (response: FunctionResponse): Promise<FunctionResponse> => {
if ((response as any).exception) {
return Promise.reject(response);
}
return body;
return response;
};

/**
Expand All @@ -33,15 +34,18 @@ export default abstract class Module {
protected async get(
wsfunction: string,
searchParams?: any
): Promise<unknown> {
const { body } = await this.client.got.get("webservice/rest/server.php", {
): Promise<FunctionResponse> {
const response = await this.client.got.get("webservice/rest/server.php", {
searchParams: {
wsfunction,
...searchParams,
},
responseType: "json",
});

return handleResponse(body);
return handleResponse({
getHttpResponse: () => response,
...(response.body as any)
});
}
}
5 changes: 5 additions & 0 deletions tests/joodle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("The Joodle client class", () => {

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=auth_email_get_signup_settings&wstoken=${token}&moodlewsrestformat=json`)
.twice()
.reply(200, {
namefields: [
"firstname",
Expand Down Expand Up @@ -45,4 +46,8 @@ describe("The Joodle client class", () => {
it("should catch errors returned by Moodle", () => {
return expect(joodle.core.webservice.getSiteInfo()).rejects.toBeDefined();
});

it("should expose the raw HTTP response through the getHttpResponse() function", () => {
return joodle.auth.email.getSignUpSettings().then((response) => expect(response.getHttpResponse()).toBeDefined());
});
});

0 comments on commit b053696

Please sign in to comment.