@verii/fastify-plugins 1.1.0-pre.1757916914 → 1.1.0-pre.1758002630

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -25,5 +25,5 @@ module.exports = {
25
25
  ...require('./src/cors-plugin'),
26
26
  ...require('./src/csv-response-hook'),
27
27
  ...require('./src/cache-plugin'),
28
- ...require('./src/request-plugin'),
28
+ ...require('./src/http-client-plugin'),
29
29
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verii/fastify-plugins",
3
- "version": "1.1.0-pre.1757916914",
3
+ "version": "1.1.0-pre.1758002630",
4
4
  "description": "Plugin for handling errors",
5
5
  "repository": "https://github.com/LFDT-Verii/core",
6
6
  "main": "index.js",
@@ -18,9 +18,9 @@
18
18
  "license": "Apache-2.0",
19
19
  "dependencies": {
20
20
  "@fastify/cors": "11.1.0",
21
- "@verii/crypto": "1.1.0-pre.1757916914",
22
- "@verii/error-aggregation": "1.1.0-pre.1757916914",
23
- "@verii/request": "1.1.0-pre.1757916914",
21
+ "@verii/crypto": "1.1.0-pre.1758002630",
22
+ "@verii/error-aggregation": "1.1.0-pre.1758002630",
23
+ "@verii/http-client": "1.1.0-pre.1758002630",
24
24
  "fastify-plugin": "~5.0.0",
25
25
  "http-errors": "^2.0.0",
26
26
  "lodash": "^4.17.21",
@@ -29,7 +29,6 @@
29
29
  "toidentifier": "~1.0.1"
30
30
  },
31
31
  "devDependencies": {
32
- "@verii/request": "~0.5.0-build",
33
32
  "eslint": "8.57.1",
34
33
  "eslint-config-airbnb-base": "14.2.1",
35
34
  "eslint-config-prettier": "8.10.2",
@@ -49,5 +48,5 @@
49
48
  "lib"
50
49
  ]
51
50
  },
52
- "gitHead": "a44ef43958d01da618eb52394f20c1d5c7b8fdd6"
51
+ "gitHead": "b961d761dc0f0710df506d90222145afe3c35cf6"
53
52
  }
@@ -15,16 +15,13 @@
15
15
  */
16
16
 
17
17
  const fp = require('fastify-plugin');
18
- const QuickLru = require('quick-lru');
18
+ const { initCache } = require('@verii/http-client');
19
19
 
20
- const cache = new QuickLru({ maxSize: 1000, maxAge: 2 * 60 * 60 * 1000 }); // 1000 items cached for a mac of 2 hours
20
+ const store = initCache();
21
21
 
22
22
  const cachePlugin = (fastify, options, done) => {
23
- fastify.decorate('cache', cache);
24
- fastify.addHook('onRequest', (request, reply, next) => {
25
- request.cache = fastify.cache;
26
- next();
27
- });
23
+ fastify.decorate('cache', store);
24
+
28
25
  done();
29
26
  };
30
27
 
@@ -15,9 +15,9 @@
15
15
  */
16
16
 
17
17
  const url = require('url');
18
- const newError = require('http-errors');
19
18
  const fp = require('fastify-plugin');
20
- const { includes, get, isEmpty, flow } = require('lodash/fp');
19
+ const newError = require('http-errors');
20
+ const { get, isEmpty, flow } = require('lodash/fp');
21
21
  const { ERROR_CODES } = require('./constants');
22
22
 
23
23
  const extractEndpoint = (endpointUrl) => {
@@ -33,101 +33,6 @@ const getDocsUrl = (endpointUrl, { endpointDocsMap } = {}) => {
33
33
  return get(endpoint, endpointDocsMap);
34
34
  };
35
35
 
36
- const handleDnsError = (error, docsUrl) => {
37
- if (includes('getaddrinfo', error.message)) {
38
- throw newError(
39
- 502,
40
- 'DNS Error - Please verify that that the server has access to an internal DNS server, and that the vendor gateway api has an entry',
41
- {
42
- endpointDocumentation: docsUrl,
43
- errorCode: 'upstream_network_dns_error',
44
- }
45
- );
46
- }
47
- };
48
-
49
- const handleConnectivityError = (error, docsUrl) => {
50
- if (
51
- includes('ETIMEDOUT', error.message) ||
52
- includes('EPIPE', error.message) ||
53
- includes('ECONNRESET', error.message) ||
54
- includes('ECONNREFUSED', error.message)
55
- ) {
56
- throw newError(
57
- 502,
58
- 'Connectivity Error - Unable to connect to the vendor gateway. Please check routing tables and firewall settings',
59
- {
60
- endpointDocumentation: docsUrl,
61
- errorCode: 'upstream_network_error',
62
- }
63
- );
64
- }
65
- };
66
-
67
- const handleBadRequestError = (error, docsUrl) => {
68
- if (error.response?.statusCode === 400) {
69
- throw newError(
70
- 502,
71
- 'Bad request sent from credential agent to vendor gateway (this should be raised with velocity support).',
72
- {
73
- endpointDocumentation: docsUrl,
74
- errorCode: 'upstream_response_invalid',
75
- }
76
- );
77
- }
78
- };
79
-
80
- const handleUnauthorizedForbiddenError = (error, docsUrl) => {
81
- if (
82
- error.response?.statusCode === 401 ||
83
- error.response?.statusCode === 403
84
- ) {
85
- throw newError(
86
- 502,
87
- 'Bad authentication of the server. Please review the supported authentication methods for the agent.',
88
- {
89
- authenticationDocumentation:
90
- 'https://docs.velocitycareerlabs.io/#/./Authentication',
91
- endpointDocumentation: docsUrl,
92
- errorCode: 'upstream_unauthorized',
93
- }
94
- );
95
- }
96
- };
97
-
98
- const handleNotFoundError = (error, endpointPath, docsUrl) => {
99
- if (error.response?.statusCode === 404) {
100
- throw newError(
101
- 502,
102
- `Missing implementation of the endpoint '${endpointPath}'.`,
103
- {
104
- endpointDocumentation: docsUrl,
105
- errorCode: 'upstream_webhook_not_implemented',
106
- }
107
- );
108
- }
109
- };
110
-
111
- const handleUnexpectedError = (docsUrl) => {
112
- throw newError(
113
- 502,
114
- 'Unexpected error received connecting to vendor gateway.',
115
- {
116
- endpointDocumentation: docsUrl,
117
- errorCode: 'upstream_unexpected_error',
118
- }
119
- );
120
- };
121
-
122
- const handleVendorError = (error, endpointPath, docsUrl) => {
123
- handleDnsError(error, docsUrl);
124
- handleConnectivityError(error, docsUrl);
125
- handleBadRequestError(error, docsUrl);
126
- handleUnauthorizedForbiddenError(error, docsUrl);
127
- handleNotFoundError(error, endpointPath, docsUrl);
128
- handleUnexpectedError(docsUrl);
129
- };
130
-
131
36
  const extractRequestPath = (requestUrl) => {
132
37
  return url.parse(requestUrl).pathname;
133
38
  };
@@ -160,28 +65,26 @@ const addValidationErrorCode = (err) => {
160
65
  err.errorCode = 'request_validation_failed';
161
66
  return err;
162
67
  };
68
+
69
+ const transformToInternalServerError = (error, fastify) => {
70
+ if (error.url && error.statusCode && error.statusCode < 500) {
71
+ fastify.log.info('Transforming error to Internal Server Error', { error });
72
+
73
+ return newError(500, 'Internal Server Error');
74
+ }
75
+
76
+ return error;
77
+ };
78
+
163
79
  const errorsPlugin = (fastify, options, next) => {
164
80
  fastify.setErrorHandler((_error, request, reply) => {
165
- const { sendError = () => {} } = fastify;
166
81
  const error = flow(
167
82
  addValidationErrorCode,
168
83
  (err) => ensureErrorCode(err, fastify),
169
- (err) => addRequestId(err, request)
170
- )(_error);
84
+ (err) => addRequestId(err, request),
85
+ (err) => transformToInternalServerError(err, fastify)
86
+ )(_error.processedError || _error);
171
87
 
172
- if (error.gatewayResponse) {
173
- try {
174
- handleVendorError(
175
- error,
176
- extractRequestPath(error.gatewayResponse.url),
177
- getDocsUrl(error.gatewayResponse.url.toLowerCase(), options)
178
- );
179
- } catch (modifiedError) {
180
- sendError(error);
181
- return reply.send(modifiedError);
182
- }
183
- }
184
- sendError(error);
185
88
  return reply.send(error);
186
89
  });
187
90
  next();
@@ -189,7 +92,6 @@ const errorsPlugin = (fastify, options, next) => {
189
92
 
190
93
  module.exports = {
191
94
  addValidationErrorCode,
192
- handleVendorError,
193
95
  ensureErrorCode,
194
96
  addRequestId,
195
97
  extractRequestPath,
@@ -15,19 +15,25 @@
15
15
  *
16
16
  */
17
17
 
18
- const initRequest = require('@verii/request');
19
- const { capitalize } = require('lodash/fp');
18
+ const { initHttpClient } = require('@verii/http-client');
20
19
  const fp = require('fastify-plugin');
21
20
 
22
- const requestPlugin = async (fastify, { name, options }) => {
23
- const fastifyDecoration = `base${capitalize(name)}`;
21
+ const httpClientPlugin = async (fastify, { name, options }) => {
22
+ const fastifyDecoration = `base${name[0].toUpperCase()}${name.slice(1)}`;
24
23
  const requestDecoration = name;
24
+ const { prefixUrl } = options;
25
25
  fastify
26
- .decorate(fastifyDecoration, initRequest(options))
26
+ .decorate(
27
+ fastifyDecoration,
28
+ () => initHttpClient({ ...options, cache: fastify.cache }),
29
+ ['cache']
30
+ )
27
31
  .decorateRequest(requestDecoration, null)
28
32
  .addHook('preValidation', async (req) => {
29
- req[requestDecoration] = fastify[fastifyDecoration](req);
33
+ req[requestDecoration] = prefixUrl
34
+ ? fastify[fastifyDecoration]()(prefixUrl, req)
35
+ : fastify[fastifyDecoration]()(req);
30
36
  });
31
37
  };
32
38
 
33
- module.exports = { requestPlugin: fp(requestPlugin) };
39
+ module.exports = { httpClientPlugin: fp(httpClientPlugin) };