@hyper-fetch/core 7.5.2 → 7.5.3

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 (54) hide show
  1. package/__tests__/features/adapter/adapter.base.spec.ts +387 -0
  2. package/__tests__/features/adapter/adapter.bindings.spec.ts +1023 -0
  3. package/__tests__/features/adapter/adapter.browser.spec.ts +103 -0
  4. package/__tests__/features/adapter/adapter.server.spec.ts +177 -0
  5. package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
  6. package/__tests__/features/cache/cache.base.spec.ts +358 -0
  7. package/__tests__/features/cache/cache.events.spec.ts +215 -0
  8. package/__tests__/features/cache/cache.garbage-collector.spec.ts +172 -0
  9. package/__tests__/features/cache/cache.lazy-storage.spec.ts +129 -0
  10. package/__tests__/features/cache/cache.utils.spec.ts +119 -0
  11. package/__tests__/features/client/client.auth.spec.ts +125 -0
  12. package/__tests__/features/client/client.base.spec.ts +149 -0
  13. package/__tests__/features/client/client.hydration.spec.ts +171 -0
  14. package/__tests__/features/client/client.interceptor.spec.ts +225 -0
  15. package/__tests__/features/client/client.methods.spec.ts +188 -0
  16. package/__tests__/features/client/client.middleware.spec.ts +118 -0
  17. package/__tests__/features/client/client.utils.spec.ts +292 -0
  18. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
  19. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +304 -0
  20. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
  21. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
  22. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +125 -0
  23. package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
  24. package/__tests__/features/managers/app/app.manager.ssr.spec.ts +40 -0
  25. package/__tests__/features/managers/logger/logger.base.spec.ts +107 -0
  26. package/__tests__/features/managers/logger/logger.utils.spec.ts +100 -0
  27. package/__tests__/features/managers/request/request.manager.base.spec.ts +222 -0
  28. package/__tests__/features/managers/request/request.manager.events.spec.ts +280 -0
  29. package/__tests__/features/mocker/mocker.base.spec.ts +567 -0
  30. package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
  31. package/__tests__/features/request/request.base.spec.ts +199 -0
  32. package/__tests__/features/request/request.cloning.spec.ts +47 -0
  33. package/__tests__/features/request/request.sending.spec.ts +272 -0
  34. package/__tests__/features/request/request.setters.spec.ts +157 -0
  35. package/__tests__/features/request/request.typescript.spec.ts +423 -0
  36. package/__tests__/features/request/request.utils.spec.ts +77 -0
  37. package/__tests__/features/sdk/sdk.base.spec.ts +130 -0
  38. package/__tests__/global.d.ts +1 -0
  39. package/__tests__/jest.setup.ts +13 -0
  40. package/__tests__/shared/callbacks.shared.ts +9 -0
  41. package/__tests__/shared/index.ts +2 -0
  42. package/__tests__/shared/progress.shared.ts +35 -0
  43. package/__tests__/tsconfig.json +13 -0
  44. package/__tests__/utils/adapter.utils.ts +38 -0
  45. package/__tests__/utils/cache.utils.ts +19 -0
  46. package/__tests__/utils/callbacks.utils.ts +47 -0
  47. package/__tests__/utils/dispatcher.utils.ts +6 -0
  48. package/__tests__/utils/index.ts +4 -0
  49. package/dist/index.js +12 -1
  50. package/dist/index.js.map +2 -2
  51. package/jest.config.ts +6 -0
  52. package/package.json +1 -1
  53. package/project.json +4 -0
  54. package/src/http-adapter/http-adapter.server.ts +15 -1
@@ -0,0 +1,103 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
5
+
6
+ import { getErrorMessage } from "adapter";
7
+ import { Client } from "client";
8
+ import { HttpAdapter } from "http-adapter";
9
+
10
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
11
+
12
+ describe("Http Adapter [ Browser ]", () => {
13
+ const requestId = "test";
14
+
15
+ let client = new Client({ url: "https://shared-base-url" });
16
+ let request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint" });
17
+
18
+ beforeAll(() => {
19
+ startServer();
20
+ });
21
+
22
+ beforeEach(() => {
23
+ client = new Client({ url: "https://shared-base-url" });
24
+ request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint" });
25
+
26
+ request.client.requestManager.addAbortController(request.abortKey, requestId);
27
+ resetMocks();
28
+ jest.resetAllMocks();
29
+ });
30
+
31
+ afterAll(() => {
32
+ stopServer();
33
+ });
34
+
35
+ it("should make a request and return success data with status", async () => {
36
+ const data = mockRequest(request, { data: { response: [] } });
37
+
38
+ const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
39
+
40
+ expect(response).toStrictEqual(data);
41
+ expect(status).toBe(200);
42
+ expect(error).toBe(null);
43
+ expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "15" } });
44
+ });
45
+
46
+ it("should make a request and return error data with status", async () => {
47
+ const data = mockRequest(request, { status: 400 });
48
+
49
+ const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
50
+
51
+ expect(response).toBe(null);
52
+ expect(status).toBe(400);
53
+ expect(error).toStrictEqual(data);
54
+ expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "19" } });
55
+ });
56
+
57
+ it("should allow to cancel request and return error", async () => {
58
+ mockRequest(request, { delay: 5 });
59
+
60
+ setTimeout(() => {
61
+ request.abort();
62
+ }, 2);
63
+
64
+ const { data: response, error } = await HttpAdapter().initialize(client).fetch(request, requestId);
65
+
66
+ expect(response).toBe(null);
67
+ expect(error.message).toEqual(getErrorMessage("abort").message);
68
+ });
69
+
70
+ it("should return timeout error when request takes too long", async () => {
71
+ const timeoutRequest = request.setOptions({ timeout: 5 });
72
+ mockRequest(timeoutRequest, { delay: 20 });
73
+
74
+ const { data: response, error } = await HttpAdapter().initialize(client).fetch(timeoutRequest, requestId);
75
+
76
+ expect(response).toBe(null);
77
+ expect(error.message).toEqual(getErrorMessage("timeout").message);
78
+ });
79
+
80
+ it("should use GET as default method when no method is specified", async () => {
81
+ const mockSpy = jest.spyOn(XMLHttpRequest.prototype, "open");
82
+ const data = mockRequest(request, { data: { response: [] } });
83
+
84
+ const { data: response } = await HttpAdapter().initialize(client).fetch(request, requestId);
85
+
86
+ expect(mockSpy).toHaveBeenCalledWith("GET", "https://shared-base-url/shared-endpoint", true);
87
+ expect(response).toStrictEqual(data);
88
+ });
89
+
90
+ // it("should not throw when XMLHttpRequest is not available on window", async () => {
91
+ // const data = mockRequest(request, { delay: 20 });
92
+ // const xml = window.XMLHttpRequest;
93
+ // window.XMLHttpRequest = undefined as any;
94
+
95
+ // const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
96
+
97
+ // expect(response).toStrictEqual(data);
98
+ // expect(status).toBe(200);
99
+ // expect(error).toBe(null);
100
+ // expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
101
+ // window.XMLHttpRequest = xml;
102
+ // });
103
+ });
@@ -0,0 +1,177 @@
1
+ /**
2
+ * @jest-environment node
3
+ */
4
+ import http from "http";
5
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
6
+
7
+ import { getErrorMessage } from "adapter";
8
+ import { Client } from "client";
9
+ import { HttpAdapter } from "http-adapter";
10
+
11
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
12
+
13
+ describe("Http Adapter [ Server ]", () => {
14
+ const requestId = "test";
15
+ const abortKey = "abort-key";
16
+ const requestCopy = http.request;
17
+
18
+ let client = new Client({ url: "http://shared-base-url" });
19
+ let clientHttps = new Client({ url: "https://shared-base-url" });
20
+ let request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint", abortKey });
21
+ let requestHttps = clientHttps.createRequest()({ endpoint: "/shared-endpoint", abortKey });
22
+
23
+ beforeAll(() => {
24
+ startServer();
25
+ });
26
+
27
+ beforeEach(() => {
28
+ client = new Client({ url: "http://shared-base-url" });
29
+ clientHttps = new Client({ url: "https://shared-base-url" });
30
+ client.appManager.isBrowser = false;
31
+ clientHttps.appManager.isBrowser = false;
32
+
33
+ request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint", abortKey });
34
+ requestHttps = clientHttps.createRequest()({ endpoint: "/shared-endpoint", abortKey });
35
+
36
+ client.requestManager.addAbortController(abortKey, requestId);
37
+ clientHttps.requestManager.addAbortController(abortKey, requestId);
38
+
39
+ resetMocks();
40
+ jest.resetAllMocks();
41
+ jest.clearAllMocks();
42
+ jest.restoreAllMocks();
43
+ });
44
+
45
+ afterAll(() => {
46
+ stopServer();
47
+ });
48
+
49
+ it("should pick correct adapter and not throw", async () => {
50
+ mockRequest(request);
51
+ client.appManager.isBrowser = true;
52
+ await expect(() => HttpAdapter().initialize(client).fetch(request, requestId)).not.toThrow();
53
+ });
54
+
55
+ it("should pick https module", async () => {
56
+ mockRequest(requestHttps);
57
+ await expect(() => HttpAdapter().initialize(client).fetch(requestHttps, requestId)).not.toThrow();
58
+ });
59
+
60
+ it("should make a request and return success data with status", async () => {
61
+ const data = mockRequest(request, { data: { response: [] } });
62
+
63
+ const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
64
+
65
+ expect(response).toStrictEqual(data);
66
+ expect(status).toBe(200);
67
+ expect(error).toBe(null);
68
+ expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "15" } });
69
+ });
70
+
71
+ it("should make a request and return error data with status", async () => {
72
+ const data = mockRequest(request, { status: 400 });
73
+
74
+ const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
75
+
76
+ expect(response).toBe(null);
77
+ expect(status).toBe(400);
78
+ expect(error).toStrictEqual(data);
79
+ expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "19" } });
80
+ });
81
+
82
+ it("should allow to cancel request and return error", async () => {
83
+ mockRequest(request, { delay: 5 });
84
+
85
+ const id = setTimeout(() => {
86
+ request.abort();
87
+ }, 2);
88
+
89
+ const { data: response, error } = await HttpAdapter().initialize(client).fetch(request, requestId);
90
+
91
+ expect(response).toBe(null);
92
+ expect(error.message).toEqual(getErrorMessage("abort").message);
93
+
94
+ clearTimeout(id);
95
+ });
96
+
97
+ it("should return timeout error when request takes too long", async () => {
98
+ const timeoutRequest = request.setOptions({ timeout: 10 });
99
+ mockRequest(timeoutRequest, { delay: 20 });
100
+
101
+ const { data: response, error } = await HttpAdapter().initialize(client).fetch(timeoutRequest, requestId);
102
+
103
+ expect(response).toBe(null);
104
+ expect(error.message).toEqual(getErrorMessage("timeout").message);
105
+ });
106
+
107
+ it("should allow to make post request with json data", async () => {
108
+ const payload = {
109
+ testData: "123",
110
+ };
111
+ const postRequest = client
112
+ .createRequest<{
113
+ response: null;
114
+ payload: { testData: string };
115
+ }>()({ endpoint: "shared-endpoint", method: "POST" })
116
+ .setPayload(payload);
117
+ client.requestManager.addAbortController(postRequest.abortKey, requestId);
118
+ const mock = mockRequest(postRequest);
119
+
120
+ const {
121
+ data: response,
122
+ error,
123
+ status,
124
+ extra,
125
+ } = await HttpAdapter().initialize(client).fetch(postRequest, requestId);
126
+
127
+ expect(response).toEqual(mock);
128
+ expect(error).toBeNull();
129
+ expect(status).toEqual(200);
130
+ expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
131
+ });
132
+
133
+ it("should allow to calculate payload size", async () => {
134
+ let receivedOptions: any;
135
+ const mutation = client.createRequest<{ response: any; payload: any }>()({
136
+ endpoint: "/shared-endpoint",
137
+ method: "POST",
138
+ });
139
+
140
+ jest.spyOn(http, "request").mockImplementation((_, options, callback) => {
141
+ receivedOptions = options;
142
+ return requestCopy(options, callback);
143
+ });
144
+ mockRequest(mutation);
145
+
146
+ await mutation.send({
147
+ payload: {
148
+ username: "Kacper",
149
+ password: "Kacper1234",
150
+ },
151
+ });
152
+
153
+ expect(receivedOptions.headers["Content-Length"]).toBeGreaterThan(0);
154
+ });
155
+
156
+ it("should allow to calculate Buffer size", async () => {
157
+ let receivedOptions: any;
158
+ const mutation = client.createRequest<{ response: any; payload: any }>()({
159
+ endpoint: "/shared-endpoint",
160
+ method: "POST",
161
+ });
162
+
163
+ jest.spyOn(http, "request").mockImplementation((_, options, callback) => {
164
+ receivedOptions = options;
165
+ return requestCopy(options, callback);
166
+ });
167
+ mockRequest(mutation);
168
+
169
+ const buffer = Buffer.from("test");
170
+
171
+ await mutation.send({
172
+ payload: buffer as any,
173
+ });
174
+
175
+ expect(receivedOptions.headers["Content-Length"]).toBeGreaterThan(0);
176
+ });
177
+ });
@@ -0,0 +1,165 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import { createClient } from "client";
4
+ import { getAdapterHeaders, getAdapterPayload, getErrorMessage } from "adapter";
5
+ import { parseResponse, parseErrorResponse, handleResponse, getResponseHeaders } from "http-adapter";
6
+
7
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
8
+
9
+ describe("Adapter [ Utils ]", () => {
10
+ beforeAll(() => {
11
+ startServer();
12
+ });
13
+
14
+ beforeEach(() => {
15
+ resetMocks();
16
+ });
17
+
18
+ afterAll(() => {
19
+ stopServer();
20
+ });
21
+
22
+ describe("When using getAdapterHeaders util", () => {
23
+ it("should allow to stringify payload", async () => {
24
+ const request = createClient({ url: "" }).createRequest<{ response: any; payload: FormData }>()({
25
+ endpoint: "shared-base-endpoint",
26
+ });
27
+
28
+ expect(() => getAdapterHeaders(request)).not.toThrow();
29
+ });
30
+ });
31
+
32
+ describe("When using getAdapterPayload util", () => {
33
+ it("should allow to stringify payload", async () => {
34
+ const data = { data: [] };
35
+
36
+ expect(() => getAdapterPayload(data)).not.toThrow();
37
+ });
38
+ });
39
+ describe("When getErrorMessage util get triggered", () => {
40
+ it("should return abort error", async () => {
41
+ const error = getErrorMessage("abort");
42
+ expect(error.message).toBe("Request aborted");
43
+ });
44
+ it("should return timeout error", async () => {
45
+ const error = getErrorMessage("timeout");
46
+ expect(error.message).toBe("Request timeout");
47
+ });
48
+ it("should return unexpected error", async () => {
49
+ const error = getErrorMessage();
50
+ expect(error.message).toBe("Unexpected error");
51
+ const error2 = getErrorMessage("unexpected");
52
+ expect(error2.message).toBe("Unexpected error");
53
+ });
54
+ it("should return deleted error", async () => {
55
+ const error = getErrorMessage("deleted");
56
+ expect(error.message).toBe("Request deleted");
57
+ });
58
+ it("should return processing error", async () => {
59
+ const error = getErrorMessage("processing");
60
+ expect(error.message).toBe("Request processing error");
61
+ });
62
+ });
63
+
64
+ describe("When getResponseHeaders util get triggered", () => {
65
+ it("should return headers", async () => {
66
+ const headersString = "Content-Type: application/json\nAuthorization: Bearer token";
67
+ const headers = getResponseHeaders(headersString);
68
+ expect(headers).toEqual({
69
+ "Content-Type": "application/json",
70
+ Authorization: "Bearer token",
71
+ });
72
+ });
73
+
74
+ it("should return empty headers", async () => {
75
+ const headersString = "";
76
+ const headers = getResponseHeaders(headersString);
77
+ expect(headers).toEqual({});
78
+ });
79
+
80
+ it("should handle header values containing colons", async () => {
81
+ const headersString = "Content-Type: application/json\nData: key: value: with: colons";
82
+ const headers = getResponseHeaders(headersString);
83
+ expect(headers).toEqual({
84
+ "Content-Type": "application/json",
85
+ Data: "key: value: with: colons",
86
+ });
87
+ });
88
+
89
+ it("should ignore malformed header lines", async () => {
90
+ const headersString =
91
+ "Content-Type: application/json\nmalformed-line-without-separator\nAuthorization: Bearer token";
92
+ const headers = getResponseHeaders(headersString);
93
+ expect(headers).toEqual({
94
+ "Content-Type": "application/json",
95
+ Authorization: "Bearer token",
96
+ "malformed-line-without-separator": "",
97
+ });
98
+ });
99
+ });
100
+
101
+ describe("When parseResponse util get triggered", () => {
102
+ it("should return parsed response json", async () => {
103
+ const response = { something: 123 };
104
+ const parsed = parseResponse(JSON.stringify(response));
105
+ expect(parsed).toEqual(response);
106
+ });
107
+ it("should return invalid original on parsing error", async () => {
108
+ const invalidResponse = () => null;
109
+ const parsed = parseResponse(invalidResponse);
110
+ expect(parsed).toBe(invalidResponse);
111
+ });
112
+ });
113
+
114
+ describe("When parseErrorResponse util get triggered", () => {
115
+ it("should return parsed error json", async () => {
116
+ const response = { something: 123 };
117
+ const parsed = parseErrorResponse(JSON.stringify(response));
118
+ expect(parsed).toEqual(response);
119
+ });
120
+ it("should return unexpected error when no response is passed", async () => {
121
+ const parsed = parseErrorResponse(null);
122
+ expect(parsed?.message).toBe("Unexpected error");
123
+ });
124
+ });
125
+
126
+ describe("When handleResponse util gets triggered", () => {
127
+ it("should return Buffer for arraybuffer response type", () => {
128
+ const chunks = [Buffer.from("Hello"), Buffer.from("World")];
129
+ const result = handleResponse(chunks, "arraybuffer", "utf-8");
130
+
131
+ expect(Buffer.isBuffer(result)).toBe(true);
132
+ expect(result.toString()).toBe("HelloWorld");
133
+ });
134
+
135
+ it("should parse JSON response type", () => {
136
+ const data = { message: "test" };
137
+ const chunks = [Buffer.from(JSON.stringify(data))];
138
+ const result = handleResponse(chunks, "json", "utf-8");
139
+
140
+ expect(result).toEqual(data);
141
+ });
142
+
143
+ it("should return string for default response type", () => {
144
+ const chunks = [Buffer.from("Hello"), Buffer.from("World")];
145
+ const result = handleResponse(chunks, "text", "utf-8");
146
+
147
+ expect(typeof result).toBe("string");
148
+ expect(result).toBe("HelloWorld");
149
+ });
150
+
151
+ it("should handle empty chunks", () => {
152
+ const chunks: Buffer[] = [];
153
+ const result = handleResponse(chunks, "text", "utf-8");
154
+
155
+ expect(result).toBe("");
156
+ });
157
+
158
+ it("should handle invalid JSON for json response type", () => {
159
+ const chunks = [Buffer.from("invalid json")];
160
+ const result = handleResponse(chunks, "json", "utf-8");
161
+
162
+ expect(result).toBe("invalid json");
163
+ });
164
+ });
165
+ });