api-core-lib 12.0.27 → 12.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{apiModule.types-Bg7m0Xoy.d.cts → apiModule.types-Bn0tJF7b.d.cts} +1 -1
- package/dist/{apiModule.types-Bg7m0Xoy.d.ts → apiModule.types-Bn0tJF7b.d.ts} +1 -1
- package/dist/{chunk-NCMDUWNO.cjs → chunk-25UFVV4F.cjs} +4 -11
- package/dist/{chunk-UWAVU6EF.js → chunk-KPZ7BF52.js} +1 -8
- package/dist/cli.cjs +129 -0
- package/dist/client.cjs +20 -22
- package/dist/client.d.cts +2 -2
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -3
- package/dist/index.cjs +9 -15
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -9
- package/dist/server.cjs +6 -8
- package/dist/server.d.cts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -3
- package/dist/{useApiRecord.types-BsemqlQl.d.ts → useApiRecord.types-CWhwCAB6.d.ts} +1 -1
- package/dist/{useApiRecord.types-CT4BDrvj.d.cts → useApiRecord.types-NZtPRga3.d.cts} +1 -1
- package/package.json +2 -2
- package/dist/chunk-NRV3EMIJ.js +0 -8
- package/dist/chunk-QOOQGLEX.cjs +0 -8
- package/dist/cli.d.cts +0 -1
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +0 -107
|
@@ -287,4 +287,4 @@ type ActionMethods<TAction extends ActionConfigModule<any, any>> = {
|
|
|
287
287
|
}) => void;
|
|
288
288
|
};
|
|
289
289
|
|
|
290
|
-
export type {
|
|
290
|
+
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, ModuleStates as M, OutputOf as O, PaginationMeta as P, QueryOptions as Q, RequestConfig as R, StandardResponse as S, Tokens as T, UseApiConfig as U, ValidationError as V, ApiModuleConfig as a, UseApiModuleOptions as b, UseApiModuleReturn as c, ModuleActions as d, UseApiQuery as e, ApiClientConfig as f, ActionOptions as g, ActionStateModule as h, ApiError as i, TokenManager as j, MiddlewareContext as k, Middleware as l, RefreshTokenConfig as m, ActionConfig as n, UseApiState as o, ActionState as p, ExecuteOptions as q, ActionsWithQuery as r, ActionMethods as s, t };
|
|
@@ -287,4 +287,4 @@ type ActionMethods<TAction extends ActionConfigModule<any, any>> = {
|
|
|
287
287
|
}) => void;
|
|
288
288
|
};
|
|
289
289
|
|
|
290
|
-
export type {
|
|
290
|
+
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, ModuleStates as M, OutputOf as O, PaginationMeta as P, QueryOptions as Q, RequestConfig as R, StandardResponse as S, Tokens as T, UseApiConfig as U, ValidationError as V, ApiModuleConfig as a, UseApiModuleOptions as b, UseApiModuleReturn as c, ModuleActions as d, UseApiQuery as e, ApiClientConfig as f, ActionOptions as g, ActionStateModule as h, ApiError as i, TokenManager as j, MiddlewareContext as k, Middleware as l, RefreshTokenConfig as m, ActionConfig as n, UseApiState as o, ActionState as p, ExecuteOptions as q, ActionsWithQuery as r, ActionMethods as s, t };
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
3
|
-
|
|
4
|
-
var _chunkQOOQGLEXcjs = require('./chunk-QOOQGLEX.cjs');
|
|
5
|
-
|
|
6
|
-
// src/core/utils.ts
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;// src/core/utils.ts
|
|
7
2
|
var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios);
|
|
8
3
|
function isAxiosResponse(obj) {
|
|
9
4
|
return obj && obj.data !== void 0 && obj.status !== void 0 && obj.config !== void 0;
|
|
@@ -273,10 +268,8 @@ var createInitialState = () => ({
|
|
|
273
268
|
validationErrors: [],
|
|
274
269
|
rawResponse: null
|
|
275
270
|
});
|
|
276
|
-
var GlobalStateManager = class {
|
|
277
|
-
|
|
278
|
-
_chunkQOOQGLEXcjs.__publicField.call(void 0, this, "store", /* @__PURE__ */ new Map());
|
|
279
|
-
}
|
|
271
|
+
var GlobalStateManager = (_class = class {constructor() { _class.prototype.__init.call(this); }
|
|
272
|
+
__init() {this.store = /* @__PURE__ */ new Map()}
|
|
280
273
|
getSnapshot(key) {
|
|
281
274
|
if (!this.store.has(key)) {
|
|
282
275
|
const initialState = createInitialState();
|
|
@@ -364,7 +357,7 @@ var GlobalStateManager = class {
|
|
|
364
357
|
console.error("[api-core-lib] Failed to rehydrate state:", e);
|
|
365
358
|
}
|
|
366
359
|
}
|
|
367
|
-
};
|
|
360
|
+
}, _class);
|
|
368
361
|
var globalStateManager = new GlobalStateManager();
|
|
369
362
|
|
|
370
363
|
// src/core/cacheKey.ts
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
__publicField
|
|
4
|
-
} from "./chunk-NRV3EMIJ.js";
|
|
5
|
-
|
|
6
1
|
// src/core/utils.ts
|
|
7
2
|
import axios from "axios";
|
|
8
3
|
function isAxiosResponse(obj) {
|
|
@@ -274,9 +269,7 @@ var createInitialState = () => ({
|
|
|
274
269
|
rawResponse: null
|
|
275
270
|
});
|
|
276
271
|
var GlobalStateManager = class {
|
|
277
|
-
|
|
278
|
-
__publicField(this, "store", /* @__PURE__ */ new Map());
|
|
279
|
-
}
|
|
272
|
+
store = /* @__PURE__ */ new Map();
|
|
280
273
|
getSnapshot(key) {
|
|
281
274
|
if (!this.store.has(key)) {
|
|
282
275
|
const initialState = createInitialState();
|
package/dist/cli.cjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
#!/usr/bin/env node
|
|
3
|
+
"use strict";
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
|
|
27
|
+
// src/cli.ts
|
|
28
|
+
var import_commander = require("commander");
|
|
29
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
30
|
+
|
|
31
|
+
// src/generator/index.ts
|
|
32
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
33
|
+
var import_path = __toESM(require("path"), 1);
|
|
34
|
+
var import_axios = __toESM(require("axios"), 1);
|
|
35
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
36
|
+
var import_dotenv = __toESM(require("dotenv"), 1);
|
|
37
|
+
var import_openapi_typescript = __toESM(require("openapi-typescript"), 1);
|
|
38
|
+
|
|
39
|
+
// src/generator/module-parser.ts
|
|
40
|
+
function parseSpecToModules(spec) {
|
|
41
|
+
const modules = {};
|
|
42
|
+
for (const apiPath in spec.paths) {
|
|
43
|
+
for (const method in spec.paths[apiPath]) {
|
|
44
|
+
const endpoint = spec.paths[apiPath][method];
|
|
45
|
+
if (!endpoint.tags || endpoint.tags.length === 0) continue;
|
|
46
|
+
const tagName = endpoint.tags[0];
|
|
47
|
+
const moduleName = tagName.replace(/[^a-zA-Z0-9]/g, "").replace(/Central|Tenant/g, "") + "Api";
|
|
48
|
+
if (!modules[moduleName]) {
|
|
49
|
+
modules[moduleName] = {
|
|
50
|
+
baseEndpoint: "",
|
|
51
|
+
// سيتم ملء المسار الكامل في path
|
|
52
|
+
actions: {}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const actionName = endpoint.operationId || method + apiPath.replace(/[\/{}]+/g, "_");
|
|
56
|
+
modules[moduleName].actions[actionName] = {
|
|
57
|
+
method: method.toUpperCase(),
|
|
58
|
+
path: apiPath,
|
|
59
|
+
description: endpoint.summary || "No description available.",
|
|
60
|
+
hasQuery: (endpoint.parameters || []).some((p) => p.in === "query"),
|
|
61
|
+
autoFetch: method.toUpperCase() === "GET" && !apiPath.includes("{"),
|
|
62
|
+
// افتراض ذكي
|
|
63
|
+
invalidates: []
|
|
64
|
+
// يمكن تركه فارغًا
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return modules;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/generator/index.ts
|
|
72
|
+
async function runGenerator(options) {
|
|
73
|
+
console.log(import_chalk.default.cyan(`Starting API module generation...`));
|
|
74
|
+
console.log(import_chalk.default.gray(`Output directory: ${options.output}`));
|
|
75
|
+
console.log(import_chalk.default.gray(`.env path: ${options.envPath}`));
|
|
76
|
+
import_dotenv.default.config({ path: options.envPath });
|
|
77
|
+
const apiUrl = process.env.NEXT_PUBLIC_API_URL || process.env.API_URL;
|
|
78
|
+
if (!apiUrl) {
|
|
79
|
+
console.error(import_chalk.default.red("Error: API_URL or NEXT_PUBLIC_API_URL not found in .env file."));
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const specUrl = `${apiUrl}/api-docs/json`;
|
|
84
|
+
console.log(`Fetching OpenAPI spec from ${specUrl}...`);
|
|
85
|
+
const response = await import_axios.default.get(specUrl);
|
|
86
|
+
const spec = response.data;
|
|
87
|
+
if (!import_fs.default.existsSync(options.output)) {
|
|
88
|
+
import_fs.default.mkdirSync(options.output, { recursive: true });
|
|
89
|
+
}
|
|
90
|
+
console.log("Generating TypeScript types...");
|
|
91
|
+
const typesContent = await (0, import_openapi_typescript.default)(spec);
|
|
92
|
+
const typesPath = import_path.default.join(options.output, "api-types.generated.ts");
|
|
93
|
+
import_fs.default.writeFileSync(typesPath, typesContent);
|
|
94
|
+
console.log(import_chalk.default.green(`\u2713 Types generated successfully at ${typesPath}`));
|
|
95
|
+
console.log("Generating API modules...");
|
|
96
|
+
const modules = parseSpecToModules(spec);
|
|
97
|
+
for (const moduleName in modules) {
|
|
98
|
+
const moduleConfig = modules[moduleName];
|
|
99
|
+
const fileName = `${moduleName}.module.ts`;
|
|
100
|
+
const filePath = import_path.default.join(options.output, fileName);
|
|
101
|
+
const fileContent = `
|
|
102
|
+
// This file is auto-generated by api-core-lib.
|
|
103
|
+
// Do not edit this file directly.
|
|
104
|
+
|
|
105
|
+
import type { ApiModuleConfig } from 'api-core-lib';
|
|
106
|
+
import type { paths } from './api-types.generated'; // \u0631\u0628\u0637 \u0627\u0644\u0623\u0646\u0648\u0627\u0639 \u0627\u0644\u0645\u0648\u0644\u062F\u0629
|
|
107
|
+
|
|
108
|
+
// A more advanced generator could automatically map the correct input/output types.
|
|
109
|
+
// For now, we use 'any' as a placeholder.
|
|
110
|
+
export const ${moduleName}Module: ApiModuleConfig<any> = ${JSON.stringify(moduleConfig, null, 2)};
|
|
111
|
+
`;
|
|
112
|
+
import_fs.default.writeFileSync(filePath, fileContent);
|
|
113
|
+
console.log(import_chalk.default.green(`\u2713 Module generated: ${fileName}`));
|
|
114
|
+
}
|
|
115
|
+
console.log(import_chalk.default.bold.green("\n\u{1F389} API generation complete!"));
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error(import_chalk.default.red("\nAn error occurred during generation:"));
|
|
118
|
+
console.error(error.message);
|
|
119
|
+
process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/cli.ts
|
|
124
|
+
console.log("API Core Lib - Code Generator");
|
|
125
|
+
import_commander.program.option("-o, --output <path>", "Output directory for generated files", "src/lib/api-generated").option("--env-path <path>", ".env file path", import_path2.default.resolve(process.cwd(), ".env")).action((options) => {
|
|
126
|
+
const absoluteOutputPath = import_path2.default.resolve(process.cwd(), options.output);
|
|
127
|
+
runGenerator({ ...options, output: absoluteOutputPath });
|
|
128
|
+
});
|
|
129
|
+
import_commander.program.parse(process.argv);
|
package/dist/client.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }"use client";
|
|
3
2
|
|
|
4
3
|
|
|
@@ -6,8 +5,7 @@
|
|
|
6
5
|
|
|
7
6
|
|
|
8
7
|
|
|
9
|
-
var
|
|
10
|
-
require('./chunk-QOOQGLEX.cjs');
|
|
8
|
+
var _chunk25UFVV4Fcjs = require('./chunk-25UFVV4F.cjs');
|
|
11
9
|
|
|
12
10
|
// src/hooks/useApi.ts
|
|
13
11
|
var _react = require('react');
|
|
@@ -48,7 +46,7 @@ function useApi(axiosInstance, config) {
|
|
|
48
46
|
});
|
|
49
47
|
const [queryOptions, setQueryOptions] = _react.useState.call(void 0, initialQuery);
|
|
50
48
|
const apiServices = _react.useMemo.call(void 0,
|
|
51
|
-
() =>
|
|
49
|
+
() => _chunk25UFVV4Fcjs.createApiServices.call(void 0, axiosInstance, finalEndpoint),
|
|
52
50
|
[axiosInstance, finalEndpoint]
|
|
53
51
|
);
|
|
54
52
|
const savedOnSuccess = _react.useRef.call(void 0, onSuccess);
|
|
@@ -62,7 +60,7 @@ function useApi(axiosInstance, config) {
|
|
|
62
60
|
try {
|
|
63
61
|
const finalQuery = currentQueryOptions || queryOptions;
|
|
64
62
|
const hasQueryParams = Object.keys(finalQuery).length > 0;
|
|
65
|
-
const queryString =
|
|
63
|
+
const queryString = _chunk25UFVV4Fcjs.buildPaginateQuery.call(void 0, finalQuery);
|
|
66
64
|
const result = hasQueryParams ? await apiServices.getWithQuery(queryString, requestConfig) : await apiServices.get(void 0, requestConfig);
|
|
67
65
|
setState(result);
|
|
68
66
|
if (result.success && savedOnSuccess.current) {
|
|
@@ -177,7 +175,7 @@ function useApiRecord(axiosInstance, config) {
|
|
|
177
175
|
savedOnError.current = onError;
|
|
178
176
|
}, [onSuccess, onError]);
|
|
179
177
|
const apiServices = _react.useMemo.call(void 0,
|
|
180
|
-
() =>
|
|
178
|
+
() => _chunk25UFVV4Fcjs.createApiServices.call(void 0, axiosInstance, finalEndpoint),
|
|
181
179
|
[axiosInstance, finalEndpoint]
|
|
182
180
|
);
|
|
183
181
|
const fetchRecord = _react.useCallback.call(void 0, async () => {
|
|
@@ -289,10 +287,10 @@ var createInitialState = () => ({
|
|
|
289
287
|
rawResponse: null
|
|
290
288
|
});
|
|
291
289
|
function useApiActionState(actionConfig, cacheKey, execute, input, enabled) {
|
|
292
|
-
const getClientSnapshot = () =>
|
|
293
|
-
const getServerSnapshot = () =>
|
|
290
|
+
const getClientSnapshot = () => _chunk25UFVV4Fcjs.globalStateManager.getSnapshot(cacheKey);
|
|
291
|
+
const getServerSnapshot = () => _chunk25UFVV4Fcjs.globalStateManager.getSnapshot(cacheKey);
|
|
294
292
|
const state = _react.useSyncExternalStore.call(void 0,
|
|
295
|
-
(callback) =>
|
|
293
|
+
(callback) => _chunk25UFVV4Fcjs.globalStateManager.subscribe(cacheKey, callback),
|
|
296
294
|
getClientSnapshot,
|
|
297
295
|
getServerSnapshot
|
|
298
296
|
);
|
|
@@ -344,7 +342,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
344
342
|
}, [onSuccess, onError]);
|
|
345
343
|
_react.useMemo.call(void 0, () => {
|
|
346
344
|
if (hydratedState) {
|
|
347
|
-
|
|
345
|
+
_chunk25UFVV4Fcjs.globalStateManager.rehydrate(hydratedState);
|
|
348
346
|
}
|
|
349
347
|
}, [hydratedState]);
|
|
350
348
|
_react.useEffect.call(void 0, () => {
|
|
@@ -352,7 +350,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
352
350
|
}, [onSuccess, onError]);
|
|
353
351
|
_react.useMemo.call(void 0, () => {
|
|
354
352
|
if (hydratedState) {
|
|
355
|
-
|
|
353
|
+
_chunk25UFVV4Fcjs.globalStateManager.rehydrate(hydratedState);
|
|
356
354
|
}
|
|
357
355
|
}, [hydratedState]);
|
|
358
356
|
const actions = _react.useMemo.call(void 0, () => {
|
|
@@ -361,23 +359,23 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
361
359
|
const shouldCache = actionConfig.cacheResponse !== false;
|
|
362
360
|
const execute = async (input, options2 = {}) => {
|
|
363
361
|
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
364
|
-
const cacheKey = shouldCache ?
|
|
362
|
+
const cacheKey = shouldCache ? _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams }) : "";
|
|
365
363
|
let mutationContext;
|
|
366
364
|
try {
|
|
367
365
|
if (options2.onMutate && shouldCache) {
|
|
368
366
|
mutationContext = await options2.onMutate(input);
|
|
369
367
|
}
|
|
370
368
|
if (shouldCache) {
|
|
371
|
-
|
|
369
|
+
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, loading: true, called: true, error: null, isStale: false }));
|
|
372
370
|
}
|
|
373
|
-
const result = await
|
|
371
|
+
const result = await _chunk25UFVV4Fcjs.callDynamicApi.call(void 0, axiosInstance, moduleConfig.baseEndpoint, actionConfig, {
|
|
374
372
|
pathParams: finalPathParams,
|
|
375
373
|
body: input,
|
|
376
374
|
config: options2.config
|
|
377
375
|
});
|
|
378
376
|
console.log("[API Result execute]", result);
|
|
379
377
|
if (shouldCache) {
|
|
380
|
-
|
|
378
|
+
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({
|
|
381
379
|
...prev,
|
|
382
380
|
// احتفظ بالخصائص القديمة مثل 'called'
|
|
383
381
|
loading: false,
|
|
@@ -403,7 +401,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
403
401
|
_optionalChain([actionConfig, 'access', _11 => _11.invalidates, 'optionalAccess', _12 => _12.forEach, 'call', _13 => _13((keyToInvalidate) => {
|
|
404
402
|
const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
|
|
405
403
|
console.log(`[Invalidating] by prefix: ${prefix}`);
|
|
406
|
-
|
|
404
|
+
_chunk25UFVV4Fcjs.globalStateManager.invalidateByPrefix(prefix);
|
|
407
405
|
})]);
|
|
408
406
|
} else {
|
|
409
407
|
_optionalChain([savedCallbacks, 'access', _14 => _14.current, 'access', _15 => _15.onError, 'optionalCall', _16 => _16(actionName, result.message || "Action failed", _nullishCoalesce(result.error, () => ( void 0)))]);
|
|
@@ -413,7 +411,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
413
411
|
} catch (error) {
|
|
414
412
|
const apiError = _optionalChain([error, 'access', _19 => _19.response, 'optionalAccess', _20 => _20.data]) || { status: 500, message: error.message };
|
|
415
413
|
if (shouldCache) {
|
|
416
|
-
|
|
414
|
+
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, error: apiError, loading: false, success: false }));
|
|
417
415
|
}
|
|
418
416
|
_optionalChain([savedCallbacks, 'access', _21 => _21.current, 'access', _22 => _22.onError, 'optionalCall', _23 => _23(actionName, apiError.message, apiError)]);
|
|
419
417
|
_optionalChain([options2, 'access', _24 => _24.onError, 'optionalCall', _25 => _25(apiError, mutationContext)]);
|
|
@@ -427,8 +425,8 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
427
425
|
const reset = (input, options2 = {}) => {
|
|
428
426
|
if (shouldCache) {
|
|
429
427
|
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
430
|
-
const cacheKey =
|
|
431
|
-
|
|
428
|
+
const cacheKey = _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
429
|
+
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, () => createInitialState());
|
|
432
430
|
}
|
|
433
431
|
};
|
|
434
432
|
acc[actionName] = { execute, reset };
|
|
@@ -471,7 +469,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
471
469
|
}
|
|
472
470
|
const input = queryOptions2;
|
|
473
471
|
const pathParams = JSON.parse(pathParamsString);
|
|
474
|
-
const cacheKey =
|
|
472
|
+
const cacheKey = _chunk25UFVV4Fcjs.generateCacheKey.call(void 0,
|
|
475
473
|
moduleConfig.baseEndpoint,
|
|
476
474
|
actionName,
|
|
477
475
|
input,
|
|
@@ -499,7 +497,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
499
497
|
const state = states[actionName];
|
|
500
498
|
if (state && state.called && !state.loading) {
|
|
501
499
|
const prefix = `${moduleConfig.baseEndpoint}/${actionName}::`;
|
|
502
|
-
|
|
500
|
+
_chunk25UFVV4Fcjs.globalStateManager.invalidateByPrefix(prefix);
|
|
503
501
|
}
|
|
504
502
|
}
|
|
505
503
|
}
|
|
@@ -515,7 +513,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
515
513
|
};
|
|
516
514
|
}, [enabled, refetchOnWindowFocus, moduleConfig, states]);
|
|
517
515
|
const dehydrate = _react.useMemo.call(void 0, () => {
|
|
518
|
-
return () =>
|
|
516
|
+
return () => _chunk25UFVV4Fcjs.globalStateManager.dehydrate();
|
|
519
517
|
}, []);
|
|
520
518
|
const baseApiReturn = { actions, states, queries, dehydrate };
|
|
521
519
|
const finalApiReturn = _react.useMemo.call(void 0, () => ({
|
package/dist/client.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { U as UseApiConfig,
|
|
3
|
-
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-
|
|
2
|
+
import { U as UseApiConfig, A as ActionConfigModule, a as ApiModuleConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, M as ModuleStates, d as ModuleActions, e as UseApiQuery } from './apiModule.types-Bn0tJF7b.cjs';
|
|
3
|
+
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-NZtPRga3.cjs';
|
|
4
4
|
import * as React from 'react';
|
|
5
5
|
|
|
6
6
|
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): any;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { U as UseApiConfig,
|
|
3
|
-
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-
|
|
2
|
+
import { U as UseApiConfig, A as ActionConfigModule, a as ApiModuleConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, M as ModuleStates, d as ModuleActions, e as UseApiQuery } from './apiModule.types-Bn0tJF7b.js';
|
|
3
|
+
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-CWhwCAB6.js';
|
|
4
4
|
import * as React from 'react';
|
|
5
5
|
|
|
6
6
|
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): any;
|
package/dist/client.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
"use client";
|
|
3
2
|
import {
|
|
4
3
|
buildPaginateQuery,
|
|
@@ -6,8 +5,7 @@ import {
|
|
|
6
5
|
createApiServices,
|
|
7
6
|
generateCacheKey,
|
|
8
7
|
globalStateManager
|
|
9
|
-
} from "./chunk-
|
|
10
|
-
import "./chunk-NRV3EMIJ.js";
|
|
8
|
+
} from "./chunk-KPZ7BF52.js";
|
|
11
9
|
|
|
12
10
|
// src/hooks/useApi.ts
|
|
13
11
|
import { useState, useEffect, useCallback, useRef, useMemo } from "react";
|
package/dist/index.cjs
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
|
|
6
5
|
|
|
7
6
|
|
|
8
7
|
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var _chunkQOOQGLEXcjs = require('./chunk-QOOQGLEX.cjs');
|
|
8
|
+
var _chunk25UFVV4Fcjs = require('./chunk-25UFVV4F.cjs');
|
|
13
9
|
|
|
14
10
|
// src/core/client.ts
|
|
15
11
|
var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios);
|
|
@@ -218,9 +214,9 @@ function createAction(axiosInstance, method, endpoint) {
|
|
|
218
214
|
...method.toUpperCase() === "GET" ? { params: payload } : { data: payload },
|
|
219
215
|
...config
|
|
220
216
|
});
|
|
221
|
-
return
|
|
217
|
+
return _chunk25UFVV4Fcjs.processResponse.call(void 0, response);
|
|
222
218
|
} catch (error) {
|
|
223
|
-
return
|
|
219
|
+
return _chunk25UFVV4Fcjs.processResponse.call(void 0, error);
|
|
224
220
|
}
|
|
225
221
|
};
|
|
226
222
|
}
|
|
@@ -236,11 +232,9 @@ function createApiActions(axiosInstance, actionsConfig) {
|
|
|
236
232
|
}
|
|
237
233
|
|
|
238
234
|
// src/core/cache.ts
|
|
239
|
-
var CacheManager = class {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
_chunkQOOQGLEXcjs.__publicField.call(void 0, this, "defaultDuration", 15 * 60 * 1e3);
|
|
243
|
-
}
|
|
235
|
+
var CacheManager = (_class = class {constructor() { _class.prototype.__init.call(this);_class.prototype.__init2.call(this); }
|
|
236
|
+
__init() {this.cache = /* @__PURE__ */ new Map()}
|
|
237
|
+
__init2() {this.defaultDuration = 15 * 60 * 1e3}
|
|
244
238
|
// 15 minutes
|
|
245
239
|
set(key, data, duration) {
|
|
246
240
|
this.cache.set(key, {
|
|
@@ -289,7 +283,7 @@ var CacheManager = class {
|
|
|
289
283
|
keysToDelete.forEach((key) => this.cache.delete(key));
|
|
290
284
|
console.log(`Invalidated ${keysToDelete.length} cache entries with prefix: ${prefix}`);
|
|
291
285
|
}
|
|
292
|
-
};
|
|
286
|
+
}, _class);
|
|
293
287
|
var cacheManager = new CacheManager();
|
|
294
288
|
|
|
295
289
|
|
|
@@ -301,4 +295,4 @@ var cacheManager = new CacheManager();
|
|
|
301
295
|
|
|
302
296
|
|
|
303
297
|
|
|
304
|
-
exports.buildPaginateQuery =
|
|
298
|
+
exports.buildPaginateQuery = _chunk25UFVV4Fcjs.buildPaginateQuery; exports.cacheManager = cacheManager; exports.callDynamicApi = _chunk25UFVV4Fcjs.callDynamicApi; exports.createApiActions = createApiActions; exports.createApiClient = createApiClient; exports.createApiServices = _chunk25UFVV4Fcjs.createApiServices; exports.generateCacheKey = _chunk25UFVV4Fcjs.generateCacheKey; exports.globalStateManager = _chunk25UFVV4Fcjs.globalStateManager; exports.processResponse = _chunk25UFVV4Fcjs.processResponse;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosRequestConfig, Method, AxiosResponse, AxiosError } from 'axios';
|
|
2
|
-
import {
|
|
3
|
-
export { n as ActionConfig, s as ActionMethods, p as ActionState, r as ActionsWithQuery,
|
|
4
|
-
export { c as UseApiRecordActions, U as UseApiRecordConfig, a as UseApiRecordReturn, b as UseApiRecordState } from './useApiRecord.types-
|
|
2
|
+
import { f as ApiClientConfig, A as ActionConfigModule, S as StandardResponse, g as ActionOptions, R as RequestConfig, h as ActionStateModule, Q as QueryOptions, e as UseApiQuery, i as ApiError, L as LogLevel } from './apiModule.types-Bn0tJF7b.cjs';
|
|
3
|
+
export { n as ActionConfig, s as ActionMethods, p as ActionState, r as ActionsWithQuery, a as ApiModuleConfig, E as ExecutableAction, q as ExecuteOptions, I as InputOf, l as Middleware, k as MiddlewareContext, d as ModuleActions, M as ModuleStates, O as OutputOf, P as PaginationMeta, m as RefreshTokenConfig, j as TokenManager, T as Tokens, U as UseApiConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, o as UseApiState, V as ValidationError, t } from './apiModule.types-Bn0tJF7b.cjs';
|
|
4
|
+
export { c as UseApiRecordActions, U as UseApiRecordConfig, a as UseApiRecordReturn, b as UseApiRecordState } from './useApiRecord.types-NZtPRga3.cjs';
|
|
5
5
|
import 'react';
|
|
6
6
|
|
|
7
7
|
declare function createApiClient(config: ApiClientConfig): AxiosInstance;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosRequestConfig, Method, AxiosResponse, AxiosError } from 'axios';
|
|
2
|
-
import {
|
|
3
|
-
export { n as ActionConfig, s as ActionMethods, p as ActionState, r as ActionsWithQuery,
|
|
4
|
-
export { c as UseApiRecordActions, U as UseApiRecordConfig, a as UseApiRecordReturn, b as UseApiRecordState } from './useApiRecord.types-
|
|
2
|
+
import { f as ApiClientConfig, A as ActionConfigModule, S as StandardResponse, g as ActionOptions, R as RequestConfig, h as ActionStateModule, Q as QueryOptions, e as UseApiQuery, i as ApiError, L as LogLevel } from './apiModule.types-Bn0tJF7b.js';
|
|
3
|
+
export { n as ActionConfig, s as ActionMethods, p as ActionState, r as ActionsWithQuery, a as ApiModuleConfig, E as ExecutableAction, q as ExecuteOptions, I as InputOf, l as Middleware, k as MiddlewareContext, d as ModuleActions, M as ModuleStates, O as OutputOf, P as PaginationMeta, m as RefreshTokenConfig, j as TokenManager, T as Tokens, U as UseApiConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, o as UseApiState, V as ValidationError, t } from './apiModule.types-Bn0tJF7b.js';
|
|
4
|
+
export { c as UseApiRecordActions, U as UseApiRecordConfig, a as UseApiRecordReturn, b as UseApiRecordState } from './useApiRecord.types-CWhwCAB6.js';
|
|
5
5
|
import 'react';
|
|
6
6
|
|
|
7
7
|
declare function createApiClient(config: ApiClientConfig): AxiosInstance;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
import {
|
|
3
2
|
buildPaginateQuery,
|
|
4
3
|
callDynamicApi,
|
|
@@ -6,10 +5,7 @@ import {
|
|
|
6
5
|
generateCacheKey,
|
|
7
6
|
globalStateManager,
|
|
8
7
|
processResponse
|
|
9
|
-
} from "./chunk-
|
|
10
|
-
import {
|
|
11
|
-
__publicField
|
|
12
|
-
} from "./chunk-NRV3EMIJ.js";
|
|
8
|
+
} from "./chunk-KPZ7BF52.js";
|
|
13
9
|
|
|
14
10
|
// src/core/client.ts
|
|
15
11
|
import axios from "axios";
|
|
@@ -237,10 +233,8 @@ function createApiActions(axiosInstance, actionsConfig) {
|
|
|
237
233
|
|
|
238
234
|
// src/core/cache.ts
|
|
239
235
|
var CacheManager = class {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
__publicField(this, "defaultDuration", 15 * 60 * 1e3);
|
|
243
|
-
}
|
|
236
|
+
cache = /* @__PURE__ */ new Map();
|
|
237
|
+
defaultDuration = 15 * 60 * 1e3;
|
|
244
238
|
// 15 minutes
|
|
245
239
|
set(key, data, duration) {
|
|
246
240
|
this.cache.set(key, {
|
package/dist/server.cjs
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
|
|
6
|
-
var
|
|
7
|
-
require('./chunk-QOOQGLEX.cjs');
|
|
5
|
+
var _chunk25UFVV4Fcjs = require('./chunk-25UFVV4F.cjs');
|
|
8
6
|
|
|
9
7
|
// src/server.ts
|
|
10
8
|
function createServerApi(apiClient, moduleConfig) {
|
|
@@ -17,13 +15,13 @@ function createServerApi(apiClient, moduleConfig) {
|
|
|
17
15
|
if (!actionConfig) {
|
|
18
16
|
throw new Error(`Action "${actionName}" not found in module.`);
|
|
19
17
|
}
|
|
20
|
-
const result = await
|
|
18
|
+
const result = await _chunk25UFVV4Fcjs.callDynamicApi.call(void 0, apiClient, moduleConfig.baseEndpoint, actionConfig, {
|
|
21
19
|
pathParams: options.pathParams,
|
|
22
20
|
body: input
|
|
23
21
|
});
|
|
24
|
-
const cacheKey =
|
|
22
|
+
const cacheKey = _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: options.pathParams });
|
|
25
23
|
if (result.success) {
|
|
26
|
-
|
|
24
|
+
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, () => ({
|
|
27
25
|
data: result.data,
|
|
28
26
|
error: null,
|
|
29
27
|
loading: false,
|
|
@@ -33,7 +31,7 @@ function createServerApi(apiClient, moduleConfig) {
|
|
|
33
31
|
rawResponse: result.rawResponse
|
|
34
32
|
}));
|
|
35
33
|
} else {
|
|
36
|
-
|
|
34
|
+
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, () => ({
|
|
37
35
|
data: null,
|
|
38
36
|
error: result.error,
|
|
39
37
|
loading: false,
|
|
@@ -49,7 +47,7 @@ function createServerApi(apiClient, moduleConfig) {
|
|
|
49
47
|
* Dehydrates the current state in the global manager into a JSON string.
|
|
50
48
|
*/
|
|
51
49
|
dehydrate: () => {
|
|
52
|
-
return
|
|
50
|
+
return _chunk25UFVV4Fcjs.globalStateManager.dehydrate();
|
|
53
51
|
}
|
|
54
52
|
};
|
|
55
53
|
}
|
package/dist/server.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import {
|
|
2
|
+
import { A as ActionConfigModule, a as ApiModuleConfig, I as InputOf } from './apiModule.types-Bn0tJF7b.cjs';
|
|
3
3
|
import 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import {
|
|
2
|
+
import { A as ActionConfigModule, a as ApiModuleConfig, I as InputOf } from './apiModule.types-Bn0tJF7b.js';
|
|
3
3
|
import 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
package/dist/server.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
import {
|
|
3
2
|
callDynamicApi,
|
|
4
3
|
generateCacheKey,
|
|
5
4
|
globalStateManager
|
|
6
|
-
} from "./chunk-
|
|
7
|
-
import "./chunk-NRV3EMIJ.js";
|
|
5
|
+
} from "./chunk-KPZ7BF52.js";
|
|
8
6
|
|
|
9
7
|
// src/server.ts
|
|
10
8
|
function createServerApi(apiClient, moduleConfig) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
-
import { R as RequestConfig,
|
|
2
|
+
import { R as RequestConfig, i as ApiError, S as StandardResponse, g as ActionOptions } from './apiModule.types-Bn0tJF7b.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Represents the internal state of the `useApiRecord` hook.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
-
import { R as RequestConfig,
|
|
2
|
+
import { R as RequestConfig, i as ApiError, S as StandardResponse, g as ActionOptions } from './apiModule.types-Bn0tJF7b.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Represents the internal state of the `useApiRecord` hook.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-core-lib",
|
|
3
|
-
"version": "12.0.
|
|
3
|
+
"version": "12.0.28",
|
|
4
4
|
"description": "A flexible and powerful API client library for modern web applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"module": "./dist/index.js",
|
|
27
27
|
"types": "./dist/index.d.ts",
|
|
28
28
|
"bin": {
|
|
29
|
-
"api-core-generate": "./dist/cli.
|
|
29
|
+
"api-core-generate": "./dist/cli.cjs"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsup",
|
package/dist/chunk-NRV3EMIJ.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
|
|
6
|
-
export {
|
|
7
|
-
__publicField
|
|
8
|
-
};
|
package/dist/chunk-QOOQGLEX.cjs
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
|
|
3
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
exports.__publicField = __publicField;
|
package/dist/cli.d.cts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
package/dist/cli.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
package/dist/cli.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
#!/usr/bin/env node
|
|
3
|
-
import "./chunk-NRV3EMIJ.js";
|
|
4
|
-
|
|
5
|
-
// src/cli.ts
|
|
6
|
-
import { program } from "commander";
|
|
7
|
-
import path2 from "path";
|
|
8
|
-
|
|
9
|
-
// src/generator/index.ts
|
|
10
|
-
import fs from "fs";
|
|
11
|
-
import path from "path";
|
|
12
|
-
import axios from "axios";
|
|
13
|
-
import chalk from "chalk";
|
|
14
|
-
import dotenv from "dotenv";
|
|
15
|
-
import openapiTS from "openapi-typescript";
|
|
16
|
-
|
|
17
|
-
// src/generator/module-parser.ts
|
|
18
|
-
function parseSpecToModules(spec) {
|
|
19
|
-
const modules = {};
|
|
20
|
-
for (const apiPath in spec.paths) {
|
|
21
|
-
for (const method in spec.paths[apiPath]) {
|
|
22
|
-
const endpoint = spec.paths[apiPath][method];
|
|
23
|
-
if (!endpoint.tags || endpoint.tags.length === 0) continue;
|
|
24
|
-
const tagName = endpoint.tags[0];
|
|
25
|
-
const moduleName = tagName.replace(/[^a-zA-Z0-9]/g, "").replace(/Central|Tenant/g, "") + "Api";
|
|
26
|
-
if (!modules[moduleName]) {
|
|
27
|
-
modules[moduleName] = {
|
|
28
|
-
baseEndpoint: "",
|
|
29
|
-
// سيتم ملء المسار الكامل في path
|
|
30
|
-
actions: {}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
const actionName = endpoint.operationId || method + apiPath.replace(/[\/{}]+/g, "_");
|
|
34
|
-
modules[moduleName].actions[actionName] = {
|
|
35
|
-
method: method.toUpperCase(),
|
|
36
|
-
path: apiPath,
|
|
37
|
-
description: endpoint.summary || "No description available.",
|
|
38
|
-
hasQuery: (endpoint.parameters || []).some((p) => p.in === "query"),
|
|
39
|
-
autoFetch: method.toUpperCase() === "GET" && !apiPath.includes("{"),
|
|
40
|
-
// افتراض ذكي
|
|
41
|
-
invalidates: []
|
|
42
|
-
// يمكن تركه فارغًا
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return modules;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// src/generator/index.ts
|
|
50
|
-
async function runGenerator(options) {
|
|
51
|
-
console.log(chalk.cyan(`Starting API module generation...`));
|
|
52
|
-
console.log(chalk.gray(`Output directory: ${options.output}`));
|
|
53
|
-
console.log(chalk.gray(`.env path: ${options.envPath}`));
|
|
54
|
-
dotenv.config({ path: options.envPath });
|
|
55
|
-
const apiUrl = process.env.NEXT_PUBLIC_API_URL || process.env.API_URL;
|
|
56
|
-
if (!apiUrl) {
|
|
57
|
-
console.error(chalk.red("Error: API_URL or NEXT_PUBLIC_API_URL not found in .env file."));
|
|
58
|
-
process.exit(1);
|
|
59
|
-
}
|
|
60
|
-
try {
|
|
61
|
-
const specUrl = `${apiUrl}/api-docs/json`;
|
|
62
|
-
console.log(`Fetching OpenAPI spec from ${specUrl}...`);
|
|
63
|
-
const response = await axios.get(specUrl);
|
|
64
|
-
const spec = response.data;
|
|
65
|
-
if (!fs.existsSync(options.output)) {
|
|
66
|
-
fs.mkdirSync(options.output, { recursive: true });
|
|
67
|
-
}
|
|
68
|
-
console.log("Generating TypeScript types...");
|
|
69
|
-
const typesContent = await openapiTS(spec);
|
|
70
|
-
const typesPath = path.join(options.output, "api-types.generated.ts");
|
|
71
|
-
fs.writeFileSync(typesPath, typesContent);
|
|
72
|
-
console.log(chalk.green(`\u2713 Types generated successfully at ${typesPath}`));
|
|
73
|
-
console.log("Generating API modules...");
|
|
74
|
-
const modules = parseSpecToModules(spec);
|
|
75
|
-
for (const moduleName in modules) {
|
|
76
|
-
const moduleConfig = modules[moduleName];
|
|
77
|
-
const fileName = `${moduleName}.module.ts`;
|
|
78
|
-
const filePath = path.join(options.output, fileName);
|
|
79
|
-
const fileContent = `
|
|
80
|
-
// This file is auto-generated by api-core-lib.
|
|
81
|
-
// Do not edit this file directly.
|
|
82
|
-
|
|
83
|
-
import type { ApiModuleConfig } from 'api-core-lib';
|
|
84
|
-
import type { paths } from './api-types.generated'; // \u0631\u0628\u0637 \u0627\u0644\u0623\u0646\u0648\u0627\u0639 \u0627\u0644\u0645\u0648\u0644\u062F\u0629
|
|
85
|
-
|
|
86
|
-
// A more advanced generator could automatically map the correct input/output types.
|
|
87
|
-
// For now, we use 'any' as a placeholder.
|
|
88
|
-
export const ${moduleName}Module: ApiModuleConfig<any> = ${JSON.stringify(moduleConfig, null, 2)};
|
|
89
|
-
`;
|
|
90
|
-
fs.writeFileSync(filePath, fileContent);
|
|
91
|
-
console.log(chalk.green(`\u2713 Module generated: ${fileName}`));
|
|
92
|
-
}
|
|
93
|
-
console.log(chalk.bold.green("\n\u{1F389} API generation complete!"));
|
|
94
|
-
} catch (error) {
|
|
95
|
-
console.error(chalk.red("\nAn error occurred during generation:"));
|
|
96
|
-
console.error(error.message);
|
|
97
|
-
process.exit(1);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// src/cli.ts
|
|
102
|
-
console.log("API Core Lib - Code Generator");
|
|
103
|
-
program.option("-o, --output <path>", "Output directory for generated files", "src/lib/api-generated").option("--env-path <path>", ".env file path", path2.resolve(process.cwd(), ".env")).action((options) => {
|
|
104
|
-
const absoluteOutputPath = path2.resolve(process.cwd(), options.output);
|
|
105
|
-
runGenerator({ ...options, output: absoluteOutputPath });
|
|
106
|
-
});
|
|
107
|
-
program.parse(process.argv);
|