@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.
Files changed (93) hide show
  1. package/README.md +0 -0
  2. package/dist/auth/use-login.d.ts +6 -0
  3. package/dist/auth/use-login.js +9 -0
  4. package/dist/auth/use-logout.d.ts +6 -0
  5. package/dist/auth/use-logout.js +9 -0
  6. package/dist/auth/use-signup.d.ts +6 -0
  7. package/dist/auth/use-signup.js +9 -0
  8. package/dist/cart/use-add-item.d.ts +6 -0
  9. package/dist/cart/use-add-item.js +9 -0
  10. package/dist/cart/use-cart.d.ts +6 -0
  11. package/dist/cart/use-cart.js +18 -0
  12. package/dist/cart/use-remove-item.d.ts +6 -0
  13. package/dist/cart/use-remove-item.js +9 -0
  14. package/dist/cart/use-update-item.d.ts +6 -0
  15. package/dist/cart/use-update-item.js +9 -0
  16. package/dist/checkout/use-checkout.d.ts +6 -0
  17. package/dist/checkout/use-checkout.js +18 -0
  18. package/dist/checkout/use-submit-checkout.d.ts +6 -0
  19. package/dist/checkout/use-submit-checkout.js +9 -0
  20. package/dist/contexts.d.ts +7 -0
  21. package/dist/contexts.js +46 -0
  22. package/dist/customer/address/use-add-item.d.ts +6 -0
  23. package/dist/customer/address/use-add-item.js +9 -0
  24. package/dist/customer/address/use-addresses.d.ts +6 -0
  25. package/dist/customer/address/use-addresses.js +18 -0
  26. package/dist/customer/address/use-remove-item.d.ts +6 -0
  27. package/dist/customer/address/use-remove-item.js +9 -0
  28. package/dist/customer/address/use-update-item.d.ts +6 -0
  29. package/dist/customer/address/use-update-item.js +9 -0
  30. package/dist/customer/card/use-add-item.d.ts +6 -0
  31. package/dist/customer/card/use-add-item.js +9 -0
  32. package/dist/customer/card/use-cards.d.ts +6 -0
  33. package/dist/customer/card/use-cards.js +18 -0
  34. package/dist/customer/card/use-remove-item.d.ts +6 -0
  35. package/dist/customer/card/use-remove-item.js +9 -0
  36. package/dist/customer/card/use-update-item.d.ts +6 -0
  37. package/dist/customer/card/use-update-item.js +9 -0
  38. package/dist/customer/use-customer.d.ts +6 -0
  39. package/dist/customer/use-customer.js +9 -0
  40. package/dist/index.d.ts +71 -0
  41. package/dist/index.js +29 -0
  42. package/dist/product/use-price.d.ts +26 -0
  43. package/dist/product/use-price.js +33 -0
  44. package/dist/product/use-search.d.ts +6 -0
  45. package/dist/product/use-search.js +9 -0
  46. package/dist/registerProductCollection.d.ts +11 -0
  47. package/dist/registerProductCollection.js +42 -0
  48. package/dist/registerProductImage.d.ts +13 -0
  49. package/dist/registerProductImage.js +37 -0
  50. package/dist/registerable.d.ts +4 -0
  51. package/dist/registerable.js +0 -0
  52. package/dist/tsconfig.tsbuildinfo +2970 -0
  53. package/dist/types/cart.d.ts +157 -0
  54. package/dist/types/cart.js +0 -0
  55. package/dist/types/checkout.d.ts +57 -0
  56. package/dist/types/checkout.js +0 -0
  57. package/dist/types/common.d.ts +13 -0
  58. package/dist/types/common.js +0 -0
  59. package/dist/types/customer/address.d.ts +110 -0
  60. package/dist/types/customer/address.js +0 -0
  61. package/dist/types/customer/card.d.ts +113 -0
  62. package/dist/types/customer/card.js +0 -0
  63. package/dist/types/customer/index.d.ts +24 -0
  64. package/dist/types/customer/index.js +2 -0
  65. package/dist/types/index.d.ts +12 -0
  66. package/dist/types/index.js +11 -0
  67. package/dist/types/login.d.ts +27 -0
  68. package/dist/types/login.js +0 -0
  69. package/dist/types/logout.d.ts +17 -0
  70. package/dist/types/logout.js +0 -0
  71. package/dist/types/page.d.ts +24 -0
  72. package/dist/types/page.js +0 -0
  73. package/dist/types/product.d.ts +99 -0
  74. package/dist/types/product.js +0 -0
  75. package/dist/types/signup.d.ts +23 -0
  76. package/dist/types/signup.js +0 -0
  77. package/dist/types/site.d.ts +17 -0
  78. package/dist/types/site.js +0 -0
  79. package/dist/types/wishlist.d.ts +83 -0
  80. package/dist/types/wishlist.js +0 -0
  81. package/dist/utils/default-fetcher.d.ts +4 -0
  82. package/dist/utils/default-fetcher.js +3 -0
  83. package/dist/utils/define-property.d.ts +21 -0
  84. package/dist/utils/define-property.js +4 -0
  85. package/dist/utils/errors.d.ts +27 -0
  86. package/dist/utils/errors.js +24 -0
  87. package/dist/utils/types.d.ts +94 -0
  88. package/dist/utils/types.js +0 -0
  89. package/dist/utils/use-data.d.ts +12 -0
  90. package/dist/utils/use-data.js +43 -0
  91. package/dist/utils/use-hook.d.ts +6 -0
  92. package/dist/utils/use-hook.js +37 -0
  93. package/package.json +42 -0
@@ -0,0 +1,23 @@
1
+ export declare type SignupBody = {
2
+ firstName: string;
3
+ lastName: string;
4
+ email: string;
5
+ password: string;
6
+ };
7
+ export declare type SignupTypes = {
8
+ body: SignupBody;
9
+ };
10
+ export declare type SignupHook<T extends SignupTypes = SignupTypes> = {
11
+ data: null;
12
+ body: T['body'];
13
+ actionInput: T['body'];
14
+ fetcherInput: T['body'];
15
+ };
16
+ export declare type SignupSchema<T extends SignupTypes = SignupTypes> = {
17
+ endpoint: {
18
+ options: {};
19
+ handlers: {
20
+ signup: SignupHook<T>;
21
+ };
22
+ };
23
+ };
File without changes
@@ -0,0 +1,17 @@
1
+ export declare type Category = {
2
+ id: string;
3
+ name: string;
4
+ slug: string;
5
+ path: string;
6
+ };
7
+ export declare type Brand = any;
8
+ export declare type SiteTypes = {
9
+ category: Category;
10
+ brand: Brand;
11
+ };
12
+ export declare type GetSiteInfoOperation<T extends SiteTypes = SiteTypes> = {
13
+ data: {
14
+ categories: T['category'][];
15
+ brands: T['brand'][];
16
+ };
17
+ };
File without changes
@@ -0,0 +1,83 @@
1
+ export declare type Wishlist = any;
2
+ export declare type WishlistItemBody = {
3
+ variantId: string | number;
4
+ productId: string;
5
+ };
6
+ export declare type WishlistTypes = {
7
+ wishlist: Wishlist;
8
+ itemBody: WishlistItemBody;
9
+ };
10
+ export declare type GetWishlistHook<T extends WishlistTypes = WishlistTypes> = {
11
+ data: T['wishlist'] | null;
12
+ body: {
13
+ includeProducts?: boolean;
14
+ };
15
+ input: {
16
+ includeProducts?: boolean;
17
+ };
18
+ fetcherInput: {
19
+ customerId: string;
20
+ includeProducts?: boolean;
21
+ };
22
+ swrState: {
23
+ isEmpty: boolean;
24
+ };
25
+ };
26
+ export declare type AddItemHook<T extends WishlistTypes = WishlistTypes> = {
27
+ data: T['wishlist'];
28
+ body: {
29
+ item: T['itemBody'];
30
+ };
31
+ fetcherInput: {
32
+ item: T['itemBody'];
33
+ };
34
+ actionInput: T['itemBody'];
35
+ };
36
+ export declare type RemoveItemHook<T extends WishlistTypes = WishlistTypes> = {
37
+ data: T['wishlist'] | null;
38
+ body: {
39
+ itemId: string;
40
+ };
41
+ fetcherInput: {
42
+ itemId: string;
43
+ };
44
+ actionInput: {
45
+ id: string;
46
+ };
47
+ input: {
48
+ wishlist?: {
49
+ includeProducts?: boolean;
50
+ };
51
+ };
52
+ };
53
+ export declare type WishlistSchema<T extends WishlistTypes = WishlistTypes> = {
54
+ endpoint: {
55
+ options: {};
56
+ handlers: {
57
+ getWishlist: GetWishlistHook<T> & {
58
+ data: T['wishlist'] | null;
59
+ body: {
60
+ customerToken?: string;
61
+ };
62
+ };
63
+ addItem: AddItemHook<T> & {
64
+ body: {
65
+ customerToken?: string;
66
+ };
67
+ };
68
+ removeItem: RemoveItemHook<T> & {
69
+ body: {
70
+ customerToken?: string;
71
+ };
72
+ };
73
+ };
74
+ };
75
+ };
76
+ export declare type GetCustomerWishlistOperation<T extends WishlistTypes = WishlistTypes> = {
77
+ data: {
78
+ wishlist?: T['wishlist'];
79
+ };
80
+ variables: {
81
+ customerId: string;
82
+ };
83
+ };
File without changes
@@ -0,0 +1,4 @@
1
+ import type { HookFetcherFn } from './types';
2
+ export declare const SWRFetcher: HookFetcherFn<any>;
3
+ export declare const mutationFetcher: HookFetcherFn<any>;
4
+ export default SWRFetcher;
@@ -0,0 +1,3 @@
1
+ export const SWRFetcher = ({ options, fetch }) => fetch(options);
2
+ export const mutationFetcher = ({ input, options, fetch, }) => fetch({ ...options, body: input });
3
+ export default SWRFetcher;
@@ -0,0 +1,21 @@
1
+ declare type InferValue<Prop extends PropertyKey, Desc> = Desc extends {
2
+ get(): any;
3
+ value: any;
4
+ } ? never : Desc extends {
5
+ value: infer T;
6
+ } ? Record<Prop, T> : Desc extends {
7
+ get(): infer T;
8
+ } ? Record<Prop, T> : never;
9
+ declare type DefineProperty<Prop extends PropertyKey, Desc extends PropertyDescriptor> = Desc extends {
10
+ writable: any;
11
+ set(val: any): any;
12
+ } ? never : Desc extends {
13
+ writable: any;
14
+ get(): any;
15
+ } ? never : Desc extends {
16
+ writable: false;
17
+ } ? Readonly<InferValue<Prop, Desc>> : Desc extends {
18
+ writable: true;
19
+ } ? InferValue<Prop, Desc> : Readonly<InferValue<Prop, Desc>>;
20
+ export default function defineProperty<Obj extends object, Key extends PropertyKey, PDesc extends PropertyDescriptor>(obj: Obj, prop: Key, val: PDesc): asserts obj is Obj & DefineProperty<Key, PDesc>;
21
+ export {};
@@ -0,0 +1,4 @@
1
+ // Taken from https://fettblog.eu/typescript-assertion-signatures/
2
+ export default function defineProperty(obj, prop, val) {
3
+ Object.defineProperty(obj, prop, val);
4
+ }
@@ -0,0 +1,27 @@
1
+ export declare type ErrorData = {
2
+ message: string;
3
+ code?: string;
4
+ };
5
+ export declare type ErrorProps = {
6
+ code?: string;
7
+ } & ({
8
+ message: string;
9
+ errors?: never;
10
+ } | {
11
+ message?: never;
12
+ errors: ErrorData[];
13
+ });
14
+ export declare class CommerceError extends Error {
15
+ code?: string;
16
+ errors: ErrorData[];
17
+ constructor({ message, code, errors }: ErrorProps);
18
+ }
19
+ export declare class ValidationError extends CommerceError {
20
+ constructor(options: ErrorProps);
21
+ }
22
+ export declare class FetcherError extends CommerceError {
23
+ status: number;
24
+ constructor(options: {
25
+ status: number;
26
+ } & ErrorProps);
27
+ }
@@ -0,0 +1,24 @@
1
+ export class CommerceError extends Error {
2
+ constructor({ message, code, errors }) {
3
+ const error = message
4
+ ? { message, ...(code ? { code } : {}) }
5
+ : errors[0];
6
+ super(error.message);
7
+ this.errors = message ? [error] : errors;
8
+ if (error.code)
9
+ this.code = error.code;
10
+ }
11
+ }
12
+ // Used for errors that come from a bad implementation of the hooks
13
+ export class ValidationError extends CommerceError {
14
+ constructor(options) {
15
+ super(options);
16
+ this.code = 'validation_error';
17
+ }
18
+ }
19
+ export class FetcherError extends CommerceError {
20
+ constructor(options) {
21
+ super(options);
22
+ this.status = options.status;
23
+ }
24
+ }
@@ -0,0 +1,94 @@
1
+ import type { SWRConfiguration } from 'swr';
2
+ import type { CommerceError } from './errors';
3
+ import type { ResponseState } from './use-data';
4
+ /**
5
+ * Returns the properties in T with the properties in type K, overriding properties defined in T
6
+ */
7
+ export declare type Override<T, K> = Omit<T, keyof K> & K;
8
+ /**
9
+ * Returns the properties in T with the properties in type K changed from optional to required
10
+ */
11
+ export declare type PickRequired<T, K extends keyof T> = Omit<T, K> & {
12
+ [P in K]-?: NonNullable<T[P]>;
13
+ };
14
+ /**
15
+ * Core fetcher added by CommerceProvider
16
+ */
17
+ export declare type Fetcher<T = any, B = any> = (options: FetcherOptions<B>) => T | Promise<T>;
18
+ export declare type FetcherOptions<Body = any> = {
19
+ url?: string;
20
+ query?: string;
21
+ method?: string;
22
+ variables?: any;
23
+ body?: Body;
24
+ };
25
+ export declare type HookFetcher<Data, Input = null, Result = any> = (options: HookFetcherOptions | null, input: Input, fetch: <T = Result, Body = any>(options: FetcherOptions<Body>) => Promise<T>) => Data | Promise<Data>;
26
+ export declare type HookFetcherFn<H extends HookSchemaBase> = (context: HookFetcherContext<H>) => H['data'] | Promise<H['data']>;
27
+ export declare type HookFetcherContext<H extends HookSchemaBase> = {
28
+ options: HookFetcherOptions;
29
+ input: H['fetcherInput'];
30
+ fetch: <T = H['fetchData'] extends {} | null ? H['fetchData'] : any, B = H['body']>(options: FetcherOptions<B>) => Promise<T>;
31
+ };
32
+ export declare type HookFetcherOptions = {
33
+ method?: string;
34
+ } & ({
35
+ query: string;
36
+ url?: string;
37
+ } | {
38
+ query?: string;
39
+ url: string;
40
+ });
41
+ export declare type HookInputValue = string | number | boolean | undefined;
42
+ export declare type HookSWRInput = [string, HookInputValue][];
43
+ export declare type HookFetchInput = {
44
+ [k: string]: HookInputValue;
45
+ };
46
+ export declare type HookFunction<Input extends {
47
+ [k: string]: unknown;
48
+ } | undefined, T> = keyof Input extends never ? () => T : Partial<Input> extends Input ? (input?: Input) => T : (input: Input) => T;
49
+ export declare type HookSchemaBase = {
50
+ data: any;
51
+ input?: {};
52
+ fetcherInput?: {};
53
+ body?: {};
54
+ fetchData?: any;
55
+ };
56
+ export declare type SWRHookSchemaBase = HookSchemaBase & {
57
+ swrState?: {};
58
+ mutations?: Record<string, ReturnType<MutationHook<any>['useHook']>>;
59
+ };
60
+ export declare type MutationSchemaBase = HookSchemaBase & {
61
+ actionInput?: {};
62
+ };
63
+ /**
64
+ * Generates a SWR hook handler based on the schema of a hook
65
+ */
66
+ export declare type SWRHook<H extends SWRHookSchemaBase> = {
67
+ useHook(context: SWRHookContext<H>): HookFunction<H['input'] & {
68
+ swrOptions?: SwrOptions<H['data'], H['fetcherInput']>;
69
+ }, ResponseState<H['data']> & H['swrState'] & H['mutations']>;
70
+ fetchOptions: HookFetcherOptions;
71
+ fetcher?: HookFetcherFn<H>;
72
+ };
73
+ export declare type SWRHookContext<H extends SWRHookSchemaBase> = {
74
+ useData(context?: {
75
+ input?: HookFetchInput | HookSWRInput;
76
+ swrOptions?: SwrOptions<H['data'], H['fetcherInput']>;
77
+ }): ResponseState<H['data']>;
78
+ };
79
+ /**
80
+ * Generates a mutation hook handler based on the schema of a hook
81
+ */
82
+ export declare type MutationHook<H extends MutationSchemaBase> = {
83
+ useHook(context: MutationHookContext<H>): HookFunction<H['input'], HookFunction<H['actionInput'], H['data'] | Promise<H['data']>>>;
84
+ fetchOptions: HookFetcherOptions;
85
+ fetcher?: HookFetcherFn<H>;
86
+ };
87
+ export declare type MutationHookContext<H extends MutationSchemaBase> = {
88
+ fetch: keyof H['fetcherInput'] extends never ? () => H['data'] | Promise<H['data']> : Partial<H['fetcherInput']> extends H['fetcherInput'] ? (context?: {
89
+ input?: H['fetcherInput'];
90
+ }) => H['data'] | Promise<H['data']> : (context: {
91
+ input: H['fetcherInput'];
92
+ }) => H['data'] | Promise<H['data']>;
93
+ };
94
+ export declare type SwrOptions<Data, Input = null, Result = any> = SWRConfiguration<Data, CommerceError, HookFetcher<Data, Input, Result>>;
File without changes
@@ -0,0 +1,12 @@
1
+ import { SWRResponse } from 'swr';
2
+ import type { HookSWRInput, HookFetchInput, HookFetcherOptions, HookFetcherFn, Fetcher, SwrOptions, SWRHookSchemaBase } from './types';
3
+ import { CommerceError } from './errors';
4
+ export declare type ResponseState<Result> = SWRResponse<Result, CommerceError> & {
5
+ isLoading: boolean;
6
+ };
7
+ export declare type UseData = <H extends SWRHookSchemaBase>(options: {
8
+ fetchOptions: HookFetcherOptions;
9
+ fetcher: HookFetcherFn<H>;
10
+ }, input: HookFetchInput | HookSWRInput, fetcherFn: Fetcher, swrOptions?: SwrOptions<H['data'], H['fetcherInput']>) => ResponseState<H['data']>;
11
+ declare const useData: UseData;
12
+ export default useData;
@@ -0,0 +1,43 @@
1
+ import useSWR from 'swr';
2
+ import defineProperty from './define-property';
3
+ import { CommerceError } from './errors';
4
+ const useData = (options, input, fetcherFn, swrOptions) => {
5
+ const hookInput = Array.isArray(input) ? input : Object.entries(input);
6
+ const fetcher = async (url, query, method, ...args) => {
7
+ try {
8
+ return await options.fetcher({
9
+ options: { url, query, method },
10
+ // Transform the input array into an object
11
+ input: args.reduce((obj, val, i) => {
12
+ obj[hookInput[i][0]] = val;
13
+ return obj;
14
+ }, {}),
15
+ fetch: fetcherFn,
16
+ });
17
+ }
18
+ catch (error) {
19
+ // SWR will not log errors, but any error that's not an instance
20
+ // of CommerceError is not welcomed by this hook
21
+ if (!(error instanceof CommerceError)) {
22
+ console.error(error);
23
+ }
24
+ throw error;
25
+ }
26
+ };
27
+ const response = useSWR(() => {
28
+ const opts = options.fetchOptions;
29
+ return opts
30
+ ? [opts.url, opts.query, opts.method, ...hookInput.map((e) => e[1])]
31
+ : null;
32
+ }, fetcher, swrOptions);
33
+ if (!('isLoading' in response)) {
34
+ defineProperty(response, 'isLoading', {
35
+ get() {
36
+ return response.data === undefined;
37
+ },
38
+ enumerable: true,
39
+ });
40
+ }
41
+ return response;
42
+ };
43
+ export default useData;
@@ -0,0 +1,6 @@
1
+ import { Provider } from '..';
2
+ import type { MutationHook, PickRequired, SWRHook } from './types';
3
+ export declare function useFetcher(): import("./types").Fetcher<any, any>;
4
+ export declare function useHook<P extends Provider, H extends MutationHook<any> | SWRHook<any>>(fn: (provider: P) => H): H;
5
+ export declare function useSWRHook<H extends SWRHook<any>>(hook: PickRequired<H, 'fetcher'>): (input?: any) => any;
6
+ export declare function useMutationHook<H extends MutationHook<any>>(hook: PickRequired<H, 'fetcher'>): (input?: any) => (input?: any) => any;
@@ -0,0 +1,37 @@
1
+ import { useCallback } from 'react';
2
+ import { useCommerce } from '..';
3
+ import useData from './use-data';
4
+ export function useFetcher() {
5
+ const { providerRef, fetcherRef } = useCommerce();
6
+ return providerRef.current.fetcher ?? fetcherRef.current;
7
+ }
8
+ export function useHook(fn) {
9
+ console.log("oi?");
10
+ const x = useCommerce();
11
+ console.log("test", x);
12
+ const { providerRef } = useCommerce();
13
+ console.log("dale", providerRef);
14
+ const provider = providerRef.current;
15
+ return fn(provider);
16
+ }
17
+ export function useSWRHook(hook) {
18
+ const fetcher = useFetcher();
19
+ return hook.useHook({
20
+ useData(ctx) {
21
+ const response = useData(hook, ctx?.input ?? [], fetcher, ctx?.swrOptions);
22
+ return response;
23
+ },
24
+ });
25
+ }
26
+ export function useMutationHook(hook) {
27
+ const fetcher = useFetcher();
28
+ return hook.useHook({
29
+ fetch: useCallback(({ input } = {}) => {
30
+ return hook.fetcher({
31
+ input,
32
+ options: hook.fetchOptions,
33
+ fetch: fetcher,
34
+ });
35
+ }, [fetcher, hook.fetchOptions]),
36
+ });
37
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@plasmicpkgs/commerce",
3
+ "version": "0.0.1",
4
+ "description": "Plasmic components and registration calls for commerce providers",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "module": "dist/commerce.esm.js",
8
+ "type": "module",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "exports": {
13
+ ".": "./dist/index.js",
14
+ "./*": [
15
+ "./dist/*.js",
16
+ "./dist/*/index.js"
17
+ ]
18
+ },
19
+ "scripts": {
20
+ "build": "tsdx build",
21
+ "start": "tsdx watch",
22
+ "test": "tsdx test --passWithNoTests",
23
+ "lint": "tsdx lint",
24
+ "prepare": "tsc",
25
+ "size": "size-limit",
26
+ "analyze": "size-limit --why"
27
+ },
28
+ "devDependencies": {
29
+ "@size-limit/preset-small-lib": "^4.11.0",
30
+ "@types/js-cookie": "^3.0.1",
31
+ "@types/node": "^14.0.26",
32
+ "size-limit": "^4.11.0",
33
+ "tsdx": "^0.14.1",
34
+ "tslib": "^2.2.0",
35
+ "typescript": "^3.9.7"
36
+ },
37
+ "dependencies": {
38
+ "antd": "^4.19.1",
39
+ "js-cookie": "^3.0.1",
40
+ "swr": "^1.2.2"
41
+ }
42
+ }