typed-rest-client
Advanced tools
| /** | ||
| * creates an url from a request url and optional base url (http://server:8080) | ||
| * @param {string} requestUrl - a fully qualified url or relative url | ||
| * @param {string} baseUrl - an optional baseUrl (http://server:8080) | ||
| * @return {string} - resultant url | ||
| */ | ||
| export declare function getUrl(requestUrl: string, baseUrl?: string): string; |
+23
| "use strict"; | ||
| const url = require("url"); | ||
| /** | ||
| * creates an url from a request url and optional base url (http://server:8080) | ||
| * @param {string} requestUrl - a fully qualified url or relative url | ||
| * @param {string} baseUrl - an optional baseUrl (http://server:8080) | ||
| * @return {string} - resultant url | ||
| */ | ||
| function getUrl(requestUrl, baseUrl) { | ||
| if (!baseUrl) { | ||
| return requestUrl; | ||
| } | ||
| let base = url.parse(baseUrl); | ||
| // requestUrl (specific per request) always wins | ||
| let combined = url.parse(requestUrl); | ||
| combined.protocol = combined.protocol || base.protocol; | ||
| combined.auth = combined.auth || base.auth; | ||
| combined.host = combined.host || base.host; | ||
| // path from requestUrl always wins | ||
| let res = url.format(combined); | ||
| return res; | ||
| } | ||
| exports.getUrl = getUrl; |
+1
-1
| { | ||
| "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", |
+19
-2
| # 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) | ||
|  | ||
| - 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 |
+58
-11
| 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); | ||
| } |
+93
-40
@@ -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 @@ } |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
43180
15.89%15
15.38%866
17.66%77
28.33%