@upsnap/strapi 1.0.7 → 1.0.9

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.
@@ -38,7 +38,7 @@ const index = {
38
38
  defaultMessage: PLUGIN_ID.slice(0, 1).toUpperCase() + PLUGIN_ID.slice(1)
39
39
  },
40
40
  Component: async () => {
41
- const { App } = await Promise.resolve().then(() => require("./App-Bo_F3q6I.js"));
41
+ const { App } = await Promise.resolve().then(() => require("./App-CEvUaGYv.js"));
42
42
  return App;
43
43
  }
44
44
  });
@@ -36,7 +36,7 @@ const index = {
36
36
  defaultMessage: PLUGIN_ID.slice(0, 1).toUpperCase() + PLUGIN_ID.slice(1)
37
37
  },
38
38
  Component: async () => {
39
- const { App } = await import("./App-DQN0F1ZK.mjs");
39
+ const { App } = await import("./App-CZcDoGMB.mjs");
40
40
  return App;
41
41
  }
42
42
  });
@@ -32,7 +32,7 @@ const service = ({ strapi }) => ({
32
32
  const settings2 = await this.settingsStore.get();
33
33
  return settings2?.token || null;
34
34
  },
35
- async makeBackendRequest(endpoint, options, forValidation = false) {
35
+ async makeBackendRequest(endpoint, options, forValidation = false, sessionToken = "") {
36
36
  const token = await this.getToken();
37
37
  if (!token && !forValidation) {
38
38
  return { error: "No token found in settings" };
@@ -40,12 +40,27 @@ const service = ({ strapi }) => ({
40
40
  const response = await fetch(`${BACKEND_URL}${endpoint}`, {
41
41
  ...options,
42
42
  headers: {
43
- Authorization: `Bearer ${token}`,
43
+ Authorization: `Bearer ${sessionToken || token}`,
44
44
  "Content-Type": "application/json",
45
45
  ...options.headers || {}
46
46
  }
47
47
  });
48
- return response.json();
48
+ const contentType = response.headers.get("content-type") || "";
49
+ if (contentType.includes("application/json")) {
50
+ return await response.json();
51
+ }
52
+ if (contentType.includes("text/csv")) {
53
+ return {
54
+ type: "csv",
55
+ data: await response.text(),
56
+ headers: response.headers
57
+ };
58
+ }
59
+ return {
60
+ type: "blob",
61
+ data: await response.arrayBuffer(),
62
+ headers: response.headers
63
+ };
49
64
  }
50
65
  });
51
66
  const settings = ({ strapi }) => ({
@@ -57,14 +72,16 @@ const settings = ({ strapi }) => ({
57
72
  };
58
73
  },
59
74
  async set(ctx) {
60
- const { token } = ctx.request.body;
61
- const isValidData = await service({ strapi }).makeBackendRequest("/tokens/validate", {
62
- method: "POST",
63
- body: JSON.stringify({ token })
64
- }, true);
65
- if (!isValidData?.data?.valid) {
66
- ctx.body = { ok: false, error: "Invalid token" };
67
- return;
75
+ const { token, logOut } = ctx.request.body;
76
+ if (!logOut) {
77
+ const isValidData = await service({ strapi }).makeBackendRequest("/tokens/validate", {
78
+ method: "POST",
79
+ body: JSON.stringify({ token })
80
+ }, true);
81
+ if (!isValidData?.data?.valid) {
82
+ ctx.body = { ok: false, error: "Invalid token" };
83
+ return;
84
+ }
68
85
  }
69
86
  const store = service({ strapi }).settingsStore;
70
87
  const current = await store.get() || {};
@@ -595,6 +612,81 @@ const monitor = ({ strapi }) => ({
595
612
  body: JSON.stringify({ ids: monitorIds, action: "delete" })
596
613
  });
597
614
  ctx.body = { monitorsData };
615
+ },
616
+ async getAllIncidents(ctx) {
617
+ const { ...params } = ctx.request.body;
618
+ const {
619
+ monitorId,
620
+ timeRange,
621
+ page,
622
+ pageSize,
623
+ checkType,
624
+ region,
625
+ search,
626
+ sortBy,
627
+ sortOrder
628
+ } = params;
629
+ if (!monitorId) {
630
+ ctx.status = 400;
631
+ ctx.body = { error: "monitorId is required" };
632
+ return;
633
+ }
634
+ const queryParams = new URLSearchParams();
635
+ queryParams.set("monitorId", monitorId);
636
+ if (timeRange) queryParams.set("time_range", timeRange);
637
+ if (page !== void 0) queryParams.set("page", page.toString());
638
+ if (pageSize !== void 0)
639
+ queryParams.set("page_size", pageSize.toString());
640
+ if (checkType) queryParams.set("check_type", checkType);
641
+ if (region) queryParams.set("region", region);
642
+ if (search) queryParams.set("search", search);
643
+ if (sortBy) queryParams.set("sort_by", sortBy);
644
+ if (sortOrder) queryParams.set("sort_order", sortOrder);
645
+ const incidentsData = await service({ strapi }).makeBackendRequest(
646
+ `/user/monitors/incidents?${queryParams.toString()}`,
647
+ {
648
+ method: "GET"
649
+ }
650
+ );
651
+ ctx.body = { incidentsData };
652
+ },
653
+ async exportIncidents(ctx) {
654
+ try {
655
+ const { monitorId, region, type, start_time, end_time, search, file_type } = ctx.request.body;
656
+ if (!monitorId) {
657
+ ctx.status = 400;
658
+ ctx.body = { error: "monitorId is required" };
659
+ return;
660
+ }
661
+ const queryParams = new URLSearchParams();
662
+ if (type) queryParams.set("type", type);
663
+ if (start_time) queryParams.set("start_time", start_time);
664
+ if (end_time) queryParams.set("end_time", end_time);
665
+ if (search) queryParams.set("search", search);
666
+ if (file_type) queryParams.set("file_type", file_type);
667
+ if (region) queryParams.set("region", region);
668
+ const response = await service({ strapi }).makeBackendRequest(
669
+ `/user/monitors/${monitorId}/incidents/export?${queryParams.toString()}`,
670
+ { method: "GET" }
671
+ );
672
+ if (!response?.type) {
673
+ ctx.body = response;
674
+ return;
675
+ }
676
+ const contentType = response.headers.get("content-type") || "application/octet-stream";
677
+ const disposition = response.headers.get("content-disposition") || `attachment; filename=incidents.${file_type === "excel" ? "xlsx" : file_type || "csv"}`;
678
+ ctx.set("Content-Type", contentType);
679
+ ctx.set("Content-Disposition", disposition);
680
+ if (response.type === "csv") {
681
+ ctx.body = response.data;
682
+ } else {
683
+ ctx.body = Buffer.from(response.data);
684
+ }
685
+ } catch (error) {
686
+ console.error("Error exporting incidents: ", error);
687
+ ctx.status = 500;
688
+ ctx.body = { error: "Failed to export incidents" };
689
+ }
598
690
  }
599
691
  });
600
692
  const statusPage = ({ strapi }) => ({
@@ -662,12 +754,212 @@ const regions = ({ strapi }) => ({
662
754
  ctx.body = { regionsData };
663
755
  }
664
756
  });
757
+ const userDetailsService = ({ strapi }) => ({
758
+ async createUserApiToken(sessionToken) {
759
+ const apiTokens = await service({ strapi }).makeBackendRequest(
760
+ `/tokens/generate`,
761
+ {
762
+ method: "POST",
763
+ body: JSON.stringify({
764
+ name: "For Strapi",
765
+ description: "Token for strapi plugin",
766
+ expires: 0
767
+ })
768
+ },
769
+ true,
770
+ sessionToken
771
+ );
772
+ if (apiTokens?.data?.token_hash) {
773
+ const token = apiTokens?.data?.token_hash;
774
+ return token;
775
+ }
776
+ return null;
777
+ },
778
+ async getUserApiToken(sessionToken) {
779
+ const apiTokens = await service({ strapi }).makeBackendRequest(
780
+ `/tokens`,
781
+ {
782
+ method: "GET"
783
+ },
784
+ true,
785
+ sessionToken
786
+ );
787
+ const tokens = apiTokens?.data?.tokens || [];
788
+ let apiToken = "";
789
+ if (apiTokens?.status === "success" && tokens.length === 0) {
790
+ apiToken = await this.createUserApiToken(sessionToken);
791
+ } else if (tokens.length > 0) {
792
+ apiToken = tokens?.[0]?.token_hash;
793
+ }
794
+ return apiToken;
795
+ },
796
+ async createInitialMonitor(site_url, apiToken) {
797
+ try {
798
+ const payload = {
799
+ name: "Default Monitor",
800
+ service_type: "website",
801
+ is_enabled: true,
802
+ channel_ids: [],
803
+ tag_ids: [],
804
+ regions: [
805
+ {
806
+ id: "default",
807
+ is_primary: true,
808
+ name: "Default (Server Region)"
809
+ }
810
+ ],
811
+ config: {
812
+ meta: {
813
+ follow_redirects: true,
814
+ timeout: 5,
815
+ url: site_url
816
+ },
817
+ services: {
818
+ broken_links: {
819
+ enabled: true,
820
+ monitor_interval: 86400
821
+ },
822
+ domain: {
823
+ enabled: true,
824
+ monitor_interval: 86400,
825
+ notify_days_before_expiry: 7
826
+ },
827
+ lighthouse: {
828
+ enabled: true,
829
+ strategy: "desktop",
830
+ monitor_interval: 604800
831
+ },
832
+ mixed_content: {
833
+ enabled: true,
834
+ monitor_interval: 86400
835
+ },
836
+ ssl: {
837
+ enabled: true,
838
+ monitor_interval: 86400,
839
+ notify_days_before_expiry: 7
840
+ },
841
+ uptime: {
842
+ enabled: true,
843
+ monitor_interval: 300
844
+ }
845
+ }
846
+ }
847
+ };
848
+ const monitorsData = await service({ strapi }).makeBackendRequest(`/user/monitors`, {
849
+ method: "POST",
850
+ body: JSON.stringify(payload)
851
+ }, true, apiToken);
852
+ if (monitorsData?.status === "success") {
853
+ const monitorId = monitorsData?.data?.monitor?.id;
854
+ return monitorId;
855
+ }
856
+ return null;
857
+ } catch (err) {
858
+ console.log("Error creating initial monitor ", err);
859
+ return null;
860
+ }
861
+ }
862
+ });
665
863
  const userDetails = ({ strapi }) => ({
666
864
  async getUserDetails(ctx) {
667
865
  const userDetailsData = await service({ strapi }).makeBackendRequest(`/user/details`, {
668
866
  method: "GET"
669
867
  });
670
868
  ctx.body = { userDetailsData };
869
+ },
870
+ async signUp(ctx) {
871
+ try {
872
+ const { email, password, source, site_url, fullName } = ctx.request.body;
873
+ const registerData = await service({ strapi }).makeBackendRequest(
874
+ `/user/register`,
875
+ {
876
+ method: "POST",
877
+ body: JSON.stringify({
878
+ email,
879
+ password,
880
+ source,
881
+ full_name: fullName
882
+ })
883
+ },
884
+ true
885
+ );
886
+ if (registerData?.data?.token) {
887
+ const apiToken = await userDetailsService({ strapi }).getUserApiToken(registerData?.data?.token);
888
+ const monitorId = await userDetailsService({ strapi }).createInitialMonitor(site_url, apiToken);
889
+ if (!apiToken || !monitorId) {
890
+ ctx.body = { ok: false, message: "Error creating API token or Monitor" };
891
+ return;
892
+ }
893
+ const store = service({ strapi }).settingsStore;
894
+ const current = await store.get() || {};
895
+ await store.set({
896
+ value: {
897
+ ...current,
898
+ token: apiToken,
899
+ primaryMonitorId: monitorId
900
+ }
901
+ });
902
+ return ctx.body = { ok: true, message: registerData?.data?.message };
903
+ }
904
+ ctx.body = { ok: false };
905
+ } catch (err) {
906
+ console.log("Error signing up ", err);
907
+ ctx.body = { ok: false };
908
+ }
909
+ },
910
+ async signIn(ctx) {
911
+ try {
912
+ const { email, password } = ctx.request.body;
913
+ const loginData = await service({ strapi }).makeBackendRequest(
914
+ `/user/login`,
915
+ {
916
+ method: "POST",
917
+ body: JSON.stringify({
918
+ email,
919
+ password
920
+ })
921
+ },
922
+ true
923
+ );
924
+ if (loginData?.data?.token) {
925
+ const apiToken = await userDetailsService({ strapi }).getUserApiToken(loginData?.data?.token);
926
+ const store = service({ strapi }).settingsStore;
927
+ const current = await store.get() || {};
928
+ await store.set({
929
+ value: {
930
+ ...current,
931
+ token: apiToken
932
+ }
933
+ });
934
+ return ctx.body = { ok: true };
935
+ }
936
+ ctx.body = { ok: false, message: loginData?.message };
937
+ } catch (err) {
938
+ console.log("Error signing in ", err);
939
+ ctx.body = { ok: false };
940
+ }
941
+ },
942
+ async forgotPassword(ctx) {
943
+ try {
944
+ const { email } = ctx.request.body;
945
+ const forgotPasswordData = await service({ strapi }).makeBackendRequest(
946
+ `/user/forgot-password`,
947
+ {
948
+ method: "POST",
949
+ body: JSON.stringify({
950
+ email
951
+ })
952
+ },
953
+ true
954
+ );
955
+ if (forgotPasswordData?.status === "success") {
956
+ return ctx.body = { ok: true, message: forgotPasswordData?.data?.message };
957
+ }
958
+ ctx.body = { ok: false };
959
+ } catch (err) {
960
+ console.log("Error for forgot password ", err);
961
+ ctx.body = { ok: false };
962
+ }
671
963
  }
672
964
  });
673
965
  const notificationChannels = ({ strapi }) => ({
@@ -1068,12 +1360,58 @@ const routes = {
1068
1360
  policies: [],
1069
1361
  auth: false
1070
1362
  }
1363
+ },
1364
+ {
1365
+ method: "POST",
1366
+ path: "/signup",
1367
+ handler: "userDetails.signUp",
1368
+ config: {
1369
+ policies: [],
1370
+ auth: false
1371
+ }
1372
+ },
1373
+ {
1374
+ method: "POST",
1375
+ path: "/login",
1376
+ handler: "userDetails.signIn",
1377
+ config: {
1378
+ policies: [],
1379
+ auth: false
1380
+ }
1381
+ },
1382
+ {
1383
+ method: "POST",
1384
+ path: "/forgot-password",
1385
+ handler: "userDetails.forgotPassword",
1386
+ config: {
1387
+ policies: [],
1388
+ auth: false
1389
+ }
1390
+ },
1391
+ {
1392
+ method: "POST",
1393
+ path: "/monitor/all-incidents",
1394
+ handler: "monitor.getAllIncidents",
1395
+ config: {
1396
+ policies: [],
1397
+ auth: false
1398
+ }
1399
+ },
1400
+ {
1401
+ method: "POST",
1402
+ path: "/monitor/incidents/export",
1403
+ handler: "monitor.exportIncidents",
1404
+ config: {
1405
+ policies: [],
1406
+ auth: false
1407
+ }
1071
1408
  }
1072
1409
  ]
1073
1410
  }
1074
1411
  };
1075
1412
  const services = {
1076
- service
1413
+ service,
1414
+ userDetailsService
1077
1415
  };
1078
1416
  const index = {
1079
1417
  register,