diff --git a/easy-api-request/easy-api-request-tests.ts b/easy-api-request/easy-api-request-tests.ts new file mode 100644 index 000000000..38a4b68dd --- /dev/null +++ b/easy-api-request/easy-api-request-tests.ts @@ -0,0 +1,44 @@ +/** + * Created by karl on 14/07/15. + */ + +/// +/// + +import express = require('express'); +import APIRequest = require('easy-api-request'); + +APIRequest.create({ + name: 'testAPI', + config: { + url: 'http://example.com' + } +}); + +var app = express(); + +app.get('/', function (req:any, res:express.Response) { + var rMaker = req.testAPI; + var r = rMaker(); + r.get('/', function (err, resp) { + if(err) { + console.error(err); + } + res.json(err || resp); + }); +}); + +app.get('/', function (req:any, res:express.Response) { + var rMaker = req.testAPI; + var r = rMaker(); + r.get('/') + .then(function (resp) { + res.json(resp); + }) + .catch(function (err){ + if(err) { + console.error(err); + } + res.json(err); + }); +}); diff --git a/easy-api-request/easy-api-request.d.ts b/easy-api-request/easy-api-request.d.ts new file mode 100644 index 000000000..d3152e49d --- /dev/null +++ b/easy-api-request/easy-api-request.d.ts @@ -0,0 +1,115 @@ +// Type definitions for easy-api-request +// Project: https://github.com/DeadAlready/easy-api-request +// Definitions by: Karl Düüna +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// +/// +/// +/// + +declare module "easy-api-request" { + import stream = require('stream'); + import http = require('http'); + import request = require('request'); + import bunyan = require('bunyan'); + import express = require('express'); + + export function create(opts: { + name: any; + config: { + url: string; + internal?: boolean; + headers?: string[]; + cookies?: string[]; + replyCookies?: string[]; + jSend?: boolean; + opts?: Object; + }; + }): void; + + import Q = require('q'); + interface Result { + response: http.IncomingMessage; + body: any; + err?: any; + data?: any; + } + class BaseRequest { + protected base: request.Request; + protected req: express.Request; + protected log: bunyan.Logger; + protected replyCookies: string[]; + protected jSend: boolean; + constructor(opts: any); + _parseOptions(args: IArguments, type: string): { + opts: any; + cb: any; + }; + _do(args: IArguments, type?: string): any; + _request(opts?: any, cb?:any): any; + + get(url?: any, opts?: any, cb?: any): any; + post(url?: any, opts?: any, cb?: any): any; + patch(url?: any, opts?: any, cb?: any): any; + del(url?: any, opts?: any, cb?: any): any; + } + + class StreamRequest extends BaseRequest { + _request(opts: Object): stream.Stream; + + get(url: string, params: Object): stream.Stream; + get(opts: Object): stream.Stream; + get(url: string): stream.Stream; + + post(url: string, params: Object): stream.Stream; + post(opts: Object): stream.Stream; + post(url: string): stream.Stream; + + patch(url: string, params: Object): stream.Stream; + patch(opts: Object): stream.Stream; + patch(url: string): stream.Stream; + + del(url: string, params: Object): stream.Stream; + del(opts: Object): stream.Stream; + del(url: string): stream.Stream; + } + + class CBPromiseRequest extends BaseRequest { + _request(opts: Object): stream.Stream; + + get(url: string, params: Object, cb:(err?:any, resp?: Result) => void): void; + get(url: string, cb:(err?:any, resp?: Result) => void): void; + get(opts: Object, cb:(err?:any, resp?: Result) => void): void; + get(url: string, params: Object): Q.Promise; + get(opts: Object): Q.Promise; + get(url: string): Q.Promise; + + post(url: string, params: Object, cb:(err?:any, resp?: Result) => void): void; + post(url: string, cb:(err?:any, resp?: Result) => void): void; + post(opts: Object, cb:(err?:any, resp?: Result) => void): void; + post(url: string, params: Object): Q.Promise; + post(opts: Object): Q.Promise; + post(url: string): Q.Promise; + + patch(url: string, params: Object, cb:(err?:any, resp?: Result) => void): void; + patch(url: string, cb:(err?:any, resp?: Result) => void): void; + patch(opts: Object, cb:(err?:any, resp?: Result) => void): void; + patch(url: string, params: Object): Q.Promise; + patch(opts: Object): Q.Promise; + patch(url: string): Q.Promise; + + del(url: string, params: Object, cb:(err?:any, resp?: Result) => void): void; + del(url: string, cb:(err?:any, resp?: Result) => void): void; + del(opts: Object, cb:(err?:any, resp?: Result) => void): void; + del(url: string, params: Object): Q.Promise; + del(opts: Object): Q.Promise; + del(url: string): Q.Promise; + } + + export interface RequestMaker { + (): CBPromiseRequest; + (stream: boolean): StreamRequest | CBPromiseRequest; + } +}