@beseif-solutions/prow-core 0.3.9 → 0.3.10

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/core.d.ts CHANGED
@@ -3,16 +3,16 @@ import Joi from "joi";
3
3
  import lodash from "lodash";
4
4
  import moment from "moment-timezone";
5
5
  import { FromCatalog, FromDirectory, T } from "./minified-utils/locales";
6
- type SyncOrAsync<T> = T | Promise<T>;
6
+ import { SourceConfig } from "./entities/source";
7
7
  export type SourceFunctions = {
8
- register: <T>(source: string, sandbox: boolean, data: T) => SyncOrAsync<{
8
+ register: <SC extends SourceConfig = SourceConfig>(source: SC[`id`], sandbox: boolean, data: SC[`fields`]) => Promise<{
9
9
  id: string;
10
10
  }>;
11
- consume: <T>(source: string, sandbox: boolean) => SyncOrAsync<{
11
+ consume: <SC extends SourceConfig = SourceConfig>(source: SC[`id`], sandbox: boolean) => Promise<{
12
12
  id: string;
13
- data: T;
13
+ data: SC[`fields`];
14
14
  }[]>;
15
- unregister: (source: string, sandbox: boolean, id: string) => SyncOrAsync<void>;
15
+ unregister: <SC extends SourceConfig = SourceConfig>(source: SC[`id`], sandbox: boolean, id: string) => Promise<void>;
16
16
  };
17
17
  export type Core = {
18
18
  axios: AxiosInstance;
@@ -20,7 +20,7 @@ export type Core = {
20
20
  lodash: lodash.LoDashStatic;
21
21
  env: Record<string, any>;
22
22
  joi: Joi.Root;
23
- sources: SourceFunctions;
23
+ sources?: SourceFunctions;
24
24
  t: T;
25
25
  };
26
26
  type CoreOptions = {
@@ -14,9 +14,11 @@ type Configuration<Config extends CoreConfig> = ({
14
14
  credentials: Config[`credentials`];
15
15
  }));
16
16
  export type CoreFunction<Config extends CoreConfig = {}> = ((core: Core, configuration: Configuration<Config>) => Promise<Config[`outputs`]>) | ((core: Core, configuration: Configuration<Config>) => Config[`outputs`]);
17
- type CommonConfig = Pick<CoreConfig, `credentials`>;
17
+ type CommonConfig = Pick<CoreConfig, `credentials`> & {
18
+ id?: string;
19
+ };
18
20
  export type Common<Config extends CommonConfig = {}> = {
19
- id: string;
21
+ id: Config[`id`] extends string ? Config[`id`] : string;
20
22
  helpers?: Record<string, CoreFunction<{
21
23
  credentials: Config[`credentials`];
22
24
  inputs: any;
@@ -1,7 +1,11 @@
1
1
  import Joi from "joi";
2
2
  import { Field } from "../minified-utils/fields";
3
3
  import { Common } from "./commons";
4
- export type Source = Common & {
4
+ export type SourceConfig = {
5
+ id: string;
6
+ fields: Record<string, any>;
7
+ };
8
+ export type Source<Config extends SourceConfig = SourceConfig> = Common<Config> & {
5
9
  model: "source";
6
10
  sandbox: boolean;
7
11
  fields: Field[];
@@ -7,7 +7,6 @@ exports.Protocol = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
8
  const joi_1 = __importDefault(require("joi"));
9
9
  const lodash_1 = __importDefault(require("lodash"));
10
- const moment_timezone_1 = __importDefault(require("moment-timezone"));
11
10
  const core_1 = require("../core");
12
11
  const provider_1 = require("../entities/provider");
13
12
  const locales_1 = require("./locales");
@@ -45,88 +44,73 @@ const localeListSchema = joi_1.default.object({
45
44
  command: joi_1.default.string().valid(`list:locales`).required(),
46
45
  }).required();
47
46
  const commandSchema = joi_1.default.alternatives(invokeSchema, credentialListSchema, actionListSchema, triggerListSchema, sourceListSchema, localeListSchema);
48
- const API_SOURCES = (provider, configuration, context) => ({
49
- register: async (source, sandbox, data) => {
50
- try {
51
- const { data: [created] } = await axios_1.default.post(`/api/v1/sources`, {
52
- organization: context.organization,
53
- workspace: context.workspace,
54
- provider: provider.id,
55
- version: provider.version,
56
- source: source,
57
- sandbox: sandbox,
58
- data: data,
59
- created_date: (0, moment_timezone_1.default)().format(`YYYY-MM-DDTHH:mm:ssZ`),
60
- }, {
61
- baseURL: configuration.host,
62
- headers: {
63
- "X-Internal-Token": configuration.token,
64
- },
65
- });
66
- if (!created || !created.uuid) {
67
- throw new Error(`Source registration failed`);
47
+ const API_SOURCES = (provider, configuration, context) => {
48
+ const REQ_CONFIG = {
49
+ baseURL: configuration.host,
50
+ headers: {
51
+ "x-prow-provider": provider.id,
52
+ "x-prow-version": provider.version,
53
+ "x-prow-token": configuration.token,
54
+ "x-prow-organization": context.organization,
55
+ "x-active-workspace": `${context.workspace}`,
56
+ },
57
+ };
58
+ return {
59
+ register: async (source, sandbox, data) => {
60
+ try {
61
+ const { data: [created] } = await axios_1.default.post(`/feu/v1/sources`, {
62
+ provider: provider.id,
63
+ version: provider.version,
64
+ source: source,
65
+ sandbox: sandbox,
66
+ data: data,
67
+ }, REQ_CONFIG);
68
+ if (!created || !created.uuid) {
69
+ throw new Error(`Source registration failed`);
70
+ }
71
+ return { id: created.uuid };
68
72
  }
69
- return { id: created.uuid };
70
- }
71
- catch (e) {
72
- throw e;
73
- }
74
- },
75
- unregister: async (source, sandbox, id) => {
76
- try {
77
- const { data: [get] } = await axios_1.default.post(`/api/v1/sources/list`, {
78
- organization: context.organization,
79
- workspace: context.workspace,
80
- provider: provider.id,
81
- version: provider.version,
82
- source: source,
83
- sandbox: sandbox,
84
- uuid: id,
85
- }, {
86
- baseURL: configuration.host,
87
- headers: {
88
- "X-Internal-Token": configuration.token,
89
- },
90
- });
91
- if (!get) {
92
- throw new Error(`Source with ID ${id} not found`);
73
+ catch (e) {
74
+ throw e;
93
75
  }
94
- await axios_1.default.delete(`/api/v1/sources/${get.id}`, {
95
- baseURL: configuration.host,
96
- headers: {
97
- "X-Internal-Token": configuration.token,
98
- },
99
- });
100
- }
101
- catch (e) {
102
- throw e;
103
- }
104
- },
105
- consume: async (source, sandbox) => {
106
- try {
107
- const { data: sources } = await axios_1.default.post(`/api/v1/sources/list`, {
108
- organization: context.organization,
109
- workspace: context.workspace,
110
- provider: provider.id,
111
- version: provider.version,
112
- source: source,
113
- sandbox: sandbox,
114
- }, {
115
- baseURL: configuration.host,
116
- headers: {
117
- "X-Internal-Token": configuration.token,
118
- },
119
- });
120
- return sources.map((s) => ({
121
- id: s.uuid,
122
- data: s.data,
123
- }));
124
- }
125
- catch (e) {
126
- throw e;
127
- }
128
- },
129
- });
76
+ },
77
+ unregister: async (source, sandbox, id) => {
78
+ try {
79
+ const { data: [get] } = await axios_1.default.post(`/feu/v1/sources/list`, {
80
+ provider: provider.id,
81
+ version: provider.version,
82
+ source: source,
83
+ sandbox: sandbox,
84
+ uuid: id,
85
+ }, REQ_CONFIG);
86
+ if (!get) {
87
+ throw new Error(`Source with ID ${id} not found`);
88
+ }
89
+ await axios_1.default.delete(`/feu/v1/sources/${get.id}`, REQ_CONFIG);
90
+ }
91
+ catch (e) {
92
+ throw e;
93
+ }
94
+ },
95
+ consume: async (source, sandbox) => {
96
+ try {
97
+ const { data: sources } = await axios_1.default.post(`/feu/v1/sources/list`, {
98
+ provider: provider.id,
99
+ version: provider.version,
100
+ source: source,
101
+ sandbox: sandbox,
102
+ }, REQ_CONFIG);
103
+ return sources.map((s) => ({
104
+ id: s.uuid,
105
+ data: s.data,
106
+ }));
107
+ }
108
+ catch (e) {
109
+ throw e;
110
+ }
111
+ },
112
+ };
113
+ };
130
114
  class Protocol {
131
115
  constructor(provider, configuration) {
132
116
  this.evaluate = async (req) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beseif-solutions/prow-core",
3
- "version": "0.3.9",
3
+ "version": "0.3.10",
4
4
  "description": "Prow core functionalities and classes",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/core.ts CHANGED
@@ -3,13 +3,12 @@ import Joi from "joi";
3
3
  import lodash from "lodash";
4
4
  import moment from "moment-timezone";
5
5
  import { FromCatalog, FromDirectory, i18n, T } from "./minified-utils/locales";
6
-
7
- type SyncOrAsync<T> = T | Promise<T>;
6
+ import { SourceConfig } from "./entities/source";
8
7
 
9
8
  export type SourceFunctions = {
10
- register: <T>(source: string, sandbox: boolean, data: T) => SyncOrAsync<{ id: string }>,
11
- consume: <T>(source: string, sandbox: boolean) => SyncOrAsync<{ id: string, data: T }[]>,
12
- unregister: (source: string, sandbox: boolean, id: string) => SyncOrAsync<void>,
9
+ register: <SC extends SourceConfig = SourceConfig>(source: SC[`id`], sandbox: boolean, data: SC[`fields`]) => Promise<{ id: string }>,
10
+ consume: <SC extends SourceConfig = SourceConfig>(source: SC[`id`], sandbox: boolean) => Promise<{ id: string, data: SC[`fields`] }[]>,
11
+ unregister: <SC extends SourceConfig = SourceConfig>(source: SC[`id`], sandbox: boolean, id: string) => Promise<void>,
13
12
  };
14
13
 
15
14
  const FALLBACK_SOURCES: SourceFunctions = {
@@ -30,7 +29,7 @@ export type Core = {
30
29
  lodash: lodash.LoDashStatic,
31
30
  env: Record<string, any>,
32
31
  joi: Joi.Root,
33
- sources: SourceFunctions,
32
+ sources?: SourceFunctions,
34
33
  t: T,
35
34
  };
36
35
 
@@ -26,10 +26,10 @@ export type CoreFunction<Config extends CoreConfig = {}> =
26
26
  ((core: Core, configuration: Configuration<Config>) => Promise<Config[`outputs`]>)
27
27
  | ((core: Core, configuration: Configuration<Config>) => Config[`outputs`]);
28
28
 
29
- type CommonConfig = Pick<CoreConfig, `credentials`>;
29
+ type CommonConfig = Pick<CoreConfig, `credentials`> & { id?: string };
30
30
 
31
31
  export type Common<Config extends CommonConfig = {}> = {
32
- id: string,
32
+ id: Config[`id`] extends string ? Config[`id`] : string,
33
33
  helpers?: Record<string, CoreFunction<{ credentials: Config[`credentials`], inputs: any, outputs: any }>>,
34
34
  };
35
35
 
@@ -2,7 +2,12 @@ import Joi from "joi";
2
2
  import { Field, fieldsSchema } from "../minified-utils/fields";
3
3
  import { Common, commonSchema } from "./commons";
4
4
 
5
- export type Source = Common & {
5
+ export type SourceConfig = {
6
+ id: string,
7
+ fields: Record<string, any>,
8
+ };
9
+
10
+ export type Source<Config extends SourceConfig = SourceConfig> = Common<Config> & {
6
11
  model: "source",
7
12
  sandbox: boolean,
8
13
  fields: Field[],
@@ -1,7 +1,6 @@
1
1
  import axios from "axios";
2
2
  import Joi from "joi";
3
3
  import _ from "lodash";
4
- import moment from "moment-timezone";
5
4
  import { core, SourceFunctions } from "../core";
6
5
  import { Credentials } from "../entities/credentials";
7
6
  import { Action, Trigger } from "../entities/events";
@@ -56,76 +55,63 @@ const commandSchema = Joi.alternatives(
56
55
  localeListSchema,
57
56
  );
58
57
 
59
- const API_SOURCES = (provider: Provider, configuration: Protocol.SourcesConfiguration, context: { organization: string, workspace: number }): SourceFunctions => ({
60
- register: async (source, sandbox, data) => {
61
- try {
62
- const { data: [created] } = await axios.post(`/api/v1/sources`, {
63
- organization: context.organization,
64
- workspace: context.workspace,
65
- provider: provider.id,
66
- version: provider.version,
67
- source: source,
68
- sandbox: sandbox,
69
- data: data,
70
- created_date: moment().format(`YYYY-MM-DDTHH:mm:ssZ`),
71
- }, {
72
- baseURL: configuration.host,
73
- headers: {
74
- "X-Internal-Token": configuration.token,
75
- },
76
- });
77
- if (!created || !created.uuid) { throw new Error(`Source registration failed`); }
78
- return { id: created.uuid };
79
- } catch (e) { throw e; }
80
- },
81
- unregister: async (source, sandbox, id) => {
82
- try {
83
- const { data: [get] } = await axios.post(`/api/v1/sources/list`, {
84
- organization: context.organization,
85
- workspace: context.workspace,
86
- provider: provider.id,
87
- version: provider.version,
88
- source: source,
89
- sandbox: sandbox,
90
- uuid: id,
91
- }, {
92
- baseURL: configuration.host,
93
- headers: {
94
- "X-Internal-Token": configuration.token,
95
- },
96
- });
97
- if (!get) { throw new Error(`Source with ID ${id} not found`); }
58
+ const API_SOURCES = (provider: Provider, configuration: Protocol.SourcesConfiguration, context: { organization: string, workspace: number }): SourceFunctions => {
98
59
 
99
- await axios.delete(`/api/v1/sources/${get.id}`, {
100
- baseURL: configuration.host,
101
- headers: {
102
- "X-Internal-Token": configuration.token,
103
- },
104
- });
105
- } catch (e) { throw e; }
106
- },
107
- consume: async (source, sandbox) => {
108
- try {
109
- const { data: sources } = await axios.post(`/api/v1/sources/list`, {
110
- organization: context.organization,
111
- workspace: context.workspace,
112
- provider: provider.id,
113
- version: provider.version,
114
- source: source,
115
- sandbox: sandbox,
116
- }, {
117
- baseURL: configuration.host,
118
- headers: {
119
- "X-Internal-Token": configuration.token,
120
- },
121
- });
122
- return sources.map((s) => ({
123
- id: s.uuid,
124
- data: s.data,
125
- }));
126
- } catch (e) { throw e; }
127
- },
128
- });
60
+ const REQ_CONFIG = {
61
+ baseURL: configuration.host,
62
+ headers: {
63
+ "x-prow-provider": provider.id,
64
+ "x-prow-version": provider.version,
65
+ "x-prow-token": configuration.token,
66
+ "x-prow-organization": context.organization,
67
+ "x-active-workspace": `${context.workspace}`,
68
+ },
69
+ };
70
+
71
+ return {
72
+ register: async (source, sandbox, data) => {
73
+ try {
74
+ const { data: [created] } = await axios.post(`/feu/v1/sources`, {
75
+ provider: provider.id,
76
+ version: provider.version,
77
+ source: source,
78
+ sandbox: sandbox,
79
+ data: data,
80
+ }, REQ_CONFIG);
81
+ if (!created || !created.uuid) { throw new Error(`Source registration failed`); }
82
+ return { id: created.uuid };
83
+ } catch (e) { throw e; }
84
+ },
85
+ unregister: async (source, sandbox, id) => {
86
+ try {
87
+ const { data: [get] } = await axios.post(`/feu/v1/sources/list`, {
88
+ provider: provider.id,
89
+ version: provider.version,
90
+ source: source,
91
+ sandbox: sandbox,
92
+ uuid: id,
93
+ }, REQ_CONFIG);
94
+ if (!get) { throw new Error(`Source with ID ${id} not found`); }
95
+
96
+ await axios.delete(`/feu/v1/sources/${get.id}`, REQ_CONFIG);
97
+ } catch (e) { throw e; }
98
+ },
99
+ consume: async (source, sandbox) => {
100
+ try {
101
+ const { data: sources } = await axios.post(`/feu/v1/sources/list`, {
102
+ provider: provider.id,
103
+ version: provider.version,
104
+ source: source,
105
+ sandbox: sandbox,
106
+ }, REQ_CONFIG);
107
+ return sources.map((s) => ({
108
+ id: s.uuid,
109
+ data: s.data,
110
+ }));
111
+ } catch (e) { throw e; }
112
+ },
113
+ };
114
+ };
129
115
 
130
116
  export class Protocol {
131
117