-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
plugin.ts
158 lines (132 loc) · 3.75 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Defines the repository of the plugin.
* Used to know where to get the plugin from.
*/
export enum PluginSource {
NPM = 'npm',
GITHUB = 'github',
URL = 'url',
}
/**
* Specifies the type of the plugin.
* Determines how the plugin would interact with Altair.
*/
export enum PluginType {
HEADER = 'header',
SIDEBAR = 'sidebar',
ACTION_BUTTON = 'action_button',
}
/**
* Specifies the capabilities (functionalities) available to the plugin.
* In the future, this would be used to request the necessary permissions from the user.
*/
export type PluginCapabilities =
| 'query:read'
| 'query:write'
| 'header:read'
| 'header:write'
| 'environment:read'
| 'environment:write'
;
export interface PluginSidebarOptions {
element_name: string;
icon: string;
}
export enum PluginTypeActionButtonLocation {
RESULT_PANE = 'result_pane',
}
export interface PluginTypeActionButtonOptions {
class_name: string;
location: PluginTypeActionButtonLocation;
}
/**
* Plugin Manifest Structure
*/
export interface PluginManifest {
// Version of manifest. Should be 1 for now.
manifest_version: number;
name: string;
display_name: string;
version: string;
description: string;
author_email?: string;
author?: string;
type: PluginType;
sidebar_opts?: PluginSidebarOptions;
action_button_opts?: PluginTypeActionButtonOptions;
scripts: string[];
styles?: string[];
// Plugin capabilities
capabilities?: PluginCapabilities[];
}
export interface PluginInstance {
name: string;
display_name: string;
type: PluginType;
capabilities: PluginCapabilities[];
sidebar_opts?: PluginSidebarOptions;
isActive: boolean;
manifest: PluginManifest;
}
export interface PluginRegistryMap {
[s: string]: PluginInstance;
}
export interface GetPluginOption {
pluginSource?: PluginSource;
version?: string;
[key: string]: any;
}
export interface PluginComponentDataProps {
ctx: PluginComponentDataContext;
// SDL representing GraphQL schema for the current window
sdl?: string;
// Query for the current window
query?: string;
// Variables for the current window
variables?: string;
}
export interface PluginComponentDataContext {
// Sets the query in the current window
setQuery?: (...args: any) => void;
getQuery?: (...args: any) => string;
setVariables?: (...args: any) => void;
getVariables?: (...args: any) => string;
setEndpoint?: (...args: any) => void;
getEndpoint?: (...args: any) => string;
getSDL?: (...args: any) => string;
createWindow?: (...args: any) => void;
on: (...args: any) => void;
}
export interface PluginComponentData extends PluginInstance {
props: PluginComponentDataProps;
}
export interface PluginElement extends HTMLElement {
props?: PluginComponentDataProps;
}
export class AltairPlugin implements PluginInstance {
type = PluginType.SIDEBAR;
sidebar_opts?: PluginSidebarOptions;
isActive = false;
display_name = '';
capabilities: PluginCapabilities[] = [ 'query:read', 'query:write' ];
constructor(public name: string, public manifest: PluginManifest) {
this.sidebar_opts = manifest.sidebar_opts;
this.type = manifest.type;
this.display_name = manifest.display_name || name;
this.capabilities = Array.from(new Set([ ...(manifest.capabilities || []), ...this.capabilities ]));
}
}
export const isAppLevelPluginType = (pluginType: PluginType) => {
return [ PluginType.HEADER ].includes(pluginType);
}
export interface ActionPluginRenderOutput {
pluginName: string;
text: string;
instance?: ActionPlugin;
}
export interface ActionPlugin {
new(props: PluginComponentDataProps): ActionPlugin;
render(props: PluginComponentDataProps): Promise<ActionPluginRenderOutput>;
execute(props: PluginComponentDataProps): Promise<void>;
destroy?(): Promise<void>;
}