api-core-lib 12.0.10 → 12.0.11
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/{apiModule.types-2yhaJWN3.d.cts → apiModule.types-CpwGDEpG.d.cts} +2 -2
- package/dist/{apiModule.types-2yhaJWN3.d.ts → apiModule.types-CpwGDEpG.d.ts} +2 -2
- package/dist/client.cjs +864 -0
- package/dist/client.d.cts +28 -0
- package/dist/client.d.ts +28 -0
- package/dist/client.js +824 -0
- package/dist/index.cjs +2 -458
- package/dist/index.d.cts +5 -113
- package/dist/index.d.ts +5 -113
- package/dist/index.js +1 -451
- package/dist/server.d.cts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/useApiRecord.types-B45E0qux.d.cts +90 -0
- package/dist/useApiRecord.types-DlrlXL1t.d.ts +90 -0
- package/package.json +7 -2
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
+
import { R as RequestConfig, h as ApiError, S as StandardResponse, e as ActionOptions } from './apiModule.types-CpwGDEpG.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents the internal state of the `useApiRecord` hook.
|
|
6
|
+
* It mirrors the `StandardResponse` structure, which is the unified response format
|
|
7
|
+
* used across the library.
|
|
8
|
+
* @template T The data type of the record.
|
|
9
|
+
*/
|
|
10
|
+
type UseApiRecordState<T> = StandardResponse<T>;
|
|
11
|
+
/**
|
|
12
|
+
* Defines the action methods provided by the `useApiRecord` hook to interact with
|
|
13
|
+
* a single API resource.
|
|
14
|
+
* @template T The data type of the record.
|
|
15
|
+
*/
|
|
16
|
+
interface UseApiRecordActions<T> {
|
|
17
|
+
/**
|
|
18
|
+
* Manually fetches or refetches the record from the API.
|
|
19
|
+
*/
|
|
20
|
+
fetch: () => Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Partially updates the record using an HTTP PATCH request.
|
|
23
|
+
* @param updatedItem An object containing the fields to update.
|
|
24
|
+
* @param options Additional request options, allowing overrides for this specific action.
|
|
25
|
+
* @returns A promise that resolves to the standard response object for the updated record.
|
|
26
|
+
*/
|
|
27
|
+
update: (updatedItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
28
|
+
/**
|
|
29
|
+
* Replaces the entire record with a new one using an HTTP PUT request.
|
|
30
|
+
* @param item The complete new record object.
|
|
31
|
+
* @param options Additional request options.
|
|
32
|
+
* @returns A promise that resolves to the standard response object for the replaced record.
|
|
33
|
+
*/
|
|
34
|
+
put: (item: T, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
35
|
+
/**
|
|
36
|
+
* Deletes the record using an HTTP DELETE request.
|
|
37
|
+
* @param options Additional request options.
|
|
38
|
+
* @returns A promise that resolves to a standard response object with a null data payload.
|
|
39
|
+
*/
|
|
40
|
+
remove: (options?: ActionOptions) => Promise<StandardResponse<null>>;
|
|
41
|
+
/**
|
|
42
|
+
* Resets the hook's state to its initial value.
|
|
43
|
+
*/
|
|
44
|
+
resetState: () => void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Defines the configuration options for the `useApiRecord` hook.
|
|
48
|
+
* @template T The data type of the record.
|
|
49
|
+
*/
|
|
50
|
+
interface UseApiRecordConfig<T> {
|
|
51
|
+
/**
|
|
52
|
+
* The base API endpoint template for the resource.
|
|
53
|
+
* Can contain placeholders like `{endpointName}`.
|
|
54
|
+
* @example 'v1/dynamic/{endpointName}'
|
|
55
|
+
*/
|
|
56
|
+
endpoint: string;
|
|
57
|
+
/**
|
|
58
|
+
* An object containing key-value pairs to replace placeholders in the endpoint template.
|
|
59
|
+
* @example { endpointName: 'products' }
|
|
60
|
+
*/
|
|
61
|
+
pathParams?: Record<string, string | number>;
|
|
62
|
+
/** The unique identifier of the record to fetch or modify. */
|
|
63
|
+
recordId?: string | number | null;
|
|
64
|
+
/** Optional initial data to populate the state before the first fetch is complete. */
|
|
65
|
+
initialData?: T | null;
|
|
66
|
+
/** If `false`, the hook will not automatically fetch data on mount. Defaults to `true`. */
|
|
67
|
+
enabled?: boolean;
|
|
68
|
+
/** If `true`, the record will be refetched after a successful `update`, `put`, or `remove` action. Defaults to `true`. */
|
|
69
|
+
refetchAfterChange?: boolean;
|
|
70
|
+
/** Default `RequestConfig` to apply to the initial GET request made by the hook. */
|
|
71
|
+
requestConfig?: RequestConfig;
|
|
72
|
+
/** A callback function executed on a successful API action. */
|
|
73
|
+
onSuccess?: (message: string, data?: any) => void;
|
|
74
|
+
/** A callback function executed on a failed API action. */
|
|
75
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The return value of the `useApiRecord` hook.
|
|
79
|
+
* @template T The data type of the record.
|
|
80
|
+
*/
|
|
81
|
+
interface UseApiRecordReturn<T> {
|
|
82
|
+
/** The current state of the API request, including data, loading, and error status. */
|
|
83
|
+
state: UseApiRecordState<T>;
|
|
84
|
+
/** Action methods to manipulate the record. */
|
|
85
|
+
actions: UseApiRecordActions<T>;
|
|
86
|
+
/** A React dispatch function to manually set the hook's state. Use with caution. */
|
|
87
|
+
setState: Dispatch<SetStateAction<UseApiRecordState<T>>>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type { UseApiRecordConfig as U, UseApiRecordReturn as a, UseApiRecordState as b, UseApiRecordActions as c };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
+
import { R as RequestConfig, h as ApiError, S as StandardResponse, e as ActionOptions } from './apiModule.types-CpwGDEpG.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents the internal state of the `useApiRecord` hook.
|
|
6
|
+
* It mirrors the `StandardResponse` structure, which is the unified response format
|
|
7
|
+
* used across the library.
|
|
8
|
+
* @template T The data type of the record.
|
|
9
|
+
*/
|
|
10
|
+
type UseApiRecordState<T> = StandardResponse<T>;
|
|
11
|
+
/**
|
|
12
|
+
* Defines the action methods provided by the `useApiRecord` hook to interact with
|
|
13
|
+
* a single API resource.
|
|
14
|
+
* @template T The data type of the record.
|
|
15
|
+
*/
|
|
16
|
+
interface UseApiRecordActions<T> {
|
|
17
|
+
/**
|
|
18
|
+
* Manually fetches or refetches the record from the API.
|
|
19
|
+
*/
|
|
20
|
+
fetch: () => Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Partially updates the record using an HTTP PATCH request.
|
|
23
|
+
* @param updatedItem An object containing the fields to update.
|
|
24
|
+
* @param options Additional request options, allowing overrides for this specific action.
|
|
25
|
+
* @returns A promise that resolves to the standard response object for the updated record.
|
|
26
|
+
*/
|
|
27
|
+
update: (updatedItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
28
|
+
/**
|
|
29
|
+
* Replaces the entire record with a new one using an HTTP PUT request.
|
|
30
|
+
* @param item The complete new record object.
|
|
31
|
+
* @param options Additional request options.
|
|
32
|
+
* @returns A promise that resolves to the standard response object for the replaced record.
|
|
33
|
+
*/
|
|
34
|
+
put: (item: T, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
35
|
+
/**
|
|
36
|
+
* Deletes the record using an HTTP DELETE request.
|
|
37
|
+
* @param options Additional request options.
|
|
38
|
+
* @returns A promise that resolves to a standard response object with a null data payload.
|
|
39
|
+
*/
|
|
40
|
+
remove: (options?: ActionOptions) => Promise<StandardResponse<null>>;
|
|
41
|
+
/**
|
|
42
|
+
* Resets the hook's state to its initial value.
|
|
43
|
+
*/
|
|
44
|
+
resetState: () => void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Defines the configuration options for the `useApiRecord` hook.
|
|
48
|
+
* @template T The data type of the record.
|
|
49
|
+
*/
|
|
50
|
+
interface UseApiRecordConfig<T> {
|
|
51
|
+
/**
|
|
52
|
+
* The base API endpoint template for the resource.
|
|
53
|
+
* Can contain placeholders like `{endpointName}`.
|
|
54
|
+
* @example 'v1/dynamic/{endpointName}'
|
|
55
|
+
*/
|
|
56
|
+
endpoint: string;
|
|
57
|
+
/**
|
|
58
|
+
* An object containing key-value pairs to replace placeholders in the endpoint template.
|
|
59
|
+
* @example { endpointName: 'products' }
|
|
60
|
+
*/
|
|
61
|
+
pathParams?: Record<string, string | number>;
|
|
62
|
+
/** The unique identifier of the record to fetch or modify. */
|
|
63
|
+
recordId?: string | number | null;
|
|
64
|
+
/** Optional initial data to populate the state before the first fetch is complete. */
|
|
65
|
+
initialData?: T | null;
|
|
66
|
+
/** If `false`, the hook will not automatically fetch data on mount. Defaults to `true`. */
|
|
67
|
+
enabled?: boolean;
|
|
68
|
+
/** If `true`, the record will be refetched after a successful `update`, `put`, or `remove` action. Defaults to `true`. */
|
|
69
|
+
refetchAfterChange?: boolean;
|
|
70
|
+
/** Default `RequestConfig` to apply to the initial GET request made by the hook. */
|
|
71
|
+
requestConfig?: RequestConfig;
|
|
72
|
+
/** A callback function executed on a successful API action. */
|
|
73
|
+
onSuccess?: (message: string, data?: any) => void;
|
|
74
|
+
/** A callback function executed on a failed API action. */
|
|
75
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The return value of the `useApiRecord` hook.
|
|
79
|
+
* @template T The data type of the record.
|
|
80
|
+
*/
|
|
81
|
+
interface UseApiRecordReturn<T> {
|
|
82
|
+
/** The current state of the API request, including data, loading, and error status. */
|
|
83
|
+
state: UseApiRecordState<T>;
|
|
84
|
+
/** Action methods to manipulate the record. */
|
|
85
|
+
actions: UseApiRecordActions<T>;
|
|
86
|
+
/** A React dispatch function to manually set the hook's state. Use with caution. */
|
|
87
|
+
setState: Dispatch<SetStateAction<UseApiRecordState<T>>>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type { UseApiRecordConfig as U, UseApiRecordReturn as a, UseApiRecordState as b, UseApiRecordActions as c };
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-core-lib",
|
|
3
|
-
"version": "12.0.
|
|
3
|
+
"version": "12.0.11",
|
|
4
4
|
"description": "A flexible and powerful API client library for modern web applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
|
|
7
7
|
"exports": {
|
|
8
|
-
".": {
|
|
8
|
+
".": {
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
10
|
"import": "./dist/index.js",
|
|
11
11
|
"require": "./dist/index.cjs"
|
|
@@ -14,6 +14,11 @@
|
|
|
14
14
|
"types": "./dist/server.d.ts",
|
|
15
15
|
"import": "./dist/server.js",
|
|
16
16
|
"require": "./dist/server.cjs"
|
|
17
|
+
},
|
|
18
|
+
"./client": {
|
|
19
|
+
"types": "./dist/client.d.ts",
|
|
20
|
+
"import": "./dist/client.js",
|
|
21
|
+
"require": "./dist/client.cjs"
|
|
17
22
|
}
|
|
18
23
|
},
|
|
19
24
|
|