@dropsh/sdc-client 0.0.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/LICENSE ADDED
@@ -0,0 +1,51 @@
1
+ drupal-cli is dual-licensed. You may use this software under the terms of
2
+ either the MIT License or the GNU General Public License version 2 or any
3
+ later version, at your option.
4
+
5
+ SPDX-License-Identifier: (MIT OR GPL-2.0-or-later)
6
+
7
+ ================================================================================
8
+ MIT License
9
+ ================================================================================
10
+
11
+ Copyright (c) 2026 Christian Wiedemann
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ SOFTWARE.
30
+
31
+ ================================================================================
32
+ GNU General Public License, version 2 or later
33
+ ================================================================================
34
+
35
+ The full text of the GNU GPL v2 is available at:
36
+ https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
37
+
38
+ The full text of any later GPL version is available at:
39
+ https://www.gnu.org/licenses/
40
+
41
+ Copyright (C) 2026 Christian Wiedemann
42
+
43
+ This program is free software; you can redistribute it and/or modify it under
44
+ the terms of the GNU General Public License as published by the Free Software
45
+ Foundation; either version 2 of the License, or (at your option) any later
46
+ version.
47
+
48
+ This program is distributed in the hope that it will be useful, but WITHOUT
49
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
50
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
51
+ details.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # @dropsh/sdc-client
2
+
3
+ Single Directory Components (SDC) client for [dropsh](https://github.com/pen-drop/dropsh),
4
+ the entity-agnostic CLI for Drupal 11 JSON:API.
5
+
6
+ A small library that fetches and types a Drupal site's SDC component definitions
7
+ via the `jsonapi_sdc` module. It is the building block used by
8
+ [`@dropsh/plugin-canvas`](https://www.npmjs.com/package/@dropsh/plugin-canvas) to
9
+ build Canvas component schemas; you normally depend on it transitively rather than
10
+ directly.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ pnpm add @dropsh/sdc-client
16
+ ```
17
+
18
+ `dropsh` is a peer dependency.
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import { fetchSdcComponents, type SdcComponent } from "@dropsh/sdc-client";
24
+ import type { PluginContext } from "dropsh/plugin";
25
+
26
+ async function listComponents(ctx: PluginContext): Promise<SdcComponent[]> {
27
+ return fetchSdcComponents(ctx); // uses ctx.http / ctx.auth / ctx.baseUrl
28
+ }
29
+ ```
30
+
31
+ `fetchSdcComponents` throws an `HttpError` (status 404) when `jsonapi_sdc` is not
32
+ installed or reachable.
33
+
34
+ ## Required Drupal module
35
+
36
+ - `jsonapi_sdc`
37
+
38
+ ## License
39
+
40
+ MIT OR GPL-2.0-or-later
@@ -0,0 +1,24 @@
1
+ import { type PluginContext } from "dropsh/plugin";
2
+ export interface SdcComponent {
3
+ id: string;
4
+ jsonapiId: string;
5
+ name: string;
6
+ description: string;
7
+ status: string;
8
+ provider: string;
9
+ props: Record<string, unknown>;
10
+ slots: Record<string, unknown>;
11
+ variants: Record<string, unknown>;
12
+ }
13
+ /**
14
+ * Normalises a Drupal SDC id (e.g. "olivero:teaser") into the dotted form
15
+ * used by dropsh schemas: "sdc.<provider>.<name>".
16
+ */
17
+ export declare function toSdcComponentId(sdcId: string): string;
18
+ /**
19
+ * Fetches all SDC components from the Drupal jsonapi_sdc endpoint and
20
+ * returns them as a normalised list. Throws a `404 HttpError` with a clear
21
+ * message if jsonapi_sdc is not installed, and `502`/`422` HttpErrors for
22
+ * malformed or empty responses.
23
+ */
24
+ export declare function fetchSdcComponents(ctx: PluginContext): Promise<SdcComponent[]>;
@@ -0,0 +1,83 @@
1
+ import { HttpError } from "dropsh/plugin";
2
+ const invalidJsonApiMessage = "jsonapi_sdc did not return a valid JSON:API document.";
3
+ function asObject(value) {
4
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
5
+ return {};
6
+ }
7
+ return value;
8
+ }
9
+ function asString(value, fallback) {
10
+ return typeof value === "string" && value.length > 0 ? value : fallback;
11
+ }
12
+ /**
13
+ * Normalises a Drupal SDC id (e.g. "olivero:teaser") into the dotted form
14
+ * used by dropsh schemas: "sdc.<provider>.<name>".
15
+ */
16
+ export function toSdcComponentId(sdcId) {
17
+ const [provider, name] = sdcId.split(":", 2);
18
+ if (!provider || !name) {
19
+ return `sdc.${sdcId.replace(/:/g, ".")}`;
20
+ }
21
+ return `sdc.${provider}.${name}`;
22
+ }
23
+ function normalizeResource(resource) {
24
+ const attributes = resource.attributes ?? {};
25
+ const id = asString(attributes.drupal_internal__id, "");
26
+ if (!id.includes(":")) {
27
+ return null;
28
+ }
29
+ const provider = asString(attributes.provider, id.split(":", 1)[0] ?? "");
30
+ return {
31
+ id,
32
+ jsonapiId: asString(resource.id, id.replace(/:/g, "--")),
33
+ name: asString(attributes.name, id),
34
+ description: asString(attributes.description, ""),
35
+ status: asString(attributes.status, "stable"),
36
+ provider,
37
+ props: asObject(attributes.props),
38
+ slots: asObject(attributes.slots),
39
+ variants: asObject(attributes.variants),
40
+ };
41
+ }
42
+ /**
43
+ * Fetches all SDC components from the Drupal jsonapi_sdc endpoint and
44
+ * returns them as a normalised list. Throws a `404 HttpError` with a clear
45
+ * message if jsonapi_sdc is not installed, and `502`/`422` HttpErrors for
46
+ * malformed or empty responses.
47
+ */
48
+ export async function fetchSdcComponents(ctx) {
49
+ const request = await ctx.auth.apply({
50
+ method: "GET",
51
+ url: `${ctx.baseUrl.replace(/\/+$/, "")}/jsonapi/sdc_component`,
52
+ headers: { Accept: "application/vnd.api+json" },
53
+ });
54
+ let response;
55
+ try {
56
+ response = await ctx.http.send(request);
57
+ }
58
+ catch (error) {
59
+ if (error instanceof HttpError && error.status === 404) {
60
+ throw new HttpError(error.status, "Drupal module jsonapi_sdc is required to fetch SDC components.", error.body);
61
+ }
62
+ throw error;
63
+ }
64
+ let body;
65
+ try {
66
+ body = JSON.parse(response.body);
67
+ }
68
+ catch {
69
+ throw new HttpError(502, invalidJsonApiMessage, response.body);
70
+ }
71
+ if (typeof body !== "object" || body === null || Array.isArray(body)) {
72
+ throw new HttpError(502, invalidJsonApiMessage, body);
73
+ }
74
+ const jsonApiBody = body;
75
+ const resources = Array.isArray(jsonApiBody.data) ? jsonApiBody.data : [];
76
+ const components = resources
77
+ .map((resource) => normalizeResource(asObject(resource)))
78
+ .filter((component) => component !== null);
79
+ if (components.length === 0) {
80
+ throw new HttpError(422, "jsonapi_sdc returned no SDC components.", jsonApiBody);
81
+ }
82
+ return components;
83
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@dropsh/sdc-client",
3
+ "version": "0.0.2",
4
+ "type": "module",
5
+ "main": "./dist/packages/sdc-client/src/index.js",
6
+ "types": "./dist/packages/sdc-client/src/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/packages/sdc-client/src/index.d.ts",
10
+ "default": "./dist/packages/sdc-client/src/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "files": [
15
+ "dist/packages/sdc-client/src",
16
+ "README.md"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "peerDependencies": {
22
+ "dropsh": "*"
23
+ },
24
+ "devDependencies": {
25
+ "typescript": "^5.6.0",
26
+ "vitest": "^4.1.8",
27
+ "dropsh": "0.0.2"
28
+ },
29
+ "scripts": {
30
+ "build": "tsc",
31
+ "test": "vitest run --passWithNoTests",
32
+ "typecheck": "tsc --noEmit"
33
+ }
34
+ }