Skip to content

Commit cdb97ab

Browse files
committed
more APIs
1 parent fbb9615 commit cdb97ab

File tree

7 files changed

+218
-35
lines changed

7 files changed

+218
-35
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@beekeeperstudio/plugin",
3-
"version": "1.4.0-beta.1",
3+
"version": "1.4.0-beta.2",
44
"description": "A simple TypeScript wrapper to send messages from your Beekeeper Studio plugin to the main app.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/commonTypes.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ export interface QueryResult {
99
rows: Record<string, unknown>[];
1010
}
1111

12+
export interface TableKey {
13+
isComposite: boolean;
14+
toTable: string;
15+
toSchema: string;
16+
toColumn: string | string[];
17+
fromTable: string;
18+
fromSchema: string;
19+
fromColumn: string | string[];
20+
constraintName?: string;
21+
onUpdate?: string;
22+
onDelete?: string;
23+
}
24+
1225
export type WindowEventClass =
1326
| "MouseEvent"
1427
| "KeyboardEvent"
@@ -27,3 +40,4 @@ export type TableFilter = {
2740
op?: 'AND' | 'OR';
2841
}
2942

43+
export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };

src/comms.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import { JsonValue } from "./commonTypes";
2+
import {
3+
BroadcastNotification,
4+
PluginErrorNotification,
5+
ThemeChangedNotification,
6+
ViewLoadedNotification,
7+
WindowEventNotification,
8+
} from "./notificationTypes";
19
import type { PluginRequestPayload } from "./requestTypes";
210
import { generateUUID } from "./utils";
311

@@ -85,8 +93,20 @@ export async function request(raw: any): Promise<any> {
8593
});
8694
}
8795

96+
export function notify<Message extends JsonValue = JsonValue>(
97+
name: BroadcastNotification["name"],
98+
args: BroadcastNotification<Message>["args"],
99+
): void;
100+
export function notify(
101+
name: PluginErrorNotification["name"],
102+
args: PluginErrorNotification["args"],
103+
): void;
104+
export function notify(
105+
name: WindowEventNotification["name"],
106+
args: WindowEventNotification["args"],
107+
): void;
88108
export function notify(name: string, args: any) {
89-
const payload = { name, args }
109+
const payload = { name, args };
90110
if (debugComms) {
91111
const time = new Date().toLocaleTimeString("en-GB");
92112
console.groupCollapsed(`${time} [NOTIFICATION] ${name}`);
@@ -99,7 +119,19 @@ export function notify(name: string, args: any) {
99119

100120
const notificationListeners = new Map<string, ((args: any) => void)[]>();
101121

102-
export async function addNotificationListener(
122+
export function addNotificationListener<Message extends JsonValue = JsonValue>(
123+
name: BroadcastNotification["name"],
124+
handler: (args: BroadcastNotification<Message>["args"]) => void,
125+
): void;
126+
export function addNotificationListener(
127+
name: ViewLoadedNotification["name"],
128+
handler: (args: ViewLoadedNotification["args"]) => void,
129+
): void;
130+
export function addNotificationListener(
131+
name: ThemeChangedNotification["name"],
132+
handler: (args: ThemeChangedNotification["args"]) => void,
133+
): void;
134+
export function addNotificationListener(
103135
name: string,
104136
handler: (args: any) => void,
105137
) {
@@ -108,3 +140,16 @@ export async function addNotificationListener(
108140
}
109141
notificationListeners.get(name)!.push(handler);
110142
}
143+
144+
export function removeNotificationListener(
145+
name: string,
146+
handler: (args: any) => void,
147+
) {
148+
const handlers = notificationListeners.get(name);
149+
if (handlers) {
150+
const index = handlers.indexOf(handler);
151+
if (index > -1) {
152+
handlers.splice(index, 1);
153+
}
154+
}
155+
}

src/index.ts

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import type {
1313
OpenQueryTabRequest,
1414
OpenTableTableTabRequest,
1515
OpenTableStructureTabRequest,
16+
GetTableKeysRequest,
1617
} from "./requestTypes";
1718
import type {
1819
GetTablesResponse,
1920
GetColumnsResponse,
2021
GetConnectionInfoResponse,
21-
GetAllTabsResponse,
2222
RunQueryResponse,
2323
ExpandTableResultResponse,
2424
SetTabTitleResponse,
@@ -30,48 +30,113 @@ import type {
3030
GetEncryptedDataResponse,
3131
SetEncryptedDataResponse,
3232
OpenTabResponse,
33+
GetTableKeysResponse,
34+
GetAppInfoResponse,
35+
CheckForUpdateResponse,
3336
} from "./responseTypes";
3437

38+
/**
39+
* Get a list of tables from the current database.
40+
* @since Beekeeper Studio 5.3.0
41+
**/
3542
export async function getTables(schema?: string): Promise<GetTablesResponse['result']> {
3643
return await request({ name: "getTables", args: { schema } as GetTablesRequest['args'] });
3744
}
3845

46+
/**
47+
* Get a list of columns from a table.
48+
*
49+
* @since Beekeeper Studio 5.3.0
50+
**/
3951
export async function getColumns(table: string, schema?: string): Promise<GetColumnsResponse['result']> {
4052
return await request({ name: "getColumns", args: { table, schema } as GetColumnsRequest['args'] });
4153
}
4254

55+
/** @since Beekeeper Studio 5.4.0 */
56+
export async function getTableKeys(table: string, schema?: string): Promise<GetTableKeysResponse['result']> {
57+
return await request({ name: "getTableKeys", args: { table, schema } as GetTableKeysRequest['args'] });
58+
}
59+
60+
/**
61+
* Get information about the current database connection.
62+
*
63+
* @since Beekeeper Studio 5.3.0
64+
**/
4365
export async function getConnectionInfo(): Promise<GetConnectionInfoResponse['result']> {
4466
return await request({ name: "getConnectionInfo", args: void 0 });
4567
}
4668

47-
export async function getAllTabs(): Promise<GetAllTabsResponse['result']> {
48-
return await request({ name: "getAllTabs", args: void 0 });
69+
/** @since Beekeeper Studio 5.4.0 */
70+
export async function getAppInfo(): Promise<GetAppInfoResponse['result']> {
71+
return await request({ name: "getAppInfo", args: void 0 });
4972
}
5073

74+
/**
75+
* Check if plugin's update is available.
76+
*
77+
* @since Beekeeper Studio 5.4.0
78+
**/
79+
export async function checkForUpdate(): Promise<CheckForUpdateResponse['result']> {
80+
return await request({ name: "checkForUpdate", args: void 0 });
81+
}
82+
83+
/**
84+
* Execute a SQL query against the current database.
85+
*
86+
* WARNING: The query will be executed exactly as provided with no modification
87+
* or sanitization. Always validate and sanitize user input before including it
88+
* in queries to prevent unwanted actions.
89+
*
90+
* @since Beekeeper Studio 5.3.0
91+
**/
5192
export async function runQuery(query: string): Promise<RunQueryResponse['result']> {
5293
return await request({ name: "runQuery", args: { query } as RunQueryRequest['args'] });
5394
}
5495

96+
/**
97+
* Display query results in the bottom table panel (shell-type tabs only).
98+
*
99+
* @since Beekeeper Studio 5.3.0
100+
**/
55101
export async function expandTableResult(results: any[]): Promise<ExpandTableResultResponse['result']> {
56102
return await request({ name: "expandTableResult", args: { results } as ExpandTableResultRequest['args'] });
57103
}
58104

105+
/**
106+
* Set the title of the current plugin tab.
107+
*
108+
* @since Beekeeper Studio 5.3.0
109+
**/
59110
export async function setTabTitle(title: string): Promise<SetTabTitleResponse['result']> {
60111
return await request({ name: "setTabTitle", args: { title } as SetTabTitleRequest['args'] });
61112
}
62113

114+
/**
115+
* Get the current state of your view instance.
116+
*
117+
* @see {@link https://docs.beekeeperstudio.io/plugin_development/plugin-views/#view-state|View State}
118+
* @since Beekeeper Studio 5.3.0
119+
**/
63120
export async function getViewState<T>(): Promise<GetViewStateResponse<T>['result']> {
64121
return await request({ name: "getViewState", args: void 0 });
65122
}
66123

124+
/**
125+
* Set the state of your view instance.
126+
*
127+
* @see {@link https://docs.beekeeperstudio.io/plugin_development/plugin-views/#view-state|View State}
128+
* @since Beekeeper Studio 5.3.0
129+
**/
67130
export async function setViewState<T>(state: T): Promise<SetViewStateResponse['result']> {
68131
return await request({ name: "setViewState", args: { state } as SetViewStateRequest<T>['args'] });
69132
}
70133

134+
/** @since Beekeeper Studio 5.3.0 */
71135
export async function openExternal(link: string): Promise<OpenExternalResponse['result']> {
72136
return await request({ name: "openExternal", args: { link } as OpenExternalRequest['args'] });
73137
}
74138

139+
/** @since Beekeeper Studio 5.3.0 */
75140
export async function getData<T>(key: string = "default"): Promise<GetDataResponse<T>['result']> {
76141
return await request({ name: "getData", args: { key } });
77142
}
@@ -85,6 +150,8 @@ export async function getData<T>(key: string = "default"): Promise<GetDataRespon
85150
*
86151
* // Store with default key (equivalent to setData("default", value))
87152
* await setData({ name: "John" });
153+
*
154+
* @since Beekeeper Studio 5.3.0
88155
*/
89156
export async function setData<T>(key: string, value: T): Promise<SetDataResponse['result']>;
90157
export async function setData<T>(value: T): Promise<SetDataResponse['result']>;
@@ -96,6 +163,7 @@ export async function setData<T>(keyOrValue: string | T, value?: T): Promise<Set
96163
}
97164
}
98165

166+
/** @since Beekeeper Studio 5.3.0 */
99167
export async function getEncryptedData<T>(key: string): Promise<GetEncryptedDataResponse<T>['result']> {
100168
return await request({ name: "getEncryptedData", args: { key } });
101169
}
@@ -109,6 +177,8 @@ export async function getEncryptedData<T>(key: string): Promise<GetEncryptedData
109177
*
110178
* // Store with default key (equivalent to setEncryptedData("default", value))
111179
* await setEncryptedData({ token: "abc123" });
180+
*
181+
* @since Beekeeper Studio 5.3.0
112182
*/
113183
export async function setEncryptedData<T>(key: string, value: T): Promise<SetEncryptedDataResponse['result']>;
114184
export async function setEncryptedData<T>(value: T): Promise<SetEncryptedDataResponse['result']>;
@@ -120,6 +190,7 @@ export async function setEncryptedData<T>(keyOrValue: string | T, value?: T): Pr
120190
}
121191
}
122192

193+
/** @since Beekeeper Studio 5.4.0 */
123194
export async function openTab(type: "query", args?: Omit<OpenQueryTabRequest['args'], 'type'>): Promise<OpenTabResponse>;
124195
export async function openTab(type: "tableTable", args: Omit<OpenTableTableTabRequest['args'], 'type'>): Promise<OpenTabResponse>;
125196
export async function openTab(type: "tableStructure", args: Omit<OpenTableStructureTabRequest['args'], 'type'>): Promise<OpenTabResponse>;

src/notificationTypes.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,57 @@
1-
import { ThemeType, WindowEventInits, WindowEventClass } from "./commonTypes";
1+
import {
2+
ThemeType,
3+
WindowEventInits,
4+
WindowEventClass,
5+
JsonValue,
6+
} from "./commonTypes";
27

3-
export interface ThemeChangedNotification {
8+
export type AppTheme = {
9+
palette: Record<string, string>;
10+
cssString: string;
11+
type: ThemeType;
12+
};
13+
14+
export type ThemeChangedNotification = {
415
name: "themeChanged";
5-
args: {
6-
palette: Record<string, string>;
7-
cssString: string;
8-
type: ThemeType;
9-
};
10-
}
16+
args: AppTheme;
17+
};
1118

12-
export interface WindowEventNotification {
19+
export type WindowEventNotification = {
1320
name: "windowEvent";
1421
args: {
1522
eventType: string;
1623
eventClass: WindowEventClass;
1724
eventInitOptions: WindowEventInits;
1825
};
19-
}
26+
};
2027

21-
export interface PluginErrorNotification {
28+
export type PluginErrorNotification = {
2229
name: "pluginError";
2330
args: {
2431
name?: string;
2532
message?: string;
2633
stack?: string;
2734
};
28-
}
35+
};
36+
37+
export type BroadcastNotification<Message extends JsonValue = JsonValue> = {
38+
name: "broadcast";
39+
args: {
40+
message: Message;
41+
};
42+
};
43+
44+
export type ViewLoadedNotification = {
45+
name: "viewLoaded";
46+
args: {
47+
command: string;
48+
args?: JsonValue;
49+
};
50+
};
2951

3052
export type PluginNotificationData =
3153
| ThemeChangedNotification
3254
| WindowEventNotification
33-
| PluginErrorNotification;
55+
| PluginErrorNotification
56+
| ViewLoadedNotification
57+
| BroadcastNotification;

0 commit comments

Comments
 (0)