@forgepack/request 1.0.5 → 1.0.6
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/README.md +217 -86
- package/dist/api/client.d.ts +17 -10
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +18 -6
- package/dist/hooks/AuthContext.d.ts +6 -6
- package/dist/hooks/AuthContext.d.ts.map +1 -1
- package/dist/hooks/AuthContext.js +1 -1
- package/dist/hooks/AuthProvider.d.ts +34 -17
- package/dist/hooks/AuthProvider.d.ts.map +1 -1
- package/dist/hooks/AuthProvider.js +26 -15
- package/dist/hooks/useAuth.d.ts +33 -12
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +30 -12
- package/dist/hooks/useRequest.d.ts +37 -17
- package/dist/hooks/useRequest.d.ts.map +1 -1
- package/dist/hooks/useRequest.js +36 -13
- package/dist/index.d.ts +159 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +159 -14
- package/dist/services/api.d.ts +37 -15
- package/dist/services/api.d.ts.map +1 -1
- package/dist/services/api.js +40 -14
- package/dist/services/auth.d.ts +76 -19
- package/dist/services/auth.d.ts.map +1 -1
- package/dist/services/auth.js +94 -22
- package/dist/services/crud.d.ts +115 -62
- package/dist/services/crud.d.ts.map +1 -1
- package/dist/services/crud.js +132 -85
- package/dist/services/token.d.ts +83 -26
- package/dist/services/token.d.ts.map +1 -1
- package/dist/services/token.js +141 -52
- package/dist/types/auth.d.ts +20 -20
- package/dist/types/auth.d.ts.map +1 -1
- package/dist/types/error.d.ts +3 -3
- package/dist/types/error.d.ts.map +1 -1
- package/dist/types/request.d.ts +8 -8
- package/dist/types/request.d.ts.map +1 -1
- package/dist/types/response.d.ts +15 -15
- package/dist/types/response.d.ts.map +1 -1
- package/dist/types/token.d.ts +15 -15
- package/dist/types/token.d.ts.map +1 -1
- package/dist/utils/constants.d.ts +7 -7
- package/dist/utils/constants.js +7 -7
- package/package.json +15 -3
package/dist/services/crud.d.ts
CHANGED
|
@@ -1,87 +1,127 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
2
|
import { ErrorMessage } from '../types/error';
|
|
3
3
|
import { Search } from '../types/request';
|
|
4
|
+
import { Page } from '../types/response';
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
+
* Creates a new record in the API via POST request
|
|
6
7
|
*
|
|
7
|
-
* @template T -
|
|
8
|
-
* @param api -
|
|
9
|
-
* @param url -
|
|
10
|
-
* @param object -
|
|
11
|
-
* @returns Promise
|
|
8
|
+
* @template T - Type of object to be created
|
|
9
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
10
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
11
|
+
* @param {T} object - Object data to be created
|
|
12
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to created data or array of errors
|
|
13
|
+
*
|
|
14
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
12
15
|
*
|
|
13
16
|
* @example
|
|
14
17
|
* ```typescript
|
|
15
|
-
*
|
|
18
|
+
* // Create a user
|
|
19
|
+
* const result = await create(api, 'users', { name: 'John Snow', email: 'john@example.com', role: 'USER' })
|
|
16
20
|
* ```
|
|
17
21
|
*/
|
|
18
|
-
export declare const create: <T>(api: AxiosInstance, url: string, object: T) => Promise<
|
|
22
|
+
export declare const create: <T>(api: AxiosInstance, url: string, object: T) => Promise<T | ErrorMessage[]>;
|
|
19
23
|
/**
|
|
20
|
-
*
|
|
24
|
+
* Creates multiple records at once in the API via batch POST request
|
|
25
|
+
*
|
|
26
|
+
* @template T - Type of objects to be created
|
|
27
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
28
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
29
|
+
* @param {T[]} object - Array of objects to be created
|
|
30
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to created data or array of errors
|
|
31
|
+
*
|
|
32
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // Create multiple users at once
|
|
37
|
+
* const users = [
|
|
38
|
+
* { name: 'John Snow', email: 'john@example.com' },
|
|
39
|
+
* { name: 'Jane Smith', email: 'jane@example.com' },
|
|
40
|
+
* { name: 'Bob Johnson', email: 'bob@example.com' }
|
|
41
|
+
* ]
|
|
21
42
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @param url - Endpoint da API (sem barra inicial)
|
|
25
|
-
* @param object - Array de objetos a serem criados
|
|
26
|
-
* @returns Promise com dados criados ou array de erros
|
|
43
|
+
* const result = await createAll(api, 'users', users)
|
|
44
|
+
* ```
|
|
27
45
|
*/
|
|
28
|
-
export declare const createAll: <T>(api: AxiosInstance, url: string, object: T[]) => Promise<ErrorMessage[]
|
|
46
|
+
export declare const createAll: <T>(api: AxiosInstance, url: string, object: T[]) => Promise<T | ErrorMessage[]>;
|
|
29
47
|
/**
|
|
30
|
-
*
|
|
48
|
+
* Retrieves records from the API with optional pagination, search, and sorting support
|
|
49
|
+
*
|
|
50
|
+
* This function delegates to `fetchPage` for paginated requests and handles
|
|
51
|
+
* unpaginated retrieval directly.
|
|
31
52
|
*
|
|
32
|
-
*
|
|
33
|
-
* -
|
|
34
|
-
* -
|
|
35
|
-
* -
|
|
53
|
+
* Behaviors based on parameters:
|
|
54
|
+
* - Without search: retrieves all records
|
|
55
|
+
* - With page/size: paginated search
|
|
56
|
+
* - With sort: paginated and sorted search
|
|
36
57
|
*
|
|
37
|
-
* @template T -
|
|
38
|
-
* @param api -
|
|
39
|
-
* @param url -
|
|
40
|
-
* @param search -
|
|
41
|
-
* @param
|
|
42
|
-
* @
|
|
58
|
+
* @template T - Type of individual items in the response
|
|
59
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
60
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
61
|
+
* @param {Search} [search] - Optional search, pagination and sorting parameters
|
|
62
|
+
* @param {number} [search.page] - Page number (zero-indexed)
|
|
63
|
+
* @param {number} [search.size] - Number of items per page
|
|
64
|
+
* @param {string} [search.value] - Search term for filtering
|
|
65
|
+
* @param {Object} [search.sort] - Sorting configuration
|
|
66
|
+
* @param {string} [search.sort.key] - Field name to sort by
|
|
67
|
+
* @param {'asc'|'desc'} [search.sort.order] - Sort order
|
|
68
|
+
* @param {AbortSignal} [signal] - Signal for request cancellation
|
|
69
|
+
* @returns {Promise<T[] | Page<T> | ErrorMessage[]>} Promise resolving to:
|
|
70
|
+
* - Array of items (unpaginated)
|
|
71
|
+
* - Page object (paginated)
|
|
72
|
+
* - Array of errors (on failure)
|
|
73
|
+
*
|
|
74
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
43
75
|
*
|
|
44
76
|
* @example
|
|
45
77
|
* ```typescript
|
|
46
|
-
* //
|
|
47
|
-
* const
|
|
78
|
+
* // Simple search
|
|
79
|
+
* const allUsers = await retrieve(api, 'users')
|
|
48
80
|
*
|
|
49
|
-
* //
|
|
81
|
+
* // Paginated search
|
|
50
82
|
* const page = await retrieve(api, 'users', { page: 0, size: 10 })
|
|
51
83
|
*
|
|
52
|
-
* //
|
|
84
|
+
* // Search with filter + pagination + sorting
|
|
53
85
|
* const filtered = await retrieve(api, 'users', {
|
|
54
|
-
* value: '
|
|
86
|
+
* value: 'John',
|
|
55
87
|
* page: 0,
|
|
56
88
|
* size: 10,
|
|
57
89
|
* sort: { key: 'name', order: 'ASC' }
|
|
58
90
|
* })
|
|
91
|
+
*
|
|
92
|
+
* // With cancellation support
|
|
93
|
+
* const controller = new AbortController()
|
|
94
|
+
* const promise = retrieve(api, 'posts', { page: 0, size: 15 }, controller.signal)
|
|
59
95
|
* ```
|
|
60
96
|
*/
|
|
61
|
-
export declare const retrieve: <T>(api: AxiosInstance, url: string, search?: Search, signal?: AbortSignal) => Promise<ErrorMessage[]
|
|
97
|
+
export declare const retrieve: <T>(api: AxiosInstance, url: string, search?: Search, signal?: AbortSignal) => Promise<Page<T> | ErrorMessage[]>;
|
|
62
98
|
/**
|
|
63
|
-
*
|
|
99
|
+
* Updates an existing record in the API via PUT request
|
|
64
100
|
*
|
|
65
|
-
* @template T -
|
|
66
|
-
* @param api -
|
|
67
|
-
* @param url -
|
|
68
|
-
* @param object -
|
|
69
|
-
* @returns Promise
|
|
101
|
+
* @template T - Type of object to be updated
|
|
102
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
103
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
104
|
+
* @param {T} object - Updated object data (must include identifier)
|
|
105
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to response or array of errors
|
|
106
|
+
*
|
|
107
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
70
108
|
*
|
|
71
109
|
* @example
|
|
72
110
|
* ```typescript
|
|
73
|
-
* const result = await update(api, 'users', { id: 1, name: '
|
|
111
|
+
* const result = await update(api, 'users', { id: 1, name: 'John Silva' })
|
|
74
112
|
* ```
|
|
75
113
|
*/
|
|
76
|
-
export declare const update: <T>(api: AxiosInstance, url: string, object: T) => Promise<ErrorMessage[]
|
|
114
|
+
export declare const update: <T>(api: AxiosInstance, url: string, object: T) => Promise<T | ErrorMessage[]>;
|
|
77
115
|
/**
|
|
78
|
-
*
|
|
116
|
+
* Removes a specific record from the API via DELETE request
|
|
117
|
+
*
|
|
118
|
+
* @template T - Type of response
|
|
119
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
120
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
121
|
+
* @param {string} id - ID of the record to be removed
|
|
122
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to response or array of errors
|
|
79
123
|
*
|
|
80
|
-
* @
|
|
81
|
-
* @param api - Instância do Axios configurada
|
|
82
|
-
* @param url - Endpoint da API (sem barra inicial)
|
|
83
|
-
* @param id - ID do registro a ser removido
|
|
84
|
-
* @returns Promise com resposta ou array de erros
|
|
124
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
85
125
|
*
|
|
86
126
|
* @example
|
|
87
127
|
* ```typescript
|
|
@@ -90,26 +130,39 @@ export declare const update: <T>(api: AxiosInstance, url: string, object: T) =>
|
|
|
90
130
|
*/
|
|
91
131
|
export declare const remove: <T>(api: AxiosInstance, url: string, id: string) => Promise<ErrorMessage[] | T>;
|
|
92
132
|
/**
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* @template T -
|
|
96
|
-
* @param api -
|
|
97
|
-
* @param url -
|
|
98
|
-
* @param object -
|
|
99
|
-
* @param one -
|
|
100
|
-
* @param two -
|
|
101
|
-
* @param three -
|
|
102
|
-
* @param four -
|
|
103
|
-
* @returns Promise
|
|
133
|
+
* Removes a record with composite key (multiple identifiers) via DELETE request
|
|
134
|
+
*
|
|
135
|
+
* @template T - Type of response
|
|
136
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
137
|
+
* @param {string} url - API endpoint path (without leading slash)
|
|
138
|
+
* @param {Object} object - Object with additional data for deletion (if needed by backend)
|
|
139
|
+
* @param {string} one - First identifier segment
|
|
140
|
+
* @param {string} two - Second identifier segment
|
|
141
|
+
* @param {string} three - Third identifier segment (optional, pass '' if unused)
|
|
142
|
+
* @param {string} four - Fourth identifier segment (optional, pass '' if unused)
|
|
143
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to response or array of errors
|
|
144
|
+
*
|
|
145
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* // Delete with 2-part composite key
|
|
150
|
+
* const result = await removeComposite(api, 'user-roles', {}, 'user-123', 'role-admin', '', '')
|
|
151
|
+
* ```
|
|
104
152
|
*/
|
|
105
153
|
export declare const removeComposite: <T>(api: AxiosInstance, url: string, object: Object, one: string, two: string, three: string, four: string) => Promise<ErrorMessage[] | T>;
|
|
106
154
|
/**
|
|
107
|
-
*
|
|
155
|
+
* Removes all records from a specific endpoint via DELETE request
|
|
156
|
+
*
|
|
157
|
+
* ⚠️ **WARNING**: This is a destructive operation that removes ALL records.
|
|
158
|
+
* Use with extreme caution and ensure this is the intended action.
|
|
159
|
+
*
|
|
160
|
+
* @template T - Type of response
|
|
161
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
162
|
+
* @param {string} url - API endpoint path (without leading slash)
|
|
163
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to response or array of errors
|
|
108
164
|
*
|
|
109
|
-
* @
|
|
110
|
-
* @param api - Instância do Axios configurada
|
|
111
|
-
* @param url - Endpoint da API (sem barra inicial)
|
|
112
|
-
* @returns Promise com resposta ou array de erros
|
|
165
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
113
166
|
*
|
|
114
167
|
* @example
|
|
115
168
|
* ```typescript
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../src/services/crud.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../src/services/crud.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAEzC,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAA;AA+BxC;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,MAAM,GAAU,CAAC,EAAG,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,QAAQ,CAAC,KAAG,OAAO,CAAC,CAAC,GAAG,YAAY,EAAE,CAIvG,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,SAAS,GAAU,CAAC,EAAG,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,QAAQ,CAAC,EAAE,KAAG,OAAO,CAAC,CAAC,GAAG,YAAY,EAAE,CAI5G,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,eAAO,MAAM,QAAQ,GAAU,CAAC,EAAE,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,SAAS,MAAM,EAAE,SAAS,WAAW,KAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,YAAY,EAAE,CAM1I,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,MAAM,GAAU,CAAC,EAAG,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,QAAQ,CAAC,KAAG,OAAO,CAAC,CAAC,GAAG,YAAY,EAAE,CAIvG,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,MAAM,GAAU,CAAC,EAAG,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,IAAI,MAAM,gCAI3E,CAAA;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,eAAe,GAAU,CAAC,EAAG,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,QAAQ,MAAM,EAAE,KAAK,MAAM,EAAE,KAAK,MAAM,EAAE,OAAO,MAAM,EAAE,MAAM,MAAM,gCAU/I,CAAA;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,SAAS,GAAU,CAAC,EAAG,KAAK,aAAa,EAAE,KAAK,MAAM,gCAIlE,CAAA"}
|
package/dist/services/crud.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.removeAll = exports.removeComposite = exports.remove = exports.update = exports.retrieve = exports.createAll = exports.create = void 0;
|
|
4
|
-
|
|
5
|
-
// Respostas de informação (100-199),
|
|
6
|
-
// Respostas de sucesso (200-299),
|
|
7
|
-
// Redirecionamentos (300-399)
|
|
8
|
-
// Erros do cliente (400-499)
|
|
9
|
-
// Erros do servidor (500-599).
|
|
4
|
+
const api_1 = require("./api");
|
|
10
5
|
/**
|
|
11
|
-
*
|
|
6
|
+
* HTTP Status Code Reference:
|
|
7
|
+
* - 1xx: Informational responses
|
|
8
|
+
* - 2xx: Success responses
|
|
9
|
+
* - 3xx: Redirects
|
|
10
|
+
* - 4xx: Client errors
|
|
11
|
+
* - 5xx: Server errors
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Processes API response errors and converts to standardized format
|
|
12
15
|
*
|
|
13
|
-
* @param error -
|
|
14
|
-
* @returns Array
|
|
16
|
+
* @param {any} error - Axios error object from catch block
|
|
17
|
+
* @returns {ErrorMessage[]} Array of formatted error messages with field and message properties
|
|
18
|
+
*
|
|
19
|
+
* @internal This is a utility function used internally by CRUD operations
|
|
15
20
|
*/
|
|
16
21
|
const addError = (error) => {
|
|
17
|
-
var _a, _b;
|
|
22
|
+
var _a, _b, _c, _d;
|
|
18
23
|
let errorMessage = [];
|
|
19
24
|
if (error.response.data.validationErrors !== undefined) {
|
|
20
25
|
(_b = (_a = error.response.data) === null || _a === void 0 ? void 0 : _a.validationErrors) === null || _b === void 0 ? void 0 : _b.forEach((element) => {
|
|
@@ -22,22 +27,25 @@ const addError = (error) => {
|
|
|
22
27
|
});
|
|
23
28
|
}
|
|
24
29
|
else {
|
|
25
|
-
errorMessage.push({ field: 'Error', message: 'Internal Error' });
|
|
30
|
+
errorMessage.push({ field: 'Error', message: ((_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message) || 'Internal Server Error' });
|
|
26
31
|
}
|
|
27
32
|
return errorMessage;
|
|
28
33
|
};
|
|
29
34
|
/**
|
|
30
|
-
*
|
|
35
|
+
* Creates a new record in the API via POST request
|
|
36
|
+
*
|
|
37
|
+
* @template T - Type of object to be created
|
|
38
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
39
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
40
|
+
* @param {T} object - Object data to be created
|
|
41
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to created data or array of errors
|
|
31
42
|
*
|
|
32
|
-
* @
|
|
33
|
-
* @param api - Instância do Axios configurada
|
|
34
|
-
* @param url - Endpoint da API (sem barra inicial)
|
|
35
|
-
* @param object - Dados do objeto a ser criado
|
|
36
|
-
* @returns Promise com dados criados ou array de erros
|
|
43
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
37
44
|
*
|
|
38
45
|
* @example
|
|
39
46
|
* ```typescript
|
|
40
|
-
*
|
|
47
|
+
* // Create a user
|
|
48
|
+
* const result = await create(api, 'users', { name: 'John Snow', email: 'john@example.com', role: 'USER' })
|
|
41
49
|
* ```
|
|
42
50
|
*/
|
|
43
51
|
const create = async (api, url, object) => {
|
|
@@ -47,13 +55,27 @@ const create = async (api, url, object) => {
|
|
|
47
55
|
};
|
|
48
56
|
exports.create = create;
|
|
49
57
|
/**
|
|
50
|
-
*
|
|
58
|
+
* Creates multiple records at once in the API via batch POST request
|
|
51
59
|
*
|
|
52
|
-
* @template T -
|
|
53
|
-
* @param api -
|
|
54
|
-
* @param url -
|
|
55
|
-
* @param object - Array
|
|
56
|
-
* @returns Promise
|
|
60
|
+
* @template T - Type of objects to be created
|
|
61
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
62
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
63
|
+
* @param {T[]} object - Array of objects to be created
|
|
64
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to created data or array of errors
|
|
65
|
+
*
|
|
66
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // Create multiple users at once
|
|
71
|
+
* const users = [
|
|
72
|
+
* { name: 'John Snow', email: 'john@example.com' },
|
|
73
|
+
* { name: 'Jane Smith', email: 'jane@example.com' },
|
|
74
|
+
* { name: 'Bob Johnson', email: 'bob@example.com' }
|
|
75
|
+
* ]
|
|
76
|
+
*
|
|
77
|
+
* const result = await createAll(api, 'users', users)
|
|
78
|
+
* ```
|
|
57
79
|
*/
|
|
58
80
|
const createAll = async (api, url, object) => {
|
|
59
81
|
return await api.post(`/${url}/createAll`, object)
|
|
@@ -62,68 +84,78 @@ const createAll = async (api, url, object) => {
|
|
|
62
84
|
};
|
|
63
85
|
exports.createAll = createAll;
|
|
64
86
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* @
|
|
76
|
-
* @param
|
|
77
|
-
* @
|
|
87
|
+
* Retrieves records from the API with optional pagination, search, and sorting support
|
|
88
|
+
*
|
|
89
|
+
* This function delegates to `fetchPage` for paginated requests and handles
|
|
90
|
+
* unpaginated retrieval directly.
|
|
91
|
+
*
|
|
92
|
+
* Behaviors based on parameters:
|
|
93
|
+
* - Without search: retrieves all records
|
|
94
|
+
* - With page/size: paginated search
|
|
95
|
+
* - With sort: paginated and sorted search
|
|
96
|
+
*
|
|
97
|
+
* @template T - Type of individual items in the response
|
|
98
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
99
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
100
|
+
* @param {Search} [search] - Optional search, pagination and sorting parameters
|
|
101
|
+
* @param {number} [search.page] - Page number (zero-indexed)
|
|
102
|
+
* @param {number} [search.size] - Number of items per page
|
|
103
|
+
* @param {string} [search.value] - Search term for filtering
|
|
104
|
+
* @param {Object} [search.sort] - Sorting configuration
|
|
105
|
+
* @param {string} [search.sort.key] - Field name to sort by
|
|
106
|
+
* @param {'asc'|'desc'} [search.sort.order] - Sort order
|
|
107
|
+
* @param {AbortSignal} [signal] - Signal for request cancellation
|
|
108
|
+
* @returns {Promise<T[] | Page<T> | ErrorMessage[]>} Promise resolving to:
|
|
109
|
+
* - Array of items (unpaginated)
|
|
110
|
+
* - Page object (paginated)
|
|
111
|
+
* - Array of errors (on failure)
|
|
112
|
+
*
|
|
113
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
78
114
|
*
|
|
79
115
|
* @example
|
|
80
116
|
* ```typescript
|
|
81
|
-
* //
|
|
82
|
-
* const
|
|
117
|
+
* // Simple search
|
|
118
|
+
* const allUsers = await retrieve(api, 'users')
|
|
83
119
|
*
|
|
84
|
-
* //
|
|
120
|
+
* // Paginated search
|
|
85
121
|
* const page = await retrieve(api, 'users', { page: 0, size: 10 })
|
|
86
122
|
*
|
|
87
|
-
* //
|
|
123
|
+
* // Search with filter + pagination + sorting
|
|
88
124
|
* const filtered = await retrieve(api, 'users', {
|
|
89
|
-
* value: '
|
|
125
|
+
* value: 'John',
|
|
90
126
|
* page: 0,
|
|
91
127
|
* size: 10,
|
|
92
128
|
* sort: { key: 'name', order: 'ASC' }
|
|
93
129
|
* })
|
|
130
|
+
*
|
|
131
|
+
* // With cancellation support
|
|
132
|
+
* const controller = new AbortController()
|
|
133
|
+
* const promise = retrieve(api, 'posts', { page: 0, size: 15 }, controller.signal)
|
|
94
134
|
* ```
|
|
95
135
|
*/
|
|
96
136
|
const retrieve = async (api, url, search, signal) => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return await api.get(`/${url}`)
|
|
100
|
-
.then(response => { return response.data; })
|
|
101
|
-
.catch(error => { return addError(error); });
|
|
137
|
+
try {
|
|
138
|
+
return await (0, api_1.fetchPage)(api, url, search, signal);
|
|
102
139
|
}
|
|
103
|
-
|
|
104
|
-
return
|
|
105
|
-
.then(response => { return response.data; })
|
|
106
|
-
.catch(error => { return addError(error); });
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
return await api.get(`/${url}?value=${search === null || search === void 0 ? void 0 : search.value}`, { params: { page: search === null || search === void 0 ? void 0 : search.page, size: search === null || search === void 0 ? void 0 : search.size, sort: `${(_b = search === null || search === void 0 ? void 0 : search.sort) === null || _b === void 0 ? void 0 : _b.key},${(_c = search === null || search === void 0 ? void 0 : search.sort) === null || _c === void 0 ? void 0 : _c.order}` }, signal })
|
|
110
|
-
.then(response => { return response.data; })
|
|
111
|
-
.catch(error => { return addError(error); });
|
|
140
|
+
catch (error) {
|
|
141
|
+
return addError(error);
|
|
112
142
|
}
|
|
113
143
|
};
|
|
114
144
|
exports.retrieve = retrieve;
|
|
115
145
|
/**
|
|
116
|
-
*
|
|
146
|
+
* Updates an existing record in the API via PUT request
|
|
147
|
+
*
|
|
148
|
+
* @template T - Type of object to be updated
|
|
149
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
150
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
151
|
+
* @param {T} object - Updated object data (must include identifier)
|
|
152
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to response or array of errors
|
|
117
153
|
*
|
|
118
|
-
* @
|
|
119
|
-
* @param api - Instância do Axios configurada
|
|
120
|
-
* @param url - Endpoint da API (sem barra inicial)
|
|
121
|
-
* @param object - Dados atualizados do objeto
|
|
122
|
-
* @returns Promise com dados atualizados ou array de erros
|
|
154
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
123
155
|
*
|
|
124
156
|
* @example
|
|
125
157
|
* ```typescript
|
|
126
|
-
* const result = await update(api, 'users', { id: 1, name: '
|
|
158
|
+
* const result = await update(api, 'users', { id: 1, name: 'John Silva' })
|
|
127
159
|
* ```
|
|
128
160
|
*/
|
|
129
161
|
const update = async (api, url, object) => {
|
|
@@ -133,13 +165,15 @@ const update = async (api, url, object) => {
|
|
|
133
165
|
};
|
|
134
166
|
exports.update = update;
|
|
135
167
|
/**
|
|
136
|
-
*
|
|
168
|
+
* Removes a specific record from the API via DELETE request
|
|
169
|
+
*
|
|
170
|
+
* @template T - Type of response
|
|
171
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
172
|
+
* @param {string} url - API endpoint path (without leading slash, e.g., 'users')
|
|
173
|
+
* @param {string} id - ID of the record to be removed
|
|
174
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to response or array of errors
|
|
137
175
|
*
|
|
138
|
-
* @
|
|
139
|
-
* @param api - Instância do Axios configurada
|
|
140
|
-
* @param url - Endpoint da API (sem barra inicial)
|
|
141
|
-
* @param id - ID do registro a ser removido
|
|
142
|
-
* @returns Promise com resposta ou array de erros
|
|
176
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
143
177
|
*
|
|
144
178
|
* @example
|
|
145
179
|
* ```typescript
|
|
@@ -153,17 +187,25 @@ const remove = async (api, url, id) => {
|
|
|
153
187
|
};
|
|
154
188
|
exports.remove = remove;
|
|
155
189
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* @template T -
|
|
159
|
-
* @param api -
|
|
160
|
-
* @param url -
|
|
161
|
-
* @param object -
|
|
162
|
-
* @param one -
|
|
163
|
-
* @param two -
|
|
164
|
-
* @param three -
|
|
165
|
-
* @param four -
|
|
166
|
-
* @returns Promise
|
|
190
|
+
* Removes a record with composite key (multiple identifiers) via DELETE request
|
|
191
|
+
*
|
|
192
|
+
* @template T - Type of response
|
|
193
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
194
|
+
* @param {string} url - API endpoint path (without leading slash)
|
|
195
|
+
* @param {Object} object - Object with additional data for deletion (if needed by backend)
|
|
196
|
+
* @param {string} one - First identifier segment
|
|
197
|
+
* @param {string} two - Second identifier segment
|
|
198
|
+
* @param {string} three - Third identifier segment (optional, pass '' if unused)
|
|
199
|
+
* @param {string} four - Fourth identifier segment (optional, pass '' if unused)
|
|
200
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to response or array of errors
|
|
201
|
+
*
|
|
202
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* // Delete with 2-part composite key
|
|
207
|
+
* const result = await removeComposite(api, 'user-roles', {}, 'user-123', 'role-admin', '', '')
|
|
208
|
+
* ```
|
|
167
209
|
*/
|
|
168
210
|
const removeComposite = async (api, url, object, one, two, three, four) => {
|
|
169
211
|
if (three !== '' && four !== '') {
|
|
@@ -179,12 +221,17 @@ const removeComposite = async (api, url, object, one, two, three, four) => {
|
|
|
179
221
|
};
|
|
180
222
|
exports.removeComposite = removeComposite;
|
|
181
223
|
/**
|
|
182
|
-
*
|
|
224
|
+
* Removes all records from a specific endpoint via DELETE request
|
|
225
|
+
*
|
|
226
|
+
* ⚠️ **WARNING**: This is a destructive operation that removes ALL records.
|
|
227
|
+
* Use with extreme caution and ensure this is the intended action.
|
|
228
|
+
*
|
|
229
|
+
* @template T - Type of response
|
|
230
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
231
|
+
* @param {string} url - API endpoint path (without leading slash)
|
|
232
|
+
* @returns {Promise<T | ErrorMessage[]>} Promise resolving to response or array of errors
|
|
183
233
|
*
|
|
184
|
-
* @
|
|
185
|
-
* @param api - Instância do Axios configurada
|
|
186
|
-
* @param url - Endpoint da API (sem barra inicial)
|
|
187
|
-
* @returns Promise com resposta ou array de erros
|
|
234
|
+
* @throws {never} Never throws - all errors are caught and returned as ErrorMessage[]
|
|
188
235
|
*
|
|
189
236
|
* @example
|
|
190
237
|
* ```typescript
|