Skip to content

Commit

Permalink
Merge pull request #16 from vansergen/types
Browse files Browse the repository at this point in the history
Update return types
  • Loading branch information
vansergen authored Jan 18, 2020
2 parents 5727771 + 543fc4a commit 1b29c51
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 169 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ npm install rpc-request
Please refer to the [`request` documentation](https://github.com/request/request#requestdefaultsoptions) for the full list of all supported options you can pass to the constructor.

```javascript
const { RPC } = require("rpc-request");
const url = "http://localhost:8332";
const headers = { "content-type": "text/plain" };
const auth = { user: "rpcuser", pass: "rpcpass" };
const rpc = new RPC({ url, headers, auth, method: "POST" });
const info = await rpc.request({
body: JSON.stringify({ method: "getwalletinfo" })
});
import { RPC } from "rpc-request";
class MyClass extends RPC {
constructor() {
super({ uri: "http://worldtimeapi.org/api/ip", json: true });
}
}
const myClass = new MyClass();
myClass
.get()
.then(data => console.log(data))
.catch(error => console.error(error));
```

- [`request`](https://github.com/request/request#requestoptions-callback)
Expand Down
85 changes: 1 addition & 84 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1 @@
import * as request from "request-promise-native";
import { Cookie, CookieJar } from "request";

export type RPCOptions = request.RequestPromiseOptions | request.Options;

export class RPC {
readonly _rpoptions: RPCOptions;

constructor(options: RPCOptions = {}) {
this._rpoptions = options;
}

async get(options: RPCOptions = {}): Promise<any> {
return this.request({ ...options, method: "GET" });
}

async post(options: RPCOptions = {}): Promise<any> {
return this.request({ ...options, method: "POST" });
}

async put(options: RPCOptions = {}): Promise<any> {
return this.request({ ...options, method: "PUT" });
}

async patch(options: RPCOptions = {}): Promise<any> {
return this.request({ ...options, method: "PATCH" });
}

async delete(options: RPCOptions = {}): Promise<any> {
return this.request({ ...options, method: "DELETE" });
}

async head(options: RPCOptions = {}): Promise<any> {
return this.request({ ...options, method: "HEAD" });
}

async options(options: RPCOptions = {}): Promise<any> {
return this.request({ ...options, method: "OPTIONS" });
}

async request(options: RPCOptions = {}): Promise<any> {
return this.defaults(RPC.prepareOptions(options, this._rpoptions));
}

/**
* Create a new cookie
*/
static cookie(key: string, value: string): Cookie | undefined {
return request.cookie(key + "=" + value);
}

/**
* Create a new cookie jar
*/
static jar(cookieStore?: any): CookieJar {
return request.jar(cookieStore);
}

static defaults(
options: request.RequestPromiseOptions = {}
): request.RequestPromiseAPI {
return request.defaults(options);
}

static prepareOptions(
options: RPCOptions,
rpoptions: RPCOptions
): request.Options {
if (!("url" in options || "uri" in options)) {
if (!("url" in rpoptions || "uri" in rpoptions)) {
throw new Error("options.uri is a required argument");
}
if ("uri" in rpoptions) {
return { ...options, uri: rpoptions.uri };
}
return { ...options, uri: rpoptions.url };
}
return options;
}

get defaults(): request.RequestPromiseAPI {
return RPC.defaults(this._rpoptions);
}
}
export * from "./src/rpc";
138 changes: 66 additions & 72 deletions package-lock.json

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

Loading

0 comments on commit 1b29c51

Please sign in to comment.