Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(httptransport): injected fetcher #330

Merged
merged 2 commits into from
Jan 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions src/transports/HTTPTransport.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
import fetch from "isomorphic-fetch";
import { Transport } from "./Transport";
import { JSONRPCRequestData, getNotifications, getBatchRequests } from "../Request";
import {
JSONRPCRequestData,
getNotifications,
getBatchRequests,
} from "../Request";
import { ERR_UNKNOWN, JSONRPCError } from "../Error";

type CredentialsOption = "omit" | "same-origin" | "include"
type CredentialsOption = "omit" | "same-origin" | "include";

interface HTTPTransportOptions {
credentials?: CredentialsOption
headers?: Record<string, string>
credentials?: CredentialsOption;
headers?: Record<string, string>;
fetcher?: typeof fetch;
}

class HTTPTransport extends Transport {
public uri: string;
private readonly credentials?: CredentialsOption;
private readonly headers: Headers
private readonly headers: Headers;
private readonly injectedFetcher?: typeof fetch;
constructor(uri: string, options?: HTTPTransportOptions) {
super();
this.uri = uri;
this.credentials = options && options.credentials;
this.headers = HTTPTransport.setupHeaders(options && options.headers)
this.headers = HTTPTransport.setupHeaders(options && options.headers);
this.injectedFetcher = options?.fetcher;
}
public connect(): Promise<any> {
return Promise.resolve();
}

public async sendData(data: JSONRPCRequestData, timeout: number | null = null): Promise<any> {
public async sendData(
data: JSONRPCRequestData,
timeout: number | null = null
): Promise<any> {
const prom = this.transportRequestManager.addRequest(data, timeout);
const notifications = getNotifications(data);
const batch = getBatchRequests(data);
const fetcher = this.injectedFetcher || fetch;
try {
const result = await fetch(this.uri, {
const result = await fetcher(this.uri, {
method: "POST",
headers: this.headers,
body: JSON.stringify(this.parseData(data)),
Expand All @@ -50,30 +61,40 @@ class HTTPTransport extends Transport {
}
} catch (e) {
const responseErr = new JSONRPCError(e.message, ERR_UNKNOWN, e);
this.transportRequestManager.settlePendingRequest(notifications, responseErr);
this.transportRequestManager.settlePendingRequest(getBatchRequests(data), responseErr);
this.transportRequestManager.settlePendingRequest(
notifications,
responseErr
);
this.transportRequestManager.settlePendingRequest(
getBatchRequests(data),
responseErr
);
return Promise.reject(responseErr);
}
return prom;
}

// tslint:disable-next-line:no-empty
public close(): void { }
public close(): void {}

private onlyNotifications = (data: JSONRPCRequestData) => {
if (data instanceof Array) {
return data.every((datum) => datum.request.request.id === null || datum.request.request.id === undefined);
return data.every(
(datum) =>
datum.request.request.id === null ||
datum.request.request.id === undefined
);
}
return (data.request.id === null || data.request.id === undefined);
}
return data.request.id === null || data.request.id === undefined;
};

private static setupHeaders(headerOptions?: Record<string, string>): Headers {
const headers = new Headers(headerOptions)
const headers = new Headers(headerOptions);
// Overwrite header options to ensure correct content type.
headers.set("Content-Type", "application/json")
return headers
headers.set("Content-Type", "application/json");
return headers;
}
}

export default HTTPTransport;
export {HTTPTransport, HTTPTransportOptions, CredentialsOption}
export { HTTPTransport, HTTPTransportOptions, CredentialsOption };