@youzan-cloud/zan-saas-cli 0.0.0-stage → 0.1.0-beta.3
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/DEPENDENCIES.json +50 -0
- package/INSTALL.md +79 -0
- package/LICENSE +21 -0
- package/NOTICE +1 -0
- package/README.md +151 -2
- package/THIRD_PARTY_NOTICES.txt +162 -0
- package/bin/zan-saas-cli.js +28 -0
- package/internal/registry/index.js +123 -0
- package/internal/registry/manifest.json +424 -0
- package/npm-shrinkwrap.json +86 -0
- package/package.json +33 -4
- package/skills/README.md +30 -0
- package/skills/zan-saas/SKILL.md +54 -0
- package/skills/zan-saas/references/host-entry.md +25 -0
- package/skills/zan-saas-access/SKILL.md +37 -0
- package/skills/zan-saas-access/references/role-list.md +11 -0
- package/skills/zan-saas-access/references/staff-perms-get.md +11 -0
- package/skills/zan-saas-aftersale/SKILL.md +31 -0
- package/skills/zan-saas-aftersale/references/case-list.md +13 -0
- package/skills/zan-saas-aftersale/references/case-scan.md +31 -0
- package/skills/zan-saas-benefit/SKILL.md +46 -0
- package/skills/zan-saas-benefit/references/card-get.md +7 -0
- package/skills/zan-saas-benefit/references/card-grant-list.md +11 -0
- package/skills/zan-saas-benefit/references/card-list.md +7 -0
- package/skills/zan-saas-benefit/references/customer-card-get.md +7 -0
- package/skills/zan-saas-benefit/references/customer-card-list.md +7 -0
- package/skills/zan-saas-coupon/SKILL.md +41 -0
- package/skills/zan-saas-coupon/references/activity-get.md +11 -0
- package/skills/zan-saas-coupon/references/activity-list.md +15 -0
- package/skills/zan-saas-coupon/references/voucher-get.md +13 -0
- package/skills/zan-saas-coupon/references/voucher-list.md +25 -0
- package/skills/zan-saas-customer/SKILL.md +73 -0
- package/skills/zan-saas-customer/references/customer-scan.md +11 -0
- package/skills/zan-saas-customer/references/segment-list.md +22 -0
- package/skills/zan-saas-fulfillment/SKILL.md +36 -0
- package/skills/zan-saas-fulfillment/references/delivery-get.md +11 -0
- package/skills/zan-saas-fulfillment/references/shipment-list.md +11 -0
- package/skills/zan-saas-fulfillment/references/waybill-get.md +9 -0
- package/skills/zan-saas-interaction/SKILL.md +37 -0
- package/skills/zan-saas-interaction/references/lottery-get.md +11 -0
- package/skills/zan-saas-interaction/references/lottery-list.md +19 -0
- package/skills/zan-saas-item/SKILL.md +61 -0
- package/skills/zan-saas-item/references/attribute-list.md +11 -0
- package/skills/zan-saas-item/references/brand-list.md +7 -0
- package/skills/zan-saas-item/references/classification-get.md +7 -0
- package/skills/zan-saas-item/references/classification-list.md +7 -0
- package/skills/zan-saas-item/references/group-list.md +9 -0
- package/skills/zan-saas-item/references/item-content.md +9 -0
- package/skills/zan-saas-item/references/item-get.md +9 -0
- package/skills/zan-saas-item/references/item-search.md +23 -0
- package/skills/zan-saas-item/references/product-identity.md +11 -0
- package/skills/zan-saas-member/SKILL.md +41 -0
- package/skills/zan-saas-member/references/customer-level-get.md +7 -0
- package/skills/zan-saas-member/references/growth-log-list.md +17 -0
- package/skills/zan-saas-member/references/membership-get.md +7 -0
- package/skills/zan-saas-order/SKILL.md +56 -0
- package/skills/zan-saas-order/references/order-get.md +26 -0
- package/skills/zan-saas-order/references/order-list.md +25 -0
- package/skills/zan-saas-order/references/order-scan.md +35 -0
- package/skills/zan-saas-org/SKILL.md +37 -0
- package/skills/zan-saas-org/references/staff-get.md +15 -0
- package/skills/zan-saas-org/references/staff-role-get.md +11 -0
- package/skills/zan-saas-org/references/unit-get.md +11 -0
- package/skills/zan-saas-org/references/unit-list.md +15 -0
- package/skills/zan-saas-points/SKILL.md +42 -0
- package/skills/zan-saas-points/references/account-get.md +7 -0
- package/skills/zan-saas-points/references/log-list.md +17 -0
- package/skills/zan-saas-points/references/rule-list.md +2 -0
- package/skills/zan-saas-privacy/SKILL.md +30 -0
- package/skills/zan-saas-privacy/references/field-decrypt.md +19 -0
- package/skills/zan-saas-shared/SKILL.md +94 -0
- package/skills/zan-saas-shared/references/authentication.md +52 -0
- package/skills/zan-saas-shared/references/value-processing.md +27 -0
- package/skills/zan-saas-stock/SKILL.md +28 -0
- package/skills/zan-saas-stock/references/log-list.md +11 -0
- package/skills/zan-saas-valuecard/SKILL.md +37 -0
- package/skills/zan-saas-valuecard/references/card-get.md +13 -0
- package/skills/zan-saas-valuecard/references/card-list.md +11 -0
- package/src/auth/browser.js +28 -0
- package/src/auth/callback-server.js +99 -0
- package/src/auth/config.js +53 -0
- package/src/auth/credential-store.js +109 -0
- package/src/auth/oauth-client.js +105 -0
- package/src/auth/pkce.js +28 -0
- package/src/auth/session.js +178 -0
- package/src/auth/tool.js +25 -0
- package/src/cli/command-paths.js +126 -0
- package/src/cli/context-help.js +102 -0
- package/src/cli/diagnostic-command.js +57 -0
- package/src/cli/domain-catalog.js +42 -0
- package/src/cli/help-text.js +32 -0
- package/src/cli/native-loader.js +120 -0
- package/src/cli/native-path.js +22 -0
- package/src/cli/parser-policy.js +141 -0
- package/src/cli/program.js +134 -0
- package/src/cli/skill-catalog.js +241 -0
- package/src/commands/native/access/commands.index.json +24 -0
- package/src/commands/native/access/domain.json +3 -0
- package/src/commands/native/access/role/list/command.js +141 -0
- package/src/commands/native/access/role/list/schema.json +393 -0
- package/src/commands/native/access/staff-perms/get/command.js +119 -0
- package/src/commands/native/access/staff-perms/get/schema.json +703 -0
- package/src/commands/native/aftersale/case/get/command.js +104 -0
- package/src/commands/native/aftersale/case/get/exact-json.js +52 -0
- package/src/commands/native/aftersale/case/get/money.js +28 -0
- package/src/commands/native/aftersale/case/get/projection.js +120 -0
- package/src/commands/native/aftersale/case/get/query.js +72 -0
- package/src/commands/native/aftersale/case/get/schema.json +134 -0
- package/src/commands/native/aftersale/case/list/command.js +135 -0
- package/src/commands/native/aftersale/case/list/schema.json +1095 -0
- package/src/commands/native/aftersale/case/scan/command.js +110 -0
- package/src/commands/native/aftersale/case/scan/query.js +62 -0
- package/src/commands/native/aftersale/case/scan/scan.js +78 -0
- package/src/commands/native/aftersale/case/scan/schema.json +964 -0
- package/src/commands/native/aftersale/case/scan/search.js +108 -0
- package/src/commands/native/aftersale/commands.index.json +42 -0
- package/src/commands/native/aftersale/domain.json +3 -0
- package/src/commands/native/aftersale/refund/list/command.js +97 -0
- package/src/commands/native/aftersale/refund/list/money.js +25 -0
- package/src/commands/native/aftersale/refund/list/projection.js +50 -0
- package/src/commands/native/aftersale/refund/list/query.js +73 -0
- package/src/commands/native/aftersale/refund/list/schema.json +130 -0
- package/src/commands/native/aftersale/shared/query.js +58 -0
- package/src/commands/native/aftersale/shared/search.js +128 -0
- package/src/commands/native/benefit/card-definition/get/command.js +110 -0
- package/src/commands/native/benefit/card-definition/get/schema.json +545 -0
- package/src/commands/native/benefit/card-definition/list/command.js +105 -0
- package/src/commands/native/benefit/card-definition/list/schema.json +690 -0
- package/src/commands/native/benefit/card-grant/list/command.js +135 -0
- package/src/commands/native/benefit/card-grant/list/schema.json +758 -0
- package/src/commands/native/benefit/commands.index.json +51 -0
- package/src/commands/native/benefit/customer-card/get/command.js +110 -0
- package/src/commands/native/benefit/customer-card/get/schema.json +340 -0
- package/src/commands/native/benefit/customer-card/list/command.js +102 -0
- package/src/commands/native/benefit/customer-card/list/schema.json +731 -0
- package/src/commands/native/benefit/domain.json +3 -0
- package/src/commands/native/coupon/activity/get/command.js +183 -0
- package/src/commands/native/coupon/activity/get/schema.json +624 -0
- package/src/commands/native/coupon/activity/list/command.js +169 -0
- package/src/commands/native/coupon/activity/list/schema.json +845 -0
- package/src/commands/native/coupon/commands.index.json +42 -0
- package/src/commands/native/coupon/domain.json +3 -0
- package/src/commands/native/coupon/shared/query.js +15 -0
- package/src/commands/native/coupon/voucher/get/command.js +113 -0
- package/src/commands/native/coupon/voucher/get/schema.json +147 -0
- package/src/commands/native/coupon/voucher/list/command.js +155 -0
- package/src/commands/native/coupon/voucher/list/schema.json +772 -0
- package/src/commands/native/customer/commands.index.json +96 -0
- package/src/commands/native/customer/customer/get/adapter.js +82 -0
- package/src/commands/native/customer/customer/get/command.js +106 -0
- package/src/commands/native/customer/customer/get/schema.json +958 -0
- package/src/commands/native/customer/customer/list/command.js +78 -0
- package/src/commands/native/customer/customer/list/input.js +145 -0
- package/src/commands/native/customer/customer/list/query.js +103 -0
- package/src/commands/native/customer/customer/list/schema.json +969 -0
- package/src/commands/native/customer/customer/scan/command.js +80 -0
- package/src/commands/native/customer/customer/scan/scan.js +92 -0
- package/src/commands/native/customer/customer/scan/schema.json +211 -0
- package/src/commands/native/customer/customer-tag/get/command.js +166 -0
- package/src/commands/native/customer/customer-tag/get/schema.json +317 -0
- package/src/commands/native/customer/customer-tag/list/command.js +143 -0
- package/src/commands/native/customer/customer-tag/list/schema.json +259 -0
- package/src/commands/native/customer/domain.json +3 -0
- package/src/commands/native/customer/segment/list/command.js +96 -0
- package/src/commands/native/customer/segment/list/query.js +98 -0
- package/src/commands/native/customer/segment/list/schema.json +135 -0
- package/src/commands/native/customer/segment-customer/list/command.js +171 -0
- package/src/commands/native/customer/segment-customer/list/schema.json +123 -0
- package/src/commands/native/customer/shared/search-input.js +179 -0
- package/src/commands/native/customer/shared/search-page.js +54 -0
- package/src/commands/native/customer/tag/get/command.js +73 -0
- package/src/commands/native/customer/tag/get/schema.json +339 -0
- package/src/commands/native/customer/tagcategory/get/command.js +80 -0
- package/src/commands/native/customer/tagcategory/get/query.js +98 -0
- package/src/commands/native/customer/tagcategory/get/schema.json +339 -0
- package/src/commands/native/customer/tagcategory/list/command.js +127 -0
- package/src/commands/native/customer/tagcategory/list/schema.json +394 -0
- package/src/commands/native/fulfillment/commands.index.json +33 -0
- package/src/commands/native/fulfillment/delivery/get/command.js +107 -0
- package/src/commands/native/fulfillment/delivery/get/projection.js +142 -0
- package/src/commands/native/fulfillment/delivery/get/schema.json +173 -0
- package/src/commands/native/fulfillment/domain.json +3 -0
- package/src/commands/native/fulfillment/shipment/list/command.js +83 -0
- package/src/commands/native/fulfillment/shipment/list/projection.js +130 -0
- package/src/commands/native/fulfillment/shipment/list/schema.json +752 -0
- package/src/commands/native/fulfillment/waybill/get/command.js +193 -0
- package/src/commands/native/fulfillment/waybill/get/response.js +68 -0
- package/src/commands/native/fulfillment/waybill/get/schema.json +190 -0
- package/src/commands/native/interaction/commands.index.json +24 -0
- package/src/commands/native/interaction/domain.json +3 -0
- package/src/commands/native/interaction/lottery/get/command.js +61 -0
- package/src/commands/native/interaction/lottery/get/query.js +152 -0
- package/src/commands/native/interaction/lottery/get/schema.json +1004 -0
- package/src/commands/native/interaction/lottery/list/command.js +176 -0
- package/src/commands/native/interaction/lottery/list/schema.json +898 -0
- package/src/commands/native/item/attribute/list/command.js +122 -0
- package/src/commands/native/item/attribute/list/schema.json +727 -0
- package/src/commands/native/item/brand/list/command.js +114 -0
- package/src/commands/native/item/brand/list/schema.json +725 -0
- package/src/commands/native/item/classification/get/command.js +87 -0
- package/src/commands/native/item/classification/get/schema.json +596 -0
- package/src/commands/native/item/classification/list/command.js +105 -0
- package/src/commands/native/item/classification/list/schema.json +676 -0
- package/src/commands/native/item/commands.index.json +87 -0
- package/src/commands/native/item/domain.json +1 -0
- package/src/commands/native/item/group/list/command.js +122 -0
- package/src/commands/native/item/group/list/schema.json +809 -0
- package/src/commands/native/item/item/get/command.js +122 -0
- package/src/commands/native/item/item/get/schema.json +3799 -0
- package/src/commands/native/item/item/list/command.js +7 -0
- package/src/commands/native/item/item/list/schema.json +1118 -0
- package/src/commands/native/item/item/scan/command.js +7 -0
- package/src/commands/native/item/item/scan/schema.json +1046 -0
- package/src/commands/native/item/item-content/get/command.js +54 -0
- package/src/commands/native/item/item-content/get/schema.json +677 -0
- package/src/commands/native/item/shared/api.js +31 -0
- package/src/commands/native/item/shared/content.js +20 -0
- package/src/commands/native/item/shared/diagnostics.js +61 -0
- package/src/commands/native/item/shared/h5.js +63 -0
- package/src/commands/native/item/shared/identity.js +62 -0
- package/src/commands/native/item/shared/normalize.js +49 -0
- package/src/commands/native/item/shared/scope.js +22 -0
- package/src/commands/native/item/shared/search-command.js +61 -0
- package/src/commands/native/item/shared/search-options.js +150 -0
- package/src/commands/native/item/shared/search.js +158 -0
- package/src/commands/native/member/commands.index.json +60 -0
- package/src/commands/native/member/customer-level/get/command.js +122 -0
- package/src/commands/native/member/customer-level/get/schema.json +300 -0
- package/src/commands/native/member/domain.json +3 -0
- package/src/commands/native/member/growth/get/command.js +64 -0
- package/src/commands/native/member/growth/get/schema.json +283 -0
- package/src/commands/native/member/growth-log/list/command.js +203 -0
- package/src/commands/native/member/growth-log/list/schema.json +695 -0
- package/src/commands/native/member/level/get/command.js +70 -0
- package/src/commands/native/member/level/get/query.js +125 -0
- package/src/commands/native/member/level/get/schema.json +339 -0
- package/src/commands/native/member/level/list/command.js +50 -0
- package/src/commands/native/member/level/list/query.js +109 -0
- package/src/commands/native/member/level/list/schema.json +104 -0
- package/src/commands/native/member/membership/get/command.js +78 -0
- package/src/commands/native/member/membership/get/schema.json +283 -0
- package/src/commands/native/order/commands.index.json +33 -0
- package/src/commands/native/order/domain.json +3 -0
- package/src/commands/native/order/order/get/command.js +155 -0
- package/src/commands/native/order/order/get/schema.json +1167 -0
- package/src/commands/native/order/order/list/command.js +230 -0
- package/src/commands/native/order/order/list/schema.json +1363 -0
- package/src/commands/native/order/order/scan/command.js +148 -0
- package/src/commands/native/order/order/scan/scan.js +89 -0
- package/src/commands/native/order/order/scan/schema.json +1445 -0
- package/src/commands/native/order/shared/projection.js +183 -0
- package/src/commands/native/order/shared/query.js +84 -0
- package/src/commands/native/order/shared/search.js +112 -0
- package/src/commands/native/org/commands.index.json +42 -0
- package/src/commands/native/org/domain.json +3 -0
- package/src/commands/native/org/shared/single-staff.js +115 -0
- package/src/commands/native/org/staff/get/command.js +148 -0
- package/src/commands/native/org/staff/get/schema.json +114 -0
- package/src/commands/native/org/staff-role/get/command.js +45 -0
- package/src/commands/native/org/staff-role/get/schema.json +93 -0
- package/src/commands/native/org/unit/get/command.js +131 -0
- package/src/commands/native/org/unit/get/schema.json +478 -0
- package/src/commands/native/org/unit/list/command.js +187 -0
- package/src/commands/native/org/unit/list/schema.json +828 -0
- package/src/commands/native/points/account/get/command.js +65 -0
- package/src/commands/native/points/account/get/schema.json +303 -0
- package/src/commands/native/points/commands.index.json +33 -0
- package/src/commands/native/points/domain.json +3 -0
- package/src/commands/native/points/log/list/command.js +141 -0
- package/src/commands/native/points/log/list/schema.json +761 -0
- package/src/commands/native/points/rule/list/command.js +82 -0
- package/src/commands/native/points/rule/list/schema.json +294 -0
- package/src/commands/native/privacy/commands.index.json +15 -0
- package/src/commands/native/privacy/domain.json +3 -0
- package/src/commands/native/privacy/field/decrypt/command.js +185 -0
- package/src/commands/native/privacy/field/decrypt/schema.json +656 -0
- package/src/commands/native/privacy/shared/order-source.js +33 -0
- package/src/commands/native/stock/commands.index.json +15 -0
- package/src/commands/native/stock/domain.json +3 -0
- package/src/commands/native/stock/log/list/command.js +207 -0
- package/src/commands/native/stock/log/list/schema.json +922 -0
- package/src/commands/native/valuecard/card/get/command.js +107 -0
- package/src/commands/native/valuecard/card/get/schema.json +683 -0
- package/src/commands/native/valuecard/card/list/command.js +136 -0
- package/src/commands/native/valuecard/card/list/pagination.js +23 -0
- package/src/commands/native/valuecard/card/list/schema.json +780 -0
- package/src/commands/native/valuecard/commands.index.json +24 -0
- package/src/commands/native/valuecard/domain.json +3 -0
- package/src/commands/native/valuecard/shared/projection.js +57 -0
- package/src/commands/system/api.js +47 -0
- package/src/commands/system/auth.js +118 -0
- package/src/commands/system/contracts.js +373 -0
- package/src/commands/system/doctor.js +24 -0
- package/src/commands/system/install.js +41 -0
- package/src/commands/system/printer.js +16 -0
- package/src/commands/system/schema.js +162 -0
- package/src/commands/system/shop-guidance.js +9 -0
- package/src/commands/system/shop.js +237 -0
- package/src/commands/system/skills.js +49 -0
- package/src/execution/beijing-date-time.js +29 -0
- package/src/execution/beijing-time.js +21 -0
- package/src/execution/bounded-input.js +42 -0
- package/src/execution/command-io.js +137 -0
- package/src/execution/command-policy.js +41 -0
- package/src/execution/command-schema.js +131 -0
- package/src/execution/datetime.js +83 -0
- package/src/execution/error-details.js +399 -0
- package/src/execution/error-model.js +82 -0
- package/src/execution/exact-json.js +80 -0
- package/src/execution/list-total.js +10 -0
- package/src/execution/money.js +59 -0
- package/src/execution/parser-error.js +66 -0
- package/src/execution/privacy.js +197 -0
- package/src/execution/public-schema.js +52 -0
- package/src/execution/query-api.js +101 -0
- package/src/execution/range.js +84 -0
- package/src/execution/request-state.js +55 -0
- package/src/execution/schema-completeness.js +116 -0
- package/src/execution/shop-query.js +54 -0
- package/src/execution/target-shop-default.js +23 -0
- package/src/execution/target-shop.js +49 -0
- package/src/identity/context-store.js +54 -0
- package/src/identity/context.js +38 -0
- package/src/installation/install.js +158 -0
- package/src/installation/npm-error.js +93 -0
- package/src/installation/skill.js +113 -0
- package/src/transport/account-api.js +152 -0
- package/src/transport/api.js +199 -0
- package/src/transport/shop-app.js +63 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Ajv = require('ajv');
|
|
4
|
+
const { diagnosticDefinitions } = require('./error-model');
|
|
5
|
+
|
|
6
|
+
const object = properties => ({
|
|
7
|
+
type: 'object', additionalProperties: false, required: Object.keys(properties), properties
|
|
8
|
+
});
|
|
9
|
+
const argv = {
|
|
10
|
+
type: 'array', minItems: 1,
|
|
11
|
+
items: { type: 'string', pattern: '^(?:[a-z][a-z0-9-]*|\\+[a-z][a-z0-9-]*|--help)$' },
|
|
12
|
+
description: '从当前安装版注册树生成的参数数组,不含可执行文件名或收到的参数值。'
|
|
13
|
+
};
|
|
14
|
+
const issue = structuredClone(diagnosticDefinitions.diagnosticIssue);
|
|
15
|
+
issue.properties.source = { const: 'argv', description: '命令行解析阶段的问题。' };
|
|
16
|
+
issue.properties.expected = structuredClone(diagnosticDefinitions.diagnosticExpected);
|
|
17
|
+
const details = object({
|
|
18
|
+
category: { const: 'parameter', description: 'CLI 命令或参数不符合调用要求。' },
|
|
19
|
+
stage: { const: 'input', description: '业务动作执行前的命令行解析阶段。' },
|
|
20
|
+
command: { type: 'string', description: '最近已识别节点的点分路径;根路径为空字符串。' },
|
|
21
|
+
targetScope: object({}),
|
|
22
|
+
requestState: { const: 'not_sent', description: '本地解析拒绝,尚未发送请求。' },
|
|
23
|
+
errorVersion: { const: 2, description: '公共解析错误复用第二版诊断字段。' },
|
|
24
|
+
issues: { type: 'array', minItems: 1, maxItems: 16, items: issue,
|
|
25
|
+
description: '已定位的参数问题,不回显原始输入。' },
|
|
26
|
+
helpArgv: { ...argv, description: '最近已识别节点的帮助参数,不含可执行文件名。' },
|
|
27
|
+
recovery: structuredClone(diagnosticDefinitions.diagnosticRecovery),
|
|
28
|
+
execution: object({
|
|
29
|
+
requestState: { const: 'not_sent' },
|
|
30
|
+
coverage: { const: 'complete' },
|
|
31
|
+
observedAttempts: { const: 0 }
|
|
32
|
+
}),
|
|
33
|
+
nextStep: { type: 'string', description: '从本地命令契约生成的安全修正或帮助提示。' }
|
|
34
|
+
});
|
|
35
|
+
details.properties.suggestions = {
|
|
36
|
+
type: 'array', minItems: 1, maxItems: 3, items: argv,
|
|
37
|
+
description: '可靠候选的完整参数数组;没有可靠候选时省略。仅提示,不自动执行。'
|
|
38
|
+
};
|
|
39
|
+
const parserErrorModel = {
|
|
40
|
+
...object({
|
|
41
|
+
success: { const: false }, code: { const: -10001 },
|
|
42
|
+
message: { type: 'string', description: '简短中文摘要;机器判断使用 issues.reason。' },
|
|
43
|
+
details
|
|
44
|
+
}),
|
|
45
|
+
description: '公共命令行解析错误,stdout 为空,stderr 单行 JSON,退出 1。已有叶子的专用诊断可继续使用其 error 模型。'
|
|
46
|
+
};
|
|
47
|
+
const validate = new Ajv({ strict: true, allErrors: true }).compile(parserErrorModel);
|
|
48
|
+
|
|
49
|
+
class ParserError extends Error {
|
|
50
|
+
constructor(message, facts) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.result = {
|
|
53
|
+
success: false, code: -10001, message,
|
|
54
|
+
details: {
|
|
55
|
+
category: 'parameter', stage: 'input', targetScope: {}, requestState: 'not_sent',
|
|
56
|
+
errorVersion: 2,
|
|
57
|
+
recovery: { action: 'correct_input', automaticBusinessRetry: false },
|
|
58
|
+
execution: { requestState: 'not_sent', coverage: 'complete', observedAttempts: 0 },
|
|
59
|
+
...facts
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
if (!validate(this.result)) throw new Error('公共解析错误不符合诊断契约');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = { ParserError, parserErrorModel };
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { snapshotApiEnvironment } = require('../transport/api');
|
|
4
|
+
const { createShopQuery, failure } = require('./shop-query');
|
|
5
|
+
const { object } = require('./query-api');
|
|
6
|
+
const { copyErrorDetails } = require('./error-details');
|
|
7
|
+
const { incompleteCoverage } = require('./request-state');
|
|
8
|
+
|
|
9
|
+
const RESPONSE_LIMIT = 10 * 1024 * 1024;
|
|
10
|
+
const OUTPUT_LIMIT = 5 * 1024 * 1024;
|
|
11
|
+
const paths = Object.freeze({
|
|
12
|
+
verifySingle: 'youzan.cloud.secret.verify.isencrypt/1.0.0',
|
|
13
|
+
verifyBatch: 'youzan.cloud.secret.verify.isencryptlist/1.0.0',
|
|
14
|
+
decryptSingle: 'youzan.cloud.secret.decrypt.single/1.0.0',
|
|
15
|
+
decryptBatch: 'youzan.cloud.secret.decrypt.batch/1.0.0'
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
function validReason(value) {
|
|
19
|
+
return typeof value === 'string' && value === value.trim() &&
|
|
20
|
+
[...value].length >= 1 && [...value].length <= 200 && !/[\p{Cc}\p{Zl}\p{Zp}]/u.test(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function privacyFailure(command, scope, category, stage, state = 'not_sent', facts = {}) {
|
|
24
|
+
const error = failure(command, category, stage, scope, state, facts);
|
|
25
|
+
error.cliDetails.nextStep = '停止明文披露。核对原员工会话、应用、店铺、对象字段权限和可靠访问记录;不自动重试、换身份或改用其他入口。';
|
|
26
|
+
return error;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function privacyError(command, category, stage, scope = {}, requestState = 'not_sent', facts = {}) {
|
|
30
|
+
return privacyFailure(command, scope, category, stage, requestState, facts);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function safeFailure(error, command, scope, stage, state, independent = false) {
|
|
34
|
+
const details = error?.cliDetails;
|
|
35
|
+
const facts = {};
|
|
36
|
+
if (Number.isInteger(details?.httpStatus)) facts.httpStatus = details.httpStatus;
|
|
37
|
+
for (const key of ['businessCode', 'gatewayCode']) {
|
|
38
|
+
if (Number.isSafeInteger(details?.[key])) facts[key] = details[key];
|
|
39
|
+
}
|
|
40
|
+
const api = details?.api;
|
|
41
|
+
if (Object.values(paths).includes(api) || api === 'youzan.shop.basic.get/3.0.0' ||
|
|
42
|
+
api === 'youzan.trade.get/4.0.2') facts.api = api;
|
|
43
|
+
if (!facts.api) {
|
|
44
|
+
const stageApi = {
|
|
45
|
+
'shop-binding': 'youzan.shop.basic.get/3.0.0',
|
|
46
|
+
source: 'youzan.trade.get/4.0.2',
|
|
47
|
+
'source-object': 'youzan.trade.get/4.0.2',
|
|
48
|
+
'ciphertext-verification': paths.verifySingle,
|
|
49
|
+
verify: paths.verifySingle,
|
|
50
|
+
decryption: paths.decryptSingle,
|
|
51
|
+
decrypt: paths.decryptSingle
|
|
52
|
+
};
|
|
53
|
+
if (stageApi[stage]) facts.api = stageApi[stage];
|
|
54
|
+
}
|
|
55
|
+
const categories = ['configuration', 'authentication', 'permission', 'scope_unresolved',
|
|
56
|
+
'http', 'business_rejected', 'transport', 'protocol', 'limit'];
|
|
57
|
+
const failure = privacyFailure(command, scope, categories.includes(details?.category) ? details.category : 'protocol',
|
|
58
|
+
independent && details?.stage ? details.stage : stage,
|
|
59
|
+
['not_sent', 'sent', 'unknown'].includes(details?.requestState) ? details.requestState : state, facts);
|
|
60
|
+
if (details) {
|
|
61
|
+
const safe = copyErrorDetails(details);
|
|
62
|
+
for (const key of ['issues', 'issuesTruncated', 'errorVersion', 'recovery', 'execution']) {
|
|
63
|
+
if (Object.hasOwn(safe, key)) failure.cliDetails[key] = safe[key];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (error.diagnosticReason) failure.diagnosticReason = error.diagnosticReason;
|
|
67
|
+
return failure;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function discloseFields(input, dependencies) {
|
|
71
|
+
const { command, allowedFields, readSource, validateResult, provider } = dependencies;
|
|
72
|
+
const scope = Object.freeze({ kdtId: input?.scope?.kdtId });
|
|
73
|
+
const fail = (category, stage, state, facts) => privacyFailure(command, scope, category, stage, state, facts);
|
|
74
|
+
const fields = input?.fields;
|
|
75
|
+
const independent = input?.preserveStage === true;
|
|
76
|
+
if (!Array.isArray(allowedFields) || typeof readSource !== 'function' || typeof validateResult !== 'function') {
|
|
77
|
+
throw fail('configuration', 'configuration');
|
|
78
|
+
}
|
|
79
|
+
if (typeof scope.kdtId !== 'string' || !/^[1-9]\d*$/.test(scope.kdtId) || !Number.isSafeInteger(Number(scope.kdtId)) ||
|
|
80
|
+
!object(input.object) || !Array.isArray(fields) || fields.length < 1 || fields.length > 3 ||
|
|
81
|
+
new Set(fields).size !== fields.length || fields.some(field => !allowedFields.includes(field)) ||
|
|
82
|
+
!validReason(input.reason)) throw fail('validation', 'input');
|
|
83
|
+
const expected = input.expectedCiphertexts;
|
|
84
|
+
if (expected !== undefined && (!object(expected) || Object.keys(expected).length !== fields.length ||
|
|
85
|
+
fields.some(field => !Object.hasOwn(expected, field) || typeof expected[field] !== 'string'))) {
|
|
86
|
+
throw fail('validation', 'input');
|
|
87
|
+
}
|
|
88
|
+
const request = Object.freeze({ scope, object: Object.freeze({ ...input.object }),
|
|
89
|
+
fields: Object.freeze([...fields]), reason: input.reason,
|
|
90
|
+
...(expected === undefined ? {} : { expectedCiphertexts: Object.freeze({ ...expected }) }) });
|
|
91
|
+
if (!provider || typeof provider.authorize !== 'function') {
|
|
92
|
+
throw fail('configuration', 'sensitive-authorization');
|
|
93
|
+
}
|
|
94
|
+
let environment;
|
|
95
|
+
try { environment = await snapshotApiEnvironment(undefined, Number(scope.kdtId)); }
|
|
96
|
+
catch (error) {
|
|
97
|
+
const failure = fail('configuration', 'configuration');
|
|
98
|
+
failure.diagnosticReason = error.diagnosticReason;
|
|
99
|
+
throw failure;
|
|
100
|
+
}
|
|
101
|
+
let access;
|
|
102
|
+
incompleteCoverage();
|
|
103
|
+
try { access = await provider.authorize(request, { environment }); }
|
|
104
|
+
catch { throw fail('permission', 'sensitive-authorization', 'unknown'); }
|
|
105
|
+
if (!access || typeof access.verifySource !== 'function' || typeof access.recordAccess !== 'function') {
|
|
106
|
+
if (independent) throw privacyFailure(command, scope, 'permission', 'privacy-authorization');
|
|
107
|
+
throw fail('permission', 'sensitive-authorization');
|
|
108
|
+
}
|
|
109
|
+
let stage = 'shop-binding';
|
|
110
|
+
let requestCount = 0;
|
|
111
|
+
let responseBytes = 0;
|
|
112
|
+
const requestState = () => requestCount ? 'sent' : 'not_sent';
|
|
113
|
+
let result;
|
|
114
|
+
let error;
|
|
115
|
+
try {
|
|
116
|
+
const read = await createShopQuery({ kdtId: Number(scope.kdtId) }, command, {
|
|
117
|
+
environment,
|
|
118
|
+
transportOptions: {
|
|
119
|
+
shopApp: false,
|
|
120
|
+
onRequest: () => {
|
|
121
|
+
if (requestCount >= 4) throw fail('limit', stage, requestState());
|
|
122
|
+
requestCount += 1;
|
|
123
|
+
},
|
|
124
|
+
maxResponseBytes: () => RESPONSE_LIMIT - responseBytes
|
|
125
|
+
},
|
|
126
|
+
onResponse: response => {
|
|
127
|
+
responseBytes += response.responseBytes;
|
|
128
|
+
if (responseBytes > RESPONSE_LIMIT) throw fail('limit', stage, requestState());
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
stage = 'source';
|
|
132
|
+
const source = await readSource(read, request);
|
|
133
|
+
if (!object(source) || !Array.isArray(source.values) || source.values.length !== request.fields.length) {
|
|
134
|
+
throw fail('protocol', stage, requestState());
|
|
135
|
+
}
|
|
136
|
+
const selected = request.fields.map((field, index) => {
|
|
137
|
+
const entry = source.values[index];
|
|
138
|
+
if (!object(entry) || entry.field !== field || typeof entry.value !== 'string' ||
|
|
139
|
+
!entry.value.trim() || Buffer.byteLength(entry.value, 'utf8') > 16384 ||
|
|
140
|
+
/[\p{Cc}\p{Zl}\p{Zp}]/u.test(entry.value)) throw fail('protocol', stage, requestState());
|
|
141
|
+
if (request.expectedCiphertexts && request.expectedCiphertexts[field] !== entry.value) {
|
|
142
|
+
throw fail('scope_unresolved', stage, requestState());
|
|
143
|
+
}
|
|
144
|
+
return Object.freeze({ field, value: entry.value });
|
|
145
|
+
});
|
|
146
|
+
if (await access.verifySource({ sourceData: source.sourceData, values: selected }) !== true) {
|
|
147
|
+
throw fail('scope_unresolved', stage, requestState());
|
|
148
|
+
}
|
|
149
|
+
const sources = [...new Set(selected.map(entry => entry.value))];
|
|
150
|
+
const single = sources.length === 1;
|
|
151
|
+
stage = 'ciphertext-verification';
|
|
152
|
+
const verification = await read(single ? paths.verifySingle : paths.verifyBatch,
|
|
153
|
+
single ? { secret_key_single_encrypt_query_request: JSON.stringify({ source: sources[0] }) } :
|
|
154
|
+
{ request: JSON.stringify({ sources }) }, stage);
|
|
155
|
+
if (!object(verification.data) || verification.data.is_encrypt !== true) {
|
|
156
|
+
throw fail('protocol', stage, requestState());
|
|
157
|
+
}
|
|
158
|
+
stage = 'decryption';
|
|
159
|
+
const decrypted = await read(single ? paths.decryptSingle : paths.decryptBatch,
|
|
160
|
+
single ? { source: sources[0] } : { sources }, stage);
|
|
161
|
+
if (!single && (!object(decrypted.data) || Object.keys(decrypted.data).length !== sources.length ||
|
|
162
|
+
sources.some(sourceValue => !Object.hasOwn(decrypted.data, sourceValue)))) {
|
|
163
|
+
throw fail('protocol', stage, requestState());
|
|
164
|
+
}
|
|
165
|
+
const plaintexts = new Map(sources.map(sourceValue => {
|
|
166
|
+
const value = single ? decrypted.data : decrypted.data[sourceValue];
|
|
167
|
+
if (typeof value !== 'string' || (!input.allowEmptyPlaintext && !value.trim()) || value === sourceValue) {
|
|
168
|
+
throw fail('protocol', stage, requestState());
|
|
169
|
+
}
|
|
170
|
+
return [sourceValue, value];
|
|
171
|
+
}));
|
|
172
|
+
result = { sourceData: source.sourceData,
|
|
173
|
+
values: selected.map(entry => ({ field: entry.field, value: plaintexts.get(entry.value) })) };
|
|
174
|
+
stage = 'output-model';
|
|
175
|
+
await validateResult(result);
|
|
176
|
+
} catch (caught) {
|
|
177
|
+
error = safeFailure(caught, command, scope, stage, requestState(), independent);
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
const outcome = !error ? 'release_authorized' :
|
|
181
|
+
error.cliDetails?.requestState === 'unknown' ? 'unknown' : 'denied';
|
|
182
|
+
const auditEvent = {
|
|
183
|
+
outcome, stage,
|
|
184
|
+
...(error?.cliDetails?.api ? { api: error.cliDetails.api } : {})
|
|
185
|
+
};
|
|
186
|
+
if (await access.recordAccess(auditEvent) !== true) {
|
|
187
|
+
throw new Error('audit unavailable');
|
|
188
|
+
}
|
|
189
|
+
} catch {
|
|
190
|
+
if (independent) throw privacyFailure(command, scope, 'audit_failed', 'audit', 'unknown');
|
|
191
|
+
throw fail('transport', 'sensitive-audit', 'unknown');
|
|
192
|
+
}
|
|
193
|
+
if (error) throw error;
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = { discloseFields, privacyError, validReason, paths, RESPONSE_LIMIT, OUTPUT_LIMIT };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const modelMaps = new Set([
|
|
4
|
+
'properties', 'patternProperties', 'definitions', '$defs', 'dependentSchemas'
|
|
5
|
+
]);
|
|
6
|
+
const modelValues = new Set([
|
|
7
|
+
'items', 'additionalItems', 'additionalProperties', 'contains', 'not',
|
|
8
|
+
'if', 'then', 'else', 'propertyNames', 'contentSchema'
|
|
9
|
+
]);
|
|
10
|
+
const modelArrays = new Set(['allOf', 'anyOf', 'oneOf', 'prefixItems']);
|
|
11
|
+
|
|
12
|
+
function publicModel(model) {
|
|
13
|
+
if (typeof model === 'boolean') return model;
|
|
14
|
+
const result = {};
|
|
15
|
+
for (const [key, value] of Object.entries(model)) {
|
|
16
|
+
if (key === '$comment') continue;
|
|
17
|
+
if (modelMaps.has(key) || key === 'dependencies') {
|
|
18
|
+
result[key] = Object.fromEntries(Object.entries(value).map(([name, child]) => [
|
|
19
|
+
name, Array.isArray(child) ? structuredClone(child) : publicModel(child)
|
|
20
|
+
]));
|
|
21
|
+
} else if (modelArrays.has(key) || (key === 'items' && Array.isArray(value))) {
|
|
22
|
+
result[key] = value.map(publicModel);
|
|
23
|
+
} else if (modelValues.has(key)) {
|
|
24
|
+
result[key] = publicModel(value);
|
|
25
|
+
} else {
|
|
26
|
+
result[key] = structuredClone(value);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function publicCommandSchema(schema) {
|
|
33
|
+
if (!schema) return {};
|
|
34
|
+
const result = { schemaVersion: schema.schemaVersion };
|
|
35
|
+
for (const section of ['input', 'output', 'preview', 'error']) {
|
|
36
|
+
if (schema[section]) result[section] = publicModel(schema[section]);
|
|
37
|
+
}
|
|
38
|
+
if (schema.definitions) {
|
|
39
|
+
result.definitions = Object.fromEntries(Object.entries(schema.definitions)
|
|
40
|
+
.map(([name, model]) => [name, publicModel(model)]));
|
|
41
|
+
}
|
|
42
|
+
if (schema.modules) {
|
|
43
|
+
result.modules = Object.fromEntries(Object.entries(schema.modules)
|
|
44
|
+
.map(([name, { base, description }]) => [name, { base, description }]));
|
|
45
|
+
}
|
|
46
|
+
for (const key of ['success', 'errors', 'notes']) {
|
|
47
|
+
if (schema[key]) result[key] = structuredClone(schema[key]);
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
module.exports = { publicModel, publicCommandSchema };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { requestApi, ApiConfigurationError, ApiResponseLimitError } = require('../transport/api');
|
|
4
|
+
const { parseExactJson } = require('./exact-json');
|
|
5
|
+
|
|
6
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
7
|
+
|
|
8
|
+
class QueryError extends Error {
|
|
9
|
+
constructor(category, stage, scope, facts, requestState, command, nextStep) {
|
|
10
|
+
super(`查询失败:${category}(${stage})`);
|
|
11
|
+
this.cliDetails = {
|
|
12
|
+
...facts, category, stage, command, targetScope: scope, requestState, nextStep
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function assertSafeNumbers(value) {
|
|
18
|
+
if (typeof value === 'number' && (!Number.isFinite(value) ||
|
|
19
|
+
(Number.isInteger(value) && !Number.isSafeInteger(value)))) {
|
|
20
|
+
throw new Error('unsafe numeric value');
|
|
21
|
+
}
|
|
22
|
+
if (Array.isArray(value)) value.forEach(assertSafeNumbers);
|
|
23
|
+
else if (object(value)) Object.values(value).forEach(assertSafeNumbers);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function diagnosticCode(value) {
|
|
27
|
+
return Number.isSafeInteger(value) ||
|
|
28
|
+
(typeof value === 'string' && value.trim() === value && /^-?\d{1,12}$/.test(value));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function diagnostics(result, path, httpStatus) {
|
|
32
|
+
const facts = { api: path, httpStatus };
|
|
33
|
+
if (diagnosticCode(result?.code)) facts.businessCode = result.code;
|
|
34
|
+
const gateway = object(result?.gw_err_resp) ? result.gw_err_resp : null;
|
|
35
|
+
if (diagnosticCode(gateway?.err_code)) facts.gatewayCode = gateway.err_code;
|
|
36
|
+
for (const field of ['trace_id', 'request_id']) {
|
|
37
|
+
const value = result?.[field] ?? gateway?.[field];
|
|
38
|
+
if (typeof value === 'string' && value.trim() === value &&
|
|
39
|
+
/^[a-zA-Z0-9_-]{1,128}$/.test(value)) {
|
|
40
|
+
facts[field] = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return facts;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function readApiEnvelope(path, body, failure, env, onResponse, transportOptions) {
|
|
47
|
+
let response;
|
|
48
|
+
try {
|
|
49
|
+
response = await requestApi({ path, body }, env, transportOptions);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof ApiConfigurationError) {
|
|
52
|
+
const rejected = failure('configuration', {}, 'not_sent', 'configuration');
|
|
53
|
+
rejected.diagnosticReason = error.diagnosticReason;
|
|
54
|
+
throw rejected;
|
|
55
|
+
}
|
|
56
|
+
if (error instanceof ApiResponseLimitError) {
|
|
57
|
+
const limitStage = typeof transportOptions?.limitStage === 'function'
|
|
58
|
+
? transportOptions.limitStage(path) : transportOptions?.limitStage;
|
|
59
|
+
throw failure(transportOptions?.limitCategory || 'limit',
|
|
60
|
+
{ api: path, httpStatus: error.httpStatus }, 'sent',
|
|
61
|
+
limitStage || 'response');
|
|
62
|
+
}
|
|
63
|
+
throw failure('transport', {}, 'unknown', 'transport');
|
|
64
|
+
}
|
|
65
|
+
if (onResponse) onResponse(response, path);
|
|
66
|
+
let result;
|
|
67
|
+
try {
|
|
68
|
+
result = parseExactJson(response.body);
|
|
69
|
+
if (!object(result)) result = null;
|
|
70
|
+
} catch {
|
|
71
|
+
result = null;
|
|
72
|
+
}
|
|
73
|
+
const facts = diagnostics(result, path, response.httpStatus);
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
const category = response.httpStatus === 401 ? 'authentication' :
|
|
76
|
+
response.httpStatus === 403 ? 'permission' : 'http';
|
|
77
|
+
throw failure(category, facts, 'sent', 'http');
|
|
78
|
+
}
|
|
79
|
+
if (!result) throw failure('protocol', facts, 'sent', 'response');
|
|
80
|
+
return { result, facts, rawBody: response.body, responseBytes: response.responseBytes };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function assertApiSuccess(result, reject, successCodes = [200]) {
|
|
84
|
+
if (Object.hasOwn(result, 'gw_err_resp')) {
|
|
85
|
+
if (result.success === true || successCodes.includes(result.code)) reject('protocol');
|
|
86
|
+
if (object(result.gw_err_resp) && diagnosticCode(result.gw_err_resp.err_code)) {
|
|
87
|
+
reject('business_rejected');
|
|
88
|
+
}
|
|
89
|
+
reject('protocol');
|
|
90
|
+
}
|
|
91
|
+
if (result.success === false) {
|
|
92
|
+
reject(successCodes.includes(result.code) ? 'protocol' : 'business_rejected');
|
|
93
|
+
}
|
|
94
|
+
if (result.success === undefined && Number.isSafeInteger(result.code) &&
|
|
95
|
+
![0, 200].includes(result.code)) reject('business_rejected');
|
|
96
|
+
if (result.success !== true || !successCodes.includes(result.code)) reject('protocol');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = {
|
|
100
|
+
QueryError, readApiEnvelope, assertApiSuccess, assertSafeNumbers, object, diagnosticCode
|
|
101
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { parseDateTime, formatDateTime, apiSeconds } = require('./datetime');
|
|
4
|
+
const { formatYuan, compareYuan } = require('./money');
|
|
5
|
+
|
|
6
|
+
function bounds(raw, allowBare) {
|
|
7
|
+
if (typeof raw !== 'string' || !raw || raw.trim() !== raw) {
|
|
8
|
+
throw new Error('区间须为非空文本,不能包含首尾空白');
|
|
9
|
+
}
|
|
10
|
+
const parts = raw.split('..');
|
|
11
|
+
if (parts.length === 1 && allowBare) return [raw, raw];
|
|
12
|
+
if (parts.length !== 2 || (!parts[0] && !parts[1])) {
|
|
13
|
+
throw new Error('区间须使用 LOW..HIGH、LOW.. 或 ..HIGH,时间必须包含 ..');
|
|
14
|
+
}
|
|
15
|
+
return parts;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parseRange(raw, { keys, allowBare = false, requireBoth = false, strictOrder = false,
|
|
19
|
+
parse, compare }) {
|
|
20
|
+
const parts = bounds(raw, allowBare);
|
|
21
|
+
if (requireBoth && parts.some(value => value === '')) throw new Error('此区间必须显式提供两个边界');
|
|
22
|
+
const result = {};
|
|
23
|
+
parts.forEach((value, index) => {
|
|
24
|
+
if (value === '') return;
|
|
25
|
+
if (value.trim() !== value) throw new Error('区间边界不能包含首尾空白');
|
|
26
|
+
result[keys[index]] = parse(value);
|
|
27
|
+
});
|
|
28
|
+
if (parts.every(value => value !== '')) {
|
|
29
|
+
const order = compare(result[keys[0]], result[keys[1]]);
|
|
30
|
+
if (order > 0 || (strictOrder && order === 0)) {
|
|
31
|
+
throw new Error(strictOrder ? '区间起点必须早于终点' : '区间起点不得晚于终点');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function parseIntegerRange(raw, { minimum = 0, maximum = Number.MAX_SAFE_INTEGER } = {}) {
|
|
38
|
+
if (!Number.isSafeInteger(minimum) || !Number.isSafeInteger(maximum) || minimum > maximum) {
|
|
39
|
+
throw new Error('整数区间配置无效');
|
|
40
|
+
}
|
|
41
|
+
return parseRange(raw, { keys: ['min', 'max'], allowBare: true,
|
|
42
|
+
parse(value) {
|
|
43
|
+
if (!/^-?\d+$/.test(value)) throw new Error('区间边界须为十进制整数');
|
|
44
|
+
const number = Number(value);
|
|
45
|
+
if (!Number.isSafeInteger(number) || number < minimum || number > maximum) {
|
|
46
|
+
throw new Error('整数区间边界超出命令允许范围');
|
|
47
|
+
}
|
|
48
|
+
return number;
|
|
49
|
+
},
|
|
50
|
+
compare: (a, b) => a < b ? -1 : a > b ? 1 : 0
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function parseMoneyRange(raw, { minimum = '0.00', maximum } = {}) {
|
|
55
|
+
const lower = formatYuan(minimum);
|
|
56
|
+
const upper = maximum === undefined ? undefined : formatYuan(maximum);
|
|
57
|
+
if (!lower || upper === '' || (upper !== undefined && compareYuan(lower, upper) > 0)) {
|
|
58
|
+
throw new Error('金额区间配置无效');
|
|
59
|
+
}
|
|
60
|
+
return parseRange(raw, { keys: ['min', 'max'], allowBare: true,
|
|
61
|
+
parse(value) {
|
|
62
|
+
const normalized = formatYuan(value);
|
|
63
|
+
if (!normalized || compareYuan(normalized, lower) < 0 ||
|
|
64
|
+
(upper !== undefined && compareYuan(normalized, upper) > 0)) {
|
|
65
|
+
throw new Error('金额区间边界超出命令允许范围');
|
|
66
|
+
}
|
|
67
|
+
return normalized;
|
|
68
|
+
},
|
|
69
|
+
compare: compareYuan
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseDateTimeRange(raw, { precision, requireBoth = false, strictOrder = false } = {}) {
|
|
74
|
+
if (!['seconds', 'milliseconds'].includes(precision)) throw new Error('须声明接口时间精度');
|
|
75
|
+
return parseRange(raw, { keys: ['start', 'end'], requireBoth, strictOrder,
|
|
76
|
+
parse(value) {
|
|
77
|
+
const milliseconds = precision === 'seconds' ? apiSeconds(value) * 1000 : parseDateTime(value);
|
|
78
|
+
return formatDateTime(milliseconds);
|
|
79
|
+
},
|
|
80
|
+
compare: (a, b) => a < b ? -1 : a > b ? 1 : 0
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = { parseIntegerRange, parseMoneyRange, parseDateTimeRange };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { AsyncLocalStorage } = require('node:async_hooks');
|
|
4
|
+
|
|
5
|
+
const sessions = new AsyncLocalStorage();
|
|
6
|
+
const failures = new WeakMap();
|
|
7
|
+
|
|
8
|
+
function requestState() {
|
|
9
|
+
const state = sessions.getStore();
|
|
10
|
+
const complete = state && !state.partial;
|
|
11
|
+
return {
|
|
12
|
+
requestState: !complete || state.unknown || state.pending ? 'unknown' :
|
|
13
|
+
state.attempts ? 'sent' : 'not_sent',
|
|
14
|
+
coverage: complete ? 'complete' : 'partial',
|
|
15
|
+
observedAttempts: state?.attempts || 0
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function runWithRequestState(action) {
|
|
20
|
+
incompleteCoverage();
|
|
21
|
+
return sessions.run({ attempts: 0, pending: 0, unknown: false, partial: false }, async () => {
|
|
22
|
+
try { return await action(); }
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (error instanceof Error) failures.set(error, requestState());
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function beginRequest() {
|
|
31
|
+
const state = sessions.getStore();
|
|
32
|
+
if (!state) return undefined;
|
|
33
|
+
state.attempts++;
|
|
34
|
+
state.pending++;
|
|
35
|
+
let finished = false;
|
|
36
|
+
return outcome => {
|
|
37
|
+
if (finished) return;
|
|
38
|
+
finished = true;
|
|
39
|
+
state.pending--;
|
|
40
|
+
if (outcome === 'unknown') state.unknown = true;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function incompleteCoverage() {
|
|
45
|
+
const state = sessions.getStore();
|
|
46
|
+
if (state) state.partial = true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function executionFor(error) {
|
|
50
|
+
return failures.get(error) || requestState();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = {
|
|
54
|
+
runWithRequestState, requestState, beginRequest, incompleteCoverage, executionFor
|
|
55
|
+
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assertionKeywords = new Set([
|
|
4
|
+
'$ref', 'type', 'const', 'enum', 'allOf', 'anyOf', 'oneOf', 'not', 'if',
|
|
5
|
+
'contains', 'pattern', 'format', 'minLength', 'maxLength', 'minimum',
|
|
6
|
+
'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'multipleOf', 'minItems',
|
|
7
|
+
'maxItems', 'uniqueItems', 'minProperties', 'maxProperties', 'properties',
|
|
8
|
+
'patternProperties', 'additionalProperties', 'propertyNames', 'dependencies',
|
|
9
|
+
'dependentRequired', 'dependentSchemas'
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
function object(value) {
|
|
13
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function includesType(schema, type) {
|
|
17
|
+
return schema.type === type || (Array.isArray(schema.type) && schema.type.includes(type));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function hasObjectShape(schema) {
|
|
21
|
+
return ['properties', 'patternProperties', 'additionalProperties', 'maxProperties']
|
|
22
|
+
.some(key => Object.hasOwn(schema, key));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function addIssue(issues, path, message) {
|
|
26
|
+
issues.push({ path, message });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function visitSchema(schema, path, issues, seen) {
|
|
30
|
+
if (typeof schema === 'boolean') return;
|
|
31
|
+
if (!object(schema)) {
|
|
32
|
+
addIssue(issues, path, 'must be a schema object');
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (seen.has(schema)) return;
|
|
36
|
+
seen.add(schema);
|
|
37
|
+
|
|
38
|
+
if (Object.keys(schema).every(key => !assertionKeywords.has(key))) {
|
|
39
|
+
addIssue(issues, path, 'must define a validation rule, not only metadata such as description');
|
|
40
|
+
}
|
|
41
|
+
if (includesType(schema, 'object') && !hasObjectShape(schema)) {
|
|
42
|
+
addIssue(issues, path,
|
|
43
|
+
'object schemas must define properties, patternProperties, additionalProperties, or maxProperties');
|
|
44
|
+
}
|
|
45
|
+
if (includesType(schema, 'array') && !Object.hasOwn(schema, 'items') &&
|
|
46
|
+
!Object.hasOwn(schema, 'contains') && schema.maxItems !== 0) {
|
|
47
|
+
addIssue(issues, path, 'array schemas must define items or contains');
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(schema.required) && object(schema.properties)) {
|
|
50
|
+
for (const name of schema.required) {
|
|
51
|
+
if (!Object.hasOwn(schema.properties, name)) {
|
|
52
|
+
addIssue(issues, `${path}.required`, `references missing property ${JSON.stringify(name)}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (object(schema.properties)) {
|
|
58
|
+
for (const [name, child] of Object.entries(schema.properties)) {
|
|
59
|
+
visitSchema(child, `${path}.properties.${name}`, issues, seen);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (object(schema.patternProperties)) {
|
|
63
|
+
for (const [name, child] of Object.entries(schema.patternProperties)) {
|
|
64
|
+
visitSchema(child, `${path}.patternProperties.${name}`, issues, seen);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (object(schema.additionalProperties)) {
|
|
68
|
+
visitSchema(schema.additionalProperties, `${path}.additionalProperties`, issues, seen);
|
|
69
|
+
}
|
|
70
|
+
if (object(schema.propertyNames)) {
|
|
71
|
+
visitSchema(schema.propertyNames, `${path}.propertyNames`, issues, seen);
|
|
72
|
+
}
|
|
73
|
+
if (object(schema.dependencies)) {
|
|
74
|
+
for (const [name, child] of Object.entries(schema.dependencies)) {
|
|
75
|
+
if (object(child)) visitSchema(child, `${path}.dependencies.${name}`, issues, seen);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (object(schema.dependentSchemas)) {
|
|
79
|
+
for (const [name, child] of Object.entries(schema.dependentSchemas)) {
|
|
80
|
+
visitSchema(child, `${path}.dependentSchemas.${name}`, issues, seen);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const key of ['items', 'contains', 'not', 'if', 'then', 'else']) {
|
|
84
|
+
if (object(schema[key])) visitSchema(schema[key], `${path}.${key}`, issues, seen);
|
|
85
|
+
}
|
|
86
|
+
for (const key of ['allOf', 'anyOf', 'oneOf']) {
|
|
87
|
+
if (!Array.isArray(schema[key])) continue;
|
|
88
|
+
if (schema[key].length === 0) addIssue(issues, `${path}.${key}`, 'must contain at least one schema');
|
|
89
|
+
schema[key].forEach((child, index) => {
|
|
90
|
+
visitSchema(child, `${path}.${key}[${index}]`, issues, seen);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function findSchemaCompletenessIssues(schema) {
|
|
96
|
+
const issues = [];
|
|
97
|
+
const seen = new WeakSet();
|
|
98
|
+
for (const section of ['input', 'output', 'preview', 'error', 'parserError']) {
|
|
99
|
+
if (Object.hasOwn(schema, section)) visitSchema(schema[section], section, issues, seen);
|
|
100
|
+
}
|
|
101
|
+
if (object(schema.definitions)) {
|
|
102
|
+
for (const [name, definition] of Object.entries(schema.definitions)) {
|
|
103
|
+
visitSchema(definition, `definitions.${name}`, issues, seen);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return issues;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function assertSchemaCompleteness(schema) {
|
|
110
|
+
const issues = findSchemaCompletenessIssues(schema);
|
|
111
|
+
if (issues.length === 0) return;
|
|
112
|
+
const details = issues.map(({ path, message }) => `${path} ${message}`).join('; ');
|
|
113
|
+
throw new Error(`Incomplete command schema: ${details}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
module.exports = { assertSchemaCompleteness, findSchemaCompletenessIssues };
|