@sharadox/form-web-library 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -56,6 +56,7 @@ The email server comes from `EMAIL_SERVER_*` and `EMAIL_SENDER_ADDRESS`.
56
56
  | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
57
57
  | `…/contact-action` | `createContactAction(options)` → `(form, ip) => Promise<ContactState>`; `ENGLISH_LABELS`; `ContactState`, `ContactActionOptions` and `ContactLabels` in `…/contact-action/type` |
58
58
  | `…/parse-contact` | `parseContact(form, { choices?, choiceField?, reach?, requireMessage?, fields?, now? })` → `bot`, `invalid` or `valid` |
59
+ | `…/contact-check` | `isEmail(text)`, `isPhone(text)`: the rules the action uses, safe in the browser for checks before sending |
59
60
  | `…/bot-check` | `isLikelyBot(form, { honeypotField?, startedAtField?, minimumFillTime?, now? })` |
60
61
  | `…/rate-limit` | `createRateLimiter({ window, maximum })` → `(key, now?) => boolean` (kept in memory, per server instance) |
61
62
  | `…/field` | `readField(form, name, maximumLength)` |
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Whether text looks like an email address. Safe in the browser, so forms can check before sending what the server
3
+ * checks again.
4
+ *
5
+ * @example
6
+ * isEmail("ana@example.com"); // true
7
+ */
8
+ export declare function isEmail(text: string): boolean;
9
+ /**
10
+ * Whether text looks like a phone number: digits with spaces, brackets, dots or dashes, an optional leading +, and at
11
+ * least seven digits. Safe in the browser.
12
+ *
13
+ * @example
14
+ * isPhone("+351 912 345 678"); // true
15
+ */
16
+ export declare function isPhone(text: string): boolean;
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-check/index.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE7C;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE7C"}
@@ -0,0 +1,24 @@
1
+ const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
2
+ /** Digits with the usual phone punctuation. */
3
+ const PHONE = /^\+?[\d\s().-]+$/;
4
+ /**
5
+ * Whether text looks like an email address. Safe in the browser, so forms can check before sending what the server
6
+ * checks again.
7
+ *
8
+ * @example
9
+ * isEmail("ana@example.com"); // true
10
+ */
11
+ export function isEmail(text) {
12
+ return EMAIL.test(text);
13
+ }
14
+ /**
15
+ * Whether text looks like a phone number: digits with spaces, brackets, dots or dashes, an optional leading +, and at
16
+ * least seven digits. Safe in the browser.
17
+ *
18
+ * @example
19
+ * isPhone("+351 912 345 678"); // true
20
+ */
21
+ export function isPhone(text) {
22
+ return PHONE.test(text) && text.replace(/\D/g, "").length >= 7;
23
+ }
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-check/index.ts"],"names":[],"mappings":"AAAA,MAAM,KAAK,GAAG,+BAA+B,CAAC;AAC9C,+CAA+C;AAC/C,MAAM,KAAK,GAAG,kBAAkB,CAAC;AAEjC;;;;;;GAMG;AACH,MAAM,UAAU,OAAO,CAAC,IAAY;IAClC,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,OAAO,CAAC,IAAY;IAClC,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC;AACjE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/parse-contact/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,+CAA+C,CAAC;AAOxG;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,QAAQ,EACd,EACE,OAAY,EACZ,WAAoB,EACpB,KAAe,EACf,cAAqB,EACrB,MAAW,EACX,GAAgB,GACjB,GAAE,mBAAwB,GAC1B,aAAa,CAmBf"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/parse-contact/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,+CAA+C,CAAC;AAExG;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,QAAQ,EACd,EACE,OAAY,EACZ,WAAoB,EACpB,KAAe,EACf,cAAqB,EACrB,MAAW,EACX,GAAgB,GACjB,GAAE,mBAAwB,GAC1B,aAAa,CAmBf"}
@@ -1,9 +1,6 @@
1
1
  import { isLikelyBot } from "@sharadox/form-web-library/bot-check";
2
+ import { isEmail, isPhone } from "@sharadox/form-web-library/contact-check";
2
3
  import { readField } from "@sharadox/form-web-library/field";
3
- const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
4
- /** Digits with the usual phone punctuation, and at least seven digits. */
5
- const PHONE = /^\+?[\d\s().-]+$/;
6
- const isPhone = (phone) => PHONE.test(phone) && phone.replace(/\D/g, "").length >= 7;
7
4
  /**
8
5
  * Reads and validates a contact form (`name`, `email`, `phone`, `company`, `message`, `locale`, checkbox choices and
9
6
  * any extra `fields`), and flags likely bots (honeypot filled or submitted too fast). Fields are trimmed and capped in
@@ -27,7 +24,7 @@ export function parseContact(form, { choices = [], choiceField = "need", reach =
27
24
  choices: [...new Set(form.getAll(choiceField).map(String))].filter((choice) => choices.includes(choice)),
28
25
  fields: Object.fromEntries(fields.map((field) => [field, readField(form, field, 200)])),
29
26
  };
30
- const emailValid = EMAIL.test(value.email);
27
+ const emailValid = isEmail(value.email);
31
28
  const reachable = reach === "email"
32
29
  ? emailValid
33
30
  : (value.email || value.phone) !== "" && (!value.email || emailValid) && (!value.phone || isPhone(value.phone));
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/parse-contact/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sCAAsC,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAG7D,MAAM,KAAK,GAAG,+BAA+B,CAAC;AAC9C,0EAA0E;AAC1E,MAAM,KAAK,GAAG,kBAAkB,CAAC;AACjC,MAAM,OAAO,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC;AAE7F;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAC1B,IAAc,EACd,EACE,OAAO,GAAG,EAAE,EACZ,WAAW,GAAG,MAAM,EACpB,KAAK,GAAG,OAAO,EACf,cAAc,GAAG,IAAI,EACrB,MAAM,GAAG,EAAE,EACX,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,MACO,EAAE;IAE3B,IAAI,WAAW,CAAC,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IACvD,MAAM,KAAK,GAAG;QACZ,IAAI,EAAE,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC;QAClC,KAAK,EAAE,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC;QACpC,KAAK,EAAE,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC;QACnC,OAAO,EAAE,SAAS,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC;QACxC,OAAO,EAAE,SAAS,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC;QACzC,MAAM,EAAE,SAAS,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C,CAAC,CAAC;KACxF,CAAC;IACF,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACxC,MAAM,SAAS,GACb,KAAK,KAAK,OAAO;QACf,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;IACpH,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,cAAc,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;IAChG,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC"}
package/llms.txt CHANGED
@@ -5,5 +5,5 @@
5
5
  - /contact-action: createContactAction({ to, senderName, senderAddress?, site, subject(message), choices?: { key: label }, reach? = 'email' | 'email-or-phone', requireMessage? = true, fields?: { name: label }, labels?: Partial<ContactLabels> (English default, ENGLISH_LABELS), rateLimit? = 5/hour, readConfiguration?, createSender?, reportError? }) → (form, ip) => { status: idle|sent|invalid|rate-limited|failed }. Reply-to = visitor when they gave an email; bots get fake 'sent'.
6
6
  - Form fields: name, email, phone (valid: + digits spaces ().- with ≥7 digits), company, message, extra fields, locale, started-at, website (honeypot), need (checkboxes).
7
7
  - /parse-contact: parseContact(form, { choices, choiceField='need', reach, requireMessage, fields, now }) → value { name, email, phone, company, message, locale, choices, fields }
8
- - /bot-check isLikelyBot; /rate-limit createRateLimiter({ window, maximum }); /field readField(form, name, max)
8
+ - /contact-check isEmail(text), isPhone(text) (browser-safe, same rules as the server); /bot-check isLikelyBot; /rate-limit createRateLimiter({ window, maximum }); /field readField(form, name, max)
9
9
  - Env: EMAIL_SERVER_HOST/PORT/USERNAME/PASSWORD, EMAIL_SENDER_ADDRESS. License: Sharadox Open Source License 0.1.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/form-web-library",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Forms on the server: field reading, honeypot and fill-time bot checks, rate limiting and a contact form action that emails messages with the visitor as reply-to.",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENCA.md",