@chill-sharp/react-client 1.1.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.
package/dist/hooks.js ADDED
@@ -0,0 +1,434 @@
1
+ /*
2
+ * ChillSharp is a lightweight .NET library that sits on top of Entity Framework Core
3
+ * and turns an existing data model into a fully working REST API with almost no setup.
4
+ * Copyright (C) 2025 Andrea Piovesan
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Affero General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Affero General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Affero General Public License
17
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+ */
19
+ import { useEffect, useRef, useState } from "react";
20
+ import { CHILL_SHARP_REACT_CLIENT_VERSION } from "./version.js";
21
+ import { useChillSharpClient } from "./context.js";
22
+ export function useSchema(chillType, chillViewCode = "default", cultureName, update = false) {
23
+ const client = useChillSharpClient();
24
+ const [data, setData] = useState(null);
25
+ const [error, setError] = useState(null);
26
+ const [isLoading, setIsLoading] = useState(true);
27
+ const load = async () => {
28
+ setIsLoading(true);
29
+ setError(null);
30
+ try {
31
+ const response = await client.getSchema(chillType, chillViewCode, cultureName, update);
32
+ setData(response);
33
+ return response;
34
+ }
35
+ catch (err) {
36
+ setError(err);
37
+ throw err;
38
+ }
39
+ finally {
40
+ setIsLoading(false);
41
+ }
42
+ };
43
+ useEffect(() => {
44
+ void load();
45
+ }, [client, chillType, chillViewCode, cultureName, update]);
46
+ return {
47
+ data,
48
+ error,
49
+ isLoading,
50
+ reload: load
51
+ };
52
+ }
53
+ export function useSchemaList(cultureName) {
54
+ const client = useChillSharpClient();
55
+ const [data, setData] = useState(null);
56
+ const [error, setError] = useState(null);
57
+ const [isLoading, setIsLoading] = useState(true);
58
+ const load = async () => {
59
+ setIsLoading(true);
60
+ setError(null);
61
+ try {
62
+ const response = await client.getSchemaList(cultureName);
63
+ setData(response);
64
+ return response;
65
+ }
66
+ catch (err) {
67
+ setError(err);
68
+ throw err;
69
+ }
70
+ finally {
71
+ setIsLoading(false);
72
+ }
73
+ };
74
+ useEffect(() => {
75
+ void load();
76
+ }, [client, cultureName]);
77
+ return {
78
+ data,
79
+ error,
80
+ isLoading,
81
+ reload: load
82
+ };
83
+ }
84
+ export function useText(request) {
85
+ const client = useChillSharpClient();
86
+ const [data, setData] = useState(null);
87
+ const [error, setError] = useState(null);
88
+ const [isLoading, setIsLoading] = useState(true);
89
+ const load = async () => {
90
+ setIsLoading(true);
91
+ setError(null);
92
+ try {
93
+ const response = await client.getText(request);
94
+ setData(response);
95
+ return response;
96
+ }
97
+ catch (err) {
98
+ setError(err);
99
+ throw err;
100
+ }
101
+ finally {
102
+ setIsLoading(false);
103
+ }
104
+ };
105
+ useEffect(() => {
106
+ void load();
107
+ }, [client, request]);
108
+ return {
109
+ data,
110
+ error,
111
+ isLoading,
112
+ reload: load
113
+ };
114
+ }
115
+ export function useTexts(requests) {
116
+ const client = useChillSharpClient();
117
+ const [data, setData] = useState(null);
118
+ const [error, setError] = useState(null);
119
+ const [isLoading, setIsLoading] = useState(true);
120
+ const load = async () => {
121
+ setIsLoading(true);
122
+ setError(null);
123
+ try {
124
+ const response = await client.getTexts(requests);
125
+ setData(response);
126
+ return response;
127
+ }
128
+ catch (err) {
129
+ setError(err);
130
+ throw err;
131
+ }
132
+ finally {
133
+ setIsLoading(false);
134
+ }
135
+ };
136
+ useEffect(() => {
137
+ void load();
138
+ }, [client, requests]);
139
+ return {
140
+ data,
141
+ error,
142
+ isLoading,
143
+ reload: load
144
+ };
145
+ }
146
+ export function useVersion() {
147
+ return CHILL_SHARP_REACT_CLIENT_VERSION;
148
+ }
149
+ export function useTest() {
150
+ const client = useChillSharpClient();
151
+ const [data, setData] = useState(null);
152
+ const [error, setError] = useState(null);
153
+ const [isLoading, setIsLoading] = useState(true);
154
+ const load = async () => {
155
+ setIsLoading(true);
156
+ setError(null);
157
+ try {
158
+ const response = await client.test();
159
+ setData(response);
160
+ return response;
161
+ }
162
+ catch (err) {
163
+ setError(err);
164
+ throw err;
165
+ }
166
+ finally {
167
+ setIsLoading(false);
168
+ }
169
+ };
170
+ useEffect(() => {
171
+ void load();
172
+ }, [client]);
173
+ return {
174
+ data,
175
+ error,
176
+ isLoading,
177
+ reload: load
178
+ };
179
+ }
180
+ export function useCurrentUserPreferences() {
181
+ const client = useChillSharpClient();
182
+ const [data, setData] = useState(null);
183
+ const [error, setError] = useState(null);
184
+ const [isLoading, setIsLoading] = useState(true);
185
+ const load = async () => {
186
+ setIsLoading(true);
187
+ setError(null);
188
+ try {
189
+ const response = await client.getCurrentUserPreferences();
190
+ setData(response);
191
+ return response;
192
+ }
193
+ catch (err) {
194
+ setError(err);
195
+ throw err;
196
+ }
197
+ finally {
198
+ setIsLoading(false);
199
+ }
200
+ };
201
+ useEffect(() => { void load(); }, [client]);
202
+ return { data, error, isLoading, reload: load };
203
+ }
204
+ export function useQueryMutation() {
205
+ const client = useChillSharpClient();
206
+ const [data, setData] = useState(null);
207
+ const [error, setError] = useState(null);
208
+ const [isLoading, setIsLoading] = useState(false);
209
+ const execute = async (...args) => {
210
+ const payload = args[0];
211
+ setIsLoading(true);
212
+ setError(null);
213
+ try {
214
+ const response = await client.query(payload);
215
+ setData(response);
216
+ return response;
217
+ }
218
+ catch (err) {
219
+ setError(err);
220
+ throw err;
221
+ }
222
+ finally {
223
+ setIsLoading(false);
224
+ }
225
+ };
226
+ return {
227
+ data,
228
+ error,
229
+ isLoading,
230
+ execute,
231
+ reset: () => {
232
+ setData(null);
233
+ setError(null);
234
+ setIsLoading(false);
235
+ }
236
+ };
237
+ }
238
+ export function useLookupMutation() {
239
+ const client = useChillSharpClient();
240
+ const [data, setData] = useState(null);
241
+ const [error, setError] = useState(null);
242
+ const [isLoading, setIsLoading] = useState(false);
243
+ const execute = async (...args) => {
244
+ const payload = args[0];
245
+ setIsLoading(true);
246
+ setError(null);
247
+ try {
248
+ const response = await client.lookup(payload);
249
+ setData(response);
250
+ return response;
251
+ }
252
+ catch (err) {
253
+ setError(err);
254
+ throw err;
255
+ }
256
+ finally {
257
+ setIsLoading(false);
258
+ }
259
+ };
260
+ return {
261
+ data,
262
+ error,
263
+ isLoading,
264
+ execute,
265
+ reset: () => {
266
+ setData(null);
267
+ setError(null);
268
+ setIsLoading(false);
269
+ }
270
+ };
271
+ }
272
+ export function useAutocompleteMutation() {
273
+ const client = useChillSharpClient();
274
+ const [data, setData] = useState(null);
275
+ const [error, setError] = useState(null);
276
+ const [isLoading, setIsLoading] = useState(false);
277
+ const execute = async (...args) => {
278
+ const payload = args[0];
279
+ setIsLoading(true);
280
+ setError(null);
281
+ try {
282
+ const response = await client.autocomplete(payload);
283
+ setData(response);
284
+ return response;
285
+ }
286
+ catch (err) {
287
+ setError(err);
288
+ throw err;
289
+ }
290
+ finally {
291
+ setIsLoading(false);
292
+ }
293
+ };
294
+ return {
295
+ data,
296
+ error,
297
+ isLoading,
298
+ execute,
299
+ reset: () => {
300
+ setData(null);
301
+ setError(null);
302
+ setIsLoading(false);
303
+ }
304
+ };
305
+ }
306
+ export function useValidateMutation() {
307
+ const client = useChillSharpClient();
308
+ const [data, setData] = useState(null);
309
+ const [error, setError] = useState(null);
310
+ const [isLoading, setIsLoading] = useState(false);
311
+ const execute = async (...args) => {
312
+ const payload = args[0];
313
+ setIsLoading(true);
314
+ setError(null);
315
+ try {
316
+ const response = await client.validate(payload);
317
+ setData(response);
318
+ return response;
319
+ }
320
+ catch (err) {
321
+ setError(err);
322
+ throw err;
323
+ }
324
+ finally {
325
+ setIsLoading(false);
326
+ }
327
+ };
328
+ return {
329
+ data,
330
+ error,
331
+ isLoading,
332
+ execute,
333
+ reset: () => {
334
+ setData(null);
335
+ setError(null);
336
+ setIsLoading(false);
337
+ }
338
+ };
339
+ }
340
+ export function useEntityMutation(action) {
341
+ const client = useChillSharpClient();
342
+ const [data, setData] = useState(null);
343
+ const [error, setError] = useState(null);
344
+ const [isLoading, setIsLoading] = useState(false);
345
+ const execute = async (...args) => {
346
+ const payload = args[0];
347
+ setIsLoading(true);
348
+ setError(null);
349
+ try {
350
+ let response;
351
+ switch (action) {
352
+ case "find":
353
+ response = await client.find(payload);
354
+ break;
355
+ case "create":
356
+ response = await client.create(payload);
357
+ break;
358
+ case "update":
359
+ response = await client.update(payload);
360
+ break;
361
+ default:
362
+ await client.delete(payload);
363
+ response = null;
364
+ break;
365
+ }
366
+ setData(response);
367
+ return response;
368
+ }
369
+ catch (err) {
370
+ setError(err);
371
+ throw err;
372
+ }
373
+ finally {
374
+ setIsLoading(false);
375
+ }
376
+ };
377
+ return {
378
+ data,
379
+ error,
380
+ isLoading,
381
+ execute,
382
+ reset: () => {
383
+ setData(null);
384
+ setError(null);
385
+ setIsLoading(false);
386
+ }
387
+ };
388
+ }
389
+ export function useEntityChanges(chillType, onChanges, guid) {
390
+ const client = useChillSharpClient();
391
+ const onChangesRef = useRef(onChanges);
392
+ const unsubscribeRef = useRef(null);
393
+ const [error, setError] = useState(null);
394
+ const [isSubscribed, setIsSubscribed] = useState(false);
395
+ useEffect(() => {
396
+ onChangesRef.current = onChanges;
397
+ }, [onChanges]);
398
+ useEffect(() => {
399
+ let isDisposed = false;
400
+ setError(null);
401
+ setIsSubscribed(false);
402
+ unsubscribeRef.current = null;
403
+ void client
404
+ .subscribeToEntityChanges(chillType, async (changes) => {
405
+ await onChangesRef.current(changes);
406
+ }, guid)
407
+ .then((subscription) => {
408
+ if (isDisposed) {
409
+ void subscription.unsubscribe();
410
+ return;
411
+ }
412
+ unsubscribeRef.current = () => subscription.unsubscribe();
413
+ setIsSubscribed(true);
414
+ })
415
+ .catch((err) => {
416
+ if (!isDisposed) {
417
+ setError(err);
418
+ setIsSubscribed(false);
419
+ }
420
+ });
421
+ return () => {
422
+ isDisposed = true;
423
+ setIsSubscribed(false);
424
+ if (unsubscribeRef.current) {
425
+ void unsubscribeRef.current();
426
+ unsubscribeRef.current = null;
427
+ }
428
+ };
429
+ }, [client, chillType, guid]);
430
+ return {
431
+ error,
432
+ isSubscribed
433
+ };
434
+ }
@@ -0,0 +1,7 @@
1
+ export { ChillSharpProvider, useChillSharpClient } from "./context.js";
2
+ export { useAutocompleteMutation, useCurrentUserPreferences, useEntityChanges, useEntityMutation, useQueryMutation, useSchema, useSchemaList, useTest, useText, useTexts, useValidateMutation, useVersion } from "./hooks.js";
3
+ export type { ChillSharpProviderProps } from "./context.js";
4
+ export type { UseChillAsyncState, UseChillMutationState, UseChillSubscriptionState } from "./hooks.js";
5
+ export { CHILL_SHARP_REACT_CLIENT_VERSION } from "./version.js";
6
+ export { API_BASE_PATH, ChillSharpClient, ChillSharpClientError, PermissionAction, PermissionEffect, PermissionScope } from "@chill-sharp/ts-client";
7
+ export type { AuthPermissionRule, AuthPermissionRuleItem, AuthRoleDetailsResponse, AuthRoleListItem, AuthRolePermissions, AuthTokenResponse, AuthUserDetailsResponse, AuthUserListItem, ChillDtoEntity, ChillUserPreferences, ChillDtoEntityOptions, ChillDtoMenuItem, ChillDtoProperty, ChillDtoPropertySchema, ChillDtoQuery, ChillDtoSchema, ChillDtoSchemaRelation, ChillDtoSchemaRelationLabel, ChillDtoSchemaListItem, ChillAttachmentUploadFile, ChillAttachmentUploadOptions, ChillEntityChangeAction, ChillEntityChangeCallback, ChillEntityChangeNotification, ChillEntityChangeSubscription, ChillOrdering, ChillPagination, ChillSharpClientOptions, GetAuthPermissionsResponse, GetTextRequest, GetTextResponse, JsonObject, JsonPrimitive, JsonValue, RegisterAuthIdentityRequest, SetAuthRoleRequest, SetAuthUserRequest } from "@chill-sharp/ts-client";
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ /*
2
+ * ChillSharp is a lightweight .NET library that sits on top of Entity Framework Core
3
+ * and turns an existing data model into a fully working REST API with almost no setup.
4
+ * Copyright (C) 2025 Andrea Piovesan
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Affero General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Affero General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Affero General Public License
17
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+ */
19
+ export { ChillSharpProvider, useChillSharpClient } from "./context.js";
20
+ export { useAutocompleteMutation, useCurrentUserPreferences, useEntityChanges, useEntityMutation, useQueryMutation, useSchema, useSchemaList, useTest, useText, useTexts, useValidateMutation, useVersion } from "./hooks.js";
21
+ export { CHILL_SHARP_REACT_CLIENT_VERSION } from "./version.js";
22
+ export { API_BASE_PATH, ChillSharpClient, ChillSharpClientError, PermissionAction, PermissionEffect, PermissionScope } from "@chill-sharp/ts-client";
@@ -0,0 +1 @@
1
+ export declare const CHILL_SHARP_REACT_CLIENT_VERSION = "0.1.0";
@@ -0,0 +1,19 @@
1
+ /*
2
+ * ChillSharp is a lightweight .NET library that sits on top of Entity Framework Core
3
+ * and turns an existing data model into a fully working REST API with almost no setup.
4
+ * Copyright (C) 2025 Andrea Piovesan
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Affero General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Affero General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Affero General Public License
17
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+ */
19
+ export const CHILL_SHARP_REACT_CLIENT_VERSION = "0.1.0";
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@chill-sharp/react-client",
3
+ "repository": {
4
+ "type": "git",
5
+ "url": "git+https://github.com/e-500/chill-sharp.git",
6
+ "directory": "extra/chill-sharp-react-client"
7
+ },
8
+ "version": "1.1.12",
9
+ "description": "React client helpers for generic ChillSharp services",
10
+ "license": "AGPL-3.0-or-later",
11
+ "type": "module",
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "src",
23
+ "README.md"
24
+ ],
25
+ "scripts": {
26
+ "build": "tsc -p tsconfig.json",
27
+ "check": "tsc -p tsconfig.json --noEmit",
28
+ "prepare": "npm run build",
29
+ "prepack": "npm run build"
30
+ },
31
+ "keywords": [
32
+ "chillsharp",
33
+ "react",
34
+ "typescript",
35
+ "client",
36
+ "hooks"
37
+ ],
38
+ "author": "Andrea Piovesan",
39
+ "peerDependencies": {
40
+ "react": "^18.2.0 || ^19.0.0",
41
+ "@chill-sharp/ts-client": "^1.1.12"
42
+ },
43
+ "devDependencies": {
44
+ "@types/react": "^19.1.0",
45
+ "typescript": "^5.8.2",
46
+ "@chill-sharp/ts-client": "file:../chill-sharp-ts-client"
47
+ }
48
+ }
49
+
@@ -0,0 +1,58 @@
1
+ /*
2
+ * ChillSharp is a lightweight .NET library that sits on top of Entity Framework Core
3
+ * and turns an existing data model into a fully working REST API with almost no setup.
4
+ * Copyright (C) 2025 Andrea Piovesan
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Affero General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Affero General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Affero General Public License
17
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+ */
19
+
20
+ import React, { createContext, useContext, useRef } from "react";
21
+ import { ChillSharpClient } from "@chill-sharp/ts-client";
22
+ import type { ChillSharpClientOptions } from "@chill-sharp/ts-client";
23
+
24
+ export interface ChillSharpProviderProps {
25
+ baseUrl: string;
26
+ client?: ChillSharpClient;
27
+ options?: ChillSharpClientOptions;
28
+ children: React.ReactNode;
29
+ }
30
+
31
+ const ChillSharpContext = createContext<ChillSharpClient | null>(null);
32
+
33
+ export function ChillSharpProvider(props: ChillSharpProviderProps) {
34
+ const clientRef = useRef<ChillSharpClient | null>(props.client ?? null);
35
+
36
+ if (props.client && clientRef.current !== props.client) {
37
+ clientRef.current = props.client;
38
+ }
39
+
40
+ if (!clientRef.current) {
41
+ clientRef.current = new ChillSharpClient(props.baseUrl, props.options);
42
+ }
43
+
44
+ return (
45
+ <ChillSharpContext.Provider value={clientRef.current}>
46
+ {props.children}
47
+ </ChillSharpContext.Provider>
48
+ );
49
+ }
50
+
51
+ export function useChillSharpClient(): ChillSharpClient {
52
+ const client = useContext(ChillSharpContext);
53
+ if (!client) {
54
+ throw new Error("useChillSharpClient must be used inside ChillSharpProvider.");
55
+ }
56
+
57
+ return client;
58
+ }