silgi 0.24.15 → 0.24.17

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.
Files changed (91) hide show
  1. package/dist/cli/build/prepare.mjs +43 -0
  2. package/dist/cli/{dev.mjs → commands/dev.mjs} +4 -43
  3. package/dist/cli/commands/env.mjs +40 -0
  4. package/dist/cli/{install.mjs → commands/install.mjs} +3 -37
  5. package/dist/cli/commands/prepare.mjs +66 -0
  6. package/dist/cli/{prepare.mjs → commands/run.mjs} +9 -82
  7. package/dist/cli/config/defaults.mjs +129 -0
  8. package/dist/cli/config/index.mjs +2 -16
  9. package/dist/cli/config/loader.mjs +97 -0
  10. package/dist/cli/config/resolvers/compatibility.mjs +90 -0
  11. package/dist/cli/config/resolvers/imports.mjs +96 -0
  12. package/dist/cli/config/resolvers/paths.mjs +167 -0
  13. package/dist/cli/config/resolvers/storage.mjs +25 -0
  14. package/dist/cli/config/resolvers/url.mjs +7 -0
  15. package/dist/cli/config/types.mjs +191 -0
  16. package/dist/cli/core/app.mjs +92 -0
  17. package/dist/cli/core/devServer.mjs +8 -0
  18. package/dist/cli/core/env.mjs +76 -0
  19. package/dist/cli/core/installPackage.mjs +31 -0
  20. package/dist/cli/core/prepare.mjs +7 -0
  21. package/dist/cli/core/runtimeConfig.mjs +14 -0
  22. package/dist/cli/core/scan.mjs +39 -0
  23. package/dist/cli/core/silgi.mjs +125 -0
  24. package/dist/cli/core/templates.mjs +31 -0
  25. package/dist/cli/framework/emptyFramework.mjs +9 -0
  26. package/dist/cli/framework/h3.mjs +89 -0
  27. package/dist/cli/framework/index.mjs +8 -0
  28. package/dist/cli/framework/nitro.mjs +106 -0
  29. package/dist/cli/framework/nuxt.mjs +9 -0
  30. package/dist/cli/index.mjs +6 -9
  31. package/dist/cli/module/exportScan.mjs +130 -0
  32. package/dist/cli/module/install.mjs +59 -0
  33. package/dist/cli/module/scan.mjs +227 -0
  34. package/dist/cli/scan/commands.mjs +47 -0
  35. package/dist/cli/scan/prepareConfigs.mjs +46 -0
  36. package/dist/cli/scan/prepareCoreFile.mjs +166 -0
  37. package/dist/cli/scan/prepareFramework.mjs +73 -0
  38. package/dist/cli/scan/prepareSchema.mjs +177 -0
  39. package/dist/cli/scan/prepareServerFiles.mjs +119 -0
  40. package/dist/cli/scan/scanExportFile.mjs +155 -0
  41. package/dist/cli/scan/writeCoreFile.mjs +34 -0
  42. package/dist/cli/scan/writeScanFiles.mjs +30 -0
  43. package/dist/cli/scan/writeTypesAndFiles.mjs +109 -0
  44. package/dist/cli/utils/build-uri-map.mjs +39 -0
  45. package/dist/cli/utils/common.mjs +13 -0
  46. package/dist/cli/{compatibility.mjs → utils/compatibility.mjs} +1 -1
  47. package/dist/cli/utils/generateRouterDTS.mjs +80 -0
  48. package/dist/cli/utils/ignore.mjs +46 -0
  49. package/dist/cli/utils/parser.mjs +168 -0
  50. package/dist/cli/utils/readScanFile.mjs +79 -0
  51. package/dist/cli/utils/storage.mjs +25 -0
  52. package/dist/core/createSilgi.mjs +76 -0
  53. package/dist/core/error.mjs +227 -0
  54. package/dist/core/index.mjs +8 -0
  55. package/dist/core/routeRules.mjs +288 -0
  56. package/dist/core/silgi.mjs +128 -0
  57. package/dist/core/silgiApp.mjs +25 -0
  58. package/dist/core/storage.mjs +15 -0
  59. package/dist/core/unctx.mjs +27 -0
  60. package/dist/core/uris/uri.mjs +33 -0
  61. package/dist/core/uris/utils.mjs +130 -0
  62. package/dist/core/utils/event.mjs +15 -0
  63. package/dist/core/utils/merge.mjs +29 -0
  64. package/dist/core/utils/runtime.mjs +22 -0
  65. package/dist/core/utils/schema.mjs +5 -0
  66. package/dist/core/utils/service.mjs +5 -0
  67. package/dist/core/utils/shared.mjs +5 -0
  68. package/dist/core/utils/storage.mjs +72 -0
  69. package/dist/index.mjs +20 -793
  70. package/dist/kit/errors.mjs +11 -0
  71. package/dist/kit/esm.mjs +14 -0
  72. package/dist/kit/fs.mjs +30 -0
  73. package/dist/kit/gen.mjs +44 -0
  74. package/dist/kit/hash.mjs +8 -0
  75. package/dist/kit/index.mjs +15 -528
  76. package/dist/kit/isFramework.mjs +25 -0
  77. package/dist/kit/logger.mjs +7 -0
  78. package/dist/kit/module.mjs +73 -0
  79. package/dist/kit/parseServices.mjs +57 -0
  80. package/dist/kit/path.mjs +34 -0
  81. package/dist/kit/preset.mjs +6 -0
  82. package/dist/kit/resolve.mjs +80 -0
  83. package/dist/kit/template.mjs +49 -0
  84. package/dist/kit/useRequest.mjs +91 -0
  85. package/dist/kit/utils.mjs +43 -0
  86. package/dist/package.json.mjs +5 -0
  87. package/dist/types/index.d.mts +1 -1
  88. package/package.json +1 -1
  89. package/dist/_chunks/silgiApp.mjs +0 -2682
  90. package/dist/cli/types.mjs +0 -772
  91. /package/dist/cli/{init.mjs → commands/init.mjs} +0 -0
@@ -0,0 +1,27 @@
1
+ import { getContext } from 'unctx';
2
+
3
+ const silgiCtx = getContext("silgi");
4
+ function useSilgi() {
5
+ const instance = silgiCtx.tryUse();
6
+ if (!instance) {
7
+ throw new Error("Silgi instance is unavailable!");
8
+ }
9
+ return instance;
10
+ }
11
+ function normalizeResult(result) {
12
+ if (Array.isArray(result)) {
13
+ return [...result];
14
+ }
15
+ if (result && typeof result === "object") {
16
+ if (Object.keys(result).every((key) => !Number.isNaN(Number(key)))) {
17
+ return Object.values(result);
18
+ }
19
+ return { ...result };
20
+ }
21
+ return result;
22
+ }
23
+ function tryUseSilgi() {
24
+ return silgiCtx.tryUse();
25
+ }
26
+
27
+ export { normalizeResult, silgiCtx, tryUseSilgi, useSilgi };
@@ -0,0 +1,33 @@
1
+ import { parseURI } from './utils.mjs';
2
+
3
+ async function findAction(silgi, uri) {
4
+ const { parts } = parseURI(uri, silgi.uris);
5
+ let result = silgi.services;
6
+ for (const part of parts) {
7
+ if (result && Object.prototype.hasOwnProperty.call(result, part)) {
8
+ result = Object.assign({}, result[part]);
9
+ } else {
10
+ silgi.logger.fail("Action not found:", `${parts.join("/")}`);
11
+ continue;
12
+ }
13
+ }
14
+ return result;
15
+ }
16
+ async function scanAction(silgi) {
17
+ for (const [key, _value] of Object.entries(silgi.uris)) {
18
+ const segments = key.split("/").filter(Boolean);
19
+ if (segments.length !== 4) {
20
+ console.error(`Invalid URI format for key "${key}". URI must have exactly 4 segments in format: namespace/service/method/action`);
21
+ continue;
22
+ }
23
+ const [namespace, service, method, action] = segments;
24
+ if (!namespace || !service || !method || !action) {
25
+ console.error(`Invalid URI segments for key "${key}". All segments must be non-empty`);
26
+ continue;
27
+ }
28
+ const handler = await findAction(silgi, key);
29
+ silgi.scannedHandlers.set(key, handler);
30
+ }
31
+ }
32
+
33
+ export { findAction, scanAction };
@@ -0,0 +1,130 @@
1
+ import { ErrorFactory, HttpStatus } from '../error.mjs';
2
+
3
+ function parseURI(uri, uris) {
4
+ if (!uri) {
5
+ throw ErrorFactory.create({
6
+ message: "URI cannot be empty",
7
+ httpStatus: HttpStatus.BAD_REQUEST,
8
+ context: { uri }
9
+ });
10
+ }
11
+ if (!uris) {
12
+ throw ErrorFactory.create({
13
+ message: "URIs configuration is not provided",
14
+ httpStatus: HttpStatus.INTERNAL_SERVER_ERROR,
15
+ context: { uri }
16
+ });
17
+ }
18
+ const cleanUri = uri.replace(/^\/*(srn\/)?/, "").replace(/\/*$/, "");
19
+ const [path, queryString] = cleanUri.split("?");
20
+ const parts = path.split("/");
21
+ const query = queryString ? Object.fromEntries(
22
+ queryString.split("&").map((param) => param.split("="))
23
+ ) : void 0;
24
+ const method = query?.method ? query?.method.toLowerCase() : void 0;
25
+ const namespaceName = parts[0];
26
+ const serviceName = parts[1];
27
+ const methodName = method || parts[2];
28
+ const actionName = method ? parts[2] : parts[3];
29
+ if (!namespaceName || !serviceName || !methodName || !actionName) {
30
+ throw ErrorFactory.create({
31
+ message: "Invalid URI format: Insufficient path segments",
32
+ httpStatus: HttpStatus.BAD_REQUEST,
33
+ context: {
34
+ uri,
35
+ cleanUri,
36
+ partsLength: parts.length,
37
+ method,
38
+ namespaceName,
39
+ serviceName,
40
+ methodName,
41
+ actionName
42
+ }
43
+ });
44
+ }
45
+ const baseUri = `${namespaceName}/${serviceName}/${methodName}/${actionName}`;
46
+ const paramStartIndex = method ? 3 : 4;
47
+ const parameters = parts.slice(paramStartIndex);
48
+ const normalizedUri = method ? `${namespaceName}/${serviceName}/${method}/${actionName}${parameters.length ? `/${parameters.join("/")}` : ""}` : cleanUri;
49
+ const template = uris[baseUri];
50
+ if (template === void 0) {
51
+ throw ErrorFactory.create({
52
+ message: "No route found for URI",
53
+ httpStatus: HttpStatus.NOT_FOUND,
54
+ context: {
55
+ uri,
56
+ baseUri
57
+ }
58
+ });
59
+ }
60
+ if (template === "") {
61
+ if (parameters.length > 0) {
62
+ throw ErrorFactory.create({
63
+ message: "No parameters expected for this route",
64
+ httpStatus: HttpStatus.BAD_REQUEST,
65
+ context: {
66
+ uri,
67
+ baseUri,
68
+ extraParams: parameters
69
+ }
70
+ });
71
+ }
72
+ if (query && query.method) {
73
+ delete query.method;
74
+ }
75
+ return {
76
+ namespaceName,
77
+ serviceName,
78
+ methodName,
79
+ actionName,
80
+ raw: normalizedUri,
81
+ parts: [namespaceName, serviceName, methodName, actionName],
82
+ routerParams: {},
83
+ query,
84
+ uri: baseUri
85
+ };
86
+ }
87
+ const routeTemplate = typeof template === "string" ? template : template.pattern;
88
+ const validators = typeof template === "string" ? void 0 : template.validators;
89
+ const routerParams = {};
90
+ const templateParts = routeTemplate.split("/").filter(Boolean);
91
+ const paramValues = parameters;
92
+ let valueIndex = 0;
93
+ templateParts.forEach((part) => {
94
+ if (part.startsWith(":")) {
95
+ const paramName = part.substring(1);
96
+ const paramValue = paramValues[valueIndex];
97
+ if (validators?.[paramName] && paramValue) {
98
+ if (!validators[paramName](paramValue)) {
99
+ throw ErrorFactory.create({
100
+ message: "Invalid value for parameter",
101
+ httpStatus: HttpStatus.UNPROCESSABLE_ENTITY,
102
+ context: {
103
+ uri,
104
+ paramName,
105
+ paramValue,
106
+ validatorName: paramName
107
+ }
108
+ });
109
+ }
110
+ }
111
+ routerParams[paramName] = paramValue || void 0;
112
+ valueIndex++;
113
+ } else if (part && part === paramValues[valueIndex]) {
114
+ valueIndex++;
115
+ }
116
+ });
117
+ return {
118
+ namespaceName,
119
+ serviceName,
120
+ methodName,
121
+ actionName,
122
+ raw: normalizedUri,
123
+ parts: [namespaceName, serviceName, methodName, actionName],
124
+ routerParams,
125
+ query: method ? void 0 : query,
126
+ uri: baseUri
127
+ };
128
+ }
129
+
130
+ export { parseURI };
@@ -0,0 +1,15 @@
1
+ import { useSilgi } from '../unctx.mjs';
2
+
3
+ function getEvent(event) {
4
+ const silgi = useSilgi();
5
+ if (silgi.options.present === "h3" || silgi.options.present === "nitro" || silgi.options.present === "nuxt") {
6
+ return event.event;
7
+ }
8
+ return event;
9
+ }
10
+ function getEventContext(event) {
11
+ const _event = getEvent(event);
12
+ return _event.context;
13
+ }
14
+
15
+ export { getEvent, getEventContext };
@@ -0,0 +1,29 @@
1
+ function merge(items, maxLevel = 4, currentLevel = 1) {
2
+ const arrayItems = Array.isArray(items) ? items : [items];
3
+ return arrayItems.reduce((acc, item) => {
4
+ if (!item)
5
+ return acc;
6
+ Object.keys(item).forEach((key) => {
7
+ if (typeof item[key] === "object" && item[key] !== null && currentLevel < maxLevel) {
8
+ if (!acc[key] || typeof acc[key] !== "object") {
9
+ acc[key] = {};
10
+ }
11
+ acc[key] = merge([acc[key], item[key]], maxLevel, currentLevel + 1);
12
+ } else if (acc[key] === void 0) {
13
+ acc[key] = item[key];
14
+ }
15
+ });
16
+ return acc;
17
+ }, {});
18
+ }
19
+ function mergeSchemas(typesOrArray) {
20
+ return merge(typesOrArray);
21
+ }
22
+ function mergeServices(servicesOrArray) {
23
+ return merge(servicesOrArray);
24
+ }
25
+ function mergeShared(sharedOrArray) {
26
+ return merge(sharedOrArray, 1, 1);
27
+ }
28
+
29
+ export { mergeSchemas, mergeServices, mergeShared };
@@ -0,0 +1,22 @@
1
+ function replaceRuntimeValues(obj, runtime) {
2
+ if (!obj || typeof obj !== "object")
3
+ return obj;
4
+ for (const key in obj) {
5
+ if (typeof obj[key] === "string" && obj[key].startsWith("runtime.")) {
6
+ const runtimePath = obj[key].substring(8).split(".");
7
+ let value = runtime;
8
+ for (const segment of runtimePath) {
9
+ if (value === void 0 || value === null)
10
+ break;
11
+ value = value[segment];
12
+ }
13
+ if (value !== void 0)
14
+ obj[key] = value;
15
+ } else if (typeof obj[key] === "object" && obj[key] !== null) {
16
+ obj[key] = replaceRuntimeValues(obj[key], runtime);
17
+ }
18
+ }
19
+ return obj;
20
+ }
21
+
22
+ export { replaceRuntimeValues };
@@ -0,0 +1,5 @@
1
+ function createSchema(silgiType) {
2
+ return silgiType;
3
+ }
4
+
5
+ export { createSchema };
@@ -0,0 +1,5 @@
1
+ function createService(variables) {
2
+ return variables;
3
+ }
4
+
5
+ export { createService };
@@ -0,0 +1,5 @@
1
+ function createShared(shared) {
2
+ return shared;
3
+ }
4
+
5
+ export { createShared };
@@ -0,0 +1,72 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { klona } from 'klona';
3
+ import { useSilgiRuntimeConfig } from 'silgi/runtime';
4
+ import { createStorage as createStorage$1, builtinDrivers, prefixStorage } from 'unstorage';
5
+ import { ErrorFactory, HttpStatus } from '../error.mjs';
6
+ import { useSilgi } from '../unctx.mjs';
7
+ import { replaceRuntimeValues } from './runtime.mjs';
8
+
9
+ async function createStorage(silgi) {
10
+ const storage = createStorage$1();
11
+ const runtime = useSilgiRuntimeConfig();
12
+ const mounts = klona({
13
+ ...silgi.options.storage,
14
+ ...silgi.options.devStorage
15
+ });
16
+ for (const [path, opts] of Object.entries(mounts)) {
17
+ if (opts.driver) {
18
+ const driver = await import(builtinDrivers[opts.driver] || opts.driver).then((r) => r.default || r);
19
+ const processedOpts = replaceRuntimeValues({ ...opts }, runtime);
20
+ storage.mount(path, driver(processedOpts));
21
+ } else {
22
+ silgi.logger.warn(`No \`driver\` set for storage mount point "${path}".`);
23
+ }
24
+ }
25
+ return storage;
26
+ }
27
+ function useSilgiStorage(base = "/memory:cache") {
28
+ const silgi = useSilgi();
29
+ return base ? prefixStorage(silgi.storage, base) : silgi.storage;
30
+ }
31
+ async function generateStorageKey(params) {
32
+ const {
33
+ operation,
34
+ input,
35
+ keyGenerator,
36
+ requestId,
37
+ storageOptions
38
+ } = params;
39
+ const cacheScopePrefix = storageOptions?.scope === "request" ? "req" : "global";
40
+ const parts = [
41
+ cacheScopePrefix,
42
+ // Always include scope prefix first
43
+ operation.namespaceName,
44
+ operation.serviceName,
45
+ operation.methodName
46
+ ].filter(Boolean);
47
+ if (storageOptions?.scope === "request") {
48
+ if (!requestId) {
49
+ throw ErrorFactory.create({
50
+ code: HttpStatus.BAD_REQUEST,
51
+ message: "Request ID is required for request-scoped cache",
52
+ context: {
53
+ requestId,
54
+ operation,
55
+ input,
56
+ storageOptions,
57
+ keyGenerator
58
+ }
59
+ });
60
+ }
61
+ parts.push(requestId);
62
+ }
63
+ if (keyGenerator) {
64
+ const customKey = await Promise.resolve(keyGenerator(input));
65
+ parts.push(customKey);
66
+ } else {
67
+ parts.push(typeof input === "object" ? JSON.stringify(input) : String(input));
68
+ }
69
+ return Buffer.from(parts.join(":")).toString("base64");
70
+ }
71
+
72
+ export { createStorage, generateStorageKey, useSilgiStorage };