ferst-core 0.9.8 → 0.10.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.
- package/bin/ferst-modules.mjs +2 -2
- package/components/recipes/Form.astro +7 -6
- package/content/recipe-questions.ts +78 -0
- package/lib/forms.ts +29 -4
- package/lib/git-base.mjs +15 -0
- package/package.json +1 -1
package/bin/ferst-modules.mjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// npx --no-install ferst-modules --json # the raw answer
|
|
8
8
|
//
|
|
9
9
|
// Forms and calendars live in the portal, never in the repo: place a form with a
|
|
10
|
-
// `{ "type": "form", "form": "<
|
|
10
|
+
// `{ "type": "form", "form": "<its name on this site>" }` block, and show the calendar by setting
|
|
11
11
|
// calendarSettings.embedUrl to the address printed here.
|
|
12
12
|
import { readFileSync, realpathSync } from 'node:fs';
|
|
13
13
|
import { resolve } from 'node:path';
|
|
@@ -46,7 +46,7 @@ export function formatModules(data) {
|
|
|
46
46
|
else if (data.forms.length === 0) out.push(' No forms yet. They are created in the portal (Forms).');
|
|
47
47
|
for (const f of data.forms) {
|
|
48
48
|
out.push(` ${f.name}`);
|
|
49
|
-
out.push(` place it with: { "type": "form", "form": "${f.id}" }`);
|
|
49
|
+
out.push(` place it with: { "type": "form", "form": "${f.slot ?? f.id}" }`);
|
|
50
50
|
out.push(` fields: ${f.fields.map((x) => `${x.label}${x.required ? ' *' : ''}`).join(', ') || '(none)'}`);
|
|
51
51
|
out.push(` button: ${f.submitLabel}`);
|
|
52
52
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
---
|
|
2
2
|
/**
|
|
3
|
-
* Form — one of the organisation's forms from the Ferst portal, placed by
|
|
4
|
-
* form's fields, button and submit address from the
|
|
5
|
-
*
|
|
6
|
-
* change made in the portal reaches the site on its next
|
|
3
|
+
* Form — one of the organisation's forms from the Ferst portal, placed by its name on this site
|
|
4
|
+
* (its slot; older pages give its id). Reads the form's fields, button and submit address from the
|
|
5
|
+
* platform at build time, through the site (lib/forms), and renders it with the ContactForm recipe,
|
|
6
|
+
* so it looks like every other form and a change made in the portal reaches the site on its next
|
|
7
|
+
* build.
|
|
7
8
|
*/
|
|
8
9
|
import ContactForm from './ContactForm.astro';
|
|
9
10
|
import { loadFormDefinition } from '../../lib/forms';
|
|
10
|
-
import { resolveGitBase } from '../../lib/git-base.mjs';
|
|
11
|
+
import { resolveGitBase, resolveSiteId } from '../../lib/git-base.mjs';
|
|
11
12
|
|
|
12
13
|
interface Props {
|
|
13
14
|
form: string;
|
|
@@ -16,7 +17,7 @@ interface Props {
|
|
|
16
17
|
note?: string;
|
|
17
18
|
}
|
|
18
19
|
const { form, title, intro, note } = Astro.props;
|
|
19
|
-
const def = await loadFormDefinition(form, { base: resolveGitBase() });
|
|
20
|
+
const def = await loadFormDefinition(form, { base: resolveGitBase(), siteId: resolveSiteId() });
|
|
20
21
|
---
|
|
21
22
|
|
|
22
23
|
<ContactForm
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Setup questions, kept with the recipes. When a client sets up a page from a design, each
|
|
3
|
+
* section of the design asks one free-flowing question; the answer is a paragraph in the client's
|
|
4
|
+
* own words, from which the section is filled. Deliberately not a field-by-field form: the
|
|
5
|
+
* recipe decides how the words are laid out, so the client only has to say what they mean.
|
|
6
|
+
*
|
|
7
|
+
* - `ask`: the question, a hint, and a plain name for the section.
|
|
8
|
+
* - `note`: the section fills itself (from the platform or the site), so nothing is asked.
|
|
9
|
+
* - `null`: spacing and layout pieces, which have no content of their own (containers are
|
|
10
|
+
* walked into by the caller).
|
|
11
|
+
*
|
|
12
|
+
* Generic wording only. How questions are ordered and combined into a request lives with the
|
|
13
|
+
* platform.
|
|
14
|
+
*/
|
|
15
|
+
export type RecipeQuestion =
|
|
16
|
+
| { kind: 'ask'; label: string; question: string; hint: string }
|
|
17
|
+
| { kind: 'note'; label: string; note: string };
|
|
18
|
+
|
|
19
|
+
const ask = (label: string, question: string, hint: string): RecipeQuestion => ({ kind: 'ask', label, question, hint });
|
|
20
|
+
const note = (label: string, text: string): RecipeQuestion => ({ kind: 'note', label, note: text });
|
|
21
|
+
|
|
22
|
+
const QUESTIONS: Record<string, RecipeQuestion | null> = {
|
|
23
|
+
// Recipes
|
|
24
|
+
hero: ask('Opening banner', 'What should the first thing visitors see say?', 'Your main message in your own words, a supporting line, and where the main button should take people.'),
|
|
25
|
+
featureCards: ask('Highlights', 'What are the main things you want to highlight here?', 'Describe each one in a sentence or two, and say where each should lead if people want more.'),
|
|
26
|
+
cta: ask('Call to action', 'What do you most want visitors to do next?', 'For example: book, call, visit, sign up. Say it the way you would to a customer.'),
|
|
27
|
+
faq: ask('Questions and answers', 'What do people often ask you, and how do you answer?', 'Write each question with your usual answer. As many as you like.'),
|
|
28
|
+
gallery: ask('Photo gallery', 'What should these photos show?', 'Describe the pictures you have or want: your work, your place, your people. You can upload them in the content editor.'),
|
|
29
|
+
testimonial: ask('What people say', 'What have customers said about you?', 'Paste a kind word or review, with the person’s name or initials if they’re happy to be named.'),
|
|
30
|
+
contactForm: ask('Contact form', 'What should people be able to ask you through this form?', 'Who answers, what you need to know from them, and how soon they can expect a reply.'),
|
|
31
|
+
form: note('Form', 'This shows one of your organisation’s forms. Choose or create it in the portal under Forms.'),
|
|
32
|
+
accordion: ask('Expandable sections', 'What information should people be able to open up and read here?', 'Give each topic a short heading and the detail that goes under it.'),
|
|
33
|
+
tiles: ask('Quick links', 'Where do visitors most often want to go from here?', 'List the places or tasks, a few words each, for example: opening times, book now, prices.'),
|
|
34
|
+
banner: ask('Banner', 'What message deserves a bold band across the page?', 'A short statement or invitation, and where it should lead if anywhere.'),
|
|
35
|
+
stats: ask('Key numbers', 'Which numbers say something good about you?', 'Years running, people helped, places served: the number and what it means.'),
|
|
36
|
+
steps: ask('How it works', 'What are the steps someone goes through with you?', 'From first contact to finished, in your own words, in order.'),
|
|
37
|
+
pricing: ask('Prices', 'What do you charge, and what does each option include?', 'List each option with its price and what someone gets. Say if prices start “from”.'),
|
|
38
|
+
logos: ask('Logos', 'Whose logos should appear here?', 'Partners, accreditations or clients you are proud to show. You can upload the images in the content editor.'),
|
|
39
|
+
tabs: ask('Tabbed information', 'What information would people like to switch between here?', 'Name each topic and what it should say.'),
|
|
40
|
+
announcement: ask('Announcement', 'Is there a short announcement visitors should see?', 'One or two sentences: news, a change to times, an offer. Leave it blank if not.'),
|
|
41
|
+
bento: ask('Feature panels', 'What would you show off in a set of feature panels?', 'Your strongest points, products or moments, a sentence or two each.'),
|
|
42
|
+
marquee: ask('Scrolling line', 'What short words or phrases should scroll across here?', 'Things you offer, places you serve, or values, a few words each.'),
|
|
43
|
+
latestPosts: note('Latest posts', 'This shows your newest posts automatically. Write posts in the content editor.'),
|
|
44
|
+
mapEmbed: ask('Map', 'Where should the map show?', 'The address or place name, and anything about finding you or parking.'),
|
|
45
|
+
table: ask('Table', 'What information belongs in a table here?', 'Describe the rows and columns, or paste the details as you have them.'),
|
|
46
|
+
notice: ask('Notice', 'What notice should stand out on this page?', 'An important point people must not miss, in a sentence or two.'),
|
|
47
|
+
mediaCards: ask('Picture cards', 'What should each picture card show and say?', 'For each one: what the picture is, a short title and a line of text.'),
|
|
48
|
+
contactCard: ask('Contact details', 'How would you like people to reach you?', 'Phone, email, address and opening hours, as you want them shown.'),
|
|
49
|
+
locations: ask('Locations', 'Which places should be listed?', 'Each place with its address and anything visitors should know.'),
|
|
50
|
+
video: ask('Video', 'Which video should play here, and what is it about?', 'A link to the video (for example YouTube) and a line about it.'),
|
|
51
|
+
documents: ask('Documents', 'Which documents should people be able to open or download?', 'List each with a short description. You can upload the files in the content editor.'),
|
|
52
|
+
linkRow: ask('Links', 'Which links should sit together here?', 'Each link’s words and where it goes.'),
|
|
53
|
+
document: ask('Document', 'Which document should be shown here?', 'What it is and who it is for. You can upload the file in the content editor.'),
|
|
54
|
+
timetable: ask('Timetable', 'What happens when?', 'The days and times, and what is on at each. Say if it changes through the year.'),
|
|
55
|
+
contacts: ask('People to contact', 'Who should people contact, and about what?', 'Each person or role, what they help with, and how to reach them.'),
|
|
56
|
+
mediaText: ask('Picture with text', 'What should this picture and its words tell people?', 'Describe the picture and write the words that go beside it.'),
|
|
57
|
+
// Leaves and primitives
|
|
58
|
+
heading: ask('Heading', 'What should this heading say?', 'A few words that introduce what comes next.'),
|
|
59
|
+
prose: ask('Text', 'What should this text say?', 'Write it as you would explain it to a visitor. The more you give, the better the result.'),
|
|
60
|
+
image: ask('Picture', 'What should this picture show?', 'Describe it, or say which of your photos to use. You can upload it in the content editor.'),
|
|
61
|
+
button: ask('Button', 'What should this button say, and where should it take people?', 'A few words for the button and the page or address it opens.'),
|
|
62
|
+
quote: ask('Quote', 'What quote belongs here, and who said it?', 'The words and the person, if they are happy to be named.'),
|
|
63
|
+
list: ask('List', 'What should this list contain?', 'One item per line, in the order you want them.'),
|
|
64
|
+
stat: ask('Number', 'Which number should stand out here, and what does it mean?', 'The figure and a few words about it.'),
|
|
65
|
+
badge: ask('Label', 'What short label belongs here?', 'One to three words, for example: New, Open now, Since 1998.'),
|
|
66
|
+
divider: null,
|
|
67
|
+
spacer: null,
|
|
68
|
+
section: null,
|
|
69
|
+
grid: null,
|
|
70
|
+
stack: null,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const GENERAL = ask('Section', 'What should this section say?', 'Describe what it is for and the words you want in it.');
|
|
74
|
+
|
|
75
|
+
/** The setup question for a block type; `null` for spacing and layout pieces. */
|
|
76
|
+
export function recipeQuestion(type: string): RecipeQuestion | null {
|
|
77
|
+
return type in QUESTIONS ? QUESTIONS[type] : GENERAL;
|
|
78
|
+
}
|
package/lib/forms.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A form placed
|
|
2
|
+
* A form placed on a page (the `form` block) is read from the Ferst platform at build time, so
|
|
3
3
|
* a change made to it in the portal reaches the site on its next build — no pasted copy of
|
|
4
4
|
* its fields to keep in step. A missing form or an unreachable platform fails the build:
|
|
5
5
|
* better the last good deploy stays live than a form that can't be sent goes out.
|
|
6
|
+
*
|
|
7
|
+
* The block names the form by its name on this site — its slot — and the site says who it is
|
|
8
|
+
* (`siteId` in ferst-site.json): the platform answers with whichever of the organisation's forms
|
|
9
|
+
* fills that slot, and the form posts to the site's own address for it. So the organisation can
|
|
10
|
+
* swap, copy or move forms without anyone editing the site. Pages from before slots name a form
|
|
11
|
+
* by its id, which still answers; a platform that doesn't know the site (a site id from another
|
|
12
|
+
* environment) falls back to the form's id.
|
|
6
13
|
*/
|
|
7
14
|
export interface FormField {
|
|
8
15
|
name: string;
|
|
@@ -24,17 +31,35 @@ type FetchFn = (input: string, init?: RequestInit) => Promise<Response>;
|
|
|
24
31
|
|
|
25
32
|
const cache = new Map<string, Promise<FormDefinition>>();
|
|
26
33
|
|
|
27
|
-
export function loadFormDefinition(
|
|
28
|
-
const key = `${opts.base}|${
|
|
34
|
+
export function loadFormDefinition(ref: string, opts: { base: string; siteId?: string | null; fetch?: FetchFn }): Promise<FormDefinition> {
|
|
35
|
+
const key = `${opts.base}|${opts.siteId ?? ''}|${ref}`;
|
|
29
36
|
let hit = cache.get(key);
|
|
30
37
|
if (!hit) {
|
|
31
|
-
hit = fetchDefinition(
|
|
38
|
+
hit = opts.siteId ? fetchThroughSite(ref, opts.siteId, opts.base, opts.fetch ?? fetch) : fetchDefinition(ref, opts.base, opts.fetch ?? fetch);
|
|
32
39
|
cache.set(key, hit);
|
|
33
40
|
hit.catch(() => cache.delete(key)); // a failure is retried by the next caller
|
|
34
41
|
}
|
|
35
42
|
return hit;
|
|
36
43
|
}
|
|
37
44
|
|
|
45
|
+
async function fetchThroughSite(slot: string, siteId: string, base: string, fetchFn: FetchFn): Promise<FormDefinition> {
|
|
46
|
+
const url = `${base}/sites/${encodeURIComponent(siteId)}/forms/${encodeURIComponent(slot)}`;
|
|
47
|
+
let res: Response;
|
|
48
|
+
try {
|
|
49
|
+
res = await fetchFn(url, { headers: { Accept: 'application/json' } });
|
|
50
|
+
} catch (e) {
|
|
51
|
+
throw new Error(`Form block: couldn’t reach the Ferst platform for form "${slot}" (${url}): ${e instanceof Error ? e.message : String(e)}`);
|
|
52
|
+
}
|
|
53
|
+
if (res.status === 404) {
|
|
54
|
+
const body = (await res.json().catch(() => ({}))) as { error?: string };
|
|
55
|
+
if (body.error === 'site_not_found') return fetchDefinition(slot, base, fetchFn);
|
|
56
|
+
throw new Error(`Form block: form "${slot}" isn’t on this site. Check its name on the site in the portal (Forms → Place on a site), or run \`npx ferst-modules\` to list this site's forms.`);
|
|
57
|
+
}
|
|
58
|
+
if (!res.ok) throw new Error(`Form block: the Ferst platform answered ${res.status} for form "${slot}" (${url}).`);
|
|
59
|
+
const def = (await res.json()) as { name: string; submit_label?: string; fields?: FormField[]; action: string };
|
|
60
|
+
return { name: def.name, submitLabel: def.submit_label || 'Send', fields: def.fields ?? [], action: def.action };
|
|
61
|
+
}
|
|
62
|
+
|
|
38
63
|
async function fetchDefinition(id: string, base: string, fetchFn: FetchFn): Promise<FormDefinition> {
|
|
39
64
|
const url = `${base}/forms/${encodeURIComponent(id)}`;
|
|
40
65
|
let res: Response;
|
package/lib/git-base.mjs
CHANGED
|
@@ -46,6 +46,21 @@ export function resolveGitBase(cwd = process.cwd(), branch = deploymentBranch())
|
|
|
46
46
|
return branch === prodBranch ? PROD_GIT_BASE : DEV_GIT_BASE;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Who this site is on the Ferst platform: `FERST_SITE_ID` env, else ferst-site.json's `siteId`
|
|
51
|
+
* (written when the site was set up), else null. The Form block asks for its forms through it.
|
|
52
|
+
*/
|
|
53
|
+
export function resolveSiteId(cwd = process.cwd()) {
|
|
54
|
+
const fromEnv = env('FERST_SITE_ID');
|
|
55
|
+
if (fromEnv) return fromEnv;
|
|
56
|
+
try {
|
|
57
|
+
const site = JSON.parse(readFileSync(resolve(cwd, 'ferst-site.json'), 'utf8'));
|
|
58
|
+
return site && typeof site.siteId === 'string' && site.siteId.trim() ? site.siteId.trim() : null;
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
49
64
|
/** The deployment branch: CF Pages env → current git branch → main. */
|
|
50
65
|
export function deploymentBranch() {
|
|
51
66
|
const cf = env('CF_PAGES_BRANCH');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ferst-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Ferst Core — the shared, brand-agnostic client-site system: Astro components, layouts, content schemas, the layered config resolver, and a neutral styling architecture that every client overrides.",
|
|
6
6
|
"license": "BUSL-1.1",
|