@done-coding/schema-api 0.0.1-alpha.0 → 0.0.1-alpha.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/README.md CHANGED
@@ -4,7 +4,9 @@
4
4
  跨前后端通用接口结构的类型、常量与定义文件
5
5
  ```
6
6
 
7
- 成功外壳、错误体、三种列表(一次性 / 页码式 / 游标式)、列表请求参数与业务码的 TypeScript 类型、字段路径常量与 JSON Schema 定义文件,由同一份定义生成;同版本的 Python 包为 PyPI 上的 `done-coding-schema-api`。
7
+ 成功外壳、错误体、三种列表(一次性 / 页码式 / 游标式)、列表请求参数与业务码的 TypeScript 类型与 JSON Schema 定义文件,由同一份定义生成;同版本的 Python 包为 PyPI 上的 `done-coding-schema-api`。
8
+
9
+ 两个出口:主入口 `@done-coding/schema-api` 只有结构(类型、成功码、结构定义文件);通用失败码与码表、字段路径与参数名、参考数值在 `@done-coding/schema-api/reference`,只是参考,不随结构生效,要用须显式引入。
8
10
 
9
11
  ## 使用
10
12
 
@@ -13,11 +15,8 @@ npm install @done-coding/schema-api
13
15
  ```
14
16
 
15
17
  ```ts
16
- import {
17
- PAGED_LIST_REQUEST_PARAMS,
18
- type PagedList,
19
- type SuccessEnvelope,
20
- } from "@done-coding/schema-api";
18
+ import type { PagedList, SuccessEnvelope } from "@done-coding/schema-api";
19
+ import { PAGED_LIST_REQUEST_PARAMS } from "@done-coding/schema-api/reference";
21
20
 
22
21
  interface User {
23
22
  id: string;
@@ -29,6 +28,6 @@ response.data.page.totalRecord; // number
29
28
  PAGED_LIST_REQUEST_PARAMS.pageSize; // "page.pageSize"
30
29
  ```
31
30
 
32
- 定义文件以 `schemas` 导出,也可按路径取:`@done-coding/schema-api/schemas/<结构名>.schema.json`。
31
+ 结构定义文件以 `schemas` 导出,参考资源定义文件以 `referenceSchemas` 从 `./reference` 导出,也可按路径取:`@done-coding/schema-api/schemas/<结构名>.schema.json`。
33
32
 
34
33
  各结构的说明、怎么选列表、业务码与版本规则见仓库 README:<https://github.com/done-coding/done-coding-schema#readme>
package/es/index.mjs CHANGED
@@ -1,108 +1,5 @@
1
- const d = {
1
+ const c = {
2
2
  OK: "OK"
3
- }, u = {
4
- INVALID_REQUEST: "INVALID_REQUEST",
5
- UNAUTHENTICATED: "UNAUTHENTICATED",
6
- FORBIDDEN: "FORBIDDEN",
7
- NOT_FOUND: "NOT_FOUND",
8
- ROUTE_NOT_FOUND: "ROUTE_NOT_FOUND",
9
- METHOD_NOT_ALLOWED: "METHOD_NOT_ALLOWED",
10
- CONFLICT: "CONFLICT",
11
- RATE_LIMITED: "RATE_LIMITED",
12
- INTERNAL_ERROR: "INTERNAL_ERROR",
13
- LIST_LIMIT_EXCEEDED: "LIST_LIMIT_EXCEEDED",
14
- SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE"
15
- }, m = {
16
- INVALID_REQUEST: {
17
- httpStatus: 400,
18
- retryable: !1,
19
- outcome: "not_executed",
20
- meaning: "请求不符合接口契约"
21
- },
22
- UNAUTHENTICATED: {
23
- httpStatus: 401,
24
- retryable: !1,
25
- outcome: "not_executed",
26
- meaning: "未登录,或令牌无效、过期"
27
- },
28
- FORBIDDEN: {
29
- httpStatus: 403,
30
- retryable: !1,
31
- outcome: "not_executed",
32
- meaning: "已认证但无权执行"
33
- },
34
- NOT_FOUND: {
35
- httpStatus: 404,
36
- retryable: !1,
37
- outcome: "not_executed",
38
- meaning: "资源不存在,或调用方无权得知其存在"
39
- },
40
- ROUTE_NOT_FOUND: {
41
- httpStatus: 404,
42
- retryable: !1,
43
- outcome: "not_executed",
44
- meaning: "接口路径不存在"
45
- },
46
- METHOD_NOT_ALLOWED: {
47
- httpStatus: 405,
48
- retryable: !1,
49
- outcome: "not_executed",
50
- meaning: "接口不支持该 HTTP 方法"
51
- },
52
- CONFLICT: {
53
- httpStatus: 409,
54
- retryable: !1,
55
- outcome: "not_executed",
56
- meaning: "资源状态冲突"
57
- },
58
- RATE_LIMITED: {
59
- httpStatus: 429,
60
- retryable: !0,
61
- outcome: "not_executed",
62
- meaning: "请求过于频繁,可稍后重试"
63
- },
64
- INTERNAL_ERROR: {
65
- httpStatus: 500,
66
- retryable: !1,
67
- outcome: "unknown",
68
- meaning: "服务内部错误,可能已产生副作用"
69
- },
70
- LIST_LIMIT_EXCEEDED: {
71
- httpStatus: 500,
72
- retryable: !1,
73
- outcome: "unknown",
74
- meaning: "一次性列表的结果超出接口声明的上限"
75
- },
76
- SERVICE_UNAVAILABLE: {
77
- httpStatus: 503,
78
- retryable: !0,
79
- outcome: "unknown",
80
- meaning: "服务或其依赖暂不可用"
81
- }
82
- }, l = {
83
- items: "items"
84
- }, h = {
85
- after: "cursor.after",
86
- limit: "cursor.limit"
87
- }, E = {
88
- items: "items",
89
- next: "cursor.next",
90
- hasMore: "cursor.hasMore"
91
- }, S = {
92
- prefix: "filter.",
93
- operators: {
94
- eq: "eq"
95
- }
96
- }, _ = {
97
- page: "page.page",
98
- pageSize: "page.pageSize",
99
- query: "page.query",
100
- orderBy: "page.orderBy"
101
- }, T = {
102
- items: "items",
103
- totalRecord: "page.totalRecord",
104
- pageSize: "page.pageSize",
105
- totalPage: "page.totalPage"
106
3
  }, e = {
107
4
  $schema: "https://json-schema.org/draft/2020-12/schema",
108
5
  $id: "bounded-list.schema.json",
@@ -120,317 +17,6 @@ const d = {
120
17
  },
121
18
  required: ["items"]
122
19
  }, t = {
123
- $schema: "https://json-schema.org/draft/2020-12/schema",
124
- $id: "common-error-codes.schema.json",
125
- title: "CommonErrorCodes",
126
- description: "通用失败码表:每个码固定 HTTP 状态(x-httpStatus)、能否重试与是否已执行。作为约束使用时(错误体引用它):code 是通用失败码时,error.retryable 与 error.outcome 必须与本表一致。$defs/code 列出全部通用失败码,须与 allOf 各项一一对应。",
127
- $defs: {
128
- code: {
129
- description: "通用失败码。",
130
- enum: [
131
- "INVALID_REQUEST",
132
- "UNAUTHENTICATED",
133
- "FORBIDDEN",
134
- "NOT_FOUND",
135
- "ROUTE_NOT_FOUND",
136
- "METHOD_NOT_ALLOWED",
137
- "CONFLICT",
138
- "RATE_LIMITED",
139
- "INTERNAL_ERROR",
140
- "LIST_LIMIT_EXCEEDED",
141
- "SERVICE_UNAVAILABLE"
142
- ]
143
- }
144
- },
145
- allOf: [
146
- {
147
- description: "请求不符合接口契约",
148
- "x-httpStatus": 400,
149
- if: {
150
- properties: {
151
- code: {
152
- const: "INVALID_REQUEST"
153
- }
154
- },
155
- required: ["code"]
156
- },
157
- then: {
158
- properties: {
159
- error: {
160
- properties: {
161
- retryable: {
162
- const: !1
163
- },
164
- outcome: {
165
- const: "not_executed"
166
- }
167
- }
168
- }
169
- }
170
- }
171
- },
172
- {
173
- description: "未登录,或令牌无效、过期",
174
- "x-httpStatus": 401,
175
- if: {
176
- properties: {
177
- code: {
178
- const: "UNAUTHENTICATED"
179
- }
180
- },
181
- required: ["code"]
182
- },
183
- then: {
184
- properties: {
185
- error: {
186
- properties: {
187
- retryable: {
188
- const: !1
189
- },
190
- outcome: {
191
- const: "not_executed"
192
- }
193
- }
194
- }
195
- }
196
- }
197
- },
198
- {
199
- description: "已认证但无权执行",
200
- "x-httpStatus": 403,
201
- if: {
202
- properties: {
203
- code: {
204
- const: "FORBIDDEN"
205
- }
206
- },
207
- required: ["code"]
208
- },
209
- then: {
210
- properties: {
211
- error: {
212
- properties: {
213
- retryable: {
214
- const: !1
215
- },
216
- outcome: {
217
- const: "not_executed"
218
- }
219
- }
220
- }
221
- }
222
- }
223
- },
224
- {
225
- description: "资源不存在,或调用方无权得知其存在",
226
- "x-httpStatus": 404,
227
- if: {
228
- properties: {
229
- code: {
230
- const: "NOT_FOUND"
231
- }
232
- },
233
- required: ["code"]
234
- },
235
- then: {
236
- properties: {
237
- error: {
238
- properties: {
239
- retryable: {
240
- const: !1
241
- },
242
- outcome: {
243
- const: "not_executed"
244
- }
245
- }
246
- }
247
- }
248
- }
249
- },
250
- {
251
- description: "接口路径不存在",
252
- "x-httpStatus": 404,
253
- if: {
254
- properties: {
255
- code: {
256
- const: "ROUTE_NOT_FOUND"
257
- }
258
- },
259
- required: ["code"]
260
- },
261
- then: {
262
- properties: {
263
- error: {
264
- properties: {
265
- retryable: {
266
- const: !1
267
- },
268
- outcome: {
269
- const: "not_executed"
270
- }
271
- }
272
- }
273
- }
274
- }
275
- },
276
- {
277
- description: "接口不支持该 HTTP 方法",
278
- "x-httpStatus": 405,
279
- if: {
280
- properties: {
281
- code: {
282
- const: "METHOD_NOT_ALLOWED"
283
- }
284
- },
285
- required: ["code"]
286
- },
287
- then: {
288
- properties: {
289
- error: {
290
- properties: {
291
- retryable: {
292
- const: !1
293
- },
294
- outcome: {
295
- const: "not_executed"
296
- }
297
- }
298
- }
299
- }
300
- }
301
- },
302
- {
303
- description: "资源状态冲突",
304
- "x-httpStatus": 409,
305
- if: {
306
- properties: {
307
- code: {
308
- const: "CONFLICT"
309
- }
310
- },
311
- required: ["code"]
312
- },
313
- then: {
314
- properties: {
315
- error: {
316
- properties: {
317
- retryable: {
318
- const: !1
319
- },
320
- outcome: {
321
- const: "not_executed"
322
- }
323
- }
324
- }
325
- }
326
- }
327
- },
328
- {
329
- description: "请求过于频繁,可稍后重试",
330
- "x-httpStatus": 429,
331
- if: {
332
- properties: {
333
- code: {
334
- const: "RATE_LIMITED"
335
- }
336
- },
337
- required: ["code"]
338
- },
339
- then: {
340
- properties: {
341
- error: {
342
- properties: {
343
- retryable: {
344
- const: !0
345
- },
346
- outcome: {
347
- const: "not_executed"
348
- }
349
- }
350
- }
351
- }
352
- }
353
- },
354
- {
355
- description: "服务内部错误,可能已产生副作用",
356
- "x-httpStatus": 500,
357
- if: {
358
- properties: {
359
- code: {
360
- const: "INTERNAL_ERROR"
361
- }
362
- },
363
- required: ["code"]
364
- },
365
- then: {
366
- properties: {
367
- error: {
368
- properties: {
369
- retryable: {
370
- const: !1
371
- },
372
- outcome: {
373
- const: "unknown"
374
- }
375
- }
376
- }
377
- }
378
- }
379
- },
380
- {
381
- description: "一次性列表的结果超出接口声明的上限",
382
- "x-httpStatus": 500,
383
- if: {
384
- properties: {
385
- code: {
386
- const: "LIST_LIMIT_EXCEEDED"
387
- }
388
- },
389
- required: ["code"]
390
- },
391
- then: {
392
- properties: {
393
- error: {
394
- properties: {
395
- retryable: {
396
- const: !1
397
- },
398
- outcome: {
399
- const: "unknown"
400
- }
401
- }
402
- }
403
- }
404
- }
405
- },
406
- {
407
- description: "服务或其依赖暂不可用",
408
- "x-httpStatus": 503,
409
- if: {
410
- properties: {
411
- code: {
412
- const: "SERVICE_UNAVAILABLE"
413
- }
414
- },
415
- required: ["code"]
416
- },
417
- then: {
418
- properties: {
419
- error: {
420
- properties: {
421
- retryable: {
422
- const: !0
423
- },
424
- outcome: {
425
- const: "unknown"
426
- }
427
- }
428
- }
429
- }
430
- }
431
- }
432
- ]
433
- }, r = {
434
20
  $schema: "https://json-schema.org/draft/2020-12/schema",
435
21
  $id: "cursor-list-request.schema.json",
436
22
  title: "CursorListRequest",
@@ -446,14 +32,13 @@ const d = {
446
32
  maxLength: 1024
447
33
  },
448
34
  limit: {
449
- description: "本批最多取多少条;缺省值由各接口自定。",
35
+ description: "本批最多取多少条,至少 1;不带时取多少条、上限多少由接口自己声明。",
450
36
  type: "integer",
451
- minimum: 1,
452
- maximum: 100
37
+ minimum: 1
453
38
  }
454
39
  },
455
40
  additionalProperties: !1
456
- }, o = {
41
+ }, r = {
457
42
  $schema: "https://json-schema.org/draft/2020-12/schema",
458
43
  $id: "cursor-list.schema.json",
459
44
  title: "CursorList",
@@ -507,7 +92,7 @@ const d = {
507
92
  $schema: "https://json-schema.org/draft/2020-12/schema",
508
93
  $id: "error-body.schema.json",
509
94
  title: "ErrorBody",
510
- description: "错误体。HTTP 状态表达大类,包体带业务码、说明、能否重试、是否已执行与出错字段定位。code 是通用失败码时,retryable 与 outcome 必须与通用失败码表一致。",
95
+ description: "错误体。HTTP 状态表达大类,包体带业务码、说明、能否重试、是否已执行与出错字段定位。retryable 与 outcome 由服务按实际情形填写,包不替使用方填写;通用失败码表给了一组参考值,使用方可显式引用。",
511
96
  type: "object",
512
97
  properties: {
513
98
  code: {
@@ -543,11 +128,6 @@ const d = {
543
128
  },
544
129
  required: ["code", "message", "data", "error", "requestId"],
545
130
  additionalProperties: !1,
546
- allOf: [
547
- {
548
- $ref: "common-error-codes.schema.json"
549
- }
550
- ],
551
131
  $defs: {
552
132
  errorDetail: {
553
133
  title: "ErrorDetail",
@@ -658,7 +238,7 @@ const d = {
658
238
  additionalProperties: !1
659
239
  }
660
240
  }
661
- }, a = {
241
+ }, o = {
662
242
  $schema: "https://json-schema.org/draft/2020-12/schema",
663
243
  $id: "paged-list-request.schema.json",
664
244
  title: "PagedListRequest",
@@ -668,17 +248,14 @@ const d = {
668
248
  type: "object",
669
249
  properties: {
670
250
  page: {
671
- description: "页码,从 1 开始。",
251
+ description: "页码,从 1 开始;不带时的含义由接口自己声明。",
672
252
  type: "integer",
673
- minimum: 1,
674
- default: 1
253
+ minimum: 1
675
254
  },
676
255
  pageSize: {
677
- description: "每页条数。",
256
+ description: "每页条数,至少 1;不带时的含义与上限由接口自己声明。",
678
257
  type: "integer",
679
- minimum: 1,
680
- maximum: 100,
681
- default: 10
258
+ minimum: 1
682
259
  },
683
260
  query: {
684
261
  description: "模糊搜索词,最多 200 个字符。",
@@ -693,11 +270,11 @@ const d = {
693
270
  }
694
271
  },
695
272
  additionalProperties: !1
696
- }, n = {
273
+ }, a = {
697
274
  $schema: "https://json-schema.org/draft/2020-12/schema",
698
275
  $id: "paged-list.schema.json",
699
276
  title: "PagedList",
700
- description: "页码式列表,作成功外壳的 data:给人翻页的表格,能跳页、有总数。顶层允许接口自己声明的附带字段,但不得重复表达翻页信息;不带 cursor。",
277
+ description: "页码式列表,作成功外壳的 data:给人翻页的表格,能跳页、有总数。顶层允许接口自己声明的附带字段,但不得重复表达翻页信息;不带 cursor。items 的条数不超过 page.pageSize,也不超过 page.totalRecord(x-itemsWithinPage)。",
701
278
  "x-pathConstants": "PAGED_LIST_RESPONSE_PATHS",
702
279
  type: "object",
703
280
  properties: {
@@ -711,6 +288,7 @@ const d = {
711
288
  cursor: !1
712
289
  },
713
290
  required: ["items", "page"],
291
+ "x-itemsWithinPage": !0,
714
292
  $defs: {
715
293
  page: {
716
294
  title: "PagedListPage",
@@ -723,10 +301,9 @@ const d = {
723
301
  minimum: 0
724
302
  },
725
303
  pageSize: {
726
- description: "每页条数,回显请求值。",
304
+ description: "每页条数,回显请求值,至少 1。",
727
305
  type: "integer",
728
- minimum: 1,
729
- maximum: 100
306
+ minimum: 1
730
307
  },
731
308
  totalPage: {
732
309
  description: "总页数。",
@@ -754,11 +331,11 @@ const d = {
754
331
  "x-totalPageEqualsCeil": !0
755
332
  }
756
333
  }
757
- }, c = {
334
+ }, n = {
758
335
  $schema: "https://json-schema.org/draft/2020-12/schema",
759
336
  $id: "service-error-codes.schema.json",
760
337
  title: "ServiceErrorCodes",
761
- description: "一个服务的专有业务码清单。不得重新定义通用码;OK 与 HTTP_<数字> 形式的码保留,不得使用;比较按字节精确进行,不做大小写归一。",
338
+ description: "一个服务的专有业务码清单。OK 与 HTTP_<数字> 形式的码是结构保留字,不得使用;比较按字节精确进行,不做大小写归一。专有码是否要避开某组通用码由使用方显式检查:把那组码传给参考资源出口的检查函数,不传就不比。",
762
339
  type: "object",
763
340
  properties: {
764
341
  service: {
@@ -800,11 +377,6 @@ const d = {
800
377
  type: "string",
801
378
  pattern: "^HTTP_[0-9]+$"
802
379
  }
803
- },
804
- {
805
- not: {
806
- $ref: "common-error-codes.schema.json#/$defs/code"
807
- }
808
380
  }
809
381
  ]
810
382
  },
@@ -893,37 +465,27 @@ const d = {
893
465
  },
894
466
  required: ["code", "message", "data"],
895
467
  additionalProperties: !1
896
- }, g = {
468
+ }, d = {
897
469
  "bounded-list.schema.json": e,
898
- "common-error-codes.schema.json": t,
899
- "cursor-list-request.schema.json": r,
900
- "cursor-list.schema.json": o,
470
+ "cursor-list-request.schema.json": t,
471
+ "cursor-list.schema.json": r,
901
472
  "error-body.schema.json": s,
902
473
  "list-filter.schema.json": i,
903
- "paged-list-request.schema.json": a,
904
- "paged-list.schema.json": n,
905
- "service-error-codes.schema.json": c,
474
+ "paged-list-request.schema.json": o,
475
+ "paged-list.schema.json": a,
476
+ "service-error-codes.schema.json": n,
906
477
  "success-envelope.schema.json": p
907
478
  };
908
479
  export {
909
- l as BOUNDED_LIST_RESPONSE_PATHS,
910
- u as COMMON_ERROR_CODES,
911
- m as COMMON_ERROR_CODE_TABLE,
912
- h as CURSOR_LIST_REQUEST_PARAMS,
913
- E as CURSOR_LIST_RESPONSE_PATHS,
914
- S as LIST_FILTER_PARAMS,
915
- _ as PAGED_LIST_REQUEST_PARAMS,
916
- T as PAGED_LIST_RESPONSE_PATHS,
917
- d as SUCCESS_CODES,
480
+ c as SUCCESS_CODES,
918
481
  e as boundedListSchema,
919
- t as commonErrorCodesSchema,
920
- r as cursorListRequestSchema,
921
- o as cursorListSchema,
482
+ t as cursorListRequestSchema,
483
+ r as cursorListSchema,
922
484
  s as errorBodySchema,
923
485
  i as listFilterSchema,
924
- a as pagedListRequestSchema,
925
- n as pagedListSchema,
926
- g as schemas,
927
- c as serviceErrorCodesSchema,
486
+ o as pagedListRequestSchema,
487
+ a as pagedListSchema,
488
+ d as schemas,
489
+ n as serviceErrorCodesSchema,
928
490
  p as successEnvelopeSchema
929
491
  };