@checkstack/common 0.8.0 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,69 @@
1
1
  # @checkstack/common
2
2
 
3
+ ## 0.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 9016526: Add a `/rest/:pluginId/*` HTTP mount that serves every plugin's oRPC contract
8
+ through the REST/OpenAPI shape described by `/api/openapi.json`. Queries are
9
+ `GET` with query parameters, mutations are `POST` with the input as the raw
10
+ JSON body. The existing `/api/:pluginId/*` mount continues to serve oRPC's
11
+ native wire protocol unchanged, so existing clients are not affected.
12
+
13
+ The OpenAPI spec at `/api/openapi.json` now reflects the real mount: every
14
+ `paths` entry is prefixed with `/rest` instead of `/api`.
15
+
16
+ Also fixes a SPA-fallback bug: the backend's `/api-docs` route previously
17
+ returned 404 on production deployments because the static-file middleware
18
+ skipped any path starting with `/api`, capturing `/api-docs` along with real
19
+ API routes. The skip now requires a trailing slash (`/api/`, `/rest/`).
20
+
21
+ Required access rules are now visible in the API Docs UI. The OpenAPI spec
22
+ generator was reading a non-existent `accessRules` field on procedure
23
+ metadata; the real field is `access: AccessRule[]`. Each procedure's access
24
+ rules are now flattened to fully-qualified IDs (e.g. `catalog.system.read`)
25
+ and emitted under `x-orpc-meta.accessRules`, which the existing
26
+ `Required Access Rules` section in the docs UI already knew how to render.
27
+
28
+ The API Docs schema renderer now handles record types (zod `z.record`),
29
+ `$ref`s into `components.schemas`, `oneOf`/`anyOf`/`allOf`, nullable union
30
+ types (`type: ["string", "null"]`), and `format` qualifiers. Previously
31
+ record outputs like `{ statuses: object }` masked the actual value type;
32
+ they now render as `{ [key]: <ResolvedType> { ... } }` with the inner
33
+ schema expanded, capped at 12 levels with cycle detection.
34
+
35
+ **REST method conventions.** `proc()` now defaults to `GET` for queries and
36
+ `POST` for mutations on the `/rest` mount, using bracket-notation query
37
+ params (`?filter[status]=active&ids[0]=a`) for GET inputs. Existing
38
+ procedures were updated to follow REST semantics:
39
+
40
+ - `update*` mutations → `PATCH`
41
+ - `delete*` / `remove*` mutations → `DELETE`
42
+ - `getBulk*` queries and any query taking a large array input → `POST`
43
+ (because `@orpc/openapi@1.13.x` has no GET→POST URL-length fallback)
44
+
45
+ GET endpoints require an `object` input — bare scalars like
46
+ `.input(z.string())` are not valid on GET. `getSystemConfigurations` was
47
+ refactored from `.input(z.string())` to `.input(z.object({ systemId: ... }))`
48
+ to fit the GET shape; the only call-site update was the in-process router
49
+ unpacking `input.systemId` instead of passing `input` directly.
50
+
51
+ The API Docs UI now renders query parameters (path/query/header/cookie) in a
52
+ dedicated table for GET endpoints, and the fetch example shows them in the
53
+ URL with `<required>` / `<optional>` placeholders.
54
+
55
+ ## 0.9.0
56
+
57
+ ### Minor Changes
58
+
59
+ - 42abfff: Add practical-significance floors to anomaly detection.
60
+
61
+ Two new schema annotations — `x-anomaly-min-absolute-delta` and `x-anomaly-min-relative-delta` — let plugin authors and operators suppress alerts whose statistical deviation is large but practical impact is negligible. Both floors must clear in addition to the existing μ ± Nσ trigger; defaults are 0 (disabled) so existing behaviour is unchanged.
62
+
63
+ This is the fix for cases like a 6 ms latency baseline whose σ ≈ 1 ms causes routine 20 ms blips to fire as anomalies despite Δ=14 ms being operationally irrelevant. With `min-absolute-delta: 50` and `min-relative-delta: 0.5`, those blips stay silent while a 6 ms → 200 ms spike still fires.
64
+
65
+ Built-in plugins ship with sensible defaults applied to every per-run field: 50 ms + 50 % for ms-unit fields, 5 percentage points for `%`-unit fields, 1 + 25 % for counter fields, 1 GB + 5 % for disk fields, 50 MB + 10 % for memory fields, 1 day for TLS expiry, 0.5 + 25 % for load average, 1 + 5 % for Minecraft TPS. Operators can override per-system or per-field via the assignment UI.
66
+
3
67
  ## 0.8.0
4
68
 
5
69
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@checkstack/common",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "license": "Elastic-2.0",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -19,8 +19,8 @@
19
19
  },
20
20
  "devDependencies": {
21
21
  "typescript": "^5.7.2",
22
- "@checkstack/tsconfig": "0.0.6",
23
- "@checkstack/scripts": "0.1.2"
22
+ "@checkstack/tsconfig": "0.0.7",
23
+ "@checkstack/scripts": "0.3.1"
24
24
  },
25
25
  "scripts": {
26
26
  "typecheck": "tsgo -b",
@@ -22,6 +22,18 @@ export type ChartType =
22
22
  | "text"
23
23
  | "status";
24
24
 
25
+ /**
26
+ * Numeric anomaly directions — these operate on a μ ± Nσ statistical band
27
+ * over a continuous metric, so practical-significance floors apply.
28
+ */
29
+ export type NumericAnomalyDirection =
30
+ | "higher-is-better"
31
+ | "lower-is-better"
32
+ | "deviation";
33
+
34
+ /** Categorical anomaly direction — fires on dominance flips, no σ band. */
35
+ export type CategoricalAnomalyDirection = "dominance";
36
+
25
37
  /**
26
38
  * Base metadata for all health check result schema fields.
27
39
  */
@@ -63,14 +75,56 @@ export interface BaseHealthResultMeta {
63
75
  }
64
76
 
65
77
  /**
66
- * Metadata for a field that exposes a chart AND has anomaly detection enabled.
78
+ * Metadata for a chartable field with numeric anomaly detection.
79
+ * Carries the practical-significance floors (`x-anomaly-min-*-delta`)
80
+ * because they only make sense against a μ ± Nσ band.
81
+ */
82
+ export interface ChartMetaAnomalyNumeric extends BaseHealthResultMeta {
83
+ "x-chart-type": ChartType;
84
+ "x-anomaly-enabled": true;
85
+ "x-anomaly-direction": NumericAnomalyDirection;
86
+ /**
87
+ * Practical-significance floor on absolute deviation. Default 0 (disabled).
88
+ * An anomaly only fires when |value − μ| ≥ this floor — even if the
89
+ * statistical trigger (μ ± Nσ) is exceeded. Use to suppress alerts on
90
+ * statistically-unusual but operationally-irrelevant swings on
91
+ * low-baseline metrics (e.g. 6 ms → 20 ms latency).
92
+ * Same field unit as the metric itself.
93
+ */
94
+ "x-anomaly-min-absolute-delta"?: number;
95
+ /**
96
+ * Practical-significance floor on relative deviation. Default 0 (disabled).
97
+ * An anomaly only fires when |value − μ| / max(|μ|, ε) ≥ this floor — even
98
+ * if the statistical trigger is exceeded. Expressed as a fraction
99
+ * (e.g. 0.5 = 50%). Use to suppress alerts whose proportional change is
100
+ * small on high-magnitude metrics.
101
+ */
102
+ "x-anomaly-min-relative-delta"?: number;
103
+ }
104
+
105
+ /**
106
+ * Metadata for a chartable field with categorical (dominance) anomaly
107
+ * detection. Practical-significance floors are deliberately disallowed
108
+ * because they have no meaning against a categorical baseline — there's
109
+ * no μ to subtract from.
67
110
  */
68
- export interface ChartMetaAnomalyEnabled extends BaseHealthResultMeta {
111
+ export interface ChartMetaAnomalyCategorical extends BaseHealthResultMeta {
69
112
  "x-chart-type": ChartType;
70
113
  "x-anomaly-enabled": true;
71
- "x-anomaly-direction": "higher-is-better" | "lower-is-better" | "deviation" | "dominance";
114
+ "x-anomaly-direction": CategoricalAnomalyDirection;
115
+ "x-anomaly-min-absolute-delta"?: never;
116
+ "x-anomaly-min-relative-delta"?: never;
72
117
  }
73
118
 
119
+ /**
120
+ * Union of the two anomaly-enabled variants. Kept for back-compat with
121
+ * existing callers — new code should narrow against the discriminated
122
+ * union directly via `x-anomaly-direction`.
123
+ */
124
+ export type ChartMetaAnomalyEnabled =
125
+ | ChartMetaAnomalyNumeric
126
+ | ChartMetaAnomalyCategorical;
127
+
74
128
  /**
75
129
  * Metadata for a field that exposes a chart but explicitly disables anomaly detection.
76
130
  */
@@ -78,6 +132,8 @@ export interface ChartMetaAnomalyDisabled extends BaseHealthResultMeta {
78
132
  "x-chart-type": ChartType;
79
133
  "x-anomaly-enabled": false;
80
134
  "x-anomaly-direction"?: never;
135
+ "x-anomaly-min-absolute-delta"?: never;
136
+ "x-anomaly-min-relative-delta"?: never;
81
137
  }
82
138
 
83
139
  /**
@@ -87,6 +143,8 @@ export interface NonChartMeta extends BaseHealthResultMeta {
87
143
  "x-chart-type"?: never;
88
144
  "x-anomaly-enabled"?: never;
89
145
  "x-anomaly-direction"?: never;
146
+ "x-anomaly-min-absolute-delta"?: never;
147
+ "x-anomaly-min-relative-delta"?: never;
90
148
  }
91
149
 
92
150
  /**
@@ -94,8 +152,8 @@ export interface NonChartMeta extends BaseHealthResultMeta {
94
152
  * Provides autocompletion and enforces that ANY field exposing a chart
95
153
  * MUST explicitly define its anomaly behavior.
96
154
  */
97
- export type HealthResultMeta =
98
- | ChartMetaAnomalyEnabled
99
- | ChartMetaAnomalyDisabled
155
+ export type HealthResultMeta =
156
+ | ChartMetaAnomalyNumeric
157
+ | ChartMetaAnomalyCategorical
158
+ | ChartMetaAnomalyDisabled
100
159
  | NonChartMeta;
101
-
@@ -22,19 +22,40 @@ export type TypedContractProcedureBuilder<TMeta extends ProcedureMetadata> =
22
22
  * The return type preserves the operationType in the generic parameter,
23
23
  * enabling type-safe hook selection (useQuery vs useMutation) in usePluginClient.
24
24
  *
25
+ * REST shape (via `/rest/:pluginId/*` mounted with oRPC's OpenAPIHandler):
26
+ * queries default to HTTP `GET` (input encoded into the URL as bracket-notation
27
+ * query params, e.g. `?ids[0]=a&ids[1]=b`); mutations default to `POST` with the
28
+ * input as the JSON body.
29
+ *
30
+ * Override the method by chaining `.route({ method: "..." })` after
31
+ * `proc({...})`. The repo convention is:
32
+ * - `create*` / `add*` → `POST` (default for mutations)
33
+ * - `update*` → `PATCH` (partial updates)
34
+ * - `delete*` / `remove*` → `DELETE`
35
+ * - `getBulk*` and any query with → `POST` (avoid URL-length limits;
36
+ * a large array input no automatic GET→POST fallback
37
+ * in `@orpc/openapi@1.13.x`)
38
+ *
39
+ * Constraint to know about: when method is `GET` (the default for queries),
40
+ * the input schema must be an `object`, `any`, or `unknown` — never a bare
41
+ * scalar like `z.string()`. GET has no field name to attach a top-level scalar
42
+ * to in a query string, so oRPC's OpenAPI generator throws at boot. Wrap the
43
+ * input in an object (`z.object({ id: z.string() })`) or, if the existing call
44
+ * sites can't be migrated, override the method to `POST`.
45
+ *
25
46
  * @example
26
47
  * ```typescript
27
48
  * import { proc } from "@checkstack/common";
28
49
  *
29
50
  * export const myContract = {
30
- * // Returns TypedContractProcedureBuilder<{ operationType: "query", ... }>
51
+ * // GET /rest/myplugin/getItems
31
52
  * getItems: proc({
32
53
  * operationType: "query",
33
54
  * userType: "public",
34
55
  * access: [myAccess.items.read],
35
56
  * }).output(z.array(ItemSchema)),
36
57
  *
37
- * // Returns TypedContractProcedureBuilder<{ operationType: "mutation", ... }>
58
+ * // POST /rest/myplugin/createItem
38
59
  * createItem: proc({
39
60
  * operationType: "mutation",
40
61
  * userType: "authenticated",
@@ -42,11 +63,24 @@ export type TypedContractProcedureBuilder<TMeta extends ProcedureMetadata> =
42
63
  * })
43
64
  * .input(CreateItemSchema)
44
65
  * .output(ItemSchema),
66
+ *
67
+ * // POST /rest/myplugin/getBulkItems — bulk query overrides default GET
68
+ * // because `ids` can be too long for a query string.
69
+ * getBulkItems: proc({
70
+ * operationType: "query",
71
+ * userType: "public",
72
+ * access: [myAccess.items.read],
73
+ * })
74
+ * .route({ method: "POST" })
75
+ * .input(z.object({ ids: z.array(z.string()) })),
45
76
  * };
46
77
  * ```
47
78
  */
48
79
  export function proc<TMeta extends ProcedureMetadata>(
49
80
  meta: TMeta
50
81
  ): TypedContractProcedureBuilder<TMeta> {
51
- return oc.$meta<TMeta>(meta) as TypedContractProcedureBuilder<TMeta>;
82
+ const defaultMethod = meta.operationType === "query" ? "GET" : "POST";
83
+ return oc
84
+ .$meta<TMeta>(meta)
85
+ .route({ method: defaultMethod }) as TypedContractProcedureBuilder<TMeta>;
52
86
  }