Skip to content

Commit e8d2d77

Browse files
committed
feat: appkit exposed apis
chore: fixup chore: fixup chore: fixup chore: fixup chore: fixup chore: fixup
1 parent 56a45f4 commit e8d2d77

22 files changed

Lines changed: 412 additions & 120 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface ReconnectStreamResponse {
1515

1616
export class ReconnectPlugin extends Plugin {
1717
public name = "reconnect";
18-
public envVars = [];
18+
protected envVars: string[] = [];
1919

2020
injectRoutes(router: IAppRouter): void {
2121
this.route<ReconnectResponse>(router, {

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

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

1818
class TelemetryExamples extends Plugin {
1919
public name = "telemetry-examples" as const;
20-
public envVars: string[] = [];
20+
protected envVars: string[] = [];
2121

2222
private requestCounter: Counter;
2323
private durationHistogram: Histogram;
@@ -516,5 +516,5 @@ class TelemetryExamples extends Plugin {
516516
export const telemetryExamples = toPlugin<
517517
typeof TelemetryExamples,
518518
BasePluginConfig,
519-
"telemetry-examples"
520-
>(TelemetryExamples, "telemetry-examples");
519+
"telemetryExamples"
520+
>(TelemetryExamples, "telemetryExamples");

docs/docs/api/appkit/Class.AppKitError.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ readonly optional cause: Error;
7676

7777
Optional cause of the error
7878

79+
#### Overrides
80+
81+
```ts
82+
Error.cause
83+
```
84+
7985
***
8086

8187
### code

docs/docs/api/appkit/Class.Plugin.md

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ BasePlugin.abortActiveOperations
142142
asUser(req: Request): this;
143143
```
144144

145-
Execute operations using the user's identity from the request.
145+
Defined in: [appkit/src/plugin/plugin.ts:127](https://github.com/databricks/appkit/blob/main/packages/appkit/src/plugin/plugin.ts#L127)
146146

147-
Returns a scoped instance of this plugin where all method calls
148-
will execute with the user's Databricks credentials instead of
149-
the service principal.
147+
Execute operations using the user's identity from the request.
148+
Returns a proxy of this plugin where all method calls execute
149+
with the user's Databricks credentials instead of the service principal.
150150

151151
#### Parameters
152152

@@ -158,31 +158,12 @@ the service principal.
158158

159159
`this`
160160

161-
A scoped plugin instance that executes as the user
161+
A proxied plugin instance that executes as the user
162162

163163
#### Throws
164164

165165
Error if user token is not available in request headers
166166

167-
#### Example
168-
169-
```typescript
170-
// In route handler - execute query as the requesting user
171-
router.post('/users/me/query/:key', async (req, res) => {
172-
const result = await this.asUser(req).query(req.params.key)
173-
res.json(result)
174-
})
175-
176-
// Mixed execution in same handler
177-
router.post('/dashboard', async (req, res) => {
178-
const [systemData, userData] = await Promise.all([
179-
this.getSystemStats(), // Service principal
180-
this.asUser(req).getUserPreferences(), // User context
181-
])
182-
res.json({ systemData, userData })
183-
})
184-
```
185-
186167
***
187168

188169
### execute()
@@ -194,6 +175,8 @@ protected execute<T>(
194175
userKey?: string): Promise<T | undefined>;
195176
```
196177

178+
Defined in: [appkit/src/plugin/plugin.ts:249](https://github.com/databricks/appkit/blob/main/packages/appkit/src/plugin/plugin.ts#L249)
179+
197180
#### Type Parameters
198181

199182
| Type Parameter |
@@ -224,6 +207,8 @@ protected executeStream<T>(
224207
userKey?: string): Promise<void>;
225208
```
226209

210+
Defined in: [appkit/src/plugin/plugin.ts:187](https://github.com/databricks/appkit/blob/main/packages/appkit/src/plugin/plugin.ts#L187)
211+
227212
#### Type Parameters
228213

229214
| Type Parameter |
@@ -293,6 +278,8 @@ BasePlugin.injectRoutes
293278
protected registerEndpoint(name: string, path: string): void;
294279
```
295280

281+
Defined in: [appkit/src/plugin/plugin.ts:274](https://github.com/databricks/appkit/blob/main/packages/appkit/src/plugin/plugin.ts#L274)
282+
296283
#### Parameters
297284

298285
| Parameter | Type |
@@ -312,6 +299,8 @@ protected registerEndpoint(name: string, path: string): void;
312299
protected route<_TResponse>(router: Router, config: RouteConfig): void;
313300
```
314301

302+
Defined in: [appkit/src/plugin/plugin.ts:278](https://github.com/databricks/appkit/blob/main/packages/appkit/src/plugin/plugin.ts#L278)
303+
315304
#### Type Parameters
316305

317306
| Type Parameter |
@@ -331,6 +320,30 @@ protected route<_TResponse>(router: Router, config: RouteConfig): void;
331320

332321
***
333322

323+
### sdk()
324+
325+
```ts
326+
sdk(): unknown;
327+
```
328+
329+
Defined in: [appkit/src/plugin/plugin.ts:114](https://github.com/databricks/appkit/blob/main/packages/appkit/src/plugin/plugin.ts#L114)
330+
331+
Returns the public SDK for this plugin.
332+
Override this to define a custom public API.
333+
By default, returns an empty object.
334+
335+
#### Returns
336+
337+
`unknown`
338+
339+
#### Implementation of
340+
341+
```ts
342+
BasePlugin.sdk
343+
```
344+
345+
***
346+
334347
### setup()
335348

336349
```ts

docs/docs/api/appkit/Function.createApp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ function createApp<T>(config: {
88
}): Promise<PluginMap<T>>;
99
```
1010

11+
Defined in: [appkit/src/core/appkit.ts:176](https://github.com/databricks/appkit/blob/main/packages/appkit/src/core/appkit.ts#L176)
12+
1113
Bootstraps AppKit with the provided configuration.
1214

1315
## Type Parameters
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Function: getExecutionContext()
2+
3+
```ts
4+
function getExecutionContext(): ExecutionContext;
5+
```
6+
7+
Defined in: [appkit/src/context/execution-context.ts:35](https://github.com/databricks/appkit/blob/main/packages/appkit/src/context/execution-context.ts#L35)
8+
9+
Get the current execution context.
10+
11+
- If running inside a user context (via asUser), returns the user context
12+
- Otherwise, returns the service context
13+
14+
## Returns
15+
16+
`ExecutionContext`
17+
18+
## Throws
19+
20+
Error if ServiceContext is not initialized

docs/docs/api/appkit/Interface.BasePluginConfig.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Interface: BasePluginConfig
22

33
Base configuration interface for AppKit plugins
4+
Defined in: [shared/src/plugin.ts:19](https://github.com/databricks/appkit/blob/main/packages/shared/src/plugin.ts#L19)
45

56
## Indexable
67

@@ -16,6 +17,8 @@ Base configuration interface for AppKit plugins
1617
optional host: string;
1718
```
1819

20+
Defined in: [shared/src/plugin.ts:21](https://github.com/databricks/appkit/blob/main/packages/shared/src/plugin.ts#L21)
21+
1922
***
2023

2124
### name?
@@ -24,10 +27,14 @@ optional host: string;
2427
optional name: string;
2528
```
2629

30+
Defined in: [shared/src/plugin.ts:20](https://github.com/databricks/appkit/blob/main/packages/shared/src/plugin.ts#L20)
31+
2732
***
2833

2934
### telemetry?
3035

3136
```ts
3237
optional telemetry: TelemetryOptions;
3338
```
39+
40+
Defined in: [shared/src/plugin.ts:29](https://github.com/databricks/appkit/blob/main/packages/shared/src/plugin.ts#L29)

docs/docs/api/appkit/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ plugin architecture, and React integration.
4646
| ------ | ------ |
4747
| [appKitTypesPlugin](Function.appKitTypesPlugin.md) | Vite plugin to generate types for AppKit queries. Calls generateFromEntryPoint under the hood. |
4848
| [createApp](Function.createApp.md) | Bootstraps AppKit with the provided configuration. |
49+
| [getExecutionContext](Function.getExecutionContext.md) | Get the current execution context. |
4950
| [isSQLTypeMarker](Function.isSQLTypeMarker.md) | Type guard to check if a value is a SQL type marker |

docs/docs/api/appkit/typedoc-sidebar.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ const typedocSidebar: SidebarsConfig = {
124124
id: "api/appkit/Function.createApp",
125125
label: "createApp"
126126
},
127+
{
128+
type: "doc",
129+
id: "api/appkit/Function.getExecutionContext",
130+
label: "getExecutionContext"
131+
},
127132
{
128133
type: "doc",
129134
id: "api/appkit/Function.isSQLTypeMarker",

docs/docs/plugins.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,25 @@ import type express from "express";
219219

220220
class MyPlugin extends Plugin {
221221
name = "myPlugin";
222-
envVars = []; // list required env vars here
223-
224-
injectRoutes(router: express.Router) {
225-
this.route(router, {
226-
name: "hello",
227-
method: "get",
228-
path: "/hello",
229-
handler: async (_req, res) => {
230-
res.json({ ok: true });
231-
},
232-
});
222+
envVars = ["MY_API_KEY"];
223+
224+
async setup() {
225+
// Initialize your plugin
226+
}
227+
228+
myCustomMethod() {
229+
// Some implementation
230+
}
231+
232+
async shutdown() {
233+
// Clean up resources
234+
}
235+
236+
sdk() {
237+
// an object with the methods from this plugin to expose as SDK
238+
return {
239+
myCustomMethod: this.myCustomMethod
240+
}
233241
}
234242
}
235243

@@ -248,6 +256,23 @@ export const myPlugin = toPlugin<typeof MyPlugin, Record<string, never>, "myPlug
248256
- **Telemetry**: Instrument your plugin with traces and metrics via `this.telemetry`. See [`ITelemetry`](api/appkit/Interface.ITelemetry.md).
249257
- **Execution interceptors**: Use `execute()` and `executeStream()` with [`StreamExecutionSettings`](api/appkit/Interface.StreamExecutionSettings.md)
250258

259+
**Consuming your plugin as an SDK**
260+
261+
Optionally, you may want to provide a way to consume your plugin in an imperative way using the AppKit object.
262+
To do that, your plugin needs to implement the `sdk` method, returning an object with the API methods you want to expose. From the previous example, the plugin could be consumed as follows:
263+
264+
```ts
265+
const AppKit = await createApp({
266+
plugins: [
267+
server({ port: 8000 }),
268+
analytics(),
269+
myPlugin(),
270+
],
271+
});
272+
273+
AppKit.myPlugin.myCustomMethod();
274+
```
275+
251276
See the [`Plugin`](api/appkit/Class.Plugin.md) API reference for complete documentation.
252277

253278
## Caching

0 commit comments

Comments
 (0)