Skip to content

Commit 518038b

Browse files
committed
chore: align output to docs
1 parent 51becd4 commit 518038b

4 files changed

Lines changed: 48 additions & 25 deletions

File tree

packages/openapi-ts-tests/orpc/v1/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@hey-api/openapi-ts": "workspace:*",
1111
"@orpc/contract": "1.13.4",
1212
"typescript": "5.9.3",
13+
"valibot": "1.2.0",
1314
"zod": "4.3.6"
1415
},
1516
"engines": {

packages/openapi-ts/src/plugins/orpc/contracts/node.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { StructureItem, StructureNode, StructureShell, Symbol } from '@hey-api/codegen-core';
1+
import type {
2+
StructureItem,
3+
StructureNode,
4+
StructureShell,
5+
Symbol,
6+
SymbolMeta,
7+
} from '@hey-api/codegen-core';
28
import type { IR } from '@hey-api/shared';
39
import { applyNaming } from '@hey-api/shared';
410

@@ -15,6 +21,15 @@ export interface ContractItem {
1521

1622
export const source = globalThis.Symbol('orpc');
1723

24+
function createShellMeta(node: StructureNode): SymbolMeta {
25+
return {
26+
category: 'contract',
27+
resource: 'container',
28+
resourceId: node.getPath().join('.'),
29+
tool: 'orpc',
30+
};
31+
}
32+
1833
function createContractSymbol(
1934
plugin: OrpcPlugin['Instance'],
2035
item: StructureItem & { data: ContractItem },
@@ -100,16 +115,17 @@ function buildContainerObject(
100115
const contractSymbol = symbols.get(operation.id)!;
101116
const name = item.location[item.location.length - 1]!;
102117
const propName = applyNaming(name, plugin.config.contracts.contractName);
103-
obj.prop(propName, $(contractSymbol));
118+
obj.prop(propName, contractSymbol);
104119
}
105120

106121
for (const child of node.children.values()) {
107122
if (child.shell) {
108123
const childShell = child.shell.define(child);
109-
const childSymbol = (childShell.node as ReturnType<typeof $.const>).symbol;
124+
const childNode = childShell.node as ReturnType<typeof $.const>;
125+
const childSymbol = childNode.symbol;
110126
if (childSymbol) {
111127
const propName = applyNaming(child.name, plugin.config.contracts.segmentName);
112-
obj.prop(propName, $(childSymbol));
128+
obj.prop(propName, childSymbol);
113129
}
114130
}
115131
}
@@ -118,26 +134,29 @@ function buildContainerObject(
118134
}
119135

120136
export function createShell(plugin: OrpcPlugin['Instance']): StructureShell {
137+
const cache = new Map<string | number, ReturnType<typeof $.const>>();
138+
121139
return {
122140
define: (node) => {
141+
const resourceId = node.getPath().join('.');
142+
const cached = cache.get(resourceId);
143+
if (cached) {
144+
return { dependencies: [], node: cached };
145+
}
123146
const symbol = plugin.symbol(
124147
applyNaming(
125148
node.name,
126149
node.isRoot ? plugin.config.contracts.containerName : plugin.config.contracts.segmentName,
127150
),
128151
{
129-
meta: {
130-
category: 'contract',
131-
resource: 'container',
132-
resourceId: node.getPath().join('.'),
133-
tool: plugin.name,
134-
},
152+
meta: createShellMeta(node),
135153
},
136154
);
137155

138-
const placeholder = $.const(symbol).export().assign($.object());
156+
const o = $.const(symbol).export().assign($.object());
157+
cache.set(resourceId, o);
139158

140-
return { dependencies: [], node: placeholder };
159+
return { dependencies: [], node: o };
141160
},
142161
};
143162
}

packages/openapi-ts/src/plugins/orpc/v1/plugin.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,34 @@ export const handlerV1: OrpcPlugin['Handler'] = ({ plugin }) => {
2020
);
2121
plugin.node(baseNode);
2222

23-
const contractsStructure = new StructureModel();
24-
const contractsShell = createShell(plugin);
25-
const contractsStrategy = resolveStrategy(plugin);
23+
const structure = new StructureModel();
24+
const shell = createShell(plugin);
25+
const strategy = resolveStrategy(plugin);
2626

2727
plugin.forEach(
2828
'operation',
2929
(event) => {
30-
const { operation } = event;
31-
32-
const contractPaths = contractsStrategy(operation);
33-
contractsStructure.insert({
30+
structure.insert({
3431
data: {
35-
operation,
32+
operation: event.operation,
3633
path: event._path,
3734
tags: event.tags,
3835
} satisfies ContractItem,
39-
locations: contractPaths.map((path) => ({ path, shell: contractsShell })),
36+
locations: strategy(event.operation).map((path) => ({ path, shell })),
4037
source,
4138
});
4239
},
4340
{ order: 'declarations' },
4441
);
4542

46-
for (const node of contractsStructure.walk()) {
43+
const allNodes: Array<ReturnType<typeof $.class | typeof $.var>> = [];
44+
45+
for (const node of structure.walk()) {
4746
const { nodes } = toNode(node, plugin, baseSymbol);
47+
allNodes.push(...nodes);
48+
}
4849

49-
for (const node of nodes) {
50-
plugin.node(node);
51-
}
50+
for (const node of allNodes) {
51+
plugin.node(node);
5252
}
5353
};

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)