typed-rest-client
Advanced tools
Comparing version 0.7.0 to 0.9.0
{ | ||
"name": "typed-rest-client", | ||
"version": "0.7.0", | ||
"version": "0.9.0", | ||
"description": "Node Rest and Http Clients with typings for use with TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "./RestClient.js", |
# Typed Rest and Http Client with TypeScript Typings | ||
A lightweight Rest and Http Client optimized for use with TypeScript and async await. | ||
A lightweight Rest and Http Client optimized for use with TypeScript with generics and async await. | ||
@@ -9,4 +9,21 @@ ## Features | ||
- Http Client with pipe stream support and async/await/Promises | ||
```javascript | ||
import * as rm from 'typed-rest-client/RestClient'; | ||
let restc: rm.RestClient = new rm.RestClient('rest-samples', | ||
'https://mystudentapiserver'); | ||
let res: rm.IRestResponse<Student> = await restc.get<Student>('/students/5'); | ||
console.log(res.statusCode); | ||
console.log(res.result.name); | ||
``` | ||
- Typings included so no need to acquire separately (great for intellisense and no versioning drift) | ||
![intellisense](./docs/intellisense.png) | ||
- Basic, Bearer and NTLM Support out of the box | ||
- Typings included so no need to acquire separately (great for versioning drift) | ||
- Proxy support | ||
- Layered for Rest or Http use | ||
@@ -13,0 +30,0 @@ - Full Samples and Tests included for usage |
import httpm = require('./HttpClient'); | ||
import ifm = require("./Interfaces"); | ||
export interface IRestClientResponse { | ||
export interface IRestResponse<T> { | ||
statusCode: number; | ||
result: any; | ||
result: T; | ||
} | ||
export interface IRequestOptions { | ||
acceptHeader?: string; | ||
additionalHeaders?: ifm.IHeaders; | ||
responseProcessor?: Function; | ||
} | ||
export declare class RestClient { | ||
client: httpm.HttpClient; | ||
versionParam: string; | ||
constructor(userAgent: string, handlers?: ifm.IRequestHandler[], socketTimeout?: number, versionParam?: string); | ||
get(requestUrl: string, apiVersion: string, additionalHeaders?: ifm.IHeaders): Promise<IRestClientResponse>; | ||
del(requestUrl: string, apiVersion: string, additionalHeaders?: ifm.IHeaders): Promise<IRestClientResponse>; | ||
create(requestUrl: string, apiVersion: string, resources: any, additionalHeaders?: ifm.IHeaders): Promise<IRestClientResponse>; | ||
update(requestUrl: string, apiVersion: string, resources: any, additionalHeaders?: ifm.IHeaders): Promise<IRestClientResponse>; | ||
replace(requestUrl: string, apiVersion: string, resources: any, additionalHeaders?: ifm.IHeaders): Promise<IRestClientResponse>; | ||
uploadStream(verb: string, requestUrl: string, apiVersion: string, stream: NodeJS.ReadableStream, additionalHeaders: ifm.IHeaders): Promise<IRestClientResponse>; | ||
createAcceptHeader(type: string, apiVersion?: string): string; | ||
private _processResponse(res); | ||
/** | ||
* Creates an instance of the RestClient | ||
* @constructor | ||
* @param {string} userAgent - userAgent for requests | ||
* @param {string} baseUrl - (Optional) If not specified, use full urls per request. If supplied and a function passes a relative url, it will be appended to this | ||
* @param {ifm.IRequestHandler[]} handlers - handlers are typically auth handlers (basic, bearer, ntlm supplied) | ||
* @param {number} socketTimeout - default socket timeout. Can also supply per method | ||
*/ | ||
constructor(userAgent: string, baseUrl?: string, handlers?: ifm.IRequestHandler[], socketTimeout?: number); | ||
private _baseUrl; | ||
/** | ||
* Gets a resource from an endpoint | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
get<T>(requestUrl: string, options?: IRequestOptions): Promise<IRestResponse<T>>; | ||
/** | ||
* Deletes a resource from an endpoint | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
del<T>(requestUrl: string, options?: IRequestOptions): Promise<IRestResponse<T>>; | ||
/** | ||
* Creates resource(s) from an endpoint | ||
* T type of object returned. | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
create<T>(requestUrl: string, resources: any, options?: IRequestOptions): Promise<IRestResponse<T>>; | ||
/** | ||
* Updates resource(s) from an endpoint | ||
* T type of object returned. | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
update<T>(requestUrl: string, resources: any, options?: IRequestOptions): Promise<IRestResponse<T>>; | ||
/** | ||
* Replaces resource(s) from an endpoint | ||
* T type of object returned. | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
replace<T>(requestUrl: string, resources: any, options?: IRequestOptions): Promise<IRestResponse<T>>; | ||
uploadStream<T>(verb: string, requestUrl: string, stream: NodeJS.ReadableStream, options?: IRequestOptions): Promise<IRestResponse<T>>; | ||
private _headersFromOptions(options, contentType?); | ||
private _processResponse<T>(res, options); | ||
} |
@@ -13,66 +13,114 @@ // Copyright (c) Microsoft. All rights reserved. | ||
const httpm = require("./HttpClient"); | ||
const util = require("./Util"); | ||
class RestClient { | ||
constructor(userAgent, handlers, socketTimeout, versionParam) { | ||
// TODO: should we really do this? | ||
this.versionParam = versionParam || 'api-version'; | ||
/** | ||
* Creates an instance of the RestClient | ||
* @constructor | ||
* @param {string} userAgent - userAgent for requests | ||
* @param {string} baseUrl - (Optional) If not specified, use full urls per request. If supplied and a function passes a relative url, it will be appended to this | ||
* @param {ifm.IRequestHandler[]} handlers - handlers are typically auth handlers (basic, bearer, ntlm supplied) | ||
* @param {number} socketTimeout - default socket timeout. Can also supply per method | ||
*/ | ||
constructor(userAgent, baseUrl, handlers, socketTimeout) { | ||
this.client = new httpm.HttpClient(userAgent, handlers, socketTimeout); | ||
if (baseUrl) { | ||
this._baseUrl = baseUrl; | ||
} | ||
} | ||
get(requestUrl, apiVersion, additionalHeaders) { | ||
/** | ||
* Gets a resource from an endpoint | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
get(requestUrl, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
var headers = additionalHeaders || {}; | ||
headers["Accept"] = this.createAcceptHeader('application/json', apiVersion); | ||
let res = yield this.client.get(requestUrl, headers); | ||
return this._processResponse(res); | ||
let url = util.getUrl(requestUrl, this._baseUrl); | ||
let res = yield this.client.get(url, this._headersFromOptions(options)); | ||
return this._processResponse(res, options); | ||
}); | ||
} | ||
del(requestUrl, apiVersion, additionalHeaders) { | ||
/** | ||
* Deletes a resource from an endpoint | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
del(requestUrl, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
var headers = additionalHeaders || {}; | ||
headers["Accept"] = this.createAcceptHeader('application/json', apiVersion); | ||
let res = yield this.client.del(requestUrl, headers); | ||
return this._processResponse(res); | ||
let url = util.getUrl(requestUrl, this._baseUrl); | ||
let res = yield this.client.del(url, this._headersFromOptions(options)); | ||
return this._processResponse(res, options); | ||
}); | ||
} | ||
create(requestUrl, apiVersion, resources, additionalHeaders) { | ||
/** | ||
* Creates resource(s) from an endpoint | ||
* T type of object returned. | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
create(requestUrl, resources, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
var headers = additionalHeaders || {}; | ||
headers["Accept"] = this.createAcceptHeader('application/json', apiVersion); | ||
headers["Content-Type"] = headers["Content-Type"] || 'application/json; charset=utf-8'; | ||
let url = util.getUrl(requestUrl, this._baseUrl); | ||
let headers = this._headersFromOptions(options, true); | ||
let data = JSON.stringify(resources, null, 2); | ||
let res = yield this.client.post(requestUrl, data, headers); | ||
return this._processResponse(res); | ||
let res = yield this.client.post(url, data, headers); | ||
return this._processResponse(res, options); | ||
}); | ||
} | ||
update(requestUrl, apiVersion, resources, additionalHeaders) { | ||
/** | ||
* Updates resource(s) from an endpoint | ||
* T type of object returned. | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
update(requestUrl, resources, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
var headers = additionalHeaders || {}; | ||
headers["Accept"] = this.createAcceptHeader('application/json', apiVersion); | ||
headers["Content-Type"] = headers["Content-Type"] || 'application/json; charset=utf-8'; | ||
let url = util.getUrl(requestUrl, this._baseUrl); | ||
let headers = this._headersFromOptions(options, true); | ||
let data = JSON.stringify(resources, null, 2); | ||
let res = yield this.client.patch(requestUrl, data, headers); | ||
return this._processResponse(res); | ||
let res = yield this.client.patch(url, data, headers); | ||
return this._processResponse(res, options); | ||
}); | ||
} | ||
replace(requestUrl, apiVersion, resources, additionalHeaders) { | ||
/** | ||
* Replaces resource(s) from an endpoint | ||
* T type of object returned. | ||
* Be aware that not found returns a null. Other error conditions reject the promise | ||
* @param {string} requestUrl - fully qualified or relative url | ||
* @param {IRequestOptions} requestOptions - (optional) requestOptions object | ||
*/ | ||
replace(requestUrl, resources, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
var headers = additionalHeaders || {}; | ||
headers["Accept"] = this.createAcceptHeader('application/json', apiVersion); | ||
headers["Content-Type"] = headers["Content-Type"] || 'application/json; charset=utf-8'; | ||
let url = util.getUrl(requestUrl, this._baseUrl); | ||
let headers = this._headersFromOptions(options, true); | ||
let data = JSON.stringify(resources, null, 2); | ||
let res = yield this.client.put(requestUrl, data, headers); | ||
return this._processResponse(res); | ||
let res = yield this.client.put(url, data, headers); | ||
return this._processResponse(res, options); | ||
}); | ||
} | ||
uploadStream(verb, requestUrl, apiVersion, stream, additionalHeaders) { | ||
uploadStream(verb, requestUrl, stream, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
var headers = additionalHeaders || {}; | ||
headers["Accept"] = this.createAcceptHeader('application/json', apiVersion); | ||
let res = yield this.client.sendStream(verb, requestUrl, stream, headers); | ||
return this._processResponse(res); | ||
let url = util.getUrl(requestUrl, this._baseUrl); | ||
let headers = this._headersFromOptions(options, true); | ||
let res = yield this.client.sendStream(verb, url, stream, headers); | ||
return this._processResponse(res, options); | ||
}); | ||
} | ||
createAcceptHeader(type, apiVersion) { | ||
return type + (apiVersion ? (';' + this.versionParam + '=' + apiVersion) : ''); | ||
// should move to the consumer | ||
// public createAcceptHeader(type: string, apiVersion?: string): string { | ||
// return type + (apiVersion ? (';' + this.versionParam + '=' + apiVersion) : ''); | ||
// } | ||
_headersFromOptions(options, contentType) { | ||
options = options || {}; | ||
let headers = options.additionalHeaders || {}; | ||
headers["Accept"] = options.acceptHeader || "application/json"; | ||
if (contentType) { | ||
headers["Content-Type"] = headers["Content-Type"] || 'application/json; charset=utf-8'; | ||
} | ||
return headers; | ||
} | ||
_processResponse(res) { | ||
_processResponse(res, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -93,3 +141,8 @@ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
obj = JSON.parse(contents); | ||
rres.result = obj; | ||
if (options && options.responseProcessor) { | ||
rres.result = options.responseProcessor(obj); | ||
} | ||
else { | ||
rres.result = obj; | ||
} | ||
} | ||
@@ -96,0 +149,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43180
15
866
77