mappersmith
Advanced tools
Comparing version 2.39.1 to 2.40.0
import type { GlobalConfigs, ManifestOptions, ResourceTypeConstraint } from './manifest' | ||
export type { GlobalConfigs, ManifestOptions, ResourceTypeConstraint } | ||
export type { Request } from './request' | ||
export type { Request, RequestContext } from './request' | ||
export type { Headers, Params as Parameters, Auth as Authorization } from './types' | ||
@@ -5,0 +5,0 @@ export type { Gateway } from './gateway' |
{ | ||
"name": "mappersmith", | ||
"version": "2.39.1", | ||
"version": "2.40.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>", |
@@ -424,4 +424,2 @@ [![npm version](https://badge.fury.io/js/mappersmith.svg)](http://badge.fury.io/js/mappersmith) | ||
The `response` method receives a function which returns a `Promise` resolving the [Response](https://github.com/tulios/mappersmith/blob/master/src/response.js). This function must return a `Promise` resolving the Response. The method `enhance` can be used to generate a new response based on the previous one. | ||
```javascript | ||
@@ -433,4 +431,32 @@ const MyMiddleware = () => ({ | ||
})) | ||
}, | ||
} | ||
}) | ||
``` | ||
If you have multiple middleware it is possible to pass information from an earlier ran middleware to a later one via the request context: | ||
```javascript | ||
const MyMiddlewareOne = () => ({ | ||
async prepareRequest(next) { | ||
const request = await next().then(request => request.enhance({}, { message: 'hello from mw1' })) | ||
} | ||
}) | ||
const MyMiddlewareTwo = () => ({ | ||
async prepareRequest(next) { | ||
const request = await next() | ||
const { message } = request.getContext() | ||
// Logs: "hello from mw1" | ||
console.log(message) | ||
return request | ||
} | ||
}) | ||
``` | ||
The above example assumes you synthesized your middleware in this order when calling `forge`: `middleware: [MyMiddlewareOne, MyMiddlewareTwo]` | ||
The `response` method receives a function which returns a `Promise` resolving the [Response](https://github.com/tulios/mappersmith/blob/master/src/response.js). This function must return a `Promise` resolving the Response. The method `enhance` can be used to generate a new response based on the previous one. | ||
```javascript | ||
const MyMiddleware = () => ({ | ||
response(next) { | ||
@@ -437,0 +463,0 @@ return next().then((response) => response.enhance({ |
import { MethodDescriptor } from './method-descriptor'; | ||
import type { Auth, Body, Headers, RequestParams } from './types'; | ||
export declare type RequestContext = Record<string, unknown>; | ||
/** | ||
@@ -11,2 +12,3 @@ * Removes the object type without removing Record types in the union | ||
* @param {RequestParams} requestParams, defaults to an empty object ({}) | ||
* @param {RequestContext} request context store, defaults to an empty object ({}) | ||
*/ | ||
@@ -16,6 +18,12 @@ export declare class Request { | ||
requestParams: RequestParams; | ||
constructor(methodDescriptor: MethodDescriptor, requestParams?: RequestParams); | ||
private requestContext; | ||
constructor(methodDescriptor: MethodDescriptor, requestParams?: RequestParams, requestContext?: RequestContext); | ||
private isParam; | ||
params(): RequestParams; | ||
/** | ||
* Returns the request context; a key value object. | ||
* Useful to pass information from upstream middleware to a downstream one. | ||
*/ | ||
context<T extends RequestContext = RequestContext>(): T; | ||
/** | ||
* Returns the HTTP method in lowercase | ||
@@ -72,4 +80,5 @@ */ | ||
* @param {Number} extras.timeout - it will replace the current timeout | ||
* @param {Object} requestContext - Use to pass information between different middleware. | ||
*/ | ||
enhance(extras: RequestParams): Request; | ||
enhance(extras: RequestParams, requestContext?: RequestContext): Request; | ||
/** | ||
@@ -76,0 +85,0 @@ * Is the request expecting a binary response? |
@@ -27,5 +27,2 @@ "use strict"; | ||
var REGEXP_TRAILING_SLASH = /\/$/; | ||
/** | ||
* Removes the object type without removing Record types in the union | ||
*/ | ||
@@ -36,2 +33,3 @@ /** | ||
* @param {RequestParams} requestParams, defaults to an empty object ({}) | ||
* @param {RequestContext} request context store, defaults to an empty object ({}) | ||
*/ | ||
@@ -41,2 +39,3 @@ var Request = /*#__PURE__*/function () { | ||
var requestParams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var requestContext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
@@ -49,4 +48,7 @@ _classCallCheck(this, Request); | ||
_defineProperty(this, "requestContext", void 0); | ||
this.methodDescriptor = methodDescriptor; | ||
this.requestParams = requestParams; | ||
this.requestContext = requestContext; | ||
} | ||
@@ -74,2 +76,12 @@ | ||
/** | ||
* Returns the request context; a key value object. | ||
* Useful to pass information from upstream middleware to a downstream one. | ||
*/ | ||
}, { | ||
key: "context", | ||
value: function context() { | ||
return this.requestContext; | ||
} | ||
/** | ||
* Returns the HTTP method in lowercase | ||
@@ -225,2 +237,6 @@ */ | ||
if (typeof headers === 'function') { | ||
return headers; | ||
} | ||
var mergedHeaders = _objectSpread(_objectSpread({}, this.methodDescriptor.headers), headers); | ||
@@ -269,2 +285,3 @@ | ||
* @param {Number} extras.timeout - it will replace the current timeout | ||
* @param {Object} requestContext - Use to pass information between different middleware. | ||
*/ | ||
@@ -274,3 +291,3 @@ | ||
key: "enhance", | ||
value: function enhance(extras) { | ||
value: function enhance(extras, requestContext) { | ||
var authKey = this.methodDescriptor.authAttr; | ||
@@ -291,3 +308,6 @@ var bodyKey = this.methodDescriptor.bodyAttr; | ||
extras.timeout && (requestParams[timeoutKey] = extras.timeout); | ||
return new Request(this.methodDescriptor, requestParams); | ||
var nextContext = _objectSpread(_objectSpread({}, this.requestContext), requestContext); | ||
return new Request(this.methodDescriptor, requestParams, nextContext); | ||
} | ||
@@ -294,0 +314,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import { Request } from '../request'; | ||
import { Request, RequestContext } from '../request'; | ||
import { RequestParams } from '../types'; | ||
@@ -6,2 +6,3 @@ export interface RequestFactoryArgs extends RequestParams { | ||
path?: string; | ||
context?: RequestContext; | ||
} | ||
@@ -12,2 +13,2 @@ /** | ||
*/ | ||
export declare const requestFactory: ({ method, host, path, auth, body, headers, params, timeout, ...rest }?: RequestFactoryArgs) => Request; | ||
export declare const requestFactory: ({ method, host, path, auth, body, headers, params, timeout, context, ...rest }?: RequestFactoryArgs) => Request; |
@@ -12,3 +12,3 @@ "use strict"; | ||
var _excluded = ["method", "host", "path", "auth", "body", "headers", "params", "timeout"]; | ||
var _excluded = ["method", "host", "path", "auth", "body", "headers", "params", "timeout", "context"]; | ||
@@ -43,2 +43,3 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
timeout = _ref.timeout, | ||
context = _ref.context, | ||
rest = _objectWithoutProperties(_ref, _excluded); | ||
@@ -57,5 +58,5 @@ | ||
timeout: timeout | ||
}, rest)); | ||
}, rest), context); | ||
}; | ||
exports.requestFactory = requestFactory; |
@@ -1,1 +0,1 @@ | ||
{"version":"2.39.1"} | ||
{"version":"2.40.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
243934
4262
1408