@hyper-fetch/core 9.7.0 → 9.8.1

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
@@ -106,7 +106,7 @@ const { data } = await getUser.setParams({ userId: 1 }).send();
106
106
 
107
107
  // Payload is type-checked: { name, email } required here
108
108
  const { data: newUser } = await createUser.send({
109
- data: { name: "Jane", email: "jane@example.com" },
109
+ payload: { name: "Jane", email: "jane@example.com" },
110
110
  });
111
111
  ```
112
112
 
@@ -0,0 +1,34 @@
1
+ import { expectTypeOf } from "vitest";
2
+
3
+ import { Adapter } from "adapter";
4
+ import { createClient } from "client";
5
+ import { HttpMethods } from "constants/http.constants";
6
+ import type { ExtractAdapterType, ExtractUnionAdapter } from "types";
7
+
8
+ const createCustomAdapter = () =>
9
+ new Adapter({
10
+ name: "custom-adapter",
11
+ defaultMethod: HttpMethods.GET,
12
+ defaultExtra: {},
13
+ systemErrorStatus: 0,
14
+ systemErrorExtra: {},
15
+ });
16
+
17
+ type CustomAdapter = ReturnType<typeof createCustomAdapter>;
18
+
19
+ describe("Adapter [ TypeScript ]", () => {
20
+ describe("when selecting the adapter of a request", () => {
21
+ it("should keep a single adapter without comparing it", () => {
22
+ // A single adapter is returned as it is - comparing its whole class is too deep for tsgo
23
+ expectTypeOf<ExtractUnionAdapter<CustomAdapter, { method: "unknown" }>>().toEqualTypeOf<CustomAdapter>();
24
+ });
25
+
26
+ it("should type requests of a client with a custom adapter", () => {
27
+ const adapter = createCustomAdapter();
28
+ const client = createClient({ url: "https://api.test.com" }).setAdapter(adapter);
29
+ const request = client.createRequest<{ response: { id: number } }>()({ endpoint: "/users/:userId" });
30
+
31
+ expectTypeOf<ExtractAdapterType<typeof request>>().toEqualTypeOf<CustomAdapter>();
32
+ });
33
+ });
34
+ });
@@ -29,6 +29,16 @@ describe("Adapter [ Utils ]", () => {
29
29
  });
30
30
 
31
31
  describe("When using getAdapterPayload util", () => {
32
+ it("should not create a body without a payload", () => {
33
+ expect(getAdapterPayload(undefined)).toBeUndefined();
34
+ });
35
+ it.each([
36
+ [0, "0"],
37
+ [false, "false"],
38
+ ["", '""'],
39
+ ])("should create a body for the %j payload", (payload, body) => {
40
+ expect(getAdapterPayload(payload)).toBe(body);
41
+ });
32
42
  it("should allow to stringify payload", async () => {
33
43
  const data = { data: [] };
34
44
 
@@ -6,6 +6,24 @@ import type { ResponseDetailsType } from "managers";
6
6
 
7
7
  describe("Cache [ Utils ]", () => {
8
8
  describe("when getCacheData function is used", () => {
9
+ it.each([[0], [false], [""]])("should keep the %j response data", (data) => {
10
+ const previousResponse = {
11
+ data: "previous",
12
+ error: null,
13
+ status: 200,
14
+ success: true,
15
+ extra: xhrExtra,
16
+ } as unknown as Parameters<typeof getCacheData>[0];
17
+ const response = {
18
+ data,
19
+ error: null,
20
+ status: 200,
21
+ success: true,
22
+ extra: xhrExtra,
23
+ } as unknown as Parameters<typeof getCacheData>[1];
24
+
25
+ expect(getCacheData(previousResponse, response).data).toBe(data);
26
+ });
9
27
  it("should not override cache on retry or refresh error response", async () => {
10
28
  const previousResponse = {
11
29
  data: {},
@@ -43,4 +43,32 @@ describe("Request [ Cloning ]", () => {
43
43
  expect(clone.cacheKey).toBe("test");
44
44
  });
45
45
  });
46
+
47
+ describe("When cloning request with a falsy payload", () => {
48
+ const createPayloadRequest = () =>
49
+ client.createRequest<{ payload: number | boolean | string }>()({ endpoint: "/users", method: "POST" });
50
+
51
+ it.each([[0], [false], [""]])("should set the %j payload", (payload) => {
52
+ expect(createPayloadRequest().setPayload(payload).payload).toBe(payload);
53
+ });
54
+
55
+ it.each([[0], [false], [""]])("should keep the %j payload in the next clones", (payload) => {
56
+ const clone = createPayloadRequest().setPayload(payload).setHeaders({ "x-test": "1" }).clone();
57
+
58
+ expect(clone.payload).toBe(payload);
59
+ });
60
+
61
+ it.each([[0], [false], [""]])("should replace the previous payload with %j", (payload) => {
62
+ expect(createPayloadRequest().setPayload("previous").setPayload(payload).payload).toBe(payload);
63
+ });
64
+
65
+ it.each([[0], [false], [""]])("should pass the %j payload through the clone options", (payload) => {
66
+ expect(createPayloadRequest().clone({ payload }).payload).toBe(payload);
67
+ });
68
+
69
+ it("should keep the payload when the clone does not change it", () => {
70
+ expect(createPayloadRequest().setPayload("previous").clone({}).payload).toBe("previous");
71
+ expect(createPayloadRequest().setPayload("previous").clone({ payload: undefined }).payload).toBe("previous");
72
+ });
73
+ });
46
74
  });
@@ -170,11 +170,12 @@ describe("Request [ Sending ]", () => {
170
170
  });
171
171
  it("should return error once request got removed", async () => {
172
172
  mockRequest(request, { delay: 10, status: 400 });
173
- const requestExecution = request.send({});
174
- await sleep(2);
175
-
176
- const runningRequests = client.fetchDispatcher.getAllRunningRequests();
177
- client.fetchDispatcher.delete(request.queryKey, runningRequests[0].requestId, request.abortKey);
173
+ // Removed once it is surely running - a fixed sleep could fire before the request started
174
+ const requestExecution = request.send({
175
+ onRequestStart: ({ requestId }) => {
176
+ client.fetchDispatcher.delete(request.queryKey, requestId, request.abortKey);
177
+ },
178
+ });
178
179
 
179
180
  const {
180
181
  responseTimestamp: _responseTimestamp,
@@ -192,11 +193,13 @@ describe("Request [ Sending ]", () => {
192
193
  it("should call remove error", async () => {
193
194
  const spy = vi.fn();
194
195
  mockRequest(request, { delay: 10, status: 400 });
195
- const requestExecution = request.send({ onRemove: spy });
196
- await sleep(2);
197
-
198
- const runningRequests = client.fetchDispatcher.getAllRunningRequests();
199
- client.fetchDispatcher.delete(request.queryKey, runningRequests[0].requestId, request.abortKey);
196
+ // Removed once it is surely running - a fixed sleep could fire before the request started
197
+ const requestExecution = request.send({
198
+ onRemove: spy,
199
+ onRequestStart: ({ requestId }) => {
200
+ client.fetchDispatcher.delete(request.queryKey, requestId, request.abortKey);
201
+ },
202
+ });
200
203
 
201
204
  await requestExecution;
202
205
  expect(spy).toHaveBeenCalledTimes(1);
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Checked in a program of its own by `pnpm typecheck`. Inside the whole test program, types resolved earlier by other
3
+ * files hide how deep these instantiations go - a client with a custom adapter used to exceed the instantiation depth
4
+ * of tsgo in `createRequest()()` only when checked on its own.
5
+ */
6
+ import { Adapter } from "adapter";
7
+ import { createClient } from "client";
8
+ import { HttpMethods } from "constants/http.constants";
9
+
10
+ const adapter = new Adapter({
11
+ name: "custom-adapter",
12
+ defaultMethod: HttpMethods.GET,
13
+ defaultExtra: {},
14
+ systemErrorStatus: 0,
15
+ systemErrorExtra: {},
16
+ });
17
+
18
+ const client = createClient({ url: "https://api.test.com" }).setAdapter(adapter);
19
+
20
+ export const request = client.createRequest()({ endpoint: "test" });
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "include": ["custom-adapter.ts", "../../../../../global.d.ts"]
4
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export * from './optimistic';
2
1
  export * from './plugin';
3
2
  export * from './client';
4
3
  export * from './cache';
@@ -7,6 +6,7 @@ export * from './sdk';
7
6
  export * from './http-adapter';
8
7
  export * from './request';
9
8
  export * from './infinite';
9
+ export * from './optimistic';
10
10
  export * from './constants/time.constants';
11
11
  export * from './constants/http.constants';
12
12
  export * from './managers';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}