@beseif-solutions/prow-core 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/core.d.ts +21 -0
  2. package/dist/core.js +46 -0
  3. package/dist/entities/category.d.ts +1 -0
  4. package/dist/entities/category.js +20 -0
  5. package/dist/entities/commons.d.ts +26 -0
  6. package/dist/entities/commons.js +15 -0
  7. package/dist/entities/credentials.d.ts +85 -0
  8. package/dist/entities/credentials.js +48 -0
  9. package/dist/entities/events.d.ts +131 -0
  10. package/dist/entities/events.js +82 -0
  11. package/dist/entities/provider.d.ts +12 -0
  12. package/dist/entities/provider.js +25 -0
  13. package/dist/index.d.ts +8 -0
  14. package/dist/index.js +33 -0
  15. package/dist/minified-utils/context.d.ts +6 -0
  16. package/dist/minified-utils/context.js +55 -0
  17. package/dist/minified-utils/env.d.ts +13 -0
  18. package/dist/minified-utils/env.js +96 -0
  19. package/dist/minified-utils/fields.d.ts +128 -0
  20. package/dist/minified-utils/fields.js +458 -0
  21. package/dist/minified-utils/locales.d.ts +27 -0
  22. package/dist/minified-utils/locales.js +88 -0
  23. package/dist/minified-utils/proxy.d.ts +9 -0
  24. package/dist/minified-utils/proxy.js +55 -0
  25. package/dist/minified-utils/types.d.ts +11 -0
  26. package/dist/minified-utils/types.js +2 -0
  27. package/dist/minified-utils/utils.d.ts +1 -0
  28. package/dist/minified-utils/utils.js +26 -0
  29. package/dist/minified-utils/where.d.ts +40 -0
  30. package/dist/minified-utils/where.js +265 -0
  31. package/package.json +44 -0
  32. package/src/core.ts +56 -0
  33. package/src/entities/category.ts +9 -0
  34. package/src/entities/commons.ts +40 -0
  35. package/src/entities/credentials.ts +131 -0
  36. package/src/entities/events.ts +198 -0
  37. package/src/entities/provider.ts +29 -0
  38. package/src/index.ts +8 -0
  39. package/src/minified-utils/context.ts +56 -0
  40. package/src/minified-utils/env.ts +90 -0
  41. package/src/minified-utils/fields.ts +628 -0
  42. package/src/minified-utils/locales.ts +124 -0
  43. package/src/minified-utils/proxy.ts +44 -0
  44. package/src/minified-utils/types.ts +18 -0
  45. package/src/minified-utils/utils.ts +18 -0
  46. package/src/minified-utils/where.ts +330 -0
  47. package/tsconfig.json +21 -0
@@ -0,0 +1,40 @@
1
+ import Joi from "joi";
2
+ import { Core } from "../core";
3
+
4
+ export type CoreConfig = {
5
+ inputs?: Record<string, any>,
6
+ credentials?: Record<string, any>,
7
+ outputs?: Record<string, any>,
8
+ };
9
+
10
+ export type CoreFunction<Config extends CoreConfig = {}> = (core: Core, configuration: ({
11
+ sandbox: boolean,
12
+ } & (
13
+ Config[`inputs`] extends undefined ?
14
+ {} :
15
+ { inputs: Config[`inputs`] }
16
+ )
17
+ & (
18
+ Config[`credentials`] extends undefined ?
19
+ {} :
20
+ { credentials: Config[`credentials`] }
21
+ )
22
+ )) => Promise<Config[`outputs`]>;
23
+
24
+
25
+ type CommonConfig = Pick<CoreConfig, `credentials`>;
26
+
27
+ export type Common<Config extends CommonConfig = {}> = {
28
+ id: string,
29
+ helpers?: Record<string, CoreFunction<{ credentials: Config[`credentials`], inputs: any, outputs: any }>>,
30
+ };
31
+
32
+ export const idSchema = () =>
33
+ Joi.string().required().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/);
34
+
35
+ export const commonSchema = () =>
36
+ Joi.object({
37
+ id: idSchema(),
38
+ helpers: Joi.object()
39
+ .pattern(Joi.string(), Joi.function()),
40
+ });
@@ -0,0 +1,131 @@
1
+ import Joi from "joi";
2
+ import { Field, fieldsSchema } from "../minified-utils/fields";
3
+ import { Common, commonSchema, CoreConfig, CoreFunction } from "./commons";
4
+
5
+ type CredentialsConfig = Pick<CoreConfig, `credentials`>;
6
+
7
+ type CommonCredentials<Config extends CredentialsConfig = {}> = Common<Config> & {
8
+ model: `credentials`,
9
+ sandbox: boolean,
10
+ functions: {
11
+ check: CoreFunction<{
12
+ credentials: Config[`credentials`],
13
+ inputs: undefined,
14
+ outputs: { valid: boolean },
15
+ }>,
16
+ },
17
+ };
18
+
19
+ export type OAuthCredentials<Config extends CredentialsConfig = {}> = CommonCredentials<Config> & {
20
+ type: `oauth`,
21
+ functions: {
22
+ oauth: CoreFunction<{
23
+ credentials: undefined,
24
+ inputs: { redirect: string },
25
+ outputs: {
26
+ action: string,
27
+ method: string,
28
+ body: Record<string, any>,
29
+ },
30
+ }>,
31
+ connect: CoreFunction<{
32
+ credentials: undefined,
33
+ inputs: { redirect: string, code: string },
34
+ outputs: {
35
+ username?: string,
36
+ credentials: Config[`credentials`],
37
+ },
38
+ }>,
39
+ refresh: CoreFunction<{
40
+ credentials: Config[`credentials`],
41
+ inputs: undefined,
42
+ outputs: {
43
+ username?: string,
44
+ credentials: Config[`credentials`],
45
+ },
46
+ }>,
47
+ disconnect: CoreFunction<{
48
+ credentials: Config[`credentials`],
49
+ inputs: undefined,
50
+ outputs: {
51
+ done: true,
52
+ },
53
+ }>,
54
+ },
55
+ };
56
+
57
+ export type SessionCredentials<Config extends CredentialsConfig = {}> = CommonCredentials<Config> & {
58
+ type: `session`,
59
+ fields: Field[],
60
+ functions: {
61
+ connect: CoreFunction<{
62
+ credentials: undefined,
63
+ inputs: { fields: Record<string, any> },
64
+ outputs: {
65
+ username?: string,
66
+ credentials: Config[`credentials`],
67
+ },
68
+ }>,
69
+ // refresh: CoreFunction<{
70
+ // credentials: Config[`credentials`],
71
+ // inputs: undefined,
72
+ // outputs: {
73
+ // username?: string,
74
+ // credentials: Config[`credentials`],
75
+ // },
76
+ // }>,
77
+ },
78
+ };
79
+
80
+ export type StaticCredentials<Config extends CredentialsConfig = {}> = CommonCredentials<Config> & {
81
+ type: `static`,
82
+ fields: Field[],
83
+ };
84
+
85
+ export type Credentials<Config extends CredentialsConfig = {}> = OAuthCredentials<Config> | StaticCredentials<Config> | SessionCredentials<Config>;
86
+
87
+
88
+ const commonCredentialsSchema = () =>
89
+ Joi.object({
90
+ model: Joi.valid(`credentials`).required(),
91
+ sandbox: Joi.bool().required(),
92
+ functions: Joi.object({
93
+ check: Joi.function().required(),
94
+ }).required(),
95
+ });
96
+
97
+ export const oauthCredentialsSchema = () =>
98
+ Joi.object({
99
+ type: Joi.valid(`oauth`).required(),
100
+ functions: Joi.object({
101
+ oauth: Joi.function().required(),
102
+ connect: Joi.function().required(),
103
+ refresh: Joi.function().required(),
104
+ disconnect: Joi.function().required(),
105
+ }).required(),
106
+ })
107
+ .concat(commonCredentialsSchema())
108
+ .concat(commonSchema());
109
+
110
+ export const sessionCredentialsSchema = () =>
111
+ Joi.object({
112
+ type: Joi.valid(`session`).required(),
113
+ functions: Joi.object({
114
+ connect: Joi.function().required(),
115
+ }).required(),
116
+ })
117
+ .concat(commonCredentialsSchema())
118
+ .concat(commonSchema());
119
+
120
+ export const staticCredentialsSchema = () =>
121
+ Joi.object({
122
+ type: Joi.valid(`static`).required(),
123
+ fields: fieldsSchema()
124
+ .required()
125
+ .min(1),
126
+ })
127
+ .concat(commonCredentialsSchema())
128
+ .concat(commonSchema());
129
+
130
+ export const credentialsSchema = () =>
131
+ Joi.alternatives(oauthCredentialsSchema, staticCredentialsSchema, sessionCredentialsSchema);
@@ -0,0 +1,198 @@
1
+ import { Request } from "express";
2
+ import Joi from "joi";
3
+ import { Field, fieldsSchema } from "../minified-utils/fields";
4
+ import { Common, commonSchema, CoreConfig, CoreFunction, idSchema } from "./commons";
5
+
6
+ type CommonConnectableEvent<Keys extends string = string> = {
7
+ key: Keys,
8
+ };
9
+
10
+ type DefaultConnectableEvent<Keys extends string = string> = CommonConnectableEvent<Keys> & {
11
+ default: true,
12
+ };
13
+
14
+ type ErrorConnectableEvent<Keys extends string = string> = CommonConnectableEvent<Keys> & {
15
+ error: true,
16
+ };
17
+
18
+ type AdditionalConnectableEvent<Keys extends string = string> = CommonConnectableEvent<Keys> & {};
19
+
20
+ export type ConnectableEvent<Keys extends string = string> = DefaultConnectableEvent<Keys> | ErrorConnectableEvent<Keys> | AdditionalConnectableEvent<Keys>;
21
+
22
+ type EventConfig = Pick<CoreConfig, `credentials` | `outputs`> & {
23
+ flags?: {
24
+ inputs?: string[],
25
+ outputs: string[],
26
+ },
27
+ };
28
+
29
+ type CommonEvents<Config extends EventConfig = {}> = Common<Config> & {
30
+ model: `event`,
31
+ categories: string[],
32
+ credentials: string[],
33
+ sandbox: boolean,
34
+ fields: Field[],
35
+ examples: EventConfig[`outputs`][],
36
+ };
37
+
38
+ export type ImmediateTrigger<Config extends EventConfig = {}> = CommonEvents<Config> & {
39
+ type: `trigger`,
40
+ immediate: true,
41
+ outputs: [DefaultConnectableEvent<Config[`flags`][`outputs`][number]>],
42
+ functions: {
43
+ mount?: CoreFunction<{
44
+ credentials: Config[`credentials`],
45
+ inputs: { fields: Record<string, any> },
46
+ outputs: { done: boolean },
47
+ }>,
48
+ unmount?: CoreFunction<{
49
+ credentials: Config[`credentials`],
50
+ inputs: { fields: Record<string, any> },
51
+ outputs: { done: boolean },
52
+ }>,
53
+ handle: CoreFunction<{
54
+ credentials: Config[`credentials`],
55
+ inputs: { fields: Record<string, any>, request: Request },
56
+ outputs: { records: Config[`outputs`][], flags: Partial<Record<Config[`flags`][`outputs`][number], true>> },
57
+ }>,
58
+ fire?: CoreFunction<{
59
+ credentials: Config[`credentials`],
60
+ inputs: { fields: Record<string, any> },
61
+ outputs: { done: boolean },
62
+ }>,
63
+ poll?: CoreFunction<{
64
+ credentials: Config[`credentials`],
65
+ inputs: { fields: Record<string, any>, last_schedule?: string, limit?: number },
66
+ outputs: { records: Config[`outputs`][], flags: Partial<Record<Config[`flags`][`outputs`][number], true>> },
67
+ }>,
68
+ },
69
+ };
70
+
71
+ export type ScheduledTrigger<Config extends EventConfig = {}> = CommonEvents<Config> & {
72
+ type: `trigger`,
73
+ immediate: false,
74
+ outputs: [DefaultConnectableEvent<Config[`flags`][`outputs`][number]>],
75
+ functions: {
76
+ poll: CoreFunction<{
77
+ credentials: Config[`credentials`],
78
+ inputs: { fields: Record<string, any>, last_schedule?: string, limit?: number },
79
+ outputs: { records: Config[`outputs`][], flags: Partial<Record<Config[`flags`][`outputs`][number], true>> },
80
+ }>,
81
+ },
82
+ };
83
+
84
+ export type Trigger<Config extends EventConfig = {}> =
85
+ | ImmediateTrigger<Config>
86
+ | ScheduledTrigger<Config>;
87
+
88
+ export type Action<Config extends EventConfig = {}> = CommonEvents<Config> & {
89
+ type: `action`,
90
+ inputs: [DefaultConnectableEvent<Config[`flags`][`inputs`][number]>, ...AdditionalConnectableEvent<Config[`flags`][`inputs`][number]>[]],
91
+ outputs: [DefaultConnectableEvent<Config[`flags`][`outputs`][number]>, ...(AdditionalConnectableEvent<Config[`flags`][`outputs`][number]> | ErrorConnectableEvent<Config[`flags`][`outputs`][number]>)[]],
92
+ functions: {
93
+ execute: CoreFunction<{
94
+ credentials: Config[`credentials`],
95
+ inputs: { fields: Record<string, any>, flags: Partial<Record<Config[`flags`][`inputs`][number], true>> },
96
+ outputs: { output: Config[`outputs`], flags: Partial<Record<Config[`flags`][`outputs`][number], true>> },
97
+ }>,
98
+ },
99
+ };
100
+
101
+ export type Event<Config extends EventConfig = {}> =
102
+ | Action<Config>
103
+ | Trigger<Config>;
104
+
105
+ const commonConnectableEventSchema = () =>
106
+ Joi.object({
107
+ key: Joi.string().required(),
108
+ });
109
+
110
+ const defaultConnectableEventSchema = () =>
111
+ Joi.object({
112
+ default: Joi.valid(true).required(),
113
+ })
114
+ .concat(commonConnectableEventSchema());
115
+
116
+ const errorConnectableEventSchema = () =>
117
+ Joi.object({
118
+ error: Joi.valid(true).required(),
119
+ })
120
+ .concat(commonConnectableEventSchema());
121
+
122
+ const additionalConnectableEventSchema = () =>
123
+ Joi.object({})
124
+ .concat(commonConnectableEventSchema());
125
+
126
+ const commonEventsSchema = () =>
127
+ Joi.object({
128
+ model: Joi.valid(`event`).required(),
129
+ categories: Joi.array().items(Joi.string()).required(),
130
+ credentials: Joi.array().items(idSchema().optional()).required().min(0),
131
+ sandbox: Joi.bool().required(),
132
+ fields: fieldsSchema().required(),
133
+ examples: Joi.array(),
134
+ });
135
+
136
+ export const scheduledTriggerSchema = () =>
137
+ Joi.object({
138
+ type: Joi.valid(`trigger`).required(),
139
+ immediate: Joi.valid(false).required(),
140
+ outputs: Joi.array()
141
+ .length(1)
142
+ .items(defaultConnectableEventSchema())
143
+ .required(),
144
+ functions: Joi.object({
145
+ poll: Joi.function().required(),
146
+ }).required(),
147
+ })
148
+ .concat(commonSchema())
149
+ .concat(commonEventsSchema());
150
+
151
+ export const immediateTriggerSchema = () =>
152
+ Joi.object({
153
+ type: Joi.valid(`trigger`).required(),
154
+ immediate: Joi.valid(true).required(),
155
+ outputs: Joi.array()
156
+ .items(defaultConnectableEventSchema())
157
+ .length(1)
158
+ .required(),
159
+ functions: Joi.object({
160
+ mount: Joi.function(),
161
+ unmount: Joi.function(),
162
+ handle: Joi.function().required(),
163
+ poll: Joi.function(),
164
+ }).required(),
165
+ })
166
+ .concat(commonSchema())
167
+ .concat(commonEventsSchema());
168
+
169
+ export const triggerSchema = () =>
170
+ Joi.alternatives(scheduledTriggerSchema(), immediateTriggerSchema());
171
+
172
+ export const actionSchema = () =>
173
+ Joi.object({
174
+ type: Joi.valid(`action`).required(),
175
+ inputs: Joi.array()
176
+ .items(
177
+ defaultConnectableEventSchema().required(),
178
+ additionalConnectableEventSchema(),
179
+ )
180
+ .min(1)
181
+ .required(),
182
+ outputs: Joi.array()
183
+ .items(
184
+ defaultConnectableEventSchema().required(),
185
+ errorConnectableEventSchema().optional(),
186
+ additionalConnectableEventSchema().optional(),
187
+ )
188
+ .min(1)
189
+ .required(),
190
+ functions: Joi.object({
191
+ execute: Joi.function().required(),
192
+ }).required(),
193
+ })
194
+ .concat(commonSchema())
195
+ .concat(commonEventsSchema());
196
+
197
+ export const eventSchema = () =>
198
+ Joi.alternatives(triggerSchema(), actionSchema());
@@ -0,0 +1,29 @@
1
+ import Joi from "joi";
2
+ import { Credentials, credentialsSchema } from "./credentials";
3
+ import { Action, actionSchema, Trigger, triggerSchema } from "./events";
4
+ import { idSchema } from "./commons";
5
+
6
+ export type Provider = {
7
+ id: string,
8
+ model: `provider`,
9
+ categories: string[],
10
+ credentials: Record<string, Credentials>,
11
+ triggers: Record<string, Trigger>,
12
+ actions: Record<string, Action>,
13
+ };
14
+
15
+ export const providerSchema = () =>
16
+ Joi.object({
17
+ id: idSchema(),
18
+ model: Joi.valid(`provider`).required(),
19
+ categories: Joi.array().items(Joi.string()).required(),
20
+ credentials: Joi.object()
21
+ .pattern(idSchema(), credentialsSchema())
22
+ .required(),
23
+ triggers: Joi.object()
24
+ .pattern(idSchema(), triggerSchema())
25
+ .required(),
26
+ actions: Joi.object()
27
+ .pattern(idSchema(), actionSchema())
28
+ .required(),
29
+ });
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export { Core, core } from './core';
2
+ export { getCategories } from './entities/category';
3
+ export { CoreFunction, idSchema } from './entities/commons';
4
+ export { Credentials, OAuthCredentials, StaticCredentials, SessionCredentials, credentialsSchema, oauthCredentialsSchema, staticCredentialsSchema, sessionCredentialsSchema } from './entities/credentials';
5
+ export { Action, Event, ImmediateTrigger, ScheduledTrigger, Trigger, actionSchema, eventSchema, immediateTriggerSchema, scheduledTriggerSchema, triggerSchema } from './entities/events';
6
+ export { Provider, providerSchema } from './entities/provider';
7
+ export { Field, SelectFieldChoice, ValidationResult, extract, fieldSchema, fieldsSchema, validate } from './minified-utils/fields';
8
+ export { i18n, localizeFields, localizeInputs, localizeOutputs, localizeProperties } from './minified-utils/locales';
@@ -0,0 +1,56 @@
1
+ import Handlebars from "handlebars";
2
+ import _ from "lodash";
3
+
4
+ class Context {
5
+
6
+ constructor() {
7
+
8
+ // logic and math helpers
9
+ Handlebars.registerHelper({
10
+ eq: (v1: any, v2: any) => v1 === v2,
11
+ ne: (v1: any, v2: any) => v1 !== v2,
12
+ lt: (v1: any, v2: any) => v1 < v2,
13
+ gt: (v1: any, v2: any) => v1 > v2,
14
+ lte: (v1: any, v2: any) => v1 <= v2,
15
+ gte: (v1: any, v2: any) => v1 >= v2,
16
+ and: (v1: any, v2: any) => v1 && v2,
17
+ or: (v1: any, v2: any) => v1 || v2,
18
+ "+": (v1: number, v2: number) => v1 + v2,
19
+ "-": (v1: number, v2: number) => v1 - v2,
20
+ "*": (v1: number, v2: number) => v1 * v2,
21
+ "/": (v1: number, v2: number) => v1 / v2,
22
+ "sum": (v1: number, v2: number) => v1 + v2,
23
+ "subtract": (v1: number, v2: number) => v1 - v2,
24
+ "multiply": (v1: number, v2: number) => v1 * v2,
25
+ "divide": (v1: number, v2: number) => v1 / v2,
26
+ "abs": (v1: number) => Math.abs(v1),
27
+ });
28
+
29
+ // TODO: register helpers
30
+
31
+ }
32
+
33
+ public readonly resolve = (source: any, data: Record<string, any>) => {
34
+ let isObject = false;
35
+ let unresolved: string;
36
+
37
+ if (_.isNull(source) || _.isUndefined(source)) { return source; }
38
+
39
+ if (_.isObject(source) || _.isArray(source)) {
40
+ isObject = true;
41
+ unresolved = JSON.stringify(source);
42
+ } else if (typeof source === `string`) {
43
+ unresolved = source;
44
+ } else { return source; }
45
+
46
+ if (!unresolved.includes(`{{`)) { return source; }
47
+
48
+ const template = Handlebars.compile(unresolved);
49
+ const resolved = template(data);
50
+ return isObject ? JSON.parse(resolved) : resolved;
51
+ };
52
+
53
+ }
54
+
55
+ const context = new Context();
56
+ export default context;
@@ -0,0 +1,90 @@
1
+ import { DotenvParseOutput, parse } from "dotenv";
2
+ import { closeSync, openSync, readFileSync, writeSync } from "fs";
3
+ import _ from "lodash";
4
+ import path from "path";
5
+ import { createInterface } from "readline";
6
+
7
+ type ReadOptions = {
8
+ create?: boolean,
9
+ };
10
+
11
+ export class Env {
12
+
13
+ private readonly filepath: string;
14
+ private readonly env: DotenvParseOutput;
15
+
16
+ private readonly getEnv = (flag: `w` | `r`): DotenvParseOutput => {
17
+ try {
18
+ const fd = openSync(this.filepath, flag);
19
+ const buffer = readFileSync(fd);
20
+ const parsed = parse(buffer);
21
+ return parsed;
22
+ } catch (e) { throw e; }
23
+ };
24
+
25
+ constructor(root: string) {
26
+ if (path.isAbsolute(root)) {
27
+ this.filepath = path.resolve(root, `.env`);
28
+ } else {
29
+ const absolute = process.cwd();
30
+ this.filepath = path.resolve(absolute, root, `.env`);
31
+ }
32
+
33
+ try {
34
+ this.env = this.getEnv(`r`);
35
+ } catch (e) {
36
+ if (e?.code === `ENOENT`) {
37
+ this.env = this.getEnv(`w`);
38
+ } else { throw e; }
39
+ }
40
+ }
41
+
42
+ public readonly read = async (name: string, options: ReadOptions = {}): Promise<string> => {
43
+ try {
44
+ let value: string;
45
+ if (!_.has(this.env, name)) {
46
+ if (!options.create) { throw new Error(`Environment variable not found: ${name}`); }
47
+ value = await this.prompt(`${name}: `);
48
+ await this.write(name, value);
49
+ } else {
50
+ value = _.get(this.env, name);
51
+ }
52
+
53
+ return value;
54
+ } catch (e) { throw e; }
55
+ };
56
+
57
+ public readonly write = async (name: string, value: string): Promise<void> => {
58
+ try {
59
+ const fd = openSync(this.filepath, `w`);
60
+
61
+ this.env[name] = value;
62
+
63
+ const arr: string[] = [];
64
+ for (const key of Object.keys(this.env)) {
65
+ arr.push(`${key}=${this.env[key]}`);
66
+ }
67
+
68
+ const buffer = Buffer.from(arr.join(`\n`));
69
+ writeSync(fd, buffer, 0, buffer.length);
70
+ closeSync(fd);
71
+ } catch (e) { throw e; }
72
+ };
73
+
74
+ private readonly prompt = async (text: string): Promise<string> =>
75
+ new Promise(async (resolve, reject) => {
76
+ try {
77
+ const stdin = createInterface({
78
+ input: process.stdin,
79
+ output: process.stdout,
80
+ });
81
+
82
+ stdin.on(`close`, () => { reject(`Aborted`); });
83
+ stdin.question(text, (answer) => {
84
+ resolve(answer);
85
+ stdin.close();
86
+ });
87
+ } catch (e) { reject(e.message || e); }
88
+ });
89
+
90
+ }