@baijimu/cmodel 1.0.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/LICENSE ADDED
@@ -0,0 +1,178 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Baijimu
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @baijimu/cmodel
2
+
3
+ Strict TypeScript types and parser for Baijimu CModel Error Contract `1.0.0`.
4
+
5
+ ```bash
6
+ npm install @baijimu/cmodel@1.0.0
7
+ ```
8
+
9
+ ```ts
10
+ import { parseCModelResponse } from "@baijimu/cmodel";
11
+
12
+ const response = parseCModelResponse(httpStatus, body, (value) => value);
13
+ ```
14
+
15
+ The public package contains only the protocol types and validation logic. It does not include Baijimu's internal service catalog or internal data models. See <https://docs.baijimu.com/integration/cmodel-error-model/>.
16
+
@@ -0,0 +1,3 @@
1
+ export * from "./parser.js";
2
+ export * from "./types.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./parser.js";
2
+ export * from "./types.js";
@@ -0,0 +1,8 @@
1
+ import { type CModelResponse } from "./types.js";
2
+ export declare class CModelContractError extends Error {
3
+ readonly path: string;
4
+ constructor(path: string, message: string);
5
+ }
6
+ export type DataParser<T> = (value: unknown, path: string) => T;
7
+ export declare function parseCModelResponse<T>(httpStatus: number, input: unknown, parseData: DataParser<T>): CModelResponse<T>;
8
+ //# sourceMappingURL=parser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,cAAc,EAQpB,MAAM,YAAY,CAAC;AAqBpB,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK1C;AAED,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC,CAAC;AAEhE,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,OAAO,EACd,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,GACvB,cAAc,CAAC,CAAC,CAAC,CAwEnB"}
package/dist/parser.js ADDED
@@ -0,0 +1,256 @@
1
+ import { CONTRACT_VERSION, } from "./types.js";
2
+ const codePattern = /^(?:0|[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*)$/;
3
+ const reasonPattern = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/;
4
+ const domainPattern = /^[a-z0-9]+(?:-[a-z0-9]+)*(?:\.[a-z0-9]+(?:-[a-z0-9]+)*)+$/;
5
+ const servicePattern = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
6
+ const operationPattern = /^[a-z0-9]+(?:[._-][a-z0-9]+)*$/;
7
+ const categories = new Set([
8
+ "validation",
9
+ "auth",
10
+ "permission",
11
+ "not_found",
12
+ "conflict",
13
+ "rate_limit",
14
+ "upstream_timeout",
15
+ "upstream_rate_limit",
16
+ "upstream_unavailable",
17
+ "internal",
18
+ "cancelled",
19
+ ]);
20
+ export class CModelContractError extends Error {
21
+ path;
22
+ constructor(path, message) {
23
+ super(`CModel contract violation at ${path}: ${message}`);
24
+ this.name = "CModelContractError";
25
+ this.path = path;
26
+ }
27
+ }
28
+ export function parseCModelResponse(httpStatus, input, parseData) {
29
+ const envelope = objectAt(input, "$", [
30
+ "contractVersion",
31
+ "errorCode",
32
+ "value",
33
+ "data",
34
+ "systemCurrentTime",
35
+ "error",
36
+ ]);
37
+ requiredKeys(envelope, "$", [
38
+ "contractVersion",
39
+ "errorCode",
40
+ "value",
41
+ "data",
42
+ "systemCurrentTime",
43
+ ]);
44
+ const contractVersion = stringAt(envelope.contractVersion, "$.contractVersion", 1, 64);
45
+ if (contractVersion !== CONTRACT_VERSION) {
46
+ fail("$.contractVersion", `unsupported contract version ${contractVersion}`);
47
+ }
48
+ const errorCode = stringAt(envelope.errorCode, "$.errorCode", 1, 128);
49
+ if (!codePattern.test(errorCode)) {
50
+ fail("$.errorCode", "must be 0 or upper snake case");
51
+ }
52
+ const value = stringAt(envelope.value, "$.value", 0, 512);
53
+ const systemCurrentTime = safeIntegerAt(envelope.systemCurrentTime, "$.systemCurrentTime", 0);
54
+ const httpSuccess = httpStatus >= 200 && httpStatus < 300;
55
+ if (httpStatus === 204) {
56
+ fail("$httpStatus", "CModel responses cannot use 204 No Content");
57
+ }
58
+ if (errorCode === "0") {
59
+ if (!httpSuccess) {
60
+ fail("$httpStatus", "non-2xx status cannot carry a success envelope");
61
+ }
62
+ if ("error" in envelope) {
63
+ fail("$.error", "success response cannot contain error");
64
+ }
65
+ const data = envelope.data === null ? null : parseData(envelope.data, "$.data");
66
+ return {
67
+ contractVersion: CONTRACT_VERSION,
68
+ errorCode: "0",
69
+ value,
70
+ data,
71
+ systemCurrentTime,
72
+ };
73
+ }
74
+ if (httpSuccess) {
75
+ fail("$httpStatus", "2xx status cannot carry a failure envelope");
76
+ }
77
+ if (envelope.data !== null) {
78
+ fail("$.data", "failure response data must be null");
79
+ }
80
+ if (!("error" in envelope)) {
81
+ fail("$.error", "failure response must contain error");
82
+ }
83
+ const error = parseError(envelope.error, "$.error");
84
+ if (error.code !== errorCode) {
85
+ fail("$.error.code", "must equal errorCode");
86
+ }
87
+ if (error.userMessage !== value) {
88
+ fail("$.value", "must equal error.userMessage");
89
+ }
90
+ return {
91
+ contractVersion: CONTRACT_VERSION,
92
+ errorCode,
93
+ value,
94
+ data: null,
95
+ systemCurrentTime,
96
+ error,
97
+ };
98
+ }
99
+ function parseError(input, path) {
100
+ const value = objectAt(input, path, [
101
+ "code",
102
+ "domain",
103
+ "reason",
104
+ "category",
105
+ "message",
106
+ "userMessage",
107
+ "retryable",
108
+ "source",
109
+ "trace",
110
+ "upstream",
111
+ "causes",
112
+ "violations",
113
+ ]);
114
+ requiredKeys(value, path, [
115
+ "code",
116
+ "domain",
117
+ "reason",
118
+ "category",
119
+ "message",
120
+ "userMessage",
121
+ "retryable",
122
+ "source",
123
+ "trace",
124
+ ]);
125
+ const code = stringAt(value.code, `${path}.code`, 1, 128);
126
+ if (code === "0" || !codePattern.test(code))
127
+ fail(`${path}.code`, "invalid failure code");
128
+ const domain = stringAt(value.domain, `${path}.domain`, 3, 128);
129
+ if (!domainPattern.test(domain))
130
+ fail(`${path}.domain`, "invalid domain");
131
+ const reason = stringAt(value.reason, `${path}.reason`, 1, 96);
132
+ if (!reasonPattern.test(reason))
133
+ fail(`${path}.reason`, "invalid reason");
134
+ const category = stringAt(value.category, `${path}.category`, 1, 64);
135
+ if (!categories.has(category))
136
+ fail(`${path}.category`, "unknown category");
137
+ const message = stringAt(value.message, `${path}.message`, 1, 1024);
138
+ const userMessage = stringAt(value.userMessage, `${path}.userMessage`, 1, 512);
139
+ if (typeof value.retryable !== "boolean")
140
+ fail(`${path}.retryable`, "must be boolean");
141
+ const source = parseSource(value.source, `${path}.source`);
142
+ const trace = parseTrace(value.trace, `${path}.trace`);
143
+ const upstream = value.upstream === undefined ? undefined : parseUpstream(value.upstream, `${path}.upstream`);
144
+ const causes = value.causes === undefined ? undefined : arrayAt(value.causes, `${path}.causes`, 1, 16).map((cause, index) => parseCause(cause, `${path}.causes[${index}]`));
145
+ const violations = value.violations === undefined ? undefined : arrayAt(value.violations, `${path}.violations`, 1, 100).map((violation, index) => parseViolation(violation, `${path}.violations[${index}]`));
146
+ return {
147
+ code,
148
+ domain,
149
+ reason,
150
+ category: category,
151
+ message,
152
+ userMessage,
153
+ retryable: value.retryable,
154
+ source,
155
+ trace,
156
+ ...(upstream === undefined ? {} : { upstream }),
157
+ ...(causes === undefined ? {} : { causes }),
158
+ ...(violations === undefined ? {} : { violations }),
159
+ };
160
+ }
161
+ function parseSource(input, path) {
162
+ const value = objectAt(input, path, ["service", "operation"]);
163
+ requiredKeys(value, path, ["service"]);
164
+ const service = stringAt(value.service, `${path}.service`, 1, 96);
165
+ if (!servicePattern.test(service))
166
+ fail(`${path}.service`, "must use lower-kebab-case");
167
+ const operation = value.operation === undefined ? undefined : stringAt(value.operation, `${path}.operation`, 1, 160);
168
+ if (operation !== undefined && !operationPattern.test(operation))
169
+ fail(`${path}.operation`, "contains unsupported characters");
170
+ return { service, ...(operation === undefined ? {} : { operation }) };
171
+ }
172
+ function parseTrace(input, path) {
173
+ const value = objectAt(input, path, ["requestId", "traceId"]);
174
+ requiredKeys(value, path, ["requestId"]);
175
+ const requestId = stringAt(value.requestId, `${path}.requestId`, 1, 256);
176
+ const traceId = value.traceId === undefined ? undefined : stringAt(value.traceId, `${path}.traceId`, 1, 256);
177
+ return { requestId, ...(traceId === undefined ? {} : { traceId }) };
178
+ }
179
+ function parseUpstream(input, path) {
180
+ const value = objectAt(input, path, ["service", "status", "code", "retryAfterMillis"]);
181
+ requiredKeys(value, path, ["service"]);
182
+ const service = stringAt(value.service, `${path}.service`, 1, 96);
183
+ const status = value.status === undefined ? undefined : integerAt(value.status, `${path}.status`, 100, 599);
184
+ const code = value.code === undefined ? undefined : stringAt(value.code, `${path}.code`, 1, 256);
185
+ const retryAfterMillis = value.retryAfterMillis === undefined ? undefined : safeIntegerAt(value.retryAfterMillis, `${path}.retryAfterMillis`, 0);
186
+ return {
187
+ service,
188
+ ...(status === undefined ? {} : { status }),
189
+ ...(code === undefined ? {} : { code }),
190
+ ...(retryAfterMillis === undefined ? {} : { retryAfterMillis }),
191
+ };
192
+ }
193
+ function parseCause(input, path) {
194
+ const value = objectAt(input, path, ["code", "domain", "reason", "source"]);
195
+ requiredKeys(value, path, ["code", "domain", "reason", "source"]);
196
+ const code = stringAt(value.code, `${path}.code`, 1, 128);
197
+ const domain = stringAt(value.domain, `${path}.domain`, 3, 128);
198
+ const reason = stringAt(value.reason, `${path}.reason`, 1, 96);
199
+ if (code === "0" || !codePattern.test(code))
200
+ fail(`${path}.code`, "invalid cause code");
201
+ if (!domainPattern.test(domain))
202
+ fail(`${path}.domain`, "invalid domain");
203
+ if (!reasonPattern.test(reason))
204
+ fail(`${path}.reason`, "invalid reason");
205
+ return { code, domain, reason, source: parseSource(value.source, `${path}.source`) };
206
+ }
207
+ function parseViolation(input, path) {
208
+ const value = objectAt(input, path, ["path", "reason", "message"]);
209
+ requiredKeys(value, path, ["path", "reason", "message"]);
210
+ const violationPath = stringAt(value.path, `${path}.path`, 1, 256);
211
+ const reason = stringAt(value.reason, `${path}.reason`, 1, 96);
212
+ if (!reasonPattern.test(reason))
213
+ fail(`${path}.reason`, "invalid reason");
214
+ return { path: violationPath, reason, message: stringAt(value.message, `${path}.message`, 1, 512) };
215
+ }
216
+ function objectAt(input, path, allowedKeys) {
217
+ if (typeof input !== "object" || input === null || Array.isArray(input))
218
+ fail(path, "must be an object");
219
+ const value = input;
220
+ const allowed = new Set(allowedKeys);
221
+ for (const key of Object.keys(value))
222
+ if (!allowed.has(key))
223
+ fail(`${path}.${key}`, "unknown field");
224
+ return value;
225
+ }
226
+ function requiredKeys(value, path, keys) {
227
+ for (const key of keys)
228
+ if (!(key in value))
229
+ fail(`${path}.${key}`, "missing required field");
230
+ }
231
+ function stringAt(input, path, minimum, maximum) {
232
+ if (typeof input !== "string")
233
+ fail(path, "must be a string");
234
+ const length = [...input].length;
235
+ if (length < minimum || length > maximum)
236
+ fail(path, `length must be between ${minimum} and ${maximum}`);
237
+ return input;
238
+ }
239
+ function integerAt(input, path, minimum, maximum) {
240
+ if (!Number.isInteger(input) || typeof input !== "number" || input < minimum || input > maximum)
241
+ fail(path, `must be an integer between ${minimum} and ${maximum}`);
242
+ return input;
243
+ }
244
+ function safeIntegerAt(input, path, minimum) {
245
+ if (typeof input !== "number" || !Number.isSafeInteger(input) || input < minimum)
246
+ fail(path, `must be a safe integer greater than or equal to ${minimum}`);
247
+ return input;
248
+ }
249
+ function arrayAt(input, path, minimum, maximum) {
250
+ if (!Array.isArray(input) || input.length < minimum || input.length > maximum)
251
+ fail(path, `must contain between ${minimum} and ${maximum} items`);
252
+ return input;
253
+ }
254
+ function fail(path, message) {
255
+ throw new CModelContractError(path, message);
256
+ }
@@ -0,0 +1,59 @@
1
+ export declare const CONTRACT_VERSION: "1.0.0";
2
+ export type ErrorCategory = "validation" | "auth" | "permission" | "not_found" | "conflict" | "rate_limit" | "upstream_timeout" | "upstream_rate_limit" | "upstream_unavailable" | "internal" | "cancelled";
3
+ export interface ErrorSource {
4
+ readonly service: string;
5
+ readonly operation?: string;
6
+ }
7
+ export interface ErrorTrace {
8
+ readonly requestId: string;
9
+ readonly traceId?: string;
10
+ }
11
+ export interface UpstreamError {
12
+ readonly service: string;
13
+ readonly status?: number;
14
+ readonly code?: string;
15
+ readonly retryAfterMillis?: number;
16
+ }
17
+ export interface ErrorCause {
18
+ readonly code: string;
19
+ readonly domain: string;
20
+ readonly reason: string;
21
+ readonly source: ErrorSource;
22
+ }
23
+ export interface ErrorViolation {
24
+ readonly path: string;
25
+ readonly reason: string;
26
+ readonly message: string;
27
+ }
28
+ export interface CModelError {
29
+ readonly code: string;
30
+ readonly domain: string;
31
+ readonly reason: string;
32
+ readonly category: ErrorCategory;
33
+ readonly message: string;
34
+ readonly userMessage: string;
35
+ readonly retryable: boolean;
36
+ readonly source: ErrorSource;
37
+ readonly trace: ErrorTrace;
38
+ readonly upstream?: UpstreamError;
39
+ readonly causes?: readonly ErrorCause[];
40
+ readonly violations?: readonly ErrorViolation[];
41
+ }
42
+ export interface CModelSuccess<T> {
43
+ readonly contractVersion: typeof CONTRACT_VERSION;
44
+ readonly errorCode: "0";
45
+ readonly value: string;
46
+ readonly data: T | null;
47
+ readonly systemCurrentTime: number;
48
+ }
49
+ export interface CModelFailure {
50
+ readonly contractVersion: typeof CONTRACT_VERSION;
51
+ readonly errorCode: string;
52
+ readonly value: string;
53
+ readonly data: null;
54
+ readonly systemCurrentTime: number;
55
+ readonly error: CModelError;
56
+ }
57
+ export type CModelResponse<T> = CModelSuccess<T> | CModelFailure;
58
+ export declare function isCModelSuccess<T>(response: CModelResponse<T>): response is CModelSuccess<T>;
59
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"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"}
package/dist/types.js ADDED
@@ -0,0 +1,4 @@
1
+ export const CONTRACT_VERSION = "1.0.0";
2
+ export function isCModelSuccess(response) {
3
+ return response.errorCode === "0";
4
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@baijimu/cmodel",
3
+ "version": "1.0.0",
4
+ "description": "Strict TypeScript parser and types for the Baijimu CModel Error Contract",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "node ../../scripts/clean-package-output.mjs dist && tsc -p tsconfig.json",
13
+ "test": "npm run build && node --test test/*.test.mjs"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "homepage": "https://docs.baijimu.com/integration/cmodel-error-model/",
19
+ "license": "Apache-2.0",
20
+ "devDependencies": {
21
+ "typescript": "^5.9.3"
22
+ }
23
+ }