silgi 0.7.0 → 0.7.1

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 (77) hide show
  1. package/dist/cli/build/framework/h3.mjs +46 -0
  2. package/dist/cli/build/framework/index.mjs +7 -0
  3. package/dist/cli/build/framework/nitro.mjs +28 -0
  4. package/dist/cli/build/framework/nuxt.mjs +9 -0
  5. package/dist/cli/build/prepare.mjs +7 -0
  6. package/dist/cli/build/scanURIs.mjs +27 -0
  7. package/dist/cli/build/template/framework.mjs +91 -0
  8. package/dist/cli/build/template/schema.mjs +115 -0
  9. package/dist/cli/build/template/silgi.mjs +149 -0
  10. package/dist/cli/build/types.mjs +130 -0
  11. package/dist/cli/commands/prepare.mjs +49 -0
  12. package/dist/cli/common.mjs +13 -0
  13. package/dist/cli/core/app.mjs +89 -0
  14. package/dist/cli/core/scan.mjs +40 -0
  15. package/dist/cli/core/silgi.mjs +77 -0
  16. package/dist/cli/core/storage.mjs +11 -0
  17. package/dist/cli/core/templates.mjs +29 -0
  18. package/dist/cli/index.mjs +3 -3
  19. package/dist/cli/module/exportScan.mjs +69 -0
  20. package/dist/cli/module/install.mjs +52 -0
  21. package/dist/cli/module/scan.mjs +141 -0
  22. package/dist/cli/{compatibility.mjs → utils/compatibility.mjs} +1 -1
  23. package/dist/cli/utils/generateRouterDTS.mjs +84 -0
  24. package/dist/cli/utils/ignore.mjs +46 -0
  25. package/dist/cli/utils/readCoreFile.mjs +47 -0
  26. package/dist/cli/utils/scan.mjs +147 -0
  27. package/dist/cli/utils/storage.mjs +21 -0
  28. package/dist/cli/utils/uri.mjs +71 -0
  29. package/dist/core/config/defaults.mjs +96 -0
  30. package/dist/core/config/loader.mjs +98 -0
  31. package/dist/core/config/resolvers/compatibility.mjs +90 -0
  32. package/dist/core/config/resolvers/imports.mjs +96 -0
  33. package/dist/core/config/resolvers/paths.mjs +194 -0
  34. package/dist/core/config/resolvers/storage.mjs +25 -0
  35. package/dist/core/config/resolvers/url.mjs +7 -0
  36. package/dist/core/config/types.mjs +160 -0
  37. package/dist/core/createSilgi.mjs +84 -0
  38. package/dist/core/error.mjs +227 -0
  39. package/dist/core/fetch/ofetch.mjs +35 -0
  40. package/dist/core/index.mjs +16 -1678
  41. package/dist/core/parser.mjs +136 -0
  42. package/dist/core/silgi.mjs +114 -0
  43. package/dist/core/silgiApp.mjs +15 -0
  44. package/dist/core/unctx.mjs +27 -0
  45. package/dist/core/uris/uri.mjs +33 -0
  46. package/dist/core/uris/utils.mjs +127 -0
  47. package/dist/core/utils/event.mjs +5 -0
  48. package/dist/core/utils/global.mjs +12 -0
  49. package/dist/core/utils/merge.mjs +25 -0
  50. package/dist/core/utils/schema.mjs +5 -0
  51. package/dist/core/utils/service.mjs +5 -0
  52. package/dist/core/utils/shared.mjs +5 -0
  53. package/dist/core/utils/storage.mjs +70 -0
  54. package/dist/ecosystem/nitro/index.mjs +1 -62
  55. package/dist/ecosystem/nitro/module.mjs +62 -0
  56. package/dist/kit/esm.mjs +10 -0
  57. package/dist/kit/fs.mjs +25 -0
  58. package/dist/kit/index.mjs +10 -299
  59. package/dist/kit/isFramework.mjs +25 -0
  60. package/dist/kit/logger.mjs +8 -0
  61. package/dist/kit/module.mjs +73 -0
  62. package/dist/kit/path.mjs +34 -0
  63. package/dist/kit/preset.mjs +6 -0
  64. package/dist/kit/resolve.mjs +78 -0
  65. package/dist/kit/template.mjs +47 -0
  66. package/dist/kit/utils.mjs +20 -0
  67. package/dist/meta/index.d.mts +1 -1
  68. package/dist/meta/index.d.ts +1 -1
  69. package/dist/meta/index.mjs +1 -1
  70. package/dist/package.json.mjs +5 -0
  71. package/dist/schema/common.mjs +43 -0
  72. package/dist/schema/index.mjs +9 -0
  73. package/dist/schema/internal.mjs +22 -0
  74. package/package.json +1 -1
  75. package/dist/_chunks/index.mjs +0 -5
  76. package/dist/cli/prepare.mjs +0 -1481
  77. /package/dist/cli/{init.mjs → commands/init.mjs} +0 -0
@@ -0,0 +1,136 @@
1
+ import { writeFileSync } from 'node:fs';
2
+ import { parseSync } from '@oxc-parser/wasm';
3
+
4
+ class SchemaParser {
5
+ options = {
6
+ debug: false
7
+ };
8
+ /**
9
+ *
10
+ */
11
+ constructor(options) {
12
+ this.options = {
13
+ ...this.options,
14
+ ...options
15
+ };
16
+ }
17
+ parseExports(content, filePath) {
18
+ const ast = parseSync(content, { sourceType: "module", sourceFilename: filePath });
19
+ if (this.options.debug)
20
+ writeFileSync(`${filePath}.ast.json`, JSON.stringify(ast.program, null, 2));
21
+ return {
22
+ exportVariables: (search, path) => this.parseTypeDeclarations(ast, search, path),
23
+ parseInterfaceDeclarations: (search, path) => this.parseInterfaceDeclarations(ast, search, path)
24
+ // parsePlugin: (path: string) => this.parsePlugin(ast, path),
25
+ };
26
+ }
27
+ parseVariableDeclaration(ast) {
28
+ return ast.program.body.filter((i) => i.type === "ExportNamedDeclaration").filter((i) => i.declaration?.type === "VariableDeclaration");
29
+ }
30
+ parseTSInterfaceDeclaration(ast) {
31
+ return ast.program.body.filter((i) => i.type === "ExportNamedDeclaration").filter((i) => i.declaration?.type === "TSInterfaceDeclaration");
32
+ }
33
+ parseTypeDeclarations(ast, find = "", path = "") {
34
+ const data = [];
35
+ for (const item of this.parseVariableDeclaration(ast)) {
36
+ for (const declaration of item.declaration.declarations) {
37
+ if (declaration.init?.callee?.name === find) {
38
+ const options = {};
39
+ if (declaration.init.arguments) {
40
+ for (const argument of declaration.init.arguments) {
41
+ for (const propertie of argument.properties) {
42
+ if (propertie.key.name === "name")
43
+ options.pluginName = propertie.value.value;
44
+ }
45
+ }
46
+ }
47
+ for (const key in declaration.init.properties) {
48
+ const property = declaration.init.properties[key];
49
+ if (property.type === "ObjectProperty") {
50
+ if (property.key.name === "options") {
51
+ for (const key2 in property.value.properties) {
52
+ const option = property.value.properties[key2];
53
+ if (option.type === "ObjectProperty") {
54
+ options[option.key.name] = option.value.value;
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+ options.type = false;
61
+ data.push({
62
+ exportName: declaration.id.name,
63
+ options,
64
+ // object: declaration.init,
65
+ path
66
+ });
67
+ }
68
+ }
69
+ }
70
+ return data;
71
+ }
72
+ parseInterfaceDeclarations(ast, find = "", path = "") {
73
+ const data = [];
74
+ for (const item of this.parseTSInterfaceDeclaration(ast)) {
75
+ if (!item?.declaration?.extends)
76
+ continue;
77
+ for (const declaration of item?.declaration?.extends) {
78
+ if (declaration.expression.name === find) {
79
+ const options = {};
80
+ options.type = true;
81
+ data.push({
82
+ exportName: item.declaration.id.name,
83
+ options,
84
+ // object: declaration.init,
85
+ path
86
+ });
87
+ }
88
+ }
89
+ }
90
+ return data;
91
+ }
92
+ // private parsePlugin(ast: any, path: string = '') {
93
+ // const data = {
94
+ // export: [],
95
+ // name: '',
96
+ // path: '',
97
+ // } as DataTypePlugin
98
+ // for (const item of this.parseVariableDeclaration(ast)) {
99
+ // for (const declaration of item.declaration.declarations) {
100
+ // if (declaration.init.callee?.name === 'defineSilgiModule') {
101
+ // if (declaration.init.arguments) {
102
+ // for (const argument of declaration.init.arguments) {
103
+ // for (const propertie of argument.properties) {
104
+ // if (propertie.key.name === 'name')
105
+ // data.name = propertie.value.value
106
+ // }
107
+ // }
108
+ // }
109
+ // data.export.push({
110
+ // name: data.name,
111
+ // as: camelCase(`${data.name}DefineSilgiModule`),
112
+ // type: false,
113
+ // })
114
+ // }
115
+ // }
116
+ // }
117
+ // for (const item of this.parseTSInterfaceDeclaration(ast)) {
118
+ // if (!item?.declaration?.extends)
119
+ // continue
120
+ // for (const declaration of item?.declaration?.extends) {
121
+ // if (declaration.expression.name === 'ModuleOptions') {
122
+ // data.export.push({
123
+ // name: item.declaration.id.name,
124
+ // as: camelCase(`${data.name}ModuleOptions`),
125
+ // type: true,
126
+ // })
127
+ // }
128
+ // // TODO add other plugins
129
+ // }
130
+ // }
131
+ // data.path = path
132
+ // return data
133
+ // }
134
+ }
135
+
136
+ export { SchemaParser };
@@ -0,0 +1,114 @@
1
+ import { ErrorFactory, HttpStatus, SilgiError } from './error.mjs';
2
+ import { useSilgi, normalizeResult } from './unctx.mjs';
3
+ import { parseURI } from './uris/utils.mjs';
4
+ import { useSilgiStorage, generateStorageKey } from './utils/storage.mjs';
5
+
6
+ function silgi(event) {
7
+ return {
8
+ execute: (uriString, input) => {
9
+ return execute(uriString, input, event);
10
+ }
11
+ };
12
+ }
13
+ async function execute(uriString, input, event) {
14
+ const silgiCtx = useSilgi();
15
+ if (event) {
16
+ await silgiCtx.callHook("event:before", event);
17
+ }
18
+ let success = false;
19
+ let cached = false;
20
+ let result;
21
+ try {
22
+ const operation = parseURI(uriString, silgiCtx.uris);
23
+ if (!operation) {
24
+ throw ErrorFactory.create({ message: "Invalid URI", httpStatus: HttpStatus.BAD_REQUEST });
25
+ }
26
+ const handler = silgiCtx.scannedHandlers.get(operation.uri);
27
+ if (!handler) {
28
+ throw ErrorFactory.create({
29
+ message: "Action not found",
30
+ httpStatus: HttpStatus.NOT_FOUND,
31
+ context: {
32
+ uri: uriString
33
+ }
34
+ });
35
+ }
36
+ await silgiCtx.callHook("action:before", {
37
+ operation,
38
+ input,
39
+ event,
40
+ modules: handler.modules,
41
+ result
42
+ });
43
+ const cacheData = await cacheExecute(input, operation, handler, event);
44
+ if (cacheData?.success) {
45
+ result = cacheData.data;
46
+ success = cacheData.success;
47
+ cached = cacheData.cached;
48
+ } else {
49
+ const router = {
50
+ params: operation.routerParams,
51
+ query: operation.query
52
+ };
53
+ silgiCtx.shared.silgi = (_event) => silgi(_event || event);
54
+ result = await handler?.handler(router, input, silgiCtx.shared, event);
55
+ success = true;
56
+ }
57
+ await silgiCtx.callHook("action:after", {
58
+ operation,
59
+ input,
60
+ event,
61
+ result,
62
+ success,
63
+ modules: handler.modules
64
+ });
65
+ if (!cached) {
66
+ if (success && cacheData?.cachedKey && handler.storage) {
67
+ await useSilgiStorage(handler.storage.base).setItem(cacheData.cachedKey, result, handler.storage.options);
68
+ }
69
+ }
70
+ return result;
71
+ } catch (err) {
72
+ await silgiCtx.callHook("action:error", {
73
+ input,
74
+ event,
75
+ error: err instanceof Error ? err : new Error(String(err)),
76
+ timestamp: Date.now()
77
+ });
78
+ silgiCtx.captureError(SilgiError.from(err), {
79
+ event,
80
+ tags: ["execute"]
81
+ });
82
+ throw err;
83
+ }
84
+ }
85
+ async function cacheExecute(input, operation, handler, event) {
86
+ if (!handler.storage)
87
+ return;
88
+ const cacheKey = handler.storage ? await generateStorageKey({
89
+ operation,
90
+ input,
91
+ keyGenerator: handler.storage.key,
92
+ storageOptions: handler.storage,
93
+ requestId: event?.requestId
94
+ }) : null;
95
+ if (cacheKey) {
96
+ const cachedResult = await useSilgiStorage(handler.storage.base).getItem(cacheKey);
97
+ if (cachedResult !== null) {
98
+ return {
99
+ success: true,
100
+ data: normalizeResult(cachedResult),
101
+ cached: true,
102
+ cachedKey: cacheKey
103
+ };
104
+ }
105
+ }
106
+ return {
107
+ success: false,
108
+ data: null,
109
+ cached: false,
110
+ cachedKey: cacheKey
111
+ };
112
+ }
113
+
114
+ export { execute, silgi };
@@ -0,0 +1,15 @@
1
+ import { getContext } from 'unctx';
2
+
3
+ const silgiCLICtx = getContext("SilgiCLI");
4
+ function useSilgiCLI() {
5
+ const instance = silgiCLICtx.tryUse();
6
+ if (!instance) {
7
+ throw new Error("Silgi instance is unavailable!");
8
+ }
9
+ return instance;
10
+ }
11
+ function tryUseSilgiCLI() {
12
+ return silgiCLICtx.tryUse();
13
+ }
14
+
15
+ export { silgiCLICtx, tryUseSilgiCLI, useSilgiCLI };
@@ -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
+ console.error("Property not found:", part);
11
+ break;
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,127 @@
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
+ return {
73
+ namespaceName,
74
+ serviceName,
75
+ methodName,
76
+ actionName,
77
+ raw: normalizedUri,
78
+ parts: [namespaceName, serviceName, methodName, actionName],
79
+ routerParams: {},
80
+ query: method ? void 0 : query,
81
+ uri: baseUri
82
+ };
83
+ }
84
+ const routeTemplate = typeof template === "string" ? template : template.pattern;
85
+ const validators = typeof template === "string" ? void 0 : template.validators;
86
+ const routerParams = {};
87
+ const templateParts = routeTemplate.split("/").filter(Boolean);
88
+ const paramValues = parameters;
89
+ let valueIndex = 0;
90
+ templateParts.forEach((part) => {
91
+ if (part.startsWith(":")) {
92
+ const paramName = part.substring(1);
93
+ const paramValue = paramValues[valueIndex];
94
+ if (validators?.[paramName] && paramValue) {
95
+ if (!validators[paramName](paramValue)) {
96
+ throw ErrorFactory.create({
97
+ message: "Invalid value for parameter",
98
+ httpStatus: HttpStatus.UNPROCESSABLE_ENTITY,
99
+ context: {
100
+ uri,
101
+ paramName,
102
+ paramValue,
103
+ validatorName: paramName
104
+ }
105
+ });
106
+ }
107
+ }
108
+ routerParams[paramName] = paramValue || void 0;
109
+ valueIndex++;
110
+ } else if (part && part === paramValues[valueIndex]) {
111
+ valueIndex++;
112
+ }
113
+ });
114
+ return {
115
+ namespaceName,
116
+ serviceName,
117
+ methodName,
118
+ actionName,
119
+ raw: normalizedUri,
120
+ parts: [namespaceName, serviceName, methodName, actionName],
121
+ routerParams,
122
+ query: method ? void 0 : query,
123
+ uri: baseUri
124
+ };
125
+ }
126
+
127
+ export { parseURI };
@@ -0,0 +1,5 @@
1
+ function getEvent(event) {
2
+ return event.event || event || {};
3
+ }
4
+
5
+ export { getEvent };
@@ -0,0 +1,12 @@
1
+ import { lstatSync } from 'node:fs';
2
+ import { dirname, isAbsolute } from 'pathe';
3
+
4
+ function getDirectory(p) {
5
+ try {
6
+ return isAbsolute(p) && lstatSync(p).isFile() ? dirname(p) : p;
7
+ } catch {
8
+ }
9
+ return p;
10
+ }
11
+
12
+ export { getDirectory };
@@ -0,0 +1,25 @@
1
+ function merge(items, maxLevel = 4, currentLevel = 3) {
2
+ const arrayItems = Array.isArray(items) ? items : [items];
3
+ return arrayItems.reduce((acc, item) => {
4
+ Object.keys(item).forEach((key) => {
5
+ if (typeof item[key] === "object" && item[key] !== null && currentLevel < maxLevel) {
6
+ acc[key] = acc[key] || {};
7
+ acc[key] = merge([acc[key], item[key]], maxLevel, currentLevel + 1);
8
+ } else {
9
+ acc[key] = item[key];
10
+ }
11
+ });
12
+ return acc;
13
+ }, {});
14
+ }
15
+ function mergeSchemas(typesOrArray) {
16
+ return merge(typesOrArray);
17
+ }
18
+ function mergeServices(servicesOrArray) {
19
+ return merge(servicesOrArray);
20
+ }
21
+ function mergeShared(sharedOrArray) {
22
+ return merge(sharedOrArray, 1, 1);
23
+ }
24
+
25
+ export { mergeSchemas, mergeServices, mergeShared };
@@ -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,70 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { klona } from 'klona';
3
+ import { createStorage as createStorage$1, builtinDrivers, prefixStorage } from 'unstorage';
4
+ import memoryDriver from 'unstorage/drivers/memory';
5
+ import { ErrorFactory, HttpStatus } from '../error.mjs';
6
+ import { useSilgi } from '../unctx.mjs';
7
+
8
+ async function createStorage(silgi) {
9
+ const storage = createStorage$1();
10
+ const mounts = klona({
11
+ ...silgi.options.storage,
12
+ ...silgi.options.devStorage
13
+ });
14
+ for (const [path, opts] of Object.entries(mounts)) {
15
+ if (opts.driver) {
16
+ const driver = await import(builtinDrivers[opts.driver] || opts.driver).then((r) => r.default || r);
17
+ storage.mount("/memory:cache", memoryDriver());
18
+ storage.mount(path, driver(opts));
19
+ } else {
20
+ silgi.logger.warn(`No \`driver\` set for storage mount point "${path}".`);
21
+ }
22
+ }
23
+ return storage;
24
+ }
25
+ function useSilgiStorage(base = "/memory:cache") {
26
+ const silgi = useSilgi();
27
+ return base ? prefixStorage(silgi.storage, base) : silgi.storage;
28
+ }
29
+ async function generateStorageKey(params) {
30
+ const {
31
+ operation,
32
+ input,
33
+ keyGenerator,
34
+ requestId,
35
+ storageOptions
36
+ } = params;
37
+ const cacheScopePrefix = storageOptions?.scope === "request" ? "req" : "global";
38
+ const parts = [
39
+ cacheScopePrefix,
40
+ // Always include scope prefix first
41
+ operation.namespaceName,
42
+ operation.serviceName,
43
+ operation.methodName
44
+ ].filter(Boolean);
45
+ if (storageOptions?.scope === "request") {
46
+ if (!requestId) {
47
+ throw ErrorFactory.create({
48
+ code: HttpStatus.BAD_REQUEST,
49
+ message: "Request ID is required for request-scoped cache",
50
+ context: {
51
+ requestId,
52
+ operation,
53
+ input,
54
+ storageOptions,
55
+ keyGenerator
56
+ }
57
+ });
58
+ }
59
+ parts.push(requestId);
60
+ }
61
+ if (keyGenerator) {
62
+ const customKey = await Promise.resolve(keyGenerator(input));
63
+ parts.push(customKey);
64
+ } else {
65
+ parts.push(typeof input === "object" ? JSON.stringify(input) : String(input));
66
+ }
67
+ return Buffer.from(parts.join(":")).toString("base64");
68
+ }
69
+
70
+ export { createStorage, generateStorageKey, useSilgiStorage };
@@ -1,62 +1 @@
1
- import { execSync } from 'node:child_process';
2
- import { fileURLToPath } from 'node:url';
3
- import { resolve, dirname, join } from 'pathe';
4
- import { loadOptions } from 'silgi/core';
5
- import { relativeWithDot } from 'silgi/kit';
6
- import { autoImportTypes } from 'silgi/types';
7
-
8
- const module = {
9
- name: "silgi",
10
- setup: async (nitro) => {
11
- const silgiConfig = await loadOptions({});
12
- nitro.options.plugins = nitro.options.plugins || [];
13
- nitro.options.plugins.push(
14
- fileURLToPath(new URL("runtime/plugin", import.meta.url))
15
- );
16
- const tsConfigPath = resolve(
17
- nitro.options.buildDir,
18
- nitro.options.typescript.tsconfigPath
19
- );
20
- const tsconfigDir = dirname(tsConfigPath);
21
- nitro.options.typescript.strict = true;
22
- nitro.options.typescript.tsConfig ??= {};
23
- nitro.options.typescript.tsConfig.extends = "./silgi.tsconfig.json";
24
- nitro.options.typescript.tsConfig.include ??= [];
25
- nitro.options.typescript.tsConfig.exclude ??= [];
26
- if (silgiConfig.typescript.internalPaths || silgiConfig.stub) {
27
- nitro.options.alias ||= {};
28
- nitro.options.alias = {
29
- ...nitro.options.alias,
30
- ...silgiConfig.alias
31
- // 'silgi/runtime': fileURLToPath(`${new URL(pkgDir, import.meta.url)}dist/runtime/`),
32
- };
33
- }
34
- nitro.options.typescript.tsConfig.include.push(join(relativeWithDot(tsconfigDir, silgiConfig.build.dir), "**/*"));
35
- nitro.options.typescript.tsConfig.compilerOptions ??= {};
36
- nitro.options.typescript.tsConfig.compilerOptions.paths ??= {};
37
- nitro.hooks.hook("types:extend", async () => {
38
- const isStub = silgiConfig.stub ? "pnpm silgi prepare --stub" : "pnpm silgi prepare";
39
- execSync(isStub, { stdio: "inherit", cwd: nitro.options.rootDir, env: process.env });
40
- });
41
- nitro.options.exportConditions ??= [];
42
- nitro.options.exportConditions.push(...silgiConfig.conditions);
43
- if (nitro.options.imports) {
44
- nitro.options.imports.dirs ??= [];
45
- nitro.options.imports.dirs.push(
46
- join(silgiConfig.silgi.serverDir, "**/*")
47
- );
48
- nitro.options.imports.presets ??= [];
49
- nitro.options.imports.presets.push({
50
- from: "silgi",
51
- imports: ["silgi"]
52
- });
53
- nitro.options.imports.presets.push({
54
- from: "silgi/types",
55
- imports: autoImportTypes.map((type) => type),
56
- type: true
57
- });
58
- }
59
- }
60
- };
61
-
62
- export { module as default };
1
+ export { default } from './module.mjs';