@domandigital/create-site 0.3.2 → 0.5.0

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.
@@ -0,0 +1,45 @@
1
+ // The visitor's cookie choice, shared by the banner and anything that sets a
2
+ // non-essential cookie. Written by @domandigital/create-site, from the
3
+ // dd-base banner (Doman-Digital/dd-library).
4
+ //
5
+ // Two states: strictly necessary only (the default, and what no answer
6
+ // means), or measurement as well. An analytics loader calls getConsent() and
7
+ // loads nothing unless measurement is true, then listens for CONSENT_EVENT so
8
+ // a choice made on this page takes effect without a reload. Refusing is as
9
+ // easy as accepting, and openCookieSettings() brings the banner back so a
10
+ // choice can be changed as easily as it was made (ICO guidance on PECR).
11
+
12
+ export const CONSENT_KEY = "dd-cookie-consent";
13
+ /** Fired on window with the new ConsentRecord as detail. */
14
+ export const CONSENT_EVENT = "dd-cookie-consent";
15
+ /** Fired on window to show the banner again. */
16
+ export const SETTINGS_EVENT = "dd-cookie-settings";
17
+
18
+ export type ConsentRecord = { measurement: boolean; decidedAt: string; version: "v1" };
19
+
20
+ export function getConsent(): ConsentRecord | null {
21
+ if (typeof window === "undefined") return null;
22
+ try {
23
+ const raw = window.localStorage.getItem(CONSENT_KEY);
24
+ if (!raw) return null;
25
+ const parsed = JSON.parse(raw) as ConsentRecord;
26
+ return parsed.version === "v1" && typeof parsed.measurement === "boolean" ? parsed : null;
27
+ } catch {
28
+ return null;
29
+ }
30
+ }
31
+
32
+ export function setConsent(measurement: boolean): ConsentRecord {
33
+ const record: ConsentRecord = { measurement, decidedAt: new Date().toISOString(), version: "v1" };
34
+ try {
35
+ window.localStorage.setItem(CONSENT_KEY, JSON.stringify(record));
36
+ } catch {
37
+ // Storage blocked: the choice holds for this page, and the banner asks again next visit.
38
+ }
39
+ window.dispatchEvent(new CustomEvent(CONSENT_EVENT, { detail: record }));
40
+ return record;
41
+ }
42
+
43
+ export function openCookieSettings(): void {
44
+ window.dispatchEvent(new Event(SETTINGS_EVENT));
45
+ }
@@ -43,6 +43,40 @@ export type Profile = {
43
43
  status: "to-claim" | "claimed" | "live";
44
44
  };
45
45
 
46
+ /** A company the site sends personal data to, as the privacy and cookie pages list it. */
47
+ export type Processor = {
48
+ name: string;
49
+ /** What it does for the site, e.g. "hosting", "enquiry email". */
50
+ purpose: string;
51
+ /** Sets cookies in the visitor's browser. Measurement ones load only after consent. */
52
+ setsCookies: boolean;
53
+ };
54
+
55
+ /**
56
+ * What the privacy, cookie, terms and accessibility pages need. Unknown is
57
+ * null; a page leaves out what is null rather than print a placeholder, and
58
+ * launch:check fails until the ones every site needs are filled in.
59
+ */
60
+ export type LegalFacts = {
61
+ /** Companies House number. null for a sole trader or a partnership. */
62
+ companyNumber: string | null;
63
+ /** Registered office on one line, as Companies House shows it. */
64
+ registeredOffice: string | null;
65
+ /** ICO data protection fee registration number. */
66
+ icoNumber: string | null;
67
+ /** Where privacy requests go, if not the main email. */
68
+ privacyEmail: string | null;
69
+ /** How long enquiries and customer records are kept, in a sentence. */
70
+ retention: string | null;
71
+ processors: Processor[];
72
+ /** The law the website terms are under, e.g. "England and Wales" or "Scotland". */
73
+ governingLaw: string | null;
74
+ /** Accessibility problems someone found and has not fixed yet. */
75
+ accessibilityIssues: string[];
76
+ /** ISO date someone last read the legal pages against what the site does. */
77
+ reviewedOn: string | null;
78
+ };
79
+
46
80
  export type SiteFacts = {
47
81
  /** The canonical origin, https, no trailing slash. */
48
82
  url: string;
@@ -68,6 +102,8 @@ export type SiteFacts = {
68
102
  profiles: Profile[];
69
103
  /** Hostnames of previous sites that now redirect here, for the redirect check. */
70
104
  previousHosts: string[];
105
+ /** Optional so a facts file written before the legal pages still compiles. */
106
+ legal?: LegalFacts;
71
107
  };
72
108
 
73
109
  const DAY_NAMES: Record<Day, string> = {
@@ -0,0 +1,33 @@
1
+ // The legal pages' facts, with every field present. A facts file written
2
+ // before the legal pages has no `legal` block; it reads as all unknown.
3
+ // Written by @domandigital/create-site.
4
+
5
+ import { facts } from "~site/facts";
6
+ import type { LegalFacts } from "~site/site-adapter";
7
+
8
+ export const legal: LegalFacts = {
9
+ companyNumber: null,
10
+ registeredOffice: null,
11
+ icoNumber: null,
12
+ privacyEmail: null,
13
+ retention: null,
14
+ processors: [],
15
+ governingLaw: null,
16
+ accessibilityIssues: [],
17
+ reviewedOn: null,
18
+ ...facts.legal,
19
+ };
20
+
21
+ /** Where a privacy request goes: the privacy address, else the main one. */
22
+ export const privacyEmail: string | null = legal.privacyEmail ?? facts.email;
23
+
24
+ /** "Acme Ltd, company number 01234567, registered office 1 High Street, Leeds" with what is known. */
25
+ export function identity(): string {
26
+ const parts = [facts.legalName];
27
+ if (legal.companyNumber) parts.push(`company number ${legal.companyNumber}`);
28
+ if (legal.registeredOffice) parts.push(`registered office ${legal.registeredOffice}`);
29
+ return parts.join(", ");
30
+ }
31
+
32
+ /** ICO's own page for complaints. A regulator's address, not the business's. */
33
+ export const ICO_COMPLAINTS = "https://ico.org.uk/make-a-complaint/";
@@ -8,6 +8,7 @@ import { readdirSync, readFileSync } from "node:fs";
8
8
  import { join, relative } from "node:path";
9
9
  import { describe, expect, test } from "vitest";
10
10
  import { facts } from "~site/facts";
11
+ import { legal } from "~site/legal";
11
12
 
12
13
  const ROOT = process.cwd();
13
14
  const SOURCE_DIRS = ["app", "src", "components", "lib", "content"];
@@ -53,6 +54,21 @@ describe.runIf(mode === "launch")("ready to launch", () => {
53
54
  expect(placed).toBe(true);
54
55
  });
55
56
 
57
+ test("the cookie banner is placed on the site", () => {
58
+ const placed = sourceFiles().some((file) => !/CookieBanner\.(tsx|astro)$/.test(file) && readFileSync(file, "utf8").includes("<CookieBanner"));
59
+ expect(placed).toBe(true);
60
+ });
61
+
62
+ test("the legal pages have what every site needs: retention, the processors, the governing law, a review date", () => {
63
+ const missing = [
64
+ legal.retention ? null : "legal.retention",
65
+ legal.processors.length > 0 ? null : "legal.processors (the host at least)",
66
+ legal.governingLaw ? null : "legal.governingLaw",
67
+ legal.reviewedOn ? null : "legal.reviewedOn",
68
+ ].filter(Boolean);
69
+ expect(missing).toEqual([]);
70
+ });
71
+
56
72
  test("phone and postcode are filled in", () => {
57
73
  expect(facts.phone).not.toBeNull();
58
74
  expect(facts.address?.postalCode ?? null).not.toBeNull();