@fastify/reply-from 9.3.0 → 9.5.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.
Files changed (77) hide show
  1. package/.gitattributes +2 -0
  2. package/README.md +81 -21
  3. package/index.js +36 -27
  4. package/lib/request.js +4 -4
  5. package/lib/utils.js +2 -0
  6. package/package.json +3 -2
  7. package/test/async-route-handler.test.js +1 -1
  8. package/test/base-get.test.js +1 -1
  9. package/test/base-querystring.test.js +1 -1
  10. package/test/core-with-path-in-base.test.js +1 -1
  11. package/test/custom-undici-instance.test.js +1 -1
  12. package/test/disable-request-logging.test.js +1 -1
  13. package/test/fastify-multipart-incompatibility.test.js +3 -3
  14. package/test/full-get-test.test.js +1 -1
  15. package/test/full-https-get.test.js +3 -3
  16. package/test/full-post-extended-content-type.test.js +1 -1
  17. package/test/full-post-stream-core.test.js +1 -1
  18. package/test/full-post-stream.test.js +1 -1
  19. package/test/full-post.test.js +1 -1
  20. package/test/full-querystring-rewrite-option-complex.test.js +1 -1
  21. package/test/full-querystring-rewrite-option-function.test.js +2 -2
  22. package/test/full-querystring-rewrite-option.test.js +1 -1
  23. package/test/full-querystring-rewrite-string.test.js +1 -1
  24. package/test/full-querystring-rewrite.test.js +1 -1
  25. package/test/full-querystring.test.js +1 -1
  26. package/test/full-rewrite-body-content-type.test.js +1 -1
  27. package/test/full-rewrite-body-http.test.js +1 -1
  28. package/test/full-rewrite-body-to-empty-string.test.js +1 -1
  29. package/test/full-rewrite-body-to-null.test.js +1 -1
  30. package/test/full-rewrite-body.test.js +1 -1
  31. package/test/get-upstream-http.test.js +1 -1
  32. package/test/get-upstream-type.test.js +1 -1
  33. package/test/get-upstream-undici.test.js +1 -1
  34. package/test/get-with-body.test.js +1 -1
  35. package/test/head-with-body.test.js +1 -1
  36. package/test/http-agents.test.js +2 -2
  37. package/test/http-global-agent.test.js +1 -1
  38. package/test/http-retry.test.js +1 -1
  39. package/test/http2-http2.test.js +2 -2
  40. package/test/http2-https.test.js +2 -2
  41. package/test/https-agents.test.js +4 -4
  42. package/test/https-global-agent.test.js +3 -3
  43. package/test/method.test.js +70 -0
  44. package/test/modifyCoreObjects-false.test.js +1 -1
  45. package/test/no-body-opts-with-get.test.js +1 -1
  46. package/test/no-body-opts-with-head.test.js +1 -1
  47. package/test/no-stream-body-option.test.js +2 -2
  48. package/test/onResponse.test.js +1 -1
  49. package/test/padded-body.test.js +1 -1
  50. package/test/post-formbody.test.js +1 -1
  51. package/test/post-plain-text.test.js +1 -1
  52. package/test/post-with-custom-encoded-contenttype.test.js +2 -2
  53. package/test/post-with-octet-stream.test.js +2 -2
  54. package/test/retry-on-503.test.js +1 -1
  55. package/test/retry-with-a-custom-handler.test.js +162 -0
  56. package/test/rewrite-headers-type.test.js +1 -1
  57. package/test/rewrite-headers.test.js +1 -1
  58. package/test/rewrite-request-headers-type.test.js +1 -1
  59. package/test/rewrite-request-headers.test.js +1 -1
  60. package/test/transform-body.test.js +2 -2
  61. package/test/undici-agent.test.js +1 -1
  62. package/test/undici-body.test.js +1 -1
  63. package/test/undici-connect-timeout.test.js +2 -2
  64. package/test/undici-global-agent.test.js +1 -1
  65. package/test/undici-options.test.js +1 -1
  66. package/test/undici-retry.test.js +1 -1
  67. package/test/undici-timeout-body-partial.test.js +1 -1
  68. package/test/undici-timeout-body.test.js +1 -1
  69. package/test/undici-with-path-in-base.test.js +1 -1
  70. package/test/undici.test.js +1 -1
  71. package/test/unix-http-undici-from.test.js +3 -3
  72. package/test/unix-http-undici.test.js +3 -3
  73. package/test/unix-http.test.js +3 -3
  74. package/test/unix-https-undici.test.js +4 -4
  75. package/test/unix-https.test.js +4 -4
  76. package/types/index.d.ts +25 -15
  77. package/types/index.test-d.ts +16 -6
@@ -3,7 +3,7 @@
3
3
  const t = require('tap')
4
4
  const Fastify = require('fastify')
5
5
  const From = require('..')
6
- const http = require('http')
6
+ const http = require('node:http')
7
7
  const get = require('simple-get').concat
8
8
 
9
9
  const instance = Fastify()
@@ -3,9 +3,9 @@
3
3
  const t = require('tap')
4
4
  const Fastify = require('fastify')
5
5
  const From = require('..')
6
- const fs = require('fs')
7
- const querystring = require('querystring')
8
- const http = require('http')
6
+ const fs = require('node:fs')
7
+ const querystring = require('node:querystring')
8
+ const http = require('node:http')
9
9
  const get = require('simple-get').concat
10
10
 
11
11
  if (process.platform === 'win32') {
@@ -3,9 +3,9 @@
3
3
  const t = require('tap')
4
4
  const Fastify = require('fastify')
5
5
  const From = require('..')
6
- const fs = require('fs')
7
- const querystring = require('querystring')
8
- const http = require('http')
6
+ const fs = require('node:fs')
7
+ const querystring = require('node:querystring')
8
+ const http = require('node:http')
9
9
  const get = require('simple-get').concat
10
10
 
11
11
  if (process.platform === 'win32') {
@@ -3,9 +3,9 @@
3
3
  const t = require('tap')
4
4
  const Fastify = require('fastify')
5
5
  const From = require('..')
6
- const fs = require('fs')
7
- const querystring = require('querystring')
8
- const http = require('http')
6
+ const fs = require('node:fs')
7
+ const querystring = require('node:querystring')
8
+ const http = require('node:http')
9
9
  const get = require('simple-get').concat
10
10
 
11
11
  if (process.platform === 'win32') {
@@ -3,11 +3,11 @@
3
3
  const t = require('tap')
4
4
  const Fastify = require('fastify')
5
5
  const From = require('..')
6
- const https = require('https')
6
+ const https = require('node:https')
7
7
  const get = require('simple-get').concat
8
- const fs = require('fs')
9
- const querystring = require('querystring')
10
- const path = require('path')
8
+ const fs = require('node:fs')
9
+ const querystring = require('node:querystring')
10
+ const path = require('node:path')
11
11
  const certs = {
12
12
  key: fs.readFileSync(path.join(__dirname, 'fixtures', 'fastify.key')),
13
13
  cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'fastify.cert'))
@@ -3,11 +3,11 @@
3
3
  const t = require('tap')
4
4
  const Fastify = require('fastify')
5
5
  const From = require('..')
6
- const https = require('https')
6
+ const https = require('node:https')
7
7
  const get = require('simple-get').concat
8
- const fs = require('fs')
9
- const querystring = require('querystring')
10
- const path = require('path')
8
+ const fs = require('node:fs')
9
+ const querystring = require('node:querystring')
10
+ const path = require('node:path')
11
11
  const certs = {
12
12
  key: fs.readFileSync(path.join(__dirname, 'fixtures', 'fastify.key')),
13
13
  cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'fastify.cert'))
package/types/index.d.ts CHANGED
@@ -1,33 +1,33 @@
1
1
  /// <reference types="node" />
2
2
 
3
3
  import {
4
- FastifyRequest,
4
+ FastifyPluginCallback,
5
5
  FastifyReply,
6
+ FastifyRequest,
7
+ HTTPMethods,
6
8
  RawReplyDefaultExpression,
7
9
  RawServerBase,
8
10
  RequestGenericInterface,
9
- HTTPMethods,
10
- FastifyPluginCallback,
11
11
  } from 'fastify';
12
12
 
13
13
  import {
14
+ Agent,
15
+ AgentOptions,
14
16
  IncomingHttpHeaders,
15
17
  RequestOptions,
16
- AgentOptions,
17
- Agent,
18
18
  } from "http";
19
19
  import {
20
- RequestOptions as SecureRequestOptions,
21
- AgentOptions as SecureAgentOptions,
22
- Agent as SecureAgent
23
- } from "https";
24
- import {
25
- IncomingHttpHeaders as Http2IncomingHttpHeaders,
26
- ClientSessionRequestOptions,
27
20
  ClientSessionOptions,
21
+ ClientSessionRequestOptions,
22
+ IncomingHttpHeaders as Http2IncomingHttpHeaders,
28
23
  SecureClientSessionOptions,
29
24
  } from "http2";
30
- import { Pool } from 'undici'
25
+ import {
26
+ Agent as SecureAgent,
27
+ AgentOptions as SecureAgentOptions,
28
+ RequestOptions as SecureRequestOptions
29
+ } from "https";
30
+ import { Pool } from 'undici';
31
31
 
32
32
  declare module "fastify" {
33
33
  interface FastifyReply {
@@ -39,12 +39,21 @@ declare module "fastify" {
39
39
  }
40
40
 
41
41
  type FastifyReplyFrom = FastifyPluginCallback<fastifyReplyFrom.FastifyReplyFromOptions>
42
-
43
42
  declare namespace fastifyReplyFrom {
44
43
  type QueryStringFunction = (search: string | undefined, reqUrl: string) => string;
44
+
45
+ export type RetryDetails = {
46
+ err: Error;
47
+ req: FastifyRequest<RequestGenericInterface, RawServerBase>;
48
+ res: FastifyReply<RawServerBase>;
49
+ attempt: number;
50
+ getDefaultDelay: () => number | null;
51
+ }
45
52
  export interface FastifyReplyFromHooks {
46
53
  queryString?: { [key: string]: unknown } | QueryStringFunction;
47
54
  contentType?: string;
55
+ retryDelay?: (details: RetryDetails) => {} | null;
56
+ retriesCount?: number;
48
57
  onResponse?: (
49
58
  request: FastifyRequest<RequestGenericInterface, RawServerBase>,
50
59
  reply: FastifyReply<RawServerBase>,
@@ -67,6 +76,7 @@ declare namespace fastifyReplyFrom {
67
76
  request: FastifyRequest<RequestGenericInterface, RawServerBase>,
68
77
  base: string
69
78
  ) => string;
79
+ method?: HTTPMethods;
70
80
  }
71
81
 
72
82
  interface Http2Options {
@@ -98,7 +108,7 @@ declare namespace fastifyReplyFrom {
98
108
  }
99
109
 
100
110
  export const fastifyReplyFrom: FastifyReplyFrom
101
- export { fastifyReplyFrom as default }
111
+ export { fastifyReplyFrom as default };
102
112
  }
103
113
 
104
114
  declare function fastifyReplyFrom(...params: Parameters<FastifyReplyFrom>): ReturnType<FastifyReplyFrom>
@@ -1,12 +1,12 @@
1
- import replyFrom, { FastifyReplyFromOptions } from "..";
2
- import fastify, {FastifyReply, FastifyRequest, RawServerBase, RequestGenericInterface} from "fastify";
3
- import { AddressInfo } from "net";
4
- import { IncomingHttpHeaders } from "http2";
5
- import { expectType } from 'tsd';
1
+ import fastify, { FastifyReply, FastifyRequest, RawServerBase, RequestGenericInterface } from "fastify";
6
2
  import * as http from 'http';
3
+ import { IncomingHttpHeaders } from "http2";
7
4
  import * as https from 'https';
5
+ import { AddressInfo } from "net";
6
+ import { expectType } from 'tsd';
7
+ import replyFrom, { FastifyReplyFromOptions } from "..";
8
8
  // @ts-ignore
9
- import tap from 'tap'
9
+ import tap from 'tap';
10
10
 
11
11
  const fullOptions: FastifyReplyFromOptions = {
12
12
  base: "http://example2.com",
@@ -90,6 +90,16 @@ async function main() {
90
90
 
91
91
  instance.get("/http2", (request, reply) => {
92
92
  reply.from("/", {
93
+ method: "POST",
94
+ retryDelay: ({err, req, res, attempt, getDefaultDelay}) => {
95
+ const defaultDelay = getDefaultDelay();
96
+ if (defaultDelay) return defaultDelay;
97
+
98
+ if (res && res.statusCode === 500 && req.method === "GET") {
99
+ return 300;
100
+ }
101
+ return null;
102
+ },
93
103
  rewriteHeaders(headers, req) {
94
104
  return headers;
95
105
  },