@ductape/sdk 0.0.4-v32 → 0.0.4-v34

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.
@@ -36,7 +36,7 @@ class AppBuilderService {
36
36
  }
37
37
  else {
38
38
  const app = await this.createNewApp(data);
39
- await this.initializeApp(app._id);
39
+ //await this.initializeApp(app._id);
40
40
  }
41
41
  return { app_id: this.app._id };
42
42
  }
@@ -44,12 +44,20 @@ class AppBuilderService {
44
44
  throw e;
45
45
  }
46
46
  }
47
- initAppVersion() {
48
- this.appVersion = this.app.versions.find((version) => version.latest === true);
47
+ initAppVersion(versionTag) {
48
+ if (versionTag) {
49
+ this.appVersion = this.app.versions.find((version) => version.tag === versionTag);
50
+ if (!this.appVersion) {
51
+ throw new Error(`Version ${versionTag} not found`);
52
+ }
53
+ }
54
+ else {
55
+ this.appVersion = this.app.versions.find((version) => version.latest === true);
56
+ }
49
57
  }
50
58
  async initializeAppByTag(app_tag) {
51
59
  try {
52
- this.app = await this.appApi.fetchAppByTag(app_tag, this.getUserAccess());
60
+ this.app = await this.appApi.initApp(app_tag, this.getUserAccess());
53
61
  this.app_id = this.app._id;
54
62
  this.initAppVersion();
55
63
  }
@@ -59,7 +67,7 @@ class AppBuilderService {
59
67
  }
60
68
  async initializeApp(app_id) {
61
69
  try {
62
- this.app = await this.appApi.fetchApp(app_id, this.getUserAccess());
70
+ this.app = await this.appApi.initApp(app_id, this.getUserAccess());
63
71
  this.app_id = app_id;
64
72
  this.initAppVersion();
65
73
  }
@@ -106,7 +114,7 @@ class AppBuilderService {
106
114
  throw new Error('App not initialized');
107
115
  await validators_1.UpdateAppBuilderSchema.validateAsync(data);
108
116
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, data), { component: types_1.AppComponents.APP, action: types_1.RequestAction.UPDATE }), this.getUserAccess());
109
- await this.initializeApp(this.app_id);
117
+ //await this.initializeApp(this.app_id);
110
118
  }
111
119
  catch (e) {
112
120
  throw e;
@@ -115,10 +123,11 @@ class AppBuilderService {
115
123
  async createEnv(data, throwErrorIfExists = false) {
116
124
  try {
117
125
  // TODO: figure out a way to check if this has run before, halt if it has
118
- if (!this.fetchEnv(data.slug, false)) {
126
+ const existingEnv = await this.fetchEnv(data.slug, false);
127
+ if (!existingEnv) {
119
128
  await validators_1.CreateAppEnvSchema.validateAsync(data);
120
129
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, data), { component: types_1.AppComponents.ENV, action: types_1.RequestAction.CREATE }), this.getUserAccess());
121
- await this.initializeApp(this.app_id);
130
+ //await this.initializeApp(this.app_id);
122
131
  }
123
132
  else {
124
133
  if (throwErrorIfExists)
@@ -131,24 +140,29 @@ class AppBuilderService {
131
140
  }
132
141
  async updateEnv(slug, data) {
133
142
  try {
134
- // const { _id } = this.fetchEnv(slug);
143
+ // const { _id } = await this.fetchEnv(slug);
135
144
  await validators_1.UpdateAppEnvSchema.validateAsync(Object.assign(Object.assign({}, data), { slug }));
136
- if (data.slug && this.fetchEnv(data.slug)) {
137
- throw new Error(`slug ${slug} is in use`); // TODO: also check on the backend
145
+ if (data.slug) {
146
+ const existingEnv = await this.fetchEnv(data.slug, false);
147
+ if (existingEnv) {
148
+ throw new Error(`slug ${slug} is in use`); // TODO: also check on the backend
149
+ }
138
150
  }
139
151
  // TODO: check
140
152
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({ slug }, data), { component: types_1.AppComponents.ENV, action: types_1.RequestAction.UPDATE }), this.getUserAccess());
141
- await this.initializeApp(this.app_id);
153
+ //await this.initializeApp(this.app_id);
142
154
  }
143
155
  catch (e) {
144
156
  throw e;
145
157
  }
146
158
  }
147
- fetchEnvs() {
148
- return this.appVersion.envs;
159
+ async fetchEnvs() {
160
+ const components = await this.appApi.fetchAppComponents(this.app_id, 'env', this.appVersion.tag);
161
+ return components;
149
162
  }
150
- fetchEnv(slug, throwErrorIfExists = true) {
151
- const env = this.appVersion.envs.find((data) => data.slug === slug);
163
+ async fetchEnv(slug, throwErrorIfExists = true) {
164
+ const envs = await this.fetchEnvs();
165
+ const env = envs.find((data) => data.slug === slug);
152
166
  if (!env && throwErrorIfExists)
153
167
  throw new Error(`Env ${slug} not found`);
154
168
  return env;
@@ -222,7 +236,7 @@ class AppBuilderService {
222
236
  // Check if action already exists
223
237
  let exists;
224
238
  try {
225
- exists = this.fetchAction(tag);
239
+ exists = await this.fetchAction(tag);
226
240
  }
227
241
  catch (e) {
228
242
  exists = false;
@@ -320,14 +334,14 @@ class AppBuilderService {
320
334
  case 'params':
321
335
  body.params = {
322
336
  type: types_1.InputsTypes.JSON,
323
- sample: JSON.stringify(data.params),
337
+ sample: JSON.stringify(data.params.sample),
324
338
  data: this.mergeMetadataIntoSchema(result.parsedData, result.metadata)
325
339
  };
326
340
  break;
327
341
  case 'query':
328
342
  body.query = {
329
343
  type: types_1.InputsTypes.JSON,
330
- sample: JSON.stringify(data.query),
344
+ sample: JSON.stringify(data.query.sample),
331
345
  data: this.mergeMetadataIntoSchema(result.parsedData, result.metadata)
332
346
  };
333
347
  break;
@@ -348,7 +362,7 @@ class AppBuilderService {
348
362
  case 'headers':
349
363
  body.headers = {
350
364
  type: types_1.InputsTypes.JSON,
351
- sample: JSON.stringify(data.headers),
365
+ sample: JSON.stringify(data.headers.sample),
352
366
  data: this.mergeMetadataIntoSchema(result.parsedData, result.metadata)
353
367
  };
354
368
  break;
@@ -381,7 +395,7 @@ class AppBuilderService {
381
395
  // Create action via API
382
396
  await this.updateActionProcess(tag, body, types_1.RequestAction.CREATE);
383
397
  // Update local cache instead of full app refetch
384
- this.appVersion.actions.push(body);
398
+ //this.appVersion.actions.push(body as IAppAction);
385
399
  }
386
400
  // Helper method to extract metadata from data object
387
401
  extractMetadata(data) {
@@ -395,11 +409,13 @@ class AppBuilderService {
395
409
  }
396
410
  return metadataMap;
397
411
  }
398
- fetchActions() {
399
- return this.appVersion.actions;
412
+ async fetchActions() {
413
+ const components = await this.appApi.fetchAppComponents(this.app_id, 'action', this.appVersion.tag);
414
+ return components;
400
415
  }
401
- fetchAction(identifier) {
402
- const action = this.appVersion.actions.find((data) => data.tag === identifier || data._id === identifier);
416
+ async fetchAction(identifier) {
417
+ const actions = await this.fetchActions();
418
+ const action = actions.find((data) => data.tag === identifier || data._id === identifier);
403
419
  if (!action)
404
420
  throw new Error(`Action ${identifier} not found`);
405
421
  return action;
@@ -407,7 +423,7 @@ class AppBuilderService {
407
423
  async updateAction(tag, data) {
408
424
  // Validate schema
409
425
  await validators_1.UpdateAppActionSchema.validateAsync(data);
410
- const action = this.fetchAction(tag);
426
+ const action = await this.fetchAction(tag);
411
427
  const body = action;
412
428
  if (!action) {
413
429
  throw new Error(`Action ${tag} not found`);
@@ -558,10 +574,10 @@ class AppBuilderService {
558
574
  // Update action via API
559
575
  await this.updateActionProcess(tag, body, types_1.RequestAction.UPDATE);
560
576
  // Update local cache instead of full app refetch
561
- const actionIndex = this.appVersion.actions.findIndex(a => a.tag === tag || a._id === action._id);
562
- if (actionIndex > -1) {
563
- this.appVersion.actions[actionIndex] = body;
564
- }
577
+ //const actionIndex = this.appVersion.actions.findIndex(a => a.tag === tag || a._id === action._id);
578
+ //if (actionIndex > -1) {
579
+ //this.appVersion.actions[actionIndex] = body as IAppAction;
580
+ //}
565
581
  }
566
582
  async updateActionProcess(tag, data, action) {
567
583
  const query = Object.assign({ component: types_1.AppComponents.ACTION, action,
@@ -597,10 +613,10 @@ class AppBuilderService {
597
613
  return Object.assign(Object.assign({}, payload), { sample, data });
598
614
  }
599
615
  // private fetc
600
- fetchActionRequestData(category, tag) {
616
+ async fetchActionRequestData(category, tag) {
601
617
  try {
602
618
  let data;
603
- const action = this.fetchAction(tag);
619
+ const action = await this.fetchAction(tag);
604
620
  if (category === types_1.Categories.BODY) {
605
621
  const { body } = action;
606
622
  data = body.data;
@@ -626,10 +642,10 @@ class AppBuilderService {
626
642
  throw e;
627
643
  }
628
644
  }
629
- fetchActionRequestSample(category, tag) {
645
+ async fetchActionRequestSample(category, tag) {
630
646
  try {
631
647
  let sample;
632
- const action = this.fetchAction(tag);
648
+ const action = await this.fetchAction(tag);
633
649
  if (category === types_1.Categories.BODY) {
634
650
  const { body } = action;
635
651
  sample = body.sample;
@@ -657,10 +673,10 @@ class AppBuilderService {
657
673
  }
658
674
  async createAppActionResponse(action_tag, payload) {
659
675
  try {
660
- this.fetchAction(action_tag);
676
+ await this.fetchAction(action_tag);
661
677
  await validators_1.CreateAppActionResponseSchema.validateAsync(Object.assign({}, payload));
662
678
  payload.tag = (0, string_utils_1.tagify)(payload.tag);
663
- const exists = this.fetchAppActionResponse(action_tag, payload.tag);
679
+ const exists = await this.fetchAppActionResponse(action_tag, payload.tag);
664
680
  if (!exists) {
665
681
  payload = await this.parseResponsePayload(payload);
666
682
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, payload), { action_tag, action: types_1.RequestAction.CREATE, component: types_1.AppComponents.ACTION_RESPONSE }), this.getUserAccess());
@@ -707,19 +723,21 @@ class AppBuilderService {
707
723
  payload.body.data = data;
708
724
  payload.body.sample = JSON.stringify(payload.body.sample);
709
725
  if (payload.envs && payload.envs.length) {
710
- payload.envs = payload.envs.map((slug) => {
711
- const { _id: env_id } = this.fetchEnv(slug);
712
- return env_id;
713
- });
726
+ const envIds = await Promise.all(payload.envs.map(async (slug) => {
727
+ const env = await this.fetchEnv(slug);
728
+ return env._id;
729
+ }));
730
+ payload.envs = envIds;
714
731
  }
715
732
  return payload;
716
733
  }
717
- fetchAppActionResponse(action_tag, response_tag, throwErrorIfExists = false) {
718
- const { responses } = this.fetchAction(action_tag);
719
- const action = responses.find((data) => data.tag === response_tag);
720
- if (!action && throwErrorIfExists)
734
+ async fetchAppActionResponse(action_tag, response_tag, throwErrorIfExists = false) {
735
+ const action = await this.fetchAction(action_tag);
736
+ const { responses } = action;
737
+ const response = responses.find((data) => data.tag === response_tag);
738
+ if (!response && throwErrorIfExists)
721
739
  throw new Error(`Response ${response_tag} not found`);
722
- return action;
740
+ return response;
723
741
  }
724
742
  /*async extractEventData(data: Partial<IAppEvent>): Promise<Partial<IAppEvent>> {
725
743
  if (data.response) {
@@ -797,7 +815,7 @@ class AppBuilderService {
797
815
  this.getUserAccess(),
798
816
  );
799
817
 
800
- await this.initializeApp(this.app_id);
818
+ //await this.initializeApp(this.app_id);
801
819
  } catch (e) {
802
820
  throw e;
803
821
  }
@@ -828,7 +846,7 @@ class AppBuilderService {
828
846
  this.getUserAccess(),
829
847
  );
830
848
 
831
- await this.initializeApp(this.app_id);
849
+ //await this.initializeApp(this.app_id);
832
850
  } catch (e) {
833
851
  throw e;
834
852
  }
@@ -903,11 +921,11 @@ class AppBuilderService {
903
921
  throw new Error(`token has to have one of body, headers, params or query set`);
904
922
  }
905
923
  if (setup_type === types_1.AuthTypes.CREDENTIALS) {
906
- this.fetchAction(action_tag);
924
+ await this.fetchAction(action_tag);
907
925
  }
908
926
  data = await this.extractAuthData(Object.assign({}, data));
909
927
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, data), { action: types_1.RequestAction.CREATE, component: types_1.AppComponents.AUTH }), this.getUserAccess());
910
- await this.initializeApp(this.app_id);
928
+ //await this.initializeApp(this.app_id);
911
929
  }
912
930
  catch (e) {
913
931
  throw e;
@@ -916,21 +934,23 @@ class AppBuilderService {
916
934
  async updateAuth(tag, data) {
917
935
  try {
918
936
  await validators_1.UpdateAppAuthSchema.validateAsync(data);
919
- const auth = this.fetchAuth(tag);
937
+ const auth = await this.fetchAuth(tag);
920
938
  data = await this.extractAuthData(Object.assign(Object.assign({}, auth), data));
921
939
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({ tag }, data), { action: types_1.RequestAction.UPDATE, component: types_1.AppComponents.AUTH }), this.getUserAccess());
922
- await this.initializeApp(this.app_id);
923
- await this.initializeApp(this.app_id);
940
+ //await this.initializeApp(this.app_id);
941
+ //await this.initializeApp(this.app_id);
924
942
  }
925
943
  catch (e) {
926
944
  throw e;
927
945
  }
928
946
  }
929
- fetchAuths() {
930
- return this.appVersion.auths;
947
+ async fetchAuths() {
948
+ const components = await this.appApi.fetchAppComponents(this.app_id, 'auth', this.appVersion.tag);
949
+ return components;
931
950
  }
932
- fetchAuth(tag) {
933
- const auth = this.appVersion.auths.find((data) => data.tag === tag);
951
+ async fetchAuth(tag) {
952
+ const auths = await this.fetchAuths();
953
+ const auth = auths.find((data) => data.tag === tag);
934
954
  if (!auth)
935
955
  throw new Error(`Auth ${tag} not found`);
936
956
  return auth;
@@ -938,13 +958,13 @@ class AppBuilderService {
938
958
  async createVariable(data, throwErrorIfExists = false) {
939
959
  try {
940
960
  await validators_1.CreateAppVariableSchema.validateAsync(data);
941
- const exists = this.fetchVariable(data.key, false);
961
+ const exists = await this.fetchVariable(data.key, false);
942
962
  if (exists && throwErrorIfExists)
943
963
  throw new Error(`Variable ${data.key} exists`);
944
964
  if (!exists) {
945
965
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, data), { action: types_1.RequestAction.CREATE, component: types_1.AppComponents.VARIABLE }), this.getUserAccess());
946
966
  }
947
- await this.initializeApp(this.app_id);
967
+ //await this.initializeApp(this.app_id);
948
968
  }
949
969
  catch (e) {
950
970
  throw e;
@@ -953,19 +973,21 @@ class AppBuilderService {
953
973
  async updateVariable(key, data) {
954
974
  try {
955
975
  await validators_1.UpdateAppVariableSchema.validateAsync(data);
956
- this.fetchVariable(key);
976
+ await this.fetchVariable(key);
957
977
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, data), { key, action: types_1.RequestAction.UPDATE, component: types_1.AppComponents.VARIABLE }), this.getUserAccess());
958
- await this.initializeApp(this.app_id);
978
+ //await this.initializeApp(this.app_id);
959
979
  }
960
980
  catch (e) {
961
981
  throw e;
962
982
  }
963
983
  }
964
- fetchVariables() {
965
- return this.appVersion.variables;
984
+ async fetchVariables() {
985
+ const components = await this.appApi.fetchAppComponents(this.app_id, 'variable', this.appVersion.tag);
986
+ return components;
966
987
  }
967
- fetchVariable(key, throwErrorIfExists = true) {
968
- const variable = this.appVersion.variables.find((data) => data.key === key);
988
+ async fetchVariable(key, throwErrorIfExists = true) {
989
+ const variables = await this.fetchVariables();
990
+ const variable = variables.find((data) => data.key === key);
969
991
  if (!variable && throwErrorIfExists)
970
992
  throw new Error(`Variable ${key} not found`);
971
993
  return variable;
@@ -973,11 +995,11 @@ class AppBuilderService {
973
995
  async createConstant(data, throwErrorIfExists = false) {
974
996
  try {
975
997
  await validators_1.CreateAppConstantSchema.validateAsync(data);
976
- const exists = this.fetchConstant(data.key, false);
998
+ const exists = await this.fetchConstant(data.key, false);
977
999
  if (exists && throwErrorIfExists)
978
1000
  throw new Error(`Constant ${data.key} exists`);
979
1001
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, data), { action: types_1.RequestAction.CREATE, component: types_1.AppComponents.CONSTANT }), this.getUserAccess());
980
- await this.initializeApp(this.app_id);
1002
+ //await this.initializeApp(this.app_id);
981
1003
  }
982
1004
  catch (e) {
983
1005
  throw e;
@@ -985,15 +1007,17 @@ class AppBuilderService {
985
1007
  }
986
1008
  async updateConstant(key, data) {
987
1009
  await validators_1.UpdateAppConstantSchema.validateAsync(data);
988
- this.fetchConstant(key);
1010
+ await this.fetchConstant(key);
989
1011
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, data), { action: types_1.RequestAction.UPDATE, component: types_1.AppComponents.CONSTANT }), this.getUserAccess());
990
- await this.initializeApp(this.app_id);
1012
+ //await this.initializeApp(this.app_id);
991
1013
  }
992
- fetchConstants() {
993
- return this.appVersion.constants;
1014
+ async fetchConstants() {
1015
+ const components = await this.appApi.fetchAppComponents(this.app_id, 'constant', this.appVersion.tag);
1016
+ return components;
994
1017
  }
995
- fetchConstant(key, throwErrorIfExists = true) {
996
- const variable = this.appVersion.constants.find((data) => data.key === key);
1018
+ async fetchConstant(key, throwErrorIfExists = true) {
1019
+ const constants = await this.fetchConstants();
1020
+ const variable = constants.find((data) => data.key === key);
997
1021
  if (!variable && throwErrorIfExists)
998
1022
  throw new Error(`Constant ${key} not found`);
999
1023
  return variable;
@@ -1024,7 +1048,7 @@ class AppBuilderService {
1024
1048
  async updateValidation(category, stages, update) {
1025
1049
  try {
1026
1050
  const action_tag = stages[0];
1027
- const action = this.fetchAction(action_tag);
1051
+ const action = await this.fetchAction(action_tag);
1028
1052
  const data = action[category];
1029
1053
  let level = -1;
1030
1054
  let key = '';
@@ -1064,7 +1088,7 @@ class AppBuilderService {
1064
1088
  ...exclude,
1065
1089
  ]);
1066
1090
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({ tag: action_tag }, action), { component: types_1.AppComponents.VALIDATION, action: types_1.RequestAction.UPDATE }), this.getUserAccess());
1067
- await this.initializeApp(this.app_id);
1091
+ //await this.initializeApp(this.app_id);
1068
1092
  }
1069
1093
  catch (e) {
1070
1094
  throw e;
@@ -1073,10 +1097,11 @@ class AppBuilderService {
1073
1097
  async createWebhook(data, throwErrorIfExists = false) {
1074
1098
  try {
1075
1099
  // TODO: figure out a way to check if this has run before, halt if it has
1076
- if (!this.fetchWebhook(data.tag)) {
1100
+ const existingWebhook = await this.fetchWebhook(data.tag);
1101
+ if (!existingWebhook) {
1077
1102
  await create_appWebhook_validator_1.IAppWebhookSchema.validateAsync(data);
1078
- data.envs = data.envs.map((env) => {
1079
- const exists = this.fetchEnv(env.slug);
1103
+ data.envs = await Promise.all(data.envs.map(async (env) => {
1104
+ const exists = await this.fetchEnv(env.slug);
1080
1105
  if (!exists) {
1081
1106
  throw new Error(`Env ${env.slug} does not exist`);
1082
1107
  }
@@ -1094,8 +1119,8 @@ class AppBuilderService {
1094
1119
  env.sample_data = [...bodyTemplate, ...queryTemplate, ...paramsTemplate, ...headerTemplate];
1095
1120
  }
1096
1121
  return env;
1097
- });
1098
- const envs = this.fetchEnvs();
1122
+ }));
1123
+ const envs = await this.fetchEnvs();
1099
1124
  envs.map((env) => {
1100
1125
  const exists = data.envs.findIndex((dbEnv) => dbEnv.slug === env.slug);
1101
1126
  if (exists === -1) {
@@ -1103,7 +1128,7 @@ class AppBuilderService {
1103
1128
  }
1104
1129
  });
1105
1130
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({}, data), { component: types_1.AppComponents.WEBHOOK, action: types_1.RequestAction.CREATE }), this.getUserAccess());
1106
- await this.initializeApp(this.app_id);
1131
+ //await this.initializeApp(this.app_id);
1107
1132
  }
1108
1133
  else {
1109
1134
  if (throwErrorIfExists)
@@ -1116,13 +1141,17 @@ class AppBuilderService {
1116
1141
  }
1117
1142
  async updateWebhook(tag, data) {
1118
1143
  try {
1119
- const { _id, envs } = this.fetchWebhook(tag);
1144
+ const webhook = await this.fetchWebhook(tag);
1145
+ const { _id, envs } = webhook;
1120
1146
  await update_appWebhook_validator_1.IAppWebhookUpdateSchema.validateAsync(data); // Change to update;
1121
- if (data.tag && this.fetchWebhook(data.tag)) {
1122
- throw new Error(`tag ${tag} is in use`); // TODO: also check on the backend
1147
+ if (data.tag) {
1148
+ const existingWebhook = await this.fetchWebhook(data.tag);
1149
+ if (existingWebhook) {
1150
+ throw new Error(`tag ${tag} is in use`); // TODO: also check on the backend
1151
+ }
1123
1152
  }
1124
- data.envs = data.envs.map((env) => {
1125
- const exists = this.fetchEnv(env.slug);
1153
+ data.envs = await Promise.all(data.envs.map(async (env) => {
1154
+ const exists = await this.fetchEnv(env.slug);
1126
1155
  if (!exists) {
1127
1156
  throw new Error(`Env ${env.slug} does not exist`);
1128
1157
  }
@@ -1138,12 +1167,13 @@ class AppBuilderService {
1138
1167
  env.sample_data = [...bodyTemplate, ...queryTemplate, ...paramsTemplate, ...headerTemplate];
1139
1168
  }
1140
1169
  return env;
1141
- });
1170
+ }));
1142
1171
  const overwrite = [];
1143
1172
  const newEnvs = [];
1144
- envs.map((env) => {
1173
+ for (const env of envs) {
1145
1174
  const exists = data.envs.findIndex((dataEnv) => env.slug === dataEnv.slug);
1146
- if (!this.fetchEnv(env.slug)) {
1175
+ const envExists = await this.fetchEnv(env.slug, false);
1176
+ if (!envExists) {
1147
1177
  throw new Error(`Product Environment ${env.slug} doesn't exist`);
1148
1178
  }
1149
1179
  if (exists === -1) {
@@ -1152,7 +1182,7 @@ class AppBuilderService {
1152
1182
  else {
1153
1183
  overwrite.push(Object.assign(Object.assign({}, env), data.envs[exists]));
1154
1184
  }
1155
- });
1185
+ }
1156
1186
  const unchanged = [];
1157
1187
  envs.map((env) => {
1158
1188
  const newEnv = newEnvs.findIndex((dataEnv) => env.slug === dataEnv.slug) > -1;
@@ -1164,20 +1194,20 @@ class AppBuilderService {
1164
1194
  data.envs = [...unchanged, ...overwrite, ...newEnvs];
1165
1195
  data.envs = [...overwrite, ...newEnvs];
1166
1196
  await this.appApi.updateApp(this.app_id, Object.assign(Object.assign({ _id }, data), { component: types_1.AppComponents.WEBHOOK, action: types_1.RequestAction.UPDATE }), this.getUserAccess());
1167
- await this.initializeApp(this.app_id);
1197
+ //await this.initializeApp(this.app_id);
1168
1198
  }
1169
1199
  catch (e) {
1170
1200
  throw e;
1171
1201
  }
1172
1202
  }
1173
- fetchWebhook(tag) {
1174
- var _a, _b;
1175
- const exists = (_b = (_a = this.appVersion) === null || _a === void 0 ? void 0 : _a.webhooks) === null || _b === void 0 ? void 0 : _b.find((data) => data.tag === tag);
1203
+ async fetchWebhook(tag) {
1204
+ const webhooks = await this.fetchWebhooks();
1205
+ const exists = webhooks === null || webhooks === void 0 ? void 0 : webhooks.find((data) => data.tag === tag);
1176
1206
  return exists;
1177
1207
  }
1178
- fetchWebhooks() {
1179
- var _a;
1180
- return (_a = this.appVersion) === null || _a === void 0 ? void 0 : _a.webhooks;
1208
+ async fetchWebhooks() {
1209
+ const components = await this.appApi.fetchAppComponents(this.app_id, 'webhook', this.appVersion.tag);
1210
+ return components;
1181
1211
  }
1182
1212
  async createWebhookEvent(data, throwErrorIfExists = false) {
1183
1213
  try {
@@ -1189,7 +1219,7 @@ class AppBuilderService {
1189
1219
  if (!webhookTag || !tag) {
1190
1220
  throw new Error(`tag is expected to be defined as "webhook_tag:event_tag"`);
1191
1221
  }
1192
- const exists = this.fetchWebhookEvent(data.tag);
1222
+ const exists = await this.fetchWebhookEvent(data.tag);
1193
1223
  data.tag = tag;
1194
1224
  if (!exists) {
1195
1225
  if (!data.selector.startsWith('$Event{')) {
@@ -1211,7 +1241,7 @@ class AppBuilderService {
1211
1241
  throw new Error("Selector value is not allowed to be an object|array|null|undefined");
1212
1242
  }
1213
1243
  //check other webhooksevents to see if there are other places where selector and selectorValue match up
1214
- const existing = this.fetchWebhookEvents(webhookTag);
1244
+ const existing = await this.fetchWebhookEvents(webhookTag);
1215
1245
  existing === null || existing === void 0 ? void 0 : existing.map((event) => {
1216
1246
  if (event.selector === data.selector && event.selectorValue === current) {
1217
1247
  throw new Error(`Selector ${data.selector} with value ${current} already exists on event ${event.tag}`);
@@ -1226,7 +1256,7 @@ class AppBuilderService {
1226
1256
  const payload = Object.assign(Object.assign({}, data), { webhookTag,
1227
1257
  tag, component: types_1.AppComponents.WEBHOOK_EVENT, action: types_1.RequestAction.CREATE });
1228
1258
  await this.appApi.updateApp(this.app_id, payload, this.getUserAccess());
1229
- await this.initializeApp(this.app_id);
1259
+ //await this.initializeApp(this.app_id);
1230
1260
  }
1231
1261
  else {
1232
1262
  if (throwErrorIfExists)
@@ -1275,7 +1305,7 @@ class AppBuilderService {
1275
1305
  throw new Error("Selector value is not allowed to be an object|array|null|undefined");
1276
1306
  }
1277
1307
  //check other webhooksevents to see if there are other places where selector and selectorValue match up
1278
- const existing = this.fetchWebhookEvents(webhookTag);
1308
+ const existing = await this.fetchWebhookEvents(webhookTag);
1279
1309
  existing === null || existing === void 0 ? void 0 : existing.map((event) => {
1280
1310
  if (event.selector === data.selector && event.selectorValue === current && event.tag !== tag) {
1281
1311
  throw new Error(`Selector ${data.selector} with value ${current} already exists on event ${event.tag}`);
@@ -1293,26 +1323,26 @@ class AppBuilderService {
1293
1323
  webhookTag, component: types_1.AppComponents.WEBHOOK_EVENT, action: types_1.RequestAction.UPDATE });
1294
1324
  // Update product and reinitialize
1295
1325
  await this.appApi.updateApp(this.app_id, payload, this.getUserAccess());
1296
- await this.initializeApp(this.app_id);
1326
+ //await this.initializeApp(this.app_id);
1297
1327
  }
1298
1328
  catch (error) {
1299
1329
  throw error;
1300
1330
  }
1301
1331
  }
1302
- fetchWebhookEvent(tag) {
1332
+ async fetchWebhookEvent(tag) {
1303
1333
  const [webhookTag, eventTag] = tag.split(':');
1304
1334
  ;
1305
1335
  if (!webhookTag || !eventTag) {
1306
1336
  throw new Error(`webhook events should be in the format webhook_tag:event_tag`);
1307
1337
  }
1308
- const events = this.fetchWebhookEvents(webhookTag);
1338
+ const events = await this.fetchWebhookEvents(webhookTag);
1309
1339
  const event = events.find((event) => {
1310
1340
  return event.tag === eventTag;
1311
1341
  });
1312
1342
  return event;
1313
1343
  }
1314
- fetchWebhookEvents(tag) {
1315
- const webhook = this.fetchWebhook(tag);
1344
+ async fetchWebhookEvents(tag) {
1345
+ const webhook = await this.fetchWebhook(tag);
1316
1346
  if (!webhook) {
1317
1347
  throw new Error(`Webhook ${tag} does not exist`);
1318
1348
  }