ferst-core 0.17.0 → 0.17.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/lib/members.ts CHANGED
@@ -1,115 +1,134 @@
1
- /**
2
- * Members: people who sign in on an organisation's sites (the Ferst Members module). The site
3
- * reaches the platform's members service only through its own address, `/ferst-api/…` (a Pages
4
- * Function the site template carries), so the session is a first-party cookie and nothing here
5
- * needs CORS. Every call is same-origin, `credentials: 'same-origin'`, JSON.
6
- *
7
- * Nothing a member reads is ever in the build: a members' page is a placeholder until the visitor's
8
- * browser asks for it, and the platform decides, per request, whether they may read it.
9
- *
10
- * The browser scripts (MembersContent, the header's account link) are thin; the decisions are
11
- * here, pure, so they're unit-tested.
12
- */
13
-
14
- /** Where a site's members service is, on the site's own address. */
15
- export const MEMBERS_API = '/ferst-api/members';
16
- /** The signed-in person, or `401 signed_out`. */
17
- export const MEMBERS_ME = `${MEMBERS_API}/me`;
18
- /** The members' account page: signing in, and the member's own details. */
19
- export const ACCOUNT_PATH = '/account';
20
-
21
- /** A members' page's id, as the portal makes them. */
22
- export const MEMBERS_PAGE_ID = /^mpg_[0-9a-f]{24}$/;
23
-
24
- /** Where the browser reads a members' page. */
25
- export function membersPageUrl(page: string): string {
26
- return `${MEMBERS_API}/pages/${encodeURIComponent(page)}`;
27
- }
28
-
29
- /** The account page, coming back here once signed in. */
30
- export function signInHref(here: string): string {
31
- return `${ACCOUNT_PATH}?next=${encodeURIComponent(here)}`;
32
- }
33
-
34
- /** An answer from the members service: its status, and its JSON body (null when it isn't JSON). */
35
- export interface MembersAnswer {
36
- status: number;
37
- body: Record<string, unknown> | null;
38
- }
39
-
40
- /** Read a response as a MembersAnswer. Anything but JSON (a "page not found" page) has no body. */
41
- export async function readMembersAnswer(res: Response): Promise<MembersAnswer> {
42
- const json = /application\/json/i.test(res.headers.get('Content-Type') ?? '');
43
- let body: Record<string, unknown> | null = null;
44
- if (json) {
45
- try {
46
- const parsed: unknown = await res.json();
47
- body = parsed && typeof parsed === 'object' ? (parsed as Record<string, unknown>) : null;
48
- } catch {
49
- body = null;
50
- }
51
- }
52
- return { status: res.status, body };
53
- }
54
-
55
- /** What a membersContent block shows. */
56
- export type MembersPageView =
57
- | { kind: 'page'; title: string; html: string }
58
- /** 401: "This is for members. Sign in to read it". */
59
- | { kind: 'sign-in' }
60
- /** 403 not_for_you: a guest, someone waiting for approval, or a page for other members. */
61
- | { kind: 'not-included' }
62
- /** Anything else: the page is gone, Members is off for the site, the service is down. */
63
- | { kind: 'unavailable' }
64
- /** Running on a computer (`astro dev` / `astro preview`), where the site has no /ferst-api. */
65
- | { kind: 'local' };
66
-
67
- /**
68
- * What to show for the answer to `GET /ferst-api/members/pages/:id`. `local` is true when the site
69
- * runs on the author's own computer: there the answer is the dev server's "page not found", not the
70
- * service's, so the block says sign-in works on the published site.
71
- */
72
- export function membersPageView(answer: MembersAnswer | null, local = false): MembersPageView {
73
- if (!answer || !answer.body) return local ? { kind: 'local' } : { kind: 'unavailable' };
74
- const { status, body } = answer;
75
- if (status === 200 && typeof body.html === 'string') {
76
- return { kind: 'page', title: typeof body.title === 'string' ? body.title : '', html: body.html };
77
- }
78
- if (status === 401) return { kind: 'sign-in' };
79
- // 403 is also the answer when Members isn't on for the site (`module_off`): not about them.
80
- if (status === 403 && body.error === 'not_for_you') return { kind: 'not-included' };
81
- return { kind: 'unavailable' };
82
- }
83
-
84
- /** The words a members' page shows when it can't show the page itself. */
85
- export const MEMBERS_PAGE_TEXT = {
86
- signIn: { before: 'This is for members.', link: 'Sign in', after: 'to read it.' },
87
- notIncluded: 'This isn’t included in your membership.',
88
- unavailable: 'This can’t be shown right now.',
89
- local: 'Sign-in works on the published site.',
90
- } as const;
91
-
92
- /** The first word of a person's name, for the header ("Sam" for "Sam Hill"). */
93
- export function firstName(displayName: unknown): string {
94
- if (typeof displayName !== 'string') return '';
95
- return displayName.trim().split(/\s+/)[0] ?? '';
96
- }
97
-
98
- /**
99
- * The header's account link, from the answer to `GET /ferst-api/members/me`: the member's first
100
- * name (or "Your account" when they have none yet), "Sign in" when signed out, and null (leave the
101
- * build's "Account") when the service can't say.
102
- */
103
- export function accountLinkText(answer: MembersAnswer | null): string | null {
104
- if (!answer || !answer.body) return null;
105
- if (answer.status === 401) return 'Sign in';
106
- if (answer.status !== 200) return null;
107
- const member = answer.body.member as { display_name?: unknown } | undefined;
108
- if (!member || typeof member !== 'object') return null;
109
- return firstName(member.display_name) || 'Your account';
110
- }
111
-
112
- /** True on the author's own computer, where `/ferst-api` isn't there (no Pages Functions). */
113
- export function isLocalHost(hostname: string): boolean {
114
- return /^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(hostname) || hostname.endsWith('.localhost');
115
- }
1
+ /**
2
+ * Members: people who sign in on an organisation's sites (the Ferst Members module). The site
3
+ * reaches the platform's members service only through its own address, `/ferst-api/…` (a Pages
4
+ * Function the site template carries), so the session is a first-party cookie and nothing here
5
+ * needs CORS. Every call is same-origin, `credentials: 'same-origin'`, JSON.
6
+ *
7
+ * Nothing a member reads is ever in the build: a members' page is a placeholder until the visitor's
8
+ * browser asks for it, and the platform decides, per request, whether they may read it.
9
+ *
10
+ * The browser scripts (MembersContent, the header's account link) are thin; the decisions are
11
+ * here, pure, so they're unit-tested.
12
+ */
13
+
14
+ /** Where a site's members service is, on the site's own address. */
15
+ export const MEMBERS_API = '/ferst-api/members';
16
+ /** The signed-in person, or `401 signed_out`. */
17
+ export const MEMBERS_ME = `${MEMBERS_API}/me`;
18
+ /** The members' account page: signing in, and the member's own details. */
19
+ export const ACCOUNT_PATH = '/account';
20
+
21
+ /** A members' page's id, as the portal makes them. */
22
+ export const MEMBERS_PAGE_ID = /^mpg_[0-9a-f]{24}$/;
23
+
24
+ /** Where the browser reads a members' page. */
25
+ export function membersPageUrl(page: string): string {
26
+ return `${MEMBERS_API}/pages/${encodeURIComponent(page)}`;
27
+ }
28
+
29
+ /** The account page, coming back here once signed in. */
30
+ export function signInHref(here: string): string {
31
+ return `${ACCOUNT_PATH}?next=${encodeURIComponent(here)}`;
32
+ }
33
+
34
+ /** An answer from the members service: its status, and its JSON body (null when it isn't JSON). */
35
+ export interface MembersAnswer {
36
+ status: number;
37
+ body: Record<string, unknown> | null;
38
+ }
39
+
40
+ /** Read a response as a MembersAnswer. Anything but JSON (a "page not found" page) has no body. */
41
+ export async function readMembersAnswer(res: Response): Promise<MembersAnswer> {
42
+ const json = /application\/json/i.test(res.headers.get('Content-Type') ?? '');
43
+ let body: Record<string, unknown> | null = null;
44
+ if (json) {
45
+ try {
46
+ const parsed: unknown = await res.json();
47
+ body = parsed && typeof parsed === 'object' ? (parsed as Record<string, unknown>) : null;
48
+ } catch {
49
+ body = null;
50
+ }
51
+ }
52
+ return { status: res.status, body };
53
+ }
54
+
55
+ /** What a membersContent block shows. */
56
+ export type MembersPageView =
57
+ | { kind: 'page'; title: string; html: string }
58
+ /** 401: "This is for members. Sign in to read it". */
59
+ | { kind: 'sign-in' }
60
+ /** 403 not_for_you: a guest, someone waiting for approval, or a page for other members. */
61
+ | { kind: 'not-included' }
62
+ /** Anything else: the page is gone, Members is off for the site, the service is down. */
63
+ | { kind: 'unavailable' }
64
+ /** Running on a computer (`astro dev` / `astro preview`), where the site has no /ferst-api. */
65
+ | { kind: 'local' };
66
+
67
+ /**
68
+ * What to show for the answer to `GET /ferst-api/members/pages/:id`. `local` is true when the site
69
+ * runs on the author's own computer: there the answer is the dev server's "page not found", not the
70
+ * service's, so the block says sign-in works on the published site.
71
+ */
72
+ export function membersPageView(answer: MembersAnswer | null, local = false): MembersPageView {
73
+ if (!answer || !answer.body) return local ? { kind: 'local' } : { kind: 'unavailable' };
74
+ const { status, body } = answer;
75
+ if (status === 200 && typeof body.html === 'string') {
76
+ return { kind: 'page', title: typeof body.title === 'string' ? body.title : '', html: body.html };
77
+ }
78
+ if (status === 401) return { kind: 'sign-in' };
79
+ // 403 is also the answer when Members isn't on for the site (`module_off`): not about them.
80
+ if (status === 403 && body.error === 'not_for_you') return { kind: 'not-included' };
81
+ return { kind: 'unavailable' };
82
+ }
83
+
84
+ /** The words a members' page shows when it can't show the page itself. */
85
+ export const MEMBERS_PAGE_TEXT = {
86
+ signIn: { before: 'This is for members.', link: 'Sign in', after: 'to read it.' },
87
+ notIncluded: 'This isn’t included in your membership.',
88
+ unavailable: 'This can’t be shown right now.',
89
+ local: 'Sign-in works on the published site.',
90
+ } as const;
91
+
92
+ /** The first word of a person's name, for the header ("Sam" for "Sam Hill"). */
93
+ export function firstName(displayName: unknown): string {
94
+ if (typeof displayName !== 'string') return '';
95
+ return displayName.trim().split(/\s+/)[0] ?? '';
96
+ }
97
+
98
+ /**
99
+ * The header's account link, from the answer to `GET /ferst-api/members/me`: the member's first
100
+ * name (or "Your account" when they have none yet), "Sign in" when signed out, and null (leave the
101
+ * build's "Account") when the service can't say.
102
+ */
103
+ export function accountLinkText(answer: MembersAnswer | null): string | null {
104
+ if (!answer || !answer.body) return null;
105
+ if (answer.status === 401) return 'Sign in';
106
+ if (answer.status !== 200) return null;
107
+ const member = answer.body.member as { display_name?: unknown } | undefined;
108
+ if (!member || typeof member !== 'object') return null;
109
+ return firstName(member.display_name) || 'Your account';
110
+ }
111
+
112
+ /**
113
+ * The header's account link without a flicker (0.17.1): the build says "Sign in" (most visitors
114
+ * aren't signed in), and a tiny inline script puts back the label this browser last saw before the
115
+ * page paints, under this key. The answer from `/me` still has the last word, and is remembered.
116
+ */
117
+ export const ACCOUNT_LABEL_KEY = 'ferst:account-label';
118
+
119
+ /** A remembered label worth showing: one short line of plain text, as accountLinkText makes them. */
120
+ export function rememberedLabel(v: unknown): string | null {
121
+ if (typeof v !== 'string') return null;
122
+ const t = v.trim();
123
+ return t && t.length <= 40 && !/[\n\r<>]/.test(t) ? t : null;
124
+ }
125
+
126
+ /** Remember the link's label for the next page (nothing, where the browser keeps no storage). */
127
+ export function rememberAccountLabel(text: string): void {
128
+ try { localStorage.setItem(ACCOUNT_LABEL_KEY, text); } catch { /* private mode, storage off */ }
129
+ }
130
+
131
+ /** True on the author's own computer, where `/ferst-api` isn't there (no Pages Functions). */
132
+ export function isLocalHost(hostname: string): boolean {
133
+ return /^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(hostname) || hostname.endsWith('.localhost');
134
+ }
package/package.json CHANGED
@@ -1,80 +1,80 @@
1
- {
2
- "name": "ferst-core",
3
- "version": "0.17.0",
4
- "type": "module",
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
- "license": "BUSL-1.1",
7
- "author": "MyAI4 Ltd (https://ferst.co.uk)",
8
- "homepage": "https://ferst.co.uk",
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/MyAI4WebDev/myai4-ferst-core.git",
12
- "directory": "packages/core"
13
- },
14
- "bugs": {
15
- "url": "https://github.com/MyAI4WebDev/myai4-ferst-core/issues"
16
- },
17
- "keywords": [
18
- "astro",
19
- "astro-component",
20
- "cms",
21
- "sveltia",
22
- "website",
23
- "design-tokens",
24
- "ferst"
25
- ],
26
- "scripts": {
27
- "test": "vitest run",
28
- "test:watch": "vitest"
29
- },
30
- "sideEffects": [
31
- "**/*.css"
32
- ],
33
- "files": [
34
- "bin",
35
- "cms",
36
- "components",
37
- "integration.mjs",
38
- "layouts",
39
- "content",
40
- "examples",
41
- "lib",
42
- "routes",
43
- "utils",
44
- "styles",
45
- "fonts",
46
- "README.md",
47
- "NOTICE",
48
- "LICENSE",
49
- "LICENSE-Apache-2.0"
50
- ],
51
- "bin": {
52
- "ferst-check-thin": "bin/check-thin.mjs",
53
- "ferst-check-pages": "bin/check-pages.mjs",
54
- "ferst-set-cms-branch": "bin/set-cms-branch.mjs",
55
- "ferst-cms-config": "bin/cms-config.mjs",
56
- "ferst-update": "bin/ferst-update.mjs",
57
- "ferst-modules": "bin/ferst-modules.mjs",
58
- "ferst-media": "bin/ferst-media.mjs"
59
- },
60
- "dependencies": {
61
- "@astrojs/react": "^4.4.2",
62
- "@internationalized/date": "^3.12.4",
63
- "react": "^19.3.1",
64
- "react-aria": "^3.53.1",
65
- "react-aria-components": "^1.22.1",
66
- "react-dom": "^19.3.1",
67
- "zod": "^3.25.0"
68
- },
69
- "devDependencies": {
70
- "@types/react": "^19.3.3",
71
- "@types/react-dom": "^19.3.0",
72
- "vitest": "^3.0.0"
73
- },
74
- "peerDependencies": {
75
- "astro": "^5.0.0"
76
- },
77
- "publishConfig": {
78
- "access": "public"
79
- }
80
- }
1
+ {
2
+ "name": "ferst-core",
3
+ "version": "0.17.1",
4
+ "type": "module",
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
+ "license": "BUSL-1.1",
7
+ "author": "MyAI4 Ltd (https://ferst.co.uk)",
8
+ "homepage": "https://ferst.co.uk",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/MyAI4WebDev/myai4-ferst-core.git",
12
+ "directory": "packages/core"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/MyAI4WebDev/myai4-ferst-core/issues"
16
+ },
17
+ "keywords": [
18
+ "astro",
19
+ "astro-component",
20
+ "cms",
21
+ "sveltia",
22
+ "website",
23
+ "design-tokens",
24
+ "ferst"
25
+ ],
26
+ "scripts": {
27
+ "test": "vitest run",
28
+ "test:watch": "vitest"
29
+ },
30
+ "sideEffects": [
31
+ "**/*.css"
32
+ ],
33
+ "files": [
34
+ "bin",
35
+ "cms",
36
+ "components",
37
+ "integration.mjs",
38
+ "layouts",
39
+ "content",
40
+ "examples",
41
+ "lib",
42
+ "routes",
43
+ "utils",
44
+ "styles",
45
+ "fonts",
46
+ "README.md",
47
+ "NOTICE",
48
+ "LICENSE",
49
+ "LICENSE-Apache-2.0"
50
+ ],
51
+ "bin": {
52
+ "ferst-check-thin": "bin/check-thin.mjs",
53
+ "ferst-check-pages": "bin/check-pages.mjs",
54
+ "ferst-set-cms-branch": "bin/set-cms-branch.mjs",
55
+ "ferst-cms-config": "bin/cms-config.mjs",
56
+ "ferst-update": "bin/ferst-update.mjs",
57
+ "ferst-modules": "bin/ferst-modules.mjs",
58
+ "ferst-media": "bin/ferst-media.mjs"
59
+ },
60
+ "dependencies": {
61
+ "@astrojs/react": "^4.4.2",
62
+ "@internationalized/date": "^3.12.4",
63
+ "react": "^19.3.1",
64
+ "react-aria": "^3.53.1",
65
+ "react-aria-components": "^1.22.1",
66
+ "react-dom": "^19.3.1",
67
+ "zod": "^3.25.0"
68
+ },
69
+ "devDependencies": {
70
+ "@types/react": "^19.3.3",
71
+ "@types/react-dom": "^19.3.0",
72
+ "vitest": "^3.0.0"
73
+ },
74
+ "peerDependencies": {
75
+ "astro": "^5.0.0"
76
+ },
77
+ "publishConfig": {
78
+ "access": "public"
79
+ }
80
+ }