@cogover/sdk 0.12.0 → 0.12.2
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/README.md +12 -9
- package/dist/crypto.js +182 -69
- package/dist/data.js +4 -1
- package/dist/email.js +5 -3
- package/dist/internal/base64.js +92 -28
- package/dist/internal/bridge.js +41 -3
- package/dist/internal/job-config.js +7 -1
- package/dist/internal/utf8.js +18 -53
- package/dist/locks.js +13 -3
- package/dist/logger.js +47 -1
- package/dist/org.js +14 -14
- package/dist/records.d.ts +13 -0
- package/dist/response.js +21 -1
- package/dist/schema.d.ts +1 -0
- package/dist/script.js +7 -3
- package/docs/en/api-reference/background-jobs.md +288 -0
- package/docs/en/api-reference/cryptography.md +321 -0
- package/docs/en/api-reference/fetch-and-secrets.md +184 -0
- package/docs/en/api-reference/inbound-webhooks.md +65 -0
- package/docs/en/api-reference/notifications-and-email.md +317 -0
- package/docs/en/api-reference/organization-structure.md +304 -0
- package/docs/en/api-reference/overview.md +198 -0
- package/docs/en/api-reference/push-messages.md +115 -0
- package/docs/en/api-reference/record-triggers.md +469 -0
- package/docs/en/api-reference/records.md +447 -0
- package/docs/en/api-reference/scripts-and-routes.md +354 -0
- package/docs/en/api-reference/state-and-locks.md +162 -0
- package/docs/en/usage-guide.md +17 -14
- package/package.json +1 -1
- package/docs/en/api-reference.md +0 -2944
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# `@cogover/sdk` API reference: Organization structure
|
|
2
|
+
|
|
3
|
+
This page is part of the [`@cogover/sdk` API reference](overview.md).
|
|
4
|
+
|
|
5
|
+
## Organization structure
|
|
6
|
+
|
|
7
|
+
`org` reads the departments, positions and personnel of the workspace: who works in
|
|
8
|
+
which department, which positions apply where, and who manages whom. Every method is
|
|
9
|
+
a read. Structure answers are served from a cached copy of the organization, so they
|
|
10
|
+
are fast; a change made in Cogover is visible within a few seconds. Add
|
|
11
|
+
`withDisplay: true` to also receive department and position names in a language and,
|
|
12
|
+
for personnel, their display name, email, avatar and code. Other personnel fields,
|
|
13
|
+
such as phone numbers or custom fields, are read with `data.object("personnel")`,
|
|
14
|
+
which applies record permissions.
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
interface OrgApi {
|
|
18
|
+
readonly personnel: OrgPersonnelApi;
|
|
19
|
+
readonly departments: OrgDepartmentsApi;
|
|
20
|
+
readonly positions: OrgPositionsApi;
|
|
21
|
+
me(options?: OrgReadOptions): Promise<OrgPersonnel | null>;
|
|
22
|
+
isInDepartment(personnelId: string, departmentId: string,
|
|
23
|
+
options?: OrgIsInDepartmentOptions): Promise<boolean>;
|
|
24
|
+
isManagerOf(managerId: string, personnelId: string,
|
|
25
|
+
options?: OrgIsManagerOfOptions): Promise<boolean>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface OrgPersonnelApi {
|
|
29
|
+
get(id: string, options?: OrgReadOptions): Promise<OrgPersonnel | null>;
|
|
30
|
+
getMany(ids: readonly string[], options?: OrgReadOptions):
|
|
31
|
+
Promise<OrgGetManyResult<OrgPersonnel>>;
|
|
32
|
+
managerChain(id: string, options?: OrgPersonnelChainOptions):
|
|
33
|
+
Promise<readonly OrgManagerChain[]>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface OrgDepartmentsApi {
|
|
37
|
+
get(id: string, options?: OrgReadOptions): Promise<OrgDepartment | null>;
|
|
38
|
+
tree(options?: OrgTreeOptions): Promise<readonly OrgDepartmentNode[]>;
|
|
39
|
+
ancestors(id: string, options?: OrgReadOptions): Promise<readonly OrgDepartment[]>;
|
|
40
|
+
members(id: string, options?: OrgDepartmentMembersOptions): Promise<OrgPage<OrgMember>>;
|
|
41
|
+
managers(id: string, options?: OrgManagerOptions): Promise<readonly OrgManagerTier[]>;
|
|
42
|
+
managerChain(id: string, options?: OrgManagerOptions): Promise<OrgManagerChain>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface OrgPositionsApi {
|
|
46
|
+
list(options?: OrgReadOptions): Promise<readonly OrgPosition[]>;
|
|
47
|
+
get(id: string, options?: OrgReadOptions): Promise<OrgPosition | null>;
|
|
48
|
+
members(id: string, options?: OrgPositionMembersOptions): Promise<OrgPage<OrgMember>>;
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Every method that returns data has a second overload: when `options.withDisplay` is
|
|
53
|
+
the literal `true`, the result type is the `<true>` variant of the types below, whose
|
|
54
|
+
display fields are always present. Without it, the display fields do not exist on the
|
|
55
|
+
type.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
interface OrgReadOptions {
|
|
59
|
+
readonly withDisplay?: boolean;
|
|
60
|
+
readonly language?: string; // such as "vi-VN"; defaults to the invocation language
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface OrgPageOptions extends OrgReadOptions {
|
|
64
|
+
readonly includeSubDepartments?: boolean;
|
|
65
|
+
readonly accountOnly?: boolean;
|
|
66
|
+
readonly limit?: number; // default 500, at most 2000; 200 with withDisplay
|
|
67
|
+
readonly cursor?: string;
|
|
68
|
+
}
|
|
69
|
+
interface OrgDepartmentMembersOptions extends OrgPageOptions { readonly positionId?: string; }
|
|
70
|
+
interface OrgPositionMembersOptions extends OrgPageOptions { readonly departmentId?: string; }
|
|
71
|
+
interface OrgManagerOptions extends OrgReadOptions { readonly accountOnly?: boolean; }
|
|
72
|
+
interface OrgPersonnelChainOptions extends OrgManagerOptions { readonly departmentId?: string; }
|
|
73
|
+
interface OrgTreeOptions extends OrgReadOptions {
|
|
74
|
+
readonly rootId?: string;
|
|
75
|
+
readonly depth?: number; // 1 to 100; defaults to every level
|
|
76
|
+
}
|
|
77
|
+
interface OrgIsInDepartmentOptions { readonly includeSubDepartments?: boolean; }
|
|
78
|
+
interface OrgIsManagerOfOptions {
|
|
79
|
+
readonly departmentId?: string;
|
|
80
|
+
readonly directOnly?: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface OrgPersonnelDisplay {
|
|
84
|
+
readonly name: string;
|
|
85
|
+
readonly email: string | null;
|
|
86
|
+
readonly avatar: string | null; // public URL
|
|
87
|
+
readonly code: string | null;
|
|
88
|
+
}
|
|
89
|
+
interface OrgNameDisplay { readonly name: string; }
|
|
90
|
+
|
|
91
|
+
type OrgMembership<D extends boolean = false> = {
|
|
92
|
+
readonly personnelId: string;
|
|
93
|
+
readonly departmentId: string;
|
|
94
|
+
readonly positionId: string | null;
|
|
95
|
+
readonly level: number; // 0 staff, 1 highest manager level, 2 next, ...
|
|
96
|
+
readonly primary: boolean;
|
|
97
|
+
} & (D extends true
|
|
98
|
+
? { readonly departmentName: string | null; readonly positionName: string | null }
|
|
99
|
+
: {});
|
|
100
|
+
|
|
101
|
+
type OrgPersonnel<D extends boolean = false> = {
|
|
102
|
+
readonly id: string;
|
|
103
|
+
readonly accountId: string | null; // null: no workspace account
|
|
104
|
+
readonly memberships: readonly OrgMembership<D>[];
|
|
105
|
+
} & (D extends true ? { readonly display: OrgPersonnelDisplay | null } : {});
|
|
106
|
+
|
|
107
|
+
type OrgMember<D extends boolean = false> = OrgMembership<D> & {
|
|
108
|
+
readonly accountId: string | null;
|
|
109
|
+
} & (D extends true ? { readonly display: OrgPersonnelDisplay | null } : {});
|
|
110
|
+
|
|
111
|
+
type OrgDepartment<D extends boolean = false> = {
|
|
112
|
+
readonly id: string;
|
|
113
|
+
readonly parentId: string | null;
|
|
114
|
+
readonly level: number; // 1 for a root department
|
|
115
|
+
readonly childIds: readonly string[];
|
|
116
|
+
readonly positionIds: readonly string[];
|
|
117
|
+
} & (D extends true ? { readonly display: OrgNameDisplay | null } : {});
|
|
118
|
+
|
|
119
|
+
type OrgDepartmentNode<D extends boolean = false> = OrgDepartment<D> & {
|
|
120
|
+
readonly children: readonly OrgDepartmentNode<D>[];
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
type OrgPosition<D extends boolean = false> = {
|
|
124
|
+
readonly id: string;
|
|
125
|
+
readonly departmentOnly: boolean;
|
|
126
|
+
readonly departmentIds: readonly string[];
|
|
127
|
+
readonly excludedDepartmentIds: readonly string[];
|
|
128
|
+
} & (D extends true ? { readonly display: OrgNameDisplay | null } : {});
|
|
129
|
+
|
|
130
|
+
type OrgManagerTier<D extends boolean = false> = {
|
|
131
|
+
readonly departmentId: string;
|
|
132
|
+
readonly level: number;
|
|
133
|
+
readonly distance: number; // 0 starting department, 1 its parent, ...
|
|
134
|
+
readonly personnelIds: readonly string[];
|
|
135
|
+
} & (D extends true ? {
|
|
136
|
+
readonly departmentName: string | null;
|
|
137
|
+
readonly personnel: readonly { readonly id: string; readonly display: OrgPersonnelDisplay | null }[];
|
|
138
|
+
} : {});
|
|
139
|
+
|
|
140
|
+
interface OrgManagerChain<D extends boolean = false> {
|
|
141
|
+
readonly fromDepartmentId: string;
|
|
142
|
+
readonly personnelLevel: number;
|
|
143
|
+
readonly tiers: readonly OrgManagerTier<D>[];
|
|
144
|
+
readonly vacantDepartmentIds: readonly string[];
|
|
145
|
+
readonly truncated: boolean;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface OrgPage<T> {
|
|
149
|
+
readonly items: readonly T[];
|
|
150
|
+
readonly total: number;
|
|
151
|
+
readonly nextCursor?: string;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
interface OrgGetManyResult<T> {
|
|
155
|
+
readonly items: readonly T[];
|
|
156
|
+
readonly missingIds: readonly string[];
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
export default defineScript(async ({ org, input }) => {
|
|
162
|
+
const order = input as { ownerId: string; departmentId: string };
|
|
163
|
+
// Structure only: answered without reading records.
|
|
164
|
+
const [chain] = await org.personnel.managerChain(order.ownerId, {
|
|
165
|
+
departmentId: order.departmentId,
|
|
166
|
+
accountOnly: true,
|
|
167
|
+
});
|
|
168
|
+
const approverIds = chain?.tiers[0]?.personnelIds ?? [];
|
|
169
|
+
|
|
170
|
+
// Names for a screen: one extra read per kind of data.
|
|
171
|
+
const me = await org.me({ withDisplay: true });
|
|
172
|
+
return {
|
|
173
|
+
approverIds,
|
|
174
|
+
greeting: me?.display?.name,
|
|
175
|
+
departments: me?.memberships.map(m => m.departmentName) ?? [],
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Display data
|
|
181
|
+
|
|
182
|
+
- `withDisplay` adds `display` to personnel, departments and positions, and
|
|
183
|
+
`departmentName`/`positionName` to memberships. A value is `null` when the record
|
|
184
|
+
behind it cannot be read, for example because it was deleted a moment ago.
|
|
185
|
+
- `language` selects the translation of department and position names, such as
|
|
186
|
+
`"vi-VN"` or `"en-US"`; a tag without a region (`"vi"`) matches the first
|
|
187
|
+
translation of that language. It defaults to the language of the invocation's user
|
|
188
|
+
membership, then of the user, then of the workspace, and falls back to the stored
|
|
189
|
+
name when there is no matching translation.
|
|
190
|
+
- A personnel `name` is the full name the workspace shows; `email` is the account
|
|
191
|
+
email, else the work email; `avatar` is a public image URL.
|
|
192
|
+
- Display data is limited to these fields and is readable by every member of the
|
|
193
|
+
workspace, as in user pickers. It costs at most one read per kind of data and per
|
|
194
|
+
200 IDs, and recently read values are cached.
|
|
195
|
+
|
|
196
|
+
### `org.me(options?)`
|
|
197
|
+
|
|
198
|
+
Returns the personnel of the invocation's user, or `null` for a `system` or
|
|
199
|
+
`inbound` invocation. It is `org.personnel.get` with
|
|
200
|
+
`invocation.user.membership.personnelId`.
|
|
201
|
+
|
|
202
|
+
### `org.personnel.get(id, options?)` and `org.personnel.getMany(ids, options?)`
|
|
203
|
+
|
|
204
|
+
`get` returns an active personnel with every department membership, primary
|
|
205
|
+
department first, or `null` when the ID is not an active personnel of the workspace.
|
|
206
|
+
`getMany` reads up to 200 IDs in one call: duplicates are removed, `items` follows
|
|
207
|
+
the request order, and unknown IDs are listed in `missingIds`. A personnel without a
|
|
208
|
+
workspace account has `accountId: null`; people who left the workspace are not
|
|
209
|
+
returned.
|
|
210
|
+
|
|
211
|
+
### `org.departments.get(id, options?)`, `tree(options?)` and `ancestors(id, options?)`
|
|
212
|
+
|
|
213
|
+
- `get` returns one active department or `null`. `childIds` are in display order;
|
|
214
|
+
`positionIds` are the positions that apply to the department.
|
|
215
|
+
- `tree` returns the root departments (or `rootId` alone) with their sub-departments.
|
|
216
|
+
`depth: 1` returns only the roots; below the requested depth `children` is empty
|
|
217
|
+
while `childIds` still lists the sub-departments. A tree of more than 5000
|
|
218
|
+
departments throws `ValidationError`; narrow it with `rootId` or `depth`.
|
|
219
|
+
- `ancestors` returns the parent first and the root last.
|
|
220
|
+
- A department whose parent is inactive or deleted is returned as a root
|
|
221
|
+
(`parentId: null`).
|
|
222
|
+
|
|
223
|
+
### `org.departments.members(id, options?)` and `org.positions.members(id, options?)`
|
|
224
|
+
|
|
225
|
+
Return one page of memberships: managers first by level, then staff. A personnel who
|
|
226
|
+
belongs to several listed departments appears once per department.
|
|
227
|
+
`includeSubDepartments` adds every sub-department in tree order, `positionId` keeps
|
|
228
|
+
only the holders of a position, `departmentId` limits position holders to a
|
|
229
|
+
department, and `accountOnly` keeps only personnel with a workspace account. Pass
|
|
230
|
+
`nextCursor` as `cursor` to read the next page; `total` counts every page. The page
|
|
231
|
+
size is 500 by default and at most 2000, or at most 200 with `withDisplay`.
|
|
232
|
+
|
|
233
|
+
### `org.positions.list(options?)` and `org.positions.get(id, options?)`
|
|
234
|
+
|
|
235
|
+
Return active positions, oldest first. A department-only position applies to
|
|
236
|
+
`departmentIds`; any other position applies to every department except
|
|
237
|
+
`excludedDepartmentIds`.
|
|
238
|
+
|
|
239
|
+
### Managers and manager chains
|
|
240
|
+
|
|
241
|
+
A membership's `level` marks managers: `0` is staff, `1` the highest manager level of
|
|
242
|
+
the department, `2` the next one, and so on. Several people may share a level.
|
|
243
|
+
|
|
244
|
+
- `org.departments.managers(id, options?)` returns the managers of that department
|
|
245
|
+
only, one tier per level, level `1` first.
|
|
246
|
+
- `org.departments.managerChain(id, options?)` returns the managers above a staff
|
|
247
|
+
member of the department, nearest first, up to the root department.
|
|
248
|
+
- `org.personnel.managerChain(id, options?)` returns one chain per department of the
|
|
249
|
+
personnel, primary department first; `departmentId` keeps only the chain that
|
|
250
|
+
starts in that department. The result is empty for an unknown personnel.
|
|
251
|
+
|
|
252
|
+
A chain is built with these rules:
|
|
253
|
+
|
|
254
|
+
1. In the starting department, the managers of the personnel are those with a lower
|
|
255
|
+
level number than the personnel's own level (every manager for staff), nearest
|
|
256
|
+
first: level `3`, then `2`, then `1`. A department head has none there.
|
|
257
|
+
2. Then every parent department up to the root adds all its managers, largest level
|
|
258
|
+
number first. Every manager of a parent department ranks above every manager of
|
|
259
|
+
its sub-departments.
|
|
260
|
+
3. A personnel is listed once, at the nearest tier, and the starting personnel is
|
|
261
|
+
never listed.
|
|
262
|
+
4. `accountOnly: true` skips personnel without a workspace account, for example when
|
|
263
|
+
the chain lists approvers.
|
|
264
|
+
5. `vacantDepartmentIds` lists the departments passed without any eligible manager;
|
|
265
|
+
the chain continues with their parent. A department head's own department is not
|
|
266
|
+
vacant.
|
|
267
|
+
6. `truncated` is `true` when a parent department is inactive, so the chain stops
|
|
268
|
+
before the real root.
|
|
269
|
+
|
|
270
|
+
For example, with a company board (CEO level `1`, deputy CEO level `2`), an
|
|
271
|
+
Engineering department under it (director level `1`), and a Testing department under
|
|
272
|
+
Engineering (head `1`, deputy `2`, team lead `3`), the chain of a tester is: team lead,
|
|
273
|
+
deputy, head, director, deputy CEO, CEO. The chain of the Testing deputy is: head,
|
|
274
|
+
director, deputy CEO, CEO.
|
|
275
|
+
|
|
276
|
+
### `org.isInDepartment(personnelId, departmentId, options?)` and `org.isManagerOf(managerId, personnelId, options?)`
|
|
277
|
+
|
|
278
|
+
`isInDepartment` tells whether the personnel belongs to the department, or to one of
|
|
279
|
+
its sub-departments with `includeSubDepartments`. `isManagerOf` tells whether
|
|
280
|
+
`managerId` appears in a manager chain of `personnelId`; `departmentId` considers
|
|
281
|
+
only the chain that starts there and `directOnly` only its nearest tier. Both return
|
|
282
|
+
`false` for unknown IDs.
|
|
283
|
+
|
|
284
|
+
### Rules
|
|
285
|
+
|
|
286
|
+
- `org` is available in scripts, routes, record triggers of both timings, background
|
|
287
|
+
jobs and Development Sessions.
|
|
288
|
+
- It follows the default identity of the execution: an execution with a user or a
|
|
289
|
+
system identity may read it. An execution without a user (a background job, an
|
|
290
|
+
inbound webhook, or a record trigger started by a system write) may read it only
|
|
291
|
+
when the project's approved identity policy sets `allowInternalSystem: true`;
|
|
292
|
+
otherwise every call except `org.me()` throws `PermissionDeniedError` with
|
|
293
|
+
`details.reason` `"IDENTITY_NOT_GRANTED"`. `org.me()` returns `null` there without a
|
|
294
|
+
capability call, because the execution has no user.
|
|
295
|
+
- All calls of one execution read the same version of the structure.
|
|
296
|
+
- IDs are validated by the SDK and again by Cogover; an invalid ID or option throws
|
|
297
|
+
`ValidationError`. Every method returns a promise, and an invalid argument rejects
|
|
298
|
+
that promise rather than throwing when the method is called. `get` methods return `null` for an unknown ID; `members`,
|
|
299
|
+
`managers`, `managerChain`, `ancestors` and `tree({ rootId })` throw
|
|
300
|
+
`NotFoundError` with `resource` `"department"` or `"position"`.
|
|
301
|
+
- When the structure cannot be read, the call throws `CogoverApiError` with code
|
|
302
|
+
`ORGANIZATION_UNAVAILABLE`.
|
|
303
|
+
- Call `getMany` or `members` instead of calling `get` in a loop: each call is one
|
|
304
|
+
capability call.
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# API reference: `@cogover/sdk`
|
|
2
|
+
|
|
3
|
+
`@cogover/sdk` is the public TypeScript contract for developing Custom Backend
|
|
4
|
+
Modules on Cogover. Cogover authentication credentials are never exposed to
|
|
5
|
+
module code; capability calls are restricted by the permissions and limits
|
|
6
|
+
configured for the project.
|
|
7
|
+
|
|
8
|
+
## Contents
|
|
9
|
+
|
|
10
|
+
The reference is split into the following pages. Each page covers one area of the SDK.
|
|
11
|
+
|
|
12
|
+
| Page | Covers |
|
|
13
|
+
|---|---|
|
|
14
|
+
| [Scripts and HTTP routes](scripts-and-routes.md) | `defineScript`; HTTP routers and request context; Invocation identity and workspace; HTTP response API; Logging |
|
|
15
|
+
| [Records and schema](records.md) | Record API; Filters and sorting; Record execution identities; Schema API; Workspace declarations |
|
|
16
|
+
| [Record triggers](record-triggers.md) | Record triggers |
|
|
17
|
+
| [Background jobs](background-jobs.md) | Background jobs |
|
|
18
|
+
| [Outbound HTTP and secrets](fetch-and-secrets.md) | Outbound HTTP `fetch`; Secrets and credentials |
|
|
19
|
+
| [Cryptography](cryptography.md) | Cryptography |
|
|
20
|
+
| [Inbound webhooks](inbound-webhooks.md) | Inbound webhooks |
|
|
21
|
+
| [Project state and locks](state-and-locks.md) | Project state; Distributed locks |
|
|
22
|
+
| [Push messages](push-messages.md) | Push messages |
|
|
23
|
+
| [Notifications and email](notifications-and-email.md) | Notifications; Email |
|
|
24
|
+
| [Organization structure](organization-structure.md) | Organization structure |
|
|
25
|
+
|
|
26
|
+
The sections below apply to every area: the errors every API can throw, the limits of one execution, and the compatibility helper.
|
|
27
|
+
|
|
28
|
+
## Errors
|
|
29
|
+
|
|
30
|
+
API failures become `NotFoundError`, `ValidationError`,
|
|
31
|
+
`PermissionDeniedError`, `RateLimitError`, `RetryableError`, `StateConflictError`,
|
|
32
|
+
`LockUnavailableError`, `LockLostError`, or `CogoverApiError`.
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
class CogoverApiError extends Error {
|
|
36
|
+
readonly code: string;
|
|
37
|
+
readonly r: number | undefined;
|
|
38
|
+
readonly details?: unknown;
|
|
39
|
+
constructor(code: string, message: string, details?: unknown);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class NotFoundError extends CogoverApiError {
|
|
43
|
+
readonly resource: string;
|
|
44
|
+
readonly resourceId: string;
|
|
45
|
+
constructor(resource: string, resourceId: string, message?: string, details?: unknown);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class ValidationError extends CogoverApiError {
|
|
49
|
+
constructor(message: string, details?: unknown);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class PermissionDeniedError extends CogoverApiError {
|
|
53
|
+
constructor(message: string, details?: unknown);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
class RateLimitError extends CogoverApiError {
|
|
57
|
+
constructor(message: string, details?: unknown);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class RetryableError extends CogoverApiError {
|
|
61
|
+
constructor(message: string, details?: unknown);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
class StateConflictError extends CogoverApiError {
|
|
65
|
+
constructor(message: string, details?: unknown);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
class LockUnavailableError extends CogoverApiError {
|
|
69
|
+
constructor(key: string, message?: string, details?: unknown);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class LockLostError extends CogoverApiError {
|
|
73
|
+
constructor(message: string, details?: unknown);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Errors expose the SDK category in `error.code` and the original numeric server
|
|
78
|
+
result in `error.r` (also retained in `error.details.r`). `r` is `undefined` when
|
|
79
|
+
there was no server response, such as local SDK validation. The original message
|
|
80
|
+
remains in `error.message`.
|
|
81
|
+
|
|
82
|
+
`RetryableError` (`code: "RETRYABLE"`) reports a temporary failure. The SDK throws it
|
|
83
|
+
for a record write that a before-change trigger could not check (`details.reason`
|
|
84
|
+
`"TRIGGER_FAILED"`, see [Record triggers](record-triggers.md#validating-and-changing-records)),
|
|
85
|
+
and project code throws it itself. An after-change trigger handler or a job handler
|
|
86
|
+
throws it to report a temporary failure, and Cogover runs the handler again later (see
|
|
87
|
+
[After-change execution rules](record-triggers.md#after-change-execution-rules) and
|
|
88
|
+
[Job execution rules](background-jobs.md#job-execution-rules)). Anywhere else it has no special
|
|
89
|
+
meaning: a script that lets it escape returns HTTP 422, and a before-change trigger
|
|
90
|
+
that throws it rejects the write like any other failure.
|
|
91
|
+
|
|
92
|
+
`jobs.enqueue`, `secrets.get`, and `crypto` fail with `ValidationError`,
|
|
93
|
+
`PermissionDeniedError`, or `RateLimitError` as described in their sections, and
|
|
94
|
+
with `CogoverApiError` whose `code` is `JOBS_DISABLED` or `SECRETS_DISABLED` when
|
|
95
|
+
the feature is not enabled for the Workspace. `crypto.aesDecrypt` and
|
|
96
|
+
`crypto.rsaDecrypt` throw `CogoverApiError` with `code: "DECRYPTION_FAILED"` when
|
|
97
|
+
the ciphertext cannot be decrypted.
|
|
98
|
+
|
|
99
|
+
`notifications.send` and `email.send` fail with `ValidationError`, `NotFoundError`,
|
|
100
|
+
`PermissionDeniedError`, or `RateLimitError` as described in their sections, with
|
|
101
|
+
`CogoverApiError` whose `code` is `NOTIFICATIONS_DISABLED` or `EMAIL_DISABLED` when
|
|
102
|
+
the feature is switched off, and with `CogoverApiError` whose `code` is
|
|
103
|
+
`DUPLICATE_IN_PROGRESS` while an earlier call with the same `idempotencyKey` is
|
|
104
|
+
still sending. A sender mailbox that the Project policy does not allow is reported
|
|
105
|
+
as `PermissionDeniedError` with `details.reason` `"EMAIL_SENDER_NOT_GRANTED"`.
|
|
106
|
+
|
|
107
|
+
Scripts can catch `CogoverApiError`, inspect `error.r`, and return their own client
|
|
108
|
+
error, such as `{ r: 1001, msg: "You cannot update this order." }`, without exposing
|
|
109
|
+
the original code or message. A script's returned JSON remains its own response
|
|
110
|
+
with HTTP 200. All client-facing text in `msg`, `message`, and equivalent response
|
|
111
|
+
fields must be written in English, including responses created by customer scripts.
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
try {
|
|
115
|
+
await orders.records.update(id, fields);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (error instanceof CogoverApiError && error.r === 37) {
|
|
118
|
+
return { r: 1001, msg: "You do not have permission to update this order." };
|
|
119
|
+
}
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Import `CogoverApiError` from `@cogover/sdk`.
|
|
125
|
+
|
|
126
|
+
For SDK errors escaping `defineScript`, the HTTP endpoint returns an error with
|
|
127
|
+
`r` equal to the HTTP status, `code`, and a safe `msg`:
|
|
128
|
+
|
|
129
|
+
| SDK code | HTTP / `r` |
|
|
130
|
+
|---|---|
|
|
131
|
+
| `PERMISSION_DENIED` | 403 |
|
|
132
|
+
| `NOT_FOUND` | 404 |
|
|
133
|
+
| `METHOD_NOT_ALLOWED` | 405 |
|
|
134
|
+
| `VALIDATION_ERROR` | 400 |
|
|
135
|
+
| `RATE_LIMITED` | 429 |
|
|
136
|
+
| `STATE_CONFLICT` | 409 |
|
|
137
|
+
| `LOCK_NOT_ACQUIRED` | 409 |
|
|
138
|
+
| `LOCK_LOST` | 409 |
|
|
139
|
+
| `STATE_SERVICE_UNAVAILABLE` | 503 |
|
|
140
|
+
| `LOCK_SERVICE_UNAVAILABLE` | 503 |
|
|
141
|
+
| `ORGANIZATION_UNAVAILABLE` | 503 |
|
|
142
|
+
| `FETCH_DISABLED` | 503 |
|
|
143
|
+
| `FETCH_BLOCKED` | 400 |
|
|
144
|
+
| `FETCH_REQUEST_TOO_LARGE` | 413 |
|
|
145
|
+
| `FETCH_RESPONSE_TOO_LARGE` | 502 |
|
|
146
|
+
| `FETCH_TIMEOUT` | 504 |
|
|
147
|
+
| `FETCH_FAILED` | 502 |
|
|
148
|
+
| `COGOVER_API_ERROR` | 422 |
|
|
149
|
+
| `RETRYABLE` | 422 |
|
|
150
|
+
|
|
151
|
+
Missing approval for an explicit identity adds `reason: "IDENTITY_NOT_GRANTED"`;
|
|
152
|
+
it does not mean the selected user was denied access to a particular record.
|
|
153
|
+
Raw error messages, stack traces and arbitrary details are not returned.
|
|
154
|
+
`writesMayHaveCompleted` is true if the execution sent any call that can change
|
|
155
|
+
something outside it, even if a later operation failed: a record write, `state.set` or
|
|
156
|
+
`state.delete`, any `locks` call (including an `acquire` that returned `null`), a push
|
|
157
|
+
message, `notifications.send`, `email.send`, or a `fetch` whose method is not `GET` or
|
|
158
|
+
`HEAD`. A `LockUnavailableError` that escapes a script therefore always reports `true`.
|
|
159
|
+
It is a conservative warning, not confirmation of a successful write.
|
|
160
|
+
Unknown errors and resource-limit failures retain the generic HTTP 422 response.
|
|
161
|
+
These responses do not roll back earlier writes; do not retry writes automatically.
|
|
162
|
+
|
|
163
|
+
## Execution limits
|
|
164
|
+
|
|
165
|
+
Every execution runs within platform limits. These are the default values; they are
|
|
166
|
+
set by the platform and may differ between environments.
|
|
167
|
+
|
|
168
|
+
- An HTTP route and a record trigger may make at most 20 capability calls. Every call
|
|
169
|
+
to `data`, `schema`, `state`, `locks`, `push`, `notifications`, `email`, `jobs`,
|
|
170
|
+
`secrets`, `crypto`, `org`, or `fetch` counts once. A background job may make 200.
|
|
171
|
+
The call over the budget, and every call after it, throws `RateLimitError`
|
|
172
|
+
(`details.limit` is the budget) without being sent, so a script can catch it.
|
|
173
|
+
- An HTTP route has 8 seconds of wall-clock time, counted from when its handler
|
|
174
|
+
starts, including the time spent waiting for capability calls, a lock, or a remote
|
|
175
|
+
server. A record trigger has its own `timeoutMs`, and a background job its
|
|
176
|
+
`timeoutMs`. Exceeding the time ends the whole execution: the script cannot catch it,
|
|
177
|
+
and an HTTP route returns the generic HTTP 422 response.
|
|
178
|
+
- A single capability request, JSON-encoded with its arguments, must not exceed
|
|
179
|
+
262,144 bytes. A larger request is refused before it is sent with `ValidationError`,
|
|
180
|
+
or with `CogoverApiError` `code: "FETCH_REQUEST_TOO_LARGE"` for `fetch`; `details.limit`
|
|
181
|
+
is the limit. A capability result larger than 1,048,576 bytes is withheld the same
|
|
182
|
+
way (`FETCH_RESPONSE_TOO_LARGE` for `fetch`): ask for fewer records or fields. The
|
|
183
|
+
operation itself did run, so a write may have completed.
|
|
184
|
+
- When the Workspace already has too many executions waiting, a new HTTP call is
|
|
185
|
+
refused with HTTP 429 and `code: "RATE_LIMITED"` before anything runs; retry it later.
|
|
186
|
+
|
|
187
|
+
When an API has its own per-execution quota that is not below the call budget, such
|
|
188
|
+
as 20 `fetch` calls or 20 secret reads, the call budget is reached first in a route
|
|
189
|
+
or a trigger; both are reported as `RateLimitError`.
|
|
190
|
+
|
|
191
|
+
## Compatibility helper
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
function createGreeting(name: string): string;
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
`createGreeting("Cogover")` returns `"Hello Cogover"`. It remains available for
|
|
198
|
+
SDK 0.1.x compatibility and is not part of the data APIs.
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# `@cogover/sdk` API reference: Push messages
|
|
2
|
+
|
|
3
|
+
This page is part of the [`@cogover/sdk` API reference](overview.md).
|
|
4
|
+
|
|
5
|
+
## Push messages
|
|
6
|
+
|
|
7
|
+
`push` sends messages to the web clients of the workspace: a request to reload
|
|
8
|
+
records, a toast, or a background message the client handles silently. It is the
|
|
9
|
+
same mechanism as the Workflow "Push Message" action, so the standard record pages
|
|
10
|
+
react without custom client code. Delivery is best-effort: a call resolves once the
|
|
11
|
+
messages are handed to Cogover, never when a client receives them.
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
type PushRecipients = "viewers" | readonly string[];
|
|
15
|
+
|
|
16
|
+
interface PushOptions {
|
|
17
|
+
readonly recipients?: PushRecipients;
|
|
18
|
+
readonly exclude?: readonly string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type ToastPosition = "TOP_LEFT" | "TOP_CENTER" | "TOP_RIGHT"
|
|
22
|
+
| "BOTTOM_LEFT" | "BOTTOM_CENTER" | "BOTTOM_RIGHT";
|
|
23
|
+
type ToastSize = "SMALL" | "MEDIUM" | "LARGE";
|
|
24
|
+
|
|
25
|
+
interface ToastLink {
|
|
26
|
+
readonly label: string;
|
|
27
|
+
readonly url: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface ToastMessage<ObjectSlug extends string = string> {
|
|
31
|
+
readonly title?: string;
|
|
32
|
+
readonly content?: string;
|
|
33
|
+
readonly links?: readonly ToastLink[];
|
|
34
|
+
readonly position?: ToastPosition;
|
|
35
|
+
readonly size?: ToastSize;
|
|
36
|
+
readonly durationSeconds?: number;
|
|
37
|
+
readonly object?: ObjectSlug;
|
|
38
|
+
readonly recordIds?: readonly string[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type BackgroundContentType = "text" | "template";
|
|
42
|
+
|
|
43
|
+
interface BackgroundMessage<ObjectSlug extends string = string> {
|
|
44
|
+
readonly content: string;
|
|
45
|
+
readonly contentType?: BackgroundContentType;
|
|
46
|
+
readonly object?: ObjectSlug;
|
|
47
|
+
readonly recordIds?: readonly string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface PushApi<Schema extends object = EffectiveWorkspaceObjects> {
|
|
51
|
+
refreshRecords(object: StringKeyOf<Schema>, recordIds: readonly string[],
|
|
52
|
+
options?: PushOptions): Promise<void>;
|
|
53
|
+
toast(message: ToastMessage<StringKeyOf<Schema>>, options?: PushOptions): Promise<void>;
|
|
54
|
+
message(message: BackgroundMessage<StringKeyOf<Schema>>, options?: PushOptions): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// After-change trigger: the handler changed the records, so everyone who has them
|
|
60
|
+
// open must reload, including the user whose save started the trigger.
|
|
61
|
+
await data.object("order").records.batchUpdate(items);
|
|
62
|
+
await push.refreshRecords("order", items.map(item => item.id));
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Recipients
|
|
66
|
+
|
|
67
|
+
Every method accepts the same `options`. `recipients` defaults to `"viewers"`:
|
|
68
|
+
everyone who currently has one of the target records open receives the message, so
|
|
69
|
+
`object` and `recordIds` are required. A list of personnel IDs delivers to those
|
|
70
|
+
people wherever they are in the workspace; `object` and `recordIds` are then
|
|
71
|
+
optional for `toast` and `message` and, when given, tell the client which records
|
|
72
|
+
the message is about. `exclude` lists personnel IDs that never receive the message;
|
|
73
|
+
the special value `"actor"` is the user whose action started the current execution
|
|
74
|
+
(the caller of an HTTP script, the user whose save ran a trigger). A system
|
|
75
|
+
execution has no actor, so `"actor"` excludes nobody. Personnel without a user
|
|
76
|
+
account are skipped silently; an unknown personnel ID or object slug throws
|
|
77
|
+
`NotFoundError` before anything is sent. `recordIds` accepts at most 200 distinct
|
|
78
|
+
record IDs, and `recipients` and `exclude` each at most 200 entries, counted before
|
|
79
|
+
duplicates are removed; duplicates are then removed. Cogover does not check that the
|
|
80
|
+
record IDs exist: a message about a record that does not exist reaches no viewer.
|
|
81
|
+
|
|
82
|
+
### `push.refreshRecords(object, recordIds, options?): Promise<void>`
|
|
83
|
+
|
|
84
|
+
Asks the clients that show these records to reload them. The record page of the
|
|
85
|
+
web app handles the request itself. `recordIds` accepts 1 to 200 IDs.
|
|
86
|
+
|
|
87
|
+
### `push.toast(message, options?): Promise<void>`
|
|
88
|
+
|
|
89
|
+
Shows a short notification. At least one of `title` (up to 200 characters) and
|
|
90
|
+
`content` (up to 4,000 characters) is required. `links` holds up to 5 entries whose
|
|
91
|
+
`url` is an absolute `http` or `https` URL. `position` defaults to `"TOP_RIGHT"`,
|
|
92
|
+
`size` to `"MEDIUM"` and `durationSeconds` to 5 (1 to 60). When `object` and
|
|
93
|
+
`recordIds` are given, the client shows the toast only while one of those records
|
|
94
|
+
is open.
|
|
95
|
+
|
|
96
|
+
### `push.message(message, options?): Promise<void>`
|
|
97
|
+
|
|
98
|
+
Sends a message the client handles without a visible notification, for example a
|
|
99
|
+
custom component listening for it. `content` is required (up to 16,384 characters);
|
|
100
|
+
`contentType` is `"text"` (default) or `"template"`.
|
|
101
|
+
|
|
102
|
+
### Rules
|
|
103
|
+
|
|
104
|
+
- Each call costs one capability call regardless of the number of records or
|
|
105
|
+
recipients; when the recipients are `"viewers"`, one message is sent per record.
|
|
106
|
+
- Inputs are validated by the SDK and again by Cogover; invalid input throws
|
|
107
|
+
`ValidationError` before any message is sent.
|
|
108
|
+
- `push` is available in scripts, routes, after-change triggers and Development
|
|
109
|
+
Sessions. A before-change trigger is read-only and receives `PermissionDeniedError`
|
|
110
|
+
with reason `TRIGGER_READ_ONLY`; a read-only Development Session refuses it too.
|
|
111
|
+
- When push messages are switched off for the deployment, every call throws
|
|
112
|
+
`CogoverApiError` with code `PUSH_DISABLED`; an HTTP route that does not catch it
|
|
113
|
+
returns the generic HTTP 422 response.
|
|
114
|
+
- A sent message cannot be recalled, and retrying a failed execution may send it
|
|
115
|
+
again.
|