api-core-lib 16.2.0 → 16.12.133

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.
@@ -0,0 +1,56 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+ var _chunkJAMEOM7Tcjs = require('./chunk-JAMEOM7T.cjs');
6
+
7
+ // src/server.ts
8
+ function createServerApi(apiClient, moduleConfig) {
9
+ return {
10
+ /**
11
+ * Prefetches data for a specific action on the server and populates the cache.
12
+ */
13
+ prefetch: async (actionName, input, options = {}) => {
14
+ const actionConfig = moduleConfig.actions[actionName];
15
+ if (!actionConfig) {
16
+ throw new Error(`Action "${actionName}" not found in module.`);
17
+ }
18
+ const result = await _chunkJAMEOM7Tcjs.callDynamicApi.call(void 0, apiClient, moduleConfig.baseEndpoint, actionConfig, {
19
+ pathParams: options.pathParams,
20
+ body: input
21
+ });
22
+ const cacheKey = _chunkJAMEOM7Tcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: options.pathParams });
23
+ if (result.success) {
24
+ _chunkJAMEOM7Tcjs.globalStateManager.setState(cacheKey, () => ({
25
+ data: result.data,
26
+ error: null,
27
+ loading: false,
28
+ success: true,
29
+ called: true,
30
+ isStale: false,
31
+ rawResponse: result.rawResponse
32
+ }));
33
+ } else {
34
+ _chunkJAMEOM7Tcjs.globalStateManager.setState(cacheKey, () => ({
35
+ data: null,
36
+ error: result.error,
37
+ loading: false,
38
+ success: false,
39
+ called: true,
40
+ isStale: true,
41
+ rawResponse: result.rawResponse
42
+ }));
43
+ }
44
+ return result.data;
45
+ },
46
+ /**
47
+ * Dehydrates the current state in the global manager into a JSON string.
48
+ */
49
+ dehydrate: () => {
50
+ return _chunkJAMEOM7Tcjs.globalStateManager.dehydrate();
51
+ }
52
+ };
53
+ }
54
+
55
+
56
+ exports.createServerApi = createServerApi;
@@ -0,0 +1,25 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { f as ActionConfigModule, A as ApiModuleConfig, I as InputOf } from './apiModule.types-DEskFzOY.cjs';
3
+ import 'react';
4
+
5
+ /**
6
+ * Creates a server-safe interface for pre-fetching data and dehydrating state.
7
+ * Use this in your React Server Components to prepare data for the client.
8
+ * @param apiClient A configured Axios instance.
9
+ * @param moduleConfig The configuration for the module you want to use.
10
+ * @returns An object with `prefetch` and `dehydrate` methods.
11
+ */
12
+ declare function createServerApi<TActions extends Record<string, ActionConfigModule<any, any>>>(apiClient: AxiosInstance, moduleConfig: ApiModuleConfig<TActions>): {
13
+ /**
14
+ * Prefetches data for a specific action on the server and populates the cache.
15
+ */
16
+ prefetch: <TActionName extends keyof TActions>(actionName: TActionName, input?: InputOf<TActions[TActionName]>, options?: {
17
+ pathParams?: Record<string, any>;
18
+ }) => Promise<any>;
19
+ /**
20
+ * Dehydrates the current state in the global manager into a JSON string.
21
+ */
22
+ dehydrate: () => string;
23
+ };
24
+
25
+ export { createServerApi };
@@ -0,0 +1,25 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { f as ActionConfigModule, A as ApiModuleConfig, I as InputOf } from './apiModule.types-DEskFzOY.js';
3
+ import 'react';
4
+
5
+ /**
6
+ * Creates a server-safe interface for pre-fetching data and dehydrating state.
7
+ * Use this in your React Server Components to prepare data for the client.
8
+ * @param apiClient A configured Axios instance.
9
+ * @param moduleConfig The configuration for the module you want to use.
10
+ * @returns An object with `prefetch` and `dehydrate` methods.
11
+ */
12
+ declare function createServerApi<TActions extends Record<string, ActionConfigModule<any, any>>>(apiClient: AxiosInstance, moduleConfig: ApiModuleConfig<TActions>): {
13
+ /**
14
+ * Prefetches data for a specific action on the server and populates the cache.
15
+ */
16
+ prefetch: <TActionName extends keyof TActions>(actionName: TActionName, input?: InputOf<TActions[TActionName]>, options?: {
17
+ pathParams?: Record<string, any>;
18
+ }) => Promise<any>;
19
+ /**
20
+ * Dehydrates the current state in the global manager into a JSON string.
21
+ */
22
+ dehydrate: () => string;
23
+ };
24
+
25
+ export { createServerApi };
package/dist/server.js ADDED
@@ -0,0 +1,56 @@
1
+ import {
2
+ callDynamicApi,
3
+ generateCacheKey,
4
+ globalStateManager
5
+ } from "./chunk-N7HFSKII.js";
6
+
7
+ // src/server.ts
8
+ function createServerApi(apiClient, moduleConfig) {
9
+ return {
10
+ /**
11
+ * Prefetches data for a specific action on the server and populates the cache.
12
+ */
13
+ prefetch: async (actionName, input, options = {}) => {
14
+ const actionConfig = moduleConfig.actions[actionName];
15
+ if (!actionConfig) {
16
+ throw new Error(`Action "${actionName}" not found in module.`);
17
+ }
18
+ const result = await callDynamicApi(apiClient, moduleConfig.baseEndpoint, actionConfig, {
19
+ pathParams: options.pathParams,
20
+ body: input
21
+ });
22
+ const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: options.pathParams });
23
+ if (result.success) {
24
+ globalStateManager.setState(cacheKey, () => ({
25
+ data: result.data,
26
+ error: null,
27
+ loading: false,
28
+ success: true,
29
+ called: true,
30
+ isStale: false,
31
+ rawResponse: result.rawResponse
32
+ }));
33
+ } else {
34
+ globalStateManager.setState(cacheKey, () => ({
35
+ data: null,
36
+ error: result.error,
37
+ loading: false,
38
+ success: false,
39
+ called: true,
40
+ isStale: true,
41
+ rawResponse: result.rawResponse
42
+ }));
43
+ }
44
+ return result.data;
45
+ },
46
+ /**
47
+ * Dehydrates the current state in the global manager into a JSON string.
48
+ */
49
+ dehydrate: () => {
50
+ return globalStateManager.dehydrate();
51
+ }
52
+ };
53
+ }
54
+ export {
55
+ createServerApi
56
+ };
@@ -0,0 +1,90 @@
1
+ import { Dispatch, SetStateAction } from 'react';
2
+ import { R as RequestConfig, j as ApiError, S as StandardResponse, g as ActionOptions } from './apiModule.types-DEskFzOY.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 };
@@ -0,0 +1,90 @@
1
+ import { Dispatch, SetStateAction } from 'react';
2
+ import { R as RequestConfig, j as ApiError, S as StandardResponse, g as ActionOptions } from './apiModule.types-DEskFzOY.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 };
package/package.json CHANGED
@@ -1,47 +1,78 @@
1
1
  {
2
2
  "name": "api-core-lib",
3
- "version": "16.2.0",
3
+ "version": "16.12.133",
4
4
  "description": "A flexible and powerful API client library for modern web applications.",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.cjs"
11
+ },
12
+ "./server": {
13
+ "types": "./dist/server.d.ts",
14
+ "import": "./dist/server.js",
15
+ "require": "./dist/server.cjs"
16
+ },
17
+ "./client": {
18
+ "types": "./dist/client.d.ts",
19
+ "import": "./dist/client.js",
20
+ "require": "./dist/client.cjs"
21
+ }
22
+ },
23
+ "main": "./dist/index.cjs",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "bin": {
27
+ "api-core-generate": "./bin/cli-launcher.cjs"
28
+ },
8
29
  "scripts": {
9
- "build": "tsup && node scripts/obfuscate.js",
30
+ "build": "tsup",
10
31
  "dev": "tsup --watch"
11
32
  },
12
- "keywords": [
13
- "axios",
14
- "api",
15
- "react",
16
- "nextjs",
17
- "typescript",
18
- "state-management"
19
- ],
20
- "author": "Your Name",
21
- "license": "MIT",
22
33
  "peerDependencies": {
23
- "react": ">=17.0.0"
34
+ "react": ">=19.0.0",
35
+ "react-dom": ">=19.0.0"
24
36
  },
25
37
  "dependencies": {
38
+ "@apidevtools/json-schema-ref-parser": "^14.2.0",
39
+ "@apidevtools/swagger-parser": "^12.0.0",
26
40
  "axios": "^1.6.8",
27
41
  "axios-retry": "^4.1.0",
42
+ "chalk": "^4.1.2",
28
43
  "colorette": "^2.0.20",
44
+ "commander": "^9.4.1",
45
+ "dotenv": "^16.6.1",
46
+ "fast-deep-equal": "^3.1.3",
47
+ "fs-extra": "^11.3.1",
48
+ "inquirer": "^12.9.4",
49
+ "json-schema-to-typescript": "^15.0.4",
50
+ "openapi-typescript": "^6.2.4",
51
+ "openapi-typescript-codegen": "^0.29.0",
29
52
  "path": "^0.12.7",
30
- "uuid": "^9.0.1"
53
+ "swagger-parser": "^10.0.3",
54
+ "uuid": "^9.0.1",
55
+ "zod": "^4.1.5"
31
56
  },
32
57
  "devDependencies": {
58
+ "@openapitools/openapi-generator-cli": "^2.23.1",
59
+ "@types/fs-extra": "^11.0.4",
60
+ "@types/inquirer": "^9.0.9",
33
61
  "@types/jest": "^30.0.0",
34
- "@types/node": "^24.2.1",
62
+ "@types/node": "^24.3.0",
35
63
  "@types/react": "^18.3.2",
36
64
  "@types/uuid": "^9.0.8",
37
65
  "javascript-obfuscator": "^4.1.1",
38
66
  "jest": "^30.0.5",
39
- "react": "^18.3.1",
67
+ "openapi-types": "^12.1.3",
68
+ "react": "19.1.0",
69
+ "react-dom": "19.1.0",
40
70
  "ts-jest": "^29.4.1",
41
71
  "tsup": "^8.0.2",
42
72
  "typescript": "^5.4.5"
43
73
  },
44
74
  "files": [
45
- "dist"
75
+ "dist",
76
+ "bin"
46
77
  ]
47
78
  }