Skip to content

Commit da8ca36

Browse files
committed
refactor: 💡 removed useFetch restrictions
1 parent 1c3450a commit da8ca36

File tree

6 files changed

+74
-76
lines changed

6 files changed

+74
-76
lines changed

‎examples/next-app-router/app/ssr/client-component.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React, { useState } from "react";
44
import { ExtractAdapterResolvedType, RequestInstance } from "@hyper-fetch/core";
5-
import { UseFetchRequest, useFetch } from "@hyper-fetch/react";
5+
import { useFetch } from "@hyper-fetch/react";
66
import { Stack, Button } from "@mui/material";
77
import IconButton from "@mui/material/IconButton";
88
import RefreshIcon from "@mui/icons-material/Refresh";
@@ -14,7 +14,7 @@ import { Request } from "../../components/request";
1414
import { getUser } from "../../api";
1515

1616
export const ClientComponents: React.FC<{
17-
fallback: Partial<ExtractAdapterResolvedType<UseFetchRequest<RequestInstance>>>;
17+
fallback: Partial<ExtractAdapterResolvedType<RequestInstance>>;
1818
}> = (props) => {
1919
const [dep, setDep] = useState(+new Date());
2020
const [disabled, setDisabled] = useState(true);

‎packages/cli/__tests__/commands/generate/generate.spec.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import * as fs from "fs-extra";
12
import { jest } from "@jest/globals";
3+
import { input, select, confirm } from "@inquirer/prompts";
24

35
import { generate } from "commands/generate";
46
import { handleError } from "utils/handle-error";
57
import { preFlightGenerate } from "preflights/preflight-generate";
68
import { OpenapiRequestGenerator } from "codegen/openapi/generator";
7-
import { input, select, confirm } from "@inquirer/prompts";
8-
import * as fs from "fs-extra";
99

1010
// Mocks
1111
jest.mock("utils/handle-error", () => ({ handleError: jest.fn() }));
@@ -23,7 +23,6 @@ jest.mock("codegen/openapi/generator", () => {
2323
class FakeGenerator {
2424
static getSchemaFromUrl = jest.fn(async () => ({ openapi: "3.0.0", paths: {} }));
2525
generateFile = jest.fn(async () => undefined);
26-
constructor() {}
2726
}
2827
return { OpenapiRequestGenerator: FakeGenerator };
2928
});

‎packages/react/__tests__/features/provider/provider.hydration.spec.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Fragment } from "react";
44
import { HttpAdapterExtraType, Client } from "@hyper-fetch/core";
55
import { render, waitFor } from "@testing-library/react";
66

7-
import { UseFetchRequest, UseFetchReturnType, useFetch } from "hooks/use-fetch";
7+
import { UseFetchReturnType, useFetch } from "hooks/use-fetch";
88
import { UseSubmitReturnType, useSubmit } from "hooks/use-submit";
99
import { Provider } from "provider";
1010

@@ -118,7 +118,7 @@ describe("Provider [ Hydration ]", () => {
118118
describe("given app is rendered on the Client", () => {
119119
describe("when hydration data is passed down to the Provider", () => {
120120
it("should hydrate the data for useFetch hook", async () => {
121-
let useFetchResults: UseFetchReturnType<UseFetchRequest<typeof request>> | undefined;
121+
let useFetchResults: UseFetchReturnType<typeof request> | undefined;
122122
hydrate();
123123
const Children = () => {
124124
useFetchResults = useFetch(request);
Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,59 @@
1-
import { expectType } from "@hyper-fetch/testing";
2-
import { Client, ExtractHasPayloadType, ExtractHasParamsType, ExtractHasQueryParamsType } from "@hyper-fetch/core";
1+
import { useFetch } from "hooks/use-fetch";
32

4-
import { UseFetchRequest } from "hooks/use-fetch";
5-
6-
const client = new Client({
7-
url: "http://localhost:3000",
8-
});
9-
10-
describe("when using useFetch request", () => {
11-
it("should require params", () => {
12-
const getUser = client.createRequest<{ response: { id: string } }>()({
13-
method: "GET",
14-
endpoint: "/users/:id",
15-
});
16-
const getUserWithParams = getUser.setParams({ id: 1 });
17-
expectType<ExtractHasParamsType<UseFetchRequest<typeof getUser>>>().assert(false);
18-
expectType<ExtractHasParamsType<UseFetchRequest<typeof getUserWithParams>>>().assert(true);
19-
});
20-
it("should require payload", () => {
21-
const postUser = client.createRequest<{ response: { id: string }; payload: { name: string } }>()({
22-
method: "POST",
23-
endpoint: "/users",
24-
});
25-
const postUserWithPayload = postUser.setPayload({ name: "test" });
26-
27-
expectType<ExtractHasPayloadType<UseFetchRequest<typeof postUser>>>().assert(false);
28-
expectType<ExtractHasPayloadType<UseFetchRequest<typeof postUserWithPayload>>>().assert(true);
29-
});
30-
it("should require params and payload", () => {
31-
const patchUser = client.createRequest<{
32-
response: { id: string };
33-
payload: { name: string };
34-
queryParams: { name: string };
35-
}>()({
36-
method: "PATCH",
37-
endpoint: "/users/:id",
38-
});
39-
const patchUserWithParams = patchUser.setParams({ id: 1 });
40-
const patchUserWithPayload = patchUser.setPayload({ name: "test" });
41-
const patchUserWithQueryParams = patchUser.setQueryParams({ name: "test" });
42-
43-
expectType<false>().assert<ExtractHasParamsType<UseFetchRequest<typeof patchUser>>>(false);
44-
expectType<false>().assert<ExtractHasPayloadType<UseFetchRequest<typeof patchUser>>>(false);
45-
expectType<false>().assert<ExtractHasQueryParamsType<UseFetchRequest<typeof patchUser>>>(false);
46-
47-
expectType<true>().assert<ExtractHasParamsType<UseFetchRequest<typeof patchUserWithParams>>>(true);
48-
expectType<true>().assert<ExtractHasPayloadType<UseFetchRequest<typeof patchUserWithPayload>>>(true);
49-
expectType<true>().assert<ExtractHasQueryParamsType<UseFetchRequest<typeof patchUserWithQueryParams>>>(true);
3+
describe("useFetch", () => {
4+
it("dummy test", () => {
5+
expect(useFetch).toBeDefined();
506
});
517
});
8+
9+
// import { expectType } from "@hyper-fetch/testing";
10+
// import { Client, ExtractHasPayloadType, ExtractHasParamsType, ExtractHasQueryParamsType } from "@hyper-fetch/core";
11+
12+
// import { UseFetchRequest } from "hooks/use-fetch";
13+
14+
// const client = new Client({
15+
// url: "http://localhost:3000",
16+
// });
17+
18+
// describe("when using useFetch request", () => {
19+
// it("should require params", () => {
20+
// const getUser = client.createRequest<{ response: { id: string } }>()({
21+
// method: "GET",
22+
// endpoint: "/users/:id",
23+
// });
24+
// const getUserWithParams = getUser.setParams({ id: 1 });
25+
// expectType<ExtractHasParamsType<UseFetchRequest<typeof getUser>>>().assert(false);
26+
// expectType<ExtractHasParamsType<UseFetchRequest<typeof getUserWithParams>>>().assert(true);
27+
// });
28+
// it("should require payload", () => {
29+
// const postUser = client.createRequest<{ response: { id: string }; payload: { name: string } }>()({
30+
// method: "POST",
31+
// endpoint: "/users",
32+
// });
33+
// const postUserWithPayload = postUser.setPayload({ name: "test" });
34+
35+
// expectType<ExtractHasPayloadType<UseFetchRequest<typeof postUser>>>().assert(false);
36+
// expectType<ExtractHasPayloadType<UseFetchRequest<typeof postUserWithPayload>>>().assert(true);
37+
// });
38+
// it("should require params and payload", () => {
39+
// const patchUser = client.createRequest<{
40+
// response: { id: string };
41+
// payload: { name: string };
42+
// queryParams: { name: string };
43+
// }>()({
44+
// method: "PATCH",
45+
// endpoint: "/users/:id",
46+
// });
47+
// const patchUserWithParams = patchUser.setParams({ id: 1 });
48+
// const patchUserWithPayload = patchUser.setPayload({ name: "test" });
49+
// const patchUserWithQueryParams = patchUser.setQueryParams({ name: "test" });
50+
51+
// expectType<false>().assert<ExtractHasParamsType<UseFetchRequest<typeof patchUser>>>(false);
52+
// expectType<false>().assert<ExtractHasPayloadType<UseFetchRequest<typeof patchUser>>>(false);
53+
// expectType<false>().assert<ExtractHasQueryParamsType<UseFetchRequest<typeof patchUser>>>(false);
54+
55+
// expectType<true>().assert<ExtractHasParamsType<UseFetchRequest<typeof patchUserWithParams>>>(true);
56+
// expectType<true>().assert<ExtractHasPayloadType<UseFetchRequest<typeof patchUserWithPayload>>>(true);
57+
// expectType<true>().assert<ExtractHasQueryParamsType<UseFetchRequest<typeof patchUserWithQueryParams>>>(true);
58+
// });
59+
// });

‎packages/react/src/hooks/use-fetch/use-fetch.hooks.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { useRef } from "react";
1010

1111
import { useRequestEvents, useTrackedState } from "helpers";
12-
import { UseFetchOptionsType, useFetchDefaultOptions, UseFetchReturnType, UseFetchRequest } from "hooks/use-fetch";
12+
import { UseFetchOptionsType, useFetchDefaultOptions, UseFetchReturnType } from "hooks/use-fetch";
1313
import { useProvider } from "provider";
1414
import { getBounceData } from "utils";
1515

@@ -20,7 +20,7 @@ import { getBounceData } from "utils";
2020
* @returns
2121
*/
2222
export const useFetch = <R extends RequestInstance>(
23-
request: UseFetchRequest<R>,
23+
request: R,
2424
options?: UseFetchOptionsType<R>,
2525
): UseFetchReturnType<R> => {
2626
// Build the configuration options

‎packages/react/src/hooks/use-fetch/use-fetch.types.ts‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
import {
2-
RequestInstance,
3-
NullableType,
4-
ExtractAdapterResolvedType,
5-
ExtendRequest,
6-
EmptyTypes,
7-
ExtractPayloadType,
8-
ExtractParamsType,
9-
ExtractQueryParamsType,
10-
} from "@hyper-fetch/core";
1+
import { RequestInstance, NullableType, ExtractAdapterResolvedType } from "@hyper-fetch/core";
112

123
import { UseRequestEventsActionsType, UseTrackedStateActions, UseTrackedStateType } from "helpers";
134
import { isEqual } from "utils";
145

15-
export type UseFetchCastRequest<R extends RequestInstance> = ExtendRequest<
16-
R,
17-
{
18-
hasData: ExtractPayloadType<R> extends EmptyTypes ? boolean : true;
19-
hasParams: ExtractParamsType<R> extends EmptyTypes ? boolean : true;
20-
hasQuery: ExtractQueryParamsType<R> extends EmptyTypes ? boolean : true;
21-
}
22-
>;
6+
// export type UseFetchCastRequest<R extends RequestInstance> = ExtendRequest<
7+
// R,
8+
// {
9+
// hasData: ExtractPayloadType<R> extends EmptyTypes ? boolean : true;
10+
// hasParams: ExtractParamsType<R> extends EmptyTypes ? boolean : true;
11+
// hasQuery: ExtractQueryParamsType<R> extends EmptyTypes ? boolean : true;
12+
// }
13+
// >;
2314

24-
export type UseFetchRequest<R extends RequestInstance> = R extends UseFetchCastRequest<R> ? R : UseFetchCastRequest<R>;
15+
// export type UseFetchRequest<R extends RequestInstance> = R extends UseFetchCastRequest<R> ? R : UseFetchCastRequest<R>;
2516

2617
export type UseFetchOptionsType<R extends RequestInstance> = {
2718
/**

0 commit comments

Comments
 (0)