@duplojs/http 0.2.0 → 0.4.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 (151) hide show
  1. package/dist/client/getBody.cjs +22 -0
  2. package/dist/client/getBody.d.ts +1 -0
  3. package/dist/client/getBody.mjs +20 -0
  4. package/dist/client/hooks.cjs +79 -0
  5. package/dist/client/hooks.d.ts +33 -0
  6. package/dist/client/hooks.mjs +70 -0
  7. package/dist/client/httpClient.cjs +153 -0
  8. package/dist/client/httpClient.d.ts +60 -0
  9. package/dist/client/httpClient.mjs +130 -0
  10. package/dist/client/index.cjs +33 -0
  11. package/dist/client/index.d.ts +9 -0
  12. package/dist/client/index.mjs +9 -0
  13. package/dist/client/insertParamsInPath.cjs +12 -0
  14. package/dist/client/insertParamsInPath.d.ts +2 -0
  15. package/dist/client/insertParamsInPath.mjs +10 -0
  16. package/dist/client/kind.cjs +9 -0
  17. package/dist/client/kind.d.ts +6 -0
  18. package/dist/client/kind.mjs +7 -0
  19. package/dist/client/promiseRequest.cjs +355 -0
  20. package/dist/client/promiseRequest.d.ts +98 -0
  21. package/dist/client/promiseRequest.mjs +332 -0
  22. package/dist/client/queryToString.cjs +25 -0
  23. package/dist/client/queryToString.d.ts +2 -0
  24. package/dist/client/queryToString.mjs +23 -0
  25. package/dist/client/types/ObjectCanBeEmpty.cjs +2 -0
  26. package/dist/client/types/ObjectCanBeEmpty.d.ts +4 -0
  27. package/dist/client/types/ObjectCanBeEmpty.mjs +1 -0
  28. package/dist/client/types/clientRequestParams.cjs +2 -0
  29. package/dist/client/types/clientRequestParams.d.ts +51 -0
  30. package/dist/client/types/clientRequestParams.mjs +1 -0
  31. package/dist/client/types/clientResponse.cjs +2 -0
  32. package/dist/client/types/clientResponse.d.ts +34 -0
  33. package/dist/client/types/clientResponse.mjs +1 -0
  34. package/dist/client/types/index.cjs +7 -0
  35. package/dist/client/types/index.d.ts +4 -0
  36. package/dist/client/types/index.mjs +4 -0
  37. package/dist/client/types/serverRoute.cjs +2 -0
  38. package/dist/client/types/serverRoute.d.ts +22 -0
  39. package/dist/client/types/serverRoute.mjs +1 -0
  40. package/dist/client/unexpectedResponseError.cjs +44 -0
  41. package/dist/client/unexpectedResponseError.d.ts +38 -0
  42. package/dist/client/unexpectedResponseError.mjs +39 -0
  43. package/dist/core/functionsBuilders/route/default.cjs +10 -9
  44. package/dist/core/functionsBuilders/route/default.mjs +3 -2
  45. package/dist/core/functionsBuilders/route/hook.cjs +22 -22
  46. package/dist/core/functionsBuilders/route/hook.d.ts +2 -2
  47. package/dist/core/functionsBuilders/route/hook.mjs +2 -2
  48. package/dist/core/functionsBuilders/steps/create.d.ts +2 -2
  49. package/dist/core/functionsBuilders/steps/defaults/checkerStep.cjs +3 -2
  50. package/dist/core/functionsBuilders/steps/defaults/checkerStep.mjs +3 -2
  51. package/dist/core/functionsBuilders/steps/defaults/cutStep.cjs +3 -2
  52. package/dist/core/functionsBuilders/steps/defaults/cutStep.mjs +3 -2
  53. package/dist/core/functionsBuilders/steps/defaults/extractStep.cjs +6 -5
  54. package/dist/core/functionsBuilders/steps/defaults/extractStep.mjs +4 -3
  55. package/dist/core/functionsBuilders/steps/defaults/handlerStep.cjs +3 -2
  56. package/dist/core/functionsBuilders/steps/defaults/handlerStep.mjs +3 -2
  57. package/dist/core/functionsBuilders/steps/defaults/processStep.cjs +5 -4
  58. package/dist/core/functionsBuilders/steps/defaults/processStep.mjs +3 -2
  59. package/dist/core/hub/defaultNotfoundHandler.d.ts +1 -1
  60. package/dist/core/hub/index.cjs +1 -1
  61. package/dist/core/hub/index.mjs +1 -1
  62. package/dist/core/index.cjs +37 -34
  63. package/dist/core/index.mjs +5 -3
  64. package/dist/core/response/base.cjs +40 -0
  65. package/dist/core/response/base.d.ts +21 -0
  66. package/dist/core/response/base.mjs +38 -0
  67. package/dist/core/response/contract.cjs +2 -2
  68. package/dist/core/response/contract.d.ts +120 -118
  69. package/dist/core/response/contract.mjs +2 -2
  70. package/dist/core/{route/hooks/response.cjs → response/hook.cjs} +4 -3
  71. package/dist/core/{route/hooks/response.d.ts → response/hook.d.ts} +2 -2
  72. package/dist/core/{route/hooks/response.mjs → response/hook.mjs} +3 -2
  73. package/dist/core/response/index.cjs +9 -38
  74. package/dist/core/response/index.d.ts +3 -20
  75. package/dist/core/response/index.mjs +3 -38
  76. package/dist/core/response/predicted.cjs +22 -0
  77. package/dist/core/response/predicted.d.ts +11 -0
  78. package/dist/core/response/predicted.mjs +20 -0
  79. package/dist/core/route/{hooks/index.cjs → hooks.cjs} +2 -3
  80. package/dist/core/route/{hooks/index.d.ts → hooks.d.ts} +4 -4
  81. package/dist/core/route/{hooks/index.mjs → hooks.mjs} +2 -2
  82. package/dist/core/route/index.cjs +4 -4
  83. package/dist/core/route/index.mjs +1 -1
  84. package/dist/core/steps/cut.d.ts +3 -3
  85. package/dist/core/steps/handler.d.ts +3 -3
  86. package/dist/core/steps/identifier.d.ts +2 -2
  87. package/dist/core/steps/types/stepFunctionParams.d.ts +2 -2
  88. package/dist/core/types/forbiddenBigintDataParser.cjs +2 -0
  89. package/dist/core/types/forbiddenBigintDataParser.d.ts +6 -0
  90. package/dist/core/types/forbiddenBigintDataParser.mjs +1 -0
  91. package/dist/core/types/index.cjs +1 -0
  92. package/dist/core/types/index.d.ts +1 -0
  93. package/dist/core/types/index.mjs +1 -0
  94. package/dist/interfaces/node/createHttpServer.cjs +1 -0
  95. package/dist/interfaces/node/createHttpServer.d.ts +2 -1
  96. package/dist/interfaces/node/createHttpServer.mjs +1 -0
  97. package/dist/interfaces/node/hooks.cjs +16 -13
  98. package/dist/interfaces/node/hooks.mjs +15 -12
  99. package/dist/plugins/codeGenerator/index.cjs +0 -3
  100. package/dist/plugins/codeGenerator/index.mjs +1 -1
  101. package/dist/plugins/codeGenerator/plugin.cjs +1 -1
  102. package/dist/plugins/codeGenerator/plugin.mjs +1 -1
  103. package/dist/plugins/codeGenerator/routeToDataParser.cjs +4 -55
  104. package/dist/plugins/codeGenerator/routeToDataParser.d.ts +2 -181
  105. package/dist/plugins/codeGenerator/routeToDataParser.mjs +6 -54
  106. package/dist/plugins/openApiGenerator/aggregateStepContract.cjs +62 -0
  107. package/dist/plugins/openApiGenerator/aggregateStepContract.d.ts +18 -0
  108. package/dist/plugins/openApiGenerator/aggregateStepContract.mjs +60 -0
  109. package/dist/plugins/openApiGenerator/index.cjs +17 -0
  110. package/dist/plugins/openApiGenerator/index.d.ts +7 -0
  111. package/dist/plugins/openApiGenerator/index.mjs +7 -0
  112. package/dist/plugins/openApiGenerator/makeOpenApiPage.cjs +30 -0
  113. package/dist/plugins/openApiGenerator/makeOpenApiPage.d.ts +7 -0
  114. package/dist/plugins/openApiGenerator/makeOpenApiPage.mjs +28 -0
  115. package/dist/plugins/openApiGenerator/makeOpenApiRoute.cjs +15 -0
  116. package/dist/plugins/openApiGenerator/makeOpenApiRoute.d.ts +18 -0
  117. package/dist/plugins/openApiGenerator/makeOpenApiRoute.mjs +13 -0
  118. package/dist/plugins/openApiGenerator/plugin.cjs +90 -0
  119. package/dist/plugins/openApiGenerator/plugin.d.ts +49 -0
  120. package/dist/plugins/openApiGenerator/plugin.mjs +88 -0
  121. package/dist/plugins/openApiGenerator/routeToOpenApi.cjs +163 -0
  122. package/dist/plugins/openApiGenerator/routeToOpenApi.d.ts +35 -0
  123. package/dist/plugins/openApiGenerator/routeToOpenApi.mjs +161 -0
  124. package/dist/plugins/openApiGenerator/types/endpointResponse.cjs +2 -0
  125. package/dist/plugins/openApiGenerator/types/endpointResponse.d.ts +19 -0
  126. package/dist/plugins/openApiGenerator/types/endpointResponse.mjs +1 -0
  127. package/dist/plugins/openApiGenerator/types/entrypoint.cjs +2 -0
  128. package/dist/plugins/openApiGenerator/types/entrypoint.d.ts +22 -0
  129. package/dist/plugins/openApiGenerator/types/entrypoint.mjs +1 -0
  130. package/dist/plugins/openApiGenerator/types/entrypointKey.cjs +2 -0
  131. package/dist/plugins/openApiGenerator/types/entrypointKey.d.ts +1 -0
  132. package/dist/plugins/openApiGenerator/types/entrypointKey.mjs +1 -0
  133. package/dist/plugins/openApiGenerator/types/index.cjs +11 -0
  134. package/dist/plugins/openApiGenerator/types/index.d.ts +8 -0
  135. package/dist/plugins/openApiGenerator/types/index.mjs +8 -0
  136. package/dist/plugins/openApiGenerator/types/openApiDocument.cjs +2 -0
  137. package/dist/plugins/openApiGenerator/types/openApiDocument.d.ts +31 -0
  138. package/dist/plugins/openApiGenerator/types/openApiDocument.mjs +1 -0
  139. package/dist/plugins/openApiGenerator/types/openApiMethod.cjs +2 -0
  140. package/dist/plugins/openApiGenerator/types/openApiMethod.d.ts +1 -0
  141. package/dist/plugins/openApiGenerator/types/openApiMethod.mjs +1 -0
  142. package/dist/plugins/openApiGenerator/types/openApiOperation.cjs +2 -0
  143. package/dist/plugins/openApiGenerator/types/openApiOperation.d.ts +8 -0
  144. package/dist/plugins/openApiGenerator/types/openApiOperation.mjs +1 -0
  145. package/dist/plugins/openApiGenerator/types/openApiPath.cjs +2 -0
  146. package/dist/plugins/openApiGenerator/types/openApiPath.d.ts +4 -0
  147. package/dist/plugins/openApiGenerator/types/openApiPath.mjs +1 -0
  148. package/dist/plugins/openApiGenerator/types/openApiSecuritySchema.cjs +2 -0
  149. package/dist/plugins/openApiGenerator/types/openApiSecuritySchema.d.ts +8 -0
  150. package/dist/plugins/openApiGenerator/types/openApiSecuritySchema.mjs +1 -0
  151. package/package.json +17 -4
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ var utils = require('@duplojs/utils');
4
+
5
+ const createClientKind = utils.createKindNamespace(
6
+ // @ts-expect-error reserved kind namespace
7
+ "DuplojsHttpClient");
8
+
9
+ exports.createClientKind = createClientKind;
@@ -0,0 +1,6 @@
1
+ declare module "@duplojs/utils" {
2
+ interface ReservedKindNamespace {
3
+ DuplojsHttpClient: true;
4
+ }
5
+ }
6
+ export declare const createClientKind: <GenericName extends string, GenericKindValue extends unknown = unknown>(name: GenericName & import("@duplojs/utils/string").ForbiddenString<GenericName, "@" | "/">) => import("@duplojs/utils").KindHandler<import("@duplojs/utils").KindDefinition<`@DuplojsHttpClient/${GenericName}`, GenericKindValue>>;
@@ -0,0 +1,7 @@
1
+ import { createKindNamespace } from '@duplojs/utils';
2
+
3
+ const createClientKind = createKindNamespace(
4
+ // @ts-expect-error reserved kind namespace
5
+ "DuplojsHttpClient");
6
+
7
+ export { createClientKind };
@@ -0,0 +1,355 @@
1
+ 'use strict';
2
+
3
+ var utils = require('@duplojs/utils');
4
+ var getBody = require('./getBody.cjs');
5
+ var insertParamsInPath = require('./insertParamsInPath.cjs');
6
+ var queryToString = require('./queryToString.cjs');
7
+ var hooks = require('./hooks.cjs');
8
+ var EE = require('@duplojs/utils/either');
9
+ var SS = require('@duplojs/utils/string');
10
+ var AA = require('@duplojs/utils/array');
11
+ var unexpectedResponseError = require('./unexpectedResponseError.cjs');
12
+
13
+ function _interopNamespaceDefault(e) {
14
+ var n = Object.create(null);
15
+ if (e) {
16
+ Object.keys(e).forEach(function (k) {
17
+ if (k !== 'default') {
18
+ var d = Object.getOwnPropertyDescriptor(e, k);
19
+ Object.defineProperty(n, k, d.get ? d : {
20
+ enumerable: true,
21
+ get: function () { return e[k]; }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n.default = e;
27
+ return Object.freeze(n);
28
+ }
29
+
30
+ var EE__namespace = /*#__PURE__*/_interopNamespaceDefault(EE);
31
+ var SS__namespace = /*#__PURE__*/_interopNamespaceDefault(SS);
32
+ var AA__namespace = /*#__PURE__*/_interopNamespaceDefault(AA);
33
+
34
+ class PromiseRequest extends Promise {
35
+ params;
36
+ hooks = {};
37
+ constructor(params) {
38
+ super((resolve) => void EE__namespace
39
+ .rightAsyncPipe(Promise.resolve(params), (params) => hooks.launchRequestHook(params.hooks.request, this.hooks.request ?? [], params), PromiseRequest.fetch, (response) => hooks.launchResponseHook(params.hooks.response, this.hooks.response ?? [], response), async (response) => {
40
+ if (params.disabledPredicateMode === false && response.predicted === false) {
41
+ await hooks.launchNotPredictedHook(params.hooks.notPredictedResponse, this.hooks.notPredictedResponse ?? [], response);
42
+ return EE__namespace.right("response", response);
43
+ }
44
+ if (SS__namespace.startsWith(response.code, "1")) {
45
+ await hooks.launchResponseTypeHook(params.hooks.informationalResponseType, this.hooks.informationalResponseType ?? [], response);
46
+ }
47
+ else if (SS__namespace.startsWith(response.code, "2")) {
48
+ await hooks.launchResponseTypeHook(params.hooks.successfulResponseType, this.hooks.successfulResponseType ?? [], response);
49
+ await hooks.launchExpectedResponseHook(params.hooks.expectedResponse, this.hooks.expectedResponse ?? [], response);
50
+ }
51
+ else if (SS__namespace.startsWith(response.code, "3")) {
52
+ await hooks.launchResponseTypeHook(params.hooks.redirectionResponseType, this.hooks.redirectionResponseType ?? [], response);
53
+ }
54
+ else if (SS__namespace.startsWith(response.code, "4")) {
55
+ await hooks.launchResponseTypeHook(params.hooks.clientErrorResponseType, this.hooks.clientErrorResponseType ?? [], response);
56
+ await hooks.launchExpectedResponseHook(params.hooks.expectedResponse, this.hooks.expectedResponse ?? [], response);
57
+ }
58
+ else if (SS__namespace.startsWith(response.code, "5")) {
59
+ await hooks.launchResponseTypeHook(params.hooks.serverErrorResponseType, this.hooks.serverErrorResponseType ?? [], response);
60
+ }
61
+ if (response.information !== undefined) {
62
+ await hooks.launchInformationHook(params.hooks.information[response.information] ?? [], this.hooks.information?.[response.information] ?? [], response);
63
+ }
64
+ await hooks.launchCodeHook(params.hooks.code[response.code] ?? [], this.hooks.code?.[response.code] ?? [], response);
65
+ return EE__namespace.right("response", response);
66
+ })
67
+ .then(async (result) => {
68
+ if (EE__namespace.eitherFutureErrorKind.has(result)) {
69
+ const error = utils.unwrap(result);
70
+ await hooks.launchErrorHook(params.hooks.error, this.hooks.error ?? [], error, params);
71
+ return EE__namespace.left("request-error", {
72
+ error,
73
+ requestParams: params,
74
+ });
75
+ }
76
+ return result;
77
+ })
78
+ .then(resolve));
79
+ this.params = params;
80
+ }
81
+ addRequestInterceptor(callback) {
82
+ this.hooks.request ??= [];
83
+ this.hooks.request.push(callback);
84
+ return this;
85
+ }
86
+ addResponseInterceptor(callback) {
87
+ this.hooks.response ??= [];
88
+ this.hooks.response.push(callback);
89
+ return this;
90
+ }
91
+ whenNotPredictedResponse(callback) {
92
+ this.hooks.notPredictedResponse ??= [];
93
+ this.hooks.notPredictedResponse.push(callback);
94
+ return this;
95
+ }
96
+ whenInformation(information, callback) {
97
+ const formattedInformation = AA__namespace.coalescing(information);
98
+ formattedInformation.forEach((information) => {
99
+ this.hooks.information ??= {};
100
+ this.hooks.information[information] ??= [];
101
+ this.hooks.information[information].push(callback);
102
+ });
103
+ return this;
104
+ }
105
+ whenCode(code, callback) {
106
+ const formattedCode = AA__namespace.coalescing(code);
107
+ formattedCode.forEach((code) => {
108
+ this.hooks.code ??= {};
109
+ this.hooks.code[code] ??= [];
110
+ this.hooks.code[code].push(callback);
111
+ });
112
+ return this;
113
+ }
114
+ whenInformationalResponse(callback) {
115
+ this.hooks.informationalResponseType ??= [];
116
+ this.hooks.informationalResponseType.push(callback);
117
+ return this;
118
+ }
119
+ whenSuccessfulResponse(callback) {
120
+ this.hooks.successfulResponseType ??= [];
121
+ this.hooks.successfulResponseType.push(callback);
122
+ return this;
123
+ }
124
+ whenRedirectionResponse(callback) {
125
+ this.hooks.redirectionResponseType ??= [];
126
+ this.hooks.redirectionResponseType.push(callback);
127
+ return this;
128
+ }
129
+ whenClientErrorResponse(callback) {
130
+ this.hooks.clientErrorResponseType ??= [];
131
+ this.hooks.clientErrorResponseType.push(callback);
132
+ return this;
133
+ }
134
+ whenServerErrorResponse(callback) {
135
+ this.hooks.serverErrorResponseType ??= [];
136
+ this.hooks.serverErrorResponseType.push(callback);
137
+ return this;
138
+ }
139
+ whenExpectedResponse(callback) {
140
+ this.hooks.expectedResponse ??= [];
141
+ this.hooks.expectedResponse.push(callback);
142
+ return this;
143
+ }
144
+ whenError(callback) {
145
+ this.hooks.error ??= [];
146
+ this.hooks.error.push(callback);
147
+ return this;
148
+ }
149
+ iWantInformation(information) {
150
+ const formattedInformation = AA__namespace.coalescing(information);
151
+ return this.then(EE__namespace.whenIsRight((response) => {
152
+ if (response.information !== undefined
153
+ && AA__namespace.includes(formattedInformation, response.information)) {
154
+ return EE__namespace.right("response", response);
155
+ }
156
+ return EE__namespace.left("unexpect-response", response);
157
+ }));
158
+ }
159
+ iWantCode(code) {
160
+ const formattedCode = AA__namespace.coalescing(code);
161
+ return this.then(EE__namespace.whenIsRight((response) => {
162
+ if (AA__namespace.includes(formattedCode, response.code)) {
163
+ return EE__namespace.right("response", response);
164
+ }
165
+ return EE__namespace.left("unexpect-response", response);
166
+ }));
167
+ }
168
+ iWantInformationalResponse() {
169
+ return this.then(EE__namespace.whenIsRight((response) => {
170
+ if (SS__namespace.startsWith(response.code, "1")) {
171
+ return EE__namespace.right("response", response);
172
+ }
173
+ return EE__namespace.left("unexpect-response", response);
174
+ }));
175
+ }
176
+ iWantSuccessfulResponse() {
177
+ return this.then(EE__namespace.whenIsRight((response) => {
178
+ if (SS__namespace.startsWith(response.code, "2")) {
179
+ return EE__namespace.right("response", response);
180
+ }
181
+ return EE__namespace.left("unexpect-response", response);
182
+ }));
183
+ }
184
+ iWantRedirectionResponse() {
185
+ return this.then(EE__namespace.whenIsRight((response) => {
186
+ if (SS__namespace.startsWith(response.code, "3")) {
187
+ return EE__namespace.right("response", response);
188
+ }
189
+ return EE__namespace.left("unexpect-response", response);
190
+ }));
191
+ }
192
+ iWantClientErrorResponse() {
193
+ return this.then(EE__namespace.whenIsRight((response) => {
194
+ if (SS__namespace.startsWith(response.code, "4")) {
195
+ return EE__namespace.right("response", response);
196
+ }
197
+ return EE__namespace.left("unexpect-response", response);
198
+ }));
199
+ }
200
+ iWantServerErrorResponse() {
201
+ return this.then(EE__namespace.whenIsRight((response) => {
202
+ if (SS__namespace.startsWith(response.code, "5")) {
203
+ return EE__namespace.right("response", response);
204
+ }
205
+ return EE__namespace.left("unexpect-response", response);
206
+ }));
207
+ }
208
+ iWantExpectedResponse() {
209
+ return this.then(EE__namespace.whenIsRight((response) => {
210
+ if (SS__namespace.startsWith(response.code, "2") || SS__namespace.startsWith(response.code, "4")) {
211
+ return EE__namespace.right("response", response);
212
+ }
213
+ return EE__namespace.left("unexpect-response", response);
214
+ }));
215
+ }
216
+ iWantInformationOrThrow(information) {
217
+ return this
218
+ .iWantInformation(information)
219
+ .then((maybeResponse) => {
220
+ if (EE__namespace.isRight(maybeResponse)) {
221
+ return utils.unwrap(maybeResponse);
222
+ }
223
+ throw new unexpectedResponseError.UnexpectedInformationResponseError(information, utils.unwrap(maybeResponse));
224
+ });
225
+ }
226
+ iWantCodeOrThrow(code) {
227
+ return this
228
+ .iWantCode(code)
229
+ .then((maybeResponse) => {
230
+ if (EE__namespace.isRight(maybeResponse)) {
231
+ return utils.unwrap(maybeResponse);
232
+ }
233
+ throw new unexpectedResponseError.UnexpectedCodeResponseError(code, utils.unwrap(maybeResponse));
234
+ });
235
+ }
236
+ iWantInformationalResponseOrThrow() {
237
+ return this
238
+ .iWantInformationalResponse()
239
+ .then((maybeResponse) => {
240
+ if (EE__namespace.isRight(maybeResponse)) {
241
+ return utils.unwrap(maybeResponse);
242
+ }
243
+ throw new unexpectedResponseError.UnexpectedResponseTypeError("informational", utils.unwrap(maybeResponse));
244
+ });
245
+ }
246
+ iWantSuccessfulResponseOrThrow() {
247
+ return this
248
+ .iWantSuccessfulResponse()
249
+ .then((maybeResponse) => {
250
+ if (EE__namespace.isRight(maybeResponse)) {
251
+ return utils.unwrap(maybeResponse);
252
+ }
253
+ throw new unexpectedResponseError.UnexpectedResponseTypeError("successful", utils.unwrap(maybeResponse));
254
+ });
255
+ }
256
+ iWantRedirectionResponseOrThrow() {
257
+ return this
258
+ .iWantRedirectionResponse()
259
+ .then((maybeResponse) => {
260
+ if (EE__namespace.isRight(maybeResponse)) {
261
+ return utils.unwrap(maybeResponse);
262
+ }
263
+ throw new unexpectedResponseError.UnexpectedResponseTypeError("redirection", utils.unwrap(maybeResponse));
264
+ });
265
+ }
266
+ iWantClientErrorResponseOrThrow() {
267
+ return this
268
+ .iWantClientErrorResponse()
269
+ .then((maybeResponse) => {
270
+ if (EE__namespace.isRight(maybeResponse)) {
271
+ return utils.unwrap(maybeResponse);
272
+ }
273
+ throw new unexpectedResponseError.UnexpectedResponseTypeError("clientError", utils.unwrap(maybeResponse));
274
+ });
275
+ }
276
+ iWantServerErrorResponseOrThrow() {
277
+ return this
278
+ .iWantServerErrorResponse()
279
+ .then((maybeResponse) => {
280
+ if (EE__namespace.isRight(maybeResponse)) {
281
+ return utils.unwrap(maybeResponse);
282
+ }
283
+ throw new unexpectedResponseError.UnexpectedResponseTypeError("informational", utils.unwrap(maybeResponse));
284
+ });
285
+ }
286
+ iWantExpectedResponseOrThrow() {
287
+ return this
288
+ .iWantExpectedResponse()
289
+ .then((maybeResponse) => {
290
+ if (EE__namespace.isRight(maybeResponse)) {
291
+ return utils.unwrap(maybeResponse);
292
+ }
293
+ throw new unexpectedResponseError.UnexpectedResponseError(utils.unwrap(maybeResponse));
294
+ });
295
+ }
296
+ static get [Symbol.species]() {
297
+ return Promise;
298
+ }
299
+ static fetch(requestParams) {
300
+ const path = insertParamsInPath.insertParamsInPath(requestParams.path, requestParams.params);
301
+ const query = queryToString.queryToString(requestParams.query);
302
+ const url = query
303
+ ? `${path}?${query}`
304
+ : path;
305
+ const headers = { ...requestParams.headers };
306
+ let body = requestParams.body;
307
+ if (body) {
308
+ if (!headers["content-type"]) {
309
+ if (typeof body === "string") {
310
+ headers["content-type"] = "text/plain; charset=utf-8";
311
+ body = body.toString();
312
+ }
313
+ else if ((body
314
+ && typeof body === "object"
315
+ && body?.constructor?.name === "Object")
316
+ || (body instanceof Array
317
+ && body?.constructor?.name === "Array")
318
+ || body === null
319
+ || typeof body === "boolean"
320
+ || typeof body === "number") {
321
+ headers["content-type"] = "application/json; charset=utf-8";
322
+ body = JSON.stringify(body);
323
+ }
324
+ }
325
+ }
326
+ return fetch(`${requestParams.baseUrl}${url}`, {
327
+ ...requestParams.initParams,
328
+ headers: headers,
329
+ method: requestParams.method,
330
+ body: body,
331
+ })
332
+ .then((response) => getBody.getBody(response)
333
+ .then((body) => EE__namespace.right("response", {
334
+ body,
335
+ information: response.headers.get(requestParams.informationHeaderKey) || undefined,
336
+ code: response.status.toString(),
337
+ ok: (response.status < 500)
338
+ ? response.ok
339
+ : null,
340
+ headers: response.headers,
341
+ type: response.type,
342
+ url: response.url,
343
+ redirected: response.redirected,
344
+ raw: response,
345
+ requestParams,
346
+ predicted: response.headers.get(requestParams.predictedHeaderKey) !== null,
347
+ })))
348
+ .catch((error) => EE__namespace.left("request-error", {
349
+ error,
350
+ requestParams,
351
+ }));
352
+ }
353
+ }
354
+
355
+ exports.PromiseRequest = PromiseRequest;
@@ -0,0 +1,98 @@
1
+ import { type NeverCoalescing, type MaybePromise } from "@duplojs/utils";
2
+ import { type Hooks, type ErrorHook, type NotPredictedResponseHook } from "./hooks";
3
+ import * as EE from "@duplojs/utils/either";
4
+ import { type RequestErrorContent } from "./unexpectedResponseError";
5
+ import { type NotPredictedClientResponse, type ClientRequestParams, type ClientResponse } from "./types";
6
+ type MaybeResponse<GenericClientResponse extends ClientResponse = ClientResponse> = (EE.EitherRight<"response", GenericClientResponse> | EE.EitherLeft<"request-error", RequestErrorContent>);
7
+ type MaybeWantedResponse<GenericWantedClientResponse extends ClientResponse = ClientResponse, GenericUnexpectClientResponse extends ClientResponse = ClientResponse> = (EE.EitherRight<"response", GenericWantedClientResponse> | EE.EitherLeft<"unexpect-response", GenericUnexpectClientResponse> | EE.EitherLeft<"request-error", RequestErrorContent>);
8
+ export interface PromiseRequestParams<GenericHookParams extends Record<string, unknown> = Record<string, unknown>> extends ClientRequestParams<GenericHookParams> {
9
+ baseUrl: string;
10
+ hooks: Hooks;
11
+ informationHeaderKey: string;
12
+ predictedHeaderKey: string;
13
+ disabledPredicateMode: boolean;
14
+ }
15
+ export declare class PromiseRequest<GenericPromiseRequestParams extends PromiseRequestParams = PromiseRequestParams, GenericClientResponse extends ClientResponse = ClientResponse> extends Promise<MaybeResponse<GenericClientResponse | NotPredictedClientResponse<GenericPromiseRequestParams>>> {
16
+ params: PromiseRequestParams;
17
+ readonly hooks: Partial<Hooks>;
18
+ constructor(params: PromiseRequestParams);
19
+ addRequestInterceptor(callback: (requestParams: GenericPromiseRequestParams) => MaybePromise<GenericPromiseRequestParams>): this;
20
+ addResponseInterceptor(callback: (response: GenericClientResponse) => MaybePromise<GenericClientResponse>): this;
21
+ whenNotPredictedResponse(callback: NotPredictedResponseHook<GenericPromiseRequestParams>): this;
22
+ whenInformation<GenericInformation extends Extract<GenericClientResponse["information"], string>>(information: GenericInformation | GenericInformation[], callback: (response: NeverCoalescing<Extract<GenericClientResponse, GenericInformation extends any ? {
23
+ information: GenericInformation;
24
+ } : never>, ClientResponse<GenericPromiseRequestParams>>) => MaybePromise<void>): this;
25
+ whenCode<GenericCode extends GenericClientResponse["code"]>(code: GenericCode | GenericCode[], callback: (response: NeverCoalescing<Extract<GenericClientResponse, GenericCode extends any ? {
26
+ code: GenericCode;
27
+ } : never>, ClientResponse<GenericPromiseRequestParams>>) => MaybePromise<void>): this;
28
+ whenInformationalResponse(callback: (response: NeverCoalescing<Extract<GenericClientResponse, {
29
+ code: `1${number}`;
30
+ }>, ClientResponse<GenericPromiseRequestParams>>) => MaybePromise<void>): this;
31
+ whenSuccessfulResponse(callback: (response: NeverCoalescing<Extract<GenericClientResponse, {
32
+ code: `2${number}`;
33
+ }>, ClientResponse<GenericPromiseRequestParams>>) => MaybePromise<void>): this;
34
+ whenRedirectionResponse(callback: (response: NeverCoalescing<Extract<GenericClientResponse, {
35
+ code: `3${number}`;
36
+ }>, ClientResponse<GenericPromiseRequestParams>>) => MaybePromise<void>): this;
37
+ whenClientErrorResponse(callback: (response: NeverCoalescing<Extract<GenericClientResponse, {
38
+ code: `4${number}`;
39
+ }>, ClientResponse<GenericPromiseRequestParams>>) => MaybePromise<void>): this;
40
+ whenServerErrorResponse(callback: (response: NeverCoalescing<Extract<GenericClientResponse, {
41
+ code: `5${number}`;
42
+ }>, ClientResponse<GenericPromiseRequestParams>>) => MaybePromise<void>): this;
43
+ whenExpectedResponse(callback: (response: NeverCoalescing<Extract<GenericClientResponse, {
44
+ code: `2${number}` | `4${number}`;
45
+ }>, ClientResponse<GenericPromiseRequestParams>>) => MaybePromise<void>): this;
46
+ whenError(callback: ErrorHook<GenericPromiseRequestParams>): this;
47
+ iWantInformation<GenericInformation extends Extract<GenericClientResponse["information"], string>, GenericResponse extends NeverCoalescing<Extract<GenericClientResponse, GenericInformation extends any ? {
48
+ information: GenericInformation;
49
+ } : never>, ClientResponse<GenericPromiseRequestParams>>>(information: GenericInformation | GenericInformation[]): Promise<MaybeWantedResponse<GenericResponse, NeverCoalescing<Exclude<GenericClientResponse, GenericResponse>, ClientResponse<GenericPromiseRequestParams>>>>;
50
+ iWantCode<GenericCode extends GenericClientResponse["code"], GenericResponse extends NeverCoalescing<Extract<GenericClientResponse, GenericCode extends any ? {
51
+ code: GenericCode;
52
+ } : never>, ClientResponse<GenericPromiseRequestParams>>>(code: GenericCode | GenericCode[]): Promise<MaybeWantedResponse<GenericResponse, NeverCoalescing<Exclude<GenericClientResponse, GenericResponse>, ClientResponse<GenericPromiseRequestParams>>>>;
53
+ iWantInformationalResponse<GenericResponse extends NeverCoalescing<Extract<GenericClientResponse, {
54
+ code: `1${number}`;
55
+ }>, ClientResponse<GenericPromiseRequestParams>>>(): Promise<MaybeWantedResponse<GenericResponse, NeverCoalescing<Exclude<GenericClientResponse, GenericResponse>, ClientResponse<GenericPromiseRequestParams>>>>;
56
+ iWantSuccessfulResponse<GenericResponse extends NeverCoalescing<Extract<GenericClientResponse, {
57
+ code: `2${number}`;
58
+ }>, ClientResponse<GenericPromiseRequestParams>>>(): Promise<MaybeWantedResponse<GenericResponse, NeverCoalescing<Exclude<GenericClientResponse, GenericResponse>, ClientResponse<GenericPromiseRequestParams>>>>;
59
+ iWantRedirectionResponse<GenericResponse extends NeverCoalescing<Extract<GenericClientResponse, {
60
+ code: `3${number}`;
61
+ }>, ClientResponse<GenericPromiseRequestParams>>>(): Promise<MaybeWantedResponse<GenericResponse, NeverCoalescing<Exclude<GenericClientResponse, GenericResponse>, ClientResponse<GenericPromiseRequestParams>>>>;
62
+ iWantClientErrorResponse<GenericResponse extends NeverCoalescing<Extract<GenericClientResponse, {
63
+ code: `4${number}`;
64
+ }>, ClientResponse<GenericPromiseRequestParams>>>(): Promise<MaybeWantedResponse<GenericResponse, NeverCoalescing<Exclude<GenericClientResponse, GenericResponse>, ClientResponse<GenericPromiseRequestParams>>>>;
65
+ iWantServerErrorResponse<GenericResponse extends NeverCoalescing<Extract<GenericClientResponse, {
66
+ code: `5${number}`;
67
+ }>, ClientResponse<GenericPromiseRequestParams>>>(): Promise<MaybeWantedResponse<GenericResponse, NeverCoalescing<Exclude<GenericClientResponse, GenericResponse>, ClientResponse<GenericPromiseRequestParams>>>>;
68
+ iWantExpectedResponse<GenericResponse extends NeverCoalescing<Extract<GenericClientResponse, {
69
+ code: `2${number}` | `4${number}`;
70
+ }>, ClientResponse<GenericPromiseRequestParams>>>(): Promise<MaybeWantedResponse<GenericResponse, NeverCoalescing<Exclude<GenericClientResponse, GenericResponse>, ClientResponse<GenericPromiseRequestParams>>>>;
71
+ iWantInformationOrThrow<GenericInformation extends Extract<GenericClientResponse["information"], string>>(information: GenericInformation | GenericInformation[]): Promise<NeverCoalescing<Extract<GenericClientResponse, GenericInformation extends any ? {
72
+ information: GenericInformation;
73
+ } : never>, ClientResponse<GenericPromiseRequestParams>>>;
74
+ iWantCodeOrThrow<GenericCode extends GenericClientResponse["code"]>(code: GenericCode | GenericCode[]): Promise<NeverCoalescing<Extract<GenericClientResponse, GenericCode extends any ? {
75
+ code: GenericCode;
76
+ } : never>, ClientResponse<GenericPromiseRequestParams>>>;
77
+ iWantInformationalResponseOrThrow(): Promise<NeverCoalescing<Extract<GenericClientResponse, {
78
+ code: `1${number}`;
79
+ }>, ClientResponse<GenericPromiseRequestParams>>>;
80
+ iWantSuccessfulResponseOrThrow(): Promise<NeverCoalescing<Extract<GenericClientResponse, {
81
+ code: `2${number}`;
82
+ }>, ClientResponse<GenericPromiseRequestParams>>>;
83
+ iWantRedirectionResponseOrThrow(): Promise<NeverCoalescing<Extract<GenericClientResponse, {
84
+ code: `3${number}`;
85
+ }>, ClientResponse<GenericPromiseRequestParams>>>;
86
+ iWantClientErrorResponseOrThrow(): Promise<NeverCoalescing<Extract<GenericClientResponse, {
87
+ code: `4${number}`;
88
+ }>, ClientResponse<GenericPromiseRequestParams>>>;
89
+ iWantServerErrorResponseOrThrow(): Promise<NeverCoalescing<Extract<GenericClientResponse, {
90
+ code: `5${number}`;
91
+ }>, ClientResponse<GenericPromiseRequestParams>>>;
92
+ iWantExpectedResponseOrThrow(): Promise<NeverCoalescing<Extract<GenericClientResponse, {
93
+ code: `2${number}` | `4${number}`;
94
+ }>, ClientResponse<GenericPromiseRequestParams>>>;
95
+ static get [Symbol.species](): PromiseConstructor;
96
+ static fetch<GenericPromiseRequestParams extends PromiseRequestParams>(requestParams: GenericPromiseRequestParams): Promise<MaybeResponse>;
97
+ }
98
+ export {};