Skip to content

Commit d38a89f

Browse files
authored
Merge pull request #76 from RhysSullivan/token-efficiency-responses
Improve TypeScript preview token efficiency
2 parents 149234f + 855ac0c commit d38a89f

3 files changed

Lines changed: 306 additions & 19 deletions

File tree

‎packages/core/sdk/src/__fixtures__/stripe-get-balance-transactions-id.json‎

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

‎packages/core/sdk/src/schema-types.test.ts‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFileSync } from "node:fs";
12
import { describe, expect, it } from "@effect/vitest";
23

34
import {
@@ -6,6 +7,16 @@ import {
67
schemaToTypeScriptPreviewWithDefs,
78
} from "./schema-types";
89

10+
const stripeBalanceTransactionsFixture = JSON.parse(
11+
readFileSync(
12+
new URL("./__fixtures__/stripe-get-balance-transactions-id.json", import.meta.url),
13+
"utf8",
14+
),
15+
) as {
16+
schema: unknown;
17+
defs: Record<string, unknown>;
18+
};
19+
920
describe("schema-types", () => {
1021
it("reuses referenced definitions instead of inlining them", () => {
1122
const schema = {
@@ -70,6 +81,154 @@ describe("schema-types", () => {
7081
});
7182
});
7283

84+
it("limits referenced definitions to 3 levels and marks deeper refs as omitted", () => {
85+
const defs = new Map<string, unknown>([
86+
[
87+
"LevelOne",
88+
{
89+
type: "object",
90+
properties: {
91+
next: { $ref: "#/$defs/LevelTwo" },
92+
},
93+
required: ["next"],
94+
additionalProperties: false,
95+
},
96+
],
97+
[
98+
"LevelTwo",
99+
{
100+
type: "object",
101+
properties: {
102+
next: { $ref: "#/$defs/LevelThree" },
103+
},
104+
required: ["next"],
105+
additionalProperties: false,
106+
},
107+
],
108+
[
109+
"LevelThree",
110+
{
111+
type: "object",
112+
properties: {
113+
next: { $ref: "#/$defs/LevelFour" },
114+
},
115+
required: ["next"],
116+
additionalProperties: false,
117+
},
118+
],
119+
[
120+
"LevelFour",
121+
{
122+
type: "object",
123+
properties: {
124+
value: { type: "string" },
125+
},
126+
required: ["value"],
127+
additionalProperties: false,
128+
},
129+
],
130+
]);
131+
132+
expect(
133+
schemaToTypeScriptPreviewWithDefs(
134+
{
135+
$ref: "#/$defs/LevelOne",
136+
},
137+
defs,
138+
),
139+
).toEqual({
140+
type: "LevelOne",
141+
definitions: {
142+
LevelOne: "{ next: LevelTwo }",
143+
LevelTwo: "{ next: LevelThree }",
144+
LevelThree: "{ next: unknown /* LevelFour omitted */ }",
145+
},
146+
});
147+
});
148+
149+
it("keeps ordinary unions expanded when they stay under the composite threshold", () => {
150+
const defs = new Map<string, unknown>([
151+
[
152+
"Pet",
153+
{
154+
anyOf: [
155+
{ $ref: "#/$defs/Dog" },
156+
{ $ref: "#/$defs/Cat" },
157+
{ $ref: "#/$defs/Lizard" },
158+
],
159+
},
160+
],
161+
[
162+
"Dog",
163+
{
164+
type: "object",
165+
properties: {
166+
bark: { type: "boolean" },
167+
},
168+
required: ["bark"],
169+
additionalProperties: false,
170+
},
171+
],
172+
[
173+
"Cat",
174+
{
175+
type: "object",
176+
properties: {
177+
meow: { type: "boolean" },
178+
},
179+
required: ["meow"],
180+
additionalProperties: false,
181+
},
182+
],
183+
[
184+
"Lizard",
185+
{
186+
type: "object",
187+
properties: {
188+
scales: { type: "boolean" },
189+
},
190+
required: ["scales"],
191+
additionalProperties: false,
192+
},
193+
],
194+
]);
195+
196+
expect(
197+
schemaToTypeScriptPreviewWithDefs(
198+
{
199+
$ref: "#/$defs/Pet",
200+
},
201+
defs,
202+
),
203+
).toEqual({
204+
type: "Pet",
205+
definitions: {
206+
Cat: "{ meow: boolean }",
207+
Dog: "{ bark: boolean }",
208+
Lizard: "{ scales: boolean }",
209+
Pet: "Dog | Cat | Lizard",
210+
},
211+
});
212+
});
213+
214+
it("summarizes large unions from real Stripe fixtures without pulling in the whole schema graph", () => {
215+
const defs = new Map(Object.entries(stripeBalanceTransactionsFixture.defs));
216+
217+
expect(
218+
schemaToTypeScriptPreviewWithDefs(
219+
stripeBalanceTransactionsFixture.schema,
220+
defs,
221+
),
222+
).toEqual({
223+
type: "balance_transaction",
224+
definitions: {
225+
balance_transaction: expect.stringContaining("fee_details: fee[]"),
226+
balance_transaction_source: "unknown /* 16-way anyOf omitted */",
227+
fee: "{ amount: number; application: string | null; currency: string; description: string | null; type: string }",
228+
},
229+
});
230+
});
231+
73232
it("merges input and output TypeScript definitions", () => {
74233
const defs = new Map<string, unknown>([
75234
[

0 commit comments

Comments
 (0)