Skip to content

Commit e6d6197

Browse files
committed
feat(useResource): support reactive/original value parameters
1 parent c926106 commit e6d6197

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ const [reqState, request] = useResource(
222222
[params],
223223
);
224224

225+
// reactive parameter
226+
const params = reactive({ id: userId });
227+
const [reqState, request] = useResource(
228+
({ id }) => ({
229+
url: `/user/${id}`,
230+
method: "GET",
231+
}),
232+
[params],
233+
);
234+
225235
// options: onCompleted, onError
226236
const [reqState] = useResource(
227237
() => ({

src/useResource.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
} from "./request";
1414
import { useRequest } from "./useRequest";
1515
import type { FullRefArrayItem } from "./utils";
16-
import { useReducer, unrefs } from "./utils";
16+
import { hasReactive, useReducer, unrefs } from "./utils";
1717

1818
const REQUEST_CLEAR_MESSAGE =
1919
"A new request has been made before completing the last one";
@@ -82,12 +82,14 @@ function getNextState<T extends Request>(
8282
};
8383
}
8484

85+
export type RequestDepsParameters<T extends Request> =
86+
| Parameters<T>
87+
| FullRefArrayItem<Parameters<T>>
88+
| ComputedRef<Parameters<T>>;
89+
8590
export function useResource<T extends Request>(
8691
fn: T,
87-
requestParams?:
88-
| FullRefArrayItem<Parameters<T>>
89-
| ComputedRef<Parameters<T>>
90-
| false,
92+
requestParams?: RequestDepsParameters<T> | false,
9193
options?: UseResourceOptions<T>,
9294
): UseResourceResult<T> {
9395
const [createRequest, { clear }] = useRequest(fn, {
@@ -155,6 +157,7 @@ export function useResource<T extends Request>(
155157
},
156158
{
157159
immediate: true,
160+
deep: hasReactive(requestParams),
158161
},
159162
);
160163

tests/useResource.test.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "vitest";
22
import { mount, flushPromises } from "@vue/test-utils";
3-
import { computed, defineComponent, h, ref, unref } from "vue";
3+
import { computed, defineComponent, h, ref, unref, reactive } from "vue";
44

55
import { useResource } from "../src";
66
import { getAPIFuncs, MOCK_DATA_USER_LIST } from "./setup/mock-request";
@@ -145,7 +145,7 @@ describe("useResource", () => {
145145
expect(wrapper.get('[data-t-id="res.isLoading"]').text()).toBe("false");
146146
});
147147

148-
test("options: ", async () => {
148+
test("options: defaultState", async () => {
149149
const Component = defineComponent({
150150
setup() {
151151
const [res, request] = useResource(getAPIFuncs(true).user.get, [], {
@@ -182,4 +182,55 @@ describe("useResource", () => {
182182
);
183183
expect(wrapper.get('[data-t-id="res.isLoading"]').text()).toBe("false");
184184
});
185+
186+
test("reactive parameter", async () => {
187+
const Component = defineComponent({
188+
setup() {
189+
const params = reactive({ id: "1" });
190+
const [res] = useResource(getAPIFuncs(true).user.get, [params], {
191+
filter: (p) => Boolean(p?.id),
192+
});
193+
194+
expect(unref(res).isLoading).toBeTruthy();
195+
expect(unref(res).data).toBeUndefined();
196+
expect(unref(res).response).toBeUndefined();
197+
expect(unref(res).error).toBeUndefined();
198+
199+
const handleChangeId = () => {
200+
params.id = "2";
201+
};
202+
203+
return () =>
204+
h("div", [
205+
h("button", {
206+
"data-t-id": "change_id",
207+
onClick: handleChangeId,
208+
}),
209+
h(
210+
"div",
211+
{ "data-t-id": "res.data" },
212+
JSON.stringify(res.value.data),
213+
),
214+
h("div", { "data-t-id": "res.isLoading" }, res.value.isLoading),
215+
h("div", { "data-t-id": "params.id" }, params.id),
216+
]);
217+
},
218+
});
219+
220+
const wrapper = mount(Component);
221+
await flushPromises();
222+
expect(wrapper.get('[data-t-id="res.data"]').text()).toBe(
223+
JSON.stringify(MOCK_DATA_USER_LIST.find((i) => i.id === "1")),
224+
);
225+
expect(wrapper.get('[data-t-id="res.isLoading"]').text()).toBe("false");
226+
expect(wrapper.get('[data-t-id="params.id"]').text()).toBe("1");
227+
228+
wrapper.get('[data-t-id="change_id"]').trigger("click");
229+
await flushPromises();
230+
expect(wrapper.get('[data-t-id="res.data"]').text()).toBe(
231+
JSON.stringify(MOCK_DATA_USER_LIST.find((i) => i.id === "2")),
232+
);
233+
expect(wrapper.get('[data-t-id="res.isLoading"]').text()).toBe("false");
234+
expect(wrapper.get('[data-t-id="params.id"]').text()).toBe("2");
235+
});
185236
});

0 commit comments

Comments
 (0)