@be-link/ecommerce-plan-allocation-service-node-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # EcommercePlanAllocationService SDK
2
+
3
+ EcommercePlanAllocationService Node.js SDK - 敏感信息管理服务客户端
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @be-link/ecommerce-plan-allocation-service-node-sdk
9
+ ```
10
+
11
+ ## 使用
12
+
13
+ ```typescript
14
+ import { demoService } from '@be-link/ecommerce-plan-allocation-service-node-sdk'
15
+
16
+ const result = await demoService.demoFunc({ /* 请求参数 */ })
17
+ ```
18
+
19
+ ## 开发
20
+
21
+ ```bash
22
+ # 构建
23
+ npm run build
24
+
25
+ # 生成 Swagger 文档
26
+ npm run swagger
27
+
28
+ # 构建并生成文档
29
+ npm run build:swagger
30
+ ```
31
+
32
+ ## Swagger 文档
33
+
34
+ 查看 [SWAGGER.md](./SWAGGER.md) 了解如何生成和使用 API 文档。
35
+
36
+ ## License
37
+
38
+ ISC
39
+
package/enum.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export declare namespace ENUM {
2
+ namespace ORDER_ENUM {
3
+ enum STATUS {
4
+ PENDING = "PENDING",
5
+ PAID = "PAID",
6
+ SHIPPED = "SHIPPED",
7
+ DELIVERED = "DELIVERED",
8
+ COMPLETED = "COMPLETED",
9
+ CANCELLED = "CANCELLED"
10
+ }
11
+ enum STATUS_CHINESE {
12
+ PENDING = "\u5F85\u652F\u4ED8",
13
+ PAID = "\u5DF2\u652F\u4ED8",
14
+ SHIPPED = "\u5DF2\u53D1\u8D27",
15
+ DELIVERED = "\u5DF2\u6536\u8D27",
16
+ COMPLETED = "\u5DF2\u5B8C\u6210",
17
+ CANCELLED = "\u5DF2\u53D6\u6D88"
18
+ }
19
+ }
20
+ }
package/enum.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ENUM = void 0;
4
+ var ENUM;
5
+ (function (ENUM) {
6
+ let ORDER_ENUM;
7
+ (function (ORDER_ENUM) {
8
+ let STATUS;
9
+ (function (STATUS) {
10
+ STATUS["PENDING"] = "PENDING";
11
+ STATUS["PAID"] = "PAID";
12
+ STATUS["SHIPPED"] = "SHIPPED";
13
+ STATUS["DELIVERED"] = "DELIVERED";
14
+ STATUS["COMPLETED"] = "COMPLETED";
15
+ STATUS["CANCELLED"] = "CANCELLED";
16
+ })(STATUS = ORDER_ENUM.STATUS || (ORDER_ENUM.STATUS = {}));
17
+ let STATUS_CHINESE;
18
+ (function (STATUS_CHINESE) {
19
+ STATUS_CHINESE["PENDING"] = "\u5F85\u652F\u4ED8";
20
+ STATUS_CHINESE["PAID"] = "\u5DF2\u652F\u4ED8";
21
+ STATUS_CHINESE["SHIPPED"] = "\u5DF2\u53D1\u8D27";
22
+ STATUS_CHINESE["DELIVERED"] = "\u5DF2\u6536\u8D27";
23
+ STATUS_CHINESE["COMPLETED"] = "\u5DF2\u5B8C\u6210";
24
+ STATUS_CHINESE["CANCELLED"] = "\u5DF2\u53D6\u6D88";
25
+ })(STATUS_CHINESE = ORDER_ENUM.STATUS_CHINESE || (ORDER_ENUM.STATUS_CHINESE = {}));
26
+ })(ORDER_ENUM = ENUM.ORDER_ENUM || (ENUM.ORDER_ENUM = {}));
27
+ })(ENUM || (exports.ENUM = ENUM = {}));
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { configService } from './modules/config/service';
2
+ export type { Service as ConfigServiceTypes } from './modules/config/types';
3
+ export { allocationService } from './modules/allocation/service';
4
+ export type { Service as AllocationServiceTypes } from './modules/allocation/types';
5
+ export { ENUM as DEMO_ENUM } from './enum';
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEMO_ENUM = exports.allocationService = exports.configService = void 0;
4
+ var service_1 = require("./modules/config/service");
5
+ Object.defineProperty(exports, "configService", { enumerable: true, get: function () { return service_1.configService; } });
6
+ var service_2 = require("./modules/allocation/service");
7
+ Object.defineProperty(exports, "allocationService", { enumerable: true, get: function () { return service_2.allocationService; } });
8
+ var enum_1 = require("./enum");
9
+ Object.defineProperty(exports, "DEMO_ENUM", { enumerable: true, get: function () { return enum_1.ENUM; } });
10
+ // 服务.模块.枚举名称.枚举值 示例: DEMO_ENUM.ORDER_ENUM.STATUS.PENDING
@@ -0,0 +1,18 @@
1
+ /**
2
+ * BaseService 基类
3
+ * 所有服务类都应该继承此类
4
+ */
5
+ export default abstract class BaseService {
6
+ private isPublicEnv;
7
+ /** URL一级路径 */
8
+ protected abstract prefixUrl: string;
9
+ /** 子网域名 */
10
+ protected readonly natDevHost = "http://plan-allocation:8090/plan-allocation";
11
+ protected readonly natProdHost = "http://plan-allocation:8090/plan-allocation";
12
+ /** 公网域名 */
13
+ protected readonly publicDevHost = "https://ecommerce-dev.wejourney.top/plan-allocation";
14
+ protected readonly publicProdHost = "";
15
+ constructor();
16
+ /** 获取API URL */
17
+ protected getApiUrl(func: Function): string;
18
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const env_1 = __importDefault(require("../utils/env"));
7
+ const string_1 = require("../utils/string");
8
+ /**
9
+ * BaseService 基类
10
+ * 所有服务类都应该继承此类
11
+ */
12
+ class BaseService {
13
+ constructor() {
14
+ /** 子网域名 */
15
+ this.natDevHost = 'http://plan-allocation:8090/plan-allocation';
16
+ this.natProdHost = 'http://plan-allocation:8090/plan-allocation';
17
+ /** 公网域名 */
18
+ this.publicDevHost = 'https://ecommerce-dev.wejourney.top/plan-allocation';
19
+ this.publicProdHost = '';
20
+ /** 如果是云函数环境, 默认走公网访问 */
21
+ this.isPublicEnv = (process.env.CONTAINER_ENV || 'SCF') === 'SCF';
22
+ }
23
+ /** 获取API URL */
24
+ getApiUrl(func) {
25
+ const host = this.isPublicEnv
26
+ ? env_1.default.isProduction()
27
+ ? this.publicProdHost
28
+ : this.publicDevHost
29
+ : env_1.default.isProduction()
30
+ ? this.natProdHost
31
+ : this.natDevHost;
32
+ return `${host}${this.prefixUrl}/${(0, string_1.camelToKebabCase)(func.name)}`;
33
+ }
34
+ }
35
+ exports.default = BaseService;
@@ -0,0 +1,8 @@
1
+ import { Service } from './types';
2
+ import BaseService from '../BaseService';
3
+ declare class AllocationService extends BaseService implements Service.AllocationController {
4
+ protected prefixUrl: string;
5
+ trigger(request: Service.Request.Trigger): Promise<Service.Response.Trigger>;
6
+ }
7
+ export declare const allocationService: AllocationService;
8
+ export default allocationService;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
9
+ return function (target, key) { decorator(target, key, paramIndex); }
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.allocationService = void 0;
16
+ const tsoa_1 = require("tsoa");
17
+ const http_1 = require("../../utils/http");
18
+ const BaseService_1 = __importDefault(require("../BaseService"));
19
+ let AllocationService = class AllocationService extends BaseService_1.default {
20
+ constructor() {
21
+ super(...arguments);
22
+ this.prefixUrl = '/allocation';
23
+ }
24
+ trigger(request) {
25
+ return (0, http_1.callApi)(this.getApiUrl(this.trigger), request);
26
+ }
27
+ };
28
+ __decorate([
29
+ (0, tsoa_1.Post)('trigger'),
30
+ __param(0, (0, tsoa_1.Body)())
31
+ ], AllocationService.prototype, "trigger", null);
32
+ AllocationService = __decorate([
33
+ (0, tsoa_1.Route)('allocation'),
34
+ (0, tsoa_1.Tags)('Allocation')
35
+ ], AllocationService);
36
+ exports.allocationService = new AllocationService();
37
+ exports.default = exports.allocationService;
@@ -0,0 +1,168 @@
1
+ export declare namespace Service {
2
+ namespace Entity {
3
+ /** 配货计划明细 */
4
+ interface AllocationPlanDetail {
5
+ /** 门店ID */
6
+ storeId: string;
7
+ /** SKU编码 */
8
+ skuCode: string;
9
+ /** SKU ID */
10
+ skuId: string;
11
+ /** 正常订单数量 */
12
+ normalOrderQty: number;
13
+ /** 券订单数量 */
14
+ couponOrderQty: number;
15
+ /** 退款数量 */
16
+ refundQty: number;
17
+ /** 未发货量 */
18
+ undeliveredQty?: number;
19
+ /** 配货量 */
20
+ allocationQty: number;
21
+ /** 是否库存盈余 */
22
+ isDeficit: boolean;
23
+ }
24
+ /** 配货日志 */
25
+ interface AllocationLog {
26
+ /** 任务ID */
27
+ id: string;
28
+ /** 计算批次号 */
29
+ calculationBatchNo: string;
30
+ /** 业务日期(毫秒时间戳) */
31
+ businessDate: number;
32
+ /** 业务日期(字符串) */
33
+ businessDateStr: string;
34
+ /** 状态:1-待执行、2-执行中、3-已完成、4-失败 */
35
+ status: number;
36
+ /** 状态文本 */
37
+ statusText: string;
38
+ /** 执行时长(毫秒) */
39
+ duration: number;
40
+ /** 门店总数 */
41
+ totalStores: number;
42
+ /** SKU总数 */
43
+ totalSkus: number;
44
+ /** 正常订单总数 */
45
+ totalNormalOrders: number;
46
+ /** 券订单总数 */
47
+ totalCouponOrders: number;
48
+ /** 退款总数 */
49
+ totalRefunds: number;
50
+ /** 配货总量 */
51
+ totalAllocation: number;
52
+ /** 库存盈余SKU数 */
53
+ deficitSkus: number;
54
+ /** 错误信息 */
55
+ errorMessage: string | null;
56
+ /** 创建时间 */
57
+ createdAt: number;
58
+ /** 更新时间 */
59
+ updatedAt: number;
60
+ }
61
+ /** 库存盈余明细 */
62
+ interface DeficitDetail {
63
+ /** 门店ID */
64
+ storeId: string;
65
+ /** SKU编码 */
66
+ skuCode: string;
67
+ /** SKU ID */
68
+ skuId: string;
69
+ /** 正常订单数量 */
70
+ normalOrderQty: number;
71
+ /** 券订单数量 */
72
+ couponOrderQty: number;
73
+ /** 退款数量 */
74
+ refundQty: number;
75
+ /** 盈余数量(正数表示多出来的库存) */
76
+ deficit: number;
77
+ }
78
+ }
79
+ namespace Request {
80
+ /** 查询配货计划请求 */
81
+ interface GetPlans {
82
+ /** 业务日期(毫秒时间戳) */
83
+ businessDate: number;
84
+ /** 门店ID(可选) */
85
+ storeId?: string;
86
+ /** SKU编码(可选) */
87
+ skuCode?: string;
88
+ /** 是否只查询库存盈余 */
89
+ isDeficit?: boolean;
90
+ /** 页码,默认0 */
91
+ pageIndex?: number;
92
+ /** 每页数量,默认50 */
93
+ pageSize?: number;
94
+ }
95
+ /** 查询配货日志请求 */
96
+ interface GetLog {
97
+ /** 业务日期(毫秒时间戳) */
98
+ businessDate: number;
99
+ }
100
+ /** 手动触发配货计算请求 */
101
+ interface Trigger {
102
+ /** 配货日期(毫秒时间戳),默认明天 */
103
+ targetDate?: number;
104
+ /** 门店ID列表(可选) */
105
+ storeIds?: string[];
106
+ }
107
+ /** 获取库存盈余明细请求 */
108
+ interface GetDeficit {
109
+ /** 业务日期(毫秒时间戳) */
110
+ businessDate: number;
111
+ }
112
+ }
113
+ namespace Response {
114
+ /** 查询配货计划响应 */
115
+ interface GetPlans {
116
+ /** 是否成功 */
117
+ success: boolean;
118
+ /** 数据 */
119
+ data: {
120
+ /** 业务日期(毫秒时间戳) */
121
+ businessDate: number;
122
+ /** 业务日期(字符串) */
123
+ businessDateStr: string;
124
+ /** 总数 */
125
+ total: number;
126
+ /** 列表 */
127
+ list: Entity.AllocationPlanDetail[];
128
+ };
129
+ }
130
+ /** 查询配货日志响应 */
131
+ interface GetLog {
132
+ /** 是否成功 */
133
+ success: boolean;
134
+ /** 数据 */
135
+ data: Entity.AllocationLog | null;
136
+ }
137
+ /** 手动触发配货计算响应 */
138
+ interface Trigger {
139
+ /** 是否成功 */
140
+ success: boolean;
141
+ /** 消息 */
142
+ message: string;
143
+ /** 数据 */
144
+ data: {
145
+ /** 任务ID */
146
+ taskId: string;
147
+ /** 计算批次号 */
148
+ calculationBatchNo: string;
149
+ /** 业务日期(毫秒时间戳) */
150
+ businessDate: number;
151
+ /** 业务日期(字符串) */
152
+ businessDateStr: string;
153
+ };
154
+ }
155
+ /** 获取库存盈余明细响应 */
156
+ interface GetDeficit {
157
+ /** 是否成功 */
158
+ success: boolean;
159
+ /** 数据 */
160
+ data: Entity.DeficitDetail[];
161
+ }
162
+ }
163
+ /** Allocation Controller 接口定义 */
164
+ interface AllocationController {
165
+ /** 手动触发配货计算 */
166
+ trigger(request: Service.Request.Trigger, req: any): Promise<Service.Response.Trigger>;
167
+ }
168
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { Service } from './types';
2
+ import BaseService from '../BaseService';
3
+ declare class ConfigService extends BaseService implements Service.DemoController {
4
+ protected prefixUrl: string;
5
+ demoFunc(request: Service.Request.demoFunc): Promise<Service.Response.demoFunc>;
6
+ }
7
+ export declare const configService: ConfigService;
8
+ export default configService;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
9
+ return function (target, key) { decorator(target, key, paramIndex); }
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.configService = void 0;
16
+ const tsoa_1 = require("tsoa");
17
+ const http_1 = require("../../utils/http");
18
+ const BaseService_1 = __importDefault(require("../BaseService"));
19
+ let ConfigService = class ConfigService extends BaseService_1.default {
20
+ constructor() {
21
+ super(...arguments);
22
+ this.prefixUrl = '/config';
23
+ }
24
+ demoFunc(request) {
25
+ return (0, http_1.callApi)(this.getApiUrl(this.demoFunc), request);
26
+ }
27
+ };
28
+ __decorate([
29
+ (0, tsoa_1.OperationId)('示例接口'),
30
+ (0, tsoa_1.Post)('demo-func'),
31
+ __param(0, (0, tsoa_1.Body)())
32
+ ], ConfigService.prototype, "demoFunc", null);
33
+ ConfigService = __decorate([
34
+ (0, tsoa_1.Route)('config'),
35
+ (0, tsoa_1.Tags)('Config')
36
+ ], ConfigService);
37
+ exports.configService = new ConfigService();
38
+ exports.default = exports.configService;
@@ -0,0 +1,17 @@
1
+ export declare namespace Service {
2
+ namespace Entity { }
3
+ namespace Request {
4
+ interface demoFunc {
5
+ /** 参数 */
6
+ demoParam: string;
7
+ }
8
+ }
9
+ namespace Response {
10
+ interface demoFunc {
11
+ }
12
+ }
13
+ interface DemoController {
14
+ /** 示例接口 */
15
+ demoFunc(request: Service.Request.demoFunc, req: any): Promise<Service.Response.demoFunc>;
16
+ }
17
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { Service } from './types';
2
+ import BaseService from '../BaseService';
3
+ declare class Config2Service extends BaseService implements Service.Demo2Controller {
4
+ protected prefixUrl: string;
5
+ demoFunc2(request: Service.Request.demoFunc2): Promise<Service.Response.demoFunc2>;
6
+ }
7
+ export declare const config2Service: Config2Service;
8
+ export default config2Service;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
9
+ return function (target, key) { decorator(target, key, paramIndex); }
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.config2Service = void 0;
16
+ const tsoa_1 = require("tsoa");
17
+ const http_1 = require("../../utils/http");
18
+ const BaseService_1 = __importDefault(require("../BaseService"));
19
+ let Config2Service = class Config2Service extends BaseService_1.default {
20
+ constructor() {
21
+ super(...arguments);
22
+ this.prefixUrl = '/config2';
23
+ }
24
+ demoFunc2(request) {
25
+ return (0, http_1.callApi)(this.getApiUrl(this.demoFunc2), request);
26
+ }
27
+ };
28
+ __decorate([
29
+ (0, tsoa_1.OperationId)('示例接口2'),
30
+ (0, tsoa_1.Post)('demo-func2'),
31
+ __param(0, (0, tsoa_1.Body)())
32
+ ], Config2Service.prototype, "demoFunc2", null);
33
+ Config2Service = __decorate([
34
+ (0, tsoa_1.Route)('config2'),
35
+ (0, tsoa_1.Tags)('Config2')
36
+ ], Config2Service);
37
+ exports.config2Service = new Config2Service();
38
+ exports.default = exports.config2Service;
@@ -0,0 +1,22 @@
1
+ export declare namespace Service {
2
+ namespace Entity { }
3
+ namespace Request {
4
+ interface demoFunc2 {
5
+ /** 参数 */
6
+ demoParam: string;
7
+ }
8
+ }
9
+ namespace Response {
10
+ interface demoFunc2 {
11
+ /**
12
+ * 结果
13
+ * @minLength 255
14
+ */
15
+ demoResult: string;
16
+ }
17
+ }
18
+ interface Demo2Controller {
19
+ /** 示例接口2 */
20
+ demoFunc2(request: Service.Request.demoFunc2, req: any): Promise<Service.Response.demoFunc2>;
21
+ }
22
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@be-link/ecommerce-plan-allocation-service-node-sdk",
3
+ "version": "0.0.1",
4
+ "description": "EcommercePlanAllocationService Node.js SDK",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "author": "",
8
+ "license": "ISC",
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "dependencies": {
13
+ "@fastify/request-context": "6.2.1",
14
+ "axios": "1.13.2",
15
+ "axios-retry": "4.0.0",
16
+ "uuid": "9.0.1",
17
+ "tsoa": "^6.6.0"
18
+ },
19
+ "scripts": {
20
+ "build": "rm -rf ./dist && tsc && cp package.json README.md ./dist/ 2>/dev/null || true",
21
+ "test": "jest",
22
+ "swagger": "tsoa spec",
23
+ "build:swagger": "npm run swagger && npm run build && npm run swagger",
24
+ "auto-generate": "node scripts/auto-generate.js"
25
+ }
26
+ }
package/utils/env.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ declare class EnvUtils {
2
+ /**
3
+ * 判断是否为生产环境
4
+ */
5
+ isProduction(): boolean;
6
+ /**
7
+ * 获取环境变量
8
+ */
9
+ getEnv(key: string, defaultValue?: string): string | undefined;
10
+ }
11
+ declare const envUtils: EnvUtils;
12
+ export default envUtils;
package/utils/env.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class EnvUtils {
4
+ /**
5
+ * 判断是否为生产环境
6
+ */
7
+ isProduction() {
8
+ return process.env.NODE_ENV === 'prod';
9
+ }
10
+ /**
11
+ * 获取环境变量
12
+ */
13
+ getEnv(key, defaultValue) {
14
+ return process.env[key] || defaultValue;
15
+ }
16
+ }
17
+ const envUtils = new EnvUtils();
18
+ exports.default = envUtils;
@@ -0,0 +1,10 @@
1
+ declare module '@fastify/request-context' {
2
+ interface RequestContextData {
3
+ requestId: string;
4
+ traceMessageId?: string;
5
+ pandoraUserId?: string;
6
+ beLinkUserId?: string;
7
+ pandoraRoleId?: string;
8
+ }
9
+ }
10
+ export declare function callApi<T extends (...args: any[]) => Promise<any>>(url: string, request?: Parameters<T>[0]): Promise<Awaited<ReturnType<T>>>;
package/utils/http.js ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.callApi = callApi;
40
+ const axios_1 = __importDefault(require("axios"));
41
+ const uuid_1 = require("uuid");
42
+ const axios_retry_1 = __importDefault(require("axios-retry"));
43
+ const request_context_1 = require("@fastify/request-context");
44
+ (0, axios_retry_1.default)(axios_1.default, {
45
+ retries: 1,
46
+ retryCondition(error) {
47
+ return error.response?.status === 502;
48
+ },
49
+ retryDelay: (retryCount) => {
50
+ console.info(`retryCount: ${retryCount}, retryDelay: ${retryCount * 500}`);
51
+ return retryCount * 500;
52
+ },
53
+ onRetry(retryCount, error, requestConfig) {
54
+ console.info(`retryCount: ${retryCount}, onRetry: ${error.message}, requestHeader: ${JSON.stringify(requestConfig.headers)}`);
55
+ },
56
+ });
57
+ async function callApi(url, request) {
58
+ const requestId = request_context_1.requestContext.get('requestId') || request_context_1.requestContext.get('traceMessageId') || (0, uuid_1.v4)();
59
+ const pandoraUserId = request_context_1.requestContext.get('pandoraUserId') || '';
60
+ const pandoraRoleId = request_context_1.requestContext.get('pandoraRoleId') || '';
61
+ const beLinkUserId = request_context_1.requestContext.get('beLinkUserId') || '';
62
+ try {
63
+ console.info(`准备发起ecommerce-plan-allocation-service请求[${requestId}]: ${url}, 参数: ${JSON.stringify(request)}`);
64
+ const response = await axios_1.default.post(url, request || {}, {
65
+ headers: {
66
+ 'x-request-id': requestId,
67
+ 'x-belink-pandora-userid': pandoraUserId,
68
+ 'x-belink-userid': beLinkUserId,
69
+ 'x-belink-pandora-roleid': pandoraRoleId,
70
+ },
71
+ });
72
+ const responseData = response.data;
73
+ return responseData.data;
74
+ }
75
+ catch (error) {
76
+ const axiosError = error;
77
+ if (axiosError.response) {
78
+ const response = axiosError.response;
79
+ const data = response.data;
80
+ console.error(`ecommerce-plan-allocation-service 异常: ${axiosError.message},requestId: ${requestId}`);
81
+ console.info('响应信息', data.message);
82
+ console.error('异常堆栈', JSON.stringify(error.stack));
83
+ throw error;
84
+ }
85
+ // 调用dns模块解析url
86
+ const dns = await Promise.resolve().then(() => __importStar(require('dns')));
87
+ const dnsPromise = new Promise((resolve, reject) => {
88
+ const lookupRes = dns.lookup(url, (err, address) => {
89
+ if (err) {
90
+ console.error(err.message);
91
+ reject(err);
92
+ }
93
+ console.info(`lookup: ${JSON.stringify(lookupRes)}`);
94
+ resolve(address);
95
+ });
96
+ });
97
+ try {
98
+ const address = await dnsPromise;
99
+ console.info(`address: ${JSON.stringify(address)}`);
100
+ }
101
+ catch (error) {
102
+ console.info(`error: ${JSON.stringify(error)}`);
103
+ }
104
+ console.error(`ecommerce-plan-allocation-service 未知异常: ${axiosError.message}`, error.stack);
105
+ throw error;
106
+ }
107
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 将驼峰命名转换为短横线命名
3
+ * @param str 驼峰命名字符串
4
+ * @returns 短横线命名字符串
5
+ * @example
6
+ * camelToKebabCase('fetchConfig') // 'fetch-config'
7
+ * camelToKebabCase('getCosTempSecret') // 'get-cos-temp-secret'
8
+ */
9
+ export declare function camelToKebabCase(str: string): string;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.camelToKebabCase = camelToKebabCase;
4
+ /**
5
+ * 将驼峰命名转换为短横线命名
6
+ * @param str 驼峰命名字符串
7
+ * @returns 短横线命名字符串
8
+ * @example
9
+ * camelToKebabCase('fetchConfig') // 'fetch-config'
10
+ * camelToKebabCase('getCosTempSecret') // 'get-cos-temp-secret'
11
+ */
12
+ function camelToKebabCase(str) {
13
+ return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
14
+ }