medusa-contact-logic-plugin 1.1.2 → 2.1.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/dist/{contact-logic/src/hooks → hooks}/useContactForm.d.ts +3 -1
- package/dist/index.d.ts +2 -0
- package/dist/server/index.d.ts +33 -0
- package/dist/ui-library.js +43 -26
- package/dist/ui-library.umd.cjs +1 -1
- package/package.json +15 -4
- package/src/server/index.ts +81 -0
- package/dist/contact-logic/src/index.d.ts +0 -2
- package/dist/medusa-services/contact-action.d.ts +0 -9
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
|
|
1
3
|
export interface UseContactFormProps {
|
|
2
4
|
baseUrl?: string;
|
|
3
5
|
publishableKey?: string;
|
|
@@ -7,5 +9,5 @@ export declare const useContactForm: ({ baseUrl: propsBaseUrl, publishableKey: p
|
|
|
7
9
|
status: "idle" | "success" | "error" | "pending";
|
|
8
10
|
message: string;
|
|
9
11
|
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => Promise<void>;
|
|
10
|
-
setStatus:
|
|
12
|
+
setStatus: React.Dispatch<React.SetStateAction<"idle" | "success" | "error" | "pending">>;
|
|
11
13
|
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare function submitContactRequest({ email, payload, metadata, source, }: {
|
|
2
|
+
email: string;
|
|
3
|
+
payload: {
|
|
4
|
+
full_name?: string;
|
|
5
|
+
phone?: string;
|
|
6
|
+
topic?: string;
|
|
7
|
+
subject?: string;
|
|
8
|
+
message: string;
|
|
9
|
+
};
|
|
10
|
+
metadata?: Record<string, unknown>;
|
|
11
|
+
source?: string;
|
|
12
|
+
}): Promise<{
|
|
13
|
+
success: false;
|
|
14
|
+
error: string;
|
|
15
|
+
data?: undefined;
|
|
16
|
+
} | {
|
|
17
|
+
success: true;
|
|
18
|
+
data: any;
|
|
19
|
+
error?: undefined;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function subscribeToNewsletter({ email, status, source, }: {
|
|
22
|
+
email: string;
|
|
23
|
+
status?: "subscribed" | "unsubscribed";
|
|
24
|
+
source?: string;
|
|
25
|
+
}): Promise<{
|
|
26
|
+
success: false;
|
|
27
|
+
message: string;
|
|
28
|
+
data?: undefined;
|
|
29
|
+
} | {
|
|
30
|
+
success: true;
|
|
31
|
+
message: string;
|
|
32
|
+
data: any;
|
|
33
|
+
}>;
|
package/dist/ui-library.js
CHANGED
|
@@ -1,49 +1,66 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
async function
|
|
3
|
-
const
|
|
1
|
+
import { useState as h } from "react";
|
|
2
|
+
async function S({ backendUrl: c, publishableKey: r, data: a }) {
|
|
3
|
+
const s = `${c.replace(/\/$/, "")}/store/contact-requests`, o = {
|
|
4
4
|
"Content-Type": "application/json"
|
|
5
5
|
};
|
|
6
|
-
|
|
7
|
-
const e = await fetch(
|
|
6
|
+
r && (o["x-publishable-api-key"] = r);
|
|
7
|
+
const e = await fetch(s, {
|
|
8
8
|
method: "POST",
|
|
9
9
|
headers: o,
|
|
10
|
-
body: JSON.stringify(
|
|
10
|
+
body: JSON.stringify(a)
|
|
11
11
|
});
|
|
12
12
|
if (!e.ok) {
|
|
13
|
-
const
|
|
14
|
-
throw new Error(
|
|
13
|
+
const t = await e.json().catch(() => ({}));
|
|
14
|
+
throw new Error(t.message || "Failed to send message");
|
|
15
15
|
}
|
|
16
16
|
return await e.json();
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
async function w({ backendUrl: c, publishableApiKey: r, email: a, status: u = "subscribed", source: s = "footer" }) {
|
|
19
|
+
const o = `${c.replace(/\/$/, "")}/store/contact-email-subscriptions`, e = {
|
|
20
|
+
"Content-Type": "application/json"
|
|
21
|
+
};
|
|
22
|
+
r && (e["x-publishable-api-key"] = r);
|
|
23
|
+
const t = await fetch(o, {
|
|
24
|
+
method: "POST",
|
|
25
|
+
headers: e,
|
|
26
|
+
body: JSON.stringify({ email: a, status: u, source: s })
|
|
27
|
+
});
|
|
28
|
+
if (!t.ok) {
|
|
29
|
+
const l = await t.json().catch(() => ({}));
|
|
30
|
+
throw new Error(l.message || "Failed to subscribe to newsletter");
|
|
31
|
+
}
|
|
32
|
+
return t.json();
|
|
33
|
+
}
|
|
34
|
+
const b = { BASE_URL: "/", DEV: !1, MODE: "production", PROD: !0, SSR: !1 }, y = ({
|
|
35
|
+
baseUrl: c,
|
|
36
|
+
publishableKey: r,
|
|
37
|
+
onComplete: a
|
|
22
38
|
} = {}) => {
|
|
23
|
-
const [
|
|
24
|
-
var
|
|
39
|
+
const [u, s] = h("idle"), [o, e] = h(""), t = (i) => {
|
|
40
|
+
var n;
|
|
25
41
|
try {
|
|
26
|
-
return ((
|
|
42
|
+
return ((n = process.env) == null ? void 0 : n[i]) || (b == null ? void 0 : b[i]);
|
|
27
43
|
} catch {
|
|
28
44
|
return;
|
|
29
45
|
}
|
|
30
|
-
},
|
|
46
|
+
}, l = c || t("NEXT_PUBLIC_MEDUSA_BACKEND_URL"), f = r || t("NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY");
|
|
31
47
|
return {
|
|
32
|
-
status:
|
|
48
|
+
status: u,
|
|
33
49
|
message: o,
|
|
34
|
-
handleSubmit: async (
|
|
35
|
-
|
|
50
|
+
handleSubmit: async (i) => {
|
|
51
|
+
i.preventDefault(), s("pending"), e("");
|
|
36
52
|
try {
|
|
37
|
-
const
|
|
38
|
-
await
|
|
39
|
-
} catch (
|
|
40
|
-
console.error("Submission error:",
|
|
53
|
+
const n = new FormData(i.currentTarget), d = Object.fromEntries(n);
|
|
54
|
+
await S({ backendUrl: l, publishableKey: f, data: d }), s("success"), e("Thank you! Your message has been sent."), a == null || a(d);
|
|
55
|
+
} catch (n) {
|
|
56
|
+
console.error("Submission error:", n), s("error"), e(n.message || "Something went wrong. Please try again.");
|
|
41
57
|
}
|
|
42
58
|
},
|
|
43
|
-
setStatus:
|
|
59
|
+
setStatus: s
|
|
44
60
|
};
|
|
45
61
|
};
|
|
46
62
|
export {
|
|
47
|
-
|
|
48
|
-
|
|
63
|
+
S as sendContactRequest,
|
|
64
|
+
w as subscribeToNewsletter,
|
|
65
|
+
y as useContactForm
|
|
49
66
|
};
|
package/dist/ui-library.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(t,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],c):(t=typeof globalThis<"u"?globalThis:t||self,c(t.MedusaUILibrary={},t.React))})(this,function(t,c){"use strict";async function h({backendUrl:u,publishableKey:r,data:a}){const s=`${u.replace(/\/$/,"")}/store/contact-requests`,i={"Content-Type":"application/json"};r&&(i["x-publishable-api-key"]=r);const e=await fetch(s,{method:"POST",headers:i,body:JSON.stringify(a)});if(!e.ok){const n=await e.json().catch(()=>({}));throw new Error(n.message||"Failed to send message")}return await e.json()}async function S({backendUrl:u,publishableApiKey:r,email:a,status:f="subscribed",source:s="footer"}){const i=`${u.replace(/\/$/,"")}/store/contact-email-subscriptions`,e={"Content-Type":"application/json"};r&&(e["x-publishable-api-key"]=r);const n=await fetch(i,{method:"POST",headers:e,body:JSON.stringify({email:a,status:f,source:s})});if(!n.ok){const b=await n.json().catch(()=>({}));throw new Error(b.message||"Failed to subscribe to newsletter")}return n.json()}const l={BASE_URL:"/",DEV:!1,MODE:"production",PROD:!0,SSR:!1},p=({baseUrl:u,publishableKey:r,onComplete:a}={})=>{const[f,s]=c.useState("idle"),[i,e]=c.useState(""),n=d=>{var o;try{return((o=process.env)==null?void 0:o[d])||(l==null?void 0:l[d])}catch{return}},b=u||n("NEXT_PUBLIC_MEDUSA_BACKEND_URL"),g=r||n("NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY");return{status:f,message:i,handleSubmit:async d=>{d.preventDefault(),s("pending"),e("");try{const o=new FormData(d.currentTarget),y=Object.fromEntries(o);await h({backendUrl:b,publishableKey:g,data:y}),s("success"),e("Thank you! Your message has been sent."),a==null||a(y)}catch(o){console.error("Submission error:",o),s("error"),e(o.message||"Something went wrong. Please try again.")}},setStatus:s}};t.sendContactRequest=h,t.subscribeToNewsletter=S,t.useContactForm=p,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "medusa-contact-logic-plugin",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/ui-library.umd.cjs",
|
|
6
6
|
"module": "./dist/ui-library.js",
|
|
@@ -10,19 +10,30 @@
|
|
|
10
10
|
"import": "./dist/ui-library.js",
|
|
11
11
|
"require": "./dist/ui-library.umd.cjs",
|
|
12
12
|
"types": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./server": {
|
|
15
|
+
"types": "./dist/contact-logic/src/server/index.d.ts",
|
|
16
|
+
"import": "./src/server/index.ts",
|
|
17
|
+
"default": "./src/server/index.ts"
|
|
13
18
|
}
|
|
14
19
|
},
|
|
15
20
|
"files": [
|
|
16
|
-
"dist"
|
|
21
|
+
"dist",
|
|
22
|
+
"src/server"
|
|
17
23
|
],
|
|
18
24
|
"scripts": {
|
|
19
25
|
"dev": "vite",
|
|
20
26
|
"build": "tsc && vite build",
|
|
21
27
|
"preview": "vite preview"
|
|
22
28
|
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"medusa-services": "1.2.1",
|
|
31
|
+
"medusa-storefront-core": "1.2.5"
|
|
32
|
+
},
|
|
23
33
|
"peerDependencies": {
|
|
24
34
|
"react": "^18.0.0",
|
|
25
|
-
"react-dom": "^18.0.0"
|
|
35
|
+
"react-dom": "^18.0.0",
|
|
36
|
+
"medusa-storefront-data": "^2.0.0"
|
|
26
37
|
},
|
|
27
38
|
"devDependencies": {
|
|
28
39
|
"@types/node": "^25.1.0",
|
|
@@ -35,4 +46,4 @@
|
|
|
35
46
|
"vite": "^5.1.4",
|
|
36
47
|
"vite-plugin-dts": "^3.9.1"
|
|
37
48
|
}
|
|
38
|
-
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
sendContactRequest,
|
|
5
|
+
subscribeToNewsletter as subscribeNewsletterApi,
|
|
6
|
+
} from "medusa-services/contact-action";
|
|
7
|
+
|
|
8
|
+
function getStoreConfig() {
|
|
9
|
+
const backendUrl =
|
|
10
|
+
process.env.MEDUSA_BACKEND_URL ||
|
|
11
|
+
process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL ||
|
|
12
|
+
"http://localhost:9000";
|
|
13
|
+
const publishableApiKey = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || "";
|
|
14
|
+
return { backendUrl, publishableApiKey };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function submitContactRequest({
|
|
18
|
+
email,
|
|
19
|
+
payload,
|
|
20
|
+
metadata,
|
|
21
|
+
source = "storefront",
|
|
22
|
+
}: {
|
|
23
|
+
email: string;
|
|
24
|
+
payload: {
|
|
25
|
+
full_name?: string;
|
|
26
|
+
phone?: string;
|
|
27
|
+
topic?: string;
|
|
28
|
+
subject?: string;
|
|
29
|
+
message: string;
|
|
30
|
+
};
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
32
|
+
source?: string;
|
|
33
|
+
}) {
|
|
34
|
+
try {
|
|
35
|
+
const { backendUrl, publishableApiKey } = getStoreConfig();
|
|
36
|
+
if (!publishableApiKey) {
|
|
37
|
+
return { success: false as const, error: "Configuration error: Publishable API key is missing." };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const data = await sendContactRequest({
|
|
41
|
+
backendUrl,
|
|
42
|
+
publishableKey: publishableApiKey,
|
|
43
|
+
data: { email, payload, metadata, source },
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return { success: true as const, data };
|
|
47
|
+
} catch (error: unknown) {
|
|
48
|
+
const message = error instanceof Error ? error.message : "Failed to submit contact request";
|
|
49
|
+
return { success: false as const, error: message };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function subscribeToNewsletter({
|
|
54
|
+
email,
|
|
55
|
+
status = "subscribed",
|
|
56
|
+
source = "footer",
|
|
57
|
+
}: {
|
|
58
|
+
email: string;
|
|
59
|
+
status?: "subscribed" | "unsubscribed";
|
|
60
|
+
source?: string;
|
|
61
|
+
}) {
|
|
62
|
+
try {
|
|
63
|
+
const { backendUrl, publishableApiKey } = getStoreConfig();
|
|
64
|
+
if (!publishableApiKey) {
|
|
65
|
+
return { success: false as const, message: "Server configuration error (missing key)" };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const data = await subscribeNewsletterApi({
|
|
69
|
+
backendUrl,
|
|
70
|
+
publishableApiKey,
|
|
71
|
+
email,
|
|
72
|
+
status,
|
|
73
|
+
source,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return { success: true as const, message: "Thank you for subscribing!", data };
|
|
77
|
+
} catch (error: unknown) {
|
|
78
|
+
const message = error instanceof Error ? error.message : "Something went wrong";
|
|
79
|
+
return { success: false as const, message };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export interface ContactSubmissionData {
|
|
2
|
-
backendUrl: string;
|
|
3
|
-
publishableKey?: string;
|
|
4
|
-
data: Record<string, any>;
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* Server-side compatible function to send contact request
|
|
8
|
-
*/
|
|
9
|
-
export declare function sendContactRequest({ backendUrl, publishableKey, data }: ContactSubmissionData): Promise<any>;
|