Skip to content

Commit

Permalink
Add response handling and 304 support to fetchJson.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Sep 28, 2019
1 parent a12030a commit 3d25882
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/ethers/src.ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { UnicodeNormalizationForm } from "@ethersproject/strings";

import { CoerceFunc } from "@ethersproject/abi";
import { Bytes, BytesLike, Hexable } from "@ethersproject/bytes"
import { ConnectionInfo, OnceBlockable, PollOptions } from "@ethersproject/web";
import { ConnectionInfo, FetchJsonResponse, OnceBlockable, PollOptions } from "@ethersproject/web";
import { EncryptOptions, ProgressCallback } from "@ethersproject/json-wallets";

////////////////////////
Expand Down Expand Up @@ -164,6 +164,7 @@ export {
ConnectionInfo,
OnceBlockable,
PollOptions,
FetchJsonResponse,

EncryptOptions,
ProgressCallback
Expand Down
81 changes: 54 additions & 27 deletions packages/web/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ export type PollOptions = {
onceBlock?: OnceBlockable
};

export type FetchJsonResponse = {
statusCode: number;
status: string;
headers: { [ header: string ]: string };
};


type Header = { key: string, value: string };

export function fetchJson(connection: string | ConnectionInfo, json?: string, processFunc?: (value: any) => any): Promise<any> {
export function fetchJson(connection: string | ConnectionInfo, json?: string, processFunc?: (value: any, response: FetchJsonResponse) => any): Promise<any> {
let headers: { [key: string]: Header } = { };

let url: string = null;
Expand All @@ -52,6 +57,8 @@ export function fetchJson(connection: string | ConnectionInfo, json?: string, pr
referrer: "client", // no-referrer, *client
};

let allow304 = false;

let timeout = 2 * 60 * 1000;

if (typeof(connection) === "string") {
Expand All @@ -71,6 +78,9 @@ export function fetchJson(connection: string | ConnectionInfo, json?: string, pr
if (connection.headers) {
for (let key in connection.headers) {
headers[key.toLowerCase()] = { key: key, value: String(connection.headers[key]) };
if (["if-none-match", "if-modified-since"].indexOf(key.toLowerCase()) >= 0) {
allow304 = true;
}
}
}

Expand Down Expand Up @@ -124,42 +134,59 @@ export function fetchJson(connection: string | ConnectionInfo, json?: string, pr

return fetch(url, options).then((response) => {
return response.text().then((body) => {
if (!response.ok) {

let json: any = null;

if (allow304 && response.status === 304) {
// Leave json as null

} else if (!response.ok) {
logger.throwError("bad response", Logger.errors.SERVER_ERROR, {
status: response.status,
body: body,
type: response.type,
url: response.url
});
}

return body;
});

}).then((text) => {
let json: any = null;
try {
json = JSON.parse(text);
} catch (error) {
logger.throwError("invalid JSON", Logger.errors.SERVER_ERROR, {
body: text,
error: error,
url: url
});
}
} else {
try {
json = JSON.parse(body);
} catch (error) {
logger.throwError("invalid JSON", Logger.errors.SERVER_ERROR, {
body: body,
error: error,
url: url
});
}
}

if (processFunc) {
try {
json = processFunc(json);
} catch (error) {
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, {
body: json,
error: error
});
if (processFunc) {
try {
const headers: { [ header: string ]: string } = { };
if (response.headers.forEach) {
response.headers.forEach((value, key) => {
headers[key.toLowerCase()] = value;
});
} else {
(<() => Array<string>>((<any>(response.headers)).keys))().forEach((key) => {
headers[key.toLowerCase()] = response.headers.get(key);
});
}
json = processFunc(json, {
statusCode: response.status,
status: response.statusText,
headers: headers
});
} catch (error) {
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, {
body: json,
error: error
});
}
}
}

return json;
return json;
});

}, (error) => {
throw error;
Expand Down

0 comments on commit 3d25882

Please sign in to comment.