-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from vansergen/types
Update return types
- Loading branch information
Showing
5 changed files
with
167 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.