@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.es.js CHANGED
@@ -836,29 +836,57 @@ class Document$1 extends NextDocument {
836
836
  }
837
837
  }
838
838
  const REVALIDATE_SECONDS = 1;
839
+ function getApiOrigin() {
840
+ var _a;
841
+ const apiOriginString = (_a = process["env"].MAKESWIFT_API_HOST) != null ? _a : "https://api.makeswift.com";
842
+ try {
843
+ const url = new URL(apiOriginString);
844
+ return url.origin;
845
+ } catch (error) {
846
+ const errorMessage = `"MAKESWIFT_API_HOST" environment variable must be a valid URL. Expected something like "https://api.makeswift.com" but instead received "${apiOriginString}".`;
847
+ throw new Error(errorMessage);
848
+ }
849
+ }
850
+ 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}$/;
851
+ function getApiKey() {
852
+ const apiKey = process["env"].MAKESWIFT_SITE_API_KEY;
853
+ if (apiKey == null) {
854
+ 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';
855
+ throw new Error(errorMessage);
856
+ }
857
+ if (!uuidRegExp.test(apiKey)) {
858
+ 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`;
859
+ throw new Error(errorMEssage);
860
+ }
861
+ return apiKey;
862
+ }
839
863
  async function getServerSideProps({
840
864
  query: {
841
865
  pageId
842
866
  }
843
867
  }) {
844
- const url = `${process["env"].MAKESWIFT_API_HOST}/v0/preview-page-data?id=${pageId}`;
868
+ const url = `${getApiOrigin()}/v0/preview-page-data?id=${pageId}`;
845
869
  const res = await fetch(url, {
846
870
  headers: {
847
- "x-api-key": process["env"].MAKESWIFT_SITE_API_KEY
871
+ "x-api-key": getApiKey()
848
872
  }
849
873
  });
850
- if (res.status !== 200) {
874
+ if (res.status === 404) {
851
875
  console.error(await res.json());
852
876
  return {
853
877
  notFound: true
854
878
  };
855
879
  }
880
+ if (!res.ok) {
881
+ const json = await res.json();
882
+ throw new Error(json.message);
883
+ }
856
884
  const page = await res.json();
857
885
  if (page == null)
858
886
  return {
859
887
  notFound: true
860
888
  };
861
- const makeswiftApiEndpoint = `${process["env"].MAKESWIFT_API_HOST}/graphql`;
889
+ const makeswiftApiEndpoint = `${getApiOrigin()}/graphql`;
862
890
  const client = new MakeswiftClient({
863
891
  uri: makeswiftApiEndpoint
864
892
  });
@@ -884,26 +912,30 @@ async function getStaticProps({
884
912
  const {
885
913
  path = []
886
914
  } = params;
887
- const url = `${process["env"].MAKESWIFT_API_HOST}/v0/live-page-data?path=${path.join("/")}`;
915
+ const url = `${getApiOrigin()}/v0/live-page-data?path=${path.join("/")}`;
888
916
  const res = await fetch(url, {
889
917
  headers: {
890
- "x-api-key": process["env"].MAKESWIFT_SITE_API_KEY
918
+ "x-api-key": getApiKey()
891
919
  }
892
920
  });
893
- if (res.status !== 200) {
921
+ if (res.status === 404) {
894
922
  console.error(await res.json());
895
923
  return {
896
924
  notFound: true,
897
925
  revalidate: REVALIDATE_SECONDS
898
926
  };
899
927
  }
928
+ if (!res.ok) {
929
+ const json = await res.json();
930
+ throw new Error(json.message);
931
+ }
900
932
  const page = await res.json();
901
933
  if (page == null)
902
934
  return {
903
935
  notFound: true,
904
936
  revalidate: REVALIDATE_SECONDS
905
937
  };
906
- const makeswiftApiEndpoint = `${process["env"].MAKESWIFT_API_HOST}/graphql`;
938
+ const makeswiftApiEndpoint = `${getApiOrigin()}/graphql`;
907
939
  const client = new MakeswiftClient({
908
940
  uri: makeswiftApiEndpoint
909
941
  });