@fastify/reply-from 10.0.0 → 11.0.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 (95) hide show
  1. package/.gitattributes +2 -0
  2. package/.github/workflows/ci.yml +1 -1
  3. package/.taprc +1 -9
  4. package/README.md +114 -29
  5. package/index.js +55 -47
  6. package/lib/request.js +32 -12
  7. package/lib/utils.js +8 -0
  8. package/package.json +25 -22
  9. package/test/async-route-handler.test.js +1 -1
  10. package/test/base-get.test.js +1 -1
  11. package/test/base-querystring.test.js +1 -1
  12. package/test/build-url.test.js +9 -2
  13. package/test/core-with-path-in-base.test.js +1 -1
  14. package/test/custom-undici-instance.test.js +1 -1
  15. package/test/disable-request-logging.test.js +1 -1
  16. package/test/fastify-multipart-incompatibility.test.js +3 -3
  17. package/test/fix-GHSA-v2v2-hph8-q5xp.test.js +47 -0
  18. package/test/full-delete-http2.test.js +52 -0
  19. package/test/full-get-test.test.js +1 -1
  20. package/test/full-https-get.test.js +3 -3
  21. package/test/full-post-extended-content-type.test.js +1 -1
  22. package/test/full-post-http2.test.js +2 -0
  23. package/test/full-post-stream-core.test.js +1 -1
  24. package/test/full-post-stream.test.js +1 -1
  25. package/test/full-post.test.js +1 -1
  26. package/test/full-querystring-rewrite-option-complex.test.js +1 -1
  27. package/test/full-querystring-rewrite-option-function-request.test.js +58 -0
  28. package/test/full-querystring-rewrite-option-function.test.js +2 -2
  29. package/test/full-querystring-rewrite-option.test.js +1 -1
  30. package/test/full-querystring-rewrite-string.test.js +1 -1
  31. package/test/full-querystring-rewrite.test.js +1 -1
  32. package/test/full-querystring.test.js +1 -1
  33. package/test/full-rewrite-body-content-type.test.js +1 -1
  34. package/test/full-rewrite-body-http.test.js +1 -1
  35. package/test/full-rewrite-body-to-empty-string.test.js +1 -1
  36. package/test/full-rewrite-body-to-null.test.js +1 -1
  37. package/test/full-rewrite-body.test.js +1 -1
  38. package/test/get-upstream-http.test.js +1 -1
  39. package/test/get-upstream-type.test.js +1 -1
  40. package/test/get-upstream-undici.test.js +1 -1
  41. package/test/get-with-body.test.js +1 -1
  42. package/test/head-with-body.test.js +7 -1
  43. package/test/http-agents.test.js +2 -2
  44. package/test/http-global-agent.test.js +1 -1
  45. package/test/http-http2.test.js +2 -0
  46. package/test/http-retry.test.js +1 -1
  47. package/test/http-timeout.test.js +0 -2
  48. package/test/http2-http2.test.js +4 -2
  49. package/test/http2-https.test.js +2 -2
  50. package/test/http2-invalid-base.test.js +1 -1
  51. package/test/http2-timeout-disabled.test.js +85 -0
  52. package/test/http2-timeout.test.js +2 -0
  53. package/test/http2-unix-socket.test.js +1 -1
  54. package/test/https-agents.test.js +4 -4
  55. package/test/https-global-agent.test.js +3 -3
  56. package/test/method.test.js +70 -0
  57. package/test/modifyCoreObjects-false.test.js +1 -1
  58. package/test/no-body-opts-with-get.test.js +1 -1
  59. package/test/no-body-opts-with-head.test.js +1 -1
  60. package/test/no-stream-body-option.test.js +2 -2
  61. package/test/on-error.test.js +11 -15
  62. package/test/onResponse.test.js +4 -3
  63. package/test/padded-body.test.js +1 -1
  64. package/test/post-formbody.test.js +1 -1
  65. package/test/post-plain-text.test.js +1 -1
  66. package/test/post-with-custom-encoded-contenttype.test.js +2 -2
  67. package/test/post-with-octet-stream.test.js +2 -2
  68. package/test/retry-on-503.test.js +1 -1
  69. package/test/retry-with-a-custom-handler.test.js +186 -0
  70. package/test/rewrite-headers-type.test.js +1 -1
  71. package/test/rewrite-headers.test.js +1 -1
  72. package/test/rewrite-request-headers-type.test.js +1 -1
  73. package/test/rewrite-request-headers.test.js +1 -1
  74. package/test/transform-body.test.js +3 -3
  75. package/test/undici-agent.test.js +2 -2
  76. package/test/undici-body.test.js +1 -1
  77. package/test/undici-connect-timeout.test.js +10 -14
  78. package/test/undici-custom-dispatcher.test.js +73 -0
  79. package/test/undici-global-agent.test.js +1 -1
  80. package/test/undici-no-destroy.test.js +29 -0
  81. package/test/undici-options.test.js +1 -1
  82. package/test/undici-proxy-agent.test.js +81 -0
  83. package/test/undici-retry.test.js +1 -1
  84. package/test/undici-timeout-body-partial.test.js +15 -19
  85. package/test/undici-timeout-body.test.js +13 -17
  86. package/test/undici-timeout.test.js +11 -15
  87. package/test/undici-with-path-in-base.test.js +1 -1
  88. package/test/undici.test.js +1 -1
  89. package/test/unix-http-undici-from.test.js +3 -3
  90. package/test/unix-http-undici.test.js +3 -3
  91. package/test/unix-http.test.js +3 -3
  92. package/test/unix-https-undici.test.js +4 -4
  93. package/test/unix-https.test.js +4 -4
  94. package/types/index.d.ts +37 -19
  95. package/types/index.test-d.ts +37 -10
@@ -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, RawReplyDefaultExpression, RawServerBase, RequestGenericInterface, RouteGenericInterface } 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",
@@ -38,15 +38,16 @@ const fullOptions: FastifyReplyFromOptions = {
38
38
  disableCache: false,
39
39
  undici: {
40
40
  connections: 100,
41
- pipelining: 10
41
+ pipelining: 10,
42
+ proxy: 'http://example2.com:8080'
42
43
  },
43
44
  contentTypesToEncode: ['application/x-www-form-urlencoded'],
44
45
  retryMethods: ['GET', 'HEAD', 'OPTIONS', 'TRACE'],
45
46
  maxRetriesOn503: 10,
46
47
  disableRequestLogging: false,
47
48
  globalAgent: false,
49
+ destroyAgent: true
48
50
  };
49
- tap.autoend(false);
50
51
 
51
52
  async function main() {
52
53
  const server = fastify();
@@ -59,6 +60,10 @@ async function main() {
59
60
 
60
61
  server.register(replyFrom, fullOptions);
61
62
 
63
+ server.register(replyFrom, { undici: { proxy: new URL('http://example2.com:8080') } });
64
+
65
+ server.register(replyFrom, { undici: { proxy: { uri: 'http://example2.com:8080' } } });
66
+
62
67
  server.get("/v1", (request, reply) => {
63
68
  expectType<FastifyReply>(reply.from());
64
69
  });
@@ -72,6 +77,12 @@ async function main() {
72
77
  getUpstream(req, base) {
73
78
  expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req);
74
79
  return base;
80
+ },
81
+ onResponse(request, reply, res) {
82
+ expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(request);
83
+ expectType<FastifyReply<RouteGenericInterface, RawServerBase>>(reply);
84
+ expectType<RawReplyDefaultExpression<RawServerBase>>(res);
85
+ expectType<number>(res.statusCode);
75
86
  }
76
87
  });
77
88
  });
@@ -89,6 +100,16 @@ async function main() {
89
100
 
90
101
  instance.get("/http2", (request, reply) => {
91
102
  reply.from("/", {
103
+ method: "POST",
104
+ retryDelay: ({err, req, res, attempt, retriesCount, getDefaultDelay }) => {
105
+ const defaultDelay = getDefaultDelay();
106
+ if (defaultDelay) return defaultDelay;
107
+
108
+ if (res && res.statusCode === 500 && req.method === "GET") {
109
+ return 300;
110
+ }
111
+ return null;
112
+ },
92
113
  rewriteHeaders(headers, req) {
93
114
  return headers;
94
115
  },
@@ -98,9 +119,15 @@ async function main() {
98
119
  getUpstream(req, base) {
99
120
  return base;
100
121
  },
101
- onError(reply: FastifyReply<RawServerBase>, error) {
122
+ onError(reply: FastifyReply<RouteGenericInterface, RawServerBase>, error) {
102
123
  return reply.send(error.error);
103
- }
124
+ },
125
+ queryString(search, reqUrl, request) {
126
+ expectType<string | undefined>(search);
127
+ expectType<string>(reqUrl);
128
+ expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(request);
129
+ return '';
130
+ },
104
131
  });
105
132
  });
106
133