@rpc-dart/trpc-adapter 0.1.2 → 0.1.4

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.
package/README.md CHANGED
@@ -1,17 +1,56 @@
1
1
  # @rpc-dart/trpc-adapter
2
2
 
3
- tRPC v11 extraction adapter: route probe, inference harness, Contract IR normalizer.
3
+ [![npm version](https://img.shields.io/npm/v/@rpc-dart/trpc-adapter.svg)](https://www.npmjs.com/package/@rpc-dart/trpc-adapter)
4
+ [![license](https://img.shields.io/npm/l/@rpc-dart/trpc-adapter.svg)](https://github.com/AdityaJandu/rpc-dart/blob/main/LICENSE)
4
5
 
5
- Part of the [rpc-dart](https://github.com/AdityaJandu/rpc-dart) monorepo — end-to-end type safety for tRPC v11 backends consumed from Dart and Flutter.
6
+ tRPC v11 router extraction adapter for `rpc-dart`. Inspects router definitions with the TypeScript compiler API and produces a normalized Contract IR.
6
7
 
7
- ## Install
8
+ Part of the [rpc-dart](https://github.com/AdityaJandu/rpc-dart) monorepo.
9
+
10
+ ---
11
+
12
+ ## Capabilities
13
+
14
+ - **Compiler-level inspection:** Uses `ts.TypeChecker` to inspect procedure inputs, outputs, and return types directly from TypeScript AST nodes without requiring a running server.
15
+ - **Input vs. output fidelity:** Differentiates `z.input` (wire format accepted from the client) from `z.output` (wire format returned by the procedure), accurately handling Zod `.transform()`, `.default()`, and `.coerce()`.
16
+ - **Three-state PATCH detection:** Identifies fields that are both optional and nullable, tagging them in the IR for three-state `RpcOpt<T>` handling.
17
+ - **Enums and unions:** Extracts string/numeric enums and maps discriminated unions with explicit discriminator keys into sealed type hierarchies.
18
+
19
+ ---
20
+
21
+ ## Installation
8
22
 
9
23
  ```bash
10
24
  npm install @rpc-dart/trpc-adapter
25
+ # or
26
+ pnpm add @rpc-dart/trpc-adapter
11
27
  ```
12
28
 
13
- See the [documentation](../../docs/README.md) for the full CLI reference, concepts, and guides.
29
+ ---
30
+
31
+ ## Usage
32
+
33
+ ```typescript
34
+ import { extractTrpcContract } from '@rpc-dart/trpc-adapter';
35
+
36
+ const { contract, diagnostics } = await extractTrpcContract({
37
+ entry: './src/trpc/router.ts',
38
+ exportName: 'appRouter',
39
+ transformer: 'superjson',
40
+ unknownPolicy: 'fail',
41
+ });
42
+ ```
43
+
44
+ ---
45
+
46
+ ## Links
47
+
48
+ - [GitHub Repository](https://github.com/AdityaJandu/rpc-dart)
49
+ - [CLI Package (rpc-dart)](https://www.npmjs.com/package/rpc-dart)
50
+ - [Issue Tracker](https://github.com/AdityaJandu/rpc-dart/issues)
51
+
52
+ ---
14
53
 
15
54
  ## License
16
55
 
17
- MIT — see [LICENSE](./LICENSE).
56
+ MIT © [Aditya Jandu](https://github.com/AdityaJandu)
package/dist/extract.js CHANGED
@@ -23,10 +23,8 @@ export async function extractContract(options) {
23
23
  });
24
24
  throw new ExtractionFailure(diagnostics);
25
25
  }
26
- // 2. Virtual harness over the public inference utilities, one alias pair per path.
27
- const harnessPath = path.join(tsconfigDir, harnessFileName());
28
- const harnessSource = buildHarness(entryImportPath(tsconfigDir, entryAbsolute), options.exportName, probePaths.map((p) => p.path));
29
- // 3. Program under the user's own tsconfig, harness overlaid in memory.
26
+ // 2. Program options from the user's own tsconfig (parsed before the harness
27
+ // is built so Node16/NodeNext resolution can shape the entry specifier).
30
28
  const configFile = ts.readConfigFile(options.tsconfigPath, ts.sys.readFile);
31
29
  if (configFile.error) {
32
30
  diagnostics.push({
@@ -39,11 +37,38 @@ export async function extractContract(options) {
39
37
  throw new ExtractionFailure(diagnostics);
40
38
  }
41
39
  const parsed = ts.parseJsonConfigFileContent(configFile.config, ts.sys, tsconfigDir);
42
- const baseHost = ts.createCompilerHost(parsed.options, true);
40
+ // 3. Virtual harness over the public inference utilities, one alias pair per path.
41
+ const resolution = parsed.options.moduleResolution === ts.ModuleResolutionKind.Node16
42
+ ? 'node16'
43
+ : parsed.options.moduleResolution === ts.ModuleResolutionKind.NodeNext
44
+ ? 'nodenext'
45
+ : undefined;
46
+ const harnessPath = path.join(tsconfigDir, harnessFileName());
47
+ const harnessSource = buildHarness(entryImportPath(tsconfigDir, entryAbsolute, resolution), options.exportName, probePaths.map((p) => p.path));
48
+ // 4. Program under the user's own tsconfig, harness overlaid in memory.
49
+ // Extraction-neutral overrides: the in-memory program exists only to
50
+ // answer type queries on the harness, so build/lint-only flags are
51
+ // neutralized regardless of the user's tsconfig — they would otherwise
52
+ // reject the synthetic harness (rootDir, composite emit rules) or surface
53
+ // lint-only semantic errors (noUnusedLocals/noUnusedParameters,
54
+ // isolatedDeclarations) that have nothing to do with contract extraction.
55
+ const extractionOptions = {
56
+ ...parsed.options,
57
+ noEmit: true,
58
+ declaration: false,
59
+ declarationMap: false,
60
+ composite: false,
61
+ incremental: false,
62
+ isolatedDeclarations: false,
63
+ noUnusedLocals: false,
64
+ noUnusedParameters: false,
65
+ rootDir: undefined,
66
+ };
67
+ const baseHost = ts.createCompilerHost(extractionOptions, true);
43
68
  const host = overlayHost(baseHost, harnessPath, harnessSource);
44
69
  const program = ts.createProgram({
45
70
  rootNames: [harnessPath, ...parsed.fileNames],
46
- options: parsed.options,
71
+ options: extractionOptions,
47
72
  host,
48
73
  });
49
74
  const harnessSf = program.getSourceFile(harnessPath);
package/dist/harness.d.ts CHANGED
@@ -8,5 +8,10 @@
8
8
  export declare function harnessFileName(): string;
9
9
  export declare function pathAlias(path: string, role: 'in' | 'out'): string;
10
10
  export declare function buildHarness(entryImportPath: string, exportName: string, paths: string[]): string;
11
- /** './router.ts' -> './router' (extensionless relative import for the harness). */
12
- export declare function entryImportPath(tsconfigDir: string, entryAbsolute: string): string;
11
+ /**
12
+ * './router.ts' -> './router' (bundler-style), or explicit extensions under
13
+ * node16/nodenext resolution, where extensionless relative imports are
14
+ * illegal (TS5097) and the TS-standard specifiers (.js/.mjs/.cjs) resolve to
15
+ * the .ts/.mts/.cts sources with or without allowImportingTsExtensions.
16
+ */
17
+ export declare function entryImportPath(tsconfigDir: string, entryAbsolute: string, resolution?: 'node16' | 'nodenext'): string;
package/dist/harness.js CHANGED
@@ -32,9 +32,21 @@ export type __rpc_dart_outputs = inferRouterOutputs<__rpc_dart_router>;
32
32
  ${aliases}
33
33
  `;
34
34
  }
35
- /** './router.ts' -> './router' (extensionless relative import for the harness). */
36
- export function entryImportPath(tsconfigDir, entryAbsolute) {
37
- const rel = relative(tsconfigDir, entryAbsolute).replace(/\.ts$/, '');
38
- return rel.startsWith('.') ? rel : `./${rel}`;
35
+ /**
36
+ * './router.ts' -> './router' (bundler-style), or explicit extensions under
37
+ * node16/nodenext resolution, where extensionless relative imports are
38
+ * illegal (TS5097) and the TS-standard specifiers (.js/.mjs/.cjs) resolve to
39
+ * the .ts/.mts/.cts sources with or without allowImportingTsExtensions.
40
+ */
41
+ export function entryImportPath(tsconfigDir, entryAbsolute, resolution) {
42
+ const rel = relative(tsconfigDir, entryAbsolute);
43
+ const specifier = resolution === undefined
44
+ ? rel.replace(/\.(mts|cts|tsx?)$/, '')
45
+ : rel
46
+ .replace(/\.tsx$/, '.js')
47
+ .replace(/\.mts$/, '.mjs')
48
+ .replace(/\.cts$/, '.cjs')
49
+ .replace(/\.ts$/, '.js');
50
+ return specifier.startsWith('.') ? specifier : `./${specifier}`;
39
51
  }
40
52
  import { relative } from 'node:path';
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@rpc-dart/trpc-adapter",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
+ "description": "tRPC v11 extraction adapter: route probe, inference harness, and Contract IR normalizer.",
4
5
  "type": "module",
5
6
  "main": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
@@ -8,7 +9,7 @@
8
9
  "dist"
9
10
  ],
10
11
  "dependencies": {
11
- "@rpc-dart/core": "0.1.2",
12
+ "@rpc-dart/core": "0.1.4",
12
13
  "jiti": "^2.6.0",
13
14
  "typescript": "^5.9.0"
14
15
  },
@@ -22,6 +23,7 @@
22
23
  "url": "git+https://github.com/AdityaJandu/rpc-dart.git"
23
24
  },
24
25
  "homepage": "https://github.com/AdityaJandu/rpc-dart#readme",
26
+ "bugs": "https://github.com/AdityaJandu/rpc-dart/issues",
25
27
  "keywords": [
26
28
  "trpc",
27
29
  "typescript",