@tuyau/core 0.3.1 → 0.3.2

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.
@@ -15,6 +15,7 @@ import { readFile } from "node:fs/promises";
15
15
  import { dirname, relative } from "node:path";
16
16
  import { existsSync, mkdirSync } from "node:fs";
17
17
  import string from "@adonisjs/core/helpers/string";
18
+ import { assertExists } from "@adonisjs/core/helpers/assert";
18
19
  import { parseBindingReference, slash } from "@adonisjs/core/helpers";
19
20
  var ApiTypesGenerator = class {
20
21
  #appRoot;
@@ -23,7 +24,7 @@ var ApiTypesGenerator = class {
23
24
  #config;
24
25
  #routes;
25
26
  #destination;
26
- #isTuyauInertiaInstalledCached;
27
+ #cachedPkgJson;
27
28
  constructor(options) {
28
29
  this.#config = options.config;
29
30
  this.#routes = options.routes;
@@ -32,24 +33,26 @@ var ApiTypesGenerator = class {
32
33
  this.#appRoot = options.appRoot;
33
34
  this.#prepareDestination();
34
35
  }
35
- /**
36
- * Check if the @tuyau/inertia package is installed.
37
- */
38
- async #isTuyauInertiaInstalled() {
39
- if (this.#isTuyauInertiaInstalledCached !== void 0)
40
- return this.#isTuyauInertiaInstalledCached;
36
+ async #loadPkgJson() {
37
+ if (this.#cachedPkgJson) return this.#cachedPkgJson;
41
38
  try {
42
39
  const pkgJsonText = await readFile(
43
40
  fileURLToPath(new URL("./package.json", this.#appRoot)),
44
41
  "utf-8"
45
42
  );
46
- const pkgJson = JSON.parse(pkgJsonText);
47
- this.#isTuyauInertiaInstalledCached = !!pkgJson.dependencies?.["@tuyau/inertia"];
48
- return this.#isTuyauInertiaInstalledCached;
43
+ this.#cachedPkgJson = JSON.parse(pkgJsonText);
44
+ return this.#cachedPkgJson;
49
45
  } catch (error) {
50
46
  throw new Error("Unable to read the package.json file", { cause: error });
51
47
  }
52
48
  }
49
+ #isPackageInstalled(name) {
50
+ assertExists(
51
+ this.#cachedPkgJson,
52
+ "package.json should be loaded before checking if a package is installed"
53
+ );
54
+ return !!this.#cachedPkgJson.dependencies?.[name];
55
+ }
53
56
  #getDestinationDirectory() {
54
57
  return dirname(this.#destination);
55
58
  }
@@ -218,12 +221,23 @@ var ApiTypesGenerator = class {
218
221
  });
219
222
  await file.save();
220
223
  }
224
+ /**
225
+ * If @tuyau/superjson is installed then we must use a special
226
+ * type that will not type-serialize the response
227
+ */
228
+ #getMakeTuyauResponseType() {
229
+ const isSuperJsonInstalled = this.#isPackageInstalled("@tuyau/superjson");
230
+ return isSuperJsonInstalled ? "MakeNonSerializedTuyauResponse" : "MakeTuyauResponse";
231
+ }
221
232
  async #writeApiFile(options) {
222
233
  const file = this.#project.createSourceFile(this.#destination, "", { overwrite: true });
223
234
  if (!file) throw new Error("Unable to create the api.ts file");
224
- const isTuyauInertiaInstalled = await this.#isTuyauInertiaInstalled();
235
+ const isTuyauInertiaInstalled = this.#isPackageInstalled("@tuyau/inertia");
236
+ const makeTuyauResponseType = this.#getMakeTuyauResponseType();
225
237
  file.removeText().insertText(0, (writer) => {
226
- writer.writeLine(`// @ts-nocheck`).writeLine(`/* eslint-disable */`).writeLine("// --------------------------------------------------").writeLine("// This file is auto-generated by Tuyau. Do not edit manually !").writeLine("// --------------------------------------------------").writeLine("").writeLine(`import type { MakeTuyauRequest, MakeTuyauResponse } from '@tuyau/utils/types'`).writeLine(`import type { InferInput } from '@vinejs/vine/types'`).newLine();
238
+ writer.writeLine(`// @ts-nocheck`).writeLine(`/* eslint-disable */`).writeLine("// --------------------------------------------------").writeLine("// This file is auto-generated by Tuyau. Do not edit manually !").writeLine("// --------------------------------------------------").writeLine("").writeLine(
239
+ `import type { MakeTuyauRequest, ${makeTuyauResponseType} } from '@tuyau/utils/types'`
240
+ ).writeLine(`import type { InferInput } from '@vinejs/vine/types'`).newLine();
227
241
  Object.entries(options.typesByPattern).forEach(([key, value]) => {
228
242
  writer.writeLine(`type ${key} = {`);
229
243
  writer.writeLine(` request: ${value.request}`);
@@ -257,6 +271,7 @@ var ApiTypesGenerator = class {
257
271
  const typesByPattern = {};
258
272
  const sourcesFiles = this.#project.getSourceFiles();
259
273
  const routes = this.#filterRoutes(this.#routes, "definitions");
274
+ await this.#loadPkgJson();
260
275
  for (const route of routes) {
261
276
  if (typeof route.handler === "function") continue;
262
277
  const routeHandler = await parseBindingReference(route.handler.reference);
@@ -283,9 +298,10 @@ var ApiTypesGenerator = class {
283
298
  currentLevel = currentLevel[segment];
284
299
  if (i !== segments.length - 1) return;
285
300
  const typeName = this.#generateTypeName(route);
301
+ const makeTuyauResponseType = this.#getMakeTuyauResponseType();
286
302
  typesByPattern[typeName] = {
287
303
  request: schemaImport ? `MakeTuyauRequest<${schemaImport}>` : "unknown",
288
- response: `MakeTuyauResponse<import('${relativePath}').default['${routeHandler.method}'], ${!!schemaImport}>`
304
+ response: `${makeTuyauResponseType}<import('${relativePath}').default['${routeHandler.method}'], ${!!schemaImport}>`
289
305
  };
290
306
  currentLevel.$url = {};
291
307
  for (const method of methods) currentLevel[method] = typeName;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tuyau/core",
3
3
  "type": "module",
4
- "version": "0.3.1",
4
+ "version": "0.3.2",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "MIT",
@@ -24,15 +24,15 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "ts-morph": "^25.0.0",
27
- "@tuyau/utils": "0.0.6"
27
+ "@tuyau/utils": "0.0.7"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@adonisjs/assembler": "^7.8.2",
31
31
  "@adonisjs/core": "^6.17.1",
32
32
  "@poppinss/cliui": "^6.4.2",
33
33
  "@poppinss/matchit": "^3.1.2",
34
- "@types/node": "^22.10.7",
35
- "@tuyau/client": "0.2.4"
34
+ "@types/node": "^22.13.0",
35
+ "@tuyau/client": "0.2.5"
36
36
  },
37
37
  "publishConfig": {
38
38
  "access": "public",