@beseif-solutions/prow-core 0.0.31 → 0.0.33

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.
@@ -1,11 +1,12 @@
1
1
  import { Provider } from "../entities/provider";
2
2
  export declare class Protocol {
3
3
  private readonly provider;
4
- constructor(provider: Provider);
4
+ private readonly path;
5
+ constructor(provider: Provider, path: string);
5
6
  evaluate: (req: Protocol.Command) => Promise<any>;
6
7
  }
7
8
  export declare namespace Protocol {
8
- type Model = `action` | `trigger` | `credentials`;
9
+ type Model = `action` | `trigger` | `credential`;
9
10
  type Invoke = {
10
11
  command: `invoke`;
11
12
  model: Protocol.Model;
@@ -4,14 +4,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Protocol = void 0;
7
+ const joi_1 = __importDefault(require("joi"));
7
8
  const lodash_1 = __importDefault(require("lodash"));
9
+ const core_1 = require("../core");
8
10
  const provider_1 = require("../entities/provider");
9
11
  const locales_1 = require("./locales");
10
- const core_1 = require("../core");
11
- const joi_1 = __importDefault(require("joi"));
12
12
  const invokeSchema = joi_1.default.object({
13
13
  command: joi_1.default.string().valid(`invoke`).required(),
14
- model: joi_1.default.string().valid(`action`, `trigger`, `credentials`).required(),
14
+ model: joi_1.default.string().valid(`action`, `trigger`, `credential`).required(),
15
15
  id: joi_1.default.string().required(),
16
16
  params: joi_1.default.object({
17
17
  function: joi_1.default.string().required(),
@@ -42,29 +42,34 @@ const localeListSchema = joi_1.default.object({
42
42
  }).required();
43
43
  const commandSchema = joi_1.default.alternatives(invokeSchema, credentialListSchema, actionListSchema, triggerListSchema, sourceListSchema, localeListSchema);
44
44
  class Protocol {
45
- constructor(provider) {
45
+ constructor(provider, path) {
46
46
  this.evaluate = async (req) => {
47
47
  try {
48
48
  const validation = commandSchema.validate(req);
49
49
  if (validation.error) {
50
50
  throw new Error(`Invalid request: ${validation.error.message}`);
51
51
  }
52
+ const serialize = (elem) => ({
53
+ ...lodash_1.default.omit(elem, [`functions`, `helpers`]),
54
+ functions: Object.keys(elem.functions),
55
+ helpers: Object.keys(elem.helpers),
56
+ });
52
57
  let response;
53
58
  switch (req.command) {
54
59
  case `list:actions`:
55
- response = Object.values(this.provider.actions || {});
60
+ response = Object.values(this.provider.actions || {}).map(serialize);
56
61
  break;
57
62
  case `list:triggers`:
58
- response = Object.values(this.provider.triggers || {});
63
+ response = Object.values(this.provider.triggers || {}).map(serialize);
59
64
  break;
60
65
  case `list:credentials`:
61
- response = Object.values(this.provider.credentials || {});
66
+ response = Object.values(this.provider.credentials || {}).map(serialize);
62
67
  break;
63
68
  case `list:sources`:
64
69
  response = Object.values(this.provider.sources || {});
65
70
  break;
66
71
  case `list:locales`:
67
- const i18nInstance = (0, locales_1.i18n)({ directory: `node_modules/prow-hubspot-repository/locales` });
72
+ const i18nInstance = (0, locales_1.i18n)({ directory: `${this.path}/locales` });
68
73
  response = i18nInstance.i18n.getLocales().map((l) => {
69
74
  i18nInstance.i18n.setLocale(l);
70
75
  return {
@@ -74,22 +79,20 @@ class Protocol {
74
79
  });
75
80
  break;
76
81
  case `invoke`:
77
- const mEvent = lodash_1.default.get(this.provider, `${req.model}s.${req.id}`);
78
- if (!mEvent) {
79
- return {
80
- status: 404,
81
- body: JSON.stringify({ error: `Could not find ${req.model} with ID ${req.id}` }),
82
- };
82
+ const elem = lodash_1.default.get(this.provider, `${req.model}s.${req.id}`);
83
+ if (!elem) {
84
+ throw new Error(`Could not find ${req.model} with ID ${req.id}`);
83
85
  }
84
- const f = lodash_1.default.get(mEvent, req.params.function);
85
- if (!f || typeof f !== `function`) {
86
+ const func = lodash_1.default.get(elem, req.params.function);
87
+ if (!func || typeof func !== `function`) {
86
88
  throw new Error(`Invalid function ${req.params.function}`);
87
89
  }
88
90
  const invokeInstance = (0, core_1.core)({
89
91
  env: req.context.environment,
90
92
  localization: req.context.localization,
93
+ sources: null,
91
94
  });
92
- response = await f(invokeInstance, req.params.configuration);
95
+ response = await func(invokeInstance, req.params.configuration);
93
96
  break;
94
97
  default:
95
98
  throw new Error(`Invalid command`);
@@ -105,6 +108,7 @@ class Protocol {
105
108
  throw new Error(`Invalid provider for protocol`);
106
109
  }
107
110
  this.provider = provider;
111
+ this.path = path;
108
112
  }
109
113
  }
110
114
  exports.Protocol = Protocol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,12 +1,14 @@
1
+ import Joi from "joi";
1
2
  import _ from "lodash";
3
+ import { core } from "../core";
4
+ import { Credentials } from "../entities/credentials";
5
+ import { Action, Trigger } from "../entities/events";
2
6
  import { Provider, providerSchema } from "../entities/provider";
3
7
  import { i18n } from "./locales";
4
- import { core } from "../core";
5
- import Joi from "joi";
6
8
 
7
9
  const invokeSchema = Joi.object({
8
10
  command: Joi.string().valid(`invoke`).required(),
9
- model: Joi.string().valid(`action`, `trigger`, `credentials`).required(),
11
+ model: Joi.string().valid(`action`, `trigger`, `credential`).required(),
10
12
  id: Joi.string().required(),
11
13
  params: Joi.object({
12
14
  function: Joi.string().required(),
@@ -53,11 +55,13 @@ const commandSchema = Joi.alternatives(
53
55
  export class Protocol {
54
56
 
55
57
  private readonly provider: Provider;
58
+ private readonly path: string;
56
59
 
57
- constructor(provider: Provider) {
60
+ constructor(provider: Provider, path: string) {
58
61
  const providerValidation = providerSchema().validate(provider);
59
62
  if (providerValidation.error) { throw new Error(`Invalid provider for protocol`); }
60
63
  this.provider = provider;
64
+ this.path = path;
61
65
  }
62
66
 
63
67
  public evaluate = async (req: Protocol.Command): Promise<any> => {
@@ -65,22 +69,28 @@ export class Protocol {
65
69
  const validation = commandSchema.validate(req);
66
70
  if (validation.error) { throw new Error(`Invalid request: ${validation.error.message}`); }
67
71
 
72
+ const serialize = (elem: Action | Trigger | Credentials) => ({
73
+ ..._.omit(elem, [`functions`, `helpers`]),
74
+ functions: Object.keys(elem.functions),
75
+ helpers: Object.keys(elem.helpers),
76
+ });
77
+
68
78
  let response: any;
69
79
  switch (req.command) {
70
80
  case `list:actions`:
71
- response = Object.values(this.provider.actions || {});
81
+ response = Object.values(this.provider.actions || {}).map(serialize);
72
82
  break;
73
83
  case `list:triggers`:
74
- response = Object.values(this.provider.triggers || {});
84
+ response = Object.values(this.provider.triggers || {}).map(serialize);
75
85
  break;
76
86
  case `list:credentials`:
77
- response = Object.values(this.provider.credentials || {});
87
+ response = Object.values(this.provider.credentials || {}).map(serialize);
78
88
  break;
79
89
  case `list:sources`:
80
90
  response = Object.values(this.provider.sources || {});
81
91
  break;
82
92
  case `list:locales`:
83
- const i18nInstance = i18n({ directory: `node_modules/prow-hubspot-repository/locales` });
93
+ const i18nInstance = i18n({ directory: `${this.path}/locales` });
84
94
  response = i18nInstance.i18n.getLocales().map((l) => {
85
95
  i18nInstance.i18n.setLocale(l);
86
96
  return {
@@ -90,23 +100,20 @@ export class Protocol {
90
100
  });
91
101
  break;
92
102
  case `invoke`:
93
- const mEvent = _.get(this.provider, `${req.model}s.${req.id}`);
94
- if (!mEvent) {
95
- return {
96
- status: 404,
97
- body: JSON.stringify({ error: `Could not find ${req.model} with ID ${req.id}` }),
98
- };
99
- }
103
+ const elem = _.get(this.provider, `${req.model}s.${req.id}`);
104
+ if (!elem) { throw new Error(`Could not find ${req.model} with ID ${req.id}`); }
100
105
 
101
- const f = _.get(mEvent, req.params.function);
102
- if (!f || typeof f !== `function`) { throw new Error(`Invalid function ${req.params.function}`); }
106
+ const func = _.get(elem, req.params.function);
107
+ if (!func || typeof func !== `function`) { throw new Error(`Invalid function ${req.params.function}`); }
103
108
 
104
109
  const invokeInstance = core({
105
110
  env: req.context.environment,
106
111
  localization: req.context.localization,
112
+ // TODO: sources
113
+ sources: null,
107
114
  });
108
115
 
109
- response = await f(invokeInstance, req.params.configuration);
116
+ response = await func(invokeInstance, req.params.configuration);
110
117
  break;
111
118
  default:
112
119
  throw new Error(`Invalid command`);
@@ -119,7 +126,7 @@ export class Protocol {
119
126
 
120
127
  export namespace Protocol {
121
128
 
122
- export type Model = `action` | `trigger` | `credentials`;
129
+ export type Model = `action` | `trigger` | `credential`;
123
130
 
124
131
  export type Invoke = {
125
132
  command: `invoke`,