@plasmicpkgs/commerce 0.0.1
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/README.md +0 -0
- package/dist/auth/use-login.d.ts +6 -0
- package/dist/auth/use-login.js +9 -0
- package/dist/auth/use-logout.d.ts +6 -0
- package/dist/auth/use-logout.js +9 -0
- package/dist/auth/use-signup.d.ts +6 -0
- package/dist/auth/use-signup.js +9 -0
- package/dist/cart/use-add-item.d.ts +6 -0
- package/dist/cart/use-add-item.js +9 -0
- package/dist/cart/use-cart.d.ts +6 -0
- package/dist/cart/use-cart.js +18 -0
- package/dist/cart/use-remove-item.d.ts +6 -0
- package/dist/cart/use-remove-item.js +9 -0
- package/dist/cart/use-update-item.d.ts +6 -0
- package/dist/cart/use-update-item.js +9 -0
- package/dist/checkout/use-checkout.d.ts +6 -0
- package/dist/checkout/use-checkout.js +18 -0
- package/dist/checkout/use-submit-checkout.d.ts +6 -0
- package/dist/checkout/use-submit-checkout.js +9 -0
- package/dist/contexts.d.ts +7 -0
- package/dist/contexts.js +46 -0
- package/dist/customer/address/use-add-item.d.ts +6 -0
- package/dist/customer/address/use-add-item.js +9 -0
- package/dist/customer/address/use-addresses.d.ts +6 -0
- package/dist/customer/address/use-addresses.js +18 -0
- package/dist/customer/address/use-remove-item.d.ts +6 -0
- package/dist/customer/address/use-remove-item.js +9 -0
- package/dist/customer/address/use-update-item.d.ts +6 -0
- package/dist/customer/address/use-update-item.js +9 -0
- package/dist/customer/card/use-add-item.d.ts +6 -0
- package/dist/customer/card/use-add-item.js +9 -0
- package/dist/customer/card/use-cards.d.ts +6 -0
- package/dist/customer/card/use-cards.js +18 -0
- package/dist/customer/card/use-remove-item.d.ts +6 -0
- package/dist/customer/card/use-remove-item.js +9 -0
- package/dist/customer/card/use-update-item.d.ts +6 -0
- package/dist/customer/card/use-update-item.js +9 -0
- package/dist/customer/use-customer.d.ts +6 -0
- package/dist/customer/use-customer.js +9 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +29 -0
- package/dist/product/use-price.d.ts +26 -0
- package/dist/product/use-price.js +33 -0
- package/dist/product/use-search.d.ts +6 -0
- package/dist/product/use-search.js +9 -0
- package/dist/registerProductCollection.d.ts +11 -0
- package/dist/registerProductCollection.js +42 -0
- package/dist/registerProductImage.d.ts +13 -0
- package/dist/registerProductImage.js +37 -0
- package/dist/registerable.d.ts +4 -0
- package/dist/registerable.js +0 -0
- package/dist/tsconfig.tsbuildinfo +2970 -0
- package/dist/types/cart.d.ts +157 -0
- package/dist/types/cart.js +0 -0
- package/dist/types/checkout.d.ts +57 -0
- package/dist/types/checkout.js +0 -0
- package/dist/types/common.d.ts +13 -0
- package/dist/types/common.js +0 -0
- package/dist/types/customer/address.d.ts +110 -0
- package/dist/types/customer/address.js +0 -0
- package/dist/types/customer/card.d.ts +113 -0
- package/dist/types/customer/card.js +0 -0
- package/dist/types/customer/index.d.ts +24 -0
- package/dist/types/customer/index.js +2 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/index.js +11 -0
- package/dist/types/login.d.ts +27 -0
- package/dist/types/login.js +0 -0
- package/dist/types/logout.d.ts +17 -0
- package/dist/types/logout.js +0 -0
- package/dist/types/page.d.ts +24 -0
- package/dist/types/page.js +0 -0
- package/dist/types/product.d.ts +99 -0
- package/dist/types/product.js +0 -0
- package/dist/types/signup.d.ts +23 -0
- package/dist/types/signup.js +0 -0
- package/dist/types/site.d.ts +17 -0
- package/dist/types/site.js +0 -0
- package/dist/types/wishlist.d.ts +83 -0
- package/dist/types/wishlist.js +0 -0
- package/dist/utils/default-fetcher.d.ts +4 -0
- package/dist/utils/default-fetcher.js +3 -0
- package/dist/utils/define-property.d.ts +21 -0
- package/dist/utils/define-property.js +4 -0
- package/dist/utils/errors.d.ts +27 -0
- package/dist/utils/errors.js +24 -0
- package/dist/utils/types.d.ts +94 -0
- package/dist/utils/types.js +0 -0
- package/dist/utils/use-data.d.ts +12 -0
- package/dist/utils/use-data.js +43 -0
- package/dist/utils/use-hook.d.ts +6 -0
- package/dist/utils/use-hook.js +37 -0
- package/package.json +42 -0
package/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { MutationHook, HookFetcherFn } from '../utils/types';
|
|
2
|
+
import type { LoginHook } from '../types/login';
|
|
3
|
+
export declare type UseLogin<H extends MutationHook<LoginHook<any>> = MutationHook<LoginHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<LoginHook>;
|
|
5
|
+
declare const useLogin: UseLogin;
|
|
6
|
+
export default useLogin;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.auth?.useLogin;
|
|
5
|
+
const useLogin = (...args) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(...args);
|
|
8
|
+
};
|
|
9
|
+
export default useLogin;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../utils/types';
|
|
2
|
+
import type { LogoutHook } from '../types/logout';
|
|
3
|
+
export declare type UseLogout<H extends MutationHook<LogoutHook<any>> = MutationHook<LogoutHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<LogoutHook>;
|
|
5
|
+
declare const useLogout: UseLogout;
|
|
6
|
+
export default useLogout;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.auth?.useLogout;
|
|
5
|
+
const useLogout = (...args) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(...args);
|
|
8
|
+
};
|
|
9
|
+
export default useLogout;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../utils/types';
|
|
2
|
+
import type { SignupHook } from '../types/signup';
|
|
3
|
+
export declare type UseSignup<H extends MutationHook<SignupHook<any>> = MutationHook<SignupHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<SignupHook>;
|
|
5
|
+
declare const useSignup: UseSignup;
|
|
6
|
+
export default useSignup;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.auth?.useSignup;
|
|
5
|
+
const useSignup = (...args) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(...args);
|
|
8
|
+
};
|
|
9
|
+
export default useSignup;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../utils/types';
|
|
2
|
+
import type { AddItemHook } from '../types/cart';
|
|
3
|
+
export declare type UseAddItem<H extends MutationHook<AddItemHook<any>> = MutationHook<AddItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<AddItemHook>;
|
|
5
|
+
declare const useAddItem: UseAddItem;
|
|
6
|
+
export default useAddItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.cart?.useAddItem;
|
|
5
|
+
const useAddItem = (...args) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(...args);
|
|
8
|
+
};
|
|
9
|
+
export default useAddItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SWRHook, HookFetcherFn } from '../utils/types';
|
|
2
|
+
import type { GetCartHook } from '../types/cart';
|
|
3
|
+
export declare type UseCart<H extends SWRHook<GetCartHook<any>> = SWRHook<GetCartHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<GetCartHook>;
|
|
5
|
+
declare const useCart: UseCart;
|
|
6
|
+
export default useCart;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Cookies from 'js-cookie';
|
|
2
|
+
import { useHook, useSWRHook } from '../utils/use-hook';
|
|
3
|
+
import { useCommerce } from '..';
|
|
4
|
+
export const fetcher = async ({ options, input: { cartId }, fetch, }) => {
|
|
5
|
+
return cartId ? await fetch(options) : null;
|
|
6
|
+
};
|
|
7
|
+
const fn = (provider) => provider.cart?.useCart;
|
|
8
|
+
const useCart = (input) => {
|
|
9
|
+
const hook = useHook(fn);
|
|
10
|
+
const { cartCookie } = useCommerce();
|
|
11
|
+
const fetcherFn = hook.fetcher ?? fetcher;
|
|
12
|
+
const wrapper = (context) => {
|
|
13
|
+
context.input.cartId = Cookies.get(cartCookie);
|
|
14
|
+
return fetcherFn(context);
|
|
15
|
+
};
|
|
16
|
+
return useSWRHook({ ...hook, fetcher: wrapper })(input);
|
|
17
|
+
};
|
|
18
|
+
export default useCart;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../utils/types';
|
|
2
|
+
import type { RemoveItemHook } from '../types/cart';
|
|
3
|
+
export declare type UseRemoveItem<H extends MutationHook<RemoveItemHook<any>> = MutationHook<RemoveItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<RemoveItemHook>;
|
|
5
|
+
declare const useRemoveItem: UseRemoveItem;
|
|
6
|
+
export default useRemoveItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.cart?.useRemoveItem;
|
|
5
|
+
const useRemoveItem = (input) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(input);
|
|
8
|
+
};
|
|
9
|
+
export default useRemoveItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../utils/types';
|
|
2
|
+
import type { UpdateItemHook } from '../types/cart';
|
|
3
|
+
export declare type UseUpdateItem<H extends MutationHook<UpdateItemHook<any>> = MutationHook<UpdateItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<UpdateItemHook>;
|
|
5
|
+
declare const useUpdateItem: UseUpdateItem;
|
|
6
|
+
export default useUpdateItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.cart?.useUpdateItem;
|
|
5
|
+
const useUpdateItem = (input) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(input);
|
|
8
|
+
};
|
|
9
|
+
export default useUpdateItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SWRHook, HookFetcherFn } from '../utils/types';
|
|
2
|
+
import type { GetCheckoutHook } from '../types/checkout';
|
|
3
|
+
export declare type UseCheckout<H extends SWRHook<GetCheckoutHook<any>> = SWRHook<GetCheckoutHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<GetCheckoutHook>;
|
|
5
|
+
declare const useCheckout: UseCheckout;
|
|
6
|
+
export default useCheckout;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Cookies from 'js-cookie';
|
|
2
|
+
import { useHook, useSWRHook } from '../utils/use-hook';
|
|
3
|
+
import { useCommerce } from '..';
|
|
4
|
+
export const fetcher = async ({ options, input: { cartId }, fetch, }) => {
|
|
5
|
+
return cartId ? await fetch(options) : null;
|
|
6
|
+
};
|
|
7
|
+
const fn = (provider) => provider.checkout?.useCheckout;
|
|
8
|
+
const useCheckout = (input) => {
|
|
9
|
+
const hook = useHook(fn);
|
|
10
|
+
const { cartCookie } = useCommerce();
|
|
11
|
+
const fetcherFn = hook.fetcher ?? fetcher;
|
|
12
|
+
const wrapper = (context) => {
|
|
13
|
+
context.input.cartId = Cookies.get(cartCookie);
|
|
14
|
+
return fetcherFn(context);
|
|
15
|
+
};
|
|
16
|
+
return useSWRHook({ ...hook, fetcher: wrapper })(input);
|
|
17
|
+
};
|
|
18
|
+
export default useCheckout;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../utils/types';
|
|
2
|
+
import type { SubmitCheckoutHook } from '../types/checkout';
|
|
3
|
+
export declare type UseSubmitCheckout<H extends MutationHook<SubmitCheckoutHook<any>> = MutationHook<SubmitCheckoutHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<SubmitCheckoutHook>;
|
|
5
|
+
declare const useSubmitCheckout: UseSubmitCheckout;
|
|
6
|
+
export default useSubmitCheckout;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.checkout?.useSubmitCheckout;
|
|
5
|
+
const useSubmitCheckout = (...args) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(...args);
|
|
8
|
+
};
|
|
9
|
+
export default useSubmitCheckout;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Product } from "./types/product";
|
|
3
|
+
export declare const ProductContext: React.Context<Product | undefined>;
|
|
4
|
+
export declare function useProduct(_useProductPlaceholder?: boolean): Product | undefined;
|
|
5
|
+
export declare const ProductCollectionContext: React.Context<Product[] | undefined>;
|
|
6
|
+
export declare function useProductCollection(_useProductCollectionPlaceholder?: boolean): Product[] | undefined;
|
|
7
|
+
export declare const CommerceProviderContext: React.Context<any>;
|
package/dist/contexts.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import React, { useContext } from "react";
|
|
2
|
+
export const ProductContext = React.createContext(undefined);
|
|
3
|
+
function useProductPlaceholder() {
|
|
4
|
+
const [product, setProduct] = React.useState();
|
|
5
|
+
React.useEffect(() => {
|
|
6
|
+
(async () => {
|
|
7
|
+
const response = await fetch(`/api/catalog/products`);
|
|
8
|
+
const { products } = await response.json();
|
|
9
|
+
setProduct(products[2]);
|
|
10
|
+
})();
|
|
11
|
+
}, []);
|
|
12
|
+
return product;
|
|
13
|
+
}
|
|
14
|
+
export function useProduct(_useProductPlaceholder) {
|
|
15
|
+
const product = useContext(ProductContext);
|
|
16
|
+
if (product) {
|
|
17
|
+
return product;
|
|
18
|
+
}
|
|
19
|
+
if (_useProductPlaceholder) {
|
|
20
|
+
return useProductPlaceholder();
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
export const ProductCollectionContext = React.createContext(undefined);
|
|
25
|
+
function useProductCollectionPlaceholder() {
|
|
26
|
+
const [collection, setCollection] = React.useState();
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
(async () => {
|
|
29
|
+
const response = await fetch(`/api/catalog/products`);
|
|
30
|
+
const { products } = await response.json();
|
|
31
|
+
setCollection(products);
|
|
32
|
+
})();
|
|
33
|
+
}, []);
|
|
34
|
+
return collection;
|
|
35
|
+
}
|
|
36
|
+
export function useProductCollection(_useProductCollectionPlaceholder) {
|
|
37
|
+
const collection = useContext(ProductCollectionContext);
|
|
38
|
+
if (collection) {
|
|
39
|
+
return collection;
|
|
40
|
+
}
|
|
41
|
+
if (_useProductCollectionPlaceholder) {
|
|
42
|
+
return useProductCollectionPlaceholder();
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
export const CommerceProviderContext = React.createContext(undefined);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../../utils/types';
|
|
2
|
+
import type { AddItemHook } from '../../types/customer/address';
|
|
3
|
+
export declare type UseAddItem<H extends MutationHook<AddItemHook<any>> = MutationHook<AddItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<AddItemHook>;
|
|
5
|
+
declare const useAddItem: UseAddItem;
|
|
6
|
+
export default useAddItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.customer?.address?.useAddItem;
|
|
5
|
+
const useAddItem = (...args) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(...args);
|
|
8
|
+
};
|
|
9
|
+
export default useAddItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SWRHook, HookFetcherFn } from '../../utils/types';
|
|
2
|
+
import type { GetAddressesHook } from '../../types/customer/address';
|
|
3
|
+
export declare type UseAddresses<H extends SWRHook<GetAddressesHook<any>> = SWRHook<GetAddressesHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<GetAddressesHook>;
|
|
5
|
+
declare const useAddresses: UseAddresses;
|
|
6
|
+
export default useAddresses;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Cookies from 'js-cookie';
|
|
2
|
+
import { useHook, useSWRHook } from '../../utils/use-hook';
|
|
3
|
+
import { useCommerce } from '../..';
|
|
4
|
+
export const fetcher = async ({ options, input: { cartId }, fetch, }) => {
|
|
5
|
+
return cartId ? await fetch(options) : null;
|
|
6
|
+
};
|
|
7
|
+
const fn = (provider) => provider.customer?.address?.useAddresses;
|
|
8
|
+
const useAddresses = (input) => {
|
|
9
|
+
const hook = useHook(fn);
|
|
10
|
+
const { cartCookie } = useCommerce();
|
|
11
|
+
const fetcherFn = hook.fetcher ?? fetcher;
|
|
12
|
+
const wrapper = (context) => {
|
|
13
|
+
context.input.cartId = Cookies.get(cartCookie);
|
|
14
|
+
return fetcherFn(context);
|
|
15
|
+
};
|
|
16
|
+
return useSWRHook({ ...hook, fetcher: wrapper })(input);
|
|
17
|
+
};
|
|
18
|
+
export default useAddresses;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../../utils/types';
|
|
2
|
+
import type { RemoveItemHook } from '../../types/customer/address';
|
|
3
|
+
export declare type UseRemoveItem<H extends MutationHook<RemoveItemHook<any>> = MutationHook<RemoveItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<RemoveItemHook>;
|
|
5
|
+
declare const useRemoveItem: UseRemoveItem;
|
|
6
|
+
export default useRemoveItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.customer?.address?.useRemoveItem;
|
|
5
|
+
const useRemoveItem = (input) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(input);
|
|
8
|
+
};
|
|
9
|
+
export default useRemoveItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../../utils/types';
|
|
2
|
+
import type { UpdateItemHook } from '../../types/customer/address';
|
|
3
|
+
export declare type UseUpdateItem<H extends MutationHook<UpdateItemHook<any>> = MutationHook<UpdateItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<UpdateItemHook>;
|
|
5
|
+
declare const useUpdateItem: UseUpdateItem;
|
|
6
|
+
export default useUpdateItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.customer?.address?.useUpdateItem;
|
|
5
|
+
const useUpdateItem = (input) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(input);
|
|
8
|
+
};
|
|
9
|
+
export default useUpdateItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../../utils/types';
|
|
2
|
+
import type { AddItemHook } from '../../types/customer/card';
|
|
3
|
+
export declare type UseAddItem<H extends MutationHook<AddItemHook<any>> = MutationHook<AddItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<AddItemHook>;
|
|
5
|
+
declare const useAddItem: UseAddItem;
|
|
6
|
+
export default useAddItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.customer?.card?.useAddItem;
|
|
5
|
+
const useAddItem = (...args) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(...args);
|
|
8
|
+
};
|
|
9
|
+
export default useAddItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SWRHook, HookFetcherFn } from '../../utils/types';
|
|
2
|
+
import type { GetCardsHook } from '../../types/customer/card';
|
|
3
|
+
export declare type UseCards<H extends SWRHook<GetCardsHook<any>> = SWRHook<GetCardsHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<GetCardsHook>;
|
|
5
|
+
declare const useCards: UseCards;
|
|
6
|
+
export default useCards;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Cookies from 'js-cookie';
|
|
2
|
+
import { useHook, useSWRHook } from '../../utils/use-hook';
|
|
3
|
+
import { useCommerce } from '../..';
|
|
4
|
+
export const fetcher = async ({ options, input: { cartId }, fetch, }) => {
|
|
5
|
+
return cartId ? await fetch(options) : null;
|
|
6
|
+
};
|
|
7
|
+
const fn = (provider) => provider.customer?.card?.useCards;
|
|
8
|
+
const useCards = (input) => {
|
|
9
|
+
const hook = useHook(fn);
|
|
10
|
+
const { cartCookie } = useCommerce();
|
|
11
|
+
const fetcherFn = hook.fetcher ?? fetcher;
|
|
12
|
+
const wrapper = (context) => {
|
|
13
|
+
context.input.cartId = Cookies.get(cartCookie);
|
|
14
|
+
return fetcherFn(context);
|
|
15
|
+
};
|
|
16
|
+
return useSWRHook({ ...hook, fetcher: wrapper })(input);
|
|
17
|
+
};
|
|
18
|
+
export default useCards;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../../utils/types';
|
|
2
|
+
import type { RemoveItemHook } from '../../types/customer/card';
|
|
3
|
+
export declare type UseRemoveItem<H extends MutationHook<RemoveItemHook<any>> = MutationHook<RemoveItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<RemoveItemHook>;
|
|
5
|
+
declare const useRemoveItem: UseRemoveItem;
|
|
6
|
+
export default useRemoveItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider.customer?.card?.useRemoveItem;
|
|
5
|
+
const useRemoveItem = (input) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(input);
|
|
8
|
+
};
|
|
9
|
+
export default useRemoveItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, MutationHook } from '../../utils/types';
|
|
2
|
+
import type { UpdateItemHook } from '../../types/customer/card';
|
|
3
|
+
export declare type UseUpdateItem<H extends MutationHook<UpdateItemHook<any>> = MutationHook<UpdateItemHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<UpdateItemHook>;
|
|
5
|
+
declare const useUpdateItem: UseUpdateItem;
|
|
6
|
+
export default useUpdateItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useMutationHook } from '../../utils/use-hook';
|
|
2
|
+
import { mutationFetcher } from '../../utils/default-fetcher';
|
|
3
|
+
export const fetcher = mutationFetcher;
|
|
4
|
+
const fn = (provider) => provider?.customer?.card?.useUpdateItem;
|
|
5
|
+
const useUpdateItem = (input) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useMutationHook({ fetcher, ...hook })(input);
|
|
8
|
+
};
|
|
9
|
+
export default useUpdateItem;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CustomerHook } from '../types/customer';
|
|
2
|
+
import type { HookFetcherFn, SWRHook } from '../utils/types';
|
|
3
|
+
export declare type UseCustomer<H extends SWRHook<CustomerHook<any>> = SWRHook<CustomerHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<CustomerHook>;
|
|
5
|
+
declare const useCustomer: UseCustomer;
|
|
6
|
+
export default useCustomer;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useSWRHook } from '../utils/use-hook';
|
|
2
|
+
import { SWRFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = SWRFetcher;
|
|
4
|
+
const fn = (provider) => provider.customer?.useCustomer;
|
|
5
|
+
const useCustomer = (input) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useSWRHook({ fetcher, ...hook })(input);
|
|
8
|
+
};
|
|
9
|
+
export default useCustomer;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ReactNode, MutableRefObject } from 'react';
|
|
2
|
+
import type { Customer, Wishlist, Cart, Product, Signup, Login, Logout, Checkout } from './types';
|
|
3
|
+
import type { Fetcher, SWRHook, MutationHook } from './utils/types';
|
|
4
|
+
import { Registerable } from "./registerable";
|
|
5
|
+
export * from "./registerable";
|
|
6
|
+
export * from "./registerProductImage";
|
|
7
|
+
export * from './utils/types';
|
|
8
|
+
export declare type Provider = CommerceConfig & {
|
|
9
|
+
fetcher: Fetcher;
|
|
10
|
+
cart?: {
|
|
11
|
+
useCart?: SWRHook<Cart.GetCartHook>;
|
|
12
|
+
useAddItem?: MutationHook<Cart.AddItemHook>;
|
|
13
|
+
useUpdateItem?: MutationHook<Cart.UpdateItemHook>;
|
|
14
|
+
useRemoveItem?: MutationHook<Cart.RemoveItemHook>;
|
|
15
|
+
};
|
|
16
|
+
checkout?: {
|
|
17
|
+
useCheckout?: SWRHook<Checkout.GetCheckoutHook>;
|
|
18
|
+
useSubmitCheckout?: MutationHook<Checkout.SubmitCheckoutHook>;
|
|
19
|
+
};
|
|
20
|
+
wishlist?: {
|
|
21
|
+
useWishlist?: SWRHook<Wishlist.GetWishlistHook>;
|
|
22
|
+
useAddItem?: MutationHook<Wishlist.AddItemHook>;
|
|
23
|
+
useRemoveItem?: MutationHook<Wishlist.RemoveItemHook>;
|
|
24
|
+
};
|
|
25
|
+
customer?: {
|
|
26
|
+
useCustomer?: SWRHook<Customer.CustomerHook>;
|
|
27
|
+
card?: {
|
|
28
|
+
useCards?: SWRHook<Customer.Card.GetCardsHook>;
|
|
29
|
+
useAddItem?: MutationHook<Customer.Card.AddItemHook>;
|
|
30
|
+
useUpdateItem?: MutationHook<Customer.Card.UpdateItemHook>;
|
|
31
|
+
useRemoveItem?: MutationHook<Customer.Card.RemoveItemHook>;
|
|
32
|
+
};
|
|
33
|
+
address?: {
|
|
34
|
+
useAddresses?: SWRHook<Customer.Address.GetAddressesHook>;
|
|
35
|
+
useAddItem?: MutationHook<Customer.Address.AddItemHook>;
|
|
36
|
+
useUpdateItem?: MutationHook<Customer.Address.UpdateItemHook>;
|
|
37
|
+
useRemoveItem?: MutationHook<Customer.Address.RemoveItemHook>;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
products?: {
|
|
41
|
+
useSearch?: SWRHook<Product.SearchProductsHook>;
|
|
42
|
+
};
|
|
43
|
+
auth?: {
|
|
44
|
+
useSignup?: MutationHook<Signup.SignupHook>;
|
|
45
|
+
useLogin?: MutationHook<Login.LoginHook>;
|
|
46
|
+
useLogout?: MutationHook<Logout.LogoutHook>;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
export declare type CommerceConfig = {
|
|
50
|
+
locale: string;
|
|
51
|
+
cartCookie: string;
|
|
52
|
+
};
|
|
53
|
+
export declare type CommerceContextValue<P extends Provider> = {
|
|
54
|
+
providerRef: MutableRefObject<P>;
|
|
55
|
+
fetcherRef: MutableRefObject<Fetcher>;
|
|
56
|
+
} & CommerceConfig;
|
|
57
|
+
export declare type CommerceProps<P extends Provider> = {
|
|
58
|
+
children?: ReactNode;
|
|
59
|
+
provider: P;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* These are the properties every provider should allow when implementing
|
|
63
|
+
* the core commerce provider
|
|
64
|
+
*/
|
|
65
|
+
export declare type CommerceProviderProps = {
|
|
66
|
+
children?: ReactNode;
|
|
67
|
+
} & Partial<CommerceConfig>;
|
|
68
|
+
export declare function CoreCommerceProvider<P extends Provider>({ provider, children, }: CommerceProps<P>): JSX.Element;
|
|
69
|
+
export declare function getCommerceProvider<P extends Provider>(provider: P): ({ children, ...props }: CommerceProviderProps) => JSX.Element;
|
|
70
|
+
export declare function useCommerce<P extends Provider>(): CommerceContextValue<P>;
|
|
71
|
+
export declare function registerAll(loader?: Registerable): void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import React, { createContext, useContext, useMemo, useRef, } from 'react';
|
|
2
|
+
import { registerProductCollection } from "./registerProductCollection";
|
|
3
|
+
import { registerProductImage } from "./registerProductImage";
|
|
4
|
+
export * from "./registerable";
|
|
5
|
+
export * from "./registerProductImage";
|
|
6
|
+
export * from './utils/types';
|
|
7
|
+
const Commerce = createContext({});
|
|
8
|
+
export function CoreCommerceProvider({ provider, children, }) {
|
|
9
|
+
const providerRef = useRef(provider);
|
|
10
|
+
// TODO: Remove the fetcherRef
|
|
11
|
+
const fetcherRef = useRef(provider.fetcher);
|
|
12
|
+
// If the parent re-renders this provider will re-render every
|
|
13
|
+
// consumer unless we memoize the config
|
|
14
|
+
const { locale, cartCookie } = providerRef.current;
|
|
15
|
+
const cfg = useMemo(() => ({ providerRef, fetcherRef, locale, cartCookie }), [locale, cartCookie]);
|
|
16
|
+
return React.createElement(Commerce.Provider, { value: cfg }, children);
|
|
17
|
+
}
|
|
18
|
+
export function getCommerceProvider(provider) {
|
|
19
|
+
return function CommerceProvider({ children, ...props }) {
|
|
20
|
+
return (React.createElement(CoreCommerceProvider, { provider: { ...provider, ...props } }, children));
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function useCommerce() {
|
|
24
|
+
return useContext(Commerce);
|
|
25
|
+
}
|
|
26
|
+
export function registerAll(loader) {
|
|
27
|
+
registerProductCollection(loader);
|
|
28
|
+
registerProductImage(loader);
|
|
29
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare function formatPrice({ amount, currencyCode, locale, }: {
|
|
2
|
+
amount: number;
|
|
3
|
+
currencyCode: string;
|
|
4
|
+
locale: string;
|
|
5
|
+
}): string;
|
|
6
|
+
export declare function formatVariantPrice({ amount, baseAmount, currencyCode, locale, }: {
|
|
7
|
+
baseAmount: number;
|
|
8
|
+
amount: number;
|
|
9
|
+
currencyCode: string;
|
|
10
|
+
locale: string;
|
|
11
|
+
}): {
|
|
12
|
+
price: string;
|
|
13
|
+
basePrice: string | null;
|
|
14
|
+
discount: string | null;
|
|
15
|
+
};
|
|
16
|
+
export default function usePrice(data?: {
|
|
17
|
+
amount: number;
|
|
18
|
+
baseAmount?: number;
|
|
19
|
+
currencyCode: string;
|
|
20
|
+
} | null): {
|
|
21
|
+
price: string;
|
|
22
|
+
basePrice: string | null;
|
|
23
|
+
discount: string | null;
|
|
24
|
+
} | {
|
|
25
|
+
price: string;
|
|
26
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useCommerce } from '..';
|
|
3
|
+
export function formatPrice({ amount, currencyCode, locale, }) {
|
|
4
|
+
const formatCurrency = new Intl.NumberFormat(locale, {
|
|
5
|
+
style: 'currency',
|
|
6
|
+
currency: currencyCode,
|
|
7
|
+
});
|
|
8
|
+
return formatCurrency.format(amount);
|
|
9
|
+
}
|
|
10
|
+
export function formatVariantPrice({ amount, baseAmount, currencyCode, locale, }) {
|
|
11
|
+
const hasDiscount = baseAmount > amount;
|
|
12
|
+
const formatDiscount = new Intl.NumberFormat(locale, { style: 'percent' });
|
|
13
|
+
const discount = hasDiscount
|
|
14
|
+
? formatDiscount.format((baseAmount - amount) / baseAmount)
|
|
15
|
+
: null;
|
|
16
|
+
const price = formatPrice({ amount, currencyCode, locale });
|
|
17
|
+
const basePrice = hasDiscount
|
|
18
|
+
? formatPrice({ amount: baseAmount, currencyCode, locale })
|
|
19
|
+
: null;
|
|
20
|
+
return { price, basePrice, discount };
|
|
21
|
+
}
|
|
22
|
+
export default function usePrice(data) {
|
|
23
|
+
const { amount, baseAmount, currencyCode } = data ?? {};
|
|
24
|
+
const { locale } = useCommerce();
|
|
25
|
+
const value = useMemo(() => {
|
|
26
|
+
if (typeof amount !== 'number' || !currencyCode)
|
|
27
|
+
return '';
|
|
28
|
+
return baseAmount
|
|
29
|
+
? formatVariantPrice({ amount, baseAmount, currencyCode, locale })
|
|
30
|
+
: formatPrice({ amount, currencyCode, locale });
|
|
31
|
+
}, [amount, baseAmount, currencyCode]);
|
|
32
|
+
return typeof value === 'string' ? { price: value } : value;
|
|
33
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HookFetcherFn, SWRHook } from '../utils/types';
|
|
2
|
+
import type { SearchProductsHook } from '../types/product';
|
|
3
|
+
export declare type UseSearch<H extends SWRHook<SearchProductsHook<any>> = SWRHook<SearchProductsHook>> = ReturnType<H['useHook']>;
|
|
4
|
+
export declare const fetcher: HookFetcherFn<SearchProductsHook>;
|
|
5
|
+
declare const useSearch: UseSearch;
|
|
6
|
+
export default useSearch;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useHook, useSWRHook } from '../utils/use-hook';
|
|
2
|
+
import { SWRFetcher } from '../utils/default-fetcher';
|
|
3
|
+
export const fetcher = SWRFetcher;
|
|
4
|
+
const fn = (provider) => provider.products?.useSearch;
|
|
5
|
+
const useSearch = (input) => {
|
|
6
|
+
const hook = useHook(fn);
|
|
7
|
+
return useSWRHook({ fetcher, ...hook })(input);
|
|
8
|
+
};
|
|
9
|
+
export default useSearch;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ComponentMeta } from "@plasmicapp/host/registerComponent";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { Registerable } from "./registerable";
|
|
4
|
+
export declare const productCollectionMeta: ComponentMeta<ProductCollectionProps>;
|
|
5
|
+
interface ProductCollectionProps {
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
count: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function ProductCollection(props: ProductCollectionProps): JSX.Element;
|
|
10
|
+
export declare function registerProductCollection(loader?: Registerable, customProductCollection?: ComponentMeta<ProductCollectionProps>): void;
|
|
11
|
+
export {};
|