@blogmcp/sdk 0.8.1 → 0.9.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.
Files changed (89) hide show
  1. package/dist/community/channels.d.ts +12 -0
  2. package/dist/community/channels.js +7 -0
  3. package/dist/community/client.d.ts +211 -0
  4. package/dist/community/client.js +188 -0
  5. package/dist/community/index.d.ts +34 -0
  6. package/dist/community/index.js +93 -0
  7. package/dist/community/links.d.ts +73 -0
  8. package/dist/community/links.js +85 -0
  9. package/dist/community/messages.cs.d.ts +331 -0
  10. package/dist/community/messages.cs.js +342 -0
  11. package/dist/community/messages.d.ts +40 -0
  12. package/dist/community/messages.en.d.ts +315 -0
  13. package/dist/community/messages.en.js +313 -0
  14. package/dist/community/messages.js +66 -0
  15. package/dist/community/outcome.d.ts +96 -0
  16. package/dist/community/outcome.js +211 -0
  17. package/dist/community/poll.d.ts +35 -0
  18. package/dist/community/poll.js +70 -0
  19. package/dist/community/routes.d.ts +110 -0
  20. package/dist/community/routes.js +66 -0
  21. package/dist/community/server/bff.d.ts +123 -0
  22. package/dist/community/server/bff.js +214 -0
  23. package/dist/community/server/env.d.ts +20 -0
  24. package/dist/community/server/env.js +16 -0
  25. package/dist/community/server/host-routes.d.ts +15 -0
  26. package/dist/community/server/host-routes.js +35 -0
  27. package/dist/community/server/http.d.ts +64 -0
  28. package/dist/community/server/http.js +90 -0
  29. package/dist/community/server/index.d.ts +45 -0
  30. package/dist/community/server/index.js +19 -0
  31. package/dist/community/server/route-decision.d.ts +70 -0
  32. package/dist/community/server/route-decision.js +89 -0
  33. package/dist/community/server/unknown-path.d.ts +21 -0
  34. package/dist/community/server/unknown-path.js +11 -0
  35. package/dist/community/shapes.d.ts +472 -0
  36. package/dist/community/shapes.js +63 -0
  37. package/dist/community/store.d.ts +183 -0
  38. package/dist/community/store.js +423 -0
  39. package/dist/community/transport.d.ts +42 -0
  40. package/dist/community/transport.js +59 -0
  41. package/dist/core/client.d.ts +17 -0
  42. package/dist/core/client.js +58 -55
  43. package/dist/core/comments-legacy.d.ts +19 -0
  44. package/dist/core/comments-legacy.js +18 -0
  45. package/dist/core/index.d.ts +3 -0
  46. package/dist/core/index.js +107 -101
  47. package/dist/core/queries.d.ts +46 -0
  48. package/dist/core/queries.js +128 -84
  49. package/dist/core/seo.js +40 -39
  50. package/dist/core/translations/cs.json.js +6 -6
  51. package/dist/core/translations/en.json.js +1 -1
  52. package/dist/core/types.d.ts +23 -1
  53. package/dist/core/view-model.d.ts +7 -0
  54. package/dist/core/view-model.js +29 -25
  55. package/dist/packages/tripmap-core/dist/generated/maplibre-worker.bundle.js +1 -1
  56. package/dist/react/KbMdxContent.js +4 -3
  57. package/dist/vue/components/KbMdxContent.vue2.js +3 -2
  58. package/dist/vue/composables/useKbComments.d.ts +8 -0
  59. package/package.json +13 -4
  60. package/src/community/channels.ts +12 -0
  61. package/src/community/client.ts +437 -0
  62. package/src/community/index.ts +192 -0
  63. package/src/community/links.ts +174 -0
  64. package/src/community/messages.cs.ts +371 -0
  65. package/src/community/messages.en.ts +341 -0
  66. package/src/community/messages.ts +112 -0
  67. package/src/community/outcome.ts +317 -0
  68. package/src/community/poll.ts +154 -0
  69. package/src/community/routes.ts +139 -0
  70. package/src/community/server/bff.ts +530 -0
  71. package/src/community/server/env.ts +42 -0
  72. package/src/community/server/host-routes.ts +48 -0
  73. package/src/community/server/http.ts +175 -0
  74. package/src/community/server/index.ts +55 -0
  75. package/src/community/server/route-decision.ts +226 -0
  76. package/src/community/server/unknown-path.ts +32 -0
  77. package/src/community/shapes.ts +541 -0
  78. package/src/community/store.ts +786 -0
  79. package/src/community/transport.ts +111 -0
  80. package/src/core/client.ts +24 -1
  81. package/src/core/comments-legacy.ts +39 -0
  82. package/src/core/index.ts +8 -0
  83. package/src/core/queries.ts +93 -0
  84. package/src/core/seo.ts +5 -1
  85. package/src/core/translations/cs.json +3 -1
  86. package/src/core/translations/en.json +3 -1
  87. package/src/core/types.ts +19 -1
  88. package/src/core/view-model.ts +12 -1
  89. package/src/vue/composables/useKbComments.ts +8 -0
@@ -0,0 +1,192 @@
1
+ /**
2
+ * `@blogmcp/sdk/community` — the community's browser client (docs/internal/m1-contracts.md §10.2; community.md
3
+ * §3.7, §7.7): the public discussion of an article, the member's „Moje komunita", the DSA notice form — through
4
+ * the host's relative base, never to kb-api directly.
5
+ *
6
+ * createCommunityClient one method per operation, every one a POST through the host, none ever throws;
7
+ * createDiscussionStore the immutable two-level thread, optimistic writes, open enums skipped;
8
+ * pollDiscussionHead the polling signal while the discussion is visible (no realtime in M1);
9
+ * communityMessage every text, cs and en, under the tone test;
10
+ * links statements, the panel, the notifier's receipt in the fragment.
11
+ *
12
+ * Browser-safe: nothing here takes a key, a session, a cookie or an IP, and nothing imports `node:*`. This entry
13
+ * never reaches the server entries (`@blogmcp/sdk/community/server`, `@blogmcp/sdk/consultations/server`), the
14
+ * adapter client or the delegated-identity minter — test/community/entry-boundary.spec.ts walks its import graph.
15
+ * It has no path to the private or staff routes; their client is its own entry, later.
16
+ */
17
+
18
+ export { createCommunityClient } from './client'
19
+ export type { CommunityClient, CommunityClientConfig, CommunityCallOptions } from './client'
20
+
21
+ export {
22
+ COMMUNITY_OPS,
23
+ communityOpPath,
24
+ newClientMsgId,
25
+ COMMUNITY_MESSAGE_MAX,
26
+ COMMUNITY_MESSAGE_LINKS_MAX,
27
+ COMMUNITY_REPORT_NOTE_MAX,
28
+ COMMUNITY_APPEAL_MAX,
29
+ COMMUNITY_NOTICE_DESCRIPTION_MAX,
30
+ COMMUNITY_NOTIFIER_NAME_MAX,
31
+ COMMUNITY_DISPLAY_NAME_MIN,
32
+ COMMUNITY_DISPLAY_NAME_MAX,
33
+ COMMUNITY_BIO_MAX,
34
+ COMMUNITY_HANDLE_RE,
35
+ COMMUNITY_HANDLE_CHANGE_DAYS,
36
+ COMMUNITY_HEAD_ARTICLES_MAX,
37
+ COMMUNITY_PAGE_SIZE,
38
+ COMMUNITY_REPLIES_MAX,
39
+ COMMUNITY_NOTICE_TURNSTILE_ACTION,
40
+ COMMUNITY_GOOD_FAITH,
41
+ COMMUNITY_CLIENT_MSG_ID_RE,
42
+ } from './routes'
43
+ export type { CommunityOp, CommunityOpName, CommunitySurface } from './routes'
44
+
45
+ export {
46
+ COMMUNITY_UI_CODES,
47
+ COMMUNITY_API_CODES,
48
+ COMMUNITY_OUTCOME_OF_KB,
49
+ COMMUNITY_HOST_CODES,
50
+ COMMUNITY_OUTCOME_OF_HOST,
51
+ CommunityTransportError,
52
+ isCommunityTransportError,
53
+ isCommunityUiCode,
54
+ isLoggedCommunityOutcome,
55
+ communityOutcomeOf,
56
+ communityFailureOf,
57
+ } from './outcome'
58
+ export type {
59
+ CommunityUiCode,
60
+ CommunityApiCode,
61
+ CommunityHostCode,
62
+ CommunityOutcome,
63
+ CommunityFailure,
64
+ CommunityLogger,
65
+ } from './outcome'
66
+
67
+ export { fetchTransport, isRelativeCommunityBase, assertCommunityBase, communityOpUrl } from './transport'
68
+ export type { CommunityTransport, CommunityTransportAnswer, FetchTransportOptions } from './transport'
69
+
70
+ export {
71
+ MESSAGE_STATES,
72
+ AUTHOR_KINDS,
73
+ NEEDS,
74
+ RESTRICTION_KINDS,
75
+ NOTIFICATION_CODES,
76
+ CASE_CATEGORIES,
77
+ CATEGORY_WITHOUT_NOTIFIER,
78
+ AGE_BANDS,
79
+ LEGAL_TEXT_KINDS,
80
+ isKnownState,
81
+ isKnownAuthorKind,
82
+ isKnownAuthor,
83
+ isKnownNeed,
84
+ isKnownRestrictionKind,
85
+ isKnownNotificationCode,
86
+ isCaseCategory,
87
+ communityRulesOf,
88
+ communityLegalSectionsOf,
89
+ } from './shapes'
90
+ export type {
91
+ KnownMessageState,
92
+ MessageState,
93
+ KnownAuthorKind,
94
+ AuthorKind,
95
+ KnownNeed,
96
+ Need,
97
+ KnownRestrictionKind,
98
+ RestrictionKind,
99
+ KnownNotificationCode,
100
+ NotificationCode,
101
+ CaseCategory,
102
+ AgeBand,
103
+ LegalTextKind,
104
+ AcceptanceState,
105
+ MemberAuthor,
106
+ DeletedMemberAuthor,
107
+ StaffAuthor,
108
+ SystemAuthor,
109
+ KnownAuthor,
110
+ UnknownAuthor,
111
+ Author,
112
+ ReactionSummary,
113
+ PublicMessage,
114
+ RestrictionInEffect,
115
+ DiscussionViewerState,
116
+ Discussion,
117
+ DiscussionHead,
118
+ WriteAnswer,
119
+ ReactionTarget,
120
+ BlockEntry,
121
+ PublicProfile,
122
+ OwnProfile,
123
+ MyState,
124
+ NotificationItem,
125
+ CommunityPage,
126
+ DecisionSummary,
127
+ StatementSubject,
128
+ Statement,
129
+ AppealItem,
130
+ ReportItem,
131
+ CurrentLegalText,
132
+ CommunityRule,
133
+ CommunityLegalSection,
134
+ MemberExport,
135
+ NoticeTarget,
136
+ NoticeReceipt,
137
+ NoticeStatus,
138
+ DiscussionListInput,
139
+ MessagePostInput,
140
+ MessageEditInput,
141
+ MessageReportInput,
142
+ OnboardInput,
143
+ ProfileUpdateInput,
144
+ NoticeSubmitInput,
145
+ } from './shapes'
146
+
147
+ export {
148
+ createDiscussionStore,
149
+ refreshDiscussion,
150
+ loadMoreDiscussion,
151
+ viewerNeeds,
152
+ findMessage,
153
+ isOwnMessage,
154
+ reactionsFor,
155
+ reactionAfter,
156
+ } from './store'
157
+ export type {
158
+ DiscussionStore,
159
+ DiscussionState,
160
+ DiscussionMessage,
161
+ DiscussionViewer,
162
+ DiscussionRestriction,
163
+ MessageChange,
164
+ ViewerNeeds,
165
+ } from './store'
166
+
167
+ export { pollDiscussionHead, POLL_INTERVAL_MS, POLL_MAX_BACKOFF_MS } from './poll'
168
+ export type { PollDiscussionHeadOptions, DiscussionHeadPoller } from './poll'
169
+
170
+ export { COMMUNITY_CHANNELS } from './channels'
171
+
172
+ export {
173
+ COMMUNITY_ROUTES,
174
+ COMMUNITY_RECEIPT_RE,
175
+ statementPath,
176
+ myCommunityPath,
177
+ noticeStatusLink,
178
+ signInLink,
179
+ receiptFromHash,
180
+ readReceiptFromHash,
181
+ } from './links'
182
+
183
+ export {
184
+ COMMUNITY_MESSAGES,
185
+ communityMessage,
186
+ communityMessageLanguage,
187
+ hasCommunityMessage,
188
+ warningMessageKey,
189
+ confirmMessageKey,
190
+ noteMessageKey,
191
+ } from './messages'
192
+ export type { CommunityMessageKey, CommunityMessageLanguage } from './messages'
@@ -0,0 +1,174 @@
1
+ /**
2
+ * The community's deep links (docs/internal/m1-contracts.md §7.9, §10.2, §12): a statement of reasons, the
3
+ * „Moje komunita" panel, a notifier's status page — and the notifier's receipt, which travels in a link's
4
+ * FRAGMENT only (`#t=<receipt>`): a fragment never reaches a server's log, a `Referer` or a proxy, a path or a
5
+ * query would (community.md invariant 8). The page that reads it removes it from the address bar at once.
6
+ *
7
+ * The host's pages are the host's: a tenant names its own in `community.settings` (`statement_link_path` with
8
+ * `{id}`, `notice_status_link_path`, `my_community_link_path` — they come back in MyState.links) and an element
9
+ * takes them as attributes. Without them the managed blog's pages apply, COMMUNITY_ROUTES — the paths kb-api's
10
+ * deliveries fall back to (a drift spec pins them to kb-api's).
11
+ *
12
+ * Browser-safe; readReceiptFromHash is the one function that touches `window` (on call, never on import).
13
+ */
14
+
15
+ /**
16
+ * The managed blog's community pages (router-relative; the blog prepends its own base). The three legal pages —
17
+ * `rules`, `terms`, `privacy` — are each the tenant's current `legal/current` text of that kind, rendered by the blog's
18
+ * server without JavaScript, with `/community/<kind>.json` as the machine-readable alternate (DSA art. 14(1)).
19
+ */
20
+ export const COMMUNITY_ROUTES = Object.freeze({
21
+ /** The „Moje komunita" panel. */
22
+ myCommunity: '/community',
23
+ /** A statement of reasons and its appeal; `{id}` is the decision's id (statementPath). */
24
+ statement: '/community/statements/{id}',
25
+ /** The notice form (anyone, no sign-in needed). */
26
+ report: '/community/report',
27
+ /** A notifier's status page; the receipt goes in its fragment (noticeStatusLink). */
28
+ notices: '/community/notices',
29
+ /** The rules, rendered on the server without JavaScript. */
30
+ rules: '/community/rules',
31
+ /** The discussion terms (DSA art. 14), rendered on the server without JavaScript, as the rules. */
32
+ terms: '/community/terms',
33
+ /** The discussion privacy notice, rendered the same way. */
34
+ privacy: '/community/privacy',
35
+ } as const)
36
+
37
+ /** A decision's id (kb-api's UUIDs). */
38
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
39
+
40
+ /** A notifier's receipt: kb-api's `kbnt_` capability token (≤ 512 characters, base64url). */
41
+ export const COMMUNITY_RECEIPT_RE = /^kbnt_[A-Za-z0-9_-]{1,507}$/
42
+
43
+ /** The fragment key of the receipt (`#t=…`), kb-api's RECEIPT_FRAGMENT_KEY. */
44
+ const RECEIPT_KEY = 't'
45
+
46
+ /** A path on the host's own site: one leading `/` (never `//` or `/\`), no blank, control character or backslash. */
47
+ function isSitePath(path: unknown): path is string {
48
+ return typeof path === 'string' && /^\/(?![/\\])/.test(path) && !/[\s\\\u0000-\u001f\u007f]/.test(path)
49
+ }
50
+
51
+ /**
52
+ * A statement's page: `{id}` of the template filled with the decision's id. The template is the host's (its
53
+ * attribute, MyState.links.statementPath); absent, the managed blog's. Null for an id that is not a decision id, or
54
+ * a template that is no site path, lacks `{id}` or holds another brace — no link is built from arbitrary text.
55
+ */
56
+ export function statementPath(template: string | null | undefined, decisionId: string): string | null {
57
+ const t = template === null || template === undefined || template === '' ? COMMUNITY_ROUTES.statement : template
58
+ if (!isSitePath(t) || !t.includes('{id}') || /[{}]/.test(t.split('{id}').join(''))) return null
59
+ if (typeof decisionId !== 'string' || !UUID_RE.test(decisionId)) return null
60
+ return t.split('{id}').join(decisionId.toLowerCase())
61
+ }
62
+
63
+ /** The „Moje komunita" page: the host's path when it is a site path, else the managed blog's. */
64
+ export function myCommunityPath(path?: string | null): string {
65
+ return isSitePath(path) ? path : COMMUNITY_ROUTES.myCommunity
66
+ }
67
+
68
+ /**
69
+ * A notifier's status link: the host's status page (absent: the managed blog's) with the receipt in the
70
+ * fragment. Null for a path that is no site path or already has a fragment, and for anything that is not a receipt.
71
+ */
72
+ export function noticeStatusLink(path: string | null | undefined, receipt: string): string | null {
73
+ const p = path === null || path === undefined || path === '' ? COMMUNITY_ROUTES.notices : path
74
+ if (!isSitePath(p) || p.includes('#')) return null
75
+ if (typeof receipt !== 'string' || !COMMUNITY_RECEIPT_RE.test(receipt)) return null
76
+ return `${p}#${RECEIPT_KEY}=${receipt}`
77
+ }
78
+
79
+ /**
80
+ * The sign-in link for an anonymous viewer: `signInHref` with the page's path in the fragment (the storefront's
81
+ * rule: the sign-in page reads it and returns there). A `signInHref` that carries a fragment already is kept as it
82
+ * is; one that is no site path, or a `returnPath` that is none, gives the bare link (or null without a usable href).
83
+ */
84
+ export function signInLink(signInHref: string | null | undefined, returnPath?: string | null): string | null {
85
+ if (!isSitePath(signInHref)) return null
86
+ if (signInHref.includes('#') || !isSitePath(returnPath) || returnPath.includes('#')) return signInHref
87
+ return `${signInHref}#${returnPath}`
88
+ }
89
+
90
+ /**
91
+ * The receipt in a URL fragment (`#t=<receipt>`), or null. Nothing else is read from the fragment, and only
92
+ * kb-api's exact receipt shape is accepted: a link cut short by a mail client, or with a parameter appended, is
93
+ * "no receipt", never a token sent to the host.
94
+ */
95
+ export function receiptFromHash(hash: string): string | null {
96
+ const match = /^#t=([^&]*)/.exec(typeof hash === 'string' ? hash : '')
97
+ const receipt = match?.[1] ?? ''
98
+ return COMMUNITY_RECEIPT_RE.test(receipt) ? receipt : null
99
+ }
100
+
101
+ /** The receipt each window's address bar carried, read once (the fragment is gone after the first read). */
102
+ const receipts = new WeakMap<Window, string | null>()
103
+
104
+ function browserWindow(): Window {
105
+ if (typeof window === 'undefined') {
106
+ throw new Error(
107
+ '[blogmcp-sdk] readReceiptFromHash reads the address bar — call it in the browser (onMounted / useEffect), ' +
108
+ 'never while rendering on the server',
109
+ )
110
+ }
111
+ return window
112
+ }
113
+
114
+ /** A plain object — a router's state record — not an array, a class instance or null. */
115
+ function isPlainRecord(value: unknown): value is Record<string, unknown> {
116
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return false
117
+ const proto = Object.getPrototypeOf(value)
118
+ return proto === Object.prototype || proto === null
119
+ }
120
+
121
+ /** `value` without the fragment: cut where it starts, found as the address bar showed it and by the receipt itself. */
122
+ function withoutFragment(value: string, hash: string, receipt: string | null): string {
123
+ let out = value
124
+ const at = hash ? out.indexOf(hash) : -1
125
+ if (at >= 0) out = out.slice(0, at)
126
+ if (receipt !== null) {
127
+ for (let i = out.indexOf(receipt); i >= 0; i = out.indexOf(receipt)) {
128
+ const fragment = out.lastIndexOf('#', i)
129
+ out = fragment >= 0 ? out.slice(0, fragment) : out.slice(0, i) + out.slice(i + receipt.length)
130
+ }
131
+ }
132
+ return out
133
+ }
134
+
135
+ /**
136
+ * The router's history state without any copy of the fragment (vue-router keeps the full URL in
137
+ * `history.state.current`, `back` and `forward`): a SHALLOW copy where only top-level strings change.
138
+ */
139
+ function scrubbedState(state: unknown, hash: string, receipt: string | null): unknown {
140
+ if (!isPlainRecord(state)) return state
141
+ let changed = false
142
+ const copy: Record<string, unknown> = { ...state }
143
+ for (const [key, value] of Object.entries(copy)) {
144
+ if (typeof value !== 'string') continue
145
+ const clean = withoutFragment(value, hash, receipt)
146
+ if (clean !== value) {
147
+ copy[key] = clean
148
+ changed = true
149
+ }
150
+ }
151
+ return changed ? copy : state
152
+ }
153
+
154
+ /**
155
+ * The notifier's receipt from the address bar, which is then cleared of it — the URL and the router's copy in
156
+ * `history.state` (`history.replaceState`, keeping the query string and the router's other state).
157
+ *
158
+ * The receipt opens the notifier's case: left in the address bar it ends up in the history, a bookmark, a shared
159
+ * link. Only a `#t=` fragment is cleared (a valid receipt or a cut one alike); the page's own anchors stay. It is
160
+ * remembered per window, so a second call (React StrictMode mounts twice, Vue remounts) still gets it from an
161
+ * address bar that is already clean; a later `#t=` link pasted into the same tab replaces it.
162
+ *
163
+ * ⚠️ In a vue-router app follow it with `router.replace({ hash: '' })`, so the router forgets the fragment too.
164
+ */
165
+ export function readReceiptFromHash(win: Window = browserWindow()): string | null {
166
+ const hash = win.location.hash
167
+ const fresh = receiptFromHash(hash)
168
+ const first = !receipts.has(win)
169
+ if (hash.startsWith(`#${RECEIPT_KEY}=`)) {
170
+ win.history.replaceState(scrubbedState(win.history.state, hash, fresh), '', win.location.pathname + win.location.search)
171
+ }
172
+ if (first || fresh !== null) receipts.set(win, fresh)
173
+ return receipts.get(win) ?? null
174
+ }