@capacms/sdk 1.0.0-next.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.
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WEBHOOKS_NEED_ACCESS_TOKEN = void 0;
4
+ exports.createWebhooks = createWebhooks;
5
+ const http_1 = require("./http");
6
+ // ------------------------------------------------------------ the resource --
7
+ exports.WEBHOOKS_NEED_ACCESS_TOKEN = "@capacms/sdk: webhooks need accessToken; API keys cannot manage endpoints.";
8
+ const BASE = "/v2/webhooks";
9
+ function endpointPath(id, suffix = "") {
10
+ return `${BASE}/endpoints/${encodeURIComponent(id)}${suffix}`;
11
+ }
12
+ function deliveryPath(id, suffix = "") {
13
+ return `${BASE}/deliveries/${encodeURIComponent(id)}${suffix}`;
14
+ }
15
+ function createWebhooks(resolved) {
16
+ /**
17
+ * Checked before every call rather than once in `createClient`, because a
18
+ * client built for reading content is legitimately built without a token and
19
+ * should not throw until somebody actually reaches for webhooks.
20
+ */
21
+ function requireToken() {
22
+ if (!resolved.accessToken)
23
+ throw new Error(exports.WEBHOOKS_NEED_ACCESS_TOKEN);
24
+ }
25
+ async function read(path, query = {}) {
26
+ requireToken();
27
+ const { body } = await (0, http_1.getJson)(resolved, path, query, "session");
28
+ return body;
29
+ }
30
+ async function write(method, path, body) {
31
+ requireToken();
32
+ return (0, http_1.writeJson)(resolved, method, path, body, "session");
33
+ }
34
+ /**
35
+ * A 404 is an ANSWER on `get`, not a failure: "no endpoint with that id in
36
+ * this tenant" is exactly what `null` means, and it is the same rule
37
+ * `scheduledActions.get` follows. Every other status still throws.
38
+ */
39
+ async function readOrNull(path) {
40
+ try {
41
+ return await read(path);
42
+ }
43
+ catch (err) {
44
+ if (err?.status === 404)
45
+ return null;
46
+ throw err;
47
+ }
48
+ }
49
+ return {
50
+ async events() {
51
+ const body = await read(`${BASE}/events`);
52
+ return body.events ?? [];
53
+ },
54
+ endpoints: {
55
+ async list() {
56
+ const body = await read(`${BASE}/endpoints`);
57
+ return body.endpoints ?? [];
58
+ },
59
+ async create(input) {
60
+ // Refused here rather than by the API, because both are 400s the caller
61
+ // can avoid and the message says which one they hit.
62
+ if (!input?.name)
63
+ throw new Error("@capacms/sdk: webhooks.endpoints.create needs a name.");
64
+ if (!input?.url)
65
+ throw new Error("@capacms/sdk: webhooks.endpoints.create needs a url.");
66
+ if (!input?.events?.length) {
67
+ throw new Error("@capacms/sdk: webhooks.endpoints.create needs at least one event. " +
68
+ "Read webhooks.events() for the catalogue, or subscribe to a wildcard like \"instance.*\".");
69
+ }
70
+ return write("POST", `${BASE}/endpoints`, input);
71
+ },
72
+ async get(id) {
73
+ return readOrNull(endpointPath(id));
74
+ },
75
+ async update(id, patch) {
76
+ return write("PATCH", endpointPath(id), patch ?? {});
77
+ },
78
+ async delete(id) {
79
+ const body = await write("DELETE", endpointPath(id));
80
+ return body?.deleted === true;
81
+ },
82
+ async pause(id) {
83
+ return write("POST", endpointPath(id, "/pause"), {});
84
+ },
85
+ async resume(id, options = {}) {
86
+ return write("POST", endpointPath(id, "/resume"),
87
+ // Both keys are sent only when asked for. `backfill: false` and an
88
+ // absent `backfill` mean the same thing to the route, and sending
89
+ // `since: undefined` would serialise to nothing anyway.
90
+ {
91
+ ...(options.backfill ? { backfill: true } : {}),
92
+ ...(options.since ? { since: options.since } : {}),
93
+ });
94
+ },
95
+ async revealSecret(id) {
96
+ return write("POST", endpointPath(id, "/secret/reveal"), {});
97
+ },
98
+ async rotateSecret(id) {
99
+ return write("POST", endpointPath(id, "/secret/rotate"), {});
100
+ },
101
+ async test(id) {
102
+ return write("POST", endpointPath(id, "/test"), {});
103
+ },
104
+ async deliveries(id, options = {}) {
105
+ const body = await read(endpointPath(id, "/deliveries"), {
106
+ status: options.status?.length ? options.status.join(",") : undefined,
107
+ eventType: options.eventType,
108
+ page: options.page,
109
+ limit: options.limit,
110
+ });
111
+ return {
112
+ deliveries: body.deliveries ?? [],
113
+ pagination: body.pagination ?? { total: 0, page: 1, limit: 0, totalPages: 0, hasMore: false },
114
+ };
115
+ },
116
+ async redeliverFailed(id, options = {}) {
117
+ const body = await write("POST", endpointPath(id, "/redeliver-failed"), options.since ? { since: options.since } : {});
118
+ return { created: body?.created ?? 0 };
119
+ },
120
+ },
121
+ deliveries: {
122
+ async get(id) {
123
+ return readOrNull(deliveryPath(id));
124
+ },
125
+ async redeliver(id) {
126
+ return write("POST", deliveryPath(id, "/redeliver"), {});
127
+ },
128
+ },
129
+ };
130
+ }
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@capacms/sdk",
3
+ "version": "1.0.0-next.0",
4
+ "license": "UNLICENSED",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/ZVN-DEV/capa-cms-future.git",
8
+ "directory": "packages/sdk"
9
+ },
10
+ "homepage": "https://github.com/ZVN-DEV/capa-cms-future/tree/main/packages/sdk#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/ZVN-DEV/capa-cms-future/issues"
13
+ },
14
+ "engines": {
15
+ "node": ">=18"
16
+ },
17
+ "sideEffects": false,
18
+ "files": [
19
+ "dist",
20
+ "bin",
21
+ "README.md"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public",
25
+ "tag": "next"
26
+ },
27
+ "main": "dist/index.js",
28
+ "types": "dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "./next": {
35
+ "types": "./dist/next/index.d.ts",
36
+ "default": "./dist/next/index.js"
37
+ },
38
+ "./nextjs": {
39
+ "types": "./dist/nextjs/index.d.ts",
40
+ "default": "./dist/nextjs/index.js"
41
+ },
42
+ "./package.json": "./package.json"
43
+ },
44
+ "typesVersions": {
45
+ "*": {
46
+ "next": [
47
+ "dist/next/index.d.ts"
48
+ ],
49
+ "nextjs": [
50
+ "dist/nextjs/index.d.ts"
51
+ ],
52
+ "*": [
53
+ "dist/index.d.ts"
54
+ ]
55
+ }
56
+ },
57
+ "bin": {
58
+ "capa-codegen": "bin/capa-codegen.js"
59
+ },
60
+ "devDependencies": {
61
+ "typescript": "^5.5.0",
62
+ "@capa/shared": "0.1.0"
63
+ },
64
+ "scripts": {
65
+ "build": "tsc -p tsconfig.json",
66
+ "typecheck": "tsc -p tsconfig.json --noEmit",
67
+ "test": "tsc -p tsconfig.json && node --test test/codegen.test.js test/client.test.js test/next-client.test.js test/nextjs.test.js test/webhooks.test.js && tsc -p test/types/tsconfig.consumer.json --noEmit"
68
+ }
69
+ }