sanity-plugin-shopify-assets
2.1.02.1.1
dist/index.js~
dist/index.jsModified+16−16
Index: package/dist/index.js
===================================================================
--- package/dist/index.js
+++ package/dist/index.js
@@ -11,11 +11,11 @@
import videojs from "video.js";
import { ErrorOutlineIcon } from "@sanity/icons/ErrorOutline";
import InfiniteScroll from "react-infinite-scroll-component";
import { PhotoAlbum } from "react-photo-album";
-import { BehaviorSubject, concat, defer } from "rxjs";
-import axios from "axios";
-import { debounceTime, distinctUntilChanged, map, switchMap, withLatestFrom } from "rxjs/operators";
+import { BehaviorSubject, concat } from "rxjs";
+import { fromFetch } from "rxjs/fetch";
+import { debounceTime, switchMap, withLatestFrom } from "rxjs/operators";
import { LaunchIcon } from "@sanity/icons/Launch";
const shopifyAssetMetadataSchema = defineType({
type: "object",
name: "shopify.assetMetadata",
@@ -294,22 +294,22 @@
if (!isSchemaNode(current.type)) return;
current = current.type;
}
}
-const fetchSearch = (props) => {
+const fetchAssets = (url, token) => fromFetch(url, {
+ credentials: "include",
+ headers: token ? { Authorization: `Bearer ${token}` } : {},
+ selector: async (response) => {
+ if (response.ok) return response.json();
+ let body = await response.json().catch(() => null);
+ throw Error(body?.message || `Request failed with status code ${response.status}`);
+ }
+}), fetchSearch = (props) => {
let { projectId, dataset, shop, query, cursor, resultsPerPage, token } = props, cursorParam = cursor ? `&cursor=${encodeURIComponent(cursor)}` : "", url = `https://${projectId}.api.sanity.io/v1/shopify/assets/${encodeURIComponent(dataset)}?shop=${encodeURIComponent(shop)}&query=${encodeURIComponent(query)}${cursorParam}&limit=${resultsPerPage}`;
- return defer(() => axios.get(url, {
- withCredentials: !0,
- method: "GET",
- headers: token ? { Authorization: `Bearer ${token}` } : {}
- })).pipe(map((result) => result.data));
+ return fetchAssets(url, token);
}, fetchList = (props) => {
let { projectId, dataset, shop, cursor, resultsPerPage, token } = props, cursorParam = cursor ? `&cursor=${encodeURIComponent(cursor)}` : "", url = `https://${projectId}.api.sanity.io/v1/shopify/assets/${encodeURIComponent(dataset)}?shop=${encodeURIComponent(shop)}${cursorParam}&limit=${resultsPerPage}`;
- return defer(() => axios.get(url, {
- withCredentials: !0,
- method: "GET",
- headers: token ? { Authorization: `Bearer ${token}` } : {}
- })).pipe(map((result) => result.data));
+ return fetchAssets(url, token);
}, search = (props) => {
let { projectId, dataset, shop, query, cursor, resultsPerPage, token } = props;
return concat(query.pipe(withLatestFrom(cursor), debounceTime(500), switchMap(([q, c]) => q ? fetchSearch({
projectId,
@@ -318,9 +318,9 @@
query: q,
cursor: c,
resultsPerPage,
token
- }).pipe(distinctUntilChanged()) : fetchList({
+ }) : fetchList({
projectId,
dataset,
shop,
cursor: c,
@@ -455,9 +455,9 @@
next: (results) => {
setSearchResults((prevResults) => [...prevResults, ...results.assets]), setPageInfo(results.pageInfo), setIsLoading(!1);
},
error: (err) => {
- setApiError(`${err.response?.data?.message || err.message || "An error occurred"} - check plugin configuration`);
+ setApiError(`${err.message || "An error occurred"} - check plugin configuration`);
}
});
return () => searchSubscription.unsubscribe();
}, t6 = [