@beseif-solutions/prow-core 0.0.29 → 0.0.30

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.
package/dist/index.d.ts CHANGED
@@ -6,5 +6,6 @@ export { Action, actionOutputSchema, actionSchema, Event, eventSchema, Immediate
6
6
  export { Provider, providerSchema } from './entities/provider';
7
7
  export { Source } from './entities/source';
8
8
  export { extract, Field, fieldSchema, fieldsSchema, SelectFieldChoice, validate, ValidationResult } from './minified-utils/fields';
9
- export { i18n, localizeFields, localizeInputs, localizeOutputs, localizeProperties } from './minified-utils/locales';
9
+ export { i18n, localizeFields, localizeInputs, localizeOutputs, localizeProperties, LocalizedField, LocalizedProperties } from './minified-utils/locales';
10
10
  export { Properties } from './minified-utils/properties';
11
+ export { Protocol } from './minified-utils/protocol';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Properties = exports.localizeProperties = exports.localizeOutputs = exports.localizeInputs = exports.localizeFields = exports.i18n = exports.validate = exports.fieldsSchema = exports.fieldSchema = exports.extract = exports.providerSchema = exports.triggerSchema = exports.triggerOutputSchema = exports.scheduledTriggerSchema = exports.immediateTriggerSchema = exports.eventSchema = exports.actionSchema = exports.actionOutputSchema = exports.staticCredentialsSchema = exports.sessionCredentialsSchema = exports.oauthCredentialsSchema = exports.credentialsSchema = exports.idSchema = exports.getCategories = exports.core = void 0;
3
+ exports.Protocol = exports.Properties = exports.localizeProperties = exports.localizeOutputs = exports.localizeInputs = exports.localizeFields = exports.i18n = exports.validate = exports.fieldsSchema = exports.fieldSchema = exports.extract = exports.providerSchema = exports.triggerSchema = exports.triggerOutputSchema = exports.scheduledTriggerSchema = exports.immediateTriggerSchema = exports.eventSchema = exports.actionSchema = exports.actionOutputSchema = exports.staticCredentialsSchema = exports.sessionCredentialsSchema = exports.oauthCredentialsSchema = exports.credentialsSchema = exports.idSchema = exports.getCategories = exports.core = void 0;
4
4
  var core_1 = require("./core");
5
5
  Object.defineProperty(exports, "core", { enumerable: true, get: function () { return core_1.core; } });
6
6
  var category_1 = require("./entities/category");
@@ -35,3 +35,5 @@ Object.defineProperty(exports, "localizeOutputs", { enumerable: true, get: funct
35
35
  Object.defineProperty(exports, "localizeProperties", { enumerable: true, get: function () { return locales_1.localizeProperties; } });
36
36
  var properties_1 = require("./minified-utils/properties");
37
37
  Object.defineProperty(exports, "Properties", { enumerable: true, get: function () { return properties_1.Properties; } });
38
+ var protocol_1 = require("./minified-utils/protocol");
39
+ Object.defineProperty(exports, "Protocol", { enumerable: true, get: function () { return protocol_1.Protocol; } });
@@ -3,11 +3,12 @@ import { Credentials, SessionCredentials, StaticCredentials } from '../entities/
3
3
  import { Action, ConnectableEvent, Trigger } from '../entities/events';
4
4
  import { Field } from './fields';
5
5
  import { Provider } from '../entities/provider';
6
- type LocalizedField = {
6
+ import { Source } from '../entities/source';
7
+ export type LocalizedField = {
7
8
  label: string;
8
9
  help: string;
9
10
  };
10
- type LocalizedProperties = {
11
+ export type LocalizedProperties = {
11
12
  name: string;
12
13
  description: string;
13
14
  };
@@ -31,8 +32,8 @@ export declare const i18n: (options: FromCatalog | FromDirectory) => {
31
32
  t: T;
32
33
  i18n: I18n.I18n;
33
34
  };
34
- export declare const localizeFields: (t: ReturnType<typeof i18n>[`t`], element: Action | Trigger | StaticCredentials | SessionCredentials) => Field<LocalizedField>[];
35
- export declare const localizeProperties: (t: ReturnType<typeof i18n>[`t`], element: Action | Trigger | Credentials | Provider) => LocalizedProperties;
35
+ export declare const localizeFields: (t: ReturnType<typeof i18n>[`t`], element: Action | Trigger | StaticCredentials | SessionCredentials | Source) => Field<LocalizedField>[];
36
+ export declare const localizeProperties: (t: ReturnType<typeof i18n>[`t`], element: Action | Trigger | Credentials | Provider | Source) => LocalizedProperties;
36
37
  export declare const localizeInputs: (t: ReturnType<typeof i18n>[`t`], element: Action) => (ConnectableEvent & LocalizedProperties)[];
37
38
  export declare const localizeOutputs: (t: ReturnType<typeof i18n>[`t`], element: Action | Trigger) => (ConnectableEvent & LocalizedProperties)[];
38
39
  export {};
@@ -0,0 +1,42 @@
1
+ import { Provider } from "../entities/provider";
2
+ export declare class Protocol {
3
+ private readonly provider;
4
+ constructor(provider: Provider);
5
+ evaluate: (req: Protocol.Command) => Promise<any>;
6
+ }
7
+ export declare namespace Protocol {
8
+ type Model = `action` | `trigger` | `credentials`;
9
+ type Invoke = {
10
+ command: `invoke`;
11
+ model: Protocol.Model;
12
+ id: string;
13
+ params: {
14
+ function: string;
15
+ configuration: Record<string, any>;
16
+ };
17
+ context: {
18
+ environment: Record<string, any>;
19
+ localization: {
20
+ locale: string;
21
+ catalog: Record<string, any>;
22
+ };
23
+ };
24
+ };
25
+ type CredentialList = {
26
+ command: `list:credentials`;
27
+ };
28
+ type ActionList = {
29
+ command: `list:actions`;
30
+ };
31
+ type TriggerList = {
32
+ command: `list:triggers`;
33
+ };
34
+ type SourceList = {
35
+ command: `list:sources`;
36
+ };
37
+ type LocaleList = {
38
+ command: `list:locales`;
39
+ locale: string;
40
+ };
41
+ type Command = Invoke | CredentialList | ActionList | TriggerList | SourceList | LocaleList;
42
+ }
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Protocol = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ const provider_1 = require("../entities/provider");
9
+ const locales_1 = require("./locales");
10
+ const core_1 = require("../core");
11
+ const joi_1 = __importDefault(require("joi"));
12
+ const invokeSchema = joi_1.default.object({
13
+ command: joi_1.default.string().valid(`invoke`).required(),
14
+ model: joi_1.default.string().valid(`action`, `trigger`, `credentials`).required(),
15
+ id: joi_1.default.string().required(),
16
+ params: joi_1.default.object({
17
+ function: joi_1.default.string().required(),
18
+ configuration: joi_1.default.object().required(),
19
+ }).required(),
20
+ context: joi_1.default.object({
21
+ environment: joi_1.default.object().required(),
22
+ localization: joi_1.default.object({
23
+ locale: joi_1.default.string().required(),
24
+ catalog: joi_1.default.object().required(),
25
+ }).required(),
26
+ }).required(),
27
+ }).required();
28
+ const credentialListSchema = joi_1.default.object({
29
+ command: joi_1.default.string().valid(`list:credentials`).required(),
30
+ }).required();
31
+ const actionListSchema = joi_1.default.object({
32
+ command: joi_1.default.string().valid(`list:actions`).required(),
33
+ }).required();
34
+ const triggerListSchema = joi_1.default.object({
35
+ command: joi_1.default.string().valid(`list:triggers`).required(),
36
+ }).required();
37
+ const sourceListSchema = joi_1.default.object({
38
+ command: joi_1.default.string().valid(`list:sources`).required(),
39
+ }).required();
40
+ const localeListSchema = joi_1.default.object({
41
+ command: joi_1.default.string().valid(`list:locales`).required(),
42
+ locale: joi_1.default.string().required(),
43
+ }).required();
44
+ const commandSchema = joi_1.default.alternatives(invokeSchema, credentialListSchema, actionListSchema, triggerListSchema, sourceListSchema, localeListSchema);
45
+ class Protocol {
46
+ constructor(provider) {
47
+ this.evaluate = async (req) => {
48
+ try {
49
+ const validation = commandSchema.validate(req);
50
+ if (validation.error) {
51
+ throw new Error(`Invalid request: ${validation.error.message}`);
52
+ }
53
+ let response;
54
+ switch (req.command) {
55
+ case `list:actions`:
56
+ response = Object.values(this.provider.actions || {});
57
+ break;
58
+ case `list:triggers`:
59
+ response = Object.values(this.provider.triggers || {});
60
+ break;
61
+ case `list:credentials`:
62
+ response = Object.values(this.provider.credentials || {});
63
+ break;
64
+ case `list:sources`:
65
+ response = Object.values(this.provider.sources || {});
66
+ break;
67
+ case `list:locales`:
68
+ const i18nInstance = (0, locales_1.i18n)({ directory: `node_modules/prow-hubspot-repository/locales` });
69
+ response = i18nInstance.i18n.getLocales().map((l) => {
70
+ i18nInstance.i18n.setLocale(l);
71
+ return {
72
+ code: l,
73
+ catalog: i18nInstance.i18n.getCatalog(),
74
+ };
75
+ });
76
+ break;
77
+ case `invoke`:
78
+ const mEvent = lodash_1.default.get(this.provider, `${req.model}s.${req.id}`);
79
+ if (!mEvent) {
80
+ return {
81
+ status: 404,
82
+ body: JSON.stringify({ error: `Could not find ${req.model} with ID ${req.id}` }),
83
+ };
84
+ }
85
+ const f = lodash_1.default.get(mEvent, req.params.function);
86
+ if (!f || typeof f !== `function`) {
87
+ throw new Error(`Invalid function ${req.params.function}`);
88
+ }
89
+ const invokeInstance = (0, core_1.core)({
90
+ env: req.context.environment,
91
+ localization: req.context.localization,
92
+ });
93
+ response = await f(invokeInstance, req.params.configuration);
94
+ break;
95
+ default:
96
+ throw new Error(`Invalid command`);
97
+ }
98
+ return response;
99
+ }
100
+ catch (e) {
101
+ throw e;
102
+ }
103
+ };
104
+ const providerValidation = (0, provider_1.providerSchema)().validate(provider);
105
+ if (providerValidation.error) {
106
+ throw new Error(`Invalid provider for protocol`);
107
+ }
108
+ this.provider = provider;
109
+ }
110
+ }
111
+ exports.Protocol = Protocol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "0.0.29",
3
+ "version": "0.0.30",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -17,7 +17,6 @@
17
17
  "author": "",
18
18
  "license": "ISC",
19
19
  "dependencies": {
20
- "@types/express": "^4.17.21",
21
20
  "axios": "^1.7.7",
22
21
  "dotenv": "^16.4.5",
23
22
  "handlebars": "^4.7.8",
package/src/index.ts CHANGED
@@ -6,5 +6,6 @@ export { Action, actionOutputSchema, actionSchema, Event, eventSchema, Immediate
6
6
  export { Provider, providerSchema } from './entities/provider';
7
7
  export { Source } from './entities/source';
8
8
  export { extract, Field, fieldSchema, fieldsSchema, SelectFieldChoice, validate, ValidationResult } from './minified-utils/fields';
9
- export { i18n, localizeFields, localizeInputs, localizeOutputs, localizeProperties } from './minified-utils/locales';
9
+ export { i18n, localizeFields, localizeInputs, localizeOutputs, localizeProperties, LocalizedField, LocalizedProperties } from './minified-utils/locales';
10
10
  export { Properties } from './minified-utils/properties';
11
+ export { Protocol } from './minified-utils/protocol';
@@ -6,13 +6,14 @@ import { Credentials, SessionCredentials, StaticCredentials } from '../entities/
6
6
  import { Action, ConnectableEvent, Trigger } from '../entities/events';
7
7
  import { Field, SelectFieldChoice } from './fields';
8
8
  import { Provider } from '../entities/provider';
9
+ import { Source } from '../entities/source';
9
10
 
10
- type LocalizedField = {
11
+ export type LocalizedField = {
11
12
  label: string,
12
13
  help: string,
13
14
  };
14
15
 
15
- type LocalizedProperties = {
16
+ export type LocalizedProperties = {
16
17
  name: string,
17
18
  description: string,
18
19
  };
@@ -139,7 +140,7 @@ const localizeField = (
139
140
 
140
141
  export const localizeFields = (
141
142
  t: ReturnType<typeof i18n>[`t`],
142
- element: Action | Trigger | StaticCredentials | SessionCredentials,
143
+ element: Action | Trigger | StaticCredentials | SessionCredentials | Source,
143
144
  ): Field<LocalizedField>[] => {
144
145
  const response: Field<LocalizedField>[] = [];
145
146
  if (element[`fields`] && element[`fields`].length > 0) {
@@ -150,7 +151,7 @@ export const localizeFields = (
150
151
 
151
152
  export const localizeProperties = (
152
153
  t: ReturnType<typeof i18n>[`t`],
153
- element: Action | Trigger | Credentials | Provider,
154
+ element: Action | Trigger | Credentials | Provider | Source,
154
155
  ): LocalizedProperties => ({
155
156
  name: t(`${element.id}.name`, { fallback: true }),
156
157
  description: t(`${element.id}.description`),
@@ -0,0 +1,164 @@
1
+ import _ from "lodash";
2
+ import { Provider, providerSchema } from "../entities/provider";
3
+ import { i18n } from "./locales";
4
+ import { core } from "../core";
5
+ import Joi from "joi";
6
+
7
+ const invokeSchema = Joi.object({
8
+ command: Joi.string().valid(`invoke`).required(),
9
+ model: Joi.string().valid(`action`, `trigger`, `credentials`).required(),
10
+ id: Joi.string().required(),
11
+ params: Joi.object({
12
+ function: Joi.string().required(),
13
+ configuration: Joi.object().required(),
14
+ }).required(),
15
+ context: Joi.object({
16
+ environment: Joi.object().required(),
17
+ localization: Joi.object({
18
+ locale: Joi.string().required(),
19
+ catalog: Joi.object().required(),
20
+ }).required(),
21
+ }).required(),
22
+ }).required();
23
+
24
+ const credentialListSchema = Joi.object({
25
+ command: Joi.string().valid(`list:credentials`).required(),
26
+ }).required();
27
+
28
+ const actionListSchema = Joi.object({
29
+ command: Joi.string().valid(`list:actions`).required(),
30
+ }).required();
31
+
32
+ const triggerListSchema = Joi.object({
33
+ command: Joi.string().valid(`list:triggers`).required(),
34
+ }).required();
35
+
36
+ const sourceListSchema = Joi.object({
37
+ command: Joi.string().valid(`list:sources`).required(),
38
+ }).required();
39
+
40
+ const localeListSchema = Joi.object({
41
+ command: Joi.string().valid(`list:locales`).required(),
42
+ locale: Joi.string().required(),
43
+ }).required();
44
+
45
+ const commandSchema = Joi.alternatives(
46
+ invokeSchema,
47
+ credentialListSchema,
48
+ actionListSchema,
49
+ triggerListSchema,
50
+ sourceListSchema,
51
+ localeListSchema,
52
+ );
53
+
54
+ export class Protocol {
55
+
56
+ private readonly provider: Provider;
57
+
58
+ constructor(provider: Provider) {
59
+ const providerValidation = providerSchema().validate(provider);
60
+ if (providerValidation.error) { throw new Error(`Invalid provider for protocol`); }
61
+ this.provider = provider;
62
+ }
63
+
64
+ public evaluate = async (req: Protocol.Command): Promise<any> => {
65
+ try {
66
+ const validation = commandSchema.validate(req);
67
+ if (validation.error) { throw new Error(`Invalid request: ${validation.error.message}`); }
68
+
69
+ let response: any;
70
+ switch (req.command) {
71
+ case `list:actions`:
72
+ response = Object.values(this.provider.actions || {});
73
+ break;
74
+ case `list:triggers`:
75
+ response = Object.values(this.provider.triggers || {});
76
+ break;
77
+ case `list:credentials`:
78
+ response = Object.values(this.provider.credentials || {});
79
+ break;
80
+ case `list:sources`:
81
+ response = Object.values(this.provider.sources || {});
82
+ break;
83
+ case `list:locales`:
84
+ const i18nInstance = i18n({ directory: `node_modules/prow-hubspot-repository/locales` });
85
+ response = i18nInstance.i18n.getLocales().map((l) => {
86
+ i18nInstance.i18n.setLocale(l);
87
+ return {
88
+ code: l,
89
+ catalog: i18nInstance.i18n.getCatalog(),
90
+ };
91
+ });
92
+ break;
93
+ case `invoke`:
94
+ const mEvent = _.get(this.provider, `${req.model}s.${req.id}`);
95
+ if (!mEvent) {
96
+ return {
97
+ status: 404,
98
+ body: JSON.stringify({ error: `Could not find ${req.model} with ID ${req.id}` }),
99
+ };
100
+ }
101
+
102
+ const f = _.get(mEvent, req.params.function);
103
+ if (!f || typeof f !== `function`) { throw new Error(`Invalid function ${req.params.function}`); }
104
+
105
+ const invokeInstance = core({
106
+ env: req.context.environment,
107
+ localization: req.context.localization,
108
+ });
109
+
110
+ response = await f(invokeInstance, req.params.configuration);
111
+ break;
112
+ default:
113
+ throw new Error(`Invalid command`);
114
+ }
115
+
116
+ return response;
117
+ } catch (e) { throw e; }
118
+ };
119
+ }
120
+
121
+ export namespace Protocol {
122
+
123
+ export type Model = `action` | `trigger` | `credentials`;
124
+
125
+ export type Invoke = {
126
+ command: `invoke`,
127
+ model: Protocol.Model,
128
+ id: string,
129
+ params: {
130
+ function: string,
131
+ configuration: Record<string, any>,
132
+ },
133
+ context: {
134
+ environment: Record<string, any>,
135
+ localization: {
136
+ locale: string,
137
+ catalog: Record<string, any>,
138
+ },
139
+ },
140
+ };
141
+
142
+ export type CredentialList = {
143
+ command: `list:credentials`,
144
+ };
145
+
146
+ export type ActionList = {
147
+ command: `list:actions`,
148
+ };
149
+
150
+ export type TriggerList = {
151
+ command: `list:triggers`,
152
+ };
153
+
154
+ export type SourceList = {
155
+ command: `list:sources`,
156
+ };
157
+
158
+ export type LocaleList = {
159
+ command: `list:locales`,
160
+ locale: string,
161
+ };
162
+
163
+ export type Command = Invoke | CredentialList | ActionList | TriggerList | SourceList | LocaleList;
164
+ }