Skip to content

Commit

Permalink
Make component types resistant to multiple convex packages (#29972)
Browse files Browse the repository at this point in the history
Modify component types to allow multiple copies of the convex library to typecheck against each other.

GitOrigin-RevId: 0577742902edefeebd634aa03a5355e4a90b5dc1
  • Loading branch information
thomasballinger authored and Convex, Inc. committed Sep 20, 2024
1 parent 64fe5c0 commit a59c0c3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion npm-packages/convex/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ build.cjs
tmpPackage*
tmpDist*
api-extractor-configs/temp/
*.log
*.log
9 changes: 2 additions & 7 deletions npm-packages/convex/src/server/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ComponentDefinitionAnalysis,
ComponentDefinitionType,
} from "./definition.js";
import { toReferencePath } from "./paths.js";
import { setReferencePath, toReferencePath } from "./paths.js";

/**
* A serializable reference to a Convex function.
Expand Down Expand Up @@ -158,15 +158,10 @@ class InstalledComponent<Definition extends ComponentDefinition<any>> {
*/
_name: string;

/**
* @internal
*/
[toReferencePath]: string;

constructor(definition: Definition, name: string) {
this._definition = definition;
this._name = name;
this[toReferencePath] = `_reference/childComponent/${name}`;
setReferencePath(this, `_reference/childComponent/${name}`);
}

get exports(): ComponentDefinitionExports<Definition> {
Expand Down
7 changes: 7 additions & 0 deletions npm-packages/convex/src/server/components/paths.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export const toReferencePath = Symbol.for("toReferencePath");

// Multiple instances of the same Symbol.for() are equal at runtime but not
// at type-time, so `[toReferencePath]` properties aren't used in types.
// Use this function to set the property invisibly.
export function setReferencePath<T>(obj: T, value: string) {
(obj as any)[toReferencePath] = value;
}

export function extractReferencePath(reference: any): string | null {
return reference[toReferencePath] ?? null;
}
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export type RouteSpec = RouteSpecWithPath | RouteSpecWithPathPrefix;
export class HttpRouter {
exactRoutes: Map<string, Map<RoutableMethod, PublicHttpAction>> = new Map();
prefixRoutes: Map<RoutableMethod, Map<string, PublicHttpAction>> = new Map();
isRouter = true;
isRouter: true = true;

/**
* Specify an HttpAction to be used to respond to requests
Expand Down

0 comments on commit a59c0c3

Please sign in to comment.