silgi 0.5.0 → 0.7.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.
@@ -23,7 +23,7 @@ const module = {
23
23
  nitro.options.typescript.tsConfig.extends = "./silgi.tsconfig.json";
24
24
  nitro.options.typescript.tsConfig.include ??= [];
25
25
  nitro.options.typescript.tsConfig.exclude ??= [];
26
- if (silgiConfig.typescript.internalPaths) {
26
+ if (silgiConfig.typescript.internalPaths || silgiConfig.stub) {
27
27
  nitro.options.alias ||= {};
28
28
  nitro.options.alias = {
29
29
  ...nitro.options.alias,
@@ -35,8 +35,11 @@ const module = {
35
35
  nitro.options.typescript.tsConfig.compilerOptions ??= {};
36
36
  nitro.options.typescript.tsConfig.compilerOptions.paths ??= {};
37
37
  nitro.hooks.hook("types:extend", async () => {
38
- execSync("pnpm silgi prepare", { stdio: "inherit", cwd: nitro.options.rootDir, env: process.env });
38
+ const isStub = silgiConfig.stub ? "pnpm silgi prepare --stub" : "pnpm silgi prepare";
39
+ execSync(isStub, { stdio: "inherit", cwd: nitro.options.rootDir, env: process.env });
39
40
  });
41
+ nitro.options.exportConditions ??= [];
42
+ nitro.options.exportConditions.push(...silgiConfig.conditions);
40
43
  if (nitro.options.imports) {
41
44
  nitro.options.imports.dirs ??= [];
42
45
  nitro.options.imports.dirs.push(
@@ -1,22 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
- import { GraphQLSchema } from 'graphql';
3
- import { SilgiCLI, GraphQLJSON, SilgiPreset, SilgiPresetMeta } from 'silgi/types';
4
- import { M as ModuleOptions, a as ModuleDefinition, S as SilgiModule } from '../shared/silgi.CN_giHTc.mjs';
5
- import 'defu';
6
- import '@graphql-tools/utils';
7
- import 'pkg-types';
8
- import '@standard-schema/spec';
9
- import 'consola';
10
- import 'hookable';
11
- import 'ignore';
12
- import 'silgi/presets';
13
- import 'unimport';
14
- import 'unstorage';
15
- import 'c12';
16
- import 'chokidar';
17
- import 'compatx';
18
- import 'unimport/unplugin';
19
- import 'std-env';
2
+ import * as consola from 'consola';
3
+ import { ConsolaOptions } from 'consola';
4
+ import { ModuleOptionsCustom, ModuleDefinition, SilgiModule, SilgiCLI, SilgiPreset, SilgiPresetMeta, SilgiTemplate, ResolvedSilgiTemplate } from 'silgi/types';
20
5
 
21
6
  /**
22
7
  * Resolve a module from a given root path using an algorithm patterned on
@@ -29,20 +14,18 @@ declare function tryResolveModule(id: string, url?: string | string[]): Promise<
29
14
  declare function writeFile(file: string, contents: Buffer | string, log?: boolean): Promise<void>;
30
15
  declare function isDirectory(path: string): Promise<boolean>;
31
16
 
32
- declare function exportGraphQLInterfaceType(silgi: SilgiCLI, schema: GraphQLSchema): void;
33
-
34
- declare function extractGraphQLSchemaToJSON(schema: GraphQLSchema): GraphQLJSON;
35
-
36
17
  declare function isNuxt(): boolean;
37
18
  declare function isNitro(): boolean;
38
19
  declare function isH3(): boolean;
39
20
 
21
+ declare function useLogger(tag?: string, options?: Partial<ConsolaOptions>): consola.ConsolaInstance;
22
+
40
23
  /**
41
24
  * Define a Silgi module, automatically merging defaults with user provided options, installing
42
25
  * any hooks that are provided, and calling an optional setup function for full control.
43
26
  */
44
- declare function defineSilgiModule<TOptions extends ModuleOptions>(definition: ModuleDefinition<TOptions, Partial<TOptions>, false> | SilgiModule<TOptions, Partial<TOptions>, false>): SilgiModule<TOptions, TOptions, false>;
45
- declare function defineSilgiModule<TOptions extends ModuleOptions>(): {
27
+ declare function defineSilgiModule<TOptions extends ModuleOptionsCustom>(definition: ModuleDefinition<TOptions, Partial<TOptions>, false> | SilgiModule<TOptions, Partial<TOptions>, false>): SilgiModule<TOptions, TOptions, false>;
28
+ declare function defineSilgiModule<TOptions extends ModuleOptionsCustom>(): {
46
29
  with: <TOptionsDefaults extends Partial<TOptions>>(definition: ModuleDefinition<TOptions, TOptionsDefaults, true> | SilgiModule<TOptions, TOptionsDefaults, true>) => SilgiModule<TOptions, TOptionsDefaults, true>;
47
30
  };
48
31
 
@@ -88,6 +71,25 @@ interface Resolver {
88
71
  declare function createResolver(base: string | URL): Resolver;
89
72
  declare function resolveSilgiModule(base: string, paths: string[]): Promise<string[]>;
90
73
 
74
+ /**
75
+ * Renders given template during build into the virtual file system (and optionally to disk in the project `buildDir`)
76
+ */
77
+ declare function addTemplate<T>(_template: SilgiTemplate<T> | string): ResolvedSilgiTemplate<T>;
78
+ /**
79
+ * Normalize a silgi template object
80
+ */
81
+ declare function normalizeTemplate<T>(template: SilgiTemplate<T> | string, buildDir?: string): ResolvedSilgiTemplate<T>;
82
+
91
83
  declare function relativeWithDot(from: string, to: string): string;
84
+ /** @since 3.9.0 */
85
+ declare function toArray<T>(value: T | T[]): T[];
86
+ /**
87
+ * Filter out items from an array in place. This function mutates the array.
88
+ * `predicate` get through the array from the end to the start for performance.
89
+ *
90
+ * This function should be faster than `Array.prototype.filter` on large arrays.
91
+ */
92
+ declare function filterInPlace<T>(array: T[], predicate: (item: T, index: number, arr: T[]) => unknown): T[];
93
+ declare const MODE_RE: RegExp;
92
94
 
93
- export { createResolver, defineSilgiModule, defineSilgiPreset, exportGraphQLInterfaceType, extractGraphQLSchemaToJSON, isDirectory, isH3, isNitro, isNuxt, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, tryResolveModule, writeFile };
95
+ export { MODE_RE, addTemplate, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, writeFile };
@@ -1,22 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
- import { GraphQLSchema } from 'graphql';
3
- import { SilgiCLI, GraphQLJSON, SilgiPreset, SilgiPresetMeta } from 'silgi/types';
4
- import { M as ModuleOptions, a as ModuleDefinition, S as SilgiModule } from '../shared/silgi.CN_giHTc.js';
5
- import 'defu';
6
- import '@graphql-tools/utils';
7
- import 'pkg-types';
8
- import '@standard-schema/spec';
9
- import 'consola';
10
- import 'hookable';
11
- import 'ignore';
12
- import 'silgi/presets';
13
- import 'unimport';
14
- import 'unstorage';
15
- import 'c12';
16
- import 'chokidar';
17
- import 'compatx';
18
- import 'unimport/unplugin';
19
- import 'std-env';
2
+ import * as consola from 'consola';
3
+ import { ConsolaOptions } from 'consola';
4
+ import { ModuleOptionsCustom, ModuleDefinition, SilgiModule, SilgiCLI, SilgiPreset, SilgiPresetMeta, SilgiTemplate, ResolvedSilgiTemplate } from 'silgi/types';
20
5
 
21
6
  /**
22
7
  * Resolve a module from a given root path using an algorithm patterned on
@@ -29,20 +14,18 @@ declare function tryResolveModule(id: string, url?: string | string[]): Promise<
29
14
  declare function writeFile(file: string, contents: Buffer | string, log?: boolean): Promise<void>;
30
15
  declare function isDirectory(path: string): Promise<boolean>;
31
16
 
32
- declare function exportGraphQLInterfaceType(silgi: SilgiCLI, schema: GraphQLSchema): void;
33
-
34
- declare function extractGraphQLSchemaToJSON(schema: GraphQLSchema): GraphQLJSON;
35
-
36
17
  declare function isNuxt(): boolean;
37
18
  declare function isNitro(): boolean;
38
19
  declare function isH3(): boolean;
39
20
 
21
+ declare function useLogger(tag?: string, options?: Partial<ConsolaOptions>): consola.ConsolaInstance;
22
+
40
23
  /**
41
24
  * Define a Silgi module, automatically merging defaults with user provided options, installing
42
25
  * any hooks that are provided, and calling an optional setup function for full control.
43
26
  */
44
- declare function defineSilgiModule<TOptions extends ModuleOptions>(definition: ModuleDefinition<TOptions, Partial<TOptions>, false> | SilgiModule<TOptions, Partial<TOptions>, false>): SilgiModule<TOptions, TOptions, false>;
45
- declare function defineSilgiModule<TOptions extends ModuleOptions>(): {
27
+ declare function defineSilgiModule<TOptions extends ModuleOptionsCustom>(definition: ModuleDefinition<TOptions, Partial<TOptions>, false> | SilgiModule<TOptions, Partial<TOptions>, false>): SilgiModule<TOptions, TOptions, false>;
28
+ declare function defineSilgiModule<TOptions extends ModuleOptionsCustom>(): {
46
29
  with: <TOptionsDefaults extends Partial<TOptions>>(definition: ModuleDefinition<TOptions, TOptionsDefaults, true> | SilgiModule<TOptions, TOptionsDefaults, true>) => SilgiModule<TOptions, TOptionsDefaults, true>;
47
30
  };
48
31
 
@@ -88,6 +71,25 @@ interface Resolver {
88
71
  declare function createResolver(base: string | URL): Resolver;
89
72
  declare function resolveSilgiModule(base: string, paths: string[]): Promise<string[]>;
90
73
 
74
+ /**
75
+ * Renders given template during build into the virtual file system (and optionally to disk in the project `buildDir`)
76
+ */
77
+ declare function addTemplate<T>(_template: SilgiTemplate<T> | string): ResolvedSilgiTemplate<T>;
78
+ /**
79
+ * Normalize a silgi template object
80
+ */
81
+ declare function normalizeTemplate<T>(template: SilgiTemplate<T> | string, buildDir?: string): ResolvedSilgiTemplate<T>;
82
+
91
83
  declare function relativeWithDot(from: string, to: string): string;
84
+ /** @since 3.9.0 */
85
+ declare function toArray<T>(value: T | T[]): T[];
86
+ /**
87
+ * Filter out items from an array in place. This function mutates the array.
88
+ * `predicate` get through the array from the end to the start for performance.
89
+ *
90
+ * This function should be faster than `Array.prototype.filter` on large arrays.
91
+ */
92
+ declare function filterInPlace<T>(array: T[], predicate: (item: T, index: number, arr: T[]) => unknown): T[];
93
+ declare const MODE_RE: RegExp;
92
94
 
93
- export { createResolver, defineSilgiModule, defineSilgiPreset, exportGraphQLInterfaceType, extractGraphQLSchemaToJSON, isDirectory, isH3, isNitro, isNuxt, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, tryResolveModule, writeFile };
95
+ export { MODE_RE, addTemplate, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, writeFile };
@@ -1,19 +1,18 @@
1
1
  import { resolvePath as resolvePath$1 } from 'mlly';
2
2
  import fsp from 'node:fs/promises';
3
3
  import consola, { consola as consola$1 } from 'consola';
4
- import { relative, resolve, dirname, normalize, isAbsolute, join } from 'pathe';
4
+ import { relative, resolve, dirname, normalize, isAbsolute, join, parse, basename } from 'pathe';
5
5
  import { colors } from 'consola/utils';
6
6
  import { getProperty } from 'dot-prop';
7
- import { extractGraphQLSchemaToJSON as extractGraphQLSchemaToJSON$1 } from 'silgi/kit';
8
- import { useSilgi } from 'silgi/core';
7
+ import { useSilgi, tryUseSilgiCLI, useSilgiCLI } from 'silgi/core';
9
8
  import { defu } from 'defu';
10
- import { u as useSilgiCLI, c as checkSilgiCompatibility, t as tryUseSilgiCLI } from '../cli/compatibility.mjs';
9
+ import { c as checkSilgiCompatibility } from '../cli/compatibility.mjs';
11
10
  import { existsSync, promises } from 'node:fs';
12
11
  import { fileURLToPath } from 'node:url';
13
12
  import { resolveAlias as resolveAlias$1 } from 'pathe/utils';
13
+ import { hash } from 'ohash';
14
14
  import 'semver/functions/satisfies.js';
15
15
  import 'silgi/meta';
16
- import 'unctx';
17
16
 
18
17
  async function tryResolveModule(id, url = import.meta.url) {
19
18
  try {
@@ -55,7 +54,7 @@ async function writeFile(file, contents, log = false) {
55
54
  await fsp.writeFile(
56
55
  file,
57
56
  contents,
58
- typeof contents === "string" ? "utf8" : undefined
57
+ typeof contents === "string" ? "utf8" : void 0
59
58
  );
60
59
  if (log) {
61
60
  consola$1.info("Generated", prettyPath(file));
@@ -69,102 +68,6 @@ async function isDirectory$1(path) {
69
68
  }
70
69
  }
71
70
 
72
- function exportGraphQLInterfaceType(silgi, schema) {
73
- const schemasJSON = extractGraphQLSchemaToJSON$1(schema);
74
- for (const [key, schemaValue] of Object.entries(schemasJSON)) {
75
- if (key === "queries") {
76
- for (const [queryKey, _queryValue] of Object.entries(schemaValue)) {
77
- }
78
- }
79
- if (key === "mutations") {
80
- for (const [mutationKey, _mutationValue] of Object.entries(schemaValue)) {
81
- }
82
- }
83
- if (key === "types") {
84
- for (const [typeName, typeValue] of Object.entries(schemaValue)) {
85
- if (typeof typeValue === "string" && typeValue.startsWith("$ref:")) {
86
- const refName = typeValue.replace("$ref:", "");
87
- const refFields = schemasJSON.references?.[refName];
88
- if (refFields) {
89
- for (const fieldName of Object.keys(refFields)) {
90
- }
91
- }
92
- }
93
- }
94
- }
95
- }
96
- }
97
-
98
- function extractGraphQLSchemaToJSON(schema) {
99
- const result = {
100
- queries: {},
101
- mutations: {},
102
- types: {},
103
- inputs: {},
104
- references: {}
105
- // Schema içindeki tüm referansları tutacak
106
- };
107
- function getTypeReference(type) {
108
- if (["GraphQLScalarType", "GraphQLEnumType"].includes(type.constructor.name)) {
109
- return type.name;
110
- }
111
- const typeName = type.name.toLowerCase();
112
- if (!result.references[typeName]) {
113
- const fields = {};
114
- const typeFields = type.constructor.name === "GraphQLInputObjectType" ? type.getFields() : type._fields;
115
- Object.entries(typeFields || {}).forEach(([fieldName, field]) => {
116
- if (field?.type) {
117
- if (["GraphQLObjectType", "GraphQLInputObjectType"].includes(field.type.constructor.name)) {
118
- fields[fieldName] = `$ref:${field.type.name.toLowerCase()}`;
119
- } else {
120
- fields[fieldName] = field.type.name;
121
- }
122
- }
123
- });
124
- result.references[typeName] = fields;
125
- }
126
- return `$ref:${typeName}`;
127
- }
128
- Object.entries(schema.getQueryType()?.getFields() || {}).forEach(([name, field]) => {
129
- result.queries[name] = {
130
- input: {},
131
- output: field.type.name === "String" ? "String" : `$ref:${field.type.name.toLowerCase()}`
132
- };
133
- if (field.args?.length) {
134
- field.args.forEach((arg) => {
135
- result.queries[name].input[arg.name] = getTypeReference(arg.type);
136
- });
137
- }
138
- });
139
- Object.entries(schema.getMutationType()?.getFields() || {}).forEach(([name, field]) => {
140
- result.mutations[name] = {
141
- input: {},
142
- output: field.type.name === "String" ? "String" : `$ref:${field.type.name.toLowerCase()}`
143
- };
144
- if (field.args?.length) {
145
- field.args.forEach((arg) => {
146
- result.mutations[name].input[arg.name] = getTypeReference(arg.type);
147
- });
148
- }
149
- });
150
- Object.entries(schema.getTypeMap()).forEach(([typeName, type]) => {
151
- if (typeName.startsWith("__") || ["Query", "Mutation"].includes(typeName)) {
152
- return;
153
- }
154
- if (["GraphQLScalarType", "GraphQLEnumType"].includes(type.constructor.name)) {
155
- return;
156
- }
157
- const typeKey = typeName.toLowerCase();
158
- if (type.constructor.name === "GraphQLInputObjectType") {
159
- result.inputs[typeKey] = `$ref:${typeKey}`;
160
- } else {
161
- result.types[typeKey] = `$ref:${typeKey}`;
162
- }
163
- getTypeReference(type);
164
- });
165
- return result;
166
- }
167
-
168
71
  function isNuxt() {
169
72
  const silgi = useSilgi();
170
73
  if (silgi.options.present === "nitro" || silgi.options.present === "h3" || silgi.options.present === "nuxt") {
@@ -187,6 +90,11 @@ function isH3() {
187
90
  return false;
188
91
  }
189
92
 
93
+ const logger = consola$1;
94
+ function useLogger(tag, options = {}) {
95
+ return tag ? logger.create(options).withTag(tag) : logger;
96
+ }
97
+
190
98
  function defineSilgiModule(definition) {
191
99
  if (definition) {
192
100
  return _defineSilgiModule(definition);
@@ -335,5 +243,57 @@ const RELATIVE_RE = /^([^.])/;
335
243
  function relativeWithDot(from, to) {
336
244
  return relative(from, to).replace(RELATIVE_RE, "./$1") || ".";
337
245
  }
246
+ function toArray(value) {
247
+ return Array.isArray(value) ? value : [value];
248
+ }
249
+ function filterInPlace(array, predicate) {
250
+ for (let i = array.length; i--; i >= 0) {
251
+ if (!predicate(array[i], i, array)) {
252
+ array.splice(i, 1);
253
+ }
254
+ }
255
+ return array;
256
+ }
257
+ const MODE_RE = /\.(server|client)(\.\w+)*$/;
258
+
259
+ function addTemplate(_template) {
260
+ const silgi = useSilgiCLI();
261
+ const template = normalizeTemplate(_template);
262
+ filterInPlace(silgi.options.build.templates, (p) => normalizeTemplate(p).dst !== template.dst);
263
+ silgi.options.build.templates.push(template);
264
+ return template;
265
+ }
266
+ function normalizeTemplate(template, buildDir) {
267
+ if (!template) {
268
+ throw new Error(`Invalid template: ${JSON.stringify(template)}`);
269
+ }
270
+ if (typeof template === "string") {
271
+ template = { src: template };
272
+ } else {
273
+ template = { ...template };
274
+ }
275
+ if (template.src) {
276
+ if (!existsSync(template.src)) {
277
+ throw new Error(`Template not found: ${template.src}`);
278
+ }
279
+ if (!template.filename) {
280
+ const srcPath = parse(template.src);
281
+ template.filename = template.fileName || `${basename(srcPath.dir)}.${srcPath.name}.${hash(template.src)}${srcPath.ext}`;
282
+ }
283
+ }
284
+ if (!template.src && !template.getContents) {
285
+ throw new Error(`Invalid template. Either \`getContents\` or \`src\` should be provided: ${JSON.stringify(template)}`);
286
+ }
287
+ if (!template.filename) {
288
+ throw new Error(`Invalid template. \`filename\` must be provided: ${JSON.stringify(template)}`);
289
+ }
290
+ if (template.filename.endsWith(".d.ts")) {
291
+ template.write = true;
292
+ }
293
+ if (!template.dst) {
294
+ template.dst = resolve(buildDir ?? useSilgiCLI().options.silgi.serverDir, template.filename);
295
+ }
296
+ return template;
297
+ }
338
298
 
339
- export { createResolver, defineSilgiModule, defineSilgiPreset, exportGraphQLInterfaceType, extractGraphQLSchemaToJSON, isDirectory$1 as isDirectory, isH3, isNitro, isNuxt, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, tryResolveModule, writeFile };
299
+ export { MODE_RE, addTemplate, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, isDirectory$1 as isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, toArray, tryResolveModule, useLogger, writeFile };
@@ -1,3 +1,3 @@
1
- const version = "0.5.0";
1
+ const version = "0.7.0";
2
2
 
3
3
  export { version };
@@ -1,3 +1,3 @@
1
- const version = "0.5.0";
1
+ const version = "0.7.0";
2
2
 
3
3
  export { version };
@@ -1,4 +1,4 @@
1
- import { createError, defineEventHandler, getQuery, H3Error, readBody, setResponseStatus } from "h3";
1
+ import { createError, defineEventHandler, getQuery, H3Error, readBody } 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;
@@ -30,7 +30,6 @@ export default async function addNitroApp(silgiCtx = useSilgi()) {
30
30
  }
31
31
  } catch (error) {
32
32
  if (error instanceof H3Error) {
33
- setResponseStatus(event, error.statusCode);
34
33
  throw createError({
35
34
  statusCode: error.statusCode,
36
35
  message: error.message,
@@ -6,6 +6,6 @@ declare function silgi(event?: SilgiEvent | Record<string, any>): {
6
6
  };
7
7
 
8
8
  declare function createStorage(silgi: Silgi): Promise<Storage<StorageValue>>;
9
- declare function useStorage<T extends StorageValue = StorageValue>(base?: StorageConfig<T>['base']): Storage<T>;
9
+ declare function useSilgiStorage<T extends StorageValue = StorageValue>(base?: StorageConfig<T>['base'] | (string & {})): Storage<T>;
10
10
 
11
- export { createStorage as c, silgi as s, useStorage as u };
11
+ export { createStorage as c, silgi as s, useSilgiStorage as u };
@@ -6,6 +6,6 @@ declare function silgi(event?: SilgiEvent | Record<string, any>): {
6
6
  };
7
7
 
8
8
  declare function createStorage(silgi: Silgi): Promise<Storage<StorageValue>>;
9
- declare function useStorage<T extends StorageValue = StorageValue>(base?: StorageConfig<T>['base']): Storage<T>;
9
+ declare function useSilgiStorage<T extends StorageValue = StorageValue>(base?: StorageConfig<T>['base'] | (string & {})): Storage<T>;
10
10
 
11
- export { createStorage as c, silgi as s, useStorage as u };
11
+ export { createStorage as c, silgi as s, useSilgiStorage as u };