@fastify/reply-from 9.6.0 → 9.7.0

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/README.md CHANGED
@@ -281,6 +281,7 @@ If a `handler` is passed to the `retryDelay` object the onus is on the client to
281
281
  - `res` is the raw response returned by the underlying agent (if available) __Note__: this object is not a Fastify response, but instead the low-level response from the agent. This property may be null if no response was obtained at all, like from a connection reset or timeout.
282
282
  - `attempt` in the object callback refers to the current retriesAttempt number. You are given the freedom to use this in concert with the retryCount property set to handle retries
283
283
  - `getDefaultRetry` refers to the default retry handler. If this callback returns not null and you wish to handle those case of errors simply invoke it as done below.
284
+ - `retriesCount` refers to the retriesCount property a client passes to reply-from. Note if the client does not explicitly set this value it will default to 0. The objective value here is to avoid hard-coding and seeing the retriesCount set. It is your perogative to ensure that you ensure the value here is as you wish (and not `0` if not intended to be as a result of a lack of not setting it).
284
285
 
285
286
  Given example
286
287
 
package/index.js CHANGED
@@ -163,7 +163,7 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
163
163
 
164
164
  if (retryDelay) {
165
165
  requestImpl = createRequestRetry(request, this, (req, res, err, retries) => {
166
- return retryDelay({ err, req, res, attempt: retries, getDefaultDelay })
166
+ return retryDelay({ err, req, res, attempt: retries, getDefaultDelay, retriesCount })
167
167
  })
168
168
  } else {
169
169
  requestImpl = createRequestRetry(request, this, getDefaultDelay)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/reply-from",
3
- "version": "9.6.0",
3
+ "version": "9.7.0",
4
4
  "description": "forward your HTTP request to another server, for fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -160,3 +160,27 @@ test('we can exceed our retryCount and introspect attempts independently', async
160
160
  t.equal(res.statusCode, 205)
161
161
  t.equal(res.body.toString(), 'Hello World 5!')
162
162
  })
163
+
164
+ test('we handle our retries based on the retryCount', async (t) => {
165
+ const attemptCounter = []
166
+ const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay, retriesCount }) => {
167
+ if (retriesCount < attempt) {
168
+ return null
169
+ }
170
+
171
+ if (res && res.statusCode === 500 && req.method === 'GET') {
172
+ attemptCounter.push(attempt)
173
+ return 0.1
174
+ }
175
+ return null
176
+ }
177
+
178
+ const { instance } = await setupServer(t, { retryDelay: customRetryLogic, retriesCount: 2 }, 500)
179
+
180
+ const res = await got.get(`http://localhost:${instance.server.address().port}`, { retry: 5 })
181
+
182
+ t.match(attemptCounter, [0, 1])
183
+ t.equal(res.headers['content-type'], 'text/plain')
184
+ t.equal(res.statusCode, 205)
185
+ t.equal(res.body.toString(), 'Hello World 5!')
186
+ })