Skip to content

Commit

Permalink
Removed unnecessary code from S2APIResponse.
Browse files Browse the repository at this point in the history
  • Loading branch information
T99 committed Jul 14, 2019
1 parent 7c6924d commit dfb7743
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 48 deletions.
8 changes: 3 additions & 5 deletions .d.ts/s2-api-response.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export declare class S2APIResponse<R> {
private readonly rawXHR;
private isVerified;
constructor(response: XMLHttpRequest, lazyInitialization?: boolean);
verify(): boolean;
private readonly response;
private readonly statusCode;
constructor(response: R, statusCode: number);
getBody(): R;
getStatusCode(): number;
getRawXHR(): XMLHttpRequest;
}
22 changes: 20 additions & 2 deletions js/s2-api-response.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/s2-api-response.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "@subscribeto/ts-api",
"version": "0.1.0",
"version": "0.1.1",
"description": "A collection of utility classes for accessing the sub.scribe.to API.",
"publishConfig": {
"access": "public"
},
"main": "js/main",
"types": ".d.ts/main",
"scripts": {
"build": "../ts-api2/node_modules/.bin/gulp && jest",
"test": "../ts-api2/node_modules/.bin/jest"
"build": "gulp && jest",
"test": "jest"
},
"repository": {
"type": "git",
Expand Down
45 changes: 8 additions & 37 deletions ts/s2-api-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,27 @@ import { S2API } from "./s2-api";
*/
export class S2APIResponse<R> {

private readonly rawXHR: XMLHttpRequest;
private readonly response: R;

private isVerified: boolean = false;
private readonly statusCode: number;

public constructor(response: XMLHttpRequest, lazyInitialization: boolean = false) {
public constructor(response: R, statusCode: number) {

this.rawXHR = response;

if (!lazyInitialization) this.verify();
this.response = response;
this.statusCode = statusCode;

}

public verify(): boolean {

if (this.isVerified) return true;

// TODO [7/11/19 @ 4:32 PM] - Finish the 'verify' method.

this.isVerified = true;

return true;

}

public getBody(): R {

if (!this.isVerified) {

if (!this.verify()) {

throw new Error("ERR | The response body did not conform to the expected type.");

}

}

if (S2API.isJSON(this.rawXHR.response)) return JSON.parse(this.rawXHR.response) as unknown as R;
else return this.rawXHR.response as unknown as R;
if (S2API.isJSON(this.response)) return JSON.parse(this.response as any) as unknown as R;
else return this.response as unknown as R;

}

public getStatusCode(): number {

return this.rawXHR.status;

}

public getRawXHR(): XMLHttpRequest {

return this.rawXHR;
return this.statusCode;

}

Expand Down

0 comments on commit dfb7743

Please sign in to comment.