Skip to content

Commit f90f034

Browse files
ianmacartneyConvex, Inc.
authored andcommitted
add Id to the generated api.d.ts file when doing static type generation (#42050)
We don't currently define the `Id` type that we reference when doing static api.d.ts generation - leading to any ID in those types being treated as `any` (not validated on arguments or useful in return values). GitOrigin-RevId: f5e18f583d3328e2e14229ae71c25b6561705a0d
1 parent 7c8171c commit f90f034

File tree

16 files changed

+1136
-5
lines changed

16 files changed

+1136
-5
lines changed

npm-packages/common/config/rush/pnpm-lock.yaml

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

npm-packages/convex/src/cli/codegen_templates/component_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ async function* codegenStaticApiObjects(
135135
analysis: EvaluatedComponentDefinition,
136136
) {
137137
yield `import type { FunctionReference } from "convex/server";`;
138+
yield `import type { GenericId as Id } from "convex/values";`;
138139

139140
const apiTree = await buildApiTree(ctx, analysis.functions, {
140141
kind: "public",

npm-packages/convex/src/cli/codegen_templates/dataModel.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ async function* codegenTable(ctx: Context, table: TableDefinition) {
261261
for (const index of table.vectorIndexes ?? []) {
262262
yield ` "${index.indexDescriptor}": {`;
263263
yield ` vectorField: "${index.vectorField}",`;
264-
yield ` dimensions: ${index.dimensions},`;
264+
yield ` dimensions: number,`;
265265
yield ` filterFields: ${stringLiteralUnionType(index.filterFields)},`;
266266
yield ` },`;
267267
}
@@ -340,14 +340,19 @@ async function addSystemFieldsToObject(
340340
function* extractFieldPaths(validator: ConvexValidator): Generator<string[]> {
341341
if (validator.type === "object") {
342342
for (const [fieldName, fieldValidator] of Object.entries(validator.value)) {
343+
yield [fieldName];
343344
for (const subFieldPath of extractFieldPaths(fieldValidator.fieldType)) {
344-
yield [fieldName, ...subFieldPath];
345+
if (subFieldPath.length > 0) {
346+
yield [fieldName, ...subFieldPath];
347+
}
345348
}
346349
}
347350
} else if (validator.type === "union") {
348351
for (const subValidator of validator.value) {
349352
yield* extractFieldPaths(subValidator);
350353
}
354+
} else if (validator.type === "record") {
355+
yield ["${string}"];
351356
} else {
352357
yield [];
353358
}
@@ -356,9 +361,9 @@ function* extractFieldPaths(validator: ConvexValidator): Generator<string[]> {
356361
function stringLiteralUnionType(fields: string[]) {
357362
if (fields.length === 0) {
358363
return "never";
359-
} else if (fields.length === 1) {
360-
return `"${fields[0]}"`;
361364
} else {
362-
return fields.map((field) => `"${field}"`).join(" | ");
365+
return fields
366+
.map((field) => (field.includes("${") ? `\`${field}\`` : `"${field}"`))
367+
.join(" | ");
363368
}
364369
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"codegen": {
3+
"staticApi": true,
4+
"staticDataModel": true
5+
}
6+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* eslint-disable */
2+
/**
3+
* Generated `api` utility.
4+
*
5+
* THIS CODE IS AUTOMATICALLY GENERATED.
6+
*
7+
* To regenerate, run `npx convex dev`.
8+
* @module
9+
*/
10+
11+
import type { FunctionReference } from "convex/server";
12+
import type { GenericId as Id } from "convex/values";
13+
14+
/**
15+
* A utility for referencing Convex functions in your app's public API.
16+
*
17+
* Usage:
18+
* ```js
19+
* const myFunctionReference = api.myModule.myFunction;
20+
* ```
21+
*/
22+
export declare const api: {
23+
staticFunctions: {
24+
q: FunctionReference<
25+
"query",
26+
"public",
27+
{ branded: string; id: Id<"empty"> },
28+
{ _creationTime: number; _id: Id<"empty"> } | string
29+
>;
30+
m: FunctionReference<
31+
"mutation",
32+
"public",
33+
{ branded: string; id: Id<"empty"> },
34+
string
35+
>;
36+
a: FunctionReference<
37+
"action",
38+
"public",
39+
{ branded: string; id: Id<"empty"> },
40+
string
41+
>;
42+
};
43+
};
44+
45+
/**
46+
* A utility for referencing Convex functions in your app's internal API.
47+
*
48+
* Usage:
49+
* ```js
50+
* const myFunctionReference = internal.myModule.myFunction;
51+
* ```
52+
*/
53+
export declare const internal: {
54+
staticFunctions: {
55+
iq: FunctionReference<
56+
"query",
57+
"internal",
58+
{
59+
arr: Array<number>;
60+
bool: boolean;
61+
data: ArrayBuffer;
62+
id: Id<"empty">;
63+
literal: "literal";
64+
null: null;
65+
num: number;
66+
str: string;
67+
},
68+
| string
69+
| "literal"
70+
| number
71+
| boolean
72+
| ArrayBuffer
73+
| Array<number>
74+
| null
75+
| Id<"empty">
76+
>;
77+
im: FunctionReference<"mutation", "internal", { branded: string }, null>;
78+
ia: FunctionReference<"action", "internal", {}, any>;
79+
};
80+
};
81+
82+
export declare const components: {};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable */
2+
/**
3+
* Generated `api` utility.
4+
*
5+
* THIS CODE IS AUTOMATICALLY GENERATED.
6+
*
7+
* To regenerate, run `npx convex dev`.
8+
* @module
9+
*/
10+
11+
import { anyApi, componentsGeneric } from "convex/server";
12+
13+
/**
14+
* A utility for referencing Convex functions in your app's API.
15+
*
16+
* Usage:
17+
* ```js
18+
* const myFunctionReference = api.myModule.myFunction;
19+
* ```
20+
*/
21+
export const api = anyApi;
22+
export const internal = anyApi;
23+
export const components = componentsGeneric();

0 commit comments

Comments
 (0)