@uktrade/react-component-library 0.22.11 → 0.22.12
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiCreateHooks.d.ts","sourceRoot":"","sources":["../../../src/components/ApiQuery/ApiCreateHooks.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAElE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"ApiCreateHooks.d.ts","sourceRoot":"","sources":["../../../src/components/ApiQuery/ApiCreateHooks.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAElE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAqBpD,wBAAgB,cAAc,CAAC,KAAK,SAAS,EAAE,EAC7C,MAAM,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC;UAQzC,CAAC,SAAS,eAAe,CAAC,KAAK,EAAE,KAAK,CAAC,QACrC,CAAC,UACC,GAAG;;;;;;;;;;WAQN,CAAC,SAAS,eAAe,CAAC,KAAK,EAAE,MAAM,CAAC,QACvC,CAAC,UACC,GAAG;;;;;;;;;;mBAUE,CAAC,SAAS,eAAe,CAAC,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC;;;;;;;;;;kBAKlD,CAAC,SAAS,eAAe,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC;;;;;;;;;;oBAK9C,CAAC,SAAS,eAAe,CAAC,KAAK,EAAE,OAAO,CAAC,QAAQ,CAAC;;;;;;;;;;qBAKjD,CAAC,SAAS,eAAe,CAAC,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC;;;;;;;;;;EAKvE"}
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { createApiClient } from "./ApiCreateClient";
|
|
3
3
|
import { useApi, useApiMutation } from "./ApiProvider";
|
|
4
|
+
function unwrap(res) {
|
|
5
|
+
if (res.error) {
|
|
6
|
+
throw Object.assign(res.error, {
|
|
7
|
+
status: res.response.status,
|
|
8
|
+
response: res.response,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
return res.data;
|
|
12
|
+
}
|
|
4
13
|
// API hook factory that converts a typed OpenAPI client into React hooks (`useApi` and `useApiMutation`) that the UI can use.
|
|
5
14
|
// Provides a simple interface for making API calls and handling loading and error states in your React components.
|
|
6
15
|
export function createApiHooks(client) {
|
|
@@ -10,53 +19,23 @@ export function createApiHooks(client) {
|
|
|
10
19
|
// Using useQuery
|
|
11
20
|
return {
|
|
12
21
|
get: (path, input) => {
|
|
13
|
-
return useApi(["get", path, JSON.stringify(input ?? {})], async () =>
|
|
14
|
-
const res = await client.GET(path, input);
|
|
15
|
-
if (res.error)
|
|
16
|
-
throw res.error;
|
|
17
|
-
return res.data;
|
|
18
|
-
});
|
|
22
|
+
return useApi(["get", path, JSON.stringify(input ?? {})], async () => unwrap(await client.GET(path, input)));
|
|
19
23
|
},
|
|
20
24
|
post: (path, input) => {
|
|
21
|
-
return useApi(["post", path, JSON.stringify(input ?? {})], async () =>
|
|
22
|
-
const res = await client.POST(path, input);
|
|
23
|
-
if (res.error)
|
|
24
|
-
throw res.error;
|
|
25
|
-
return res.data;
|
|
26
|
-
});
|
|
25
|
+
return useApi(["post", path, JSON.stringify(input ?? {})], async () => unwrap(await client.POST(path, input)));
|
|
27
26
|
},
|
|
28
27
|
// Using useMutation
|
|
29
28
|
postMutation: (path) => {
|
|
30
|
-
return useApiMutation(async (input) =>
|
|
31
|
-
const res = await client.POST(path, input);
|
|
32
|
-
if (res.error)
|
|
33
|
-
throw res.error;
|
|
34
|
-
return res.data;
|
|
35
|
-
});
|
|
29
|
+
return useApiMutation(async (input) => unwrap(await client.POST(path, input)));
|
|
36
30
|
},
|
|
37
31
|
putMutation: (path) => {
|
|
38
|
-
return useApiMutation(async (input) =>
|
|
39
|
-
const res = await client.PUT(path, input);
|
|
40
|
-
if (res.error)
|
|
41
|
-
throw res.error;
|
|
42
|
-
return res.data;
|
|
43
|
-
});
|
|
32
|
+
return useApiMutation(async (input) => unwrap(await client.PUT(path, input)));
|
|
44
33
|
},
|
|
45
34
|
patchMutation: (path) => {
|
|
46
|
-
return useApiMutation(async (input) =>
|
|
47
|
-
const res = await client.PATCH(path, input);
|
|
48
|
-
if (res.error)
|
|
49
|
-
throw res.error;
|
|
50
|
-
return res.data;
|
|
51
|
-
});
|
|
35
|
+
return useApiMutation(async (input) => unwrap(await client.PATCH(path, input)));
|
|
52
36
|
},
|
|
53
37
|
deleteMutation: (path) => {
|
|
54
|
-
return useApiMutation(async (input) =>
|
|
55
|
-
const res = await client.DELETE(path, input);
|
|
56
|
-
if (res.error)
|
|
57
|
-
throw res.error;
|
|
58
|
-
return res.data;
|
|
59
|
-
});
|
|
38
|
+
return useApiMutation(async (input) => unwrap(await client.DELETE(path, input)));
|
|
60
39
|
},
|
|
61
40
|
};
|
|
62
41
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uktrade/react-component-library",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.12",
|
|
4
4
|
"description": "A collection of reusable React components following GOV.UK design patterns.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -55,13 +55,13 @@
|
|
|
55
55
|
"@types/react": "^19.2.9",
|
|
56
56
|
"@types/react-dom": "^19.2.3",
|
|
57
57
|
"@typescript-eslint/eslint-plugin": "^8.59.0",
|
|
58
|
-
"@typescript-eslint/parser": "^8.
|
|
58
|
+
"@typescript-eslint/parser": "^8.62.0",
|
|
59
59
|
"@vitest/coverage-v8": "^4.0.18",
|
|
60
60
|
"@vitest/ui": "^4.0.18",
|
|
61
61
|
"eslint": "^9.39.2",
|
|
62
62
|
"eslint-plugin-react": "^7.37.5",
|
|
63
63
|
"globals": "^17.5.0",
|
|
64
|
-
"jsdom": "^
|
|
64
|
+
"jsdom": "^29.1.1",
|
|
65
65
|
"openapi-typescript": "^7.13.0",
|
|
66
66
|
"typescript": "^5.9.3",
|
|
67
67
|
"typescript-eslint": "^8.59.0",
|
|
@@ -5,6 +5,22 @@ import type { PathsWithMethod } from "openapi-typescript-helpers";
|
|
|
5
5
|
import { createApiClient } from "./ApiCreateClient";
|
|
6
6
|
import { useApi, useApiMutation } from "./ApiProvider";
|
|
7
7
|
|
|
8
|
+
|
|
9
|
+
function unwrap<T>(res: {
|
|
10
|
+
data?: T;
|
|
11
|
+
error?: any;
|
|
12
|
+
response: Response;
|
|
13
|
+
}): T {
|
|
14
|
+
if (res.error) {
|
|
15
|
+
throw Object.assign(res.error, {
|
|
16
|
+
status: res.response.status,
|
|
17
|
+
response: res.response,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return res.data as T;
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
// API hook factory that converts a typed OpenAPI client into React hooks (`useApi` and `useApiMutation`) that the UI can use.
|
|
9
25
|
// Provides a simple interface for making API calls and handling loading and error states in your React components.
|
|
10
26
|
export function createApiHooks<Paths extends {}>(
|
|
@@ -22,11 +38,7 @@ export function createApiHooks<Paths extends {}>(
|
|
|
22
38
|
) => {
|
|
23
39
|
return useApi(
|
|
24
40
|
["get", path, JSON.stringify(input ?? {})],
|
|
25
|
-
async () =>
|
|
26
|
-
const res = await client.GET(path, input);
|
|
27
|
-
if (res.error) throw res.error;
|
|
28
|
-
return res.data;
|
|
29
|
-
}
|
|
41
|
+
async () => unwrap(await client.GET(path, input))
|
|
30
42
|
);
|
|
31
43
|
},
|
|
32
44
|
|
|
@@ -36,45 +48,30 @@ export function createApiHooks<Paths extends {}>(
|
|
|
36
48
|
) => {
|
|
37
49
|
return useApi(
|
|
38
50
|
["post", path, JSON.stringify(input ?? {})],
|
|
39
|
-
async () =>
|
|
40
|
-
|
|
41
|
-
if (res.error) throw res.error;
|
|
42
|
-
return res.data;
|
|
43
|
-
}
|
|
51
|
+
async () =>
|
|
52
|
+
unwrap(await client.POST(path, input))
|
|
44
53
|
);
|
|
45
54
|
},
|
|
46
55
|
|
|
47
56
|
// Using useMutation
|
|
48
57
|
postMutation: <P extends PathsWithMethod<Paths, "post">>(path: P) => {
|
|
49
|
-
return useApiMutation(async (input: any) =>
|
|
50
|
-
|
|
51
|
-
if (res.error) throw res.error;
|
|
52
|
-
return res.data;
|
|
53
|
-
});
|
|
58
|
+
return useApiMutation(async (input: any) =>
|
|
59
|
+
unwrap(await client.POST(path, input)));
|
|
54
60
|
},
|
|
55
61
|
|
|
56
62
|
putMutation: <P extends PathsWithMethod<Paths, "put">>(path: P) => {
|
|
57
|
-
return useApiMutation(async (input: any) =>
|
|
58
|
-
|
|
59
|
-
if (res.error) throw res.error;
|
|
60
|
-
return res.data;
|
|
61
|
-
});
|
|
63
|
+
return useApiMutation(async (input: any) =>
|
|
64
|
+
unwrap(await client.PUT(path, input)));
|
|
62
65
|
},
|
|
63
66
|
|
|
64
67
|
patchMutation: <P extends PathsWithMethod<Paths, "patch">>(path: P) => {
|
|
65
|
-
return useApiMutation(async (input: any)
|
|
66
|
-
|
|
67
|
-
if (res.error) throw res.error;
|
|
68
|
-
return res.data;
|
|
69
|
-
});
|
|
68
|
+
return useApiMutation(async (input: any)=>
|
|
69
|
+
unwrap(await client.PATCH(path, input)));
|
|
70
70
|
},
|
|
71
71
|
|
|
72
72
|
deleteMutation: <P extends PathsWithMethod<Paths, "delete">>(path: P) => {
|
|
73
|
-
return useApiMutation(async (input: any) =>
|
|
74
|
-
|
|
75
|
-
if (res.error) throw res.error;
|
|
76
|
-
return res.data;
|
|
77
|
-
});
|
|
73
|
+
return useApiMutation(async (input: any) =>
|
|
74
|
+
unwrap(await client.DELETE(path, input)));
|
|
78
75
|
},
|
|
79
76
|
};
|
|
80
77
|
}
|