refactor(tui): migrate v2 calls to new client
This commit is contained in:
parent
9ff7ef3fb0
commit
e2eed76101
17 changed files with 2834 additions and 3590 deletions
|
|
@ -296,6 +296,7 @@ export function emitPromise(
|
|||
contract: Contract,
|
||||
options?: {
|
||||
readonly outputTypes?: Readonly<Record<string, { readonly name: string; readonly import: string }>>
|
||||
readonly mutableOutputs?: boolean
|
||||
},
|
||||
): Output {
|
||||
const groups = contract.groups
|
||||
|
|
@ -305,7 +306,7 @@ export function emitPromise(
|
|||
return {
|
||||
operations: promiseOperations(groups),
|
||||
files: [
|
||||
{ path: "types.ts", content: renderPromiseTypes(groups, options?.outputTypes) },
|
||||
{ path: "types.ts", content: renderPromiseTypes(groups, options?.outputTypes, options?.mutableOutputs ?? false) },
|
||||
{
|
||||
path: "client-error.ts",
|
||||
content: `export type ClientErrorReason = "Transport" | "UnexpectedStatus" | "UnsupportedContentType" | "MalformedResponse"\n\nexport class ClientError extends Error {\n override readonly name = "ClientError"\n constructor(readonly reason: ClientErrorReason, options?: ErrorOptions) {\n super(reason, options)\n }\n}\n`,
|
||||
|
|
@ -568,6 +569,7 @@ function renderImportedProjection(groups: ReadonlyArray<Group>, endpoints: Reado
|
|||
function renderPromiseTypes(
|
||||
groups: ReadonlyArray<Group>,
|
||||
outputTypes?: Readonly<Record<string, { readonly name: string; readonly import: string }>>,
|
||||
mutableOutputs = false,
|
||||
) {
|
||||
const types = new Map<SchemaAST.AST, string>()
|
||||
const typeOf = (schema: Schema.Top, decoded = false) => {
|
||||
|
|
@ -623,20 +625,25 @@ function renderPromiseTypes(
|
|||
: successSchema.events
|
||||
: successSchema,
|
||||
)
|
||||
const output = mutableOutputs ? mutableType(success) : success
|
||||
return [
|
||||
...(promiseInputMode(endpoint) === "none" ? [] : [`export type ${prefix}Input = { ${input} }`]),
|
||||
`export type ${prefix}Output = ${endpoint.unwrapData ? `(${success})["data"]` : success}`,
|
||||
`export type ${prefix}Output = ${endpoint.unwrapData ? `(${output})["data"]` : output}`,
|
||||
]
|
||||
}),
|
||||
)
|
||||
.join("\n\n")
|
||||
const json = operations.includes("JsonValue")
|
||||
? "export type JsonValue = null | boolean | number | string | ReadonlyArray<JsonValue> | { readonly [key: string]: JsonValue }"
|
||||
? `export type JsonValue = null | boolean | number | string | ${mutableOutputs ? "Array<JsonValue> | { [key: string]: JsonValue }" : "ReadonlyArray<JsonValue> | { readonly [key: string]: JsonValue }"}`
|
||||
: ""
|
||||
const imports = [...new Set(Object.values(outputTypes ?? {}).map((override) => override.import))]
|
||||
return [...imports, json, ...errorTypes, operations].filter(Boolean).join("\n\n")
|
||||
}
|
||||
|
||||
function mutableType(type: string) {
|
||||
return type.replaceAll("ReadonlyArray<", "Array<").replaceAll(/\breadonly\s+/g, "")
|
||||
}
|
||||
|
||||
function renderPromiseClient(groups: ReadonlyArray<Group>) {
|
||||
const imports = groups.flatMap((group) =>
|
||||
group.endpoints.flatMap((endpoint) => {
|
||||
|
|
|
|||
|
|
@ -513,6 +513,24 @@ describe("HttpApiCodegen.generate", () => {
|
|||
)
|
||||
})
|
||||
|
||||
test("emits mutable Promise outputs without restricting inputs", () => {
|
||||
const output = emitPromise(
|
||||
compileContract(
|
||||
api(
|
||||
HttpApiEndpoint.post("create", "/session", {
|
||||
payload: Schema.Struct({ values: Schema.Array(Schema.String) }),
|
||||
success: Schema.Struct({ data: Schema.Array(Schema.Struct({ values: Schema.Array(Schema.String) })) }),
|
||||
}),
|
||||
),
|
||||
),
|
||||
{ mutableOutputs: true },
|
||||
)
|
||||
const types = output.files.find((file) => file.path === "types.ts")?.content
|
||||
|
||||
expect(types).toContain('readonly "values": ReadonlyArray<string>')
|
||||
expect(types).toContain('export type SessionCreateOutput = ({ "data": Array<{ "values": Array<string> }> })["data"]')
|
||||
})
|
||||
|
||||
test("expands Promise references only at identifier boundaries", () => {
|
||||
const Session = Schema.Struct({ name: Schema.Literal("Session"), id: Schema.String }).annotate({
|
||||
identifier: "Session",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue