Skip to content

Commit fe76855

Browse files
authored
fix(shared): add create, edit and delete methods to endpoints rest client (#1561)
1 parent e5c152a commit fe76855

File tree

15 files changed

+226
-175
lines changed

15 files changed

+226
-175
lines changed

packages/@liexp/shared/src/providers/EndpointQueriesProvider/EndpointQueriesProvider.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fc } from "@liexp/test";
22
import { parseISO } from "date-fns";
33
import { afterEach, describe, expect, it, vi } from "vitest";
44
import { type MockProxy } from "vitest-mock-extended";
5-
import { TestEndpoints, overrides } from "../../../test/TestEndpoints";
5+
import { TestEndpoints, overrides } from "../../../test/TestEndpoints.js";
66
import { ActorArb } from "../../tests/index.js";
77
import { fromEndpoints } from "../EndpointsRESTClient/EndpointsRESTClient.js";
88
import { type APIRESTClient } from "../api-rest.provider.js";
@@ -29,6 +29,9 @@ describe("EndpointQueriesProvider", () => {
2929

3030
it("should be defined", () => {
3131
expect(Q).toBeTruthy();
32+
expect(Q.Actor.get.getKey).toBeInstanceOf(Function);
33+
expect(Q.Actor.get.fetch).toBeInstanceOf(Function);
34+
expect(Q.Actor.get.useQuery).toBeInstanceOf(Function);
3235
});
3336

3437
it("should have Actor get", async () => {
@@ -44,14 +47,16 @@ describe("EndpointQueriesProvider", () => {
4447
createdAt: a.createdAt.toISOString(),
4548
updatedAt: a.updatedAt.toISOString(),
4649
}));
50+
4751
apiProviderMock.get.mockResolvedValue({ data: actorData });
4852

49-
expect(Q.Actor.get).toBeTruthy();
5053
const params = { id: "1" };
5154
const actorKey = Q.Actor.get.getKey(params);
52-
expect(actorKey).toEqual(["Actor", params, undefined, false]);
55+
5356
const actor = await Q.Actor.get.fetch(params, undefined);
5457

58+
expect(actorKey).toEqual(["Actor", params, undefined, false]);
59+
5560
expect(apiProviderMock.get).toHaveBeenCalledWith("/actors/1", {
5661
id: "1",
5762
});
@@ -148,7 +153,7 @@ describe("EndpointQueriesProvider", () => {
148153

149154
expect(apiProviderMock.request).toHaveBeenCalledWith({
150155
data: undefined,
151-
params: {},
156+
params: undefined,
152157
method: "GET",
153158
url: "/actors/1/siblings",
154159
responseType: "json",

packages/@liexp/shared/src/providers/EndpointQueriesProvider/QueryProvider.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import {
1919
type GetListFn,
2020
type GetListFnParamsE,
2121
type GetEndpointQueryType,
22-
type Query,
23-
} from "../EndpointsRESTClient/EndpointsRESTClient.js";
22+
type EndpointREST,
23+
} from "../EndpointsRESTClient/types.js";
2424
import {
2525
type GetQueryOverride,
2626
type ResourceEndpointsQueriesOverride,
@@ -149,10 +149,13 @@ export const toQueries = <
149149
ES extends EndpointsMapType,
150150
G extends MinimalEndpoint,
151151
L extends MinimalEndpoint,
152+
C extends MinimalEndpoint,
153+
E extends MinimalEndpoint,
154+
D extends MinimalEndpoint,
152155
CC extends Record<string, MinimalEndpointInstance>,
153156
>(
154157
key: string,
155-
e: Query<G, L, CC>,
158+
e: EndpointREST<G, L, C, E, D, CC>,
156159
override?: ResourceEndpointsQueriesOverride<ES, G, L, CC>,
157160
): ResourceQueries<G, L, CC> => {
158161
return {
@@ -163,7 +166,7 @@ export const toQueries = <
163166
fp.Rec.mapWithIndex((index, ee) => {
164167
const getKey = getDefaultKey(`${key}-${index}`);
165168
const fetch = fetchQuery<any, any, any>((p, q) => {
166-
return ee({ Params: p, Query: q } as any);
169+
return ee({ Params: p, Query: q } as any, q);
167170
});
168171

169172
return {

packages/@liexp/shared/src/providers/EndpointQueriesProvider/QueryProviderOverrides.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
type GetFnParams,
1616
type GetEndpointQueryType,
1717
type GetListFnParamsE,
18-
} from "../EndpointsRESTClient/EndpointsRESTClient.js";
18+
} from "../EndpointsRESTClient/types.js";
1919
import { fetchQuery, getDefaultKey } from "./QueryProvider.js";
2020
import { type GetKeyFn, type ResourceQueries } from "./types.js";
2121

packages/@liexp/shared/src/providers/EndpointQueriesProvider/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fp, pipe } from "@liexp/core/lib/fp/index.js";
22
import { type EndpointsMapType } from "../../endpoints/Endpoints.js";
33
import { type Endpoints } from "../../endpoints/index.js";
4-
import { type EndpointsRESTClient } from "../EndpointsRESTClient/EndpointsRESTClient.js";
4+
import { type EndpointsRESTClient } from "../EndpointsRESTClient/types.js";
55
import { API } from "../api/api.provider.js";
66
import { toQueries } from "./QueryProvider.js";
77
import {

packages/@liexp/shared/src/providers/EndpointQueriesProvider/overrides.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { fp, pipe } from "@liexp/core/lib/fp/index.js";
22
import { type MinimalEndpointInstance } from "ts-endpoint";
33
import { type Endpoints } from "../../endpoints/index.js";
44
import {
5-
type GetDataOutputEI,
65
type GetListFnParamsE,
6+
type EndpointOutput,
77
type GetFnParams,
8-
} from "../EndpointsRESTClient/EndpointsRESTClient.js";
8+
} from "../EndpointsRESTClient/types.js";
99
import {
1010
type QueryProviderOverrides,
1111
type CustomQueryOverride,
@@ -17,7 +17,7 @@ const GetHierarchyNetwork: CustomQueryOverride<
1717
Endpoints,
1818
GetFnParams<typeof Endpoints.Networks.Get>,
1919
GetListFnParamsE<typeof Endpoints.Networks.Get>,
20-
GetDataOutputEI<typeof Endpoints.Networks.Get>
20+
EndpointOutput<typeof Endpoints.Networks.Get>
2121
> = (Q) => (p) => Q.Networks.get({ ...p, type: "hierarchy" });
2222

2323
const NetworksOverride: ResourceEndpointsQueriesOverride<
@@ -37,7 +37,7 @@ const GetPageContentByPath: CustomQueryOverride<
3737
Endpoints,
3838
string,
3939
undefined,
40-
GetDataOutputEI<typeof Endpoints.Page.Get>
40+
EndpointOutput<typeof Endpoints.Page.Get>
4141
> = (Q) => (path) => {
4242
return pipe(
4343
Q.Page.getList({
@@ -53,7 +53,7 @@ const GetByPath: CustomQueryOverride<
5353
Endpoints,
5454
string,
5555
undefined,
56-
GetDataOutputEI<typeof Endpoints.Story.Get>
56+
EndpointOutput<typeof Endpoints.Story.Get>
5757
> = (Q) => (p) =>
5858
pipe(
5959
Q.Story.getList({ ...defaultUseQueryListParams, filter: { path: p } }),

packages/@liexp/shared/src/providers/EndpointQueriesProvider/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
type EndpointOutput,
1313
type GetFnParams,
1414
type GetEndpointQueryType,
15-
type Query,
15+
type EndpointREST,
1616
type EndpointDataOutput,
17-
} from "../EndpointsRESTClient/EndpointsRESTClient.js";
17+
} from "../EndpointsRESTClient/types.js";
1818

1919
export type QueryFnKey<P, Q = undefined> = [string, P, Q | undefined, boolean];
2020
export type GetKeyFn<P, Q = undefined> = (
@@ -64,7 +64,7 @@ export interface ResourceQueries<G, L, CC> {
6464
}
6565

6666
export type ResourceQueryImpl<Q> =
67-
Q extends Query<infer G, infer L, infer CC>
67+
Q extends EndpointREST<infer G, infer L, unknown, unknown, unknown, infer CC>
6868
? ResourceQueries<G, L, CC>
6969
: never;
7070

0 commit comments

Comments
 (0)