@makeswift/runtime 0.1.2 → 0.1.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.
package/dist/index.cjs.js CHANGED
@@ -869,29 +869,57 @@ class Document$1 extends NextDocument__default["default"] {
869
869
  }
870
870
  }
871
871
  const REVALIDATE_SECONDS = 1;
872
+ function getApiOrigin() {
873
+ var _a;
874
+ const apiOriginString = (_a = process["env"].MAKESWIFT_API_HOST) != null ? _a : "https://api.makeswift.com";
875
+ try {
876
+ const url = new URL(apiOriginString);
877
+ return url.origin;
878
+ } catch (error) {
879
+ const errorMessage = `"MAKESWIFT_API_HOST" environment variable must be a valid URL. Expected something like "https://api.makeswift.com" but instead received "${apiOriginString}".`;
880
+ throw new Error(errorMessage);
881
+ }
882
+ }
883
+ const uuidRegExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/;
884
+ function getApiKey() {
885
+ const apiKey = process["env"].MAKESWIFT_SITE_API_KEY;
886
+ if (apiKey == null) {
887
+ const errorMessage = '"MAKESWIFT_SITE_API_KEY" environment variable must be set. Please add your site API key to your `.env.local` file. More info: https://www.makeswift.com/docs/guides/manual-setup#add-environment-variables';
888
+ throw new Error(errorMessage);
889
+ }
890
+ if (!uuidRegExp.test(apiKey)) {
891
+ const errorMEssage = `Invalid Makeswift site API key "${apiKey}". Please check your \`.env.local\` file for the "MAKESWIFT_SITE_API_KEY" environment variable. More info: https://www.makeswift.com/docs/guides/manual-setup#add-environment-variables`;
892
+ throw new Error(errorMEssage);
893
+ }
894
+ return apiKey;
895
+ }
872
896
  async function getServerSideProps({
873
897
  query: {
874
898
  pageId
875
899
  }
876
900
  }) {
877
- const url = `${process["env"].MAKESWIFT_API_HOST}/v0/preview-page-data?id=${pageId}`;
901
+ const url = `${getApiOrigin()}/v0/preview-page-data?id=${pageId}`;
878
902
  const res = await fetch(url, {
879
903
  headers: {
880
- "x-api-key": process["env"].MAKESWIFT_SITE_API_KEY
904
+ "x-api-key": getApiKey()
881
905
  }
882
906
  });
883
- if (res.status !== 200) {
907
+ if (res.status === 404) {
884
908
  console.error(await res.json());
885
909
  return {
886
910
  notFound: true
887
911
  };
888
912
  }
913
+ if (!res.ok) {
914
+ const json = await res.json();
915
+ throw new Error(json.message);
916
+ }
889
917
  const page = await res.json();
890
918
  if (page == null)
891
919
  return {
892
920
  notFound: true
893
921
  };
894
- const makeswiftApiEndpoint = `${process["env"].MAKESWIFT_API_HOST}/graphql`;
922
+ const makeswiftApiEndpoint = `${getApiOrigin()}/graphql`;
895
923
  const client2 = new MakeswiftClient({
896
924
  uri: makeswiftApiEndpoint
897
925
  });
@@ -917,26 +945,30 @@ async function getStaticProps({
917
945
  const {
918
946
  path = []
919
947
  } = params;
920
- const url = `${process["env"].MAKESWIFT_API_HOST}/v0/live-page-data?path=${path.join("/")}`;
948
+ const url = `${getApiOrigin()}/v0/live-page-data?path=${path.join("/")}`;
921
949
  const res = await fetch(url, {
922
950
  headers: {
923
- "x-api-key": process["env"].MAKESWIFT_SITE_API_KEY
951
+ "x-api-key": getApiKey()
924
952
  }
925
953
  });
926
- if (res.status !== 200) {
954
+ if (res.status === 404) {
927
955
  console.error(await res.json());
928
956
  return {
929
957
  notFound: true,
930
958
  revalidate: REVALIDATE_SECONDS
931
959
  };
932
960
  }
961
+ if (!res.ok) {
962
+ const json = await res.json();
963
+ throw new Error(json.message);
964
+ }
933
965
  const page = await res.json();
934
966
  if (page == null)
935
967
  return {
936
968
  notFound: true,
937
969
  revalidate: REVALIDATE_SECONDS
938
970
  };
939
- const makeswiftApiEndpoint = `${process["env"].MAKESWIFT_API_HOST}/graphql`;
971
+ const makeswiftApiEndpoint = `${getApiOrigin()}/graphql`;
940
972
  const client2 = new MakeswiftClient({
941
973
  uri: makeswiftApiEndpoint
942
974
  });