mappersmith
Advanced tools
Comparing version 2.40.0 to 2.41.0-beta.0
@@ -12,2 +12,4 @@ "use strict"; | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
@@ -126,3 +128,3 @@ | ||
if (!definition || !definition.path) { | ||
if (!definition || !['string', 'function'].includes(_typeof(definition.path))) { | ||
throw new Error("[Mappersmith] path is undefined for resource \"".concat(resourceName, "\" method \"").concat(methodName, "\"")); | ||
@@ -129,0 +131,0 @@ } |
@@ -18,2 +18,3 @@ import type { Headers, RequestParams, ParameterEncoderFn, Params } from './types'; | ||
path: string | ((args: RequestParams) => string); | ||
pathAttr?: string; | ||
queryParamAlias?: Record<string, string>; | ||
@@ -39,2 +40,3 @@ timeoutAttr?: string; | ||
* @param {String|Function} params.path | ||
* @param {String} params.pathAttr. Default: 'path' | ||
* @param {Object} params.queryParamAlias | ||
@@ -57,2 +59,3 @@ * @param {Number} params.timeoutAttr - timeout attribute name. Default: 'timeout' | ||
readonly path: string | ((args: RequestParams) => string); | ||
readonly pathAttr: string; | ||
readonly queryParamAlias: Record<string, string>; | ||
@@ -59,0 +62,0 @@ readonly timeoutAttr: string; |
@@ -33,2 +33,3 @@ "use strict"; | ||
* @param {String|Function} params.path | ||
* @param {String} params.pathAttr. Default: 'path' | ||
* @param {Object} params.queryParamAlias | ||
@@ -66,2 +67,4 @@ * @param {Number} params.timeoutAttr - timeout attribute name. Default: 'timeout' | ||
_defineProperty(this, "pathAttr", void 0); | ||
_defineProperty(this, "queryParamAlias", void 0); | ||
@@ -84,2 +87,3 @@ | ||
this.hostAttr = params.hostAttr || 'host'; | ||
this.pathAttr = params.pathAttr || 'path'; | ||
this.timeoutAttr = params.timeoutAttr || 'timeout'; | ||
@@ -86,0 +90,0 @@ var resourceMiddleware = params.middleware || params.middlewares || []; |
@@ -122,3 +122,3 @@ "use strict"; | ||
reject(new _response.Response(request, 400, errorMessage, {}, [response])); | ||
reject(new _response.Response(request, 400, errorMessage, {}, [new Error(errorMessage)])); | ||
} | ||
@@ -125,0 +125,0 @@ } |
{ | ||
"name": "mappersmith", | ||
"version": "2.40.0", | ||
"version": "2.41.0-beta.0", | ||
"description": "It is a lightweight rest client for node.js and the browser", | ||
@@ -5,0 +5,0 @@ "author": "Tulio Ornelas <ornelas.tulio@gmail.com>", |
@@ -21,2 +21,3 @@ [![npm version](https://badge.fury.io/js/mappersmith.svg)](http://badge.fury.io/js/mappersmith) | ||
- [Alternative host](#alternative-host) | ||
- [Alternative path](#alternative-path) | ||
- [Binary data](#binary-data) | ||
@@ -374,2 +375,28 @@ - [Promises](#promises) | ||
### <a name="alternative-path"></a> Alternative path | ||
In case you need to overwrite the path for a specific call, you can do so through the param `path`: | ||
```javascript | ||
// ... | ||
{ | ||
all: { path: '/users' } | ||
} | ||
// ... | ||
client.User.all({ path: '/people' }) | ||
``` | ||
If `path` is not possible as a special parameter for your API, you can configure it through the param `pathAttr`: | ||
```javascript | ||
// ... | ||
{ | ||
all: { path: '/users', pathAttr: '__path' } | ||
} | ||
// ... | ||
client.User.all({ __path: '/people' }) | ||
``` | ||
### <a name="binary-data"></a> Binary data | ||
@@ -376,0 +403,0 @@ |
@@ -55,3 +55,3 @@ "use strict"; | ||
value: function isParam(key) { | ||
return key !== this.methodDescriptor.headersAttr && key !== this.methodDescriptor.bodyAttr && key !== this.methodDescriptor.authAttr && key !== this.methodDescriptor.timeoutAttr && key !== this.methodDescriptor.hostAttr; | ||
return key !== this.methodDescriptor.headersAttr && key !== this.methodDescriptor.bodyAttr && key !== this.methodDescriptor.authAttr && key !== this.methodDescriptor.timeoutAttr && key !== this.methodDescriptor.hostAttr && key !== this.methodDescriptor.pathAttr; | ||
} | ||
@@ -126,13 +126,17 @@ }, { | ||
var _this$methodDescripto2 = this.methodDescriptor, | ||
mdPathAttr = _this$methodDescripto2.pathAttr, | ||
mdPath = _this$methodDescripto2.path; | ||
var originalPath = this.requestParams[mdPathAttr] || mdPath || ''; | ||
var params = this.params(); | ||
var path; | ||
if (typeof this.methodDescriptor.path === 'function') { | ||
path = this.methodDescriptor.path(params); | ||
if (typeof originalPath === 'function') { | ||
path = originalPath(params); | ||
if (typeof path !== 'string') { | ||
throw new Error("[Mappersmith] method descriptor function did not return a string, params=".concat(JSON.stringify(params))); | ||
} | ||
} else { | ||
path = this.methodDescriptor.path; | ||
} | ||
if (path[0] !== '/') { | ||
path = "/".concat(path); | ||
path = originalPath; | ||
} // RegExp with 'g'-flag is stateful, therefore defining it locally | ||
@@ -187,2 +191,7 @@ | ||
path += "".concat(hasQuery ? '&' : '?').concat(queryString); | ||
} // https://www.rfc-editor.org/rfc/rfc1738#section-3.3 | ||
if (path[0] !== '/' && path.length > 0) { | ||
path = "/".concat(path); | ||
} | ||
@@ -292,3 +301,4 @@ | ||
var hostKey = this.methodDescriptor.hostAttr; | ||
var timeoutKey = this.methodDescriptor.timeoutAttr; // Note: The result of merging an instance of RequestParams with instance of Params | ||
var timeoutKey = this.methodDescriptor.timeoutAttr; | ||
var pathKey = this.methodDescriptor.pathAttr; // Note: The result of merging an instance of RequestParams with instance of Params | ||
// is simply a RequestParams with even more [param: string]'s on it. | ||
@@ -304,2 +314,3 @@ | ||
extras.timeout && (requestParams[timeoutKey] = extras.timeout); | ||
extras.path && (requestParams[pathKey] = extras.path); | ||
@@ -306,0 +317,0 @@ var nextContext = _objectSpread(_objectSpread({}, this.requestContext), requestContext); |
@@ -24,5 +24,5 @@ import { Request } from './request'; | ||
readonly responseHeaders: Headers; | ||
readonly errors: Array<Response | Error | string>; | ||
readonly errors: Array<Error | string>; | ||
timeElapsed: number | null; | ||
constructor(originalRequest: Request, responseStatus: number, responseData?: string | null, responseHeaders?: Headers, errors?: Array<Response | Error | string>); | ||
constructor(originalRequest: Request, responseStatus: number, responseData?: string | null, responseHeaders?: Headers, errors?: Array<Error | string>); | ||
request(): Request; | ||
@@ -58,3 +58,3 @@ status(): number; | ||
*/ | ||
error(): Error | Response<ParsedJSON> | null; | ||
error(): Error | null; | ||
/** | ||
@@ -61,0 +61,0 @@ * Enhances current Response returning a new Response |
@@ -27,2 +27,3 @@ export declare type Primitive = string | number | boolean; | ||
readonly host?: string; | ||
readonly path?: string; | ||
readonly params?: Params; | ||
@@ -29,0 +30,0 @@ readonly timeout?: number; |
@@ -1,1 +0,1 @@ | ||
{"version":"2.40.0"} | ||
{"version":"2.41.0-beta.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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
245725
4280
1435
1