@stackonward/onex-cms-client 0.0.1

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 StackOnward
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @stackonward/onex-cms-client
2
+
3
+ Explicit OneX HTTP adapter for `@stackonward/cms-client`. It owns the
4
+ `X-Product-Code` and `X-Preview-Token` headers and the canonical OneX error
5
+ envelope, keeping those contracts out of the generic CMS client.
6
+
7
+ ```typescript
8
+ import { createOneXCmsClient, createOneXCmsUpstreamHeaders } from "@stackonward/onex-cms-client";
9
+
10
+ const cms = createOneXCmsClient({
11
+ baseUrl: "https://platform.example.com/api/v1/cms",
12
+ productCode: "product_alpha",
13
+ });
14
+
15
+ const upstreamHeaders = createOneXCmsUpstreamHeaders("product_alpha");
16
+ ```
@@ -0,0 +1,16 @@
1
+ import { ErrorNormalizer } from '@stackonward/api-client';
2
+ import { CmsClientConfig, CmsClient } from '@stackonward/cms-client';
3
+
4
+ interface OneXCmsClientConfig extends Omit<CmsClientConfig, "errorNormalizer" | "headers"> {
5
+ productCode: string;
6
+ previewToken?: string;
7
+ headers?: HeadersInit;
8
+ }
9
+ declare const oneXCmsErrorNormalizer: ErrorNormalizer;
10
+ /** 构建供 CMS Nuxt 上游配置使用的 OneX 产品上下文头。 */
11
+ declare function createOneXCmsUpstreamHeaders(productCode: string): Record<string, string>;
12
+ /** 创建显式绑定 OneX HTTP 合同的 CMS 客户端。 */
13
+ declare function createOneXCmsClient(config: OneXCmsClientConfig): CmsClient;
14
+
15
+ export { createOneXCmsClient, createOneXCmsUpstreamHeaders, oneXCmsErrorNormalizer };
16
+ export type { OneXCmsClientConfig };
@@ -0,0 +1,16 @@
1
+ import { ErrorNormalizer } from '@stackonward/api-client';
2
+ import { CmsClientConfig, CmsClient } from '@stackonward/cms-client';
3
+
4
+ interface OneXCmsClientConfig extends Omit<CmsClientConfig, "errorNormalizer" | "headers"> {
5
+ productCode: string;
6
+ previewToken?: string;
7
+ headers?: HeadersInit;
8
+ }
9
+ declare const oneXCmsErrorNormalizer: ErrorNormalizer;
10
+ /** 构建供 CMS Nuxt 上游配置使用的 OneX 产品上下文头。 */
11
+ declare function createOneXCmsUpstreamHeaders(productCode: string): Record<string, string>;
12
+ /** 创建显式绑定 OneX HTTP 合同的 CMS 客户端。 */
13
+ declare function createOneXCmsClient(config: OneXCmsClientConfig): CmsClient;
14
+
15
+ export { createOneXCmsClient, createOneXCmsUpstreamHeaders, oneXCmsErrorNormalizer };
16
+ export type { OneXCmsClientConfig };
package/dist/index.mjs ADDED
@@ -0,0 +1,72 @@
1
+ import { ApiHttpError, NormalizedApiError } from '@stackonward/api-client';
2
+ import { CmsApiError, CmsClient } from '@stackonward/cms-client';
3
+
4
+ function parseErrorEnvelope(body) {
5
+ if (typeof body !== "object" || body === null || !("error" in body)) {
6
+ return null;
7
+ }
8
+ const error = body.error;
9
+ if (typeof error !== "object" || error === null || typeof error.code !== "string" || typeof error.message !== "string" || typeof error.type !== "string") {
10
+ return null;
11
+ }
12
+ return error;
13
+ }
14
+ const oneXCmsErrorNormalizer = {
15
+ normalize(raw) {
16
+ if (raw instanceof CmsApiError) {
17
+ return raw;
18
+ }
19
+ if (raw instanceof ApiHttpError) {
20
+ const error = parseErrorEnvelope(raw.data);
21
+ return new CmsApiError(
22
+ raw.status,
23
+ error?.code ?? "UNKNOWN_ERROR",
24
+ error?.message ?? `OneX CMS request failed with status ${raw.status}`,
25
+ raw.data
26
+ );
27
+ }
28
+ if (raw instanceof NormalizedApiError) {
29
+ return new CmsApiError(
30
+ raw.statusCode ?? 0,
31
+ String(raw.code ?? raw.kind).toUpperCase(),
32
+ raw.message,
33
+ raw.body
34
+ );
35
+ }
36
+ return new CmsApiError(
37
+ 0,
38
+ "NETWORK_ERROR",
39
+ raw instanceof Error ? raw.message : "OneX CMS request failed",
40
+ raw
41
+ );
42
+ }
43
+ };
44
+ function assertProductCode(productCode) {
45
+ if (!/^[a-z][a-z0-9_]{1,49}$/.test(productCode)) {
46
+ throw new TypeError("OneX productCode must match ^[a-z][a-z0-9_]{1,49}$");
47
+ }
48
+ }
49
+ function createOneXCmsUpstreamHeaders(productCode) {
50
+ assertProductCode(productCode);
51
+ return { "X-Product-Code": productCode };
52
+ }
53
+ function createOneXCmsClient(config) {
54
+ assertProductCode(config.productCode);
55
+ const headers = new Headers(config.headers);
56
+ for (const [name, value] of Object.entries(createOneXCmsUpstreamHeaders(config.productCode))) {
57
+ headers.set(name, value);
58
+ }
59
+ if (config.previewToken) {
60
+ headers.set("X-Preview-Token", config.previewToken);
61
+ }
62
+ return new CmsClient({
63
+ baseUrl: config.baseUrl,
64
+ defaultLocale: config.defaultLocale,
65
+ timeout: config.timeout,
66
+ fetch: config.fetch,
67
+ headers,
68
+ errorNormalizer: oneXCmsErrorNormalizer
69
+ });
70
+ }
71
+
72
+ export { createOneXCmsClient, createOneXCmsUpstreamHeaders, oneXCmsErrorNormalizer };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@stackonward/onex-cms-client",
3
+ "version": "0.0.1",
4
+ "description": "OneX HTTP contract adapter for the StackOnward CMS client",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "sideEffects": false,
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/1yhy/stackonward.git",
27
+ "directory": "packages/integrations/onex/cms-client"
28
+ },
29
+ "dependencies": {
30
+ "@stackonward/cms-client": "0.0.1",
31
+ "@stackonward/api-client": "0.0.1"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^22.0.0",
35
+ "unbuild": "^3.5.0",
36
+ "vitest": "^3.2.4"
37
+ },
38
+ "scripts": {
39
+ "build": "unbuild",
40
+ "typecheck": "tsc --noEmit",
41
+ "test": "vitest run"
42
+ }
43
+ }