forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
371 lines (333 loc) · 9.52 KB
/
index.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
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
export type JSONValue =
| string
| number
| boolean
| null
| JSONValue[]
| { [key: string]: JSONValue; };
export type SendPayload = any;
export interface SendConfigHTTPKv {
[key: string]: string;
}
export interface SendConfigHTTPAuth {
username: string;
password: string;
}
export type UppercaseHTTPMethod =
| "GET"
| "HEAD"
| "POST"
| "PUT"
| "DELETE"
| "CONNECT"
| "OPTIONS"
| "TRACE"
| "PATCH";
export interface SendConfigHTTP {
method?: UppercaseHTTPMethod;
url: string;
headers?: SendConfigHTTPKv;
params?: SendConfigHTTPKv;
auth?: SendConfigHTTPAuth;
data?: SendPayload;
}
export interface SendConfigS3 {
bucket: string;
prefix: string;
payload: SendPayload;
}
export interface SendConfigEmail {
subject: string;
text?: string;
html?: string;
}
export interface SendConfigEmit {
raw_event: SendPayload;
}
export interface SendConfigSSE {
channel: string;
payload: SendPayload;
}
export interface SendFunctionsWrapper {
http: (config: SendConfigHTTP) => void;
email: (config: SendConfigEmail) => void;
emit: (config: SendConfigEmit) => void;
s3: (config: SendConfigS3) => void;
sse: (config: SendConfigSSE) => void;
}
/**
* Http Response.
*/
export interface HTTPResponse {
/**
* HTTP Status
*/
status: number;
/**
* Http Body
*/
body: string | Buffer | NodeJS.ReadableStream;
/**
* If true, issue the response when the promise returned is resolved, otherwise issue
* the response at the end of the workflow execution
*/
immediate?: boolean;
}
export interface Methods {
[key: string]: (...args: any) => unknown;
}
export interface FlowFunctions {
exit: (reason: string) => void;
delay: (ms: number, context: object) => {
resume_url: string;
cancel_url: string;
};
rerun: (ms: number, context: object) => {
resume_url: string;
cancel_url: string;
};
suspend: (ms: number, context: object) => {
resume_url: string;
cancel_url: string;
};
refreshTimeout: () => string;
}
export interface IApi {
open(path: string): IFile;
openDescriptor(descriptor: any): IFile;
dir(path?: string): AsyncGenerator<{
isDirectory: () => boolean;
isFile: () => boolean;
path: string;
name: string;
size?: number;
modifiedAt?: Date;
file?: IFile;
}>;
}
export interface IFile {
delete(): Promise<void>;
createReadStream(): Promise<NodeJS.ReadableStream>;
createWriteStream(contentType?: string, contentLength?: number): Promise<NodeJS.WritableStream>;
toEncodedString(encoding?: string, start?: number, end?: number): Promise<string>;
toUrl(): Promise<string>;
toFile(localFilePath: string): Promise<void>;
toBuffer(): Promise<Buffer>;
fromReadableStream(readableStream: NodeJS.ReadableStream, contentType?: string, contentSize?: number): Promise<IFile>;
fromFile(localFilePath: string, contentType?: string): Promise<IFile>;
fromUrl(url: string, options?: any): Promise<IFile>;
toJSON(): any;
}
export interface Pipedream {
export: (key: string, value: JSONValue) => void;
send: SendFunctionsWrapper;
/**
* Respond to an HTTP interface.
* @param response Define the status and body of the request.
* @returns A promise that is fulfilled when the body is read or an immediate response is issued
*/
respond: (response: HTTPResponse) => Promise<any> | void;
flow: FlowFunctions;
files: IApi;
}
// https://pipedream.com/docs/components/api/#async-options-example
export interface OptionsMethodArgs {
page?: number;
prevContext?: any; // XXX could be typed using context from OptionalOptsFn ReturnValue?
[key: string]: any; // XXX properties in the return value of OptionalOptsFn can be included. Strictly type this instead?
}
// https://pipedream.com/docs/components/api/#referencing-values-from-previous-props
export interface OptionalOptsFn {
(configuredProps: { [key: string]: any; }): object; // XXX strictly type configuredProps
}
export type PropDefinition =
[App<Methods, AppPropDefinitions>, string] |
[App<Methods, AppPropDefinitions>, string, OptionalOptsFn];
// https://pipedream.com/docs/components/api/#prop-definitions-example
export interface PropDefinitionReference {
propDefinition: PropDefinition;
}
// https://pipedream.com/docs/components/api/#app-props
// See https://www.typescriptlang.org/docs/handbook/utility-types.html#thistypetype
// for more information on this technique
export interface App<
Methods,
AppPropDefinitions
> {
type: "app";
app: string;
propDefinitions?: AppPropDefinitions;
methods?: Methods & ThisType<Methods & AppPropDefinitions>;
}
export function defineApp<
Methods,
AppPropDefinitions,
>
(app: App<Methods, AppPropDefinitions>): App<Methods, AppPropDefinitions> {
return app;
}
// Props
export interface DefaultConfig {
intervalSeconds?: number;
cron?: string;
}
export interface Field {
name: string;
value: string;
}
export interface HttpAuth {
type?: "basic" | "bearer" | "none";
username?: string;
password?: string;
token?: string;
}
export interface HttpBody {
type?: "fields" | "raw";
contentType?: string;
fields?: Field[];
mode?: "fields" | "raw";
raw?: string;
}
export interface DefaultHttpRequestPropConfig {
auth?: HttpAuth;
body?: HttpBody;
headers?: Field[];
params?: Field[];
tab?: string;
method?: string;
url?: string;
}
export interface BasePropInterface {
label?: string;
description?: string;
}
export type PropOptions = any[] | Array<{ [key: string]: string; }>;
// https://pipedream.com/docs/components/api/#user-input-props
export interface UserProp extends BasePropInterface {
type: "boolean" | "boolean[]" | "integer" | "integer[]" | "string" | "string[]" | "object" | "any";
options?: PropOptions | ((this: any, opts: OptionsMethodArgs) => Promise<PropOptions>);
optional?: boolean;
default?: JSONValue;
secret?: boolean;
min?: number;
max?: number;
}
// https://pipedream.com/docs/components/api/#interface-props
export interface InterfaceProp extends BasePropInterface {
type: "$.interface.http" | "$.interface.timer";
default?: string | DefaultConfig;
}
// https://pipedream.com/docs/components/api/#db
export interface ServiceDBProp extends BasePropInterface {
type: "$.service.db";
}
// https://pipedream.com/docs/code/nodejs/using-data-stores/#using-the-data-store
export interface DataStoreProp extends BasePropInterface {
type: "data_store";
}
export interface HttpRequestProp extends BasePropInterface {
type: "http_request";
default?: DefaultHttpRequestPropConfig;
}
export interface SourcePropDefinitions {
[name: string]: PropDefinitionReference | App<Methods, AppPropDefinitions> | UserProp | InterfaceProp | ServiceDBProp | HttpRequestProp;
}
export interface ActionPropDefinitions {
[name: string]: PropDefinitionReference | App<Methods, AppPropDefinitions> | UserProp | DataStoreProp | HttpRequestProp;
}
export interface AppPropDefinitions {
[name: string]: PropDefinitionReference | App<Methods, AppPropDefinitions> | UserProp;
}
export interface Hooks {
deploy?: () => Promise<void>;
activate?: () => Promise<void>;
deactivate?: () => Promise<void>;
}
// https://pipedream.com/docs/components/api/#http-event-shape
export type SourceHttpRunOptions = {
method: string;
path: string;
query: {
[key: string]: string;
};
headers: {
[key: string]: string;
};
bodyRaw?: string;
body?: {
[key: string]: JSONValue;
};
};
// https://pipedream.com/docs/components/api/#timer
export type SourceTimerRunOptions = {
timestamp: number;
interval_seconds: number;
};
export type SourceRunOptions = SourceHttpRunOptions | SourceTimerRunOptions;
export interface ActionRunOptions {
$: Pipedream;
steps: JSONValue;
}
// https://pipedream.com/docs/components/api/#run
export interface EmitMetadata {
id?: string | number;
name?: string;
summary?: string;
ts?: number;
}
type EmitFunction = {
$emit: (event: JSONValue, metadata?: EmitMetadata) => Promise<void>;
};
type PropThis<Props> = {
[Prop in keyof Props]: Props[Prop] extends App<Methods, AppPropDefinitions> ? any : any
};
export interface Source<
Methods,
SourcePropDefinitions
> {
key: string;
name?: string;
description?: string;
version: string;
type: "source";
methods?: Methods & ThisType<PropThis<SourcePropDefinitions> & Methods & EmitFunction>;
hooks?: Hooks & ThisType<PropThis<SourcePropDefinitions> & Methods & EmitFunction>;
props?: SourcePropDefinitions;
dedupe?: "last" | "greatest" | "unique";
additionalProps?: (
previousPropDefs: SourcePropDefinitions
) => Promise<SourcePropDefinitions>;
run: (this: PropThis<SourcePropDefinitions> & Methods & EmitFunction, options?: SourceRunOptions) => void | Promise<void>;
}
export function defineSource<
Methods,
SourcePropDefinitions,
>
(component: Source<Methods, SourcePropDefinitions>): Source<Methods, SourcePropDefinitions> {
return component;
}
export interface Action<
Methods,
ActionPropDefinitions
> {
key: string;
name?: string;
description?: string;
version: string;
type: "action";
methods?: Methods & ThisType<PropThis<ActionPropDefinitions> & Methods>;
props?: ActionPropDefinitions;
additionalProps?: (
previousPropDefs: ActionPropDefinitions
) => Promise<ActionPropDefinitions>;
run: (this: PropThis<ActionPropDefinitions> & Methods, options?: ActionRunOptions) => any;
}
export function defineAction<
Methods,
ActionPropDefinitions,
>
(component: Action<Methods, ActionPropDefinitions>): Action<Methods, ActionPropDefinitions> {
return component;
}