@localazy/strapi-plugin 1.0.0-beta.2 → 1.0.0-beta.3

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.
@@ -22,34 +22,6 @@ const require$$0__default = /* @__PURE__ */ _interopDefault(require$$0$2);
22
22
  const require$$0__default$1 = /* @__PURE__ */ _interopDefault(require$$0$3);
23
23
  const require$$2__default = /* @__PURE__ */ _interopDefault(require$$2);
24
24
  const require$$8__default = /* @__PURE__ */ _interopDefault(require$$8);
25
- class RequestInitiatorHelper {
26
- constructor(strapi2) {
27
- this.strapi = strapi2;
28
- this.ctx = strapi2.requestContext.get();
29
- }
30
- isInitiatedByLocalazyWebhook() {
31
- if (typeof this.ctx !== "undefined") {
32
- const ctxHeaders = this.ctx.headers;
33
- const xLocalazyHmac = ctxHeaders["x-localazy-hmac"];
34
- const xLocalazyTimestamp = ctxHeaders["x-localazy-timestamp"];
35
- if (!!xLocalazyHmac && !!xLocalazyTimestamp) {
36
- return true;
37
- }
38
- }
39
- return false;
40
- }
41
- isInitiatedByLocalazyPluginUI() {
42
- if (typeof this.ctx !== "undefined") {
43
- const ctxHeaders = this.ctx.headers;
44
- const xLocalazyInitiatedBy = ctxHeaders["x-localazy-initiated-by"];
45
- if (!xLocalazyInitiatedBy) {
46
- return false;
47
- }
48
- return xLocalazyInitiatedBy === "strapi-plugin-localazy";
49
- }
50
- return false;
51
- }
52
- }
53
25
  var objectProto$r = Object.prototype;
54
26
  function isPrototype$4(value) {
55
27
  var Ctor = value && value.constructor, proto = typeof Ctor == "function" && Ctor.prototype || objectProto$r;
@@ -839,7 +811,7 @@ var merge = createAssigner(function(object, source, srcIndex) {
839
811
  baseMerge(object, source, srcIndex);
840
812
  });
841
813
  const name = "@localazy/strapi-plugin";
842
- const version$1 = "1.0.0-beta.1";
814
+ const version$1 = "1.0.0-beta.2";
843
815
  const description = "The official Strapi Plugin by Localazy.";
844
816
  const keywords = [
845
817
  "strapi",
@@ -21680,6 +21652,31 @@ const require$$18 = {
21680
21652
  var distExports = dist$1.exports;
21681
21653
  const io = /* @__PURE__ */ getDefaultExportFromCjs(distExports);
21682
21654
  const { Server: Server2, Namespace, Socket: Socket3 } = io;
21655
+ const bootstrap = ({ strapi: strapi2 }) => {
21656
+ strapi2.db.lifecycles.subscribe(async (event) => {
21657
+ const action = event.action;
21658
+ switch (action) {
21659
+ case "beforeFindMany":
21660
+ case "beforeFindOne": {
21661
+ deepPopulateHook(event);
21662
+ break;
21663
+ }
21664
+ }
21665
+ });
21666
+ process.nextTick(async () => {
21667
+ const io2 = new Server2(strapi2.server.httpServer, {
21668
+ cors: {
21669
+ origin: "*",
21670
+ methods: ["GET", "POST"]
21671
+ },
21672
+ // this must be a constructor
21673
+ wsEngine: require$$8.WebSocketServer
21674
+ });
21675
+ strapi2.StrapIO = io2;
21676
+ });
21677
+ };
21678
+ const destroy = ({ strapi: strapi2 }) => {
21679
+ };
21683
21680
  const isoStrapiToLocalazy = (isoStrapi) => {
21684
21681
  if (!isoStrapi) {
21685
21682
  return null;
@@ -22964,41 +22961,25 @@ class PluginSettingsServiceHelper {
22964
22961
  return this.pluginSettings?.download?.uiLanguages || [];
22965
22962
  }
22966
22963
  }
22967
- const shouldProcessEntry = (contentTransferSetup, eventModel) => {
22964
+ const shouldProcessEntry = (contentTransferSetup, contextModel) => {
22968
22965
  let key;
22969
22966
  const modelContentTransferSetup = contentTransferSetup.setup.find((model) => {
22970
22967
  key = Object.keys(model)[0];
22971
- return key === eventModel;
22968
+ return key === contextModel;
22972
22969
  });
22973
22970
  if (!modelContentTransferSetup) {
22974
22971
  return void 0;
22975
22972
  }
22976
22973
  return !!modelContentTransferSetup[key].__model__;
22977
22974
  };
22978
- const getEventEntries = async (event) => {
22979
- switch (event.action) {
22980
- case "afterCreate":
22981
- case "afterUpdate": {
22982
- const entry = [
22983
- await strapi.documents(event.model.uid).findOne({
22984
- documentId: event.result.documentId,
22985
- locUploadPLevel: 6
22986
- })
22987
- ];
22988
- return entry;
22989
- }
22990
- case "beforeDelete": {
22991
- const whereId = event.params.where.id;
22992
- const entryById = await strapi.entityService.findOne(event.model.uid, whereId);
22993
- if (!entryById) {
22994
- return [];
22995
- }
22996
- if (entryById.id !== whereId) {
22997
- return [];
22998
- }
22999
- const documentId = entryById.documentId;
22975
+ const getEventEntries = async (params) => {
22976
+ const { context, documentId } = params;
22977
+ switch (context.action) {
22978
+ case "create":
22979
+ case "update":
22980
+ case "delete": {
23000
22981
  const entry = [
23001
- await strapi.documents(event.model.uid).findOne({
22982
+ await strapi.documents(context.contentType.uid).findOne({
23002
22983
  documentId,
23003
22984
  locUploadPLevel: 6
23004
22985
  })
@@ -23010,29 +22991,31 @@ const getEventEntries = async (event) => {
23010
22991
  }
23011
22992
  }
23012
22993
  };
23013
- const shouldSkipAction = async (event) => {
22994
+ const shouldSkipAction = async (context) => {
23014
22995
  const PluginSettingsServiceHelperInstance = new PluginSettingsServiceHelper();
23015
22996
  await PluginSettingsServiceHelperInstance.setup();
23016
- switch (event.action) {
23017
- case "afterCreate": {
22997
+ switch (context.action) {
22998
+ case "create": {
23018
22999
  if (!PluginSettingsServiceHelperInstance.shouldAllowAutomatedCreatedTrigger()) {
23019
23000
  strapi.log.info(
23020
- `Localazy Plugin: Skipping ${event.action} hook because automated created trigger is disabled.`
23001
+ `Localazy Plugin: Skipping ${context.action} hook because automated created trigger is disabled.`
23021
23002
  );
23022
23003
  return true;
23023
23004
  }
23024
23005
  return false;
23025
23006
  }
23026
- case "afterUpdate": {
23007
+ case "update": {
23027
23008
  if (!PluginSettingsServiceHelperInstance.shouldAllowAutomatedUpdatedTrigger()) {
23028
- strapi.log.info(`Localazy Plugin: Skipping ${event.action} hook because automated update trigger is disabled.`);
23009
+ strapi.log.info(
23010
+ `Localazy Plugin: Skipping ${context.action} hook because automated update trigger is disabled.`
23011
+ );
23029
23012
  return true;
23030
23013
  }
23031
23014
  return false;
23032
23015
  }
23033
- case "beforeDelete": {
23016
+ case "delete": {
23034
23017
  if (!PluginSettingsServiceHelperInstance.shouldAllowDeprecateOnDeletion()) {
23035
- strapi.log.info(`Localazy Plugin: Skipping ${event.action} hook because deprecate on deletion is disabled.`);
23018
+ strapi.log.info(`Localazy Plugin: Skipping ${context.action} hook because deprecate on deletion is disabled.`);
23036
23019
  return true;
23037
23020
  }
23038
23021
  return false;
@@ -23042,19 +23025,21 @@ const shouldSkipAction = async (event) => {
23042
23025
  }
23043
23026
  }
23044
23027
  };
23045
- const getPickedFlattenKeysForHookEntry = async (event) => {
23028
+ const getPickedFlattenKeysForHookEntry = async (params) => {
23029
+ const { context, locale } = params;
23030
+ const eventEntryLocale = locale;
23046
23031
  const LocalazyUserService2 = getLocalazyUserService();
23047
23032
  const LocalazyPubAPIService2 = getLocalazyPubAPIService();
23048
23033
  const StrapiI18nService2 = getStrapiI18nService();
23049
23034
  const contentTransferSetup = await getPluginSettingsService().getContentTransferSetup();
23050
- const eventAction = event.action;
23051
- strapi.log.info(`${eventAction} triggered`);
23035
+ const contextAction = context.action;
23036
+ strapi.log.info(`${contextAction} triggered`);
23052
23037
  if (!contentTransferSetup.has_setup) {
23053
23038
  strapi.log.warn("Localazy Plugin: Content Transfer Setup is not set up; the operation won't proceed");
23054
23039
  return;
23055
23040
  }
23056
- const eventModel = event.model.tableName;
23057
- const shouldProcess = shouldProcessEntry(contentTransferSetup, eventModel);
23041
+ const contextModel = context.contentType.collectionName;
23042
+ const shouldProcess = shouldProcessEntry(contentTransferSetup, contextModel);
23058
23043
  if (typeof shouldProcess === "undefined") {
23059
23044
  return;
23060
23045
  }
@@ -23068,7 +23053,7 @@ const getPickedFlattenKeysForHookEntry = async (event) => {
23068
23053
  strapi.log.error("Localazy Plugin: Localazy user is not logged in; the operation won't proceed");
23069
23054
  return;
23070
23055
  }
23071
- if (await shouldSkipAction(event)) {
23056
+ if (await shouldSkipAction(context)) {
23072
23057
  strapi.log.info("Localazy Plugin: Automated action skipped");
23073
23058
  return;
23074
23059
  }
@@ -23078,28 +23063,29 @@ const getPickedFlattenKeysForHookEntry = async (event) => {
23078
23063
  const sourceLanguage = projectLanguages.find((language3) => language3.id === projectSourceLanguageId);
23079
23064
  const projectSourceLanguageCode = sourceLanguage.code;
23080
23065
  const strapiLocales = await StrapiI18nService2.getLocales();
23081
- const defaultLocale = strapiLocales.find((locale) => locale.isDefault);
23066
+ const defaultLocale = strapiLocales.find((locale2) => locale2.isDefault);
23082
23067
  const strapiDefaultLocale = defaultLocale ? isoStrapiToLocalazy(defaultLocale.code) : config.default.LOCALAZY_DEFAULT_LOCALE;
23083
23068
  if (strapiDefaultLocale !== projectSourceLanguageCode) {
23084
23069
  strapi.log.warn("Localazy Plugin: Source locales do not match; the operation won't proceed");
23085
23070
  return;
23086
23071
  }
23087
- const modelUid = event.model.uid;
23088
- const entries = await getEventEntries(event);
23089
- if (entries.length === 0) {
23072
+ if (eventEntryLocale !== "*" && isoStrapiToLocalazy(eventEntryLocale) !== projectSourceLanguageCode) {
23073
+ strapi.log.info(
23074
+ "Localazy Plugin: Triggered locale is not in Localazy source language; the operation won't proceed"
23075
+ );
23090
23076
  return;
23091
23077
  }
23092
- const eventEntryLocale = entries[0].locale;
23093
- if (isoStrapiToLocalazy(eventEntryLocale) !== projectSourceLanguageCode) {
23094
- strapi.log.info("Localazy Plugin: Entry language is not in Localazy source language; the operation won't proceed");
23078
+ const modelUid = context.contentType.uid;
23079
+ const entries = await getEventEntries(params);
23080
+ if (entries.length === 0) {
23095
23081
  return;
23096
23082
  }
23097
23083
  const reducedEntries = omitDeep(entries, ["locale", "createdAt", "updatedAt", "publishedAt"]);
23098
- const transferSetupModel = findSetupModelByCollectionName(contentTransferSetup.setup, eventModel);
23099
- const currentTransferSetupModel = transferSetupModel[eventModel];
23084
+ const transferSetupModel = findSetupModelByCollectionName(contentTransferSetup.setup, contextModel);
23085
+ const currentTransferSetupModel = transferSetupModel[contextModel];
23100
23086
  const pickPaths = getPickPaths(currentTransferSetupModel);
23101
23087
  if (!pickPaths.length) {
23102
- strapi.log.warn(`No fields for collection ${eventModel} transfer are enabled.`);
23088
+ strapi.log.warn(`No fields for collection ${contextModel} transfer are enabled.`);
23103
23089
  return;
23104
23090
  }
23105
23091
  const pickPathsWithUid = pickPaths.map((pickPath) => `${modelUid}.${pickPath}`);
@@ -23115,11 +23101,12 @@ const getPickedFlattenKeysForHookEntry = async (event) => {
23115
23101
  const pickedFlatten = pickEntries(flatten, pickPathsWithUid);
23116
23102
  return {
23117
23103
  pickedFlatten,
23118
- eventEntryLocale
23104
+ eventEntryLocale,
23105
+ projectSourceLanguageCode
23119
23106
  };
23120
23107
  };
23121
- const uploadEventEntryToLocalazyHook = async (event) => {
23122
- const pickedFlattedResult = await getPickedFlattenKeysForHookEntry(event);
23108
+ const uploadEventEntryToLocalazyHook = async (params) => {
23109
+ const pickedFlattedResult = await getPickedFlattenKeysForHookEntry(params);
23123
23110
  if (typeof pickedFlattedResult === "undefined") {
23124
23111
  return;
23125
23112
  }
@@ -23863,12 +23850,12 @@ const LocalazyApiClientFactory = async () => {
23863
23850
  return api;
23864
23851
  };
23865
23852
  const delay = (ms2 = 150) => new Promise((resolve) => setTimeout(resolve, ms2));
23866
- const deprecateEventEntryInLocalazyHook = async (event) => {
23867
- const pickedFlattedResult = await getPickedFlattenKeysForHookEntry(event);
23853
+ const deprecateEventEntryInLocalazyHook = async (params) => {
23854
+ const pickedFlattedResult = await getPickedFlattenKeysForHookEntry(params);
23868
23855
  if (typeof pickedFlattedResult === "undefined") {
23869
23856
  return;
23870
23857
  }
23871
- const { pickedFlatten, eventEntryLocale } = pickedFlattedResult;
23858
+ const { pickedFlatten, projectSourceLanguageCode } = pickedFlattedResult;
23872
23859
  const LocalazyUserService2 = getLocalazyUserService();
23873
23860
  const LocalazyPubApiService = getLocalazyPubAPIService();
23874
23861
  const user = await LocalazyUserService2.getUser();
@@ -23881,7 +23868,7 @@ const deprecateEventEntryInLocalazyHook = async (event) => {
23881
23868
  const projectKeys = await LocalazyApi.files.listKeys({
23882
23869
  project: user.project.id,
23883
23870
  file: strapiFile.id,
23884
- lang: eventEntryLocale
23871
+ lang: projectSourceLanguageCode
23885
23872
  });
23886
23873
  if (!projectKeys) {
23887
23874
  strapi.log.error(`Keys not found for file ${strapiFile.id}`);
@@ -23907,66 +23894,51 @@ const deprecateEventEntryInLocalazyHook = async (event) => {
23907
23894
  success: true
23908
23895
  };
23909
23896
  };
23910
- const bootstrap = ({ strapi: strapi2 }) => {
23911
- strapi2.db.lifecycles.subscribe(async (event) => {
23912
- const requestInitiatorHelper = new RequestInitiatorHelper(strapi2);
23913
- const action = event.action;
23914
- switch (action) {
23915
- case "beforeFindMany":
23916
- case "beforeFindOne": {
23917
- deepPopulateHook(event);
23918
- break;
23919
- }
23920
- case "afterCreate":
23921
- case "afterUpdate": {
23897
+ const register = ({ strapi: strapi2 }) => {
23898
+ strapi2.documents.use(async (context, next) => {
23899
+ let result;
23900
+ switch (context.action) {
23901
+ case "create":
23902
+ case "update":
23922
23903
  try {
23923
- if (requestInitiatorHelper.isInitiatedByLocalazyWebhook() || requestInitiatorHelper.isInitiatedByLocalazyPluginUI()) {
23924
- break;
23925
- }
23926
- uploadEventEntryToLocalazyHook(event).then((result) => {
23927
- if (typeof result !== "undefined") {
23928
- strapi2.log.info(`${action} hook result: ${JSON.stringify(result)}`);
23929
- }
23904
+ result = await next();
23905
+ const hookResult = await uploadEventEntryToLocalazyHook({
23906
+ context,
23907
+ documentId: result.documentId,
23908
+ locale: context.params.locale || context["locale"]
23930
23909
  });
23910
+ if (typeof hookResult !== "undefined") {
23911
+ strapi2.log.info(`${context.action} hook result: ${JSON.stringify(hookResult)}`);
23912
+ }
23931
23913
  } catch (e3) {
23932
23914
  strapi2.log.error(e3);
23933
23915
  } finally {
23934
23916
  break;
23935
23917
  }
23936
- }
23937
- case "beforeDelete": {
23918
+ case "delete": {
23938
23919
  try {
23939
- if (requestInitiatorHelper.isInitiatedByLocalazyWebhook() || requestInitiatorHelper.isInitiatedByLocalazyPluginUI()) {
23940
- break;
23941
- }
23942
- const result = await deprecateEventEntryInLocalazyHook(event);
23943
- if (typeof result !== "undefined") {
23944
- strapi2.log.info(`${action} hook result: ${JSON.stringify(result)}`);
23920
+ const hookResult = await deprecateEventEntryInLocalazyHook({
23921
+ context,
23922
+ documentId: context.params.documentId,
23923
+ locale: context.params.locale || context["locale"]
23924
+ });
23925
+ if (typeof hookResult !== "undefined") {
23926
+ strapi2.log.info(`${context.action} hook result: ${JSON.stringify(hookResult)}`);
23945
23927
  }
23928
+ result = await next();
23946
23929
  } catch (e3) {
23947
23930
  strapi2.log.error(e3);
23948
23931
  } finally {
23949
23932
  break;
23950
23933
  }
23951
23934
  }
23935
+ default:
23936
+ result = await next();
23937
+ break;
23952
23938
  }
23953
- });
23954
- process.nextTick(async () => {
23955
- const io2 = new Server2(strapi2.server.httpServer, {
23956
- cors: {
23957
- origin: "*",
23958
- methods: ["GET", "POST"]
23959
- },
23960
- // this must be a constructor
23961
- wsEngine: require$$8.WebSocketServer
23962
- });
23963
- strapi2.StrapIO = io2;
23939
+ return result;
23964
23940
  });
23965
23941
  };
23966
- const destroy = ({ strapi: strapi2 }) => {
23967
- };
23968
- const register = ({ strapi: strapi2 }) => {
23969
- };
23970
23942
  const contentTypes = {};
23971
23943
  const LocalazyUserController = ({ strapi: strapi2 }) => ({
23972
23944
  async getUser(ctx) {
@@ -25132,6 +25104,34 @@ const shouldSetDownloadedProperty = (modelContentTransferSetup, parsedKeyRest) =
25132
25104
  return "no";
25133
25105
  }
25134
25106
  };
25107
+ class RequestInitiatorHelper {
25108
+ constructor(strapi2) {
25109
+ this.strapi = strapi2;
25110
+ this.ctx = strapi2.requestContext.get();
25111
+ }
25112
+ isInitiatedByLocalazyWebhook() {
25113
+ if (typeof this.ctx !== "undefined") {
25114
+ const ctxHeaders = this.ctx.headers;
25115
+ const xLocalazyHmac = ctxHeaders["x-localazy-hmac"];
25116
+ const xLocalazyTimestamp = ctxHeaders["x-localazy-timestamp"];
25117
+ if (!!xLocalazyHmac && !!xLocalazyTimestamp) {
25118
+ return true;
25119
+ }
25120
+ }
25121
+ return false;
25122
+ }
25123
+ isInitiatedByLocalazyPluginUI() {
25124
+ if (typeof this.ctx !== "undefined") {
25125
+ const ctxHeaders = this.ctx.headers;
25126
+ const xLocalazyInitiatedBy = ctxHeaders["x-localazy-initiated-by"];
25127
+ if (!xLocalazyInitiatedBy) {
25128
+ return false;
25129
+ }
25130
+ return xLocalazyInitiatedBy === "strapi-plugin-localazy";
25131
+ }
25132
+ return false;
25133
+ }
25134
+ }
25135
25135
  const getFilteredLanguagesCodesForDownload = async (languagesCodes) => {
25136
25136
  const pluginSettingsServiceHelper = new PluginSettingsServiceHelper();
25137
25137
  const requestInitiatorHelper = new RequestInitiatorHelper(strapi);
@@ -9,34 +9,6 @@ import require$$0$2 from "crypto";
9
9
  import require$$0$3 from "events";
10
10
  import require$$2 from "timers";
11
11
  import require$$8, { WebSocketServer } from "ws";
12
- class RequestInitiatorHelper {
13
- constructor(strapi2) {
14
- this.strapi = strapi2;
15
- this.ctx = strapi2.requestContext.get();
16
- }
17
- isInitiatedByLocalazyWebhook() {
18
- if (typeof this.ctx !== "undefined") {
19
- const ctxHeaders = this.ctx.headers;
20
- const xLocalazyHmac = ctxHeaders["x-localazy-hmac"];
21
- const xLocalazyTimestamp = ctxHeaders["x-localazy-timestamp"];
22
- if (!!xLocalazyHmac && !!xLocalazyTimestamp) {
23
- return true;
24
- }
25
- }
26
- return false;
27
- }
28
- isInitiatedByLocalazyPluginUI() {
29
- if (typeof this.ctx !== "undefined") {
30
- const ctxHeaders = this.ctx.headers;
31
- const xLocalazyInitiatedBy = ctxHeaders["x-localazy-initiated-by"];
32
- if (!xLocalazyInitiatedBy) {
33
- return false;
34
- }
35
- return xLocalazyInitiatedBy === "strapi-plugin-localazy";
36
- }
37
- return false;
38
- }
39
- }
40
12
  var objectProto$r = Object.prototype;
41
13
  function isPrototype$4(value) {
42
14
  var Ctor = value && value.constructor, proto = typeof Ctor == "function" && Ctor.prototype || objectProto$r;
@@ -826,7 +798,7 @@ var merge = createAssigner(function(object, source, srcIndex) {
826
798
  baseMerge(object, source, srcIndex);
827
799
  });
828
800
  const name = "@localazy/strapi-plugin";
829
- const version$1 = "1.0.0-beta.1";
801
+ const version$1 = "1.0.0-beta.2";
830
802
  const description = "The official Strapi Plugin by Localazy.";
831
803
  const keywords = [
832
804
  "strapi",
@@ -21667,6 +21639,31 @@ const require$$18 = {
21667
21639
  var distExports = dist$1.exports;
21668
21640
  const io = /* @__PURE__ */ getDefaultExportFromCjs(distExports);
21669
21641
  const { Server: Server2, Namespace, Socket: Socket3 } = io;
21642
+ const bootstrap = ({ strapi: strapi2 }) => {
21643
+ strapi2.db.lifecycles.subscribe(async (event) => {
21644
+ const action = event.action;
21645
+ switch (action) {
21646
+ case "beforeFindMany":
21647
+ case "beforeFindOne": {
21648
+ deepPopulateHook(event);
21649
+ break;
21650
+ }
21651
+ }
21652
+ });
21653
+ process.nextTick(async () => {
21654
+ const io2 = new Server2(strapi2.server.httpServer, {
21655
+ cors: {
21656
+ origin: "*",
21657
+ methods: ["GET", "POST"]
21658
+ },
21659
+ // this must be a constructor
21660
+ wsEngine: WebSocketServer
21661
+ });
21662
+ strapi2.StrapIO = io2;
21663
+ });
21664
+ };
21665
+ const destroy = ({ strapi: strapi2 }) => {
21666
+ };
21670
21667
  const isoStrapiToLocalazy = (isoStrapi) => {
21671
21668
  if (!isoStrapi) {
21672
21669
  return null;
@@ -22951,41 +22948,25 @@ class PluginSettingsServiceHelper {
22951
22948
  return this.pluginSettings?.download?.uiLanguages || [];
22952
22949
  }
22953
22950
  }
22954
- const shouldProcessEntry = (contentTransferSetup, eventModel) => {
22951
+ const shouldProcessEntry = (contentTransferSetup, contextModel) => {
22955
22952
  let key;
22956
22953
  const modelContentTransferSetup = contentTransferSetup.setup.find((model) => {
22957
22954
  key = Object.keys(model)[0];
22958
- return key === eventModel;
22955
+ return key === contextModel;
22959
22956
  });
22960
22957
  if (!modelContentTransferSetup) {
22961
22958
  return void 0;
22962
22959
  }
22963
22960
  return !!modelContentTransferSetup[key].__model__;
22964
22961
  };
22965
- const getEventEntries = async (event) => {
22966
- switch (event.action) {
22967
- case "afterCreate":
22968
- case "afterUpdate": {
22969
- const entry = [
22970
- await strapi.documents(event.model.uid).findOne({
22971
- documentId: event.result.documentId,
22972
- locUploadPLevel: 6
22973
- })
22974
- ];
22975
- return entry;
22976
- }
22977
- case "beforeDelete": {
22978
- const whereId = event.params.where.id;
22979
- const entryById = await strapi.entityService.findOne(event.model.uid, whereId);
22980
- if (!entryById) {
22981
- return [];
22982
- }
22983
- if (entryById.id !== whereId) {
22984
- return [];
22985
- }
22986
- const documentId = entryById.documentId;
22962
+ const getEventEntries = async (params) => {
22963
+ const { context, documentId } = params;
22964
+ switch (context.action) {
22965
+ case "create":
22966
+ case "update":
22967
+ case "delete": {
22987
22968
  const entry = [
22988
- await strapi.documents(event.model.uid).findOne({
22969
+ await strapi.documents(context.contentType.uid).findOne({
22989
22970
  documentId,
22990
22971
  locUploadPLevel: 6
22991
22972
  })
@@ -22997,29 +22978,31 @@ const getEventEntries = async (event) => {
22997
22978
  }
22998
22979
  }
22999
22980
  };
23000
- const shouldSkipAction = async (event) => {
22981
+ const shouldSkipAction = async (context) => {
23001
22982
  const PluginSettingsServiceHelperInstance = new PluginSettingsServiceHelper();
23002
22983
  await PluginSettingsServiceHelperInstance.setup();
23003
- switch (event.action) {
23004
- case "afterCreate": {
22984
+ switch (context.action) {
22985
+ case "create": {
23005
22986
  if (!PluginSettingsServiceHelperInstance.shouldAllowAutomatedCreatedTrigger()) {
23006
22987
  strapi.log.info(
23007
- `Localazy Plugin: Skipping ${event.action} hook because automated created trigger is disabled.`
22988
+ `Localazy Plugin: Skipping ${context.action} hook because automated created trigger is disabled.`
23008
22989
  );
23009
22990
  return true;
23010
22991
  }
23011
22992
  return false;
23012
22993
  }
23013
- case "afterUpdate": {
22994
+ case "update": {
23014
22995
  if (!PluginSettingsServiceHelperInstance.shouldAllowAutomatedUpdatedTrigger()) {
23015
- strapi.log.info(`Localazy Plugin: Skipping ${event.action} hook because automated update trigger is disabled.`);
22996
+ strapi.log.info(
22997
+ `Localazy Plugin: Skipping ${context.action} hook because automated update trigger is disabled.`
22998
+ );
23016
22999
  return true;
23017
23000
  }
23018
23001
  return false;
23019
23002
  }
23020
- case "beforeDelete": {
23003
+ case "delete": {
23021
23004
  if (!PluginSettingsServiceHelperInstance.shouldAllowDeprecateOnDeletion()) {
23022
- strapi.log.info(`Localazy Plugin: Skipping ${event.action} hook because deprecate on deletion is disabled.`);
23005
+ strapi.log.info(`Localazy Plugin: Skipping ${context.action} hook because deprecate on deletion is disabled.`);
23023
23006
  return true;
23024
23007
  }
23025
23008
  return false;
@@ -23029,19 +23012,21 @@ const shouldSkipAction = async (event) => {
23029
23012
  }
23030
23013
  }
23031
23014
  };
23032
- const getPickedFlattenKeysForHookEntry = async (event) => {
23015
+ const getPickedFlattenKeysForHookEntry = async (params) => {
23016
+ const { context, locale } = params;
23017
+ const eventEntryLocale = locale;
23033
23018
  const LocalazyUserService2 = getLocalazyUserService();
23034
23019
  const LocalazyPubAPIService2 = getLocalazyPubAPIService();
23035
23020
  const StrapiI18nService2 = getStrapiI18nService();
23036
23021
  const contentTransferSetup = await getPluginSettingsService().getContentTransferSetup();
23037
- const eventAction = event.action;
23038
- strapi.log.info(`${eventAction} triggered`);
23022
+ const contextAction = context.action;
23023
+ strapi.log.info(`${contextAction} triggered`);
23039
23024
  if (!contentTransferSetup.has_setup) {
23040
23025
  strapi.log.warn("Localazy Plugin: Content Transfer Setup is not set up; the operation won't proceed");
23041
23026
  return;
23042
23027
  }
23043
- const eventModel = event.model.tableName;
23044
- const shouldProcess = shouldProcessEntry(contentTransferSetup, eventModel);
23028
+ const contextModel = context.contentType.collectionName;
23029
+ const shouldProcess = shouldProcessEntry(contentTransferSetup, contextModel);
23045
23030
  if (typeof shouldProcess === "undefined") {
23046
23031
  return;
23047
23032
  }
@@ -23055,7 +23040,7 @@ const getPickedFlattenKeysForHookEntry = async (event) => {
23055
23040
  strapi.log.error("Localazy Plugin: Localazy user is not logged in; the operation won't proceed");
23056
23041
  return;
23057
23042
  }
23058
- if (await shouldSkipAction(event)) {
23043
+ if (await shouldSkipAction(context)) {
23059
23044
  strapi.log.info("Localazy Plugin: Automated action skipped");
23060
23045
  return;
23061
23046
  }
@@ -23065,28 +23050,29 @@ const getPickedFlattenKeysForHookEntry = async (event) => {
23065
23050
  const sourceLanguage = projectLanguages.find((language3) => language3.id === projectSourceLanguageId);
23066
23051
  const projectSourceLanguageCode = sourceLanguage.code;
23067
23052
  const strapiLocales = await StrapiI18nService2.getLocales();
23068
- const defaultLocale = strapiLocales.find((locale) => locale.isDefault);
23053
+ const defaultLocale = strapiLocales.find((locale2) => locale2.isDefault);
23069
23054
  const strapiDefaultLocale = defaultLocale ? isoStrapiToLocalazy(defaultLocale.code) : config.default.LOCALAZY_DEFAULT_LOCALE;
23070
23055
  if (strapiDefaultLocale !== projectSourceLanguageCode) {
23071
23056
  strapi.log.warn("Localazy Plugin: Source locales do not match; the operation won't proceed");
23072
23057
  return;
23073
23058
  }
23074
- const modelUid = event.model.uid;
23075
- const entries = await getEventEntries(event);
23076
- if (entries.length === 0) {
23059
+ if (eventEntryLocale !== "*" && isoStrapiToLocalazy(eventEntryLocale) !== projectSourceLanguageCode) {
23060
+ strapi.log.info(
23061
+ "Localazy Plugin: Triggered locale is not in Localazy source language; the operation won't proceed"
23062
+ );
23077
23063
  return;
23078
23064
  }
23079
- const eventEntryLocale = entries[0].locale;
23080
- if (isoStrapiToLocalazy(eventEntryLocale) !== projectSourceLanguageCode) {
23081
- strapi.log.info("Localazy Plugin: Entry language is not in Localazy source language; the operation won't proceed");
23065
+ const modelUid = context.contentType.uid;
23066
+ const entries = await getEventEntries(params);
23067
+ if (entries.length === 0) {
23082
23068
  return;
23083
23069
  }
23084
23070
  const reducedEntries = omitDeep(entries, ["locale", "createdAt", "updatedAt", "publishedAt"]);
23085
- const transferSetupModel = findSetupModelByCollectionName(contentTransferSetup.setup, eventModel);
23086
- const currentTransferSetupModel = transferSetupModel[eventModel];
23071
+ const transferSetupModel = findSetupModelByCollectionName(contentTransferSetup.setup, contextModel);
23072
+ const currentTransferSetupModel = transferSetupModel[contextModel];
23087
23073
  const pickPaths = getPickPaths(currentTransferSetupModel);
23088
23074
  if (!pickPaths.length) {
23089
- strapi.log.warn(`No fields for collection ${eventModel} transfer are enabled.`);
23075
+ strapi.log.warn(`No fields for collection ${contextModel} transfer are enabled.`);
23090
23076
  return;
23091
23077
  }
23092
23078
  const pickPathsWithUid = pickPaths.map((pickPath) => `${modelUid}.${pickPath}`);
@@ -23102,11 +23088,12 @@ const getPickedFlattenKeysForHookEntry = async (event) => {
23102
23088
  const pickedFlatten = pickEntries(flatten, pickPathsWithUid);
23103
23089
  return {
23104
23090
  pickedFlatten,
23105
- eventEntryLocale
23091
+ eventEntryLocale,
23092
+ projectSourceLanguageCode
23106
23093
  };
23107
23094
  };
23108
- const uploadEventEntryToLocalazyHook = async (event) => {
23109
- const pickedFlattedResult = await getPickedFlattenKeysForHookEntry(event);
23095
+ const uploadEventEntryToLocalazyHook = async (params) => {
23096
+ const pickedFlattedResult = await getPickedFlattenKeysForHookEntry(params);
23110
23097
  if (typeof pickedFlattedResult === "undefined") {
23111
23098
  return;
23112
23099
  }
@@ -23850,12 +23837,12 @@ const LocalazyApiClientFactory = async () => {
23850
23837
  return api;
23851
23838
  };
23852
23839
  const delay = (ms2 = 150) => new Promise((resolve) => setTimeout(resolve, ms2));
23853
- const deprecateEventEntryInLocalazyHook = async (event) => {
23854
- const pickedFlattedResult = await getPickedFlattenKeysForHookEntry(event);
23840
+ const deprecateEventEntryInLocalazyHook = async (params) => {
23841
+ const pickedFlattedResult = await getPickedFlattenKeysForHookEntry(params);
23855
23842
  if (typeof pickedFlattedResult === "undefined") {
23856
23843
  return;
23857
23844
  }
23858
- const { pickedFlatten, eventEntryLocale } = pickedFlattedResult;
23845
+ const { pickedFlatten, projectSourceLanguageCode } = pickedFlattedResult;
23859
23846
  const LocalazyUserService2 = getLocalazyUserService();
23860
23847
  const LocalazyPubApiService = getLocalazyPubAPIService();
23861
23848
  const user = await LocalazyUserService2.getUser();
@@ -23868,7 +23855,7 @@ const deprecateEventEntryInLocalazyHook = async (event) => {
23868
23855
  const projectKeys = await LocalazyApi.files.listKeys({
23869
23856
  project: user.project.id,
23870
23857
  file: strapiFile.id,
23871
- lang: eventEntryLocale
23858
+ lang: projectSourceLanguageCode
23872
23859
  });
23873
23860
  if (!projectKeys) {
23874
23861
  strapi.log.error(`Keys not found for file ${strapiFile.id}`);
@@ -23894,66 +23881,51 @@ const deprecateEventEntryInLocalazyHook = async (event) => {
23894
23881
  success: true
23895
23882
  };
23896
23883
  };
23897
- const bootstrap = ({ strapi: strapi2 }) => {
23898
- strapi2.db.lifecycles.subscribe(async (event) => {
23899
- const requestInitiatorHelper = new RequestInitiatorHelper(strapi2);
23900
- const action = event.action;
23901
- switch (action) {
23902
- case "beforeFindMany":
23903
- case "beforeFindOne": {
23904
- deepPopulateHook(event);
23905
- break;
23906
- }
23907
- case "afterCreate":
23908
- case "afterUpdate": {
23884
+ const register = ({ strapi: strapi2 }) => {
23885
+ strapi2.documents.use(async (context, next) => {
23886
+ let result;
23887
+ switch (context.action) {
23888
+ case "create":
23889
+ case "update":
23909
23890
  try {
23910
- if (requestInitiatorHelper.isInitiatedByLocalazyWebhook() || requestInitiatorHelper.isInitiatedByLocalazyPluginUI()) {
23911
- break;
23912
- }
23913
- uploadEventEntryToLocalazyHook(event).then((result) => {
23914
- if (typeof result !== "undefined") {
23915
- strapi2.log.info(`${action} hook result: ${JSON.stringify(result)}`);
23916
- }
23891
+ result = await next();
23892
+ const hookResult = await uploadEventEntryToLocalazyHook({
23893
+ context,
23894
+ documentId: result.documentId,
23895
+ locale: context.params.locale || context["locale"]
23917
23896
  });
23897
+ if (typeof hookResult !== "undefined") {
23898
+ strapi2.log.info(`${context.action} hook result: ${JSON.stringify(hookResult)}`);
23899
+ }
23918
23900
  } catch (e3) {
23919
23901
  strapi2.log.error(e3);
23920
23902
  } finally {
23921
23903
  break;
23922
23904
  }
23923
- }
23924
- case "beforeDelete": {
23905
+ case "delete": {
23925
23906
  try {
23926
- if (requestInitiatorHelper.isInitiatedByLocalazyWebhook() || requestInitiatorHelper.isInitiatedByLocalazyPluginUI()) {
23927
- break;
23928
- }
23929
- const result = await deprecateEventEntryInLocalazyHook(event);
23930
- if (typeof result !== "undefined") {
23931
- strapi2.log.info(`${action} hook result: ${JSON.stringify(result)}`);
23907
+ const hookResult = await deprecateEventEntryInLocalazyHook({
23908
+ context,
23909
+ documentId: context.params.documentId,
23910
+ locale: context.params.locale || context["locale"]
23911
+ });
23912
+ if (typeof hookResult !== "undefined") {
23913
+ strapi2.log.info(`${context.action} hook result: ${JSON.stringify(hookResult)}`);
23932
23914
  }
23915
+ result = await next();
23933
23916
  } catch (e3) {
23934
23917
  strapi2.log.error(e3);
23935
23918
  } finally {
23936
23919
  break;
23937
23920
  }
23938
23921
  }
23922
+ default:
23923
+ result = await next();
23924
+ break;
23939
23925
  }
23940
- });
23941
- process.nextTick(async () => {
23942
- const io2 = new Server2(strapi2.server.httpServer, {
23943
- cors: {
23944
- origin: "*",
23945
- methods: ["GET", "POST"]
23946
- },
23947
- // this must be a constructor
23948
- wsEngine: WebSocketServer
23949
- });
23950
- strapi2.StrapIO = io2;
23926
+ return result;
23951
23927
  });
23952
23928
  };
23953
- const destroy = ({ strapi: strapi2 }) => {
23954
- };
23955
- const register = ({ strapi: strapi2 }) => {
23956
- };
23957
23929
  const contentTypes = {};
23958
23930
  const LocalazyUserController = ({ strapi: strapi2 }) => ({
23959
23931
  async getUser(ctx) {
@@ -25119,6 +25091,34 @@ const shouldSetDownloadedProperty = (modelContentTransferSetup, parsedKeyRest) =
25119
25091
  return "no";
25120
25092
  }
25121
25093
  };
25094
+ class RequestInitiatorHelper {
25095
+ constructor(strapi2) {
25096
+ this.strapi = strapi2;
25097
+ this.ctx = strapi2.requestContext.get();
25098
+ }
25099
+ isInitiatedByLocalazyWebhook() {
25100
+ if (typeof this.ctx !== "undefined") {
25101
+ const ctxHeaders = this.ctx.headers;
25102
+ const xLocalazyHmac = ctxHeaders["x-localazy-hmac"];
25103
+ const xLocalazyTimestamp = ctxHeaders["x-localazy-timestamp"];
25104
+ if (!!xLocalazyHmac && !!xLocalazyTimestamp) {
25105
+ return true;
25106
+ }
25107
+ }
25108
+ return false;
25109
+ }
25110
+ isInitiatedByLocalazyPluginUI() {
25111
+ if (typeof this.ctx !== "undefined") {
25112
+ const ctxHeaders = this.ctx.headers;
25113
+ const xLocalazyInitiatedBy = ctxHeaders["x-localazy-initiated-by"];
25114
+ if (!xLocalazyInitiatedBy) {
25115
+ return false;
25116
+ }
25117
+ return xLocalazyInitiatedBy === "strapi-plugin-localazy";
25118
+ }
25119
+ return false;
25120
+ }
25121
+ }
25122
25122
  const getFilteredLanguagesCodesForDownload = async (languagesCodes) => {
25123
25123
  const pluginSettingsServiceHelper = new PluginSettingsServiceHelper();
25124
25124
  const requestInitiatorHelper = new RequestInitiatorHelper(strapi);
@@ -1,4 +1,5 @@
1
- declare const _default: (event: any) => Promise<{
1
+ import type { HookParams } from '../models/plugin/hook-params';
2
+ declare const _default: (params: HookParams) => Promise<{
2
3
  success: boolean;
3
4
  }>;
4
5
  export default _default;
@@ -1,4 +1,5 @@
1
- declare const _default: (event: any) => Promise<{
1
+ import type { HookParams } from '../models/plugin/hook-params';
2
+ declare const _default: (params: HookParams) => Promise<{
2
3
  success: boolean;
3
4
  result: import("@localazy/api-client").File & {
4
5
  importBatch: string;
@@ -0,0 +1,6 @@
1
+ import type { Context } from '@strapi/types/dist/modules/documents/middleware';
2
+ export type HookParams = {
3
+ context: Context;
4
+ documentId: string;
5
+ locale: string;
6
+ };
@@ -1,5 +1,7 @@
1
- declare const _default: (event: any) => Promise<{
1
+ import type { HookParams } from '../models/plugin/hook-params';
2
+ declare const _default: (params: HookParams) => Promise<{
2
3
  pickedFlatten: Record<string, any>;
3
- eventEntryLocale: any;
4
+ eventEntryLocale: string;
5
+ projectSourceLanguageCode: string;
4
6
  }>;
5
7
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@localazy/strapi-plugin",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "The official Strapi Plugin by Localazy.",
5
5
  "keywords": [
6
6
  "strapi",