silgi 0.4.12 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,44 +1,55 @@
1
- import { createError, defineEventHandler, getQuery, readBody } from "h3";
1
+ import { createError, defineEventHandler, getQuery, H3Error, readBody, setResponseStatus } from "h3";
2
2
  import { silgi, SilgiError, useSilgi } from "silgi/core";
3
3
  export default async function addNitroApp(silgiCtx = useSilgi()) {
4
4
  const nitro = silgiCtx.framework;
5
- nitro.router.use("/srn/**", defineEventHandler({
6
- handler: async (event) => {
5
+ nitro.router.use("/srn/**", defineEventHandler(async (event) => {
6
+ try {
7
7
  const ctx = useSilgi();
8
- if (ctx.options.environment === "nitrojs" || ctx.options.environment === "h3") {
9
- const silgiConnect = silgi(event);
10
- const query = getQuery(event);
11
- const body = await readBody(event).catch(() => {
8
+ if (ctx.options.environment !== "nitrojs" && ctx.options.environment !== "h3") {
9
+ event.node.res.statusCode = 400;
10
+ return createError({
11
+ statusCode: 400,
12
+ statusMessage: "Bad Request"
12
13
  });
13
- let newPath = event.path;
14
- if (event.path.includes("?")) {
15
- newPath = `${event.path}&method=${event.method}`;
16
- } else {
17
- newPath = `${event.path}?method=${event.method}`;
18
- }
19
- try {
20
- const data = await silgiConnect.execute(newPath, {
21
- ...query,
22
- ...body
23
- });
24
- if (data.success) {
25
- return data.data;
26
- }
27
- } catch (error) {
28
- if (error instanceof SilgiError && error.code.startsWith("URI_PARSE_ERROR")) {
29
- throw createError({
30
- statusCode: 400,
31
- message: error.message
32
- });
14
+ }
15
+ const silgiConnect = silgi(event);
16
+ const query = getQuery(event);
17
+ const body = await readBody(event).catch(() => ({}));
18
+ let newPath = event.path;
19
+ if (event.path.includes("?")) {
20
+ newPath = `${event.path}&method=${event.method}`;
21
+ } else {
22
+ newPath = `${event.path}?method=${event.method}`;
23
+ }
24
+ const data = await silgiConnect.execute(newPath, {
25
+ ...query,
26
+ ...body
27
+ });
28
+ if (data) {
29
+ return data;
30
+ }
31
+ } catch (error) {
32
+ if (error instanceof H3Error) {
33
+ setResponseStatus(event, error.statusCode);
34
+ throw createError({
35
+ statusCode: error.statusCode,
36
+ message: error.message,
37
+ data: error.data
38
+ // unhandled: false,
39
+ // fatal: false,
40
+ });
41
+ }
42
+ if (SilgiError.isError(error)) {
43
+ throw createError({
44
+ statusCode: error.httpStatus,
45
+ message: error.message,
46
+ data: {
47
+ code: error.code,
48
+ category: error.category,
49
+ severity: error.severity,
50
+ context: error.context
33
51
  }
34
- throw error;
35
- }
36
- return {
37
- path: event.path,
38
- method: event.method,
39
- body,
40
- query
41
- };
52
+ });
42
53
  }
43
54
  }
44
55
  }));
@@ -2,19 +2,18 @@ import { Defu } from 'defu';
2
2
  import { TypeSource, IResolvers } from '@graphql-tools/utils';
3
3
  import { TSConfig } from 'pkg-types';
4
4
  import { StandardSchemaV1 } from '@standard-schema/spec';
5
- import { ConsolaOptions, ConsolaInstance, LogLevel } from 'consola';
5
+ import { LogLevel, ConsolaInstance } from 'consola';
6
6
  import { Hookable } from 'hookable';
7
- import { Ignore, Options } from 'ignore';
7
+ import { Options, Ignore } from 'ignore';
8
8
  import { PresetOptions, PresetName, PresetNameInput } from 'silgi/presets';
9
- import { StorageMounts as StorageMounts$1, SilgiFrameworkInfo as SilgiFrameworkInfo$1, ResolvedServiceType as ResolvedServiceType$1, SilgiDefaultShared as SilgiDefaultShared$1, SilgiModules as SilgiModules$1, ModuleMeta as ModuleMeta$1 } from 'silgi/types';
9
+ import { StorageMounts, SilgiFrameworkInfo as SilgiFrameworkInfo$1, ResolvedServiceType, SilgiDefaultShared, SilgiModules as SilgiModules$1, ModuleMeta as ModuleMeta$1 } from 'silgi/types';
10
10
  import { Unimport } from 'unimport';
11
- import { BuiltinDriverName, TransactionOptions, Storage } from 'unstorage';
11
+ import { Storage } from 'unstorage';
12
12
  import { ResolvedConfig, ConfigWatcher, C12InputConfig, WatchConfigOptions } from 'c12';
13
13
  import { ChokidarOptions } from 'chokidar';
14
14
  import { DateString, CompatibilityDates, CompatibilityDateSpec } from 'compatx';
15
15
  import { UnimportPluginOptions } from 'unimport/unplugin';
16
16
  import { ProviderName } from 'std-env';
17
- import { s as silgi, u as useStorage } from './silgi.40ZJYm8F.js';
18
17
 
19
18
  type HookResult = Promise<void> | void;
20
19
 
@@ -29,12 +28,6 @@ interface SilgiCompatibilityIssues extends Array<SilgiCompatibilityIssue> {
29
28
  toString: () => string;
30
29
  }
31
30
 
32
- /**
33
- * Bu nitrojs, h3 event or request context.
34
- */
35
- interface SilgiEvent extends Record<string, unknown> {
36
- }
37
-
38
31
  interface SilgiSchema {
39
32
  }
40
33
  interface MergedSilgiSchema {
@@ -86,256 +79,6 @@ type ExtractInputFromURI<TURI extends keyof SilgiURIs> = GetInput<ExtractPath<TU
86
79
  type ExtractOutputFromURI<TURI extends keyof SilgiURIs> = GetOutput<ExtractPath<TURI>>;
87
80
  type ExtractRouterParamsFromURI<TURI extends keyof SilgiURIs> = GetRouterParams<ExtractPath<TURI>>;
88
81
 
89
- /**
90
- * @example
91
- * namespace: 'coreApi'
92
- * serviceName: 'basket'
93
- * method: 'post'
94
- * action: 'createBasket'
95
- */
96
- interface SilgiOperation {
97
- /**
98
- * 'coreApi'
99
- * 'storageApi'
100
- */
101
- namespaceName: keyof DefaultNamespaces;
102
- /**
103
- * 'basket'
104
- * 'user'
105
- */
106
- serviceName: string;
107
- /**
108
- * 'post'
109
- * 'delete'
110
- */
111
- methodName: string;
112
- /**
113
- * 'createBasket'
114
- * 'deleteUser'
115
- */
116
- actionName: string;
117
- /**
118
- * 'coreApi/basket/post/createBasket'
119
- */
120
- raw: string;
121
- /**
122
- * '/book/basket/getAllBaskets'
123
- */
124
- uri: keyof SilgiURIs;
125
- /**
126
- * ['coreApi', 'book', 'basket', 'getAllBaskets']
127
- */
128
- parts: string[];
129
- /**
130
- * {id: '123', name: 'John', bookId: '456', authorId: '789'}
131
- */
132
- routerParams?: Record<string, string | undefined>;
133
- /**
134
- * url ?limit=10&page=1
135
- * {limit: '10', page: '1'}
136
- */
137
- query?: Record<string, string>;
138
- }
139
-
140
- type CustomDriverName = string & {
141
- _custom?: any;
142
- };
143
- interface StorageMounts {
144
- [path: string]: {
145
- driver: BuiltinDriverName | CustomDriverName;
146
- [option: string]: any;
147
- };
148
- }
149
- interface SilgiStorageBase {
150
- }
151
- type StorageKeyGenerator<TInput> = (input: TInput) => string | Promise<string>;
152
- interface StorageConfig<TInput> {
153
- options: TransactionOptions;
154
- base: '/memory:cache' | keyof SilgiStorageBase;
155
- key?: StorageKeyGenerator<TInput>;
156
- scope?: 'request' | 'global';
157
- }
158
- interface StorageKeyParams<TInput = unknown> {
159
- operation: SilgiOperation;
160
- input: TInput;
161
- requestId?: string;
162
- keyGenerator?: StorageKeyGenerator<TInput>;
163
- storageOptions?: Pick<StorageConfig<TInput>, 'base' | 'options' | 'scope'>;
164
- }
165
-
166
- interface SilgiOptions {
167
- consolaOptions?: Partial<ConsolaOptions>;
168
- builder: 'nuxt' | 'nitrojs';
169
- /**
170
- * Set to `true` to enable debug mode.
171
- *
172
- * At the moment, it prints out hook names and timings on the server, and logs hook arguments as well in the browser.
173
- *
174
- * @default false
175
- */
176
- debug: boolean;
177
- storage: StorageMounts;
178
- [key: string]: any;
179
- }
180
-
181
- interface SilgiAppPlugin {
182
- (silgi: Silgi): Promise<void> | void;
183
- }
184
-
185
- interface CapturedErrorContext {
186
- event?: SilgiEvent;
187
- [key: string]: unknown;
188
- }
189
- type CaptureError = (error: Error, context: CapturedErrorContext) => void;
190
-
191
- /**
192
- * The listeners to Silgi
193
- */
194
- interface SilgiHooks {
195
- /**
196
- * Called after Silgi initialization, when the Silgi instance is ready to work.
197
- * @param silgi The configured Silgi object
198
- * @returns Promise
199
- */
200
- 'ready': (silgi: Silgi) => HookResult;
201
- /**
202
- * Called when silgi instance is gracefully closing.
203
- * @param silgi The configured silgi object
204
- * @returns Promise
205
- */
206
- 'close': (silgi: Silgi) => HookResult;
207
- 'app:setup:start': (silgi: Silgi) => HookResult;
208
- 'action:before': (context: ModuleHookContext) => HookResult;
209
- 'action:after': (context: ModuleHookContext) => HookResult;
210
- 'action:error': (context: ModuleHookContext) => HookResult;
211
- 'action:finally': (context: ModuleHookContext) => HookResult;
212
- 'event:before': (event: SilgiEvent) => HookResult;
213
- 'error': CaptureError;
214
- }
215
-
216
- type SilgiServiceInterface<T extends BaseSchemaType<StandardSchemaV1>> = {
217
- [Action in keyof T]: T[Action] extends Record<string, any> ? {
218
- [Method in keyof T[Action]]: {
219
- input: T[Action][Method]['input'];
220
- output: T[Action][Method]['output'];
221
- router: T[Action][Method]['router'];
222
- };
223
- } : never;
224
- };
225
- type ServiceType<T> = Partial<{
226
- [Namespace in keyof T]: T[Namespace] extends Record<string, any> ? {
227
- [Service in keyof T[Namespace]]?: MethodHandlerType<T[Namespace][Service]>;
228
- } : never;
229
- }>;
230
- type RequiredServiceType<T> = {
231
- [K in keyof T]-?: T[K] extends Record<string, any> ? {
232
- [P in keyof T[K]]-?: MethodHandlerType<T[K][P]>;
233
- } : never;
234
- };
235
- /**
236
- * const test: ResolvedServiceType = {
237
- aaa: {
238
- bbb: {
239
- delete: {
240
- storage: {
241
- handler: () => {}
242
- },
243
- },
244
- },
245
- },
246
- }
247
- */
248
- type ResolvedServiceType = {
249
- [K in string]: {
250
- [P in string]: {
251
- [Q in BaseSilgiMethodType]?: {
252
- [M in string]: ResolvedMethodHandlerType;
253
- };
254
- };
255
- };
256
- };
257
-
258
- interface FrameworkContext {
259
- }
260
- interface Silgi {
261
- schemas: any;
262
- services: ResolvedServiceType;
263
- shared: SilgiDefaultShared;
264
- uris: Record<string, any>;
265
- modulesURIs: Partial<Record<keyof SilgiModules, any>>;
266
- scannedHandlers: Map<string, ResolvedMethodHandlerType>;
267
- plugins: SilgiAppPlugin[];
268
- framework: FrameworkContext;
269
- _ignore?: Ignore;
270
- hooks: Hookable<SilgiHooks & DefaultHooks>;
271
- hook: Silgi['hooks']['hook'];
272
- callHook: Silgi['hooks']['callHook'];
273
- addHooks: Silgi['hooks']['addHooks'];
274
- ready: () => Promise<void>;
275
- close: () => Promise<void>;
276
- logger: ConsolaInstance;
277
- storage: Storage;
278
- options: SilgiOptions & SilgiModuleOptions;
279
- captureError: CaptureError;
280
- }
281
- interface SilgiConfig extends Pick<Silgi, 'services' | 'shared' | 'uris' | 'schemas' | 'modulesURIs' | 'plugins' | 'framework'>, Partial<SilgiModuleOptions> {
282
- options: DeepPartial<SilgiOptions>;
283
- }
284
- type SilgiFunction = typeof silgi;
285
-
286
- interface SilgiDefaultShared extends SilgiModuleShared {
287
- storage: (...data: Parameters<typeof useStorage>) => ReturnType<typeof useStorage>;
288
- silgi: SilgiFunction;
289
- }
290
- interface SilgiModuleShared {
291
- }
292
- interface ExtendShared {
293
- }
294
-
295
- interface MethodSchemaType<Input, Output> {
296
- default: {
297
- input?: Input;
298
- output: Output;
299
- };
300
- handler: (input: Input, shared: SilgiDefaultShared, event: SilgiEvent) => Promise<Output>;
301
- modules?: Partial<ModuleConfigurations>;
302
- storage?: StorageConfig<Input>;
303
- }
304
- type EventHandlerResponse<T = undefined> = T extends undefined ? void : void | T | Promise<T>;
305
- type MethodHandlerType<T> = {
306
- [Action in keyof T]: T[Action] extends Record<string, any> ? {
307
- [Method in keyof T[Action]]: {
308
- default?: {
309
- input?: StandardSchemaV1.InferInput<T[Action][Method]['input']>;
310
- output?: StandardSchemaV1.InferInput<T[Action][Method]['output']>;
311
- };
312
- handler: (router: StandardSchemaV1.InferInput<T[Action][Method]['router']>, input: StandardSchemaV1.InferInput<T[Action][Method]['input']>, shared: SilgiDefaultShared, event: SilgiEvent) => EventHandlerResponse<StandardSchemaV1.InferOutput<T[Action][Method]['output']>>;
313
- modules?: Partial<ModuleConfigurations>;
314
- storage?: StorageConfig<T[Action][Method]['input']>;
315
- };
316
- } : never;
317
- };
318
- interface ResolvedMethodHandlerType {
319
- input?: Partial<BaseSchemaType<any>>;
320
- output: Partial<BaseSchemaType<any>>;
321
- handler: (router: StandardSchemaV1.InferInput<any>, input: StandardSchemaV1.InferInput<any>, shared: SilgiDefaultShared, event: SilgiEvent) => Promise<StandardSchemaV1.InferInput<any>>;
322
- modules?: Partial<ModuleConfigurations>;
323
- storage?: StorageConfig<StandardSchemaV1.InferInput<any>>;
324
- execute: (input: StandardSchemaV1.InferInput<any>, shared: SilgiDefaultShared, event: SilgiEvent) => Promise<StandardSchemaV1.InferInput<any>>;
325
- }
326
- type MethodResponse<T> = {
327
- success: true;
328
- data: T;
329
- cached?: boolean;
330
- } | {
331
- success: false;
332
- error: {
333
- code: string;
334
- message: string;
335
- details?: Record<string, unknown>;
336
- };
337
- };
338
-
339
82
  interface SilgiMethods {
340
83
  }
341
84
  interface SilgiNamespaces {
@@ -354,7 +97,7 @@ type DeepPartial<T> = T extends void ? T : T extends Record<string, any> ? {
354
97
  } : T;
355
98
  type Awaitable<T> = T | Promise<T>;
356
99
  interface CreateScope {
357
- execute: <TURI extends keyof SilgiURIs>(uriString: TURI, input: ExtractInputFromURI<TURI>) => Promise<MethodResponse<ExtractOutputFromURI<TURI>>>;
100
+ execute: <TURI extends keyof SilgiURIs>(uriString: TURI, input: ExtractInputFromURI<TURI>) => Promise<ExtractOutputFromURI<TURI>>;
358
101
  }
359
102
 
360
103
  type SilgiPreset = SilgiCLIConfig | (() => SilgiCLIConfig);
@@ -391,8 +134,8 @@ interface SilgiCLIOptions extends PresetOptions {
391
134
  logLevel: LogLevel;
392
135
  appConfig: AppConfig;
393
136
  appConfigFiles: string[];
394
- storage: StorageMounts$1;
395
- devStorage: StorageMounts$1;
137
+ storage: StorageMounts;
138
+ devStorage: StorageMounts;
396
139
  workspaceDir: string;
397
140
  rootDir: string;
398
141
  srcDir: string;
@@ -574,8 +317,8 @@ interface ImportItem {
574
317
  }
575
318
  interface SilgiCLI {
576
319
  _ignore?: Ignore;
577
- services: ResolvedServiceType$1;
578
- shareds: SilgiDefaultShared$1;
320
+ services: ResolvedServiceType;
321
+ shareds: SilgiDefaultShared;
579
322
  uris: Record<string, any>;
580
323
  schemas: Record<string, any>;
581
324
  modulesURIs: Partial<Record<keyof SilgiModules$1, any>>;
@@ -721,6 +464,63 @@ interface SilgiCLIHooks {
721
464
  }) => HookResult;
722
465
  }
723
466
 
467
+ /**
468
+ * Bu nitrojs, h3 event or request context.
469
+ */
470
+ interface SilgiEvent extends Record<string, unknown> {
471
+ }
472
+
473
+ /**
474
+ * @example
475
+ * namespace: 'coreApi'
476
+ * serviceName: 'basket'
477
+ * method: 'post'
478
+ * action: 'createBasket'
479
+ */
480
+ interface SilgiOperation {
481
+ /**
482
+ * 'coreApi'
483
+ * 'storageApi'
484
+ */
485
+ namespaceName: keyof DefaultNamespaces;
486
+ /**
487
+ * 'basket'
488
+ * 'user'
489
+ */
490
+ serviceName: string;
491
+ /**
492
+ * 'post'
493
+ * 'delete'
494
+ */
495
+ methodName: string;
496
+ /**
497
+ * 'createBasket'
498
+ * 'deleteUser'
499
+ */
500
+ actionName: string;
501
+ /**
502
+ * 'coreApi/basket/post/createBasket'
503
+ */
504
+ raw: string;
505
+ /**
506
+ * '/book/basket/getAllBaskets'
507
+ */
508
+ uri: keyof SilgiURIs;
509
+ /**
510
+ * ['coreApi', 'book', 'basket', 'getAllBaskets']
511
+ */
512
+ parts: string[];
513
+ /**
514
+ * {id: '123', name: 'John', bookId: '456', authorId: '789'}
515
+ */
516
+ routerParams?: Record<string, string | undefined>;
517
+ /**
518
+ * url ?limit=10&page=1
519
+ * {limit: '10', page: '1'}
520
+ */
521
+ query?: Record<string, string>;
522
+ }
523
+
724
524
  interface SilgiModules {
725
525
  }
726
526
  interface SilgiModuleMethods {
@@ -831,4 +631,4 @@ interface SilgiModule<TOptions extends ModuleOptions = ModuleOptions, TOptionsDe
831
631
  }
832
632
  type ModuleConfigurations = SilgiModuleMethods;
833
633
 
834
- export type { ResolvedServiceType as $, AppConfig as A, BaseSchemaType as B, CreateScope as C, DefaultNamespaces as D, ModuleSetupReturn as E, SilgiModule as F, ModuleConfigurations as G, SilgiOperation as H, SilgiPreset as I, SilgiPresetMeta as J, MethodSchemaType as K, LoadConfigOptions as L, ModuleMeta as M, EventHandlerResponse as N, MethodHandlerType as O, ResolvedMethodHandlerType as P, MethodResponse as Q, ResolvedModuleMeta as R, SilgiServiceInterface as S, TSReference as T, SilgiAppPlugin as U, CapturedErrorContext as V, CaptureError as W, SilgiSchema as X, MergedSilgiSchema as Y, ServiceType as Z, RequiredServiceType as _, SilgiCLIOptions as a, SilgiDefaultShared as a0, SilgiModuleShared as a1, ExtendShared as a2, FrameworkContext as a3, Silgi as a4, SilgiConfig as a5, SilgiFunction as a6, ImportItem as a7, SilgiCLI as a8, SilgiCLIDynamicConfig as a9, SilgiFrameworkInfo as aa, NitroBuildInfo as ab, SilgiURIs as ac, URIsTypes as ad, ExtractInputFromURI as ae, ExtractOutputFromURI as af, ExtractRouterParamsFromURI as ag, StorageMounts as ah, SilgiStorageBase as ai, StorageKeyGenerator as aj, StorageConfig as ak, StorageKeyParams as al, SilgiCLIConfig as b, SilgiCLIHooks as c, SilgiCompatibilityIssue as d, SilgiCompatibilityIssues as e, SilgiOptions as f, SilgiEvent as g, SilgiMethods as h, SilgiNamespaces as i, SilgiRouterTypes as j, SilgiHook as k, BaseSilgiMethodType as l, DefaultHooks as m, DeepPartial as n, Awaitable as o, SilgiHooks as p, SilgiModules as q, SilgiModuleMethods as r, SilgiModuleOptions as s, SilgiModuleInput as t, SilgiCompatibility as u, ModuleHookContext as v, ModuleOptions as w, ResolvedModuleOptions as x, ModuleDefinition as y, ModuleSetupInstallResult as z };
634
+ export type { AppConfig as A, BaseSchemaType as B, CreateScope as C, DefaultHooks as D, ModuleSetupReturn as E, SilgiPreset as F, SilgiPresetMeta as G, HookResult as H, SilgiSchema as I, MergedSilgiSchema as J, ImportItem as K, LoadConfigOptions as L, ModuleOptions as M, SilgiCLI as N, SilgiCLIDynamicConfig as O, SilgiFrameworkInfo as P, NitroBuildInfo as Q, ResolvedModuleMeta as R, SilgiModule as S, TSReference as T, SilgiURIs as U, URIsTypes as V, ExtractInputFromURI as W, ExtractOutputFromURI as X, ExtractRouterParamsFromURI as Y, ModuleDefinition as a, SilgiOperation as b, SilgiEvent as c, ModuleConfigurations as d, BaseSilgiMethodType as e, SilgiModules as f, SilgiModuleOptions as g, DeepPartial as h, ModuleHookContext as i, DefaultNamespaces as j, SilgiCLIOptions as k, SilgiCLIConfig as l, SilgiCLIHooks as m, SilgiCompatibilityIssue as n, SilgiCompatibilityIssues as o, SilgiMethods as p, SilgiNamespaces as q, SilgiRouterTypes as r, SilgiHook as s, Awaitable as t, SilgiModuleMethods as u, SilgiModuleInput as v, SilgiCompatibility as w, ModuleMeta as x, ResolvedModuleOptions as y, ModuleSetupInstallResult as z };