nitro-graphql 2.0.0-beta.7 → 2.0.0-beta.72

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 (242) hide show
  1. package/README.md +31 -1196
  2. package/dist/cli/adapter.d.mts +13 -0
  3. package/dist/cli/adapter.mjs +67 -0
  4. package/dist/cli/commands/build.d.mts +21 -0
  5. package/dist/cli/commands/build.mjs +50 -0
  6. package/dist/cli/commands/dev.d.mts +40 -0
  7. package/dist/cli/commands/dev.mjs +74 -0
  8. package/dist/cli/commands/generate.d.mts +25 -0
  9. package/dist/cli/commands/generate.mjs +198 -0
  10. package/dist/cli/commands/index.d.mts +5 -0
  11. package/dist/cli/commands/index.mjs +5 -0
  12. package/dist/cli/commands/init.d.mts +45 -0
  13. package/dist/cli/commands/init.mjs +195 -0
  14. package/dist/cli/commands/validate.d.mts +9 -0
  15. package/dist/cli/commands/validate.mjs +65 -0
  16. package/dist/cli/completions.d.mts +6 -0
  17. package/dist/cli/completions.mjs +32 -0
  18. package/dist/cli/config.d.mts +18 -0
  19. package/dist/cli/config.mjs +19 -0
  20. package/dist/cli/index.d.mts +26 -0
  21. package/dist/cli/index.mjs +315 -0
  22. package/dist/cli/server/debug-handler.d.mts +9 -0
  23. package/dist/cli/server/debug-handler.mjs +68 -0
  24. package/dist/cli/server/dev-server.d.mts +29 -0
  25. package/dist/cli/server/dev-server.mjs +62 -0
  26. package/dist/cli/server/graphql-handler.d.mts +21 -0
  27. package/dist/cli/server/graphql-handler.mjs +43 -0
  28. package/dist/cli/server/health-handler.d.mts +12 -0
  29. package/dist/cli/server/health-handler.mjs +29 -0
  30. package/dist/cli/server/loader.d.mts +33 -0
  31. package/dist/cli/server/loader.mjs +144 -0
  32. package/dist/cli/server/sandbox-handler.d.mts +12 -0
  33. package/dist/cli/server/sandbox-handler.mjs +17 -0
  34. package/dist/cli/server/watcher.d.mts +20 -0
  35. package/dist/cli/server/watcher.mjs +64 -0
  36. package/dist/cli/server/ws-handler.d.mts +28 -0
  37. package/dist/cli/server/ws-handler.mjs +48 -0
  38. package/dist/config.d.mts +2 -0
  39. package/dist/config.mjs +2 -0
  40. package/dist/core/codegen/client.d.mts +42 -0
  41. package/dist/core/codegen/client.mjs +551 -0
  42. package/dist/core/codegen/document-loader.d.mts +9 -0
  43. package/dist/core/codegen/document-loader.mjs +16 -0
  44. package/dist/core/codegen/index.d.mts +8 -0
  45. package/dist/core/codegen/index.mjs +8 -0
  46. package/dist/core/codegen/plugin.d.mts +19 -0
  47. package/dist/core/codegen/plugin.mjs +29 -0
  48. package/dist/core/codegen/runtime.d.mts +17 -0
  49. package/dist/core/codegen/runtime.mjs +61 -0
  50. package/dist/core/codegen/schema-loader.d.mts +27 -0
  51. package/dist/core/codegen/schema-loader.mjs +126 -0
  52. package/dist/core/codegen/server.d.mts +27 -0
  53. package/dist/core/codegen/server.mjs +142 -0
  54. package/dist/core/codegen/validation.d.mts +17 -0
  55. package/dist/core/codegen/validation.mjs +33 -0
  56. package/dist/core/config.d.mts +45 -0
  57. package/dist/core/config.mjs +76 -0
  58. package/dist/core/constants.d.mts +188 -0
  59. package/dist/core/constants.mjs +208 -0
  60. package/dist/core/debug/index.d.mts +2 -0
  61. package/dist/core/debug/index.mjs +2 -0
  62. package/dist/core/debug/template.d.mts +52 -0
  63. package/dist/core/debug/template.mjs +384 -0
  64. package/dist/core/extend/index.d.mts +2 -0
  65. package/dist/core/extend/index.mjs +2 -0
  66. package/dist/core/extend/loader.d.mts +40 -0
  67. package/dist/core/extend/loader.mjs +196 -0
  68. package/dist/core/index.d.mts +33 -0
  69. package/dist/core/index.mjs +28 -0
  70. package/dist/core/manifest.d.mts +56 -0
  71. package/dist/core/manifest.mjs +100 -0
  72. package/dist/core/pubsub/index.d.mts +109 -0
  73. package/dist/core/pubsub/index.mjs +146 -0
  74. package/dist/core/scanning/ast-scanner.d.mts +27 -0
  75. package/dist/core/scanning/ast-scanner.mjs +114 -0
  76. package/dist/core/scanning/common.d.mts +25 -0
  77. package/dist/core/scanning/common.mjs +57 -0
  78. package/dist/core/scanning/directives.d.mts +13 -0
  79. package/dist/core/scanning/directives.mjs +28 -0
  80. package/dist/core/scanning/documents.d.mts +20 -0
  81. package/dist/core/scanning/documents.mjs +36 -0
  82. package/dist/core/scanning/index.d.mts +7 -0
  83. package/dist/core/scanning/index.mjs +7 -0
  84. package/dist/core/scanning/resolvers.d.mts +14 -0
  85. package/dist/core/scanning/resolvers.mjs +58 -0
  86. package/dist/core/scanning/schemas.d.mts +13 -0
  87. package/dist/core/scanning/schemas.mjs +50 -0
  88. package/dist/core/schema/builder.d.mts +59 -0
  89. package/dist/core/schema/builder.mjs +76 -0
  90. package/dist/core/schema/federation.d.mts +33 -0
  91. package/dist/core/schema/federation.mjs +38 -0
  92. package/dist/core/schema/index.d.mts +3 -0
  93. package/dist/core/schema/index.mjs +3 -0
  94. package/dist/core/server/index.d.mts +4 -0
  95. package/dist/core/server/index.mjs +3 -0
  96. package/dist/core/server/sandbox.d.mts +18 -0
  97. package/dist/core/server/sandbox.mjs +38 -0
  98. package/dist/core/server/types.d.mts +54 -0
  99. package/dist/core/server/types.mjs +1 -0
  100. package/dist/core/server/yoga.d.mts +42 -0
  101. package/dist/core/server/yoga.mjs +87 -0
  102. package/dist/core/types/adapter.d.mts +55 -0
  103. package/dist/core/types/adapter.mjs +1 -0
  104. package/dist/core/types/codegen.d.mts +136 -0
  105. package/dist/core/types/codegen.mjs +1 -0
  106. package/dist/core/types/config.d.mts +188 -0
  107. package/dist/core/types/config.mjs +1 -0
  108. package/dist/{utils/define.d.ts → core/types/define.d.mts} +2 -30
  109. package/dist/core/types/define.mjs +1 -0
  110. package/dist/core/types/index.d.mts +5 -0
  111. package/dist/core/types/index.mjs +1 -0
  112. package/dist/core/types/scanning.d.mts +64 -0
  113. package/dist/core/types/scanning.mjs +1 -0
  114. package/dist/{utils/directive-parser.d.ts → core/utils/directive-parser.d.mts} +18 -4
  115. package/dist/{utils/directive-parser.js → core/utils/directive-parser.mjs} +29 -36
  116. package/dist/core/utils/errors.d.mts +77 -0
  117. package/dist/core/utils/errors.mjs +91 -0
  118. package/dist/core/utils/file-io.d.mts +24 -0
  119. package/dist/core/utils/file-io.mjs +45 -0
  120. package/dist/core/utils/imports.d.mts +15 -0
  121. package/dist/core/utils/imports.mjs +24 -0
  122. package/dist/core/utils/index.d.mts +7 -0
  123. package/dist/core/utils/index.mjs +7 -0
  124. package/dist/core/utils/logger.d.mts +18 -0
  125. package/dist/core/utils/logger.mjs +36 -0
  126. package/dist/core/utils/ofetch-templates.d.mts +30 -0
  127. package/dist/core/utils/ofetch-templates.mjs +134 -0
  128. package/dist/core/utils/runtime.d.mts +74 -0
  129. package/dist/core/utils/runtime.mjs +109 -0
  130. package/dist/core/utils/subscribe-templates.d.mts +8 -0
  131. package/dist/core/utils/subscribe-templates.mjs +58 -0
  132. package/dist/core/validation/external-services.d.mts +11 -0
  133. package/dist/core/validation/external-services.mjs +33 -0
  134. package/dist/core/validation/index.d.mts +2 -0
  135. package/dist/core/validation/index.mjs +2 -0
  136. package/dist/core/watcher/index.d.mts +107 -0
  137. package/dist/core/watcher/index.mjs +140 -0
  138. package/dist/define.d.mts +294 -0
  139. package/dist/define.mjs +323 -0
  140. package/dist/index.d.mts +6 -0
  141. package/dist/index.mjs +5 -0
  142. package/dist/nitro/adapter.d.mts +28 -0
  143. package/dist/nitro/adapter.mjs +90 -0
  144. package/dist/{utils/apollo.d.ts → nitro/apollo.d.mts} +3 -3
  145. package/dist/nitro/apollo.mjs +57 -0
  146. package/dist/nitro/codegen.d.mts +18 -0
  147. package/dist/nitro/codegen.mjs +171 -0
  148. package/dist/nitro/config.d.mts +50 -0
  149. package/dist/nitro/config.mjs +55 -0
  150. package/dist/nitro/index.d.mts +45 -0
  151. package/dist/nitro/index.mjs +63 -0
  152. package/dist/{utils/path-resolver.d.ts → nitro/paths.d.mts} +8 -25
  153. package/dist/nitro/paths.mjs +97 -0
  154. package/dist/nitro/rollup.d.mts +6 -0
  155. package/dist/nitro/rollup.mjs +52 -0
  156. package/dist/nitro/routes/apollo-sandbox-script.d.mts +6 -0
  157. package/dist/nitro/routes/apollo-sandbox-script.mjs +12 -0
  158. package/dist/nitro/routes/apollo-server-ws.d.mts +15 -0
  159. package/dist/nitro/routes/apollo-server-ws.mjs +50 -0
  160. package/dist/nitro/routes/apollo-server.d.mts +6 -0
  161. package/dist/nitro/routes/apollo-server.mjs +69 -0
  162. package/dist/{routes/debug.d.ts → nitro/routes/debug.d.mts} +12 -19
  163. package/dist/nitro/routes/debug.mjs +98 -0
  164. package/dist/nitro/routes/graphql-yoga-ws.d.mts +12 -0
  165. package/dist/nitro/routes/graphql-yoga-ws.mjs +50 -0
  166. package/dist/nitro/routes/graphql-yoga.d.mts +12 -0
  167. package/dist/nitro/routes/graphql-yoga.mjs +39 -0
  168. package/dist/nitro/routes/health.d.mts +10 -0
  169. package/dist/{routes/health.js → nitro/routes/health.mjs} +5 -6
  170. package/dist/nitro/setup/extend-loader.d.mts +16 -0
  171. package/dist/nitro/setup/extend-loader.mjs +80 -0
  172. package/dist/nitro/setup/file-watcher.d.mts +18 -0
  173. package/dist/nitro/setup/file-watcher.mjs +62 -0
  174. package/dist/nitro/setup/logging.d.mts +16 -0
  175. package/dist/nitro/setup/logging.mjs +64 -0
  176. package/dist/nitro/setup/rollup-integration.d.mts +24 -0
  177. package/dist/nitro/setup/rollup-integration.mjs +64 -0
  178. package/dist/nitro/setup/routes.d.mts +13 -0
  179. package/dist/nitro/setup/routes.mjs +69 -0
  180. package/dist/nitro/setup/scanner.d.mts +59 -0
  181. package/dist/nitro/setup/scanner.mjs +118 -0
  182. package/dist/nitro/setup/ts-config.d.mts +10 -0
  183. package/dist/nitro/setup/ts-config.mjs +67 -0
  184. package/dist/nitro/setup.d.mts +11 -0
  185. package/dist/nitro/setup.mjs +189 -0
  186. package/dist/nitro/types.d.mts +549 -0
  187. package/dist/nitro/types.mjs +1 -0
  188. package/dist/nitro/virtual/generators.d.mts +38 -0
  189. package/dist/nitro/virtual/generators.mjs +190 -0
  190. package/dist/nitro/virtual/stubs.d.mts +21 -0
  191. package/dist/nitro/virtual/stubs.mjs +31 -0
  192. package/dist/{ecosystem/nuxt.d.ts → nuxt.d.mts} +1 -1
  193. package/dist/nuxt.mjs +111 -0
  194. package/dist/{graphql/server.d.ts → stubs/index.d.mts} +5 -1
  195. package/dist/stubs/index.mjs +1 -0
  196. package/dist/subscribe/index.d.mts +78 -0
  197. package/dist/subscribe/index.mjs +205 -0
  198. package/native/index.d.ts +20 -0
  199. package/native/index.js +585 -0
  200. package/package.json +202 -112
  201. package/dist/ecosystem/nuxt.js +0 -67
  202. package/dist/graphql/index.d.ts +0 -5
  203. package/dist/graphql/index.js +0 -1
  204. package/dist/graphql/server.js +0 -1
  205. package/dist/index.d.ts +0 -8
  206. package/dist/index.js +0 -318
  207. package/dist/rollup.js +0 -277
  208. package/dist/routes/apollo-server.d.ts +0 -6
  209. package/dist/routes/apollo-server.js +0 -89
  210. package/dist/routes/debug.js +0 -449
  211. package/dist/routes/graphql-yoga.d.ts +0 -6
  212. package/dist/routes/graphql-yoga.js +0 -91
  213. package/dist/routes/health.d.ts +0 -10
  214. package/dist/types/index.d.ts +0 -246
  215. package/dist/types/standard-schema.d.ts +0 -59
  216. package/dist/utils/apollo.js +0 -61
  217. package/dist/utils/client-codegen.d.ts +0 -38
  218. package/dist/utils/client-codegen.js +0 -290
  219. package/dist/utils/define.js +0 -57
  220. package/dist/utils/file-generator.d.ts +0 -37
  221. package/dist/utils/file-generator.js +0 -72
  222. package/dist/utils/index.d.ts +0 -39
  223. package/dist/utils/index.js +0 -278
  224. package/dist/utils/path-resolver.js +0 -127
  225. package/dist/utils/server-codegen.d.ts +0 -7
  226. package/dist/utils/server-codegen.js +0 -136
  227. package/dist/utils/type-generation.d.ts +0 -7
  228. package/dist/utils/type-generation.js +0 -360
  229. package/dist/virtual/debug-info.d.ts +0 -9
  230. package/dist/virtual/debug-info.js +0 -26
  231. package/dist/virtual/graphql-config.d.ts +0 -9
  232. package/dist/virtual/graphql-config.js +0 -10
  233. package/dist/virtual/module-config.d.ts +0 -9
  234. package/dist/virtual/module-config.js +0 -10
  235. package/dist/virtual/server-directives.d.ts +0 -11
  236. package/dist/virtual/server-directives.js +0 -10
  237. package/dist/virtual/server-resolvers.d.ts +0 -11
  238. package/dist/virtual/server-resolvers.js +0 -10
  239. package/dist/virtual/server-schemas.d.ts +0 -11
  240. package/dist/virtual/server-schemas.js +0 -10
  241. package/dist/vite.d.ts +0 -25
  242. package/dist/vite.js +0 -40
@@ -1,4 +1,4 @@
1
- //#region src/utils/directive-parser.ts
1
+ //#region src/core/utils/directive-parser.ts
2
2
  /**
3
3
  * Clean AST-based directive parser using oxc-parser
4
4
  */
@@ -180,45 +180,38 @@ function generateDirectiveSchema(directive) {
180
180
  return `directive @${directive.name}${args} on ${locations}`;
181
181
  }
182
182
  /**
183
- * Generate directive schemas file from scanned directives
183
+ * Get the file path from a directive reference
184
184
  */
185
- async function generateDirectiveSchemas(nitro, directives) {
186
- if (directives.length === 0) return;
187
- const { existsSync, readFileSync, writeFileSync, mkdirSync } = await import("node:fs");
188
- const { readFile } = await import("node:fs/promises");
189
- const { resolve, dirname } = await import("pathe");
190
- const directiveSchemas = [];
191
- const seenDirectives = /* @__PURE__ */ new Set();
192
- const parser = new DirectiveParser();
193
- for (const dir of directives) for (const _imp of dir.imports) {
194
- const fileContent = await readFile(dir.specifier, "utf-8");
195
- const directiveDefs = await parser.parseDirectives(fileContent, dir.specifier);
196
- for (const def of directiveDefs) {
197
- if (seenDirectives.has(def.name)) continue;
198
- seenDirectives.add(def.name);
199
- const schema = generateDirectiveSchema(def);
200
- directiveSchemas.push(schema);
201
- }
202
- }
203
- if (directiveSchemas.length > 0) {
204
- const directivesPath = resolve(nitro.graphql.buildDir, "_directives.graphql");
205
- const content = `# WARNING: This file is auto-generated by nitro-graphql
206
- # Do not modify this file directly. It will be overwritten.
207
- # To define custom directives, create .directive.ts files using defineDirective()
208
-
209
- ${directiveSchemas.join("\n\n")}`;
210
- const targetDir = dirname(directivesPath);
211
- if (!existsSync(targetDir)) mkdirSync(targetDir, { recursive: true });
212
- let shouldWrite = true;
213
- if (existsSync(directivesPath)) shouldWrite = readFileSync(directivesPath, "utf-8") !== content;
214
- if (shouldWrite) writeFileSync(directivesPath, content, "utf-8");
215
- if (!nitro.scanSchemas.includes(directivesPath)) nitro.scanSchemas.push(directivesPath);
216
- }
185
+ function getFilePath(ref) {
186
+ return "fullPath" in ref ? ref.fullPath : ref.specifier;
217
187
  }
218
188
  /**
219
189
  * Singleton instance for reuse
220
190
  */
221
191
  const directiveParser = new DirectiveParser();
222
-
192
+ /**
193
+ * Generate GraphQL schema content from an array of parsed directives
194
+ * Returns the schema string and optionally writes to buildDir/directives.graphql
195
+ */
196
+ async function generateDirectiveSchemas(directives, buildDir) {
197
+ if (directives.length === 0) return null;
198
+ const fs = await import("node:fs");
199
+ const path = await import("pathe");
200
+ const allParsedDirectives = [];
201
+ for (const directive of directives) try {
202
+ const filePath = getFilePath(directive);
203
+ const content = fs.readFileSync(filePath, "utf-8");
204
+ const parsed = await directiveParser.parseDirectives(content, filePath);
205
+ allParsedDirectives.push(...parsed);
206
+ } catch {}
207
+ if (allParsedDirectives.length === 0) return null;
208
+ const schemaContent = allParsedDirectives.filter((directive, index, self) => index === self.findIndex((d) => d.name === directive.name)).map((d) => generateDirectiveSchema(d)).join("\n\n");
209
+ if (buildDir) {
210
+ const directivesPath = path.join(buildDir, "directives.graphql");
211
+ fs.mkdirSync(buildDir, { recursive: true });
212
+ if ((fs.existsSync(directivesPath) ? fs.readFileSync(directivesPath, "utf-8") : null) !== schemaContent) fs.writeFileSync(directivesPath, schemaContent, "utf-8");
213
+ }
214
+ return schemaContent;
215
+ }
223
216
  //#endregion
224
- export { DirectiveParser, directiveParser, generateDirectiveSchema, generateDirectiveSchemas };
217
+ export { DirectiveParser, directiveParser, generateDirectiveSchema, generateDirectiveSchemas };
@@ -0,0 +1,77 @@
1
+ //#region src/core/utils/errors.d.ts
2
+ /**
3
+ * Error handling utilities for GraphQL
4
+ * Framework-agnostic error masking and handling
5
+ */
6
+ interface MaskErrorOptions {
7
+ /**
8
+ * Default HTTP status code for validation errors (ZodError)
9
+ * @default 400
10
+ */
11
+ validationStatusCode?: number;
12
+ /**
13
+ * Default HTTP status code for HTTP errors when status is not provided
14
+ * @default 500
15
+ */
16
+ defaultHttpStatusCode?: number;
17
+ }
18
+ /**
19
+ * Default error masking function for GraphQL Yoga
20
+ * Handles common error types like ZodError and HTTPError
21
+ *
22
+ * @param options - Configuration options for error handling
23
+ *
24
+ * @example Basic usage
25
+ * ```ts
26
+ * import { defineGraphQLConfig } from 'nitro-graphql/define'
27
+ * import { createDefaultMaskError } from 'nitro-graphql/core'
28
+ *
29
+ * export default defineGraphQLConfig({
30
+ * maskedErrors: {
31
+ * maskError: createDefaultMaskError()
32
+ * }
33
+ * })
34
+ * ```
35
+ *
36
+ * @example Custom status codes
37
+ * ```ts
38
+ * import { defineGraphQLConfig } from 'nitro-graphql/define'
39
+ * import { createDefaultMaskError } from 'nitro-graphql/core'
40
+ *
41
+ * export default defineGraphQLConfig({
42
+ * maskedErrors: {
43
+ * maskError: createDefaultMaskError({
44
+ * validationStatusCode: 422, // Use 422 for validation errors
45
+ * defaultHttpStatusCode: 500 // Use 500 for unknown HTTP errors
46
+ * })
47
+ * }
48
+ * })
49
+ * ```
50
+ *
51
+ * @example Custom error handling with fallback to default
52
+ * ```ts
53
+ * import { defineGraphQLConfig } from 'nitro-graphql/define'
54
+ * import { createDefaultMaskError } from 'nitro-graphql/core'
55
+ *
56
+ * const defaultMaskError = createDefaultMaskError()
57
+ *
58
+ * export default defineGraphQLConfig({
59
+ * maskedErrors: {
60
+ * maskError: (error: unknown) => {
61
+ * // Handle custom errors first
62
+ * if (error instanceof MyCustomError) {
63
+ * return new GraphQLError(error.message, {
64
+ * extensions: { code: 'CUSTOM_ERROR' }
65
+ * })
66
+ * }
67
+ *
68
+ * // Fall back to default handling
69
+ * return defaultMaskError(error)
70
+ * }
71
+ * }
72
+ * })
73
+ * ```
74
+ */
75
+ declare function createDefaultMaskError(options?: MaskErrorOptions): (error: unknown) => Error;
76
+ //#endregion
77
+ export { MaskErrorOptions, createDefaultMaskError };
@@ -0,0 +1,91 @@
1
+ import { GraphQLError } from "graphql";
2
+ //#region src/core/utils/errors.ts
3
+ /**
4
+ * Error handling utilities for GraphQL
5
+ * Framework-agnostic error masking and handling
6
+ */
7
+ /**
8
+ * Default error masking function for GraphQL Yoga
9
+ * Handles common error types like ZodError and HTTPError
10
+ *
11
+ * @param options - Configuration options for error handling
12
+ *
13
+ * @example Basic usage
14
+ * ```ts
15
+ * import { defineGraphQLConfig } from 'nitro-graphql/define'
16
+ * import { createDefaultMaskError } from 'nitro-graphql/core'
17
+ *
18
+ * export default defineGraphQLConfig({
19
+ * maskedErrors: {
20
+ * maskError: createDefaultMaskError()
21
+ * }
22
+ * })
23
+ * ```
24
+ *
25
+ * @example Custom status codes
26
+ * ```ts
27
+ * import { defineGraphQLConfig } from 'nitro-graphql/define'
28
+ * import { createDefaultMaskError } from 'nitro-graphql/core'
29
+ *
30
+ * export default defineGraphQLConfig({
31
+ * maskedErrors: {
32
+ * maskError: createDefaultMaskError({
33
+ * validationStatusCode: 422, // Use 422 for validation errors
34
+ * defaultHttpStatusCode: 500 // Use 500 for unknown HTTP errors
35
+ * })
36
+ * }
37
+ * })
38
+ * ```
39
+ *
40
+ * @example Custom error handling with fallback to default
41
+ * ```ts
42
+ * import { defineGraphQLConfig } from 'nitro-graphql/define'
43
+ * import { createDefaultMaskError } from 'nitro-graphql/core'
44
+ *
45
+ * const defaultMaskError = createDefaultMaskError()
46
+ *
47
+ * export default defineGraphQLConfig({
48
+ * maskedErrors: {
49
+ * maskError: (error: unknown) => {
50
+ * // Handle custom errors first
51
+ * if (error instanceof MyCustomError) {
52
+ * return new GraphQLError(error.message, {
53
+ * extensions: { code: 'CUSTOM_ERROR' }
54
+ * })
55
+ * }
56
+ *
57
+ * // Fall back to default handling
58
+ * return defaultMaskError(error)
59
+ * }
60
+ * }
61
+ * })
62
+ * ```
63
+ */
64
+ function createDefaultMaskError(options) {
65
+ const validationStatusCode = options?.validationStatusCode ?? 400;
66
+ const defaultHttpStatusCode = options?.defaultHttpStatusCode ?? 500;
67
+ return (error) => {
68
+ if (error && typeof error === "object" && "originalError" in error) {
69
+ const graphqlError = error;
70
+ if (graphqlError.originalError && typeof graphqlError.originalError === "object" && "issues" in graphqlError.originalError && Array.isArray(graphqlError.originalError.issues)) return new GraphQLError("Validation failed", { extensions: {
71
+ code: "BAD_USER_INPUT",
72
+ validationErrors: graphqlError.originalError.issues.map((issue) => ({
73
+ field: issue.path.join("."),
74
+ message: issue.message
75
+ })),
76
+ http: { status: validationStatusCode }
77
+ } });
78
+ if (graphqlError.originalError && typeof graphqlError.originalError === "object" && "statusCode" in graphqlError.originalError && "statusMessage" in graphqlError.originalError) {
79
+ const httpError = graphqlError.originalError;
80
+ const statusCode = httpError.statusCode ?? defaultHttpStatusCode;
81
+ return new GraphQLError(httpError.statusMessage || httpError.message || "Internal Server Error", { extensions: {
82
+ code: statusCode,
83
+ http: { status: statusCode }
84
+ } });
85
+ }
86
+ }
87
+ return error instanceof Error ? error : new Error(String(error));
88
+ };
89
+ }
90
+ //#endregion
91
+ export { createDefaultMaskError };
@@ -0,0 +1,24 @@
1
+ //#region src/core/utils/file-io.d.ts
2
+ /**
3
+ * Unified file I/O utilities
4
+ * Centralizes file writing patterns used across the codebase
5
+ */
6
+ /**
7
+ * Write file with automatic directory creation
8
+ */
9
+ declare function writeFile(path: string, content: string): void;
10
+ /**
11
+ * Write file only if content changed (for dev mode optimization)
12
+ * Returns true if file was written, false if unchanged
13
+ */
14
+ declare function writeFileIfChanged(path: string, content: string): boolean;
15
+ /**
16
+ * Ensure directory exists
17
+ */
18
+ declare function ensureDir(path: string): void;
19
+ /**
20
+ * Read file safely, returns undefined if file doesn't exist
21
+ */
22
+ declare function readFileSafe(path: string): string | undefined;
23
+ //#endregion
24
+ export { ensureDir, readFileSafe, writeFile, writeFileIfChanged };
@@ -0,0 +1,45 @@
1
+ import { dirname } from "pathe";
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
+ //#region src/core/utils/file-io.ts
4
+ /**
5
+ * Unified file I/O utilities
6
+ * Centralizes file writing patterns used across the codebase
7
+ */
8
+ /**
9
+ * Write file with automatic directory creation
10
+ */
11
+ function writeFile(path, content) {
12
+ mkdirSync(dirname(path), { recursive: true });
13
+ writeFileSync(path, content, "utf-8");
14
+ }
15
+ /**
16
+ * Write file only if content changed (for dev mode optimization)
17
+ * Returns true if file was written, false if unchanged
18
+ */
19
+ function writeFileIfChanged(path, content) {
20
+ try {
21
+ if (existsSync(path)) {
22
+ if (readFileSync(path, "utf-8") === content) return false;
23
+ }
24
+ } catch {}
25
+ writeFile(path, content);
26
+ return true;
27
+ }
28
+ /**
29
+ * Ensure directory exists
30
+ */
31
+ function ensureDir(path) {
32
+ mkdirSync(path, { recursive: true });
33
+ }
34
+ /**
35
+ * Read file safely, returns undefined if file doesn't exist
36
+ */
37
+ function readFileSafe(path) {
38
+ try {
39
+ return readFileSync(path, "utf-8");
40
+ } catch {
41
+ return;
42
+ }
43
+ }
44
+ //#endregion
45
+ export { ensureDir, readFileSafe, writeFile, writeFileIfChanged };
@@ -0,0 +1,15 @@
1
+ //#region src/core/utils/imports.d.ts
2
+ /**
3
+ * Import utilities
4
+ * Helpers for generating import IDs and relative paths
5
+ */
6
+ /**
7
+ * Generate a unique import ID for a file path
8
+ */
9
+ declare function getImportId(p: string, lazy?: boolean): string;
10
+ /**
11
+ * Get relative path with a leading dot for module resolution
12
+ */
13
+ declare function relativeWithDot(from: string, to: string): string;
14
+ //#endregion
15
+ export { getImportId, relativeWithDot };
@@ -0,0 +1,24 @@
1
+ import { relative } from "pathe";
2
+ import { hash } from "ohash";
3
+ //#region src/core/utils/imports.ts
4
+ /**
5
+ * Import utilities
6
+ * Helpers for generating import IDs and relative paths
7
+ */
8
+ const HYPHEN_RE = /-/g;
9
+ /**
10
+ * Generate a unique import ID for a file path
11
+ */
12
+ function getImportId(p, lazy) {
13
+ return (lazy ? "_lazy_" : "_") + hash(p).replace(HYPHEN_RE, "").slice(0, 6);
14
+ }
15
+ const RELATIVE_RE = /^\.{1,2}\//;
16
+ /**
17
+ * Get relative path with a leading dot for module resolution
18
+ */
19
+ function relativeWithDot(from, to) {
20
+ const rel = relative(from, to);
21
+ return RELATIVE_RE.test(rel) ? rel : `./${rel}`;
22
+ }
23
+ //#endregion
24
+ export { getImportId, relativeWithDot };
@@ -0,0 +1,7 @@
1
+ import { DirectiveFileRef, DirectiveParser, ParsedDirective, directiveParser, generateDirectiveSchema, generateDirectiveSchemas } from "./directive-parser.mjs";
2
+ import { MaskErrorOptions, createDefaultMaskError } from "./errors.mjs";
3
+ import { ensureDir, readFileSafe, writeFile, writeFileIfChanged } from "./file-io.mjs";
4
+ import { getImportId, relativeWithDot } from "./imports.mjs";
5
+ import { createLogger, createSilentLogger, defaultLogger } from "./logger.mjs";
6
+ import { OfetchTemplateOptions, generateOfetchTemplate } from "./ofetch-templates.mjs";
7
+ export { DirectiveFileRef, DirectiveParser, MaskErrorOptions, OfetchTemplateOptions, ParsedDirective, createDefaultMaskError, createLogger, createSilentLogger, defaultLogger, directiveParser, ensureDir, generateDirectiveSchema, generateDirectiveSchemas, generateOfetchTemplate, getImportId, readFileSafe, relativeWithDot, writeFile, writeFileIfChanged };
@@ -0,0 +1,7 @@
1
+ import { ensureDir, readFileSafe, writeFile, writeFileIfChanged } from "./file-io.mjs";
2
+ import { createLogger, createSilentLogger, defaultLogger } from "./logger.mjs";
3
+ import { DirectiveParser, directiveParser, generateDirectiveSchema, generateDirectiveSchemas } from "./directive-parser.mjs";
4
+ import { createDefaultMaskError } from "./errors.mjs";
5
+ import { getImportId, relativeWithDot } from "./imports.mjs";
6
+ import { generateOfetchTemplate } from "./ofetch-templates.mjs";
7
+ export { DirectiveParser, createDefaultMaskError, createLogger, createSilentLogger, defaultLogger, directiveParser, ensureDir, generateDirectiveSchema, generateDirectiveSchemas, generateOfetchTemplate, getImportId, readFileSafe, relativeWithDot, writeFile, writeFileIfChanged };
@@ -0,0 +1,18 @@
1
+ import { CoreLogger } from "../types/config.mjs";
2
+
3
+ //#region src/core/utils/logger.d.ts
4
+ /**
5
+ * Create a logger instance with the nitro-graphql tag
6
+ */
7
+ declare function createLogger(tag?: string): CoreLogger;
8
+ /**
9
+ * Default logger instance
10
+ */
11
+ declare const defaultLogger: CoreLogger;
12
+ /**
13
+ * Create a silent logger (no output)
14
+ * Useful for testing or when logging should be suppressed
15
+ */
16
+ declare function createSilentLogger(): CoreLogger;
17
+ //#endregion
18
+ export { createLogger, createSilentLogger, defaultLogger };
@@ -0,0 +1,36 @@
1
+ import { LOG_TAG } from "../constants.mjs";
2
+ import { consola as consola$1 } from "consola";
3
+ //#region src/core/utils/logger.ts
4
+ /**
5
+ * Create a logger instance with the nitro-graphql tag
6
+ */
7
+ function createLogger(tag = LOG_TAG) {
8
+ const logger = consola$1.withTag(tag);
9
+ return {
10
+ info: (message, ...args) => logger.info(message, ...args),
11
+ warn: (message, ...args) => logger.warn(message, ...args),
12
+ error: (message, ...args) => logger.error(message, ...args),
13
+ success: (message, ...args) => logger.success(message, ...args),
14
+ debug: (message, ...args) => logger.debug(message, ...args)
15
+ };
16
+ }
17
+ /**
18
+ * Default logger instance
19
+ */
20
+ const defaultLogger = createLogger();
21
+ /**
22
+ * Create a silent logger (no output)
23
+ * Useful for testing or when logging should be suppressed
24
+ */
25
+ function createSilentLogger() {
26
+ const noop = () => {};
27
+ return {
28
+ info: noop,
29
+ warn: noop,
30
+ error: noop,
31
+ success: noop,
32
+ debug: noop
33
+ };
34
+ }
35
+ //#endregion
36
+ export { createLogger, createSilentLogger, defaultLogger };
@@ -0,0 +1,30 @@
1
+ //#region src/core/utils/ofetch-templates.d.ts
2
+ /**
3
+ * Shared ofetch template generation utilities
4
+ * Used by both main client and external service code generation
5
+ */
6
+ interface OfetchTemplateOptions {
7
+ /** Service name (e.g., 'default', 'github') */
8
+ serviceName: string;
9
+ /** Whether to use Nuxt composables ($fetch, useRequestHeaders) */
10
+ isNuxt: boolean;
11
+ /** Endpoint URL for the GraphQL service */
12
+ endpoint: string;
13
+ /** Whether this is an external service (affects naming convention) */
14
+ isExternal?: boolean;
15
+ }
16
+ /**
17
+ * Generate ofetch client template content
18
+ *
19
+ * For main (default) service:
20
+ * - SDK exported as `$sdk`
21
+ * - Client function is `createGraphQLClient`
22
+ *
23
+ * For external services:
24
+ * - SDK exported as `$${serviceName}Sdk`
25
+ * - Client function is `create${ServiceName}GraphQLClient`
26
+ * - Endpoint has default value
27
+ */
28
+ declare function generateOfetchTemplate(options: OfetchTemplateOptions): string;
29
+ //#endregion
30
+ export { OfetchTemplateOptions, generateOfetchTemplate };
@@ -0,0 +1,134 @@
1
+ //#region src/core/utils/ofetch-templates.ts
2
+ /**
3
+ * Capitalize first letter of a string
4
+ */
5
+ function capitalize(str) {
6
+ return str.charAt(0).toUpperCase() + str.slice(1);
7
+ }
8
+ /**
9
+ * Generate ofetch client template content
10
+ *
11
+ * For main (default) service:
12
+ * - SDK exported as `$sdk`
13
+ * - Client function is `createGraphQLClient`
14
+ *
15
+ * For external services:
16
+ * - SDK exported as `$${serviceName}Sdk`
17
+ * - Client function is `create${ServiceName}GraphQLClient`
18
+ * - Endpoint has default value
19
+ */
20
+ function generateOfetchTemplate(options) {
21
+ const { serviceName, isNuxt, endpoint, isExternal = false } = options;
22
+ if (isExternal) return generateExternalOfetchTemplate({
23
+ serviceName,
24
+ isNuxt,
25
+ endpoint
26
+ });
27
+ return generateMainOfetchTemplate({
28
+ isNuxt,
29
+ endpoint
30
+ });
31
+ }
32
+ /**
33
+ * Generate ofetch template for main (default) GraphQL service
34
+ */
35
+ function generateMainOfetchTemplate(options) {
36
+ const { isNuxt, endpoint } = options;
37
+ if (isNuxt) return `// This file is auto-generated once by nitro-graphql for quick start
38
+ // You can modify this file according to your needs
39
+ import type { Requester } from './sdk'
40
+ import { getSdk } from './sdk'
41
+
42
+ export function createGraphQLClient(endpoint: string): Requester {
43
+ return async <R>(doc: string, vars?: any): Promise<R> => {
44
+ const headers = import.meta.server ? useRequestHeaders() : undefined
45
+
46
+ const result = await $fetch(endpoint, {
47
+ method: 'POST',
48
+ body: { query: doc, variables: vars },
49
+ headers: {
50
+ 'Content-Type': 'application/json',
51
+ ...headers,
52
+ },
53
+ })
54
+
55
+ return result as R
56
+ }
57
+ }
58
+
59
+ export const $sdk = getSdk(createGraphQLClient('${endpoint}'))`;
60
+ return `// This file is auto-generated once by nitro-graphql for quick start
61
+ // You can modify this file according to your needs
62
+ import type { Requester } from './sdk'
63
+ import { ofetch } from 'ofetch'
64
+ import { getSdk } from './sdk'
65
+
66
+ export function createGraphQLClient(endpoint: string): Requester {
67
+ return async <R>(doc: string, vars?: any): Promise<R> => {
68
+ const result = await ofetch(endpoint, {
69
+ method: 'POST',
70
+ body: { query: doc, variables: vars },
71
+ headers: {
72
+ 'Content-Type': 'application/json',
73
+ },
74
+ })
75
+
76
+ return result as R
77
+ }
78
+ }
79
+
80
+ export const $sdk = getSdk(createGraphQLClient('${endpoint}'))`;
81
+ }
82
+ /**
83
+ * Generate ofetch template for external GraphQL service
84
+ */
85
+ function generateExternalOfetchTemplate(options) {
86
+ const { serviceName, isNuxt, endpoint } = options;
87
+ const capitalizedName = capitalize(serviceName);
88
+ if (isNuxt) return `// This file is auto-generated once by nitro-graphql for quick start
89
+ // You can modify this file according to your needs
90
+ import type { Sdk, Requester } from './sdk'
91
+ import { getSdk } from './sdk'
92
+
93
+ export function create${capitalizedName}GraphQLClient(endpoint: string = '${endpoint}'): Requester {
94
+ return async <R>(doc: string, vars?: any): Promise<R> => {
95
+ const headers = import.meta.server ? useRequestHeaders() : undefined
96
+
97
+ const result = await $fetch(endpoint, {
98
+ method: 'POST',
99
+ body: { query: doc, variables: vars },
100
+ headers: {
101
+ 'Content-Type': 'application/json',
102
+ ...headers,
103
+ },
104
+ })
105
+
106
+ return result as R
107
+ }
108
+ }
109
+
110
+ export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedName}GraphQLClient())`;
111
+ return `// This file is auto-generated once by nitro-graphql for quick start
112
+ // You can modify this file according to your needs
113
+ import type { Sdk, Requester } from './sdk'
114
+ import { ofetch } from 'ofetch'
115
+ import { getSdk } from './sdk'
116
+
117
+ export function create${capitalizedName}GraphQLClient(endpoint: string = '${endpoint}'): Requester {
118
+ return async <R>(doc: string, vars?: any): Promise<R> => {
119
+ const result = await ofetch(endpoint, {
120
+ method: 'POST',
121
+ body: { query: doc, variables: vars },
122
+ headers: {
123
+ 'Content-Type': 'application/json',
124
+ },
125
+ })
126
+
127
+ return result as R
128
+ }
129
+ }
130
+
131
+ export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedName}GraphQLClient())`;
132
+ }
133
+ //#endregion
134
+ export { generateOfetchTemplate };
@@ -0,0 +1,74 @@
1
+ import { basename, dirname, join, resolve } from "pathe";
2
+
3
+ //#region src/core/utils/runtime.d.ts
4
+ /**
5
+ * Cross-runtime compatibility layer
6
+ *
7
+ * Provides runtime-agnostic file system and process utilities
8
+ * that work across Node.js, Bun, and Deno.
9
+ */
10
+ /**
11
+ * Read file contents asynchronously
12
+ */
13
+ declare function readFile(path: string): Promise<string>;
14
+ /**
15
+ * Read file contents synchronously
16
+ */
17
+ declare function readFileSync_(path: string): string;
18
+ /**
19
+ * Write file contents asynchronously
20
+ */
21
+ declare function writeFile(path: string, content: string): Promise<void>;
22
+ /**
23
+ * Write file contents synchronously
24
+ */
25
+ declare function writeFileSync_(path: string, content: string): void;
26
+ /**
27
+ * Check if file/directory exists asynchronously
28
+ */
29
+ declare function exists(path: string): Promise<boolean>;
30
+ /**
31
+ * Check if file/directory exists synchronously
32
+ */
33
+ declare function existsSync_(path: string): boolean;
34
+ /**
35
+ * Create directory recursively asynchronously
36
+ */
37
+ declare function mkdir(path: string): Promise<void>;
38
+ /**
39
+ * Create directory recursively synchronously
40
+ */
41
+ declare function mkdirSync_(path: string): void;
42
+ /**
43
+ * Get current working directory
44
+ * Works across Node.js, Bun, and Deno
45
+ */
46
+ declare function getCwd(): string;
47
+ /**
48
+ * Exit process with code
49
+ * Works across Node.js, Bun, and Deno
50
+ */
51
+ declare function exit(code: number): never;
52
+ /**
53
+ * Convert file path to file URL
54
+ * Works across Node.js, Bun, and Deno
55
+ */
56
+ declare function pathToFileURL(path: string): URL;
57
+ /**
58
+ * Register signal handler (SIGINT, SIGTERM, etc.)
59
+ * Works across Node.js, Bun, and Deno
60
+ */
61
+ declare function onSignal(signal: 'SIGINT' | 'SIGTERM', handler: () => void): void;
62
+ /**
63
+ * Read directory contents
64
+ */
65
+ declare function readdir(path: string): Promise<string[]>;
66
+ /**
67
+ * Get file/directory stats
68
+ */
69
+ declare function stat(path: string): Promise<{
70
+ isDirectory: () => boolean;
71
+ isFile: () => boolean;
72
+ }>;
73
+ //#endregion
74
+ export { basename, dirname, exists, existsSync_, exit, getCwd, join, mkdir, mkdirSync_, onSignal, pathToFileURL, readFile, readFileSync_, readdir, resolve, stat, writeFile, writeFileSync_ };