@checkstack/common 0.10.0 → 0.11.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 +54 -0
- package/package.json +2 -2
- package/src/pagination.test.ts +110 -43
- package/src/pagination.ts +34 -34
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,59 @@
|
|
|
1
1
|
# @checkstack/common
|
|
2
2
|
|
|
3
|
+
## 0.11.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- f23f3c9: Add the canonical `PaginationInput` zod schema and `PaginatedResult`
|
|
8
|
+
output factory in `@checkstack/common`. `PaginationInput` is an
|
|
9
|
+
integer-clamped `{ limit: 1-100 (default 20), offset: >= 0 (default 0) }`
|
|
10
|
+
shape that composes with `.extend({...})` for domain-specific filters
|
|
11
|
+
(e.g. `unreadOnly` on notifications). `PaginatedResult(itemSchema)`
|
|
12
|
+
returns the standard `{ items, total, limit, offset }` envelope. The
|
|
13
|
+
existing `PaginationInputSchema` / `paginatedOutput` / `PaginatedResponse`
|
|
14
|
+
exports are now marked `@deprecated` and will be removed once the
|
|
15
|
+
follow-up sweep migrates every `*-common` consumer to the canonical
|
|
16
|
+
contract.
|
|
17
|
+
- f23f3c9: Sweep every paginated `*-common` contract onto the canonical
|
|
18
|
+
`PaginationInput` / `PaginatedResult` from `@checkstack/common` and
|
|
19
|
+
remove the now-unused legacy exports.
|
|
20
|
+
|
|
21
|
+
**BREAKING CHANGE** - `@checkstack/common` drops the deprecated
|
|
22
|
+
`PaginationInputSchema`, `paginatedOutput`, and `PaginatedResponse`
|
|
23
|
+
symbols. Callers must consume `PaginationInput` (input) and
|
|
24
|
+
`PaginatedResult(itemSchema)` (output) instead. The canonical input is
|
|
25
|
+
`{ limit (1-100, default 20), offset (>= 0, default 0) }`; the
|
|
26
|
+
canonical output envelope is
|
|
27
|
+
`{ items, total, limit, offset }`.
|
|
28
|
+
|
|
29
|
+
**BREAKING CHANGE** - `@checkstack/notification-common` migrates
|
|
30
|
+
`getNotifications` off the legacy `PaginationInputSchema`
|
|
31
|
+
(`{ limit, offset, unreadOnly }` with output `{ notifications, total }`)
|
|
32
|
+
onto `ListNotificationsInputSchema =
|
|
33
|
+
PaginationInput.extend({ unreadOnly })` and
|
|
34
|
+
`PaginatedResult(NotificationSchema)`. The output key changes from
|
|
35
|
+
`notifications` to `items`, and `limit` / `offset` are now echoed on
|
|
36
|
+
the response. The `PaginationInput` type alias previously exported
|
|
37
|
+
from `notification-common` is removed - use `ListNotificationsInput`
|
|
38
|
+
or the canonical `PaginationInput` from `@checkstack/common`.
|
|
39
|
+
|
|
40
|
+
**BREAKING CHANGE** - `@checkstack/integration-common` migrates
|
|
41
|
+
`listSubscriptions` (inline `{ page, pageSize, ... }` -> output
|
|
42
|
+
`{ subscriptions, total }`) and `getDeliveryLogs` (via
|
|
43
|
+
`DeliveryLogQueryInputSchema` `{ subscriptionId?, eventType?, status?,
|
|
44
|
+
page, pageSize }` -> output `{ logs, total }`) onto the canonical
|
|
45
|
+
`PaginationInput.extend({...})` input and
|
|
46
|
+
`PaginatedResult(itemSchema)` output. External callers must switch
|
|
47
|
+
from `{ page, pageSize }` to `{ limit, offset }` and read response
|
|
48
|
+
items from `data.items` (no more `data.subscriptions` / `data.logs`).
|
|
49
|
+
|
|
50
|
+
The matching `*-backend` handlers were updated to consume the new
|
|
51
|
+
input shape (`offset` arithmetic in lieu of `(page - 1) * pageSize`)
|
|
52
|
+
and to echo `limit` / `offset` on the response. The `*-frontend` call
|
|
53
|
+
sites in `NotificationsPage`, `NotificationBell`, `IntegrationsPage`,
|
|
54
|
+
and `DeliveryLogsPage` were updated to send the new input shape and
|
|
55
|
+
read `data.items`.
|
|
56
|
+
|
|
3
57
|
## 0.10.0
|
|
4
58
|
|
|
5
59
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@checkstack/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"license": "Elastic-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"typescript": "^5.7.2",
|
|
22
22
|
"@checkstack/tsconfig": "0.0.7",
|
|
23
|
-
"@checkstack/scripts": "0.3.
|
|
23
|
+
"@checkstack/scripts": "0.3.2"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"typecheck": "tsgo -b",
|
package/src/pagination.test.ts
CHANGED
|
@@ -1,89 +1,156 @@
|
|
|
1
1
|
import { describe, it, expect } from "bun:test";
|
|
2
|
-
import { PaginationInputSchema, paginatedOutput } from "./pagination";
|
|
3
2
|
import { z } from "zod";
|
|
3
|
+
import { PaginationInput, PaginatedResult } from "./pagination";
|
|
4
4
|
|
|
5
|
-
describe("
|
|
6
|
-
it("
|
|
7
|
-
const result =
|
|
8
|
-
limit: 20,
|
|
9
|
-
offset: 40,
|
|
10
|
-
});
|
|
11
|
-
|
|
5
|
+
describe("PaginationInput (canonical)", () => {
|
|
6
|
+
it("applies default limit and offset when omitted", () => {
|
|
7
|
+
const result = PaginationInput.parse({});
|
|
12
8
|
expect(result.limit).toBe(20);
|
|
13
|
-
expect(result.offset).toBe(
|
|
9
|
+
expect(result.offset).toBe(0);
|
|
14
10
|
});
|
|
15
11
|
|
|
16
|
-
it("
|
|
17
|
-
const result =
|
|
12
|
+
it("accepts valid limit and offset values", () => {
|
|
13
|
+
const result = PaginationInput.parse({ limit: 50, offset: 100 });
|
|
14
|
+
expect(result.limit).toBe(50);
|
|
15
|
+
expect(result.offset).toBe(100);
|
|
16
|
+
});
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
expect(
|
|
18
|
+
it("rejects limit below 1", () => {
|
|
19
|
+
expect(() => PaginationInput.parse({ limit: 0 })).toThrow();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("rejects limit above 100", () => {
|
|
23
|
+
expect(() => PaginationInput.parse({ limit: 101 })).toThrow();
|
|
21
24
|
});
|
|
22
25
|
|
|
23
|
-
it("
|
|
24
|
-
expect(() =>
|
|
26
|
+
it("rejects negative offset", () => {
|
|
27
|
+
expect(() => PaginationInput.parse({ offset: -1 })).toThrow();
|
|
25
28
|
});
|
|
26
29
|
|
|
27
|
-
it("
|
|
28
|
-
expect(() =>
|
|
30
|
+
it("rejects non-integer limit values", () => {
|
|
31
|
+
expect(() => PaginationInput.parse({ limit: 10.5 })).toThrow();
|
|
29
32
|
});
|
|
30
33
|
|
|
31
|
-
it("
|
|
32
|
-
expect(() =>
|
|
34
|
+
it("rejects non-integer offset values", () => {
|
|
35
|
+
expect(() => PaginationInput.parse({ offset: 1.5 })).toThrow();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("supports `.extend({...})` for domain extras", () => {
|
|
39
|
+
const ListNotificationsInput = PaginationInput.extend({
|
|
40
|
+
unreadOnly: z.boolean().default(false),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const parsed = ListNotificationsInput.parse({});
|
|
44
|
+
expect(parsed.limit).toBe(20);
|
|
45
|
+
expect(parsed.offset).toBe(0);
|
|
46
|
+
expect(parsed.unreadOnly).toBe(false);
|
|
47
|
+
|
|
48
|
+
const parsedWithExtras = ListNotificationsInput.parse({
|
|
49
|
+
limit: 5,
|
|
50
|
+
unreadOnly: true,
|
|
51
|
+
});
|
|
52
|
+
expect(parsedWithExtras.limit).toBe(5);
|
|
53
|
+
expect(parsedWithExtras.unreadOnly).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("infers a typed PaginationInput", () => {
|
|
57
|
+
const sample: PaginationInput = { limit: 10, offset: 0 };
|
|
58
|
+
expect(sample.limit).toBe(10);
|
|
59
|
+
expect(sample.offset).toBe(0);
|
|
33
60
|
});
|
|
34
61
|
});
|
|
35
62
|
|
|
36
|
-
describe("
|
|
63
|
+
describe("PaginatedResult (canonical)", () => {
|
|
37
64
|
const ItemSchema = z.object({
|
|
38
65
|
id: z.string(),
|
|
39
66
|
name: z.string(),
|
|
40
67
|
});
|
|
41
68
|
|
|
42
|
-
it("
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
const result = outputSchema.parse({
|
|
69
|
+
it("produces an { items, total, limit, offset } shape", () => {
|
|
70
|
+
const schema = PaginatedResult(ItemSchema);
|
|
71
|
+
const result = schema.parse({
|
|
46
72
|
items: [
|
|
47
|
-
{ id: "1", name: "
|
|
48
|
-
{ id: "2", name: "
|
|
73
|
+
{ id: "1", name: "First" },
|
|
74
|
+
{ id: "2", name: "Second" },
|
|
49
75
|
],
|
|
50
|
-
total:
|
|
76
|
+
total: 42,
|
|
77
|
+
limit: 20,
|
|
78
|
+
offset: 0,
|
|
51
79
|
});
|
|
52
80
|
|
|
53
81
|
expect(result.items).toHaveLength(2);
|
|
54
|
-
expect(result.total).toBe(
|
|
82
|
+
expect(result.total).toBe(42);
|
|
83
|
+
expect(result.limit).toBe(20);
|
|
84
|
+
expect(result.offset).toBe(0);
|
|
55
85
|
});
|
|
56
86
|
|
|
57
|
-
it("
|
|
58
|
-
const
|
|
59
|
-
|
|
87
|
+
it("rejects items that do not match the inner schema", () => {
|
|
88
|
+
const schema = PaginatedResult(ItemSchema);
|
|
60
89
|
expect(() =>
|
|
61
|
-
|
|
62
|
-
items: [{ id: "1" }],
|
|
90
|
+
schema.parse({
|
|
91
|
+
items: [{ id: "1" }],
|
|
63
92
|
total: 1,
|
|
64
|
-
|
|
93
|
+
limit: 20,
|
|
94
|
+
offset: 0,
|
|
95
|
+
}),
|
|
65
96
|
).toThrow();
|
|
66
97
|
});
|
|
67
98
|
|
|
68
|
-
it("
|
|
69
|
-
const
|
|
99
|
+
it("rejects a missing total", () => {
|
|
100
|
+
const schema = PaginatedResult(ItemSchema);
|
|
101
|
+
expect(() =>
|
|
102
|
+
schema.parse({
|
|
103
|
+
items: [],
|
|
104
|
+
limit: 20,
|
|
105
|
+
offset: 0,
|
|
106
|
+
}),
|
|
107
|
+
).toThrow();
|
|
108
|
+
});
|
|
70
109
|
|
|
110
|
+
it("rejects a missing limit/offset", () => {
|
|
111
|
+
const schema = PaginatedResult(ItemSchema);
|
|
71
112
|
expect(() =>
|
|
72
|
-
|
|
73
|
-
items: [
|
|
74
|
-
|
|
113
|
+
schema.parse({
|
|
114
|
+
items: [],
|
|
115
|
+
total: 0,
|
|
116
|
+
}),
|
|
75
117
|
).toThrow();
|
|
76
118
|
});
|
|
77
119
|
|
|
78
|
-
it("
|
|
79
|
-
const
|
|
120
|
+
it("rejects a negative total", () => {
|
|
121
|
+
const schema = PaginatedResult(ItemSchema);
|
|
122
|
+
expect(() =>
|
|
123
|
+
schema.parse({
|
|
124
|
+
items: [],
|
|
125
|
+
total: -1,
|
|
126
|
+
limit: 20,
|
|
127
|
+
offset: 0,
|
|
128
|
+
}),
|
|
129
|
+
).toThrow();
|
|
130
|
+
});
|
|
80
131
|
|
|
81
|
-
|
|
132
|
+
it("accepts an empty items array", () => {
|
|
133
|
+
const schema = PaginatedResult(ItemSchema);
|
|
134
|
+
const result = schema.parse({
|
|
82
135
|
items: [],
|
|
83
136
|
total: 0,
|
|
137
|
+
limit: 20,
|
|
138
|
+
offset: 0,
|
|
84
139
|
});
|
|
85
|
-
|
|
86
140
|
expect(result.items).toEqual([]);
|
|
87
141
|
expect(result.total).toBe(0);
|
|
88
142
|
});
|
|
143
|
+
|
|
144
|
+
it("infers a typed result via z.infer", () => {
|
|
145
|
+
const schema = PaginatedResult(ItemSchema);
|
|
146
|
+
type Result = z.infer<typeof schema>;
|
|
147
|
+
const value: Result = {
|
|
148
|
+
items: [{ id: "x", name: "X" }],
|
|
149
|
+
total: 1,
|
|
150
|
+
limit: 20,
|
|
151
|
+
offset: 0,
|
|
152
|
+
};
|
|
153
|
+
expect(value.items[0]?.id).toBe("x");
|
|
154
|
+
});
|
|
89
155
|
});
|
|
156
|
+
|
package/src/pagination.ts
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Canonical pagination input schema for RPC procedures.
|
|
5
|
+
*
|
|
6
|
+
* This is the single source of truth for paginated list inputs across
|
|
7
|
+
* the platform. Domain-specific extras (e.g. `unreadOnly` on
|
|
8
|
+
* notifications) must compose with `.extend({...})` — do NOT redefine
|
|
9
|
+
* the base fields.
|
|
6
10
|
*
|
|
7
11
|
* @example
|
|
8
12
|
* ```typescript
|
|
9
|
-
*
|
|
10
|
-
*
|
|
13
|
+
* import { PaginationInput, PaginatedResult } from "@checkstack/common";
|
|
14
|
+
*
|
|
15
|
+
* const ListNotificationsInput = PaginationInput.extend({
|
|
16
|
+
* unreadOnly: z.boolean().default(false),
|
|
17
|
+
* });
|
|
11
18
|
*
|
|
12
|
-
* const
|
|
13
|
-
* getItems: _base
|
|
14
|
-
* .input(PaginationInputSchema.extend({ search: z.string().optional() }))
|
|
15
|
-
* .output(paginatedOutput(ItemSchema)),
|
|
16
|
-
* };
|
|
19
|
+
* const ListNotificationsOutput = PaginatedResult(NotificationSchema);
|
|
17
20
|
* ```
|
|
18
21
|
*/
|
|
19
|
-
export const
|
|
20
|
-
/** Number of items per page (1-100) */
|
|
21
|
-
limit: z.number().min(1).max(100).default(
|
|
22
|
-
/** Number of items to skip */
|
|
23
|
-
offset: z.number().min(0).default(0),
|
|
22
|
+
export const PaginationInput = z.object({
|
|
23
|
+
/** Number of items per page (1-100). */
|
|
24
|
+
limit: z.number().int().min(1).max(100).default(20),
|
|
25
|
+
/** Number of items to skip from the start of the result set. */
|
|
26
|
+
offset: z.number().int().min(0).default(0),
|
|
24
27
|
});
|
|
25
28
|
|
|
26
|
-
export type PaginationInput = z.infer<typeof
|
|
29
|
+
export type PaginationInput = z.infer<typeof PaginationInput>;
|
|
27
30
|
|
|
28
31
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
32
|
+
* Canonical paginated response factory.
|
|
33
|
+
*
|
|
34
|
+
* Wraps an item schema in the standard `{ items, total, limit, offset }`
|
|
35
|
+
* shape. The echoed `limit` / `offset` mirror what the server actually
|
|
36
|
+
* applied so clients can render correct pagination controls even when
|
|
37
|
+
* defaults were used.
|
|
31
38
|
*
|
|
32
39
|
* @example
|
|
33
40
|
* ```typescript
|
|
34
|
-
* //
|
|
35
|
-
*
|
|
36
|
-
* .input(
|
|
37
|
-
* .output(
|
|
41
|
+
* // In a contract:
|
|
42
|
+
* getNotifications: _base
|
|
43
|
+
* .input(PaginationInput)
|
|
44
|
+
* .output(PaginatedResult(NotificationSchema)),
|
|
38
45
|
*
|
|
39
|
-
* // Returns: { items:
|
|
46
|
+
* // Returns: { items: Notification[], total: number, limit: number, offset: number }
|
|
40
47
|
* ```
|
|
41
48
|
*/
|
|
42
|
-
export
|
|
43
|
-
|
|
49
|
+
export const PaginatedResult = <T extends z.ZodTypeAny>(itemSchema: T) =>
|
|
50
|
+
z.object({
|
|
44
51
|
items: z.array(itemSchema),
|
|
45
|
-
total: z.number(),
|
|
52
|
+
total: z.number().int().min(0),
|
|
53
|
+
limit: z.number().int(),
|
|
54
|
+
offset: z.number().int(),
|
|
46
55
|
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Type helper for paginated responses
|
|
51
|
-
*/
|
|
52
|
-
export type PaginatedResponse<T> = {
|
|
53
|
-
items: T[];
|
|
54
|
-
total: number;
|
|
55
|
-
};
|