@formgong/next 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +115 -2
- package/dist/chunk-F6CJPVBM.js +79 -0
- package/dist/client.cjs +76 -0
- package/dist/client.d.cts +31 -0
- package/dist/client.d.ts +31 -0
- package/dist/client.js +34 -0
- package/dist/index.cjs +127 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/server.cjs +116 -0
- package/dist/server.d.cts +59 -0
- package/dist/server.d.ts +59 -0
- package/dist/server.js +12 -0
- package/package.json +110 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Formgong
|
|
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
CHANGED
|
@@ -1,3 +1,116 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @formgong/next
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@formgong/next) [](https://bundlephobia.com/package/@formgong/next) [](https://www.npmjs.com/package/@formgong/next) [](https://github.com/formgong/js/actions/workflows/ci.yml) [](https://github.com/formgong/js/blob/main/LICENSE)
|
|
4
|
+
|
|
5
|
+
> Contact forms for **Next.js** with [Formgong](https://formgong.com). Use a Server Action or Route Handler helper, or App Router components that post straight from the browser. Submissions go to email, Telegram and webhooks. You don't need a database, Resend or SMTP.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @formgong/next
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Option A: browser-direct (no server code)
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
// app/contact/page.tsx (a Server Component is fine: the form is a client component)
|
|
15
|
+
import { ContactForm } from "@formgong/next/client";
|
|
16
|
+
|
|
17
|
+
export default function Page() {
|
|
18
|
+
return <ContactForm accessKey={process.env.NEXT_PUBLIC_FORMGONG_ACCESS_KEY!} />;
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`@formgong/next/client` re-exports everything from [`@formgong/react`](https://www.npmjs.com/package/@formgong/react).
|
|
23
|
+
|
|
24
|
+
## Option B: Server Action (`useActionState`)
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// app/contact/actions.ts
|
|
28
|
+
"use server";
|
|
29
|
+
import { submitToFormgong, type FormgongActionState } from "@formgong/next/server";
|
|
30
|
+
|
|
31
|
+
export async function contact(_prev: FormgongActionState, formData: FormData) {
|
|
32
|
+
return submitToFormgong(formData, {
|
|
33
|
+
subject: "New message from the website",
|
|
34
|
+
validate: (data) => (String(data.get("message")).length < 5 ? "Please write a little more." : null),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
// app/contact/page.tsx
|
|
41
|
+
import { ActionForm } from "@formgong/next/client";
|
|
42
|
+
import { contact } from "./actions";
|
|
43
|
+
|
|
44
|
+
export default function Page() {
|
|
45
|
+
return (
|
|
46
|
+
<ActionForm action={contact}>
|
|
47
|
+
<input name="email" type="email" required />
|
|
48
|
+
<textarea name="message" required />
|
|
49
|
+
<button>Send</button>
|
|
50
|
+
</ActionForm>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Set `FORMGONG_ACCESS_KEY=fk_…` in `.env.local`. `submitToFormgong` never throws. It returns a serialisable `{ status: "success" | "error", message, id?, code? }`, so you can render it directly.
|
|
56
|
+
|
|
57
|
+
`<ActionForm>` adds the honeypot and an `aria-live` status line, and uses `useActionState` (React 19), falling back to `useFormState` on Next 14. Use `useFormgongAction(action)` to build your own UI.
|
|
58
|
+
|
|
59
|
+
## Option C: Route Handler
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
// app/api/contact/route.ts
|
|
63
|
+
import { formgongRoute } from "@formgong/next/server";
|
|
64
|
+
export const POST = formgongRoute({ subject: "Website lead" });
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Server helper options
|
|
68
|
+
|
|
69
|
+
| Option | Default | Description |
|
|
70
|
+
| --- | --- | --- |
|
|
71
|
+
| `accessKey` | `FORMGONG_ACCESS_KEY`, then `NEXT_PUBLIC_FORMGONG_ACCESS_KEY` | Public form key. |
|
|
72
|
+
| `endpoint` | `FORMGONG_ENDPOINT`, then `https://formgong.com/submit` | Submit URL. |
|
|
73
|
+
| `forwardHeaders` | `true` | Forwards the visitor's `Origin`/`Referer` (needed for allowed-domain forms) and `Accept-Language`. |
|
|
74
|
+
| `validate(formData)` | – | Return an error message to reject the submission before it is sent. |
|
|
75
|
+
| `subject`, `replyTo`, `fromName`, `lang`, `honeypot`, `timeoutMs` | – | Same as [`@formgong/core`](https://www.npmjs.com/package/@formgong/core). |
|
|
76
|
+
|
|
77
|
+
Notes:
|
|
78
|
+
|
|
79
|
+
- Server posts carry no browser behaviour signals, and Formgong never penalises them for that. Next.js internal `$ACTION_*` fields are stripped.
|
|
80
|
+
- Rate limits then apply to your server's IP (20 per minute) and to the form (30 per minute). For high-traffic public forms, Option A spreads the load across visitors.
|
|
81
|
+
- Turnstile-protected forms need the widget token in the browser. Use Option A with `turnstileSiteKey`.
|
|
82
|
+
|
|
83
|
+
A full example lives in [`examples/next-server-action`](https://github.com/formgong/js/tree/main/examples/next-server-action). For a whole starter project, run `npm create formgong@latest -- --template nextjs`. More: [formgong.com/en/for/next/](https://formgong.com/en/for/next/).
|
|
84
|
+
|
|
85
|
+
## Why Formgong
|
|
86
|
+
|
|
87
|
+
Formgong is a hosted form backend with a free plan, built for static and AI-built sites. Submissions go to **email and Telegram**, plus webhooks for Make, n8n and Zapier. Data is stored in the EU, and messages and autoreplies come in 12 languages. Spam filtering includes a honeypot, optional Cloudflare Turnstile and cookie-free behaviour signals, with no stored fingerprints.
|
|
88
|
+
|
|
89
|
+
How it compares with Formspree, Web3Forms, Basin, Getform, FormSubmit and Netlify Forms: **[formgong.com/en/compare](https://formgong.com/en/compare/)**. Moving from Formspree takes one line: change the form `action` or the access key ([guide](https://formgong.com/en/formspree-alternative/)).
|
|
90
|
+
|
|
91
|
+
## Packages
|
|
92
|
+
|
|
93
|
+
| Package | What it is |
|
|
94
|
+
| --- | --- |
|
|
95
|
+
| [`@formgong/core`](https://www.npmjs.com/package/@formgong/core) | Tiny typed client: `submit()`, typed errors, anti-spam tracker, account API |
|
|
96
|
+
| [`@formgong/react`](https://www.npmjs.com/package/@formgong/react) | `<ContactForm>`, `<FormgongForm>`, `useFormgong()` for React, Lovable, Bolt and v0 |
|
|
97
|
+
| [`@formgong/next`](https://www.npmjs.com/package/@formgong/next) | Next.js Server Action and Route Handler helpers, plus App Router components |
|
|
98
|
+
| [`@formgong/vue`](https://www.npmjs.com/package/@formgong/vue) | Vue 3 and Nuxt components and the `useFormgong()` composable |
|
|
99
|
+
| [`@formgong/svelte`](https://www.npmjs.com/package/@formgong/svelte) | `use:formgong` action, store helper, Svelte components |
|
|
100
|
+
| [`@formgong/astro`](https://www.npmjs.com/package/@formgong/astro) | Astro components with progressive enhancement, plus a helper for Actions |
|
|
101
|
+
| [`@formgong/angular`](https://www.npmjs.com/package/@formgong/angular) | Angular 17+ standalone component, `formgongForm` directive and `FormgongService` with signals |
|
|
102
|
+
| [`formgong`](https://www.npmjs.com/package/formgong) | CLI: `npx formgong init` creates a form and adds it to your project |
|
|
103
|
+
| [`create-formgong`](https://www.npmjs.com/package/create-formgong) | `npm create formgong@latest`: starter projects |
|
|
104
|
+
|
|
105
|
+
## Links
|
|
106
|
+
|
|
107
|
+
- Docs: https://formgong.com/en/docs/
|
|
108
|
+
- MCP server for Cursor, Claude and VS Code: https://formgong.com/en/docs/mcp/
|
|
109
|
+
- Form generator with a React tab: https://formgong.com/en/tools/form-generator/
|
|
110
|
+
- Source: https://github.com/formgong/js
|
|
111
|
+
- Issues: https://github.com/formgong/js/issues
|
|
112
|
+
- Support: support@formgong.com
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
MIT © Formgong
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/server.ts
|
|
2
|
+
import { FormgongError, submit } from "@formgong/core";
|
|
3
|
+
var initialFormgongState = { status: "idle", message: "" };
|
|
4
|
+
var CLIENT_ONLY_FIELDS = ["_fg_b", "_fg_pow", "$ACTION_ID", "$ACTION_REF"];
|
|
5
|
+
function env(name) {
|
|
6
|
+
const value = globalThis.process?.env?.[name];
|
|
7
|
+
return value?.trim() || void 0;
|
|
8
|
+
}
|
|
9
|
+
async function visitorHeaders() {
|
|
10
|
+
try {
|
|
11
|
+
const mod = await import("next/headers");
|
|
12
|
+
const list = await mod.headers();
|
|
13
|
+
const out = {};
|
|
14
|
+
const origin = list.get("origin") || (() => {
|
|
15
|
+
try {
|
|
16
|
+
const ref = list.get("referer");
|
|
17
|
+
return ref ? new URL(ref).origin : null;
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
})();
|
|
22
|
+
if (origin) out.Origin = origin;
|
|
23
|
+
const referer = list.get("referer");
|
|
24
|
+
if (referer) out.Referer = referer;
|
|
25
|
+
const language = list.get("accept-language");
|
|
26
|
+
if (language) out["Accept-Language"] = language;
|
|
27
|
+
return out;
|
|
28
|
+
} catch {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function submitToFormgong(data, options = {}) {
|
|
33
|
+
const { accessKey = env("FORMGONG_ACCESS_KEY") ?? env("NEXT_PUBLIC_FORMGONG_ACCESS_KEY"), forwardHeaders = true, validate, ...rest } = options;
|
|
34
|
+
const body = new FormData();
|
|
35
|
+
if (data instanceof FormData) {
|
|
36
|
+
for (const [k, v] of data) if (!CLIENT_ONLY_FIELDS.includes(k) && !k.startsWith("$ACTION_")) body.append(k, v);
|
|
37
|
+
} else for (const [k, v] of Object.entries(data)) body.append(k, v);
|
|
38
|
+
if (validate) {
|
|
39
|
+
const problem = await validate(body);
|
|
40
|
+
if (problem) return { status: "error", message: problem, code: "validation_failed" };
|
|
41
|
+
}
|
|
42
|
+
const headers = forwardHeaders ? { ...await visitorHeaders(), ...rest.headers } : rest.headers;
|
|
43
|
+
try {
|
|
44
|
+
const result = await submit(accessKey ?? "", body, { ...rest, endpoint: rest.endpoint ?? env("FORMGONG_ENDPOINT"), headers, origin: rest.origin });
|
|
45
|
+
return { status: "success", message: result.message || "Thank you! Your message has been sent.", id: result.id };
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error instanceof FormgongError) return { status: "error", message: error.message, code: error.code };
|
|
48
|
+
return { status: "error", message: "Something went wrong. Please try again.", code: "unknown_error" };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function createFormgongAction(options = {}) {
|
|
52
|
+
return async function formgongAction(_prev, formData) {
|
|
53
|
+
return submitToFormgong(formData, options);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function formgongRoute(options = {}) {
|
|
57
|
+
return async function POST(request) {
|
|
58
|
+
let data;
|
|
59
|
+
try {
|
|
60
|
+
data = await request.formData();
|
|
61
|
+
} catch {
|
|
62
|
+
return Response.json({ success: false, code: "unsupported_type", message: "Send multipart/form-data or urlencoded." }, { status: 415 });
|
|
63
|
+
}
|
|
64
|
+
const headers = {};
|
|
65
|
+
for (const name of ["origin", "referer", "accept-language"]) {
|
|
66
|
+
const v = request.headers.get(name);
|
|
67
|
+
if (v) headers[name] = v;
|
|
68
|
+
}
|
|
69
|
+
const state = await submitToFormgong(data, { forwardHeaders: false, ...options, headers: { ...headers, ...options.headers } });
|
|
70
|
+
return Response.json(state.status === "success" ? { success: true, id: state.id, message: state.message } : { success: false, code: state.code, message: state.message }, { status: state.status === "success" ? 200 : 400 });
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export {
|
|
75
|
+
initialFormgongState,
|
|
76
|
+
submitToFormgong,
|
|
77
|
+
createFormgongAction,
|
|
78
|
+
formgongRoute
|
|
79
|
+
};
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/client.tsx
|
|
32
|
+
var client_exports = {};
|
|
33
|
+
__export(client_exports, {
|
|
34
|
+
ActionForm: () => ActionForm,
|
|
35
|
+
ContactForm: () => import_react.ContactForm,
|
|
36
|
+
DEFAULT_ENDPOINT: () => import_react.DEFAULT_ENDPOINT,
|
|
37
|
+
FormgongError: () => import_react.FormgongError,
|
|
38
|
+
FormgongForm: () => import_react.FormgongForm,
|
|
39
|
+
Honeypot: () => import_react.Honeypot,
|
|
40
|
+
default: () => import_react2.default,
|
|
41
|
+
isFormgongError: () => import_react.isFormgongError,
|
|
42
|
+
useFormgong: () => import_react.useFormgong,
|
|
43
|
+
useFormgongAction: () => useFormgongAction
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(client_exports);
|
|
46
|
+
var React = __toESM(require("react"), 1);
|
|
47
|
+
var ReactDOM = __toESM(require("react-dom"), 1);
|
|
48
|
+
var import_react = require("@formgong/react");
|
|
49
|
+
var import_react2 = __toESM(require("@formgong/react"), 1);
|
|
50
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
51
|
+
function useFormgongAction(action) {
|
|
52
|
+
const hook = React.useActionState ?? ReactDOM.useFormState;
|
|
53
|
+
const [state, formAction, pending] = hook(action, { status: "idle", message: "" });
|
|
54
|
+
return { state, formAction, pending: Boolean(pending) };
|
|
55
|
+
}
|
|
56
|
+
var honeypotStyle = { position: "absolute", left: -1e4, width: 1, height: 1, overflow: "hidden" };
|
|
57
|
+
function ActionForm({ action, children, hideStatus, statusClassName, ...rest }) {
|
|
58
|
+
const { state, formAction, pending } = useFormgongAction(action);
|
|
59
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("form", { ...rest, action: formAction, "aria-busy": pending, children: [
|
|
60
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { "aria-hidden": "true", style: honeypotStyle, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { name: "botcheck", tabIndex: -1, autoComplete: "off" }) }),
|
|
61
|
+
typeof children === "function" ? children({ state, pending }) : children,
|
|
62
|
+
hideStatus ? null : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { role: "status", "aria-live": "polite", className: statusClassName, "data-status": state.status, style: statusClassName ? void 0 : { margin: 0, fontSize: 14, color: state.status === "error" ? "#b91c1c" : "#15803d" }, children: state.message })
|
|
63
|
+
] });
|
|
64
|
+
}
|
|
65
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
66
|
+
0 && (module.exports = {
|
|
67
|
+
ActionForm,
|
|
68
|
+
ContactForm,
|
|
69
|
+
DEFAULT_ENDPOINT,
|
|
70
|
+
FormgongError,
|
|
71
|
+
FormgongForm,
|
|
72
|
+
Honeypot,
|
|
73
|
+
isFormgongError,
|
|
74
|
+
useFormgong,
|
|
75
|
+
useFormgongAction
|
|
76
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { FormgongActionState } from './server.cjs';
|
|
3
|
+
export { ContactForm, ContactFormLabels, ContactFormProps, DEFAULT_ENDPOINT, FormgongError, FormgongForm, FormgongFormProps, FormgongOptions, FormgongResult, FormgongStatus, Honeypot, UseFormgong, default, isFormgongError, useFormgong } from '@formgong/react';
|
|
4
|
+
import '@formgong/core';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @formgong/next/client: client components for the App Router.
|
|
8
|
+
* - Re-exports @formgong/react (<ContactForm>, <FormgongForm>, useFormgong) for direct browser posts.
|
|
9
|
+
* - <ActionForm action={serverAction}> for posting through your own Server Action.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
type ActionFn = (state: FormgongActionState, payload: FormData) => FormgongActionState | Promise<FormgongActionState>;
|
|
13
|
+
/** React 19 `useActionState`, falling back to React 18 `useFormState` (Next 14). */
|
|
14
|
+
declare function useFormgongAction(action: ActionFn): {
|
|
15
|
+
state: FormgongActionState;
|
|
16
|
+
formAction: (payload: FormData) => void;
|
|
17
|
+
pending: boolean;
|
|
18
|
+
};
|
|
19
|
+
type ActionFormProps = Omit<React.FormHTMLAttributes<HTMLFormElement>, "action" | "children"> & {
|
|
20
|
+
action: ActionFn;
|
|
21
|
+
children: React.ReactNode | ((state: {
|
|
22
|
+
state: FormgongActionState;
|
|
23
|
+
pending: boolean;
|
|
24
|
+
}) => React.ReactNode);
|
|
25
|
+
hideStatus?: boolean;
|
|
26
|
+
statusClassName?: string;
|
|
27
|
+
};
|
|
28
|
+
/** `<ActionForm action={contact}>…fields…</ActionForm>`: honeypot + status line around a Server Action. */
|
|
29
|
+
declare function ActionForm({ action, children, hideStatus, statusClassName, ...rest }: ActionFormProps): React.JSX.Element;
|
|
30
|
+
|
|
31
|
+
export { ActionForm, type ActionFormProps, useFormgongAction };
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { FormgongActionState } from './server.js';
|
|
3
|
+
export { ContactForm, ContactFormLabels, ContactFormProps, DEFAULT_ENDPOINT, FormgongError, FormgongForm, FormgongFormProps, FormgongOptions, FormgongResult, FormgongStatus, Honeypot, UseFormgong, default, isFormgongError, useFormgong } from '@formgong/react';
|
|
4
|
+
import '@formgong/core';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @formgong/next/client: client components for the App Router.
|
|
8
|
+
* - Re-exports @formgong/react (<ContactForm>, <FormgongForm>, useFormgong) for direct browser posts.
|
|
9
|
+
* - <ActionForm action={serverAction}> for posting through your own Server Action.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
type ActionFn = (state: FormgongActionState, payload: FormData) => FormgongActionState | Promise<FormgongActionState>;
|
|
13
|
+
/** React 19 `useActionState`, falling back to React 18 `useFormState` (Next 14). */
|
|
14
|
+
declare function useFormgongAction(action: ActionFn): {
|
|
15
|
+
state: FormgongActionState;
|
|
16
|
+
formAction: (payload: FormData) => void;
|
|
17
|
+
pending: boolean;
|
|
18
|
+
};
|
|
19
|
+
type ActionFormProps = Omit<React.FormHTMLAttributes<HTMLFormElement>, "action" | "children"> & {
|
|
20
|
+
action: ActionFn;
|
|
21
|
+
children: React.ReactNode | ((state: {
|
|
22
|
+
state: FormgongActionState;
|
|
23
|
+
pending: boolean;
|
|
24
|
+
}) => React.ReactNode);
|
|
25
|
+
hideStatus?: boolean;
|
|
26
|
+
statusClassName?: string;
|
|
27
|
+
};
|
|
28
|
+
/** `<ActionForm action={contact}>…fields…</ActionForm>`: honeypot + status line around a Server Action. */
|
|
29
|
+
declare function ActionForm({ action, children, hideStatus, statusClassName, ...rest }: ActionFormProps): React.JSX.Element;
|
|
30
|
+
|
|
31
|
+
export { ActionForm, type ActionFormProps, useFormgongAction };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/client.tsx
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import * as ReactDOM from "react-dom";
|
|
6
|
+
import { ContactForm, FormgongForm, Honeypot, useFormgong, DEFAULT_ENDPOINT, FormgongError, isFormgongError } from "@formgong/react";
|
|
7
|
+
import { default as default2 } from "@formgong/react";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
function useFormgongAction(action) {
|
|
10
|
+
const hook = React.useActionState ?? ReactDOM.useFormState;
|
|
11
|
+
const [state, formAction, pending] = hook(action, { status: "idle", message: "" });
|
|
12
|
+
return { state, formAction, pending: Boolean(pending) };
|
|
13
|
+
}
|
|
14
|
+
var honeypotStyle = { position: "absolute", left: -1e4, width: 1, height: 1, overflow: "hidden" };
|
|
15
|
+
function ActionForm({ action, children, hideStatus, statusClassName, ...rest }) {
|
|
16
|
+
const { state, formAction, pending } = useFormgongAction(action);
|
|
17
|
+
return /* @__PURE__ */ jsxs("form", { ...rest, action: formAction, "aria-busy": pending, children: [
|
|
18
|
+
/* @__PURE__ */ jsx("div", { "aria-hidden": "true", style: honeypotStyle, children: /* @__PURE__ */ jsx("input", { name: "botcheck", tabIndex: -1, autoComplete: "off" }) }),
|
|
19
|
+
typeof children === "function" ? children({ state, pending }) : children,
|
|
20
|
+
hideStatus ? null : /* @__PURE__ */ jsx("p", { role: "status", "aria-live": "polite", className: statusClassName, "data-status": state.status, style: statusClassName ? void 0 : { margin: 0, fontSize: 14, color: state.status === "error" ? "#b91c1c" : "#15803d" }, children: state.message })
|
|
21
|
+
] });
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
ActionForm,
|
|
25
|
+
ContactForm,
|
|
26
|
+
DEFAULT_ENDPOINT,
|
|
27
|
+
FormgongError,
|
|
28
|
+
FormgongForm,
|
|
29
|
+
Honeypot,
|
|
30
|
+
default2 as default,
|
|
31
|
+
isFormgongError,
|
|
32
|
+
useFormgong,
|
|
33
|
+
useFormgongAction
|
|
34
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
DEFAULT_ENDPOINT: () => import_core2.DEFAULT_ENDPOINT,
|
|
34
|
+
FormgongError: () => import_core2.FormgongError,
|
|
35
|
+
createFormgongAction: () => createFormgongAction,
|
|
36
|
+
formgongRoute: () => formgongRoute,
|
|
37
|
+
initialFormgongState: () => initialFormgongState,
|
|
38
|
+
isFormgongError: () => import_core2.isFormgongError,
|
|
39
|
+
submitToFormgong: () => submitToFormgong
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
|
+
|
|
43
|
+
// src/server.ts
|
|
44
|
+
var import_core = require("@formgong/core");
|
|
45
|
+
var initialFormgongState = { status: "idle", message: "" };
|
|
46
|
+
var CLIENT_ONLY_FIELDS = ["_fg_b", "_fg_pow", "$ACTION_ID", "$ACTION_REF"];
|
|
47
|
+
function env(name) {
|
|
48
|
+
const value = globalThis.process?.env?.[name];
|
|
49
|
+
return value?.trim() || void 0;
|
|
50
|
+
}
|
|
51
|
+
async function visitorHeaders() {
|
|
52
|
+
try {
|
|
53
|
+
const mod = await import("next/headers");
|
|
54
|
+
const list = await mod.headers();
|
|
55
|
+
const out = {};
|
|
56
|
+
const origin = list.get("origin") || (() => {
|
|
57
|
+
try {
|
|
58
|
+
const ref = list.get("referer");
|
|
59
|
+
return ref ? new URL(ref).origin : null;
|
|
60
|
+
} catch {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
})();
|
|
64
|
+
if (origin) out.Origin = origin;
|
|
65
|
+
const referer = list.get("referer");
|
|
66
|
+
if (referer) out.Referer = referer;
|
|
67
|
+
const language = list.get("accept-language");
|
|
68
|
+
if (language) out["Accept-Language"] = language;
|
|
69
|
+
return out;
|
|
70
|
+
} catch {
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function submitToFormgong(data, options = {}) {
|
|
75
|
+
const { accessKey = env("FORMGONG_ACCESS_KEY") ?? env("NEXT_PUBLIC_FORMGONG_ACCESS_KEY"), forwardHeaders = true, validate, ...rest } = options;
|
|
76
|
+
const body = new FormData();
|
|
77
|
+
if (data instanceof FormData) {
|
|
78
|
+
for (const [k, v] of data) if (!CLIENT_ONLY_FIELDS.includes(k) && !k.startsWith("$ACTION_")) body.append(k, v);
|
|
79
|
+
} else for (const [k, v] of Object.entries(data)) body.append(k, v);
|
|
80
|
+
if (validate) {
|
|
81
|
+
const problem = await validate(body);
|
|
82
|
+
if (problem) return { status: "error", message: problem, code: "validation_failed" };
|
|
83
|
+
}
|
|
84
|
+
const headers = forwardHeaders ? { ...await visitorHeaders(), ...rest.headers } : rest.headers;
|
|
85
|
+
try {
|
|
86
|
+
const result = await (0, import_core.submit)(accessKey ?? "", body, { ...rest, endpoint: rest.endpoint ?? env("FORMGONG_ENDPOINT"), headers, origin: rest.origin });
|
|
87
|
+
return { status: "success", message: result.message || "Thank you! Your message has been sent.", id: result.id };
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (error instanceof import_core.FormgongError) return { status: "error", message: error.message, code: error.code };
|
|
90
|
+
return { status: "error", message: "Something went wrong. Please try again.", code: "unknown_error" };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function createFormgongAction(options = {}) {
|
|
94
|
+
return async function formgongAction(_prev, formData) {
|
|
95
|
+
return submitToFormgong(formData, options);
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function formgongRoute(options = {}) {
|
|
99
|
+
return async function POST(request) {
|
|
100
|
+
let data;
|
|
101
|
+
try {
|
|
102
|
+
data = await request.formData();
|
|
103
|
+
} catch {
|
|
104
|
+
return Response.json({ success: false, code: "unsupported_type", message: "Send multipart/form-data or urlencoded." }, { status: 415 });
|
|
105
|
+
}
|
|
106
|
+
const headers = {};
|
|
107
|
+
for (const name of ["origin", "referer", "accept-language"]) {
|
|
108
|
+
const v = request.headers.get(name);
|
|
109
|
+
if (v) headers[name] = v;
|
|
110
|
+
}
|
|
111
|
+
const state = await submitToFormgong(data, { forwardHeaders: false, ...options, headers: { ...headers, ...options.headers } });
|
|
112
|
+
return Response.json(state.status === "success" ? { success: true, id: state.id, message: state.message } : { success: false, code: state.code, message: state.message }, { status: state.status === "success" ? 200 : 400 });
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/index.ts
|
|
117
|
+
var import_core2 = require("@formgong/core");
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
DEFAULT_ENDPOINT,
|
|
121
|
+
FormgongError,
|
|
122
|
+
createFormgongAction,
|
|
123
|
+
formgongRoute,
|
|
124
|
+
initialFormgongState,
|
|
125
|
+
isFormgongError,
|
|
126
|
+
submitToFormgong
|
|
127
|
+
});
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createFormgongAction,
|
|
3
|
+
formgongRoute,
|
|
4
|
+
initialFormgongState,
|
|
5
|
+
submitToFormgong
|
|
6
|
+
} from "./chunk-F6CJPVBM.js";
|
|
7
|
+
|
|
8
|
+
// src/index.ts
|
|
9
|
+
import { FormgongError, isFormgongError, DEFAULT_ENDPOINT } from "@formgong/core";
|
|
10
|
+
export {
|
|
11
|
+
DEFAULT_ENDPOINT,
|
|
12
|
+
FormgongError,
|
|
13
|
+
createFormgongAction,
|
|
14
|
+
formgongRoute,
|
|
15
|
+
initialFormgongState,
|
|
16
|
+
isFormgongError,
|
|
17
|
+
submitToFormgong
|
|
18
|
+
};
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/server.ts
|
|
31
|
+
var server_exports = {};
|
|
32
|
+
__export(server_exports, {
|
|
33
|
+
createFormgongAction: () => createFormgongAction,
|
|
34
|
+
formgongRoute: () => formgongRoute,
|
|
35
|
+
initialFormgongState: () => initialFormgongState,
|
|
36
|
+
submitToFormgong: () => submitToFormgong
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(server_exports);
|
|
39
|
+
var import_core = require("@formgong/core");
|
|
40
|
+
var initialFormgongState = { status: "idle", message: "" };
|
|
41
|
+
var CLIENT_ONLY_FIELDS = ["_fg_b", "_fg_pow", "$ACTION_ID", "$ACTION_REF"];
|
|
42
|
+
function env(name) {
|
|
43
|
+
const value = globalThis.process?.env?.[name];
|
|
44
|
+
return value?.trim() || void 0;
|
|
45
|
+
}
|
|
46
|
+
async function visitorHeaders() {
|
|
47
|
+
try {
|
|
48
|
+
const mod = await import("next/headers");
|
|
49
|
+
const list = await mod.headers();
|
|
50
|
+
const out = {};
|
|
51
|
+
const origin = list.get("origin") || (() => {
|
|
52
|
+
try {
|
|
53
|
+
const ref = list.get("referer");
|
|
54
|
+
return ref ? new URL(ref).origin : null;
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
})();
|
|
59
|
+
if (origin) out.Origin = origin;
|
|
60
|
+
const referer = list.get("referer");
|
|
61
|
+
if (referer) out.Referer = referer;
|
|
62
|
+
const language = list.get("accept-language");
|
|
63
|
+
if (language) out["Accept-Language"] = language;
|
|
64
|
+
return out;
|
|
65
|
+
} catch {
|
|
66
|
+
return {};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async function submitToFormgong(data, options = {}) {
|
|
70
|
+
const { accessKey = env("FORMGONG_ACCESS_KEY") ?? env("NEXT_PUBLIC_FORMGONG_ACCESS_KEY"), forwardHeaders = true, validate, ...rest } = options;
|
|
71
|
+
const body = new FormData();
|
|
72
|
+
if (data instanceof FormData) {
|
|
73
|
+
for (const [k, v] of data) if (!CLIENT_ONLY_FIELDS.includes(k) && !k.startsWith("$ACTION_")) body.append(k, v);
|
|
74
|
+
} else for (const [k, v] of Object.entries(data)) body.append(k, v);
|
|
75
|
+
if (validate) {
|
|
76
|
+
const problem = await validate(body);
|
|
77
|
+
if (problem) return { status: "error", message: problem, code: "validation_failed" };
|
|
78
|
+
}
|
|
79
|
+
const headers = forwardHeaders ? { ...await visitorHeaders(), ...rest.headers } : rest.headers;
|
|
80
|
+
try {
|
|
81
|
+
const result = await (0, import_core.submit)(accessKey ?? "", body, { ...rest, endpoint: rest.endpoint ?? env("FORMGONG_ENDPOINT"), headers, origin: rest.origin });
|
|
82
|
+
return { status: "success", message: result.message || "Thank you! Your message has been sent.", id: result.id };
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error instanceof import_core.FormgongError) return { status: "error", message: error.message, code: error.code };
|
|
85
|
+
return { status: "error", message: "Something went wrong. Please try again.", code: "unknown_error" };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function createFormgongAction(options = {}) {
|
|
89
|
+
return async function formgongAction(_prev, formData) {
|
|
90
|
+
return submitToFormgong(formData, options);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function formgongRoute(options = {}) {
|
|
94
|
+
return async function POST(request) {
|
|
95
|
+
let data;
|
|
96
|
+
try {
|
|
97
|
+
data = await request.formData();
|
|
98
|
+
} catch {
|
|
99
|
+
return Response.json({ success: false, code: "unsupported_type", message: "Send multipart/form-data or urlencoded." }, { status: 415 });
|
|
100
|
+
}
|
|
101
|
+
const headers = {};
|
|
102
|
+
for (const name of ["origin", "referer", "accept-language"]) {
|
|
103
|
+
const v = request.headers.get(name);
|
|
104
|
+
if (v) headers[name] = v;
|
|
105
|
+
}
|
|
106
|
+
const state = await submitToFormgong(data, { forwardHeaders: false, ...options, headers: { ...headers, ...options.headers } });
|
|
107
|
+
return Response.json(state.status === "success" ? { success: true, id: state.id, message: state.message } : { success: false, code: state.code, message: state.message }, { status: state.status === "success" ? 200 : 400 });
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
111
|
+
0 && (module.exports = {
|
|
112
|
+
createFormgongAction,
|
|
113
|
+
formgongRoute,
|
|
114
|
+
initialFormgongState,
|
|
115
|
+
submitToFormgong
|
|
116
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { SubmitOptions } from '@formgong/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @formgong/next/server: forward forms to Formgong from Next.js Server Actions and Route Handlers.
|
|
5
|
+
*
|
|
6
|
+
* // app/contact/actions.ts
|
|
7
|
+
* "use server";
|
|
8
|
+
* import { submitToFormgong } from "@formgong/next/server";
|
|
9
|
+
* export async function contact(prev: unknown, formData: FormData) {
|
|
10
|
+
* return submitToFormgong(formData);
|
|
11
|
+
* }
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
type FormgongActionState = {
|
|
15
|
+
status: "idle";
|
|
16
|
+
message: "";
|
|
17
|
+
code?: undefined;
|
|
18
|
+
id?: undefined;
|
|
19
|
+
} | {
|
|
20
|
+
status: "success";
|
|
21
|
+
message: string;
|
|
22
|
+
id: string;
|
|
23
|
+
code?: undefined;
|
|
24
|
+
} | {
|
|
25
|
+
status: "error";
|
|
26
|
+
message: string;
|
|
27
|
+
code: string;
|
|
28
|
+
id?: undefined;
|
|
29
|
+
};
|
|
30
|
+
/** Initial state for `useActionState(action, initialFormgongState)`. */
|
|
31
|
+
declare const initialFormgongState: FormgongActionState;
|
|
32
|
+
type ServerSubmitOptions = Omit<SubmitOptions, "tracker"> & {
|
|
33
|
+
/** Defaults to process.env.FORMGONG_ACCESS_KEY, then NEXT_PUBLIC_FORMGONG_ACCESS_KEY. (`endpoint` defaults to FORMGONG_ENDPOINT, then https://formgong.com/submit.) */
|
|
34
|
+
accessKey?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Forward the visitor's Origin/Referer and Accept-Language from next/headers (default true).
|
|
37
|
+
* Needed when the form only allows certain domains.
|
|
38
|
+
*/
|
|
39
|
+
forwardHeaders?: boolean;
|
|
40
|
+
/** Return an error message to reject the submission before it is sent. */
|
|
41
|
+
validate?: (data: FormData) => string | null | undefined | Promise<string | null | undefined>;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Send FormData (or an object) to Formgong from the server. Never throws: returns a serialisable
|
|
45
|
+
* state for `useActionState`. Behaviour signals are not forwarded or fabricated (server posts are
|
|
46
|
+
* never penalised for lacking them). Rate limits apply to your server's IP: 20/min, form: 30/min.
|
|
47
|
+
*/
|
|
48
|
+
declare function submitToFormgong(data: FormData | Record<string, string>, options?: ServerSubmitOptions): Promise<FormgongActionState>;
|
|
49
|
+
/**
|
|
50
|
+
* Build a Server Action for `useActionState`. Wrap it in your own "use server" function:
|
|
51
|
+
*
|
|
52
|
+
* const send = createFormgongAction({ subject: "New lead" });
|
|
53
|
+
* export async function contact(prev: FormgongActionState, fd: FormData) { return send(prev, fd); }
|
|
54
|
+
*/
|
|
55
|
+
declare function createFormgongAction(options?: ServerSubmitOptions): (_prev: FormgongActionState | undefined, formData: FormData) => Promise<FormgongActionState>;
|
|
56
|
+
/** Route Handler helper: `export const POST = formgongRoute()` forwards any form POST to Formgong as JSON. */
|
|
57
|
+
declare function formgongRoute(options?: ServerSubmitOptions): (request: Request) => Promise<Response>;
|
|
58
|
+
|
|
59
|
+
export { type FormgongActionState, type ServerSubmitOptions, createFormgongAction, formgongRoute, initialFormgongState, submitToFormgong };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { SubmitOptions } from '@formgong/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @formgong/next/server: forward forms to Formgong from Next.js Server Actions and Route Handlers.
|
|
5
|
+
*
|
|
6
|
+
* // app/contact/actions.ts
|
|
7
|
+
* "use server";
|
|
8
|
+
* import { submitToFormgong } from "@formgong/next/server";
|
|
9
|
+
* export async function contact(prev: unknown, formData: FormData) {
|
|
10
|
+
* return submitToFormgong(formData);
|
|
11
|
+
* }
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
type FormgongActionState = {
|
|
15
|
+
status: "idle";
|
|
16
|
+
message: "";
|
|
17
|
+
code?: undefined;
|
|
18
|
+
id?: undefined;
|
|
19
|
+
} | {
|
|
20
|
+
status: "success";
|
|
21
|
+
message: string;
|
|
22
|
+
id: string;
|
|
23
|
+
code?: undefined;
|
|
24
|
+
} | {
|
|
25
|
+
status: "error";
|
|
26
|
+
message: string;
|
|
27
|
+
code: string;
|
|
28
|
+
id?: undefined;
|
|
29
|
+
};
|
|
30
|
+
/** Initial state for `useActionState(action, initialFormgongState)`. */
|
|
31
|
+
declare const initialFormgongState: FormgongActionState;
|
|
32
|
+
type ServerSubmitOptions = Omit<SubmitOptions, "tracker"> & {
|
|
33
|
+
/** Defaults to process.env.FORMGONG_ACCESS_KEY, then NEXT_PUBLIC_FORMGONG_ACCESS_KEY. (`endpoint` defaults to FORMGONG_ENDPOINT, then https://formgong.com/submit.) */
|
|
34
|
+
accessKey?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Forward the visitor's Origin/Referer and Accept-Language from next/headers (default true).
|
|
37
|
+
* Needed when the form only allows certain domains.
|
|
38
|
+
*/
|
|
39
|
+
forwardHeaders?: boolean;
|
|
40
|
+
/** Return an error message to reject the submission before it is sent. */
|
|
41
|
+
validate?: (data: FormData) => string | null | undefined | Promise<string | null | undefined>;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Send FormData (or an object) to Formgong from the server. Never throws: returns a serialisable
|
|
45
|
+
* state for `useActionState`. Behaviour signals are not forwarded or fabricated (server posts are
|
|
46
|
+
* never penalised for lacking them). Rate limits apply to your server's IP: 20/min, form: 30/min.
|
|
47
|
+
*/
|
|
48
|
+
declare function submitToFormgong(data: FormData | Record<string, string>, options?: ServerSubmitOptions): Promise<FormgongActionState>;
|
|
49
|
+
/**
|
|
50
|
+
* Build a Server Action for `useActionState`. Wrap it in your own "use server" function:
|
|
51
|
+
*
|
|
52
|
+
* const send = createFormgongAction({ subject: "New lead" });
|
|
53
|
+
* export async function contact(prev: FormgongActionState, fd: FormData) { return send(prev, fd); }
|
|
54
|
+
*/
|
|
55
|
+
declare function createFormgongAction(options?: ServerSubmitOptions): (_prev: FormgongActionState | undefined, formData: FormData) => Promise<FormgongActionState>;
|
|
56
|
+
/** Route Handler helper: `export const POST = formgongRoute()` forwards any form POST to Formgong as JSON. */
|
|
57
|
+
declare function formgongRoute(options?: ServerSubmitOptions): (request: Request) => Promise<Response>;
|
|
58
|
+
|
|
59
|
+
export { type FormgongActionState, type ServerSubmitOptions, createFormgongAction, formgongRoute, initialFormgongState, submitToFormgong };
|
package/dist/server.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,113 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formgong/next",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Next.js contact forms with Formgong: Server Action helper (useActionState), Route Handler helper and App Router client components. Submissions go to email, Telegram and webhooks. A Formspree / Web3Forms alternative.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"./server": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/server.d.ts",
|
|
23
|
+
"default": "./dist/server.js"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/server.d.cts",
|
|
27
|
+
"default": "./dist/server.cjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"./client": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/client.d.ts",
|
|
33
|
+
"default": "./dist/client.js"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/client.d.cts",
|
|
37
|
+
"default": "./dist/client.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"README.md",
|
|
45
|
+
"LICENSE"
|
|
46
|
+
],
|
|
47
|
+
"sideEffects": false,
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@formgong/core": "^0.1.1",
|
|
50
|
+
"@formgong/react": "^0.2.1"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"next": ">=14",
|
|
54
|
+
"react": ">=18",
|
|
55
|
+
"react-dom": ">=18"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@testing-library/react": "^16.1.0",
|
|
59
|
+
"@types/react": "^19.0.0",
|
|
60
|
+
"@types/react-dom": "^19.0.0",
|
|
61
|
+
"react": "^19.0.0",
|
|
62
|
+
"react-dom": "^19.0.0"
|
|
63
|
+
},
|
|
64
|
+
"keywords": [
|
|
65
|
+
"formgong",
|
|
66
|
+
"nextjs",
|
|
67
|
+
"next.js",
|
|
68
|
+
"next",
|
|
69
|
+
"server-actions",
|
|
70
|
+
"server action",
|
|
71
|
+
"useActionState",
|
|
72
|
+
"app-router",
|
|
73
|
+
"route-handler",
|
|
74
|
+
"contact-form",
|
|
75
|
+
"contact form",
|
|
76
|
+
"form-backend",
|
|
77
|
+
"form backend",
|
|
78
|
+
"forms",
|
|
79
|
+
"react",
|
|
80
|
+
"formspree-alternative",
|
|
81
|
+
"formspree alternative",
|
|
82
|
+
"web3forms-alternative",
|
|
83
|
+
"web3forms alternative",
|
|
84
|
+
"telegram",
|
|
85
|
+
"email",
|
|
86
|
+
"gdpr",
|
|
87
|
+
"vercel",
|
|
88
|
+
"lovable",
|
|
89
|
+
"bolt",
|
|
90
|
+
"v0",
|
|
91
|
+
"no-backend"
|
|
92
|
+
],
|
|
93
|
+
"author": "Formgong <support@formgong.com>",
|
|
94
|
+
"license": "MIT",
|
|
95
|
+
"homepage": "https://formgong.com/en/docs/",
|
|
96
|
+
"repository": {
|
|
97
|
+
"type": "git",
|
|
98
|
+
"url": "git+https://github.com/formgong/js.git",
|
|
99
|
+
"directory": "packages/next"
|
|
100
|
+
},
|
|
101
|
+
"bugs": {
|
|
102
|
+
"url": "https://github.com/formgong/js/issues",
|
|
103
|
+
"email": "support@formgong.com"
|
|
104
|
+
},
|
|
105
|
+
"publishConfig": {
|
|
106
|
+
"access": "public"
|
|
107
|
+
},
|
|
108
|
+
"scripts": {
|
|
109
|
+
"build": "tsup",
|
|
110
|
+
"typecheck": "tsc --noEmit",
|
|
111
|
+
"test": "vitest run"
|
|
112
|
+
}
|
|
6
113
|
}
|