@mango-power/node-kit 0.0.3 → 0.0.5

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
@@ -8,6 +8,7 @@ Shared node toolkit package for MP services.
8
8
  - `PGClient`
9
9
  - `RedisClient`
10
10
  - `RmqService`
11
+ - `SLSClient`
11
12
  - `EmqxMqttService` / `EMQXMqttService`
12
13
 
13
14
  ## Build
@@ -33,6 +34,6 @@ registry=https://registry.npmjs.org/
33
34
  ## Install from other projects
34
35
 
35
36
  ```bash
36
- npm i @mango-power/node-kit@^0.0.3
37
+ npm i @mango-power/node-kit@^0.0.5
37
38
  ```
38
39
 
package/dist/index.d.ts CHANGED
@@ -2,5 +2,6 @@ export * from "./logger";
2
2
  export * from "./pg.service";
3
3
  export * from "./redis.service";
4
4
  export * from "./rmq.service";
5
+ export * from "./sls.service";
5
6
  export * from "./mqtt/mqtt.type";
6
7
  export * from "./mqtt/emqx-mqtt.service";
package/dist/index.js CHANGED
@@ -18,5 +18,6 @@ __exportStar(require("./logger"), exports);
18
18
  __exportStar(require("./pg.service"), exports);
19
19
  __exportStar(require("./redis.service"), exports);
20
20
  __exportStar(require("./rmq.service"), exports);
21
+ __exportStar(require("./sls.service"), exports);
21
22
  __exportStar(require("./mqtt/mqtt.type"), exports);
22
23
  __exportStar(require("./mqtt/emqx-mqtt.service"), exports);
@@ -5,7 +5,7 @@ export interface MqttConfig {
5
5
  clientId?: string;
6
6
  username?: string;
7
7
  password?: string;
8
- will?: any;
8
+ will?: mqtt.IClientOptions["will"];
9
9
  }
10
10
  export type MqttQos = 0 | 1 | 2;
11
11
  export interface MqttMessageContext {
@@ -5,13 +5,13 @@ export declare class PGClientConfig {
5
5
  database: string;
6
6
  username: string;
7
7
  password: string;
8
- ssl?: any;
8
+ ssl?: boolean;
9
9
  max?: number;
10
10
  }
11
11
  export declare class PGClient {
12
12
  private pool;
13
13
  init(config: PGClientConfig): Promise<void>;
14
- query(sql: string, params?: any[]): Promise<any[]>;
15
- queryOne(sql: string, params?: any[]): Promise<any>;
16
- transaction(callback: (client: pg.PoolClient) => any): Promise<void>;
14
+ query<T = any>(sql: string, params?: readonly unknown[]): Promise<T[]>;
15
+ queryOne<T = any>(sql: string, params?: readonly unknown[]): Promise<T>;
16
+ transaction<T>(callback: (client: pg.PoolClient) => Promise<T> | T): Promise<T>;
17
17
  }
@@ -68,7 +68,7 @@ class PGClient {
68
68
  let error;
69
69
  const client = await this.pool.connect();
70
70
  try {
71
- const { rows } = await client.query(sql, params);
71
+ const { rows } = params ? await client.query(sql, [...params]) : await client.query(sql);
72
72
  result = (0, humps_1.camelizeKeys)(rows);
73
73
  }
74
74
  catch (e) {
@@ -87,8 +87,8 @@ class PGClient {
87
87
  let result;
88
88
  const client = await this.pool.connect();
89
89
  try {
90
- const { rows } = await client.query(sql, params);
91
- result = (0, humps_1.camelizeKeys)(rows[0]) || {};
90
+ const { rows } = params ? await client.query(sql, [...params]) : await client.query(sql);
91
+ result = ((0, humps_1.camelizeKeys)(rows[0]) || {});
92
92
  }
93
93
  catch (e) {
94
94
  console.error("db error.", e);
@@ -103,9 +103,10 @@ class PGClient {
103
103
  const client = await this.pool.connect();
104
104
  await client.query("BEGIN");
105
105
  try {
106
- await callback(client);
106
+ const result = await callback(client);
107
107
  await client.query("COMMIT");
108
108
  await client.release();
109
+ return result;
109
110
  }
110
111
  catch (e) {
111
112
  console.error("Transaction error.", e);
@@ -1,21 +1,21 @@
1
1
  import Redis from "ioredis";
2
+ import { RedisOptions } from "ioredis";
2
3
  export declare class RedisClientConfig {
3
4
  host: string;
4
5
  port: number;
5
6
  db: number;
6
7
  username?: string;
7
8
  password?: string;
8
- tls?: any;
9
+ tls?: RedisOptions["tls"];
9
10
  keyPrefix?: string;
10
11
  cluster?: boolean;
11
12
  }
12
13
  export declare class RedisClient {
13
14
  client: Redis;
14
- private config;
15
15
  init(config: RedisClientConfig): Promise<void>;
16
- set(key: string, value: any, expire?: number): Promise<void>;
16
+ set(key: string, value: string | number | Buffer, expire?: number): Promise<void>;
17
17
  get(key: string): Promise<string | null>;
18
- setJsonValue(key: string, value: any): Promise<"OK">;
19
- getJsonValue(key: string): Promise<any>;
18
+ setJsonValue<T>(key: string, value: T): Promise<"OK" | null>;
19
+ getJsonValue<T>(key: string): Promise<T | null>;
20
20
  exists(key: string): Promise<number>;
21
21
  }
@@ -9,20 +9,15 @@ class RedisClientConfig {
9
9
  }
10
10
  exports.RedisClientConfig = RedisClientConfig;
11
11
  class RedisClient {
12
- constructor() {
13
- this.config = null;
14
- }
15
12
  async init(config) {
16
- this.config = config;
17
13
  try {
18
- this.client = new ioredis_1.default({
14
+ const redisOptions = {
19
15
  host: config.host,
20
16
  port: config.port,
21
17
  db: config.db,
22
18
  username: config.username,
23
19
  password: config.password,
24
20
  disableClientInfo: true,
25
- tls: config.tls || false,
26
21
  reconnectOnError: (err) => {
27
22
  console.warn("Reconnect on error:", err);
28
23
  return true;
@@ -32,7 +27,11 @@ class RedisClient {
32
27
  console.info(`Retrying connection in ${delay}ms`);
33
28
  return delay;
34
29
  },
35
- });
30
+ };
31
+ if (config.tls) {
32
+ redisOptions.tls = config.tls;
33
+ }
34
+ this.client = new ioredis_1.default(redisOptions);
36
35
  this.client.on("error", (err) => {
37
36
  console.error("Redis Error:", err);
38
37
  });
@@ -60,14 +59,16 @@ class RedisClient {
60
59
  }
61
60
  async getJsonValue(key) {
62
61
  const s = await this.client.get(key);
63
- let r = {};
62
+ if (!s) {
63
+ return null;
64
+ }
64
65
  try {
65
- r = JSON.parse(s);
66
+ return JSON.parse(s);
66
67
  }
67
68
  catch (e) {
68
69
  console.error(e);
70
+ return null;
69
71
  }
70
- return r;
71
72
  }
72
73
  async exists(key) {
73
74
  return await this.client.exists(key);
@@ -1,5 +1,7 @@
1
1
  import * as amqp from "amqplib";
2
2
  import { Channel } from "amqplib";
3
+ export type ConsumeMessage = amqp.ConsumeMessage;
4
+ export type RmqConsumeCallback = (data: ConsumeMessage, channel: Channel) => Promise<void> | void;
3
5
  export declare class QueueItem {
4
6
  queue: string;
5
7
  prefetch?: number;
@@ -39,6 +41,6 @@ export declare class RmqService {
39
41
  private restoreConsumers;
40
42
  private startConsume;
41
43
  private closeAndClearChannels;
42
- publishToExchange(exchange: string, routingKey: string | undefined, message: any, options?: amqp.Options.Publish): Promise<void>;
43
- consumer(queue: string, callback: (data: amqp.ConsumeMessage, channel: Channel) => any): Promise<void>;
44
+ publishToExchange(exchange: string, routingKey: string | undefined, message: unknown, options?: amqp.Options.Publish): Promise<void>;
45
+ consumer(queue: string, callback: RmqConsumeCallback): Promise<void>;
44
46
  }
@@ -0,0 +1,12 @@
1
+ export interface SLSServiceConfig {
2
+ endpoint: string;
3
+ accessKeyId: string;
4
+ accessKeySecret: string;
5
+ }
6
+ export interface SlsLogClient {
7
+ postLogStoreLogs: (projectName: string, logstoreName: string, logGroup: unknown) => Promise<unknown>;
8
+ }
9
+ export declare class SLSClient {
10
+ client: SlsLogClient;
11
+ init(config: SLSServiceConfig): Promise<void>;
12
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SLSClient = void 0;
4
+ const Client = require("@alicloud/log");
5
+ class SLSClient {
6
+ async init(config) {
7
+ this.client = new Client({
8
+ accessKeyId: config.accessKeyId,
9
+ accessKeySecret: config.accessKeySecret,
10
+ endpoint: config.endpoint,
11
+ });
12
+ }
13
+ }
14
+ exports.SLSClient = SLSClient;
@@ -12,12 +12,14 @@ const index_1 = require("../index");
12
12
  const pg = new index_1.PGClient();
13
13
  const redis = new index_1.RedisClient();
14
14
  const rmq = new index_1.RmqService();
15
+ const sls = new index_1.SLSClient();
15
16
  const mqtt = new index_1.EmqxMqttService();
16
17
  const mqttCompat = new index_1.EMQXMqttService();
17
18
  const runner = new index_1.MqttTopicRunner();
18
19
  strict_1.default.ok(pg);
19
20
  strict_1.default.ok(redis);
20
21
  strict_1.default.ok(rmq);
22
+ strict_1.default.ok(sls);
21
23
  strict_1.default.ok(mqtt);
22
24
  strict_1.default.ok(mqttCompat);
23
25
  strict_1.default.ok(runner.id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mango-power/node-kit",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Shared node toolkit for mp services",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -25,6 +25,7 @@
25
25
  ],
26
26
  "license": "ISC",
27
27
  "dependencies": {
28
+ "@alicloud/log": "^1.2.6",
28
29
  "amqplib": "^0.10.9",
29
30
  "humps": "^2.0.1",
30
31
  "ioredis": "^5.8.1",