@rpc-dart/trpc-adapter 0.1.3 → 0.2.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.
- package/dist/classify.js +2 -0
- package/dist/extract.js +40 -7
- package/dist/harness.d.ts +14 -4
- package/dist/harness.js +47 -6
- package/package.json +2 -2
package/dist/classify.js
CHANGED
|
@@ -103,6 +103,8 @@ export class TypeClassifier {
|
|
|
103
103
|
return 'BigInt';
|
|
104
104
|
if (flags & ts.TypeFlags.Void)
|
|
105
105
|
return 'void';
|
|
106
|
+
if (flags & ts.TypeFlags.Never)
|
|
107
|
+
return 'never';
|
|
106
108
|
if (flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown))
|
|
107
109
|
return { kind: 'unknown' };
|
|
108
110
|
if (flags & ts.TypeFlags.Null || flags & ts.TypeFlags.Undefined)
|
package/dist/extract.js
CHANGED
|
@@ -4,7 +4,7 @@ import { assembleContract, ExtractionFailure, } from '@rpc-dart/core';
|
|
|
4
4
|
import { entryImportPath, buildHarness, harnessFileName, pathAlias } from './harness.js';
|
|
5
5
|
import { probeRouter } from './probe.js';
|
|
6
6
|
import { pascal, TypeClassificationError, TypeClassifier } from './classify.js';
|
|
7
|
-
const GENERATOR = { name: 'rpc-dart', version: '0.
|
|
7
|
+
const GENERATOR = { name: 'rpc-dart', version: '0.2.0' };
|
|
8
8
|
export async function extractContract(options) {
|
|
9
9
|
const tsconfigDir = path.dirname(options.tsconfigPath);
|
|
10
10
|
const entryAbsolute = path.resolve(tsconfigDir, options.entryFile);
|
|
@@ -23,10 +23,8 @@ export async function extractContract(options) {
|
|
|
23
23
|
});
|
|
24
24
|
throw new ExtractionFailure(diagnostics);
|
|
25
25
|
}
|
|
26
|
-
// 2.
|
|
27
|
-
|
|
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,39 @@ 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
|
-
|
|
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 subscriptionPaths = new Set(probePaths.filter((p) => p.kind === 'subscription').map((p) => p.path));
|
|
47
|
+
const harnessPath = path.join(tsconfigDir, harnessFileName());
|
|
48
|
+
const harnessSource = buildHarness(entryImportPath(tsconfigDir, entryAbsolute, resolution), options.exportName, probePaths.map((p) => p.path), subscriptionPaths);
|
|
49
|
+
// 4. Program under the user's own tsconfig, harness overlaid in memory.
|
|
50
|
+
// Extraction-neutral overrides: the in-memory program exists only to
|
|
51
|
+
// answer type queries on the harness, so build/lint-only flags are
|
|
52
|
+
// neutralized regardless of the user's tsconfig — they would otherwise
|
|
53
|
+
// reject the synthetic harness (rootDir, composite emit rules) or surface
|
|
54
|
+
// lint-only semantic errors (noUnusedLocals/noUnusedParameters,
|
|
55
|
+
// isolatedDeclarations) that have nothing to do with contract extraction.
|
|
56
|
+
const extractionOptions = {
|
|
57
|
+
...parsed.options,
|
|
58
|
+
noEmit: true,
|
|
59
|
+
declaration: false,
|
|
60
|
+
declarationMap: false,
|
|
61
|
+
composite: false,
|
|
62
|
+
incremental: false,
|
|
63
|
+
isolatedDeclarations: false,
|
|
64
|
+
noUnusedLocals: false,
|
|
65
|
+
noUnusedParameters: false,
|
|
66
|
+
rootDir: undefined,
|
|
67
|
+
};
|
|
68
|
+
const baseHost = ts.createCompilerHost(extractionOptions, true);
|
|
43
69
|
const host = overlayHost(baseHost, harnessPath, harnessSource);
|
|
44
70
|
const program = ts.createProgram({
|
|
45
71
|
rootNames: [harnessPath, ...parsed.fileNames],
|
|
46
|
-
options:
|
|
72
|
+
options: extractionOptions,
|
|
47
73
|
host,
|
|
48
74
|
});
|
|
49
75
|
const harnessSf = program.getSourceFile(harnessPath);
|
|
@@ -126,6 +152,13 @@ export async function extractContract(options) {
|
|
|
126
152
|
access: p.meta['access'] === 'authenticated' ? 'authenticated' : 'public',
|
|
127
153
|
extra: {},
|
|
128
154
|
};
|
|
155
|
+
if (p.kind === 'subscription') {
|
|
156
|
+
// `tracked()` yields resolve to a true literal via the harness; without
|
|
157
|
+
// tracked events the client cannot resume, so the flag stays false.
|
|
158
|
+
const trackedType = typeFromAlias(harnessSf, checker, pathAlias(p.path, 'tracked'));
|
|
159
|
+
meta.extra['supportsLastEventId'] =
|
|
160
|
+
trackedType !== undefined && checker.typeToString(trackedType) === 'true';
|
|
161
|
+
}
|
|
129
162
|
entries.push({ path: p.path, kind: p.kind, input, output, meta, docs: null });
|
|
130
163
|
}
|
|
131
164
|
classifier.currentProcedure = undefined;
|
package/dist/harness.d.ts
CHANGED
|
@@ -4,9 +4,19 @@
|
|
|
4
4
|
* utilities - never private internals. Per-path aliases force indexed-access
|
|
5
5
|
* evaluation so deep conditional/mapped types resolve concretely, and make
|
|
6
6
|
* per-procedure failures point at exactly one line.
|
|
7
|
+
*
|
|
8
|
+
* Subscription procedures get an extra unwrap layer: tRPC v11 subscription
|
|
9
|
+
* outputs are `AsyncGenerator<Y>` / `AsyncIterable<Y>` / `Observable<Y>` and
|
|
10
|
+
* tracked generators wrap each yield in `TrackedEnvelope<T>` ([id, data,
|
|
11
|
+
* symbol]), so the aliases resolve straight to the streamed item type Y.
|
|
7
12
|
*/
|
|
8
13
|
export declare function harnessFileName(): string;
|
|
9
|
-
export
|
|
10
|
-
export declare function
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
export type HarnessAliasRole = 'in' | 'out' | 'tracked';
|
|
15
|
+
export declare function pathAlias(path: string, role: HarnessAliasRole): string;
|
|
16
|
+
export declare function buildHarness(entryImportPath: string, exportName: string, paths: string[], subscriptionPaths?: ReadonlySet<string>): string;
|
|
17
|
+
/** './router.ts' -> './router' (bundler-style), or explicit extensions under
|
|
18
|
+
* node16/nodenext resolution, where extensionless relative imports are
|
|
19
|
+
* illegal (TS5097) and the TS-standard specifiers (.js/.mjs/.cjs) resolve to
|
|
20
|
+
* the .ts/.mts/.cts sources with or without allowImportingTsExtensions.
|
|
21
|
+
*/
|
|
22
|
+
export declare function entryImportPath(tsconfigDir: string, entryAbsolute: string, resolution?: 'node16' | 'nodenext'): string;
|
package/dist/harness.js
CHANGED
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
* utilities - never private internals. Per-path aliases force indexed-access
|
|
5
5
|
* evaluation so deep conditional/mapped types resolve concretely, and make
|
|
6
6
|
* per-procedure failures point at exactly one line.
|
|
7
|
+
*
|
|
8
|
+
* Subscription procedures get an extra unwrap layer: tRPC v11 subscription
|
|
9
|
+
* outputs are `AsyncGenerator<Y>` / `AsyncIterable<Y>` / `Observable<Y>` and
|
|
10
|
+
* tracked generators wrap each yield in `TrackedEnvelope<T>` ([id, data,
|
|
11
|
+
* symbol]), so the aliases resolve straight to the streamed item type Y.
|
|
7
12
|
*/
|
|
8
13
|
export function harnessFileName() {
|
|
9
14
|
return '__rpc_dart_harness__.ts';
|
|
@@ -12,14 +17,21 @@ export function pathAlias(path, role) {
|
|
|
12
17
|
const segments = path.split('.').map((s) => s.replace(/[^A-Za-z0-9_]/g, '_'));
|
|
13
18
|
return ['__p', ...segments, role].join('_');
|
|
14
19
|
}
|
|
15
|
-
export function buildHarness(entryImportPath, exportName, paths) {
|
|
20
|
+
export function buildHarness(entryImportPath, exportName, paths, subscriptionPaths = new Set()) {
|
|
16
21
|
const aliases = paths
|
|
17
22
|
.map((p) => {
|
|
18
23
|
const access = p
|
|
19
24
|
.split('.')
|
|
20
25
|
.map((s) => `[${JSON.stringify(s)}]`)
|
|
21
26
|
.join('');
|
|
22
|
-
|
|
27
|
+
const lines = [`export type ${pathAlias(p, 'in')} = __rpc_dart_inputs${access};`];
|
|
28
|
+
if (subscriptionPaths.has(p)) {
|
|
29
|
+
lines.push(`export type ${pathAlias(p, 'out')} = __rpc_dart_unwrap_item<__rpc_dart_outputs${access}>;`, `export type ${pathAlias(p, 'tracked')} = __rpc_dart_is_tracked<__rpc_dart_outputs${access}>;`);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
lines.push(`export type ${pathAlias(p, 'out')} = __rpc_dart_outputs${access};`);
|
|
33
|
+
}
|
|
34
|
+
return lines.join('\n');
|
|
23
35
|
})
|
|
24
36
|
.join('\n');
|
|
25
37
|
return `import type { ${exportName} } from ${JSON.stringify(entryImportPath)};
|
|
@@ -29,12 +41,41 @@ type __rpc_dart_router = typeof ${exportName};
|
|
|
29
41
|
export type __rpc_dart_inputs = inferRouterInputs<__rpc_dart_router>;
|
|
30
42
|
export type __rpc_dart_outputs = inferRouterOutputs<__rpc_dart_router>;
|
|
31
43
|
|
|
44
|
+
type __rpc_dart_unwrap_item<T> =
|
|
45
|
+
T extends { subscribe(observer: infer O): unknown }
|
|
46
|
+
? O extends { next(value: infer Y): void } ? __rpc_dart_unwrap_tracked<Y> : never
|
|
47
|
+
: T extends AsyncIterable<infer Y> ? __rpc_dart_unwrap_tracked<Y> : T;
|
|
48
|
+
// tRPC v11 hands clients tracked events as either the branded tuple
|
|
49
|
+
// TrackedEnvelope<T> = [id, T, symbol] or its TrackedData<T> = { id, data }
|
|
50
|
+
// projection; both reduce to the payload T.
|
|
51
|
+
type __rpc_dart_unwrap_tracked<Y> =
|
|
52
|
+
Y extends readonly [string, infer D, symbol]
|
|
53
|
+
? D
|
|
54
|
+
: Y extends { id: string; data: infer D } ? D : Y;
|
|
55
|
+
type __rpc_dart_is_tracked<T> =
|
|
56
|
+
T extends AsyncIterable<infer Y>
|
|
57
|
+
? Y extends readonly [string, unknown, symbol]
|
|
58
|
+
? true
|
|
59
|
+
: Y extends { id: string; data: unknown } ? true : false
|
|
60
|
+
: false;
|
|
61
|
+
|
|
32
62
|
${aliases}
|
|
33
63
|
`;
|
|
34
64
|
}
|
|
35
|
-
/** './router.ts' -> './router' (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
65
|
+
/** './router.ts' -> './router' (bundler-style), or explicit extensions under
|
|
66
|
+
* node16/nodenext resolution, where extensionless relative imports are
|
|
67
|
+
* illegal (TS5097) and the TS-standard specifiers (.js/.mjs/.cjs) resolve to
|
|
68
|
+
* the .ts/.mts/.cts sources with or without allowImportingTsExtensions.
|
|
69
|
+
*/
|
|
70
|
+
export function entryImportPath(tsconfigDir, entryAbsolute, resolution) {
|
|
71
|
+
const rel = relative(tsconfigDir, entryAbsolute);
|
|
72
|
+
const specifier = resolution === undefined
|
|
73
|
+
? rel.replace(/\.(mts|cts|tsx?)$/, '')
|
|
74
|
+
: rel
|
|
75
|
+
.replace(/\.tsx$/, '.js')
|
|
76
|
+
.replace(/\.mts$/, '.mjs')
|
|
77
|
+
.replace(/\.cts$/, '.cjs')
|
|
78
|
+
.replace(/\.ts$/, '.js');
|
|
79
|
+
return specifier.startsWith('.') ? specifier : `./${specifier}`;
|
|
39
80
|
}
|
|
40
81
|
import { relative } from 'node:path';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpc-dart/trpc-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "tRPC v11 extraction adapter: route probe, inference harness, and Contract IR normalizer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@rpc-dart/core": "0.
|
|
12
|
+
"@rpc-dart/core": "0.2.0",
|
|
13
13
|
"jiti": "^2.6.0",
|
|
14
14
|
"typescript": "^5.9.0"
|
|
15
15
|
},
|