-
Notifications
You must be signed in to change notification settings - Fork 22
/
core.ts
379 lines (302 loc) · 9.78 KB
/
core.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright (C) 2020 Pixie Brix, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { JSONSchema7, JSONSchema7Definition } from "json-schema";
import { UiSchema as StandardUiSchema } from "@rjsf/core";
import { AxiosRequestConfig } from "axios";
import { Primitive } from "type-fest";
import { ErrorObject } from "serialize-error";
import { Permissions } from "webextension-polyfill-ts";
import { pick } from "lodash";
export type TemplateEngine = "mustache" | "nunjucks" | "handlebars";
export type Schema = JSONSchema7;
export type UiSchema = StandardUiSchema;
export type SchemaDefinition = JSONSchema7Definition;
export type SchemaProperties = { [key: string]: SchemaDefinition };
export type RenderedHTML = string;
export interface Message {
type: string;
payload?: unknown;
}
export interface MessageContext {
readonly deploymentId?: string;
readonly extensionPointId?: string;
readonly blockId?: string;
readonly extensionId?: string;
readonly serviceId?: string;
readonly authId?: string;
}
export type SerializedError = Primitive | ErrorObject;
export interface Logger {
readonly context: MessageContext;
/**
* Return a child logger with additional message context
*/
childLogger: (additionalContext: MessageContext) => Logger;
trace: (msg: string, data?: Record<string, unknown>) => void;
warn: (msg: string, data?: Record<string, unknown>) => void;
debug: (msg: string, data?: Record<string, unknown>) => void;
log: (msg: string, data?: Record<string, unknown>) => void;
info: (msg: string, data?: Record<string, unknown>) => void;
error: (
error: SerializedError | Error,
data?: Record<string, unknown>
) => void;
}
export type ReaderRoot = HTMLElement | Document;
export interface BlockOptions {
// Using "any" for now so that blocks don't have to assert/cast all their argument types. We're checking
// the inputs using yup/jsonschema, so the types should match what's expected.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ctxt: { [key: string]: any };
logger: Logger;
root: ReaderRoot;
headless?: boolean;
}
// Using "any" for now so that blocks don't have to assert/cast all their argument types. We're checking
// the inputs using jsonschema, so the types should match what's expected.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type BlockArg = { [key: string]: any };
export interface IOption {
value: string | number | boolean;
label: string;
}
export type BlockIcon = string;
/**
* Metadata about a block, extension point, or service
*/
export interface Metadata {
id: string;
name: string;
version?: string;
description?: string;
icon?: BlockIcon;
author?: string;
}
export function selectMetadata(metadata: Metadata): Metadata {
return pick(metadata, ["id", "name", "version", "description"]);
}
// Using "any" for now so that blocks don't have to assert/cast all their argument types. We're checking
// the inputs using jsonschema, so the types should match what's expected.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type BaseExtensionConfig = Record<string, any>;
export interface ServiceDependency {
id: string;
outputKey: string;
config?: string;
}
export type ServiceLocator = (
serviceId: string,
id?: string
) => Promise<SanitizedServiceConfiguration>;
export interface DeploymentContext {
id: string;
timestamp: string;
}
export interface IExtension<
T extends BaseExtensionConfig = BaseExtensionConfig
> {
id: string;
extensionPointId: string;
_deployment?: DeploymentContext;
label?: string;
/**
* Default template engine when running the extension
*/
templateEngine?: TemplateEngine;
/**
* Additional permissions, e.g., origins to perform effects on after opening a tab.
*/
permissions?: Permissions.Permissions;
/**
* Configured services/integrations for the extension
*/
services: ServiceDependency[];
/**
* Options the end-user has configured (i.e., during blueprint activation)
*/
optionsArgs?: OptionsArgs;
config: T;
}
export interface IExtensionPoint extends Metadata {
inputSchema: Schema;
permissions: Permissions.Permissions;
defaultOptions: { [key: string]: unknown };
defaultReader: () => Promise<IReader>;
isAvailable: () => Promise<boolean>;
/**
* true iff the extension point must be installed before the page can be considered ready
*/
syncInstall: boolean;
install(): Promise<boolean>;
/**
* Remove the extension point and installed extensions from the page.
*/
uninstall(options?: { global?: boolean }): void;
/**
* Register an extension with the extension point. Does not actually install/run the extension.
*/
addExtension(extension: IExtension): void;
/**
* Sync registered extensions, removing any extensions that aren't provided here. Does not actually install/run
* the extensions.
*/
syncExtensions(extensions: IExtension[]): void;
run(): Promise<void>;
/**
* Returns any blocks configured in extension.
*/
getBlocks: (extension: IExtension) => Promise<IBlock[]>;
}
export interface IBlock extends Metadata {
/** A JSON schema of the inputs for the block */
inputSchema: Schema;
/** An optional a JSON schema for the output of the block */
outputSchema?: Schema;
defaultOptions: { [key: string]: unknown };
/**
* Returns the optional permissions required to run this block
* https://developer.chrome.com/extensions/permission_warnings
*/
permissions: Permissions.Permissions;
run: (value: BlockArg, options: BlockOptions) => Promise<unknown>;
}
export interface ReaderOutput {
[key: string]: unknown;
}
/**
* A block that can read data from a page or part of the page.
*/
export interface IReader extends IBlock {
/** Return true if the Reader is for a page/element. */
isAvailable: ($elt?: JQuery) => Promise<boolean>;
read: (root: ReaderRoot) => Promise<ReaderOutput>;
}
type ServiceId = string;
export interface KeyedConfig {
[key: string]: string | null;
}
export interface SanitizedConfig extends KeyedConfig {
// nominal typing to distinguish from ServiceConfig
_sanitizedConfigBrand: null;
}
export interface ServiceConfig extends KeyedConfig {
// nominal typing to distinguish from SanitizedConfig
_serviceConfigBrand: null;
}
export interface AuthData {
// nominal typing to distinguish from SanitizedConfig and ServiceConfig
_oauth: null;
[key: string]: string | null;
}
export interface TokenContext {
url: string;
data: Record<string, unknown>;
}
export interface OAuth2Context {
host?: string;
authorizeUrl?: string;
tokenUrl?: string;
client_id: string;
client_secret?: string;
code_challenge_method?: "S256";
}
/** Service configuration provided by a user. */
export interface RawServiceConfiguration {
// nominal typing to distinguish from SanitizedServiceConfiguration
_rawServiceConfigurationBrand: null;
/**
* UUID of the service configuration
*/
id: string | undefined;
serviceId: ServiceId;
label: string | undefined;
/**
* Configuration including all data
*/
config: ServiceConfig;
}
export interface SanitizedServiceConfiguration {
// nominal typing to distinguish from RawServiceConfiguration
_sanitizedServiceConfigurationBrand: null;
/**
* UUID of the service configuration
*/
id?: string;
serviceId: ServiceId;
/**
* Sanitized configuration, i.e., excluding secrets and keys.
*/
config: SanitizedConfig;
/**
* true if the service must be proxied for remote configs, e.g., because it has a secret it needs
* to use to authenticate.
*/
proxy: boolean;
}
/**
* A service that can be dependency injected and used to authenticate external requests.
*
* The input/output schema is the same since it's directly user configured.
*/
export interface IService<
TConfig extends KeyedConfig = KeyedConfig,
TSanitized = TConfig & { _sanitizedConfigBrand: null },
TSecret = TConfig & { _serviceConfigBrand: null },
TOAuth extends AuthData = AuthData
> extends Metadata {
schema: Schema;
isOAuth2: boolean;
isToken: boolean;
getOrigins: (serviceConfig: TSanitized) => string[];
getOAuth2Context: (serviceConfig: TSecret) => OAuth2Context;
getTokenContext: (serviceConfig: TSecret) => TokenContext;
authenticateRequest: (
serviceConfig: TSecret,
requestConfig: AxiosRequestConfig,
oauthConfig?: TOAuth
) => AxiosRequestConfig;
}
export type IconLibrary = "bootstrap" | "simple-icons" | "custom";
export interface IconConfig {
id: string;
library?: IconLibrary;
size?: number;
color?: string;
}
export interface OptionsArgs {
[prop: string]: Primitive;
}
export interface RenderedArgs {
// FIXME: enforcing nominal typing will require helper methods to product the RenderedArgs
// _renderedArgsBrand: null;
[prop: string]: unknown;
}
export interface OrganizationAuthState {
readonly id: string;
readonly name?: string;
readonly scope?: string;
}
export interface AuthState {
readonly userId?: string | null;
readonly email?: string | null;
readonly scope?: string | null;
readonly isLoggedIn: boolean;
readonly isOnboarded: boolean;
readonly extension: boolean;
readonly organization?: OrganizationAuthState | null;
readonly flags: string[];
}