@stapel/alerts-react 0.1.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 (166) hide show
  1. package/CHANGELOG.md +109 -0
  2. package/LICENSE +21 -0
  3. package/MODULE.md +140 -0
  4. package/README.md +147 -0
  5. package/dist/analytics/events.d.ts +29 -0
  6. package/dist/analytics/events.d.ts.map +1 -0
  7. package/dist/analytics/events.js +28 -0
  8. package/dist/analytics/events.js.map +1 -0
  9. package/dist/api/alertsApi.d.ts +84 -0
  10. package/dist/api/alertsApi.d.ts.map +1 -0
  11. package/dist/api/alertsApi.js +127 -0
  12. package/dist/api/alertsApi.js.map +1 -0
  13. package/dist/api/generated/schema.d.ts +453 -0
  14. package/dist/api/generated/schema.d.ts.map +1 -0
  15. package/dist/api/generated/schema.js +2 -0
  16. package/dist/api/generated/schema.js.map +1 -0
  17. package/dist/api/types.d.ts +78 -0
  18. package/dist/api/types.d.ts.map +1 -0
  19. package/dist/api/types.js +44 -0
  20. package/dist/api/types.js.map +1 -0
  21. package/dist/default/FixIssueDialog.d.ts +15 -0
  22. package/dist/default/FixIssueDialog.d.ts.map +1 -0
  23. package/dist/default/FixIssueDialog.js +67 -0
  24. package/dist/default/FixIssueDialog.js.map +1 -0
  25. package/dist/default/IssueDetail.d.ts +10 -0
  26. package/dist/default/IssueDetail.d.ts.map +1 -0
  27. package/dist/default/IssueDetail.js +83 -0
  28. package/dist/default/IssueDetail.js.map +1 -0
  29. package/dist/default/IssueFilterBar.d.ts +21 -0
  30. package/dist/default/IssueFilterBar.d.ts.map +1 -0
  31. package/dist/default/IssueFilterBar.js +86 -0
  32. package/dist/default/IssueFilterBar.js.map +1 -0
  33. package/dist/default/IssuesFeed.d.ts +10 -0
  34. package/dist/default/IssuesFeed.d.ts.map +1 -0
  35. package/dist/default/IssuesFeed.js +137 -0
  36. package/dist/default/IssuesFeed.js.map +1 -0
  37. package/dist/default/MuteIssueDialog.d.ts +19 -0
  38. package/dist/default/MuteIssueDialog.d.ts.map +1 -0
  39. package/dist/default/MuteIssueDialog.js +72 -0
  40. package/dist/default/MuteIssueDialog.js.map +1 -0
  41. package/dist/default/index.d.ts +32 -0
  42. package/dist/default/index.d.ts.map +1 -0
  43. package/dist/default/index.js +26 -0
  44. package/dist/default/index.js.map +1 -0
  45. package/dist/default/labels.d.ts +40 -0
  46. package/dist/default/labels.d.ts.map +1 -0
  47. package/dist/default/labels.js +94 -0
  48. package/dist/default/labels.js.map +1 -0
  49. package/dist/default/layout.d.ts +32 -0
  50. package/dist/default/layout.d.ts.map +1 -0
  51. package/dist/default/layout.js +44 -0
  52. package/dist/default/layout.js.map +1 -0
  53. package/dist/default/types.d.ts +27 -0
  54. package/dist/default/types.d.ts.map +1 -0
  55. package/dist/default/types.js +2 -0
  56. package/dist/default/types.js.map +1 -0
  57. package/dist/flows/errors.d.ts +12 -0
  58. package/dist/flows/errors.d.ts.map +1 -0
  59. package/dist/flows/errors.js +13 -0
  60. package/dist/flows/errors.js.map +1 -0
  61. package/dist/flows/registry.d.ts +22 -0
  62. package/dist/flows/registry.d.ts.map +1 -0
  63. package/dist/flows/registry.js +20 -0
  64. package/dist/flows/registry.js.map +1 -0
  65. package/dist/headless/AlertsProvider.d.ts +19 -0
  66. package/dist/headless/AlertsProvider.d.ts.map +1 -0
  67. package/dist/headless/AlertsProvider.js +15 -0
  68. package/dist/headless/AlertsProvider.js.map +1 -0
  69. package/dist/i18n/errorsMap.d.ts +10 -0
  70. package/dist/i18n/errorsMap.d.ts.map +1 -0
  71. package/dist/i18n/errorsMap.js +20 -0
  72. package/dist/i18n/errorsMap.js.map +1 -0
  73. package/dist/i18n/es.d.ts +20 -0
  74. package/dist/i18n/es.d.ts.map +1 -0
  75. package/dist/i18n/es.js +152 -0
  76. package/dist/i18n/es.js.map +1 -0
  77. package/dist/i18n/generated/errors.es.gen.d.ts +9 -0
  78. package/dist/i18n/generated/errors.es.gen.d.ts.map +1 -0
  79. package/dist/i18n/generated/errors.es.gen.js +57 -0
  80. package/dist/i18n/generated/errors.es.gen.js.map +1 -0
  81. package/dist/i18n/generated/errors.gen.d.ts +323 -0
  82. package/dist/i18n/generated/errors.gen.d.ts.map +1 -0
  83. package/dist/i18n/generated/errors.gen.js +165 -0
  84. package/dist/i18n/generated/errors.gen.js.map +1 -0
  85. package/dist/i18n/generated/errors.ru.gen.d.ts +9 -0
  86. package/dist/i18n/generated/errors.ru.gen.d.ts.map +1 -0
  87. package/dist/i18n/generated/errors.ru.gen.js +57 -0
  88. package/dist/i18n/generated/errors.ru.gen.js.map +1 -0
  89. package/dist/i18n/keys.d.ts +164 -0
  90. package/dist/i18n/keys.d.ts.map +1 -0
  91. package/dist/i18n/keys.js +300 -0
  92. package/dist/i18n/keys.js.map +1 -0
  93. package/dist/i18n/ru.d.ts +27 -0
  94. package/dist/i18n/ru.d.ts.map +1 -0
  95. package/dist/i18n/ru.js +159 -0
  96. package/dist/i18n/ru.js.map +1 -0
  97. package/dist/index.d.ts +42 -0
  98. package/dist/index.d.ts.map +1 -0
  99. package/dist/index.js +45 -0
  100. package/dist/index.js.map +1 -0
  101. package/dist/model/context.d.ts +11 -0
  102. package/dist/model/context.d.ts.map +1 -0
  103. package/dist/model/context.js +16 -0
  104. package/dist/model/context.js.map +1 -0
  105. package/dist/model/issues.d.ts +103 -0
  106. package/dist/model/issues.d.ts.map +1 -0
  107. package/dist/model/issues.js +230 -0
  108. package/dist/model/issues.js.map +1 -0
  109. package/dist/model/queryKeys.d.ts +11 -0
  110. package/dist/model/queryKeys.d.ts.map +1 -0
  111. package/dist/model/queryKeys.js +32 -0
  112. package/dist/model/queryKeys.js.map +1 -0
  113. package/dist/model/refusals.d.ts +25 -0
  114. package/dist/model/refusals.d.ts.map +1 -0
  115. package/dist/model/refusals.js +73 -0
  116. package/dist/model/refusals.js.map +1 -0
  117. package/dist/model/runtime.d.ts +51 -0
  118. package/dist/model/runtime.d.ts.map +1 -0
  119. package/dist/model/runtime.js +29 -0
  120. package/dist/model/runtime.js.map +1 -0
  121. package/dist/model/status.d.ts +60 -0
  122. package/dist/model/status.d.ts.map +1 -0
  123. package/dist/model/status.js +116 -0
  124. package/dist/model/status.js.map +1 -0
  125. package/dist/nav/manifest.d.ts +43 -0
  126. package/dist/nav/manifest.d.ts.map +1 -0
  127. package/dist/nav/manifest.js +36 -0
  128. package/dist/nav/manifest.js.map +1 -0
  129. package/llms.txt +84 -0
  130. package/manifest.json +694 -0
  131. package/nav-manifest.json +46 -0
  132. package/package.json +119 -0
  133. package/src/analytics/events.ts +29 -0
  134. package/src/analytics/generated/events.json +7 -0
  135. package/src/api/alertsApi.ts +259 -0
  136. package/src/api/generated/schema.ts +455 -0
  137. package/src/api/types.ts +113 -0
  138. package/src/default/FixIssueDialog.tsx +158 -0
  139. package/src/default/IssueDetail.tsx +431 -0
  140. package/src/default/IssueFilterBar.tsx +197 -0
  141. package/src/default/IssuesFeed.tsx +343 -0
  142. package/src/default/MuteIssueDialog.tsx +186 -0
  143. package/src/default/index.ts +37 -0
  144. package/src/default/labels.ts +113 -0
  145. package/src/default/layout.ts +52 -0
  146. package/src/default/types.ts +27 -0
  147. package/src/flows/errors.ts +16 -0
  148. package/src/flows/registry.ts +32 -0
  149. package/src/headless/AlertsProvider.tsx +20 -0
  150. package/src/i18n/errorsMap.ts +31 -0
  151. package/src/i18n/es.ts +181 -0
  152. package/src/i18n/generated/errors.es.gen.ts +63 -0
  153. package/src/i18n/generated/errors.gen.ts +196 -0
  154. package/src/i18n/generated/errors.json +370 -0
  155. package/src/i18n/generated/errors.ru.gen.ts +63 -0
  156. package/src/i18n/keys.ts +341 -0
  157. package/src/i18n/ru.ts +188 -0
  158. package/src/index.ts +152 -0
  159. package/src/model/context.tsx +28 -0
  160. package/src/model/issues.ts +311 -0
  161. package/src/model/queryKeys.ts +41 -0
  162. package/src/model/refusals.ts +82 -0
  163. package/src/model/runtime.ts +80 -0
  164. package/src/model/status.ts +179 -0
  165. package/src/nav/manifest.ts +76 -0
  166. package/tsconfig.json +26 -0
@@ -0,0 +1,46 @@
1
+ {
2
+ "package": "@stapel/alerts-react",
3
+ "version": "0.1.1",
4
+ "entries": [
5
+ {
6
+ "id": "admin.alerts",
7
+ "labelKey": "alerts.nav.feed",
8
+ "icon": "BellOutlined",
9
+ "route": {
10
+ "path": "alerts"
11
+ },
12
+ "component": {
13
+ "export": "IssuesFeed",
14
+ "subpath": "default"
15
+ },
16
+ "placement": {
17
+ "level": "submenu",
18
+ "parentId": "admin.root"
19
+ },
20
+ "menuVisibleDefault": true,
21
+ "requiresAuth": true,
22
+ "surface": "member",
23
+ "order": 10
24
+ },
25
+ {
26
+ "id": "admin.alerts-issue",
27
+ "labelKey": "alerts.nav.issue",
28
+ "icon": "AuditOutlined",
29
+ "route": {
30
+ "path": ":issueId"
31
+ },
32
+ "component": {
33
+ "export": "IssueDetail",
34
+ "subpath": "default"
35
+ },
36
+ "placement": {
37
+ "level": "submenu",
38
+ "parentId": "admin.alerts"
39
+ },
40
+ "menuVisibleDefault": false,
41
+ "requiresAuth": true,
42
+ "surface": "member",
43
+ "order": 11
44
+ }
45
+ ]
46
+ }
package/package.json ADDED
@@ -0,0 +1,119 @@
1
+ {
2
+ "name": "@stapel/alerts-react",
3
+ "version": "0.1.1",
4
+ "description": "Headless React pair for stapel-alerts — the fleet's error tracker read and reconciled from a browser: the triage feed grouped by service with level, count and first/last seen, the issue with its last events, their traces and their redacted context, and the three writes an operator owns (mark fixed with the version and sha that claim it, mute until a date, reopen). The reads are ETag-aware — a conditional GET that comes back 304 keeps the rows already on screen instead of repainting them — and `regressed` is never offered as a status to set, because the store asserts it from evidence. Staff-only behind the host's session; the refusal is named, not drawn as an empty table. Zero visual opinion in the main entry; the opt-in `/default` subpath ships the antd skin.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/usestapel/stapel-react.git",
9
+ "directory": "packages/alerts-react"
10
+ },
11
+ "type": "module",
12
+ "sideEffects": false,
13
+ "main": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./default": {
21
+ "types": "./dist/default/index.d.ts",
22
+ "default": "./dist/default/index.js"
23
+ },
24
+ "./i18n/ru": {
25
+ "types": "./dist/i18n/ru.d.ts",
26
+ "default": "./dist/i18n/ru.js"
27
+ },
28
+ "./i18n/es": {
29
+ "types": "./dist/i18n/es.d.ts",
30
+ "default": "./dist/i18n/es.js"
31
+ },
32
+ "./manifest": "./manifest.json",
33
+ "./manifest.json": "./manifest.json",
34
+ "./nav-manifest": "./nav-manifest.json",
35
+ "./nav-manifest.json": "./nav-manifest.json",
36
+ "./llms.txt": "./llms.txt",
37
+ "./package.json": "./package.json"
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "src",
42
+ "tsconfig.json",
43
+ "README.md",
44
+ "MODULE.md",
45
+ "CHANGELOG.md",
46
+ "manifest.json",
47
+ "nav-manifest.json",
48
+ "llms.txt"
49
+ ],
50
+ "size-limit": [
51
+ {
52
+ "name": "index — the headless pair (client + the two reads + the three writes): no antd",
53
+ "path": "dist/index.js",
54
+ "limit": "12 KB"
55
+ },
56
+ {
57
+ "name": "default — the antd triage skin (feed, detail, filters, the two dialogs)",
58
+ "path": "dist/default/index.js",
59
+ "limit": "20 KB"
60
+ },
61
+ {
62
+ "name": "i18n/ru — the Russian bundle is opt-in, not carried by the main entry",
63
+ "path": "dist/i18n/ru.js",
64
+ "limit": "8 KB"
65
+ },
66
+ {
67
+ "name": "i18n/es — same deal for Spanish: a host pays for the locales it ships",
68
+ "path": "dist/i18n/es.js",
69
+ "limit": "8 KB"
70
+ }
71
+ ],
72
+ "peerDependencies": {
73
+ "@stapel/core": ">=0.22.0 <1.0.0",
74
+ "@stapel/tokens": ">=0.6.0",
75
+ "@stapel/tokens-antd": ">=0.15.0",
76
+ "@tanstack/react-query": "^5.0.0",
77
+ "antd": ">=5.20.0 <7",
78
+ "react": ">=19"
79
+ },
80
+ "peerDependenciesMeta": {
81
+ "@stapel/tokens-antd": {
82
+ "optional": true
83
+ },
84
+ "antd": {
85
+ "optional": true
86
+ }
87
+ },
88
+ "devDependencies": {
89
+ "@size-limit/preset-small-lib": "^11.2.0",
90
+ "@tanstack/react-query": "^5.81.0",
91
+ "@testing-library/react": "^16.3.0",
92
+ "@types/react": "^19.1.0",
93
+ "@types/react-dom": "^19.1.0",
94
+ "antd": "^6.5.0",
95
+ "jsdom": "^26.1.0",
96
+ "react": "^19.1.0",
97
+ "react-dom": "^19.1.0",
98
+ "size-limit": "^11.2.0",
99
+ "typescript": "^5.8.3",
100
+ "vitest": "^3.2.4",
101
+ "@stapel/core": "^0.26.1",
102
+ "@stapel/tokens": "^0.8.1",
103
+ "@stapel/tokens-antd": "^0.22.1",
104
+ "@stapel/showcase": "^0.3.0"
105
+ },
106
+ "engines": {
107
+ "node": ">=22"
108
+ },
109
+ "publishConfig": {
110
+ "access": "public"
111
+ },
112
+ "scripts": {
113
+ "build": "tsc -p tsconfig.json",
114
+ "test": "tsc -p tsconfig.test.json && vitest run --exclude test/prodBundlePurity.test.ts",
115
+ "test:pack": "vitest run test/prodBundlePurity.test.ts",
116
+ "lint": "eslint .",
117
+ "size": "size-limit"
118
+ }
119
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The pair's analytics vocabulary.
3
+ *
4
+ * Three events, and every one of them is a JUDGEMENT an operator made about a
5
+ * live bug — this is fixed, by this release; this is known and I do not want
6
+ * to hear about it until Monday; this is back, reopen it. Reading the feed is
7
+ * not on the list: it happens whenever the tab is open and reports nothing
8
+ * about what anyone decided.
9
+ *
10
+ * The names are constants rather than `defineEvent` declarations because a
11
+ * pair carries no `@stapel/analytics` runtime by architecture (slim wave
12
+ * §21/S1): it emits through the `Analytics` SEAM the host injects.
13
+ *
14
+ * **Nothing here carries an issue id, a title, a trace or a service name.** A
15
+ * fingerprint plus a culprit is a map of where a system is weakest, and a
16
+ * product-analytics stream is not where that belongs. `level` is the whole
17
+ * payload — enough to answer "are operators closing fatals or debug noise",
18
+ * which is the only question this data is for.
19
+ */
20
+ export const ALERTS_EVENTS = {
21
+ /** An issue was closed with a release. Prop: `level`. */
22
+ issueFixed: "alerts.issue.fixed",
23
+ /** An issue was muted. Props: `level`, `hasDeadline`. */
24
+ issueMuted: "alerts.issue.muted",
25
+ /** An issue was put back on the board. Prop: `level`. */
26
+ issueReopened: "alerts.issue.reopened",
27
+ } as const;
28
+
29
+ export type AlertsEventName = (typeof ALERTS_EVENTS)[keyof typeof ALERTS_EVENTS];
@@ -0,0 +1,7 @@
1
+ {
2
+ "$generated": "by scripts/gen-events.mjs — do not edit; drift-gated (pnpm gen:events:check)",
3
+ "package": "@stapel/alerts-react",
4
+ "version": "0.1.1",
5
+ "defined": [],
6
+ "flows": []
7
+ }
@@ -0,0 +1,259 @@
1
+ /**
2
+ * The pair's typed operation surface over stapel-alerts (`urls_v1.py`),
3
+ * mounted on the module base the host injects (`/alerts/api/v1/`).
4
+ *
5
+ * ── Four operations, and the fifth is deliberately absent ─────────────────
6
+ *
7
+ * The module has five routes. `POST /report` is not one of the four here, and
8
+ * never will be: a reporter authenticates with `X-Service-Key`, that key lives
9
+ * in every container in the fleet, and the blast radius of one leaking must
10
+ * not include a browser. This pair READS and MANAGES the tracker; the things
11
+ * that report to it are processes.
12
+ *
13
+ * ── The two reads are conditional, and the JSON client cannot do that ─────
14
+ *
15
+ * Both `GET`s answer an `ETag` and honour `If-None-Match` with a **304 and no
16
+ * body** (`views._conditional`) — the module's own stated requirement, because
17
+ * a triage screen open on a wall re-asks every minute and should not be handed
18
+ * a page of JSON it already has.
19
+ *
20
+ * `@stapel/core`'s `StapelClient` cannot express that on either side: it parses
21
+ * a JSON body and hands back nothing else (no response headers, so the `ETag`
22
+ * is unreadable), and a 304 is not `response.ok`, so it would be THROWN as a
23
+ * `StapelApiError` instead of read as "still what you have". So the two reads
24
+ * speak `fetch` directly (the `@stapel/docs-react` precedent for the same
25
+ * class of problem), bound to the runtime's base URL / credentials / headers.
26
+ *
27
+ * **One dialect out of both transports.** Every non-2xx from the raw reads is
28
+ * folded through core's own `parseErrorEnvelope`, so a caller catches a
29
+ * `StapelApiError` with a `.code` whether the failure came from the raw read
30
+ * or from the JSON client — the rethrown-body dialect CONTRIBUTING.md's "Mock
31
+ * the wire, not the module" section is about never exists here.
32
+ *
33
+ * Auth: the standard pair seams apply to the two WRITES (bearer refresh,
34
+ * verification-403) because they ride `StapelClient`. The raw reads carry the
35
+ * runtime's `credentials` and `defaultHeaders` — which is a session cookie for
36
+ * a cookie-mode host, and the header a bearer host passes as
37
+ * `defaultHeaders` — but not the 401-refresh retry. A 401 on a read surfaces
38
+ * as `error.401.unauthorized`, and the screen says so.
39
+ */
40
+ import { parseErrorEnvelope } from "@stapel/core";
41
+ import type { StapelClient } from "@stapel/core";
42
+ import type {
43
+ Issue,
44
+ IssueDetail,
45
+ IssueFix,
46
+ IssueLevel,
47
+ IssuePage,
48
+ IssuePatch,
49
+ IssueStatus,
50
+ } from "./types.js";
51
+
52
+ /** Query for `GET /issues` — every parameter `IssueListView` reads. */
53
+ export interface IssueFilters {
54
+ readonly status?: IssueStatus;
55
+ readonly level?: IssueLevel;
56
+ /** The reporting service's name, exactly as the tracker stores it. */
57
+ readonly service?: string;
58
+ /** ISO-8601; `last_seen >= since`. */
59
+ readonly since?: string;
60
+ /** Only `new` + `regressed` — the module's own definition of open. */
61
+ readonly open?: boolean;
62
+ /** Row offset; default 0. */
63
+ readonly offset?: number;
64
+ /**
65
+ * Page size, 1..200; the server's default (50) when omitted. Out-of-range
66
+ * values are CLAMPED, not refused, and the envelope echoes the size that was
67
+ * applied — page by `IssuePage.limit`, never by this number.
68
+ */
69
+ readonly limit?: number;
70
+ }
71
+
72
+ /**
73
+ * The answer to a conditional read.
74
+ *
75
+ * `"unchanged"` is a RESULT, not an error and not an absence: the server
76
+ * confirmed the rows on screen are current. A caller that folded it into
77
+ * `undefined` would blank a triage table every time nothing had happened,
78
+ * which is most of the time.
79
+ */
80
+ export type ConditionalRead<T> =
81
+ | {
82
+ readonly outcome: "modified";
83
+ /** The validator to send next time. `null` if the server sent none. */
84
+ readonly etag: string | null;
85
+ readonly data: T;
86
+ }
87
+ | {
88
+ readonly outcome: "unchanged";
89
+ readonly etag: string;
90
+ };
91
+
92
+ /** Per-read options: the validator to offer, and the abort signal. */
93
+ export interface ConditionalReadOptions {
94
+ /** The `ETag` from the previous read of this exact query, if any. */
95
+ readonly etag?: string | null | undefined;
96
+ readonly signal?: AbortSignal | undefined;
97
+ }
98
+
99
+ export interface AlertsApi {
100
+ readonly client: StapelClient;
101
+
102
+ /**
103
+ * The triage list, newest activity first.
104
+ *
105
+ * Answers a PAGE — `{count, offset, limit, results}`, the `IssuePage` the
106
+ * schema declares (`api/types.ts`).
107
+ */
108
+ issues(
109
+ filters?: IssueFilters,
110
+ options?: ConditionalReadOptions
111
+ ): Promise<ConditionalRead<IssuePage>>;
112
+
113
+ /** One issue with its last 20 events, their traces and their context. */
114
+ issue(
115
+ issueId: string,
116
+ options?: ConditionalReadOptions
117
+ ): Promise<ConditionalRead<IssueDetail>>;
118
+
119
+ /**
120
+ * Set a status a caller is allowed to assert, and/or leave a note.
121
+ *
122
+ * **`muted_until` on its own is a mute.** The field has no meaning in any
123
+ * other status, so a patch carrying only the deadline sets `status: "muted"`
124
+ * (`null` for a mute with no deadline), and any status other than `muted`
125
+ * ignores and CLEARS it — a deadline leaves with the mute. Omitting the key
126
+ * is not a mute at all, so `model/status.ts` always sends it.
127
+ */
128
+ patchIssue(issueId: string, patch: IssuePatch): Promise<Issue>;
129
+
130
+ /**
131
+ * Close an issue with the release that claims the fix.
132
+ *
133
+ * Idempotent by design: fixing a fixed issue overwrites the version, which
134
+ * is what re-deploying a better fix should do. Zeroes `count_since_fix`, so
135
+ * "has anything happened since?" stays a column rather than a query over
136
+ * events retention may already have swept.
137
+ */
138
+ fixIssue(issueId: string, fix: IssueFix): Promise<Issue>;
139
+ }
140
+
141
+ /** The raw-transport knobs `createAlertsRuntime` forwards (see the header). */
142
+ export interface AlertsRawOptions {
143
+ readonly fetch?: typeof globalThis.fetch;
144
+ readonly credentials?: RequestCredentials;
145
+ /** Merged into every raw read (a tenant id, a bearer header). */
146
+ readonly defaultHeaders?: Record<string, string>;
147
+ }
148
+
149
+ /** An id is a uuid from the server, and it is still escaped: the rule holds by
150
+ * shape rather than by an argument about this particular type. */
151
+ const seg = (value: string): string => encodeURIComponent(value);
152
+
153
+ /** Same shape as `StapelClient`'s own URL builder: one base, no double slash. */
154
+ function buildUrl(
155
+ baseUrl: string,
156
+ path: string,
157
+ query?: Readonly<Record<string, string | number | boolean | undefined>>
158
+ ): string {
159
+ const base = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
160
+ let url = `${base}${path.startsWith("/") ? path : `/${path}`}`;
161
+ if (query) {
162
+ const search = new URLSearchParams();
163
+ for (const [key, value] of Object.entries(query)) {
164
+ if (value === undefined) continue;
165
+ search.set(key, String(value));
166
+ }
167
+ const qs = search.toString();
168
+ if (qs.length > 0) url += `?${qs}`;
169
+ }
170
+ return url;
171
+ }
172
+
173
+ /** The body of a failed raw read, parsed the way the JSON client parses it. */
174
+ async function errorBodyOf(response: Response): Promise<unknown> {
175
+ const text = await response.text().catch(() => "");
176
+ if (text.length === 0) return undefined;
177
+ try {
178
+ return JSON.parse(text) as unknown;
179
+ } catch {
180
+ return text;
181
+ }
182
+ }
183
+
184
+ /**
185
+ * One conditional GET. The single place this package turns an HTTP answer into
186
+ * either a {@link ConditionalRead} or a `StapelApiError` — so the two
187
+ * transports of this pair speak ONE error dialect.
188
+ */
189
+ async function conditionalGet<T>(
190
+ options: AlertsRawOptions & { readonly baseUrl: string },
191
+ path: string,
192
+ query: Readonly<Record<string, string | number | boolean | undefined>>,
193
+ read: ConditionalReadOptions | undefined
194
+ ): Promise<ConditionalRead<T>> {
195
+ const doFetch = options.fetch ?? globalThis.fetch;
196
+ const headers = new Headers(options.defaultHeaders);
197
+ headers.set("Accept", "application/json");
198
+ if (read?.etag != null && read.etag.length > 0) {
199
+ headers.set("If-None-Match", read.etag);
200
+ }
201
+ const init: RequestInit = { method: "GET", headers };
202
+ if (options.credentials !== undefined) init.credentials = options.credentials;
203
+ if (read?.signal !== undefined) init.signal = read.signal;
204
+
205
+ const response = await doFetch(buildUrl(options.baseUrl, path, query), init);
206
+
207
+ if (response.status === 304) {
208
+ // The server echoes the validator it matched; fall back to the one we
209
+ // sent, so the next read still carries a validator either way.
210
+ const etag = response.headers.get("ETag") ?? read?.etag ?? "";
211
+ return { outcome: "unchanged", etag };
212
+ }
213
+ if (!response.ok) {
214
+ throw parseErrorEnvelope(response.status, await errorBodyOf(response));
215
+ }
216
+ return {
217
+ outcome: "modified",
218
+ etag: response.headers.get("ETag"),
219
+ data: (await response.json()) as T,
220
+ };
221
+ }
222
+
223
+ export function createAlertsApi(
224
+ client: StapelClient,
225
+ raw: AlertsRawOptions = {}
226
+ ): AlertsApi {
227
+ const transport = { ...raw, baseUrl: client.baseUrl };
228
+ return {
229
+ client,
230
+
231
+ issues: (filters, options) =>
232
+ conditionalGet<IssuePage>(
233
+ transport,
234
+ "/issues",
235
+ {
236
+ status: filters?.status,
237
+ level: filters?.level,
238
+ service: filters?.service,
239
+ since: filters?.since,
240
+ // The view reads `"1" | "true" | "True"`; anything else — including
241
+ // `open=false` — means "do not filter", which is exactly what
242
+ // omitting it means. So `false` is sent as nothing.
243
+ open: filters?.open === true ? true : undefined,
244
+ offset: filters?.offset,
245
+ limit: filters?.limit,
246
+ },
247
+ options
248
+ ),
249
+
250
+ issue: (issueId, options) =>
251
+ conditionalGet<IssueDetail>(transport, `/issues/${seg(issueId)}`, {}, options),
252
+
253
+ patchIssue: (issueId, patch) =>
254
+ client.patch(`/issues/${seg(issueId)}`, patch),
255
+
256
+ fixIssue: (issueId, fix) =>
257
+ client.post(`/issues/${seg(issueId)}/fix`, fix),
258
+ };
259
+ }