@arex95/vue-core 3.0.1 → 3.3.0
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/config/global/callbacksConfig.d.ts +14 -0
- package/dist/config/global/index.d.ts +1 -0
- package/dist/graph/GraphStd.d.ts +160 -0
- package/dist/graph/index.d.ts +1 -0
- package/dist/index.mjs +50 -11
- package/dist/rest/GraphStd.d.ts +160 -0
- package/dist/rest/RestStd.d.ts +3 -2
- package/dist/types/ArexVueCoreOptions.d.ts +10 -0
- package/dist/types/AxiosServiceOptions.d.ts +6 -0
- package/dist/types/GraphStdOptions.d.ts +42 -0
- package/dist/types/RestStdOptions.d.ts +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface CallbacksConfig {
|
|
2
|
+
onRefreshFailed?: () => void;
|
|
3
|
+
onLogout?: () => void;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Configures the lifecycle callbacks for auth events.
|
|
7
|
+
* @param {CallbacksConfig} config - Callbacks to invoke on refresh failure or logout.
|
|
8
|
+
*/
|
|
9
|
+
export declare const configCallbacks: (config: CallbacksConfig) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Returns the configured lifecycle callbacks.
|
|
12
|
+
* @returns {CallbacksConfig}
|
|
13
|
+
*/
|
|
14
|
+
export declare const getCallbacksConfig: () => CallbacksConfig;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Fetcher } from "@/types/Fetcher";
|
|
2
|
+
import { QueryOptions, MutationOptions, GraphQLResponse } from "@/types/GraphStdOptions";
|
|
3
|
+
import { RetryConfig } from "@/utils/retry";
|
|
4
|
+
/**
|
|
5
|
+
* A standardized GraphQL class that provides a generic interface for performing
|
|
6
|
+
* GraphQL queries and mutations. It is designed to be extended directly from your models.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* export class UserGraphQL extends GraphStd {
|
|
11
|
+
* static override endpoint = '/graphql';
|
|
12
|
+
* static fetchFn = createAxiosFetcher(axiosInstance);
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* const users = await UserGraphQL.query<{ users: User[] }>({
|
|
16
|
+
* query: 'query { users { id name email } }'
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class GraphStd {
|
|
21
|
+
/**
|
|
22
|
+
* The GraphQL endpoint. MUST be overridden in subclasses.
|
|
23
|
+
* @example static override endpoint = '/graphql';
|
|
24
|
+
*/
|
|
25
|
+
static endpoint: string;
|
|
26
|
+
/** A record of global headers to be sent with every request. */
|
|
27
|
+
static headers: Record<string, string>;
|
|
28
|
+
/** The function used to make the actual HTTP requests. Optional, defaults to Axios fetcher. */
|
|
29
|
+
static fetchFn?: Fetcher;
|
|
30
|
+
/** Retry configuration for failed requests. Optional. */
|
|
31
|
+
static retryConfig?: RetryConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Validates that the endpoint property is defined.
|
|
34
|
+
* @throws {Error} If endpoint is not defined
|
|
35
|
+
*/
|
|
36
|
+
protected static validateEndpoint(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Gets the fetcher function, using default if not provided.
|
|
39
|
+
* Creates a default Axios fetcher if not configured, allowing lazy initialization.
|
|
40
|
+
* @returns The fetcher function to use
|
|
41
|
+
*/
|
|
42
|
+
private static getFetchFn;
|
|
43
|
+
/**
|
|
44
|
+
* Executes a GraphQL request with optional retry logic.
|
|
45
|
+
* @param config - The fetcher configuration
|
|
46
|
+
* @returns A promise that resolves with the GraphQL response data
|
|
47
|
+
*/
|
|
48
|
+
private static executeGraphQLRequest;
|
|
49
|
+
/**
|
|
50
|
+
* Sets global headers that will be included in all subsequent requests made by this class.
|
|
51
|
+
* @param headers - An object containing the headers to be set
|
|
52
|
+
*/
|
|
53
|
+
static setHeaders(headers: Record<string, string>): void;
|
|
54
|
+
/**
|
|
55
|
+
* Executes a GraphQL query.
|
|
56
|
+
* @template TData The expected data type from the query response
|
|
57
|
+
* @template TVariables The type of variables to pass to the query
|
|
58
|
+
* @param options - Options including query string, variables, and optional endpoint override
|
|
59
|
+
* @returns A promise that resolves with the GraphQL response data
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* interface User {
|
|
64
|
+
* id: string;
|
|
65
|
+
* name: string;
|
|
66
|
+
* email: string;
|
|
67
|
+
* }
|
|
68
|
+
*
|
|
69
|
+
* interface UsersQueryVariables {
|
|
70
|
+
* limit?: number;
|
|
71
|
+
* offset?: number;
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* const response = await UserGraphQL.query<{ users: User[] }, UsersQueryVariables>({
|
|
75
|
+
* query: `
|
|
76
|
+
* query GetUsers($limit: Int, $offset: Int) {
|
|
77
|
+
* users(limit: $limit, offset: $offset) {
|
|
78
|
+
* id
|
|
79
|
+
* name
|
|
80
|
+
* email
|
|
81
|
+
* }
|
|
82
|
+
* }
|
|
83
|
+
* `,
|
|
84
|
+
* variables: { limit: 10, offset: 0 }
|
|
85
|
+
* });
|
|
86
|
+
*
|
|
87
|
+
* // Access data: response.data.users
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
static query<TData = unknown, TVariables extends Record<string, unknown> = Record<string, unknown>>(options: QueryOptions<TVariables>): Promise<GraphQLResponse<TData>>;
|
|
91
|
+
/**
|
|
92
|
+
* Executes a GraphQL mutation.
|
|
93
|
+
* @template TData The expected data type from the mutation response
|
|
94
|
+
* @template TVariables The type of variables to pass to the mutation
|
|
95
|
+
* @param options - Options including mutation string, variables, and optional endpoint override
|
|
96
|
+
* @returns A promise that resolves with the GraphQL response data
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* interface CreateUserInput {
|
|
101
|
+
* name: string;
|
|
102
|
+
* email: string;
|
|
103
|
+
* password: string;
|
|
104
|
+
* }
|
|
105
|
+
*
|
|
106
|
+
* interface CreateUserResponse {
|
|
107
|
+
* createUser: {
|
|
108
|
+
* id: string;
|
|
109
|
+
* name: string;
|
|
110
|
+
* email: string;
|
|
111
|
+
* };
|
|
112
|
+
* }
|
|
113
|
+
*
|
|
114
|
+
* const response = await UserGraphQL.mutation<CreateUserResponse, { input: CreateUserInput }>({
|
|
115
|
+
* mutation: `
|
|
116
|
+
* mutation CreateUser($input: CreateUserInput!) {
|
|
117
|
+
* createUser(input: $input) {
|
|
118
|
+
* id
|
|
119
|
+
* name
|
|
120
|
+
* email
|
|
121
|
+
* }
|
|
122
|
+
* }
|
|
123
|
+
* `,
|
|
124
|
+
* variables: {
|
|
125
|
+
* input: {
|
|
126
|
+
* name: 'John Doe',
|
|
127
|
+
* email: 'john@example.com',
|
|
128
|
+
* password: 'secret123'
|
|
129
|
+
* }
|
|
130
|
+
* }
|
|
131
|
+
* });
|
|
132
|
+
*
|
|
133
|
+
* // Access data: response.data.createUser
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
static mutation<TData = unknown, TVariables extends Record<string, unknown> = Record<string, unknown>>(options: MutationOptions<TVariables>): Promise<GraphQLResponse<TData>>;
|
|
137
|
+
/**
|
|
138
|
+
* Executes a raw GraphQL request with full control over the request body.
|
|
139
|
+
* Useful for advanced use cases like subscriptions (via WebSocket) or custom request formats.
|
|
140
|
+
* @template TData The expected data type from the response
|
|
141
|
+
* @template TVariables The type of variables
|
|
142
|
+
* @param options - Options including query/mutation string, variables, operationName, and optional endpoint
|
|
143
|
+
* @returns A promise that resolves with the GraphQL response data
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const response = await UserGraphQL.rawRequest<{ user: User }>({
|
|
148
|
+
* query: 'query { user(id: "123") { id name } }',
|
|
149
|
+
* variables: {},
|
|
150
|
+
* operationName: 'GetUser'
|
|
151
|
+
* });
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
static rawRequest<TData = unknown, TVariables extends Record<string, unknown> = Record<string, unknown>>(options: {
|
|
155
|
+
query: string;
|
|
156
|
+
variables?: TVariables;
|
|
157
|
+
operationName?: string;
|
|
158
|
+
url?: string;
|
|
159
|
+
}): Promise<GraphQLResponse<TData>>;
|
|
160
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './GraphStd';
|
package/dist/index.mjs
CHANGED
|
@@ -1246,6 +1246,20 @@ function getRefreshTokenPathsConfig() {
|
|
|
1246
1246
|
return refreshTokenPathsConfig;
|
|
1247
1247
|
}
|
|
1248
1248
|
|
|
1249
|
+
let callbacksConfig = {};
|
|
1250
|
+
/**
|
|
1251
|
+
* Configures the lifecycle callbacks for auth events.
|
|
1252
|
+
* @param {CallbacksConfig} config - Callbacks to invoke on refresh failure or logout.
|
|
1253
|
+
*/
|
|
1254
|
+
const configCallbacks = (config) => {
|
|
1255
|
+
callbacksConfig = { ...config };
|
|
1256
|
+
};
|
|
1257
|
+
/**
|
|
1258
|
+
* Returns the configured lifecycle callbacks.
|
|
1259
|
+
* @returns {CallbacksConfig}
|
|
1260
|
+
*/
|
|
1261
|
+
const getCallbacksConfig = () => callbacksConfig;
|
|
1262
|
+
|
|
1249
1263
|
/**
|
|
1250
1264
|
* Removes all stored authentication credentials (access and refresh tokens) from the specified storage locations.
|
|
1251
1265
|
*
|
|
@@ -1340,7 +1354,7 @@ const verifyAuth = async () => {
|
|
|
1340
1354
|
};
|
|
1341
1355
|
const token = await getAuthToken(getAppKey(), sessionPersistence);
|
|
1342
1356
|
if (!token) {
|
|
1343
|
-
return handleAuthError("TOKEN_MISSING: No valid token found");
|
|
1357
|
+
return handleAuthError("TOKEN_MISSING: No valid token found", false);
|
|
1344
1358
|
}
|
|
1345
1359
|
try {
|
|
1346
1360
|
const decoded = jwtDecode(token);
|
|
@@ -1349,7 +1363,7 @@ const verifyAuth = async () => {
|
|
|
1349
1363
|
return handleAuthError("TOKEN_INVALID: Invalid expiration format");
|
|
1350
1364
|
}
|
|
1351
1365
|
if (decoded.exp <= currentTime) {
|
|
1352
|
-
return handleAuthError("TOKEN_EXPIRED: Token is expired");
|
|
1366
|
+
return handleAuthError("TOKEN_EXPIRED: Token is expired", false);
|
|
1353
1367
|
}
|
|
1354
1368
|
return true;
|
|
1355
1369
|
}
|
|
@@ -1775,7 +1789,11 @@ const refreshTokens = async (fetcher) => {
|
|
|
1775
1789
|
catch (error) {
|
|
1776
1790
|
handleError(error);
|
|
1777
1791
|
await cleanCredentials(persistence);
|
|
1778
|
-
|
|
1792
|
+
const { onRefreshFailed } = getCallbacksConfig();
|
|
1793
|
+
if (onRefreshFailed) {
|
|
1794
|
+
onRefreshFailed();
|
|
1795
|
+
}
|
|
1796
|
+
else if (typeof window !== 'undefined') {
|
|
1779
1797
|
window.location.reload();
|
|
1780
1798
|
}
|
|
1781
1799
|
throw error;
|
|
@@ -1853,7 +1871,9 @@ class AxiosService {
|
|
|
1853
1871
|
},
|
|
1854
1872
|
withCredentials: options.withCredentials ?? false,
|
|
1855
1873
|
});
|
|
1856
|
-
|
|
1874
|
+
if (options.setupAuthInterceptors !== false) {
|
|
1875
|
+
this.initializeInterceptors();
|
|
1876
|
+
}
|
|
1857
1877
|
}
|
|
1858
1878
|
processQueue(error, token = null) {
|
|
1859
1879
|
this.failedQueue.forEach((prom) => {
|
|
@@ -1889,6 +1909,9 @@ class AxiosService {
|
|
|
1889
1909
|
return response;
|
|
1890
1910
|
}, async (error) => {
|
|
1891
1911
|
this.activeRequests--;
|
|
1912
|
+
if (typeof window === 'undefined') {
|
|
1913
|
+
return Promise.reject(error);
|
|
1914
|
+
}
|
|
1892
1915
|
const originalRequest = error.config;
|
|
1893
1916
|
const isAuthError = axios.isAxiosError(error) && error.response?.status === 401;
|
|
1894
1917
|
const isRefreshCall = originalRequest?.url === this.refreshTokenUrl;
|
|
@@ -3671,18 +3694,25 @@ class RestStd {
|
|
|
3671
3694
|
* Fetches a list of items from the resource's endpoint.
|
|
3672
3695
|
* @template TResponse The expected response type
|
|
3673
3696
|
* @template TParams The type of query parameters
|
|
3674
|
-
* @
|
|
3697
|
+
* @template TData The type of request body data
|
|
3698
|
+
* @param options - Options including params, data, and optional url override
|
|
3675
3699
|
* @returns A promise that resolves with the response data
|
|
3676
3700
|
*/
|
|
3677
3701
|
static getAll(options = {}) {
|
|
3678
3702
|
this.validateResource();
|
|
3679
|
-
const { params, url } = options;
|
|
3703
|
+
const { params, data, url } = options;
|
|
3680
3704
|
const finalUrl = url || this.resource;
|
|
3705
|
+
const hasData = data !== undefined && data !== null;
|
|
3706
|
+
const headers = { ...this.headers };
|
|
3707
|
+
if (hasData) {
|
|
3708
|
+
headers["Content-Type"] = ContentTypeEnum.JSON;
|
|
3709
|
+
}
|
|
3681
3710
|
const config = {
|
|
3682
3711
|
method: "GET",
|
|
3683
3712
|
url: finalUrl,
|
|
3684
|
-
params,
|
|
3685
|
-
|
|
3713
|
+
params: hasData ? undefined : params,
|
|
3714
|
+
data: hasData ? data : undefined,
|
|
3715
|
+
headers,
|
|
3686
3716
|
};
|
|
3687
3717
|
return this.executeFetch(config);
|
|
3688
3718
|
}
|
|
@@ -4272,7 +4302,11 @@ function useAuth(fetcher) {
|
|
|
4272
4302
|
}
|
|
4273
4303
|
finally {
|
|
4274
4304
|
await cleanCredentials(await getSessionPersistence());
|
|
4275
|
-
|
|
4305
|
+
const { onLogout } = getCallbacksConfig();
|
|
4306
|
+
if (onLogout) {
|
|
4307
|
+
onLogout();
|
|
4308
|
+
}
|
|
4309
|
+
else if (typeof window !== 'undefined') {
|
|
4276
4310
|
window.location.reload();
|
|
4277
4311
|
}
|
|
4278
4312
|
}
|
|
@@ -4399,9 +4433,14 @@ const ArexVueCore = {
|
|
|
4399
4433
|
baseURL: options.axios.baseURL,
|
|
4400
4434
|
headers: options.axios.headers,
|
|
4401
4435
|
timeout: options.axios.timeout,
|
|
4402
|
-
withCredentials: options.axios.withCredentials
|
|
4436
|
+
withCredentials: options.axios.withCredentials,
|
|
4437
|
+
setupAuthInterceptors: options.axios.setupAuthInterceptors,
|
|
4438
|
+
});
|
|
4439
|
+
configCallbacks({
|
|
4440
|
+
onRefreshFailed: options.onRefreshFailed,
|
|
4441
|
+
onLogout: options.onLogout,
|
|
4403
4442
|
});
|
|
4404
4443
|
},
|
|
4405
4444
|
};
|
|
4406
4445
|
|
|
4407
|
-
export { AppTypes, ArchiveTypes, ArexVueCore, AudioTypes, AuthError, AxiosService, BaseError, ContentTypeEnum, DocumentTypes, ERROR_MESSAGES, ERROR_STYLES, ErrorEnum, ErrorMessages, ErrorStyles, ExceptionEnum, FontTypes, ImageTypes, KeyCodeEnum, NetworkError, OtherTypes, RestStd, ScreenBreakpoint, ScreenSize, ServerError, StorageKeyEnum, StorageTypeEnum, TextTypes, ValidationError, VideoTypes, ab2hex, addCustomKeyboardShortcut, addDays, addDoubleClickListener, addKeyListener, ageAtDate, axiosFetch, blobToFormData, bufferToBlob, calculateAge, cleanCredentials, clickOutside, compareObject, configAppKey, configAuthFetcher, configAxios, configEndpoints, configRefreshTokenPaths, configSession, configTokenKeys, configTokenPaths, copyToClipboard, countWords, createAxiosFetcher, createKeyMap, createOfetchFetcher, customShortcut, daysBetween, daysToNextBirthday, debounce, debounceAsync, debounceAsyncValidator, debounceAsyncWithImmediate, debounceLeading, debounceLeadingTrailing, debounceTrailing, decrypt, deepClone, deepEqual, deepMerge, detectKeyHold, disableCopy, disableF12Key, disableMouseButtons, disableRightClick, disableSpecificKeys, downloadBlob, enableMouseButtons, enableRightClick, enableSpecificKeys, encrypt, exportToCSV, exportToExcel, exportToJSON, exportToText, exportToXML, extractAndValidateTokens, filterObjectByKeys, flattenObject, formDataToObject, formatDate, generateRandomString, getAppKey, getAuthRefreshToken, getAuthToken, getConfiguredAxiosInstance, getCookieStorage, getDecryptedItem, getDefaultAuthFetcher, getEndOfMonth, getEndpointsConfig, getObjectDifferences, getObjectKeys, getPreferredStorage, getQueryParam, getRefreshTokenPathsConfig, getSessionConfig, getSessionId, getSessionPersistence, getSessionStorage, getStartOfMonth, getStorage, getTokenConfig, getTokenPathsConfig, handleError, hasNestedProperties, hex2ab, importKey, isClient, isEmptyObject, isLeapYear, isServer, isStrongPassword, isValidAge, isValidCreditCard, isValidDate, isValidEmail, isValidExpiryDate, isValidHexColor, isValidHexColorAlpha, isValidHexNumber, isValidIP, isValidPhoneNumber, isValidSSN, isValidTime, isValidURL, isValidUsername, isValidZIP, lowerFirst, objectToFormData, objectToFormDataEnhanced, objectToQueryString, openWindow, parseDate, proxyToPlainObject, readFileAsDataURL, readFileAsText, refreshTokens, registerKeyboardShortcuts, removeAccent, removeClickOutside, removeCustomKeyboardShortcut, removeCustomShortcuts, removeDoubleClickListener, removeEmptyProperties, removeKeyListeners, replaceAll, retryWithBackoff, reverseString, safeGet, screenMap, scrollToTop, setDefaultAuthFetcherFactory, simulateKeyPress, stopDetectingKeyHold, storeAuthRefreshToken, storeAuthToken, storeEncryptedItem, storeTokens, stringToBlob, subtractDays, throttle, toCamelCase, toKebabCase, toggleTabNavigation, truncateString, unregisterKeyboardShortcuts, upperFirst, useAuth, useBreakpoint, useFetch, useFilter, usePagination, useSorter, validateAlphanumeric, validateLetters, validateNumbers, verifyAuth };
|
|
4446
|
+
export { AppTypes, ArchiveTypes, ArexVueCore, AudioTypes, AuthError, AxiosService, BaseError, ContentTypeEnum, DocumentTypes, ERROR_MESSAGES, ERROR_STYLES, ErrorEnum, ErrorMessages, ErrorStyles, ExceptionEnum, FontTypes, ImageTypes, KeyCodeEnum, NetworkError, OtherTypes, RestStd, ScreenBreakpoint, ScreenSize, ServerError, StorageKeyEnum, StorageTypeEnum, TextTypes, ValidationError, VideoTypes, ab2hex, addCustomKeyboardShortcut, addDays, addDoubleClickListener, addKeyListener, ageAtDate, axiosFetch, blobToFormData, bufferToBlob, calculateAge, cleanCredentials, clickOutside, compareObject, configAppKey, configAuthFetcher, configAxios, configCallbacks, configEndpoints, configRefreshTokenPaths, configSession, configTokenKeys, configTokenPaths, copyToClipboard, countWords, createAxiosFetcher, createKeyMap, createOfetchFetcher, customShortcut, daysBetween, daysToNextBirthday, debounce, debounceAsync, debounceAsyncValidator, debounceAsyncWithImmediate, debounceLeading, debounceLeadingTrailing, debounceTrailing, decrypt, deepClone, deepEqual, deepMerge, detectKeyHold, disableCopy, disableF12Key, disableMouseButtons, disableRightClick, disableSpecificKeys, downloadBlob, enableMouseButtons, enableRightClick, enableSpecificKeys, encrypt, exportToCSV, exportToExcel, exportToJSON, exportToText, exportToXML, extractAndValidateTokens, filterObjectByKeys, flattenObject, formDataToObject, formatDate, generateRandomString, getAppKey, getAuthRefreshToken, getAuthToken, getCallbacksConfig, getConfiguredAxiosInstance, getCookieStorage, getDecryptedItem, getDefaultAuthFetcher, getEndOfMonth, getEndpointsConfig, getObjectDifferences, getObjectKeys, getPreferredStorage, getQueryParam, getRefreshTokenPathsConfig, getSessionConfig, getSessionId, getSessionPersistence, getSessionStorage, getStartOfMonth, getStorage, getTokenConfig, getTokenPathsConfig, handleError, hasNestedProperties, hex2ab, importKey, isClient, isEmptyObject, isLeapYear, isServer, isStrongPassword, isValidAge, isValidCreditCard, isValidDate, isValidEmail, isValidExpiryDate, isValidHexColor, isValidHexColorAlpha, isValidHexNumber, isValidIP, isValidPhoneNumber, isValidSSN, isValidTime, isValidURL, isValidUsername, isValidZIP, lowerFirst, objectToFormData, objectToFormDataEnhanced, objectToQueryString, openWindow, parseDate, proxyToPlainObject, readFileAsDataURL, readFileAsText, refreshTokens, registerKeyboardShortcuts, removeAccent, removeClickOutside, removeCustomKeyboardShortcut, removeCustomShortcuts, removeDoubleClickListener, removeEmptyProperties, removeKeyListeners, replaceAll, retryWithBackoff, reverseString, safeGet, screenMap, scrollToTop, setDefaultAuthFetcherFactory, simulateKeyPress, stopDetectingKeyHold, storeAuthRefreshToken, storeAuthToken, storeEncryptedItem, storeTokens, stringToBlob, subtractDays, throttle, toCamelCase, toKebabCase, toggleTabNavigation, truncateString, unregisterKeyboardShortcuts, upperFirst, useAuth, useBreakpoint, useFetch, useFilter, usePagination, useSorter, validateAlphanumeric, validateLetters, validateNumbers, verifyAuth };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Fetcher } from "@/types/Fetcher";
|
|
2
|
+
import { QueryOptions, MutationOptions, GraphQLResponse } from "@/types/GraphStdOptions";
|
|
3
|
+
import { RetryConfig } from "@/utils/retry";
|
|
4
|
+
/**
|
|
5
|
+
* A standardized GraphQL class that provides a generic interface for performing
|
|
6
|
+
* GraphQL queries and mutations. It is designed to be extended directly from your models.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* export class UserGraphQL extends GraphStd {
|
|
11
|
+
* static override endpoint = '/graphql';
|
|
12
|
+
* static fetchFn = createAxiosFetcher(axiosInstance);
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* const users = await UserGraphQL.query<{ users: User[] }>({
|
|
16
|
+
* query: 'query { users { id name email } }'
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class GraphStd {
|
|
21
|
+
/**
|
|
22
|
+
* The GraphQL endpoint. MUST be overridden in subclasses.
|
|
23
|
+
* @example static override endpoint = '/graphql';
|
|
24
|
+
*/
|
|
25
|
+
static endpoint: string;
|
|
26
|
+
/** A record of global headers to be sent with every request. */
|
|
27
|
+
static headers: Record<string, string>;
|
|
28
|
+
/** The function used to make the actual HTTP requests. Optional, defaults to Axios fetcher. */
|
|
29
|
+
static fetchFn?: Fetcher;
|
|
30
|
+
/** Retry configuration for failed requests. Optional. */
|
|
31
|
+
static retryConfig?: RetryConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Validates that the endpoint property is defined.
|
|
34
|
+
* @throws {Error} If endpoint is not defined
|
|
35
|
+
*/
|
|
36
|
+
protected static validateEndpoint(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Gets the fetcher function, using default if not provided.
|
|
39
|
+
* Creates a default Axios fetcher if not configured, allowing lazy initialization.
|
|
40
|
+
* @returns The fetcher function to use
|
|
41
|
+
*/
|
|
42
|
+
private static getFetchFn;
|
|
43
|
+
/**
|
|
44
|
+
* Executes a GraphQL request with optional retry logic.
|
|
45
|
+
* @param config - The fetcher configuration
|
|
46
|
+
* @returns A promise that resolves with the GraphQL response data
|
|
47
|
+
*/
|
|
48
|
+
private static executeGraphQLRequest;
|
|
49
|
+
/**
|
|
50
|
+
* Sets global headers that will be included in all subsequent requests made by this class.
|
|
51
|
+
* @param headers - An object containing the headers to be set
|
|
52
|
+
*/
|
|
53
|
+
static setHeaders(headers: Record<string, string>): void;
|
|
54
|
+
/**
|
|
55
|
+
* Executes a GraphQL query.
|
|
56
|
+
* @template TData The expected data type from the query response
|
|
57
|
+
* @template TVariables The type of variables to pass to the query
|
|
58
|
+
* @param options - Options including query string, variables, and optional endpoint override
|
|
59
|
+
* @returns A promise that resolves with the GraphQL response data
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* interface User {
|
|
64
|
+
* id: string;
|
|
65
|
+
* name: string;
|
|
66
|
+
* email: string;
|
|
67
|
+
* }
|
|
68
|
+
*
|
|
69
|
+
* interface UsersQueryVariables {
|
|
70
|
+
* limit?: number;
|
|
71
|
+
* offset?: number;
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* const response = await UserGraphQL.query<{ users: User[] }, UsersQueryVariables>({
|
|
75
|
+
* query: `
|
|
76
|
+
* query GetUsers($limit: Int, $offset: Int) {
|
|
77
|
+
* users(limit: $limit, offset: $offset) {
|
|
78
|
+
* id
|
|
79
|
+
* name
|
|
80
|
+
* email
|
|
81
|
+
* }
|
|
82
|
+
* }
|
|
83
|
+
* `,
|
|
84
|
+
* variables: { limit: 10, offset: 0 }
|
|
85
|
+
* });
|
|
86
|
+
*
|
|
87
|
+
* // Access data: response.data.users
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
static query<TData = unknown, TVariables extends Record<string, unknown> = Record<string, unknown>>(options: QueryOptions<TVariables>): Promise<GraphQLResponse<TData>>;
|
|
91
|
+
/**
|
|
92
|
+
* Executes a GraphQL mutation.
|
|
93
|
+
* @template TData The expected data type from the mutation response
|
|
94
|
+
* @template TVariables The type of variables to pass to the mutation
|
|
95
|
+
* @param options - Options including mutation string, variables, and optional endpoint override
|
|
96
|
+
* @returns A promise that resolves with the GraphQL response data
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* interface CreateUserInput {
|
|
101
|
+
* name: string;
|
|
102
|
+
* email: string;
|
|
103
|
+
* password: string;
|
|
104
|
+
* }
|
|
105
|
+
*
|
|
106
|
+
* interface CreateUserResponse {
|
|
107
|
+
* createUser: {
|
|
108
|
+
* id: string;
|
|
109
|
+
* name: string;
|
|
110
|
+
* email: string;
|
|
111
|
+
* };
|
|
112
|
+
* }
|
|
113
|
+
*
|
|
114
|
+
* const response = await UserGraphQL.mutation<CreateUserResponse, { input: CreateUserInput }>({
|
|
115
|
+
* mutation: `
|
|
116
|
+
* mutation CreateUser($input: CreateUserInput!) {
|
|
117
|
+
* createUser(input: $input) {
|
|
118
|
+
* id
|
|
119
|
+
* name
|
|
120
|
+
* email
|
|
121
|
+
* }
|
|
122
|
+
* }
|
|
123
|
+
* `,
|
|
124
|
+
* variables: {
|
|
125
|
+
* input: {
|
|
126
|
+
* name: 'John Doe',
|
|
127
|
+
* email: 'john@example.com',
|
|
128
|
+
* password: 'secret123'
|
|
129
|
+
* }
|
|
130
|
+
* }
|
|
131
|
+
* });
|
|
132
|
+
*
|
|
133
|
+
* // Access data: response.data.createUser
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
static mutation<TData = unknown, TVariables extends Record<string, unknown> = Record<string, unknown>>(options: MutationOptions<TVariables>): Promise<GraphQLResponse<TData>>;
|
|
137
|
+
/**
|
|
138
|
+
* Executes a raw GraphQL request with full control over the request body.
|
|
139
|
+
* Useful for advanced use cases like subscriptions (via WebSocket) or custom request formats.
|
|
140
|
+
* @template TData The expected data type from the response
|
|
141
|
+
* @template TVariables The type of variables
|
|
142
|
+
* @param options - Options including query/mutation string, variables, operationName, and optional endpoint
|
|
143
|
+
* @returns A promise that resolves with the GraphQL response data
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const response = await UserGraphQL.rawRequest<{ user: User }>({
|
|
148
|
+
* query: 'query { user(id: "123") { id name } }',
|
|
149
|
+
* variables: {},
|
|
150
|
+
* operationName: 'GetUser'
|
|
151
|
+
* });
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
static rawRequest<TData = unknown, TVariables extends Record<string, unknown> = Record<string, unknown>>(options: {
|
|
155
|
+
query: string;
|
|
156
|
+
variables?: TVariables;
|
|
157
|
+
operationName?: string;
|
|
158
|
+
url?: string;
|
|
159
|
+
}): Promise<GraphQLResponse<TData>>;
|
|
160
|
+
}
|
package/dist/rest/RestStd.d.ts
CHANGED
|
@@ -69,10 +69,11 @@ export declare class RestStd {
|
|
|
69
69
|
* Fetches a list of items from the resource's endpoint.
|
|
70
70
|
* @template TResponse The expected response type
|
|
71
71
|
* @template TParams The type of query parameters
|
|
72
|
-
* @
|
|
72
|
+
* @template TData The type of request body data
|
|
73
|
+
* @param options - Options including params, data, and optional url override
|
|
73
74
|
* @returns A promise that resolves with the response data
|
|
74
75
|
*/
|
|
75
|
-
static getAll<TResponse = unknown, TParams extends Record<string, unknown> = Record<string, unknown
|
|
76
|
+
static getAll<TResponse = unknown, TParams extends Record<string, unknown> = Record<string, unknown>, TData = unknown>(options?: GetAllOptions<TParams, TData>): Promise<TResponse>;
|
|
76
77
|
/**
|
|
77
78
|
* Fetches a single item by its ID.
|
|
78
79
|
* @template TResponse The expected response type
|
|
@@ -39,4 +39,14 @@ export interface ArexVueCoreOptions {
|
|
|
39
39
|
};
|
|
40
40
|
/** The configuration options for the underlying Axios instance. */
|
|
41
41
|
axios: AxiosServiceOptions;
|
|
42
|
+
/**
|
|
43
|
+
* Called when a token refresh attempt fails (e.g., to redirect to login via Vue Router).
|
|
44
|
+
* Falls back to `window.location.reload()` if not provided.
|
|
45
|
+
*/
|
|
46
|
+
onRefreshFailed?: () => void;
|
|
47
|
+
/**
|
|
48
|
+
* Called after a successful logout (e.g., to redirect to login via Vue Router).
|
|
49
|
+
* Falls back to `window.location.reload()` if not provided.
|
|
50
|
+
*/
|
|
51
|
+
onLogout?: () => void;
|
|
42
52
|
}
|
|
@@ -12,4 +12,10 @@ export interface AxiosServiceOptions {
|
|
|
12
12
|
timeout?: number;
|
|
13
13
|
/** A boolean indicating whether cross-site Access-Control requests should be made using credentials. */
|
|
14
14
|
withCredentials?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Whether to mount the authentication interceptors (token attachment + 401 refresh).
|
|
17
|
+
* Set to `false` in SSR environments where browser storage is unavailable.
|
|
18
|
+
* Defaults to `true`.
|
|
19
|
+
*/
|
|
20
|
+
setupAuthInterceptors?: boolean;
|
|
15
21
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for GraphQL query operations.
|
|
3
|
+
* @template TVariables The type of variables to pass to the query
|
|
4
|
+
*/
|
|
5
|
+
export interface QueryOptions<TVariables extends Record<string, unknown> = Record<string, unknown>> {
|
|
6
|
+
/** The GraphQL query string */
|
|
7
|
+
query: string;
|
|
8
|
+
/** Variables to pass to the query */
|
|
9
|
+
variables?: TVariables;
|
|
10
|
+
/** Operation name (useful when multiple operations are in the query) */
|
|
11
|
+
operationName?: string;
|
|
12
|
+
/** Optional custom endpoint URL (overrides the default endpoint) */
|
|
13
|
+
url?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Options for GraphQL mutation operations.
|
|
17
|
+
* @template TVariables The type of variables to pass to the mutation
|
|
18
|
+
*/
|
|
19
|
+
export interface MutationOptions<TVariables extends Record<string, unknown> = Record<string, unknown>> {
|
|
20
|
+
/** The GraphQL mutation string */
|
|
21
|
+
mutation: string;
|
|
22
|
+
/** Variables to pass to the mutation */
|
|
23
|
+
variables?: TVariables;
|
|
24
|
+
/** Operation name (useful when multiple operations are in the mutation) */
|
|
25
|
+
operationName?: string;
|
|
26
|
+
/** Optional custom endpoint URL (overrides the default endpoint) */
|
|
27
|
+
url?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Standard GraphQL response structure.
|
|
31
|
+
* @template TData The type of the data returned by the query/mutation
|
|
32
|
+
*/
|
|
33
|
+
export interface GraphQLResponse<TData = unknown> {
|
|
34
|
+
/** The data returned by the GraphQL operation */
|
|
35
|
+
data?: TData;
|
|
36
|
+
/** Array of errors if the operation failed */
|
|
37
|
+
errors?: Array<{
|
|
38
|
+
message: string;
|
|
39
|
+
path?: (string | number)[];
|
|
40
|
+
extensions?: Record<string, unknown>;
|
|
41
|
+
}>;
|
|
42
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export interface GetAllOptions<TParams extends Record<string, unknown> = Record<string, unknown
|
|
1
|
+
export interface GetAllOptions<TParams extends Record<string, unknown> = Record<string, unknown>, TData = unknown> {
|
|
2
2
|
params?: TParams;
|
|
3
|
+
data?: TData;
|
|
3
4
|
options?: Record<string, unknown>;
|
|
4
5
|
url?: string;
|
|
5
6
|
}
|