Skip to content

Commit aea6877

Browse files
committed
refactor: toPlugin api
chore: fixup
1 parent 2acaf40 commit aea6877

13 files changed

Lines changed: 44 additions & 58 deletions

File tree

.cursor/worktrees.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"setup-worktree": ["pnpm install", "pnpm run build"]
2+
"setup-worktree": [
3+
"pnpm install",
4+
"pnpm run build",
5+
"cp $ROOT_WORKTREE_PATH/apps/dev-playground/.env apps/dev-playground/.env",
6+
"cp $ROOT_WORKTREE_PATH/apps/clean-app/.env apps/clean-app/.env"
7+
]
38
}

apps/dev-playground/server/lakebase-examples-plugin.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as typeormExample from "./lakebase-examples/typeorm-example";
1919
*/
2020

2121
export class LakebaseExamplesPlugin extends Plugin {
22+
static override readonly name = "lakebase-examples";
2223
public name = "lakebase-examples";
2324
protected envVars: string[] = [];
2425

@@ -80,8 +81,4 @@ export class LakebaseExamplesPlugin extends Plugin {
8081
}
8182
}
8283

83-
export const lakebaseExamples = toPlugin<
84-
typeof LakebaseExamplesPlugin,
85-
Record<string, never>,
86-
"lakebase-examples"
87-
>(LakebaseExamplesPlugin, "lakebase-examples");
84+
export const lakebaseExamples = toPlugin(LakebaseExamplesPlugin);

apps/dev-playground/server/reconnect-plugin.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface ReconnectStreamResponse {
1414
}
1515

1616
export class ReconnectPlugin extends Plugin {
17+
static override readonly name = "reconnect";
1718
public name = "reconnect";
1819

1920
static manifest = {
@@ -84,8 +85,4 @@ export class ReconnectPlugin extends Plugin {
8485
}
8586
}
8687

87-
export const reconnect = toPlugin<
88-
typeof ReconnectPlugin,
89-
Record<string, never>,
90-
"reconnect"
91-
>(ReconnectPlugin, "reconnect");
88+
export const reconnect = toPlugin(ReconnectPlugin);

apps/dev-playground/server/telemetry-example-plugin.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import type { Request, Response, Router } from "express";
1717

1818
class TelemetryExamples extends Plugin {
19+
static override readonly name = "telemetry-examples";
1920
public name = "telemetry-examples" as const;
2021

2122
static manifest = {
@@ -522,8 +523,4 @@ class TelemetryExamples extends Plugin {
522523
}
523524
}
524525

525-
export const telemetryExamples = toPlugin<
526-
typeof TelemetryExamples,
527-
BasePluginConfig,
528-
"telemetryExamples"
529-
>(TelemetryExamples, "telemetryExamples");
526+
export const telemetryExamples = toPlugin(TelemetryExamples);

docs/docs/plugins/custom-plugins.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import { Plugin, toPlugin } from "@databricks/appkit";
2121
import type express from "express";
2222

2323
class MyPlugin extends Plugin {
24+
static override readonly name = "myPlugin";
2425
name = "myPlugin";
2526

26-
// Define resource requirements in the static manifest
2727
static manifest = {
2828
name: "myPlugin",
2929
displayName: "My Plugin",
@@ -59,17 +59,13 @@ class MyPlugin extends Plugin {
5959
}
6060

6161
exports() {
62-
// an object with the methods from this plugin to expose
6362
return {
6463
myCustomMethod: this.myCustomMethod
6564
}
6665
}
6766
}
6867

69-
export const myPlugin = toPlugin<typeof MyPlugin, Record<string, never>, "myPlugin">(
70-
MyPlugin,
71-
"myPlugin",
72-
);
68+
export const myPlugin = toPlugin(MyPlugin);
7369
```
7470

7571
## Config-dependent resources
@@ -84,6 +80,7 @@ interface MyPluginConfig extends BasePluginConfig {
8480
}
8581

8682
class MyPlugin extends Plugin<MyPluginConfig> {
83+
static override readonly name = "myPlugin";
8784
name = "myPlugin";
8885

8986
static manifest = {
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import type { PluginData, ToPlugin } from "shared";
1+
import type { PluginConstructor, PluginData, ToPlugin } from "shared";
22

33
/**
4+
* Wraps a plugin class so it can be passed to createApp with optional config.
5+
* Infers config type from the constructor and plugin name from the static `name` property.
6+
*
47
* @internal
58
*/
6-
export function toPlugin<T, U, N extends string>(
9+
export function toPlugin<T extends PluginConstructor & { name: string }>(
710
plugin: T,
8-
name: N,
9-
): ToPlugin<T, U, N> {
10-
return (config: U = {} as U): PluginData<T, U, N> => ({
11+
): ToPlugin<T, ConstructorParameters<T>[0], T["name"]> {
12+
type Config = ConstructorParameters<T>[0];
13+
type Name = T["name"];
14+
return (config: Config = {} as Config): PluginData<T, Config, Name> => ({
1115
plugin: plugin as T,
12-
config: config as U,
13-
name,
16+
config: config as Config,
17+
name: plugin.name as Name,
1418
});
1519
}

packages/appkit/src/plugins/analytics/analytics.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
const logger = createLogger("analytics");
2727

2828
export class AnalyticsPlugin extends Plugin {
29+
static override readonly name = "analytics";
2930
name = "analytics";
3031

3132
/** Plugin manifest declaring metadata and resource requirements */
@@ -285,8 +286,4 @@ export class AnalyticsPlugin extends Plugin {
285286
/**
286287
* @internal
287288
*/
288-
export const analytics = toPlugin<
289-
typeof AnalyticsPlugin,
290-
IAnalyticsConfig,
291-
"analytics"
292-
>(AnalyticsPlugin, "analytics");
289+
export const analytics = toPlugin(AnalyticsPlugin);

packages/appkit/src/plugins/genie/genie.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
const logger = createLogger("genie");
1818

1919
export class GeniePlugin extends Plugin {
20+
static override readonly name = "genie";
2021
name = "genie";
2122

2223
static manifest = genieManifest;
@@ -230,7 +231,4 @@ export class GeniePlugin extends Plugin {
230231
/**
231232
* @internal
232233
*/
233-
export const genie = toPlugin<typeof GeniePlugin, IGenieConfig, "genie">(
234-
GeniePlugin,
235-
"genie",
236-
);
234+
export const genie = toPlugin(GeniePlugin);

packages/appkit/src/plugins/lakebase/lakebase.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const logger = createLogger("lakebase");
3030
* ```
3131
*/
3232
export class LakebasePlugin extends Plugin {
33+
static override readonly name = "lakebase";
3334
name = "lakebase";
3435

3536
/** Plugin manifest declaring metadata and resource requirements */
@@ -117,8 +118,4 @@ export class LakebasePlugin extends Plugin {
117118
/**
118119
* @internal
119120
*/
120-
export const lakebase = toPlugin<
121-
typeof LakebasePlugin,
122-
ILakebaseConfig,
123-
"lakebase"
124-
>(LakebasePlugin, "lakebase");
121+
export const lakebase = toPlugin(LakebasePlugin);

packages/appkit/src/plugins/server/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class ServerPlugin extends Plugin {
4343
/** Plugin manifest declaring metadata and resource requirements */
4444
static manifest = serverManifest;
4545

46+
static override readonly name = "server";
4647
public name = "server" as const;
4748
private serverApplication: express.Application;
4849
private server: HTTPServer | null;
@@ -354,10 +355,7 @@ const EXCLUDED_PLUGINS = [ServerPlugin.name];
354355
/**
355356
* @internal
356357
*/
357-
export const server = toPlugin<typeof ServerPlugin, ServerConfig, "server">(
358-
ServerPlugin,
359-
"server",
360-
);
358+
export const server = toPlugin(ServerPlugin);
361359

362360
// Export manifest and types
363361
export { serverManifest } from "./manifest";

0 commit comments

Comments
 (0)