@checkstack/common 0.9.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 +106 -0
- package/package.json +2 -2
- package/src/pagination.test.ts +110 -43
- package/src/pagination.ts +34 -34
- package/src/procedure-builder.ts +37 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,111 @@
|
|
|
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
|
+
|
|
57
|
+
## 0.10.0
|
|
58
|
+
|
|
59
|
+
### Minor Changes
|
|
60
|
+
|
|
61
|
+
- 9016526: Add a `/rest/:pluginId/*` HTTP mount that serves every plugin's oRPC contract
|
|
62
|
+
through the REST/OpenAPI shape described by `/api/openapi.json`. Queries are
|
|
63
|
+
`GET` with query parameters, mutations are `POST` with the input as the raw
|
|
64
|
+
JSON body. The existing `/api/:pluginId/*` mount continues to serve oRPC's
|
|
65
|
+
native wire protocol unchanged, so existing clients are not affected.
|
|
66
|
+
|
|
67
|
+
The OpenAPI spec at `/api/openapi.json` now reflects the real mount: every
|
|
68
|
+
`paths` entry is prefixed with `/rest` instead of `/api`.
|
|
69
|
+
|
|
70
|
+
Also fixes a SPA-fallback bug: the backend's `/api-docs` route previously
|
|
71
|
+
returned 404 on production deployments because the static-file middleware
|
|
72
|
+
skipped any path starting with `/api`, capturing `/api-docs` along with real
|
|
73
|
+
API routes. The skip now requires a trailing slash (`/api/`, `/rest/`).
|
|
74
|
+
|
|
75
|
+
Required access rules are now visible in the API Docs UI. The OpenAPI spec
|
|
76
|
+
generator was reading a non-existent `accessRules` field on procedure
|
|
77
|
+
metadata; the real field is `access: AccessRule[]`. Each procedure's access
|
|
78
|
+
rules are now flattened to fully-qualified IDs (e.g. `catalog.system.read`)
|
|
79
|
+
and emitted under `x-orpc-meta.accessRules`, which the existing
|
|
80
|
+
`Required Access Rules` section in the docs UI already knew how to render.
|
|
81
|
+
|
|
82
|
+
The API Docs schema renderer now handles record types (zod `z.record`),
|
|
83
|
+
`$ref`s into `components.schemas`, `oneOf`/`anyOf`/`allOf`, nullable union
|
|
84
|
+
types (`type: ["string", "null"]`), and `format` qualifiers. Previously
|
|
85
|
+
record outputs like `{ statuses: object }` masked the actual value type;
|
|
86
|
+
they now render as `{ [key]: <ResolvedType> { ... } }` with the inner
|
|
87
|
+
schema expanded, capped at 12 levels with cycle detection.
|
|
88
|
+
|
|
89
|
+
**REST method conventions.** `proc()` now defaults to `GET` for queries and
|
|
90
|
+
`POST` for mutations on the `/rest` mount, using bracket-notation query
|
|
91
|
+
params (`?filter[status]=active&ids[0]=a`) for GET inputs. Existing
|
|
92
|
+
procedures were updated to follow REST semantics:
|
|
93
|
+
|
|
94
|
+
- `update*` mutations → `PATCH`
|
|
95
|
+
- `delete*` / `remove*` mutations → `DELETE`
|
|
96
|
+
- `getBulk*` queries and any query taking a large array input → `POST`
|
|
97
|
+
(because `@orpc/openapi@1.13.x` has no GET→POST URL-length fallback)
|
|
98
|
+
|
|
99
|
+
GET endpoints require an `object` input — bare scalars like
|
|
100
|
+
`.input(z.string())` are not valid on GET. `getSystemConfigurations` was
|
|
101
|
+
refactored from `.input(z.string())` to `.input(z.object({ systemId: ... }))`
|
|
102
|
+
to fit the GET shape; the only call-site update was the in-process router
|
|
103
|
+
unpacking `input.systemId` instead of passing `input` directly.
|
|
104
|
+
|
|
105
|
+
The API Docs UI now renders query parameters (path/query/header/cookie) in a
|
|
106
|
+
dedicated table for GET endpoints, and the fetch example shows them in the
|
|
107
|
+
URL with `<required>` / `<optional>` placeholders.
|
|
108
|
+
|
|
3
109
|
## 0.9.0
|
|
4
110
|
|
|
5
111
|
### 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
|
-
};
|
package/src/procedure-builder.ts
CHANGED
|
@@ -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
|
-
* //
|
|
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
|
-
* //
|
|
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
|
-
|
|
82
|
+
const defaultMethod = meta.operationType === "query" ? "GET" : "POST";
|
|
83
|
+
return oc
|
|
84
|
+
.$meta<TMeta>(meta)
|
|
85
|
+
.route({ method: defaultMethod }) as TypedContractProcedureBuilder<TMeta>;
|
|
52
86
|
}
|