diff --git a/docs/runtime_suite_libraries/custom-plugin-lib/changelog.md b/docs/runtime_suite_libraries/custom-plugin-lib/changelog.md index a187ec50a7..99aef43a86 100644 --- a/docs/runtime_suite_libraries/custom-plugin-lib/changelog.md +++ b/docs/runtime_suite_libraries/custom-plugin-lib/changelog.md @@ -14,6 +14,25 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v7.0.1 - 2024-12-16 + +### Updated + +- axios: v1.7.9 + +## v7.0.0 - 2024-07-04 + +### BREAKING + +- dropped node 14 and node 16 support +- dropped deprecated method `getServiceProxy` and `getDirectServiceProxy`, see the [migration guide](./docs/migration_guides/10_migrate_from_v6_to_v7.md) for further details. + +### Updated + +- lc39: v8 + +## v6.0.3 + ### Updated - update axios to v1.6.7 @@ -204,6 +223,7 @@ This version contains a bug when it's configured a required env var with a defau ### Fixed - replaced expired test certificates with new ones that will expire on 3 September 3061 + ### Changed - the `getDirectServiceProxy` function now can receive a complete url as `serviceName` parameter diff --git a/docs/runtime_suite_libraries/custom-plugin-lib/http_client.md b/docs/runtime_suite_libraries/custom-plugin-lib/http_client.md index f60999cf3e..0b2b7b3a93 100644 --- a/docs/runtime_suite_libraries/custom-plugin-lib/http_client.md +++ b/docs/runtime_suite_libraries/custom-plugin-lib/http_client.md @@ -92,84 +92,3 @@ async function tokenGeneration(request, response) { // ... } ``` - -:::note -The methods below are deprecated: switch to use the `getHttpClient` method. -::: - -* `getServiceProxy(options)` - returns a proxy passing through the [Microservice Gateway](../../runtime_suite/microservice-gateway/overview). - * `options` - is an object with the following optional fields: - * `port` - an integer that identifies the port of the service to be queried - * `protocol` - a string that identifies the protocol to use (only `http` and `https` are supported, the default value is `http`) - * `headers` - an object that represents the set of headers to send to the service - * `prefix` - a string representing the prefix of the service call path - * `timeout` - set a request timeout - * `agent` - set a custom node agent -* `getDirectServiceProxy(serviceNameOrURL, options)` - returns a direct proxy to the service - * `serviceNameOrURL` - The name of the service to call. You can pass: - * just the hostname, without protocol and port (that you can specify into the `options` field) - * a complete url string (e.g. *http://myurl:3000*); you can include *protocol* and *port* into the url string directly - * `options` - The same options described above - -Potentially, the `getDirectServiceProxy` method allows you to also query services outside the Platform. In this case, however, it is necessary to bear in mind that the platform headers will be automatically forwarded. - -Both proxies, by default, forward the four Mia headers to the service called. In addition, other headers of the original request can also be forwarded to the named service. To do this it is necessary to define an additional environment variable, `ADDITIONAL_HEADERS_TO_PROXY`, whose value must be a string containing the keys of the headers to be forwarded separated by a comma. - -Both proxies expose the methods to perform a specific HTTP request to service. - -* `get(path, querystring, options)` -* `post(path, body, querystring, options)` -* `put(path, body, querystring, options)` -* `patch(path, body, querystring, options)` -* `delete(path, body, querystring, options)` - -All methods return a *Promise object*. You can access to: - -* **Status code** of the response trough the `statusCode` property -* **Body** of the response trough the `payload` property -* **Headers** of the response trough the `headers` property - -The params to be passed to these functions are: - -* `path` - a string that identifies the route to which you want to send the request. -* `body` - optional, the body of the request which can be: - * a JSON object - * a [Buffer](https://nodejs.org/api/buffer.html#) - * one [Stream](https://nodejs.org/api/stream.html) -* `querystring` - optional, an object that represents the querystring. -* `options` - optional, an object that admits all the `options` listed above for the `getServiceProxy` and `getDirectServiceProxy` methods (which will eventually be overwritten), plus the following fields: - * `returnAs` - a string that identifies the format in which you want to receive the response. It can be `JSON`,`BUFFER` or `STREAM`. Default `JSON`. - * `allowedStatusCodes` - an array of integers that defines which status codes of the response are accepted. If the response status code is not contained in this array, the promise will be rejected. If this parameter is omitted, the promise is resolved in any case (even if the interrogated server answers 500). - * `isMiaHeaderInjected` - a boolean value that identifies whether Mia's headers should be forwarded in the request. Default `true`. - -## Examples - -```js -// Example of a request towards `tokens-collection` endpoint passing through Microservice Gateway -async function tokenGeneration(request, response) { - const crudProxy = request.getServiceProxy() - const result = await crudProxy - .post('/tokens-collection/', { - id: request.body.quotationId, - valid: true - }) - - const tokens=result.payload; - // ... -} -``` - -```js -// and bypassing Microservice Gateway -async function tokenGeneration(request, response) { - const crudProxy = request.getDirectServiceProxy('crud-service') - const result = await crudProxy - .post('/tokens-collection/', { - id: request.body.quotationId, - valid: true - }) - - const tokens=result.payload; - // ... -} -``` diff --git a/docs/runtime_suite_libraries/custom-plugin-lib/migration_guides/10_migrate_from_v6_to_v7.md b/docs/runtime_suite_libraries/custom-plugin-lib/migration_guides/10_migrate_from_v6_to_v7.md new file mode 100644 index 0000000000..8593f88cde --- /dev/null +++ b/docs/runtime_suite_libraries/custom-plugin-lib/migration_guides/10_migrate_from_v6_to_v7.md @@ -0,0 +1,44 @@ +--- +id: migrate_from_vto_v7 +title: Migrate from v6 to v7 +sidebar_label: Migrate from vto v7 +--- + + + +With v7 the `getServiceProxy` and `getDirectServiceProxy` methods have been removed. + +In order to upgrade to v7 you need to change the implementation using such methods to use another HTTP Client. + +:::tip + +Custom Plugin Lib already provides the [`getHttpClient`](../http_client.md) method to build an axios-based HTTP client since [v5.0.0](../../CHANGELOG.md#v500---2022-05-13). + +Main breaking changes from the already existent `getServiceProxy` and `getDirectServiceProxy`: + +- streams respond with an object with headers, payload and statusCode. The payload has the stream interface +- `allowedStatusCodes` array of status codes is replaced by the function `validateStatus` (which accept by default 2xx) +- `agent` to configure the proxy is renamed to `proxy` and it is now an object +- `port` and `protocol` are now accepted only in url and baseUrl + +::: + +## Migrate getDirectServiceProxy + +```js +const proxy = fastify.getServiceProxy('my-service', {}) +// becomes +const proxy = fastify.getHttpClient('http://microservice-gateway/', {}) +``` + +## Migrate getServiceProxy + +```js +const proxy = fastify.getServiceProxy('my-service', {}) +// becomes +const proxy = fastify.getHttpClient('http://my-service/', {}) +``` diff --git a/docs/runtime_suite_libraries/custom-plugin-lib/testing.md b/docs/runtime_suite_libraries/custom-plugin-lib/testing.md index 998edd8f56..970856c79e 100644 --- a/docs/runtime_suite_libraries/custom-plugin-lib/testing.md +++ b/docs/runtime_suite_libraries/custom-plugin-lib/testing.md @@ -13,17 +13,6 @@ Instead, modify the source file and run the aggregator to regenerate this file. `Mia service Node.js Library` is built on Fastify and therefore integrates with [testing tools](https://www.fastify.io/docs/latest/Testing/) made available by the framework. A complete example of this type of test is available [here](https://github.com/mia-platform/custom-plugin-lib/tree/master/examples/advanced/tests). -CustomPlugin directly expose `getDirectServiceProxy` and `getServiceProxy` for testing purpose. -Ypu can import the function in you test in ths way: - -``` javascript -const { getDirectServiceProxy } = require('@mia-platform/custom-plugin-lib') -const { getServiceProxy } = require('@mia-platform/custom-plugin-lib') - -const myServiceProxy = getDirectServiceProxy(serviceName,options) -const myServiceProxy = getServiceProxy(options) -``` - ## Integration and Unit test The testing of service can be performed at multiple levels of abstraction. One possibility is to use a technique called _fake http injection_ for which it is possible to simulate