@contact-api/core 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Contact API, Mason L'Etoile
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # Contact API Core
2
+
3
+ Contact form logic: CORS evaluation, validation, honeypot handling, and email orchestration. Will be paired with one or more providers, (`Resend` or `Nodemailer`), and a platform, (`Vercel`).
4
+
5
+ ## Exports
6
+ - `handleContact(req, deps)` — orchestrates the full contact-form flow
7
+ - `evaluateCors(req, allowedOrigins)` — CORS decision logic
8
+ - `sendEmail(config, body)` — dispatches to a given `EmailProvider`
9
+ - `isValidBody(body)` — input validation
10
+ - Types: `ContactRequest`, `ContactResult`, `EmailProvider`, `EmailPayload`, `EmailBody`
11
+
12
+ ## License
13
+ MIT License - see [LICENSE](./LICENSE) for details.
@@ -0,0 +1,13 @@
1
+ //#region src/contact.d.ts
2
+ interface ContactRequest {
3
+ method: string;
4
+ headers: Record<string, string | undefined>;
5
+ body: unknown;
6
+ }
7
+ interface ContactResult {
8
+ status: number;
9
+ body: unknown;
10
+ }
11
+ //#endregion
12
+ export { ContactRequest, ContactResult };
13
+ //# sourceMappingURL=contact.d.mts.map
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import { ContactRequest } from "./contact.mjs";
2
+
3
+ //#region src/cors.d.ts
4
+ interface CorsResult {
5
+ outcome: "preflight" | "forbidden" | "ok";
6
+ headers: Record<string, string>;
7
+ status?: number;
8
+ }
9
+ declare function evaluateCors(req: ContactRequest, allowedOrigins: string[]): CorsResult;
10
+ //#endregion
11
+ export { CorsResult, evaluateCors };
12
+ //# sourceMappingURL=cors.d.mts.map
package/dist/cors.mjs ADDED
@@ -0,0 +1,32 @@
1
+ //#region src/cors.ts
2
+ function evaluateCors(req, allowedOrigins) {
3
+ const headers = { "X-Content-Type-Options": "nosniff" };
4
+ const origin = req.headers["origin"];
5
+ if (!(!!origin && allowedOrigins.includes(origin))) {
6
+ if (req.method === "OPTIONS") return {
7
+ outcome: "preflight",
8
+ headers,
9
+ status: 403
10
+ };
11
+ return {
12
+ outcome: "forbidden",
13
+ headers
14
+ };
15
+ }
16
+ headers["Access-Control-Allow-Origin"] = origin;
17
+ headers["Access-Control-Allow-Methods"] = "POST, OPTIONS";
18
+ headers["Access-Control-Allow-Headers"] = "Content-Type";
19
+ if (req.method === "OPTIONS") return {
20
+ outcome: "preflight",
21
+ headers,
22
+ status: 204
23
+ };
24
+ return {
25
+ outcome: "ok",
26
+ headers
27
+ };
28
+ }
29
+ //#endregion
30
+ export { evaluateCors };
31
+
32
+ //# sourceMappingURL=cors.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cors.mjs","names":[],"sources":["../src/cors.ts"],"sourcesContent":["import type { ContactRequest } from \"./contact.js\";\n\nexport interface CorsResult {\n outcome: \"preflight\" | \"forbidden\" | \"ok\";\n headers: Record<string, string>;\n status?: number;\n}\n\nexport function evaluateCors(req: ContactRequest, allowedOrigins: string[]): CorsResult {\n const headers: Record<string, string> = { \"X-Content-Type-Options\": \"nosniff\" };\n const origin = req.headers[\"origin\"];\n const isAllowed = !!origin && allowedOrigins.includes(origin);\n\n if (!isAllowed) {\n if (req.method === \"OPTIONS\") return { outcome: \"preflight\", headers, status: 403 };\n return { outcome: \"forbidden\", headers };\n }\n\n headers[\"Access-Control-Allow-Origin\"] = origin;\n headers[\"Access-Control-Allow-Methods\"] = \"POST, OPTIONS\";\n headers[\"Access-Control-Allow-Headers\"] = \"Content-Type\";\n\n if (req.method === \"OPTIONS\") return { outcome: \"preflight\", headers, status: 204 };\n return { outcome: \"ok\", headers };\n}\n"],"mappings":";AAQA,SAAgB,aAAa,KAAqB,gBAAsC;CACtF,MAAM,UAAkC,EAAE,0BAA0B,UAAU;CAC9E,MAAM,SAAS,IAAI,QAAQ;CAG3B,IAAI,EAFc,CAAC,CAAC,UAAU,eAAe,SAAS,MAAM,IAE5C;EACd,IAAI,IAAI,WAAW,WAAW,OAAO;GAAE,SAAS;GAAa;GAAS,QAAQ;EAAI;EAClF,OAAO;GAAE,SAAS;GAAa;EAAQ;CACzC;CAEA,QAAQ,iCAAiC;CACzC,QAAQ,kCAAkC;CAC1C,QAAQ,kCAAkC;CAE1C,IAAI,IAAI,WAAW,WAAW,OAAO;EAAE,SAAS;EAAa;EAAS,QAAQ;CAAI;CAClF,OAAO;EAAE,SAAS;EAAM;CAAQ;AAClC"}
@@ -0,0 +1,12 @@
1
+ import { EmailBody, EmailProvider } from "./types.mjs";
2
+
3
+ //#region src/email.d.ts
4
+ interface EmailConfig {
5
+ provider: EmailProvider;
6
+ from: string;
7
+ to: string[];
8
+ }
9
+ declare function sendEmail(config: EmailConfig, body: EmailBody): Promise<void>;
10
+ //#endregion
11
+ export { EmailConfig, sendEmail };
12
+ //# sourceMappingURL=email.d.mts.map
package/dist/email.mjs ADDED
@@ -0,0 +1,18 @@
1
+ //#region src/email.ts
2
+ async function sendEmail(config, body) {
3
+ const safeSubject = body.subject?.replace(/[\r\n]+/g, " ").trim() ?? "New message";
4
+ const safeName = body.name?.replace(/[\r\n]+/g, " ").trim();
5
+ const fromLine = safeName ? `${safeName} <${body.email}>` : body.email;
6
+ const payload = {
7
+ from: config.from,
8
+ to: config.to,
9
+ replyTo: body.email,
10
+ subject: `Contact form: ${safeSubject}`,
11
+ text: `From: ${fromLine}\n\n${body.message.trim()}`
12
+ };
13
+ await config.provider.send(payload);
14
+ }
15
+ //#endregion
16
+ export { sendEmail };
17
+
18
+ //# sourceMappingURL=email.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"email.mjs","names":[],"sources":["../src/email.ts"],"sourcesContent":["import type { EmailProvider, EmailPayload, EmailBody } from \"./types.js\";\n\nexport interface EmailConfig {\n provider: EmailProvider;\n from: string;\n to: string[];\n}\n\nexport async function sendEmail(\n config: EmailConfig,\n body: EmailBody\n): Promise<void> {\n const safeSubject = body.subject?.replace(/[\\r\\n]+/g, \" \").trim() ?? \"New message\";\n const safeName = body.name?.replace(/[\\r\\n]+/g, \" \").trim();\n const fromLine = safeName ? `${safeName} <${body.email}>` : body.email;\n\n const payload: EmailPayload = {\n from: config.from,\n to: config.to,\n replyTo: body.email,\n subject: `Contact form: ${safeSubject}`,\n text: `From: ${fromLine}\\n\\n${body.message.trim()}`\n }\n\n await config.provider.send(payload);\n}\n"],"mappings":";AAQA,eAAsB,UACpB,QACA,MACe;CACf,MAAM,cAAc,KAAK,SAAS,QAAQ,YAAY,GAAG,CAAC,CAAC,KAAK,KAAK;CACrE,MAAM,WAAW,KAAK,MAAM,QAAQ,YAAY,GAAG,CAAC,CAAC,KAAK;CAC1D,MAAM,WAAW,WAAW,GAAG,SAAS,IAAI,KAAK,MAAM,KAAK,KAAK;CAEjE,MAAM,UAAwB;EAC5B,MAAM,OAAO;EACb,IAAI,OAAO;EACX,SAAS,KAAK;EACd,SAAS,iBAAiB;EAC1B,MAAM,SAAS,SAAS,MAAM,KAAK,QAAQ,KAAK;CAClD;CAEA,MAAM,OAAO,SAAS,KAAK,OAAO;AACpC"}
@@ -0,0 +1,11 @@
1
+ import { ContactRequest, ContactResult } from "./contact.mjs";
2
+ import { EmailConfig } from "./email.mjs";
3
+
4
+ //#region src/handler.d.ts
5
+ interface HandleContactDeps {
6
+ emailConfig: EmailConfig | null;
7
+ }
8
+ declare function handleContact(req: ContactRequest, deps: HandleContactDeps): Promise<ContactResult>;
9
+ //#endregion
10
+ export { HandleContactDeps, handleContact };
11
+ //# sourceMappingURL=handler.d.mts.map
@@ -0,0 +1,52 @@
1
+ import { sendEmail } from "./email.mjs";
2
+ import { isValidBody } from "./validation.mjs";
3
+ //#region src/handler.ts
4
+ async function handleContact(req, deps) {
5
+ if (req.method !== "POST") return {
6
+ status: 405,
7
+ body: { error: "Method not allowed" }
8
+ };
9
+ if (!req.headers["content-type"]?.startsWith("application/json")) return {
10
+ status: 415,
11
+ body: { error: "Unsupported Media Type" }
12
+ };
13
+ const body = req.body;
14
+ if (typeof body?.["fax_number"] === "string" ? body["fax_number"].trim() : "") {
15
+ console.warn("Honeypot triggered");
16
+ return {
17
+ status: 200,
18
+ body: {
19
+ success: true,
20
+ message: "Message sent successfully"
21
+ }
22
+ };
23
+ }
24
+ if (!deps.emailConfig) return {
25
+ status: 503,
26
+ body: { error: "Service temporarily unavailable" }
27
+ };
28
+ if (!isValidBody(req.body)) return {
29
+ status: 400,
30
+ body: { error: "Invalid or missing fields" }
31
+ };
32
+ try {
33
+ await sendEmail(deps.emailConfig, req.body);
34
+ return {
35
+ status: 200,
36
+ body: {
37
+ success: true,
38
+ message: "Message sent successfully"
39
+ }
40
+ };
41
+ } catch (error) {
42
+ console.error("Email error:", error);
43
+ return {
44
+ status: 500,
45
+ body: { error: "Message delivery failed. Please try again later" }
46
+ };
47
+ }
48
+ }
49
+ //#endregion
50
+ export { handleContact };
51
+
52
+ //# sourceMappingURL=handler.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handler.mjs","names":[],"sources":["../src/handler.ts"],"sourcesContent":["import type { ContactRequest, ContactResult } from \"./contact.js\";\nimport type { EmailConfig } from \"./email.js\";\nimport { isValidBody } from \"./validation.js\";\nimport { sendEmail } from \"./email.js\";\n\nexport interface HandleContactDeps {\n emailConfig: EmailConfig | null;\n}\n\nexport async function handleContact(\n req: ContactRequest,\n deps: HandleContactDeps\n): Promise<ContactResult> {\n if (req.method !== \"POST\") {\n return { status: 405, body: { error: \"Method not allowed\" } };\n }\n\n if (!req.headers[\"content-type\"]?.startsWith(\"application/json\")) {\n return { status: 415, body: { error: \"Unsupported Media Type\" } };\n }\n\n const body = req.body as Record<string, unknown> | undefined;\n const faxNumber = typeof body?.[\"fax_number\"] === \"string\" ? (body[\"fax_number\"] as string).trim() : \"\";\n if (faxNumber) {\n console.warn(\"Honeypot triggered\");\n return { status: 200, body: { success: true, message: \"Message sent successfully\" } };\n }\n\n if (!deps.emailConfig) {\n return { status: 503, body: { error: \"Service temporarily unavailable\" } };\n }\n\n if (!isValidBody(req.body)) {\n return { status: 400, body: { error: \"Invalid or missing fields\" } };\n }\n\n try {\n await sendEmail(deps.emailConfig, req.body);\n return { status: 200, body: { success: true, message: \"Message sent successfully\" } };\n } catch (error) {\n console.error(\"Email error:\", error);\n return { status: 500, body: { error: \"Message delivery failed. Please try again later\" } };\n }\n}\n"],"mappings":";;;AASA,eAAsB,cACpB,KACA,MACwB;CACxB,IAAI,IAAI,WAAW,QACjB,OAAO;EAAE,QAAQ;EAAK,MAAM,EAAE,OAAO,qBAAqB;CAAE;CAG9D,IAAI,CAAC,IAAI,QAAQ,eAAe,EAAE,WAAW,kBAAkB,GAC7D,OAAO;EAAE,QAAQ;EAAK,MAAM,EAAE,OAAO,yBAAyB;CAAE;CAGlE,MAAM,OAAO,IAAI;CAEjB,IADkB,OAAO,OAAO,kBAAkB,WAAY,KAAK,aAAa,CAAY,KAAK,IAAI,IACtF;EACb,QAAQ,KAAK,oBAAoB;EACjC,OAAO;GAAE,QAAQ;GAAK,MAAM;IAAE,SAAS;IAAM,SAAS;GAA4B;EAAE;CACtF;CAEA,IAAI,CAAC,KAAK,aACR,OAAO;EAAE,QAAQ;EAAK,MAAM,EAAE,OAAO,kCAAkC;CAAE;CAG3E,IAAI,CAAC,YAAY,IAAI,IAAI,GACvB,OAAO;EAAE,QAAQ;EAAK,MAAM,EAAE,OAAO,4BAA4B;CAAE;CAGrE,IAAI;EACF,MAAM,UAAU,KAAK,aAAa,IAAI,IAAI;EAC1C,OAAO;GAAE,QAAQ;GAAK,MAAM;IAAE,SAAS;IAAM,SAAS;GAA4B;EAAE;CACtF,SAAS,OAAO;EACd,QAAQ,MAAM,gBAAgB,KAAK;EACnC,OAAO;GAAE,QAAQ;GAAK,MAAM,EAAE,OAAO,kDAAkD;EAAE;CAC3F;AACF"}
@@ -0,0 +1,22 @@
1
+ //#region src/types.d.ts
2
+ interface EmailBody {
3
+ email: string;
4
+ message: string;
5
+ subject?: string;
6
+ name?: string;
7
+ fax_number?: string;
8
+ }
9
+ interface EmailPayload {
10
+ from: string;
11
+ to: string[];
12
+ replyTo: string;
13
+ subject: string;
14
+ text: string;
15
+ }
16
+ interface EmailProvider {
17
+ readonly id: string;
18
+ send(body: EmailPayload): Promise<void>;
19
+ }
20
+ //#endregion
21
+ export { EmailBody, EmailPayload, EmailProvider };
22
+ //# sourceMappingURL=types.d.mts.map
package/dist/types.mjs ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { EmailBody } from "./types.mjs";
2
+
3
+ //#region src/validation.d.ts
4
+ declare const EMAIL_REGEX: RegExp;
5
+ declare function isValidBody(body: unknown): body is EmailBody;
6
+ //#endregion
7
+ export { EMAIL_REGEX, isValidBody };
8
+ //# sourceMappingURL=validation.d.mts.map
@@ -0,0 +1,11 @@
1
+ //#region src/validation.ts
2
+ const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
3
+ function isValidBody(body) {
4
+ if (body === null || typeof body !== "object") return false;
5
+ const { email, message, subject, name, fax_number } = body;
6
+ return typeof email === "string" && EMAIL_REGEX.test(email) && typeof message === "string" && !!message.trim() && message.length <= 2e3 && (subject === void 0 || typeof subject === "string" && subject.length <= 200) && (name === void 0 || typeof name === "string" && name.length <= 100) && (fax_number === void 0 || typeof fax_number === "string");
7
+ }
8
+ //#endregion
9
+ export { EMAIL_REGEX, isValidBody };
10
+
11
+ //# sourceMappingURL=validation.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.mjs","names":["record"],"sources":["../src/validation.ts"],"sourcesContent":["import type { EmailBody } from \"./types.js\";\n\nexport const EMAIL_REGEX = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n\nexport function isValidBody(body: unknown): body is EmailBody {\n if (body === null || typeof body !== \"object\") return false;\n const record = body as Record<string, unknown>;\n const { email, message, subject, name, fax_number } = record;\n return (\n typeof email === \"string\"\n && EMAIL_REGEX.test(email)\n && typeof message === \"string\"\n && !!message.trim()\n && message.length <= 2000\n && (subject === undefined || (typeof subject === \"string\" && subject.length <= 200))\n && (name === undefined || (typeof name === \"string\" && name.length <= 100))\n && (fax_number === undefined || typeof fax_number === \"string\")\n );\n}\n\n"],"mappings":";AAEA,MAAa,cAAc;AAE3B,SAAgB,YAAY,MAAkC;CAC5D,IAAI,SAAS,QAAQ,OAAO,SAAS,UAAU,OAAO;CAEtD,MAAM,EAAE,OAAO,SAAS,SAAS,MAAM,eAAeA;CACtD,OACE,OAAO,UAAU,YACjB,YAAY,KAAK,KAAK,KACtB,OAAO,YAAY,YACnB,CAAC,CAAC,QAAQ,KAAK,KACf,QAAQ,UAAU,QACjB,YAAY,KAAA,KAAc,OAAO,YAAY,YAAY,QAAQ,UAAU,SAC3E,SAAS,KAAA,KAAc,OAAO,SAAS,YAAY,KAAK,UAAU,SAClE,eAAe,KAAA,KAAa,OAAO,eAAe;AAEvD"}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@contact-api/core",
3
+ "description": "Contact form logic for the contact-api ecosystem",
4
+ "author": "Mason L'Etoile",
5
+ "version": "0.0.1",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/contact-api/core.git"
10
+ },
11
+ "homepage": "https://github.com/contact-api/core#readme",
12
+ "bugs": { "url": "https://github.com/contact-api/core/issues" },
13
+ "keywords": ["contact-form", "cors", "validation", "honeypot", "typescript", "contact-api"],
14
+ "type": "module",
15
+ "scripts": {
16
+ "typecheck": "tsc --noEmit",
17
+ "test": "vitest",
18
+ "test:watch": "vitest --watch",
19
+ "test:coverage": "vitest --coverage",
20
+ "build": "tsdown",
21
+ "prepare": "npm run build"
22
+ },
23
+ "files": ["dist"],
24
+ "exports": {
25
+ "./types": {
26
+ "types": "./dist/types.d.mts",
27
+ "import": "./dist/types.mjs"
28
+ },
29
+ "./email": {
30
+ "types": "./dist/email.d.mts",
31
+ "import": "./dist/email.mjs"
32
+ },
33
+ "./contact": {
34
+ "types": "./dist/contact.d.mts",
35
+ "import": "./dist/contact.mjs"
36
+ },
37
+ "./cors": {
38
+ "types": "./dist/cors.d.mts",
39
+ "import": "./dist/cors.mjs"
40
+ },
41
+ "./handler": {
42
+ "types": "./dist/handler.d.mts",
43
+ "import": "./dist/handler.mjs"
44
+ },
45
+ "./validation": {
46
+ "types": "./dist/validation.d.mts",
47
+ "import": "./dist/validation.mjs"
48
+ }
49
+ },
50
+ "devDependencies": {
51
+ "@types/node": "^25.5.0",
52
+ "@vitest/coverage-v8": "^4.1.0",
53
+ "typescript": "^5.9.3",
54
+ "vitest": "^4.1.0",
55
+ "tsdown": "^0.22.0"
56
+ }
57
+ }