@cogover/sdk 0.1.2 → 0.6.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/README.md +347 -24
- package/dist/data.d.ts +49 -0
- package/dist/data.js +170 -0
- package/dist/errors.d.ts +37 -0
- package/dist/errors.js +72 -0
- package/dist/fetch.d.ts +28 -0
- package/dist/fetch.js +96 -0
- package/dist/index.d.ts +23 -3
- package/dist/index.js +8 -5
- package/dist/internal/bridge.js +48 -0
- package/dist/internal/invocation.js +57 -0
- package/dist/internal/json.js +81 -0
- package/dist/internal/request.js +87 -0
- package/dist/internal/trigger-config.js +212 -0
- package/dist/internal/trigger-runtime.js +211 -0
- package/dist/internal/validation.js +28 -0
- package/dist/internal/values.js +6 -0
- package/dist/invocation.d.ts +40 -0
- package/dist/invocation.js +1 -0
- package/dist/legacy.d.ts +2 -0
- package/dist/legacy.js +4 -0
- package/dist/locks.d.ts +19 -0
- package/dist/locks.js +68 -0
- package/dist/logger.d.ts +6 -0
- package/dist/logger.js +14 -0
- package/dist/query.d.ts +52 -0
- package/dist/query.js +59 -0
- package/dist/records.d.ts +79 -0
- package/dist/records.js +1 -0
- package/dist/request.d.ts +18 -0
- package/dist/request.js +1 -0
- package/dist/response.d.ts +21 -0
- package/dist/response.js +108 -0
- package/dist/router.d.ts +13 -0
- package/dist/router.js +104 -0
- package/dist/schema.d.ts +26 -0
- package/dist/schema.js +11 -0
- package/dist/script.d.ts +23 -0
- package/dist/script.js +56 -0
- package/dist/state.d.ts +24 -0
- package/dist/state.js +91 -0
- package/dist/trigger.d.ts +121 -0
- package/dist/trigger.js +15 -0
- package/dist/workspace.d.ts +8 -0
- package/dist/workspace.js +1 -0
- package/docs/en/api-reference.md +1415 -0
- package/docs/en/usage-guide.md +495 -0
- package/package.json +17 -5
- package/scripts/generate-workspace-types.mjs +135 -0
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/state.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { callBridge } from "./internal/bridge.js";
|
|
2
|
+
import { ValidationError } from "./errors.js";
|
|
3
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9._:/-]*$/;
|
|
4
|
+
const MAX_NAMESPACE_LENGTH = 128;
|
|
5
|
+
const MAX_KEY_LENGTH = 255;
|
|
6
|
+
const MAX_VALUE_BYTES = 32 * 1024;
|
|
7
|
+
const MAX_TTL_SECONDS = 365 * 24 * 60 * 60;
|
|
8
|
+
/** @internal */
|
|
9
|
+
export function createStateApi() {
|
|
10
|
+
return Object.freeze({
|
|
11
|
+
namespace(name) {
|
|
12
|
+
const namespace = validateName(name, "state namespace", MAX_NAMESPACE_LENGTH);
|
|
13
|
+
return Object.freeze({
|
|
14
|
+
name: namespace,
|
|
15
|
+
async get(key) {
|
|
16
|
+
return callBridge("state.get", {
|
|
17
|
+
namespace,
|
|
18
|
+
key: validateName(key, "state key", MAX_KEY_LENGTH),
|
|
19
|
+
});
|
|
20
|
+
},
|
|
21
|
+
async set(key, value, options) {
|
|
22
|
+
validateValue(value);
|
|
23
|
+
return callBridge("state.set", {
|
|
24
|
+
namespace,
|
|
25
|
+
key: validateName(key, "state key", MAX_KEY_LENGTH),
|
|
26
|
+
value,
|
|
27
|
+
expectedVersion: optionalInteger(options?.expectedVersion, "expectedVersion", 0, Number.MAX_SAFE_INTEGER),
|
|
28
|
+
ttlSeconds: optionalInteger(options?.ttlSeconds, "ttlSeconds", 1, MAX_TTL_SECONDS),
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
async delete(key, options) {
|
|
32
|
+
return callBridge("state.delete", {
|
|
33
|
+
namespace,
|
|
34
|
+
key: validateName(key, "state key", MAX_KEY_LENGTH),
|
|
35
|
+
expectedVersion: optionalInteger(options?.expectedVersion, "expectedVersion", 0, Number.MAX_SAFE_INTEGER),
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function validateValue(value) {
|
|
43
|
+
if (value === undefined)
|
|
44
|
+
throw new ValidationError("state value must be JSON serializable");
|
|
45
|
+
let serialized;
|
|
46
|
+
try {
|
|
47
|
+
serialized = JSON.stringify(value);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
throw new ValidationError("state value must be JSON serializable");
|
|
51
|
+
}
|
|
52
|
+
if (serialized === undefined)
|
|
53
|
+
throw new ValidationError("state value must be JSON serializable");
|
|
54
|
+
if (utf8Bytes(serialized) > MAX_VALUE_BYTES) {
|
|
55
|
+
throw new ValidationError(`state value must not exceed ${MAX_VALUE_BYTES} UTF-8 bytes`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** @internal */
|
|
59
|
+
export function validateName(value, label, maximumLength) {
|
|
60
|
+
if (typeof value !== "string" || value.length === 0 || value.length > maximumLength || !NAME.test(value)) {
|
|
61
|
+
throw new ValidationError(`${label} is invalid`);
|
|
62
|
+
}
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
/** @internal */
|
|
66
|
+
export function optionalInteger(value, label, minimum, maximum) {
|
|
67
|
+
if (value === undefined)
|
|
68
|
+
return undefined;
|
|
69
|
+
if (!Number.isSafeInteger(value) || value < minimum || value > maximum) {
|
|
70
|
+
throw new ValidationError(`${label} must be an integer between ${minimum} and ${maximum}`);
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
function utf8Bytes(value) {
|
|
75
|
+
let bytes = 0;
|
|
76
|
+
for (let index = 0; index < value.length; index++) {
|
|
77
|
+
const code = value.charCodeAt(index);
|
|
78
|
+
if (code < 0x80)
|
|
79
|
+
bytes++;
|
|
80
|
+
else if (code < 0x800)
|
|
81
|
+
bytes += 2;
|
|
82
|
+
else if (code >= 0xd800 && code <= 0xdbff && index + 1 < value.length
|
|
83
|
+
&& value.charCodeAt(index + 1) >= 0xdc00 && value.charCodeAt(index + 1) <= 0xdfff) {
|
|
84
|
+
bytes += 4;
|
|
85
|
+
index++;
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
bytes += 3;
|
|
89
|
+
}
|
|
90
|
+
return bytes;
|
|
91
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { DataApi } from "./data.js";
|
|
2
|
+
import type { InvocationContext } from "./invocation.js";
|
|
3
|
+
import type { ScriptLogger } from "./logger.js";
|
|
4
|
+
import type { FilterOperator } from "./query.js";
|
|
5
|
+
import type { CogoverRecordId, UpdateFields } from "./records.js";
|
|
6
|
+
import type { SchemaApi } from "./schema.js";
|
|
7
|
+
import type { EffectiveWorkspaceObjects, FieldsFor, StringKeyOf } from "./workspace.js";
|
|
8
|
+
/** When a record trigger runs relative to the record change. */
|
|
9
|
+
export type TriggerTiming = "beforeChange" | "afterChange";
|
|
10
|
+
/** Record operations a trigger can subscribe to. */
|
|
11
|
+
export type TriggerOperation = "create" | "update" | "delete";
|
|
12
|
+
/** `"onEnter"` runs only when a record starts matching `when`; `"always"` runs whenever it matches. */
|
|
13
|
+
export type TriggerRunWhen = "always" | "onEnter";
|
|
14
|
+
/** One field comparison of a trigger `when` filter. */
|
|
15
|
+
export interface TriggerFilterCondition {
|
|
16
|
+
readonly op: FilterOperator;
|
|
17
|
+
readonly field: string;
|
|
18
|
+
/** Comparison value; omitted for `"is null"` and `"not null"`. */
|
|
19
|
+
readonly params?: unknown;
|
|
20
|
+
}
|
|
21
|
+
/** Combines trigger `when` filters with `and` or `or`. */
|
|
22
|
+
export interface TriggerFilterGroup {
|
|
23
|
+
readonly op: "and" | "or";
|
|
24
|
+
readonly conditions: readonly TriggerFilter[];
|
|
25
|
+
}
|
|
26
|
+
export type TriggerFilter = TriggerFilterCondition | TriggerFilterGroup;
|
|
27
|
+
type TriggerObjectFields<TObject extends string, TSchema extends object> = TObject extends keyof TSchema ? FieldsFor<TSchema, TObject> : Record<string, unknown>;
|
|
28
|
+
type TriggerFieldSlug<TObject extends string, TSchema extends object> = StringKeyOf<TriggerObjectFields<TObject, TSchema>>;
|
|
29
|
+
/** Options accepted by `defineTrigger`. */
|
|
30
|
+
export interface TriggerConfig<TObject extends string = string, TOperation extends TriggerOperation = TriggerOperation, TSchema extends object = EffectiveWorkspaceObjects> {
|
|
31
|
+
/** Stable identifier of the trigger inside the project. */
|
|
32
|
+
readonly key: string;
|
|
33
|
+
/** Display name; defaults to `key`. */
|
|
34
|
+
readonly name?: string;
|
|
35
|
+
/** Slug of the Object whose records run this trigger. */
|
|
36
|
+
readonly object: TObject;
|
|
37
|
+
readonly timing: TriggerTiming;
|
|
38
|
+
readonly operations: readonly TOperation[];
|
|
39
|
+
/** Fields delivered in `record.new` and `record.old`. */
|
|
40
|
+
readonly fields: readonly TriggerFieldSlug<TObject, TSchema>[];
|
|
41
|
+
/** Updates run the trigger only when at least one of these fields changed. */
|
|
42
|
+
readonly changedFields?: readonly TriggerFieldSlug<TObject, TSchema>[];
|
|
43
|
+
/** Only records matching this filter run the trigger. */
|
|
44
|
+
readonly when?: TriggerFilter;
|
|
45
|
+
readonly runWhen?: TriggerRunWhen;
|
|
46
|
+
/** Fields the handler may change through `record.new`. */
|
|
47
|
+
readonly writableFields?: readonly TriggerFieldSlug<TObject, TSchema>[];
|
|
48
|
+
/** Position among the triggers of the same Object; lower values run first. */
|
|
49
|
+
readonly order?: number;
|
|
50
|
+
/** Time budget of one handler call, in milliseconds. */
|
|
51
|
+
readonly timeoutMs?: number;
|
|
52
|
+
}
|
|
53
|
+
/** Normalized, immutable trigger configuration with every default applied. */
|
|
54
|
+
export interface TriggerManifest {
|
|
55
|
+
readonly key: string;
|
|
56
|
+
readonly name?: string;
|
|
57
|
+
readonly object: string;
|
|
58
|
+
readonly timing: TriggerTiming;
|
|
59
|
+
readonly operations: readonly TriggerOperation[];
|
|
60
|
+
readonly fields: readonly string[];
|
|
61
|
+
readonly changedFields?: readonly string[];
|
|
62
|
+
readonly when?: TriggerFilter;
|
|
63
|
+
readonly runWhen: TriggerRunWhen;
|
|
64
|
+
readonly writableFields: readonly string[];
|
|
65
|
+
readonly order: number;
|
|
66
|
+
readonly timeoutMs: number;
|
|
67
|
+
}
|
|
68
|
+
/** Entry point reserved for Cogover; project code does not call it. */
|
|
69
|
+
export type TriggerSandboxHandler = (inputJson: string) => Promise<string>;
|
|
70
|
+
/** Result of `defineTrigger`; list it in the project's `triggers` export. */
|
|
71
|
+
export interface TriggerDefinition {
|
|
72
|
+
readonly key: string;
|
|
73
|
+
readonly config: TriggerManifest;
|
|
74
|
+
/** Reserved for Cogover; project code does not call it. */
|
|
75
|
+
readonly __cogoverTriggerHandler: TriggerSandboxHandler;
|
|
76
|
+
}
|
|
77
|
+
/** Identifies the trigger and the record change of the current call. */
|
|
78
|
+
export interface TriggerInfo<TOperation extends TriggerOperation = TriggerOperation> {
|
|
79
|
+
readonly id: string;
|
|
80
|
+
readonly key: string;
|
|
81
|
+
readonly timing: TriggerTiming;
|
|
82
|
+
readonly operation: TOperation;
|
|
83
|
+
readonly objectSlug: string;
|
|
84
|
+
/** Identifies the record change that caused this call. */
|
|
85
|
+
readonly changeId: string;
|
|
86
|
+
}
|
|
87
|
+
/** Mutable field values after the change. Assigned values use the `records.update` input format. */
|
|
88
|
+
export type TriggerNewValues<TFields> = {
|
|
89
|
+
-readonly [K in keyof TFields]?: TFields[K] | Exclude<UpdateFields<TFields>[K], undefined>;
|
|
90
|
+
};
|
|
91
|
+
/** Immutable field values before the change. */
|
|
92
|
+
export type TriggerOldValues<TFields> = {
|
|
93
|
+
readonly [K in keyof TFields]?: TFields[K];
|
|
94
|
+
};
|
|
95
|
+
type TriggerValuesFor<TAbsentFor extends TriggerOperation, TOperation extends TriggerOperation, TValues> = [TOperation] extends [TAbsentFor] ? null : TAbsentFor extends TOperation ? TValues | null : TValues;
|
|
96
|
+
/** One record of the change being processed. */
|
|
97
|
+
export interface TriggerRecord<TFields = Record<string, unknown>, TOperation extends TriggerOperation = TriggerOperation> {
|
|
98
|
+
/** Correlates this record with its result; not a record ID. */
|
|
99
|
+
readonly key: string;
|
|
100
|
+
/** `null` while a record that is being created has no ID yet. */
|
|
101
|
+
readonly id: CogoverRecordId | null;
|
|
102
|
+
/** `null` when the record is being deleted. */
|
|
103
|
+
readonly new: TriggerValuesFor<"delete", TOperation, TriggerNewValues<TFields>>;
|
|
104
|
+
/** `null` when the record is being created. */
|
|
105
|
+
readonly old: TriggerValuesFor<"create", TOperation, TriggerOldValues<TFields>>;
|
|
106
|
+
readonly changedFields: readonly StringKeyOf<TFields>[];
|
|
107
|
+
/** Rejects this record. Pass `null` as the field for an error about the whole record. */
|
|
108
|
+
addError(field: StringKeyOf<TFields> | null, code: string, message?: string): void;
|
|
109
|
+
}
|
|
110
|
+
export interface TriggerContext<TObject extends string = string, TOperation extends TriggerOperation = TriggerOperation, TSchema extends object = EffectiveWorkspaceObjects> {
|
|
111
|
+
readonly records: readonly TriggerRecord<TriggerObjectFields<TObject, TSchema>, TOperation>[];
|
|
112
|
+
readonly trigger: TriggerInfo<TOperation>;
|
|
113
|
+
readonly invocation: InvocationContext;
|
|
114
|
+
readonly data: DataApi<TSchema>;
|
|
115
|
+
readonly schema: SchemaApi<TSchema>;
|
|
116
|
+
readonly log: ScriptLogger;
|
|
117
|
+
}
|
|
118
|
+
export type TriggerHandler<TObject extends string = string, TOperation extends TriggerOperation = TriggerOperation, TSchema extends object = EffectiveWorkspaceObjects> = (context: TriggerContext<TObject, TOperation, TSchema>) => void | Promise<void>;
|
|
119
|
+
/** Declares a record trigger. Add the result to the project's `triggers` export. */
|
|
120
|
+
export declare function defineTrigger<TObject extends StringKeyOf<TSchema>, TOperation extends TriggerOperation, TSchema extends object = EffectiveWorkspaceObjects>(config: TriggerConfig<TObject, TOperation, TSchema>, handler: TriggerHandler<TObject, TOperation, TSchema>): TriggerDefinition;
|
|
121
|
+
export {};
|
package/dist/trigger.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ValidationError } from "./errors.js";
|
|
2
|
+
import { normalizeTriggerConfig } from "./internal/trigger-config.js";
|
|
3
|
+
import { createTriggerSandboxHandler } from "./internal/trigger-runtime.js";
|
|
4
|
+
/** Declares a record trigger. Add the result to the project's `triggers` export. */
|
|
5
|
+
export function defineTrigger(config, handler) {
|
|
6
|
+
const manifest = normalizeTriggerConfig(config);
|
|
7
|
+
if (typeof handler !== "function") {
|
|
8
|
+
throw new ValidationError(`Trigger '${manifest.key}' requires a handler function`);
|
|
9
|
+
}
|
|
10
|
+
return Object.freeze({
|
|
11
|
+
key: manifest.key,
|
|
12
|
+
config: manifest,
|
|
13
|
+
__cogoverTriggerHandler: createTriggerSandboxHandler(manifest, handler),
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Extended by customer declarations through the @cogover/sdk entry point. */
|
|
2
|
+
export interface WorkspaceObjects {
|
|
3
|
+
}
|
|
4
|
+
type UnknownFields = Record<string, unknown>;
|
|
5
|
+
export type EffectiveWorkspaceObjects = keyof WorkspaceObjects extends never ? Record<string, UnknownFields> : WorkspaceObjects;
|
|
6
|
+
export type StringKeyOf<T> = Extract<keyof T, string>;
|
|
7
|
+
export type FieldsFor<TSchema, K extends keyof TSchema> = TSchema[K] extends object ? TSchema[K] : UnknownFields;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|