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

Support HTTP GET #344

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import RequestManager from "./RequestManager";
import { JSONRPCError } from "./Error";
import { IClient, RequestArguments, NotificationArguments } from "./ClientInterface";
import { IJSONRPCNotification } from "./Request";
import { TransportRequestOptions } from "./transports/Transport";

/**
* OpenRPC Client JS is a browser-compatible JSON-RPC client with multiple transports and
Expand Down Expand Up @@ -67,11 +68,11 @@ class Client implements IClient {
* @example
* myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar'));
*/
public async request(requestObject: RequestArguments, timeout?: number) {
public async request(requestObject: RequestArguments, timeout?: number, transportOptions?: TransportRequestOptions) {
if (this.requestManager.connectPromise) {
await this.requestManager.connectPromise;
}
return this.requestManager.request(requestObject, false, timeout);
return this.requestManager.request(requestObject, false, timeout, transportOptions);
}

public async notify(requestObject: NotificationArguments) {
Expand Down
6 changes: 3 additions & 3 deletions src/RequestManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Transport } from "./transports/Transport";
import { Transport, TransportRequestOptions } from "./transports/Transport";
import { IJSONRPCRequest, IJSONRPCNotification, IBatchRequest } from "./Request";
import { JSONRPCError } from "./Error";
import StrictEventEmitter from "strict-event-emitter-types";
Expand Down Expand Up @@ -53,7 +53,7 @@ class RequestManager {
return this.transports[0];
}

public async request(requestObject: JSONRPCMessage, notification: boolean = false, timeout?: number | null): Promise<any> {
public async request(requestObject: JSONRPCMessage, notification: boolean = false, timeout?: number | null, transportOptions?: TransportRequestOptions): Promise<any> {
const internalID = this.nextID().toString();
const id = notification ? null : internalID;
// naively grab first transport and use it
Expand All @@ -64,7 +64,7 @@ class RequestManager {
});
return result;
}
return this.getPrimaryTransport().sendData(payload, timeout);
return this.getPrimaryTransport().sendData(payload, timeout, transportOptions);
}

public close(): void {
Expand Down
29 changes: 23 additions & 6 deletions src/transports/HTTPTransport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from "isomorphic-fetch";
import { Transport } from "./Transport";
import { Transport, HTTPTransportRequestOptions } from "./Transport";
import {
JSONRPCRequestData,
getNotifications,
Expand All @@ -13,39 +13,56 @@ interface HTTPTransportOptions {
credentials?: CredentialsOption;
headers?: Record<string, string>;
fetcher?: typeof fetch;
getQueryParamName?: string;
}

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

public async sendData(
data: JSONRPCRequestData,
timeout: number | null = null
timeout: number | null = null,
requestOptions?: HTTPTransportRequestOptions,
): Promise<any> {
if (requestOptions === undefined) {
requestOptions = {method: "POST"};
}
const prom = this.transportRequestManager.addRequest(data, timeout);
const notifications = getNotifications(data);
const batch = getBatchRequests(data);
const fetcher = this.injectedFetcher || fetch;
try {
const result = await fetcher(this.uri, {
method: "POST",
const parsedData = JSON.stringify(this.parseData(data));
let uri = this.uri;
const params = {
method: requestOptions.method,
headers: this.headers,
body: JSON.stringify(this.parseData(data)),
credentials: this.credentials,
});
} as RequestInit;
if (requestOptions.method === 'GET') {
const q = new URLSearchParams();
q.set(this.getQueryParamName, parsedData);
uri += "?" + q.toString();
}
else {
params.body = parsedData;
}
const result = await fetcher(uri, params);
// requirements are that notifications are successfully sent
this.transportRequestManager.settlePendingRequest(notifications);
if (this.onlyNotifications(data)) {
Expand Down
7 changes: 6 additions & 1 deletion src/transports/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ interface ITransportEvents {
error: (data: JSONRPCError) => void;
}

export type HTTPTransportRequestOptions = {
method: "GET" | "POST";
}
export type TransportRequestOptions = HTTPTransportRequestOptions;

type TransportEventName = keyof ITransportEvents;
export type TransportEventChannel = StrictEventEmitter<EventEmitter, ITransportEvents>;

Expand All @@ -30,7 +35,7 @@ export abstract class Transport {

public abstract connect(): Promise<any>;
public abstract close(): void;
public abstract async sendData(data: JSONRPCRequestData, timeout?: number | null): Promise<any>;
public abstract async sendData(data: JSONRPCRequestData, timeout?: number | null, transportOptions?: TransportRequestOptions): Promise<any>;

public subscribe(event: TransportEventName, handler: ITransportEvents[TransportEventName]) {
this.transportRequestManager.transportEventChannel.addListener(event, handler);
Expand Down