mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(httpapi-codegen): emit flat Promise input field types (#58875)
This commit is contained in:
committed by
GitHub
Unverified
parent
490456db53
commit
ed74ec82ce
+658
-2551
File diff suppressed because it is too large
Load Diff
@@ -810,6 +810,17 @@ function renderPromiseTypes(
|
||||
types.set(projected.ast, type)
|
||||
return type
|
||||
}
|
||||
const fieldTypeOf = (schema: Schema.Top, name: string, decoded: boolean) => {
|
||||
const ast = (decoded ? Schema.toType(schema) : Schema.toEncoded(schema)).ast
|
||||
const property = SchemaAST.isObjects(ast)
|
||||
? ast.propertySignatures.find((property) => property.name === name)
|
||||
: undefined
|
||||
if (property === undefined) return `(${typeOf(schema, decoded)})[${JSON.stringify(name)}]`
|
||||
const type = Schema.make<Schema.Top>(property.type)
|
||||
// Indexing an optional property includes undefined, so keep it when the field schema omits it.
|
||||
if (!SchemaAST.isOptional(property.type) || includesUndefined(property.type)) return typeOf(type)
|
||||
return typeOf(Schema.UndefinedOr(type))
|
||||
}
|
||||
const outputMarkers = new Map<SchemaAST.AST, string>()
|
||||
const outputSchemas: Array<Schema.Top> = []
|
||||
const outputTypeOf = (schema: Schema.Top) => {
|
||||
@@ -853,7 +864,7 @@ function renderPromiseTypes(
|
||||
const schema = schemas[field.source]
|
||||
if (schema === undefined)
|
||||
throw new GenerationError({ reason: `Missing input schema: ${prefix}.${field.name}` })
|
||||
return `readonly ${JSON.stringify(field.name)}${field.optional ? "?" : ""}: ${isOpaquePayload(endpoint) && field.source === "payload" ? typeOf(schema) : `(${typeOf(schema, field.source === "query")})[${JSON.stringify(field.name)}]`}`
|
||||
return `readonly ${JSON.stringify(field.name)}${field.optional ? "?" : ""}: ${isOpaquePayload(endpoint) && field.source === "payload" ? typeOf(schema) : fieldTypeOf(schema, field.name, field.source === "query")}`
|
||||
})
|
||||
.join("; ")
|
||||
const successSchema = endpoint.successes[0]
|
||||
@@ -1144,6 +1155,11 @@ function structuralType(schema: Schema.Top) {
|
||||
)
|
||||
}
|
||||
|
||||
function includesUndefined(ast: SchemaAST.AST): boolean {
|
||||
if (SchemaAST.isUndefined(ast)) return true
|
||||
return SchemaAST.isUnion(ast) && ast.types.some(includesUndefined)
|
||||
}
|
||||
|
||||
function promiseTypeAst(schema: Schema.Top) {
|
||||
return codegenAst(schema.ast)
|
||||
}
|
||||
|
||||
@@ -786,6 +786,28 @@ describe("HttpApiCodegen.generate", () => {
|
||||
expect(types).not.toContain("Referenced1")
|
||||
})
|
||||
|
||||
test("emits flat Promise input field types", () => {
|
||||
const output = emitPromise(
|
||||
compileContract(
|
||||
api(
|
||||
HttpApiEndpoint.get("subscribe", "/session/:id/event", {
|
||||
params: { id: Schema.String },
|
||||
query: Schema.Struct({
|
||||
types: Schema.optional(Schema.ArrayEnsure(Schema.Literals(["created", "deleted"]))),
|
||||
cursor: Schema.optionalKey(Schema.String),
|
||||
}).annotate({ identifier: "SubscribeQuery" }),
|
||||
success: Schema.String,
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
const types = output.files.find((file) => file.path === "types.ts")?.content
|
||||
|
||||
expect(types).toContain(
|
||||
'export type SessionSubscribeInput = { readonly "id": string; readonly "types"?: ReadonlyArray<"created" | "deleted"> | undefined; readonly "cursor"?: string | undefined }',
|
||||
)
|
||||
})
|
||||
|
||||
test("inlines shared anonymous Promise wire types", () => {
|
||||
const Shared = Schema.Struct({ value: Schema.String })
|
||||
const output = emitPromise(
|
||||
|
||||
Reference in New Issue
Block a user