@hiliosai/sdk 0.1.10 → 0.1.12

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/package.json CHANGED
@@ -1,29 +1,25 @@
1
1
  {
2
2
  "name": "@hiliosai/sdk",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
7
7
  },
8
8
  "scripts": {
9
9
  "build": "tsup",
10
- "format": "bunx prettier --write ./**/*.{ts,json,md,yaml}",
10
+ "format": "bunx prettier --write ./**/*.{ts,json}",
11
11
  "typecheck": "tsc --noEmit"
12
12
  },
13
13
  "dependencies": {
14
- "@keyv/redis": "5.1.5",
15
14
  "@ltv/env": "4.0.3",
16
15
  "@moleculer/channels": "0.2.0",
17
- "keyv": "5.5.5",
18
- "lodash": "4.17.21",
19
16
  "moleculer": "0.14.35"
20
17
  },
21
18
  "devDependencies": {
22
19
  "@hiliosai/prettier": "workspace:*",
23
20
  "@hiliosai/typescript": "workspace:*",
24
21
  "@pkg/dev-utils": "workspace:*",
25
- "@types/lodash": "4.17.21",
26
22
  "bun-types": "latest"
27
23
  },
28
24
  "prettier": "@hiliosai/prettier"
29
- }
25
+ }
@@ -1,7 +1,11 @@
1
1
  import env from '@ltv/env';
2
- import type {BrokerOptions} from 'moleculer';
2
+ import type {BrokerOptions, Middleware} from 'moleculer';
3
3
  import os from 'os';
4
4
 
5
+ import {
6
+ ContextHelpersMiddleware,
7
+ PermissionsMiddleware,
8
+ } from '../../middlewares';
5
9
  import {bulkheadConfig} from './bulkhead';
6
10
  import {ChannelsMiddleware} from './channels';
7
11
  import {circuitBreakerConfig} from './circuit-breaker';
@@ -79,7 +83,16 @@ const configs: BrokerOptions = {
79
83
  // Enable built-in tracing function. More info: https://moleculer.services/docs/0.14/tracing.html
80
84
  tracing: tracingConfig,
81
85
 
82
- middlewares: [ChannelsMiddleware],
86
+ middlewares: [
87
+ ChannelsMiddleware,
88
+ PermissionsMiddleware as Middleware,
89
+ ContextHelpersMiddleware as Middleware,
90
+ ],
83
91
  };
84
92
 
93
+ export const createConfig = (customConfig: BrokerOptions) => ({
94
+ ...configs,
95
+ ...customConfig,
96
+ });
97
+
85
98
  export default configs;
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  /**
2
3
  * Soft Delete Extension for Prisma
3
4
  * Automatically handles soft deletes by setting deletedAt instead of removing records
@@ -1,18 +1,19 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  /**
2
3
  * Multi-Tenant Extension for Prisma
3
4
  * Automatically filters queries by tenant context for multi-tenant applications
4
- *
5
+ *
5
6
  * Usage:
6
7
  * ```typescript
7
8
  * import { createTenantExtension } from './extensions/tenant.extension';
8
- *
9
+ *
9
10
  * class MyPrismaDS extends PrismaDatasource<PrismaClient> {
10
11
  * protected applyExtensions(client: PrismaClient) {
11
12
  * return client.$extends(createTenantExtension());
12
13
  * }
13
14
  * }
14
15
  * ```
15
- *
16
+ *
16
17
  * Requires models to have `tenantId String` field
17
18
  */
18
19
 
@@ -27,71 +28,71 @@ export function createTenantExtension() {
27
28
 
28
29
  return {
29
30
  name: 'Tenant',
30
-
31
+
31
32
  client: {
32
33
  // Set tenant context
33
34
  $setTenant(tenantId: string) {
34
35
  tenantContext.currentTenantId = tenantId;
35
36
  },
36
-
37
+
37
38
  // Get current tenant
38
39
  $getCurrentTenant() {
39
40
  return tenantContext.currentTenantId;
40
41
  },
41
-
42
+
42
43
  // Clear tenant context
43
44
  $clearTenant() {
44
45
  tenantContext.currentTenantId = null;
45
46
  },
46
47
  },
47
-
48
+
48
49
  query: {
49
50
  $allModels: {
50
51
  // Automatically add tenantId filter to all read operations
51
- async findMany({ args, query }: any) {
52
+ async findMany({args, query}: any) {
52
53
  if (tenantContext.currentTenantId) {
53
54
  args.where ??= {};
54
55
  args.where.tenantId ??= tenantContext.currentTenantId;
55
56
  }
56
57
  return query(args);
57
58
  },
58
-
59
- async findUnique({ args, query }: any) {
59
+
60
+ async findUnique({args, query}: any) {
60
61
  if (tenantContext.currentTenantId) {
61
62
  args.where ??= {};
62
63
  args.where.tenantId ??= tenantContext.currentTenantId;
63
64
  }
64
65
  return query(args);
65
66
  },
66
-
67
- async findFirst({ args, query }: any) {
67
+
68
+ async findFirst({args, query}: any) {
68
69
  if (tenantContext.currentTenantId) {
69
70
  args.where ??= {};
70
71
  args.where.tenantId ??= tenantContext.currentTenantId;
71
72
  }
72
73
  return query(args);
73
74
  },
74
-
75
+
75
76
  // Automatically add tenantId to create operations
76
- async create({ args, query }: any) {
77
+ async create({args, query}: any) {
77
78
  if (tenantContext.currentTenantId) {
78
79
  args.data ??= {};
79
80
  args.data.tenantId ??= tenantContext.currentTenantId;
80
81
  }
81
82
  return query(args);
82
83
  },
83
-
84
+
84
85
  // Add tenantId filter to update operations
85
- async update({ args, query }: any) {
86
+ async update({args, query}: any) {
86
87
  if (tenantContext.currentTenantId) {
87
88
  args.where ??= {};
88
89
  args.where.tenantId ??= tenantContext.currentTenantId;
89
90
  }
90
91
  return query(args);
91
92
  },
92
-
93
+
93
94
  // Add tenantId filter to delete operations
94
- async delete({ args, query }: any) {
95
+ async delete({args, query}: any) {
95
96
  if (tenantContext.currentTenantId) {
96
97
  args.where ??= {};
97
98
  args.where.tenantId ??= tenantContext.currentTenantId;
@@ -101,4 +102,4 @@ export function createTenantExtension() {
101
102
  },
102
103
  },
103
104
  };
104
- }
105
+ }
package/src/index.ts CHANGED
@@ -6,4 +6,5 @@ export * from './configs';
6
6
  export * from './env';
7
7
  export * from './datasources';
8
8
  export * from './mixins';
9
+ export * from './utils';
9
10
  export {default as env} from './env';
@@ -9,11 +9,7 @@ import {
9
9
  } from '../types/channels';
10
10
  import type {AppContext} from '../types/context';
11
11
  import type {IntegrationConfig} from '../types/integration';
12
- import type {
13
- NormalizedMessage,
14
- SendResult,
15
- WebhookEvent,
16
- } from '../types/message';
12
+ import type {Message, SendResult, WebhookEvent} from '../types/message';
17
13
  import type {DatasourceInstanceTypes} from '../types/datasource';
18
14
  import type {
19
15
  IntegrationServiceConfig,
@@ -204,7 +200,7 @@ export function defineIntegration<
204
200
  ctx: AppContext<
205
201
  TDatasources,
206
202
  {
207
- message: NormalizedMessage;
203
+ message: Message;
208
204
  channelId: string;
209
205
  }
210
206
  >
@@ -1,10 +1,10 @@
1
- import omit from 'lodash/omit';
2
1
  import type {ServiceSchema as MoleculerServiceSchema} from 'moleculer';
3
2
 
4
3
  import {MemoizeMixin} from '../middlewares';
5
4
  import type {DatasourceConstructorRegistry} from '../middlewares/datasource.middleware';
6
5
  import {DatasourceMixin} from '../mixins';
7
6
  import type {ServiceConfig} from '../types/service';
7
+ import {omit} from '../utils';
8
8
 
9
9
  /**
10
10
  * Define a service
@@ -47,13 +47,10 @@ export function defineService<
47
47
  propsToOmit
48
48
  ) as unknown as MoleculerServiceSchema<TSettings>;
49
49
 
50
- const datasources = config.datasources ?? {};
51
-
52
- // TODO: Add mixins config support
53
50
  return {
54
51
  ...serviceSchema,
55
52
  mixins: [
56
- DatasourceMixin(datasources),
53
+ DatasourceMixin(config.datasources),
57
54
  MemoizeMixin(),
58
55
  ...(serviceSchema.mixins ?? []),
59
56
  ],
@@ -1,5 +1,5 @@
1
1
  import type {IntegrationPlatform} from './platform';
2
- import type {NormalizedMessage} from './message';
2
+ import type {Message} from './message';
3
3
 
4
4
  /**
5
5
  * Channel event payload types for reliable messaging via @moleculer/channels
@@ -10,7 +10,7 @@ export interface IntegrationMessageReceivedPayload {
10
10
  tenantId: string;
11
11
  channelId: string;
12
12
  platform: IntegrationPlatform;
13
- message: NormalizedMessage;
13
+ message: Message;
14
14
  timestamp: number;
15
15
  metadata?: Record<string, unknown>;
16
16
  }
@@ -29,7 +29,7 @@ export interface IntegrationMessageFailedPayload {
29
29
  channelId: string;
30
30
  platform: IntegrationPlatform;
31
31
  error: string; // Serialized error message
32
- message?: NormalizedMessage;
32
+ message?: Message;
33
33
  timestamp: number;
34
34
  retryCount?: number;
35
35
  metadata?: Record<string, unknown>;
@@ -59,22 +59,56 @@ export interface CarouselItem {
59
59
  buttons?: MessageButton[];
60
60
  }
61
61
 
62
- export interface NormalizedMessage {
62
+ export type MessageDirection = 'incoming' | 'outgoing' | 'internal';
63
+
64
+ export type MessageType =
65
+ | 'text'
66
+ | 'image'
67
+ | 'file'
68
+ | 'audio'
69
+ | 'video'
70
+ | 'location'
71
+ | 'contact'
72
+ | 'sticker'
73
+ | 'template';
74
+
75
+ export type MessageStatus =
76
+ | 'pending'
77
+ | 'sent'
78
+ | 'delivered'
79
+ | 'read'
80
+ | 'failed';
81
+
82
+ export interface Message {
63
83
  id: string;
64
84
  conversationId: string;
65
85
  from: MessageParticipant;
66
86
  to: MessageParticipant;
67
87
  content: MessageContent;
88
+ direction: MessageDirection;
89
+ type: MessageType;
90
+ status: MessageStatus;
68
91
  timestamp: number;
69
92
  platform: IntegrationPlatform;
70
93
  replyTo?: string;
71
94
  threadId?: string;
95
+ attachments?: MessageAttachment[];
96
+ metadata?: Record<string, any>;
97
+ }
98
+
99
+ export interface MessageAttachment {
100
+ id: string;
101
+ name: string;
102
+ type: string;
103
+ size: number;
104
+ url: string;
105
+ thumbnail?: string;
72
106
  metadata?: Record<string, any>;
73
107
  }
74
108
 
75
- export interface PlatformMessage {
109
+ export interface PlatformMessage<P = any> {
76
110
  platform: IntegrationPlatform;
77
- payload: any;
111
+ payload: P;
78
112
  }
79
113
 
80
114
  export interface WebhookEvent<TPayload = any> {
@@ -11,8 +11,7 @@ import type {DatasourceConstructorRegistry} from '../middlewares/datasource.midd
11
11
  import type {AppContext} from './context';
12
12
  import type {DatasourceInstanceTypes} from './datasource';
13
13
  import type {BaseSpec, IntegrationConfig} from './integration';
14
- import type {NormalizedMessage, SendResult, WebhookEvent} from './message';
15
-
14
+ import type {Message, SendResult, WebhookEvent} from './message';
16
15
 
17
16
  // Type to infer TypeScript types from Moleculer parameter schemas
18
17
  type InferParamsType<T> = T extends Record<string, any>
@@ -123,7 +122,7 @@ export type ServiceConfig<
123
122
  TSettings,
124
123
  DatasourceInstanceTypes<TDatasourceConstructors>
125
124
  > & {
126
- datasources?: TDatasourceConstructors;
125
+ datasources: TDatasourceConstructors;
127
126
  };
128
127
 
129
128
  // Integration-specific types
@@ -140,7 +139,7 @@ export interface IntegrationServiceConfig<
140
139
  // Core integration methods
141
140
  normalize<TPayload = any>(
142
141
  webhook: WebhookEvent<TPayload>
143
- ): Promise<NormalizedMessage[]>;
142
+ ): Promise<Message[]>;
144
143
  getChannelConfig(
145
144
  ctx: TContext,
146
145
  channelId: string
@@ -148,7 +147,7 @@ export interface IntegrationServiceConfig<
148
147
  validateWebhook?<TPayload = any>(webhook: WebhookEvent<TPayload>): boolean;
149
148
  sendMessage(
150
149
  ctx: TContext,
151
- message: NormalizedMessage,
150
+ message: Message,
152
151
  config: IntegrationConfig
153
152
  ): Promise<SendResult>;
154
153
 
@@ -181,7 +180,7 @@ export interface IntegrationServiceSchema<TSettings = unknown>
181
180
  spec: BaseSpec;
182
181
  normalize<TPayload = any>(
183
182
  webhook: WebhookEvent<TPayload>
184
- ): Promise<NormalizedMessage[]>;
183
+ ): Promise<Message[]>;
185
184
  getChannelConfig(
186
185
  ctx: AppContext,
187
186
  channelId: string
@@ -189,7 +188,7 @@ export interface IntegrationServiceSchema<TSettings = unknown>
189
188
  validateWebhook?<TPayload = any>(webhook: WebhookEvent<TPayload>): boolean;
190
189
  sendMessage(
191
190
  ctx: AppContext,
192
- message: NormalizedMessage,
191
+ message: Message,
193
192
  config: IntegrationConfig
194
193
  ): Promise<SendResult>;
195
194
  verifyWebhook?(params: {
@@ -0,0 +1,8 @@
1
+ export function omit<T extends Record<string, unknown>, K extends keyof T>(
2
+ obj: T,
3
+ keys: K[]
4
+ ): Omit<T, K> {
5
+ const result = {...obj};
6
+ keys.forEach((key) => delete result[key]);
7
+ return result;
8
+ }
package/tsup.config.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  import {defineConfig} from '@pkg/dev-utils';
3
2
 
4
3
  export default defineConfig({