@arex95/vue-core 3.0.0 → 3.1.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.
@@ -1,4 +1,4 @@
1
- import { AxiosServiceOptions } from "@/types/AxiosServiceOptions";
1
+ import { AxiosServiceOptions } from "../../types/AxiosServiceOptions";
2
2
  import { AxiosInstance } from "axios";
3
3
  /**
4
4
  * Configures the singleton Axios service instance for the application.
@@ -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
@@ -1994,9 +1994,8 @@ const configAxios = (config) => {
1994
1994
  timeout: config.timeout,
1995
1995
  withCredentials: config.withCredentials
1996
1996
  });
1997
- // Configure auth fetcher factory to avoid circular dependency
1998
- const { setDefaultAuthFetcherFactory } = require("@/config/auth/authFetcher");
1999
- const { createAxiosFetcher } = require("@/fetchers/axios");
1997
+ // Configure auth fetcher factory lazily to avoid circular dependency
1998
+ // The factory function is only called when getDefaultAuthFetcher() is invoked
2000
1999
  setDefaultAuthFetcherFactory(() => {
2001
2000
  return createAxiosFetcher(axiosServiceInstance.getAxiosInstance());
2002
2001
  });
@@ -2026,9 +2025,8 @@ const getConfiguredAxiosInstance = () => {
2026
2025
  withCredentials: false
2027
2026
  });
2028
2027
  }
2029
- // Configure auth fetcher factory to avoid circular dependency
2030
- const { setDefaultAuthFetcherFactory } = require("@/config/auth/authFetcher");
2031
- const { createAxiosFetcher } = require("@/fetchers/axios");
2028
+ // Configure auth fetcher factory lazily to avoid circular dependency
2029
+ // The factory function is only called when getDefaultAuthFetcher() is invoked
2032
2030
  setDefaultAuthFetcherFactory(() => {
2033
2031
  return createAxiosFetcher(axiosServiceInstance.getAxiosInstance());
2034
2032
  });
@@ -3673,18 +3671,25 @@ class RestStd {
3673
3671
  * Fetches a list of items from the resource's endpoint.
3674
3672
  * @template TResponse The expected response type
3675
3673
  * @template TParams The type of query parameters
3676
- * @param options - Options including params, options, and optional url override
3674
+ * @template TData The type of request body data
3675
+ * @param options - Options including params, data, and optional url override
3677
3676
  * @returns A promise that resolves with the response data
3678
3677
  */
3679
3678
  static getAll(options = {}) {
3680
3679
  this.validateResource();
3681
- const { params, url } = options;
3680
+ const { params, data, url } = options;
3682
3681
  const finalUrl = url || this.resource;
3682
+ const hasData = data !== undefined && data !== null;
3683
+ const headers = { ...this.headers };
3684
+ if (hasData) {
3685
+ headers["Content-Type"] = ContentTypeEnum.JSON;
3686
+ }
3683
3687
  const config = {
3684
3688
  method: "GET",
3685
3689
  url: finalUrl,
3686
- params,
3687
- headers: this.headers,
3690
+ params: hasData ? undefined : params,
3691
+ data: hasData ? data : undefined,
3692
+ headers,
3688
3693
  };
3689
3694
  return this.executeFetch(config);
3690
3695
  }
@@ -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
+ }
@@ -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
- * @param options - Options including params, options, and optional url override
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>>(options?: GetAllOptions<TParams>): Promise<TResponse>;
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
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arex95/vue-core",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Opinionated Vue Core",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",