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

Feature/add request timeout #5

Merged
merged 4 commits into from
Oct 28, 2016
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Option for request timeout


## [v0.4.0]
Expand Down
22 changes: 22 additions & 0 deletions RestExtractor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,26 @@ describe('RestExtractor', () => {
});
});

it('should set request timeout', done => {
extractor = new RestExtractor('url', RestExtractorMethod.Get, undefined, {
timeout: 42
});
(extractor as any).rest = new SelectorMock();

sinon.spy((extractor as any).rest, 'request');

extractor
.read()
.subscribe(() => {}, err => {
done(err);
}, () => {
try {
(extractor as any).rest.request.should.be.calledWith('url', { method: 'get', timeout: 42 });
done();
} catch (e) {
done(e);
}
});
});

});
33 changes: 28 additions & 5 deletions RestExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ export enum RestExtractorMethod {
Put
}

type MethodOptions = {
method?: string;
};

export type RestExtractorOptions = {
query?: any;
data?: string | any;
parser?: any;
encoding?: string;
decoding?: string;
headers?: { [name: string]: string };
username?: string;
password?: string;
accessToken?: string;
multipart?: any;
client?: any;
followRedirects?: boolean;
timeout?: number;
rejectUnauthorized?: boolean;
agent?: any;
};

/**
*
*/
Expand All @@ -18,16 +40,17 @@ export class RestExtractor implements Extractor {
* @param url
* @param method
* @param resultSelector
* @param {number} [timeout=120000] Request timeout in milliseconds
*/
constructor(private url:string, private method:RestExtractorMethod = RestExtractorMethod.Get, private resultSelector:(obj:any) => any = o => o) {
constructor(private url:string, private method:RestExtractorMethod = RestExtractorMethod.Get, private resultSelector:(obj:any) => any = o => o, private restlerOptions: RestExtractorOptions = {}) {
}

public read():Observable<any> {
public read(): Observable<any> {
return Observable.create((observer:Observer<any>) => {
let options: MethodOptions & RestExtractorOptions = this.restlerOptions;
options.method = this.getUrlMethod();
this.rest
.request(this.url, {
method: this.getUrlMethod()
})
.request(this.url, options)
.on('error', err => {
observer.error(err);
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
"dependencies": {
"proc-that": "^0.4.0",
"restler": "^3.4.0",
"rxjs": "^5.0.0-beta.10"
"rxjs": "5.0.0-beta.10"
}
}