Skip to content

Commit b787e81

Browse files
committed
fix(typegen): malformed function args
E.g. ```sql create function f(OUT text) returns void as $$ $$ language sql; ``` Shows up as ```ts export type Json = | string | number | boolean | null | { [key: string]: Json } | Json[]; export interface Database { extensions: { Functions: { f: { Args: { OUT: text }; Returns: undefined; }; }; }; } ```
1 parent 10cfc28 commit b787e81

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/server/templates/typescript.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,26 @@ export interface Database {
2727
${schemas.map((schema) => {
2828
const schemaTables = tables.filter((table) => table.schema === schema.name)
2929
const schemaViews = views.filter((view) => view.schema === schema.name)
30-
const schemaFunctions = functions.filter((func) => func.schema === schema.name)
30+
const schemaFunctions = functions.filter((func) => {
31+
if (func.schema !== schema.name) {
32+
return false
33+
}
34+
35+
// Either:
36+
// 1. All input args are be named, or
37+
// 2. There is only one input arg which is unnamed
38+
const inArgs = func.args.filter(({ mode }) => ['in', 'inout', 'variadic'].includes(mode))
39+
40+
if (!inArgs.some(({ name }) => name === '')) {
41+
return true
42+
}
43+
44+
if (inArgs.length === 1) {
45+
return true
46+
}
47+
48+
return false
49+
})
3150
const schemaEnums = types.filter((type) => type.schema === schema.name && type.enums.length > 0)
3251
return `${JSON.stringify(schema.name)}: {
3352
Tables: {
@@ -176,20 +195,16 @@ export interface Database {
176195
([fnName, fns]) =>
177196
`${JSON.stringify(fnName)}: ${fns
178197
.map(
179-
(fn) => `{
198+
({ args, return_type }) => `{
180199
Args: ${(() => {
181-
if (fn.argument_types === '') {
182-
return 'Record<PropertyKey, never>'
183-
}
200+
const inArgs = args.filter(({ mode }) => mode === 'in')
184201
185-
const splitArgs = fn.argument_types.split(',').map((arg) => arg.trim())
186-
if (splitArgs.some((arg) => arg.includes('"') || !arg.includes(' '))) {
187-
return 'Record<string, unknown>'
202+
if (inArgs.length === 0) {
203+
return 'Record<PropertyKey, never>'
188204
}
189205
190-
const argsNameAndType = splitArgs.map((arg) => {
191-
const [name, ...rest] = arg.split(' ')
192-
const type = types.find((_type) => _type.format === rest.join(' '))
206+
const argsNameAndType = inArgs.map(({ name, type_id }) => {
207+
const type = types.find(({ id }) => id === type_id)
193208
if (!type) {
194209
return { name, type: 'unknown' }
195210
}
@@ -200,7 +215,7 @@ export interface Database {
200215
({ name, type }) => `${JSON.stringify(name)}: ${type}`
201216
)} }`
202217
})()}
203-
Returns: ${pgTypeToTsType(fn.return_type, types, schemas)}
218+
Returns: ${pgTypeToTsType(return_type, types, schemas)}
204219
}`
205220
)
206221
.join('|')}`
@@ -225,6 +240,7 @@ export interface Database {
225240

226241
output = prettier.format(output, {
227242
parser: 'typescript',
243+
semi: false,
228244
})
229245
return output
230246
}

0 commit comments

Comments
 (0)