@whitewall/blip-sdk 0.0.135 → 0.0.137

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.
Files changed (56) hide show
  1. package/dist/cjs/client.js +1 -1
  2. package/dist/cjs/client.js.map +1 -1
  3. package/dist/esm/client.js +1 -1
  4. package/dist/esm/client.js.map +1 -1
  5. package/dist/types/client.d.ts +2 -2
  6. package/dist/types/client.d.ts.map +1 -1
  7. package/package.json +2 -2
  8. package/src/client.ts +117 -0
  9. package/src/index.ts +6 -0
  10. package/src/namespaces/account.ts +729 -0
  11. package/src/namespaces/activecampaign.ts +285 -0
  12. package/src/namespaces/analytics.ts +230 -0
  13. package/src/namespaces/billing.ts +17 -0
  14. package/src/namespaces/builder.ts +52 -0
  15. package/src/namespaces/configurations.ts +19 -0
  16. package/src/namespaces/context.ts +67 -0
  17. package/src/namespaces/desk.ts +679 -0
  18. package/src/namespaces/media.ts +39 -0
  19. package/src/namespaces/namespace.ts +125 -0
  20. package/src/namespaces/plugins.ts +223 -0
  21. package/src/namespaces/portal.ts +402 -0
  22. package/src/namespaces/scheduler.ts +88 -0
  23. package/src/namespaces/whatsapp.ts +383 -0
  24. package/src/sender/bliperror.ts +42 -0
  25. package/src/sender/enveloperesolver.ts +148 -0
  26. package/src/sender/gateway/customgatewaysender.ts +43 -0
  27. package/src/sender/http/httpsender.ts +94 -0
  28. package/src/sender/index.ts +7 -0
  29. package/src/sender/plugin/communication.ts +72 -0
  30. package/src/sender/plugin/pluginsender.ts +75 -0
  31. package/src/sender/security.ts +33 -0
  32. package/src/sender/sender.ts +145 -0
  33. package/src/sender/sessionnegotiator.ts +175 -0
  34. package/src/sender/tcp/tcpsender.ts +252 -0
  35. package/src/sender/throttler.ts +36 -0
  36. package/src/sender/websocket/websocketsender.ts +175 -0
  37. package/src/types/account.ts +84 -0
  38. package/src/types/analytics.ts +18 -0
  39. package/src/types/billing.ts +15 -0
  40. package/src/types/command.ts +47 -0
  41. package/src/types/commons.ts +16 -0
  42. package/src/types/desk.ts +51 -0
  43. package/src/types/envelope.ts +9 -0
  44. package/src/types/flow.ts +327 -0
  45. package/src/types/index.ts +13 -0
  46. package/src/types/message.ts +116 -0
  47. package/src/types/node.ts +86 -0
  48. package/src/types/notification.ts +18 -0
  49. package/src/types/plugins.ts +51 -0
  50. package/src/types/portal.ts +39 -0
  51. package/src/types/reason.ts +22 -0
  52. package/src/types/session.ts +22 -0
  53. package/src/types/whatsapp.ts +84 -0
  54. package/src/utils/odata.ts +114 -0
  55. package/src/utils/random.ts +3 -0
  56. package/src/utils/uri.ts +46 -0
@@ -0,0 +1,125 @@
1
+ import type { BlipClient } from '../client.ts'
2
+ import { ConnectionSender } from '../sender/sender.ts'
3
+ import { type Command, type CommandMethods, type Domain, type Identity, Node, type NodeLike } from '../types/index.ts'
4
+ import { randomId } from '../utils/random.ts'
5
+ import { type URI, uriToString } from '../utils/uri.ts'
6
+
7
+ export type SendCommandOptions = {
8
+ to?: NodeLike
9
+ ownerIdentity?: Identity | Domain
10
+ domain?: Domain
11
+ }
12
+
13
+ export type ConsumeOptions = SendCommandOptions & {
14
+ take?: number
15
+ skip?: number
16
+ fetchall?: boolean
17
+ max?: number
18
+ // If true, it will try to parallel fetch data in chunks
19
+ // Be careful as this can be slower in situations with few data and will consume much more bandwidth
20
+ optimistic?: boolean
21
+ }
22
+
23
+ export class Namespace {
24
+ constructor(
25
+ protected readonly blipClient: BlipClient,
26
+ private readonly ownerSubdomain: string,
27
+ private readonly defaultOptions?: ConsumeOptions,
28
+ ) {}
29
+
30
+ get identity() {
31
+ return `postmaster@${this.ownerSubdomain ? `${this.ownerSubdomain}.` : ''}${this.defaultOptions?.domain ?? 'msging.net'}` as Identity
32
+ }
33
+
34
+ public async sendCommand<
35
+ TMethod extends CommandMethods,
36
+ TResponse = void,
37
+ TCollection = TResponse extends Array<unknown> ? true : false,
38
+ >(
39
+ command: Omit<Command<TMethod>, 'uri' | 'id' | 'to'> & {
40
+ uri: URI
41
+ },
42
+ opts?: ConsumeOptions & {
43
+ collection?: TCollection extends true ? true : never
44
+ },
45
+ ): Promise<TResponse> {
46
+ const options = { ...this.defaultOptions, ...opts }
47
+
48
+ let path = command.uri.path
49
+ const query = command.uri.query
50
+
51
+ const take = options?.take ?? Math.min(options?.max ?? 100, 100)
52
+
53
+ if (options?.collection) {
54
+ query.set('$take', take.toString())
55
+ }
56
+
57
+ if (options?.ownerIdentity) {
58
+ path = `lime://${options.ownerIdentity}${path}`
59
+ }
60
+
61
+ const domain =
62
+ options?.domain ??
63
+ (this.blipClient.sender instanceof ConnectionSender ? this.blipClient.sender.domain : 'msging.net')
64
+ const owner = new Node('postmaster', `${this.ownerSubdomain ? `${this.ownerSubdomain}.` : ''}${domain}`)
65
+
66
+ if (!options.collection) {
67
+ const commandToSend = {
68
+ id: randomId(),
69
+ to: options.to ?? owner,
70
+ ...command,
71
+ uri: uriToString({ path, query }),
72
+ } as Command<TMethod>
73
+ const response = await this.blipClient.sender.sendCommand(commandToSend)
74
+ return response as TResponse
75
+ }
76
+
77
+ if (options.optimistic && !options.max && !options.fetchall) {
78
+ throw new Error('Optimistic consume requires max or fetchall to be set')
79
+ }
80
+
81
+ let skip = options.skip ?? 0
82
+ let results: Array<TResponse> = []
83
+
84
+ while (true) {
85
+ const remaining = options.max !== undefined ? options.max - results.length : Number.POSITIVE_INFINITY
86
+ const potentialBatches = Math.ceil(remaining / take)
87
+ const batchSize = options.optimistic ? Math.min(potentialBatches, 20) : 1
88
+ const tasks = Array.from({ length: batchSize }, async (_, i: number) => {
89
+ const currentQuery = new Map(query)
90
+ currentQuery.set('$skip', (skip + i * take).toString())
91
+
92
+ const commandToSend = {
93
+ id: randomId(),
94
+ to: options.to ?? owner,
95
+ ...command,
96
+ uri: uriToString({ path, query: currentQuery }),
97
+ } as Command<TMethod>
98
+
99
+ const response = (await this.blipClient.sender.sendCommand(commandToSend)) as
100
+ | { data: Array<TResponse> }
101
+ | { items: Array<TResponse> }
102
+ const items: Array<TResponse> =
103
+ 'items' in response ? response.items : 'data' in response ? response.data : []
104
+
105
+ return items ?? []
106
+ })
107
+
108
+ const items = await Promise.all(tasks)
109
+ results = results.concat(items.flat())
110
+
111
+ if (
112
+ !options.fetchall ||
113
+ results.length % take !== 0 ||
114
+ results.length === 0 ||
115
+ (options.max !== undefined && results.length >= options.max)
116
+ ) {
117
+ break
118
+ }
119
+
120
+ skip += take * batchSize
121
+ }
122
+
123
+ return results as TResponse
124
+ }
125
+ }
@@ -0,0 +1,223 @@
1
+ import type { BlipClient } from '../client.ts'
2
+ import type { BlipLanguage } from '../types/account.ts'
3
+ import type { Identity } from '../types/node.ts'
4
+ import type { DetailedPlugin, Plugin, PluginSubscription } from '../types/plugins.ts'
5
+ import type { Application } from '../types/portal.ts'
6
+ import { uri } from '../utils/uri.ts'
7
+ import { type ConsumeOptions, Namespace, type SendCommandOptions } from './namespace.ts'
8
+
9
+ export class PluginsNamespace extends Namespace {
10
+ constructor(blipClient: BlipClient, defaultOptions?: SendCommandOptions) {
11
+ super(blipClient, 'plugins', { ...defaultOptions, domain: 'blip.ai' })
12
+ }
13
+
14
+ public getTenantApplicationsWithPlugin(
15
+ tenantId: string,
16
+ pluginId: string,
17
+ opts?: ConsumeOptions,
18
+ ): Promise<Array<Application>> {
19
+ return this.sendCommand(
20
+ {
21
+ method: 'get',
22
+ uri: uri`/tenants/${tenantId}/plugins/${pluginId}/applications`,
23
+ },
24
+ {
25
+ ...opts,
26
+ collection: true,
27
+ // This route doesn't handle pagination correctly, it will always fetch all
28
+ fetchall: false,
29
+ },
30
+ )
31
+ }
32
+
33
+ public getInstalledPlugins(tenantId: string, opts?: ConsumeOptions): Promise<Array<Plugin>> {
34
+ return this.sendCommand(
35
+ {
36
+ method: 'get',
37
+ uri: uri`/tenants/${tenantId}/plugins?${{ charge: 'all', onlySubscribed: 'true' }}`,
38
+ },
39
+ {
40
+ collection: true,
41
+ // This route doesn't handle pagination correctly, it will always fetch all
42
+ fetchall: false,
43
+ ...opts,
44
+ },
45
+ )
46
+ }
47
+
48
+ /**
49
+ * @param tenantId - The tenant id to use when fetching the plugins, only works as blip.ai user and the user should be a member of the tenant
50
+ */
51
+ public getPlugins(tenantId?: string, opts?: ConsumeOptions): Promise<Array<Plugin>> {
52
+ return this.sendCommand(
53
+ {
54
+ method: 'get',
55
+ uri: tenantId
56
+ ? uri`/tenants/${tenantId}/plugins?${{ charge: 'all' }}`
57
+ : uri`/plugins?${{ charge: 'all' }}`,
58
+ },
59
+ {
60
+ collection: true,
61
+ // This route doesn't handle pagination correctly, it will always fetch all
62
+ fetchall: false,
63
+ ...opts,
64
+ },
65
+ )
66
+ }
67
+
68
+ public getPlugin(
69
+ pluginId: string,
70
+ query?: { returnInstalledBots?: boolean; language?: BlipLanguage },
71
+ opts?: ConsumeOptions,
72
+ ): Promise<DetailedPlugin> {
73
+ return this.sendCommand(
74
+ {
75
+ method: 'get',
76
+ uri: uri`/plugins/${pluginId}?${{ language: query?.language, returnInstalledBots: query?.returnInstalledBots }}`,
77
+ },
78
+ opts,
79
+ )
80
+ }
81
+
82
+ public getPluginSubscription(
83
+ tenantId: string,
84
+ pluginId: string,
85
+ opts?: ConsumeOptions,
86
+ ): Promise<PluginSubscription> {
87
+ return this.sendCommand(
88
+ {
89
+ method: 'get',
90
+ uri: uri`/tenants/${tenantId}/plugins/${pluginId}/subscription`,
91
+ },
92
+ opts,
93
+ )
94
+ }
95
+
96
+ /**
97
+ * @param settings.hasPartnerService - Whether the plugin has a partner service (default value: false)
98
+ * @param settings.language - The language to send the communication emails (default value: pt)
99
+ * @param settings.plan - If the plugin has multiple plans, the plan to subscribe to
100
+ */
101
+ public subscribeToPlugin(
102
+ tenantId: string,
103
+ pluginId: string,
104
+ settings: {
105
+ plan: number
106
+ hasPartnerService?: boolean
107
+ language?: BlipLanguage
108
+ },
109
+ opts?: ConsumeOptions,
110
+ ): Promise<void> {
111
+ return this.sendCommand(
112
+ {
113
+ method: 'set',
114
+ type: 'application/vnd.iris.plugins.subscribe+json',
115
+ uri: uri`/tenants/${tenantId}/plugins/${pluginId}/subscribe`,
116
+ resource: {
117
+ hasPartnerService: settings.hasPartnerService ?? false,
118
+ language: settings.language ?? 'pt',
119
+ plan: settings.plan,
120
+ pluginId,
121
+ // That means that it will be charged to the tenant, not sure if exists other values
122
+ chargeType: 3,
123
+ },
124
+ },
125
+ opts,
126
+ )
127
+ }
128
+
129
+ public upgradePluginSubscription(
130
+ tenantId: string,
131
+ pluginId: string,
132
+ settings: {
133
+ plan: number
134
+ language?: BlipLanguage
135
+ },
136
+ opts?: ConsumeOptions,
137
+ ): Promise<void> {
138
+ return this.sendCommand(
139
+ {
140
+ method: 'set',
141
+ type: 'application/vnd.iris.plugins.subscribe+json',
142
+ uri: uri`/tenants/${tenantId}/plugins/${pluginId}/upgrade`,
143
+ resource: {
144
+ pluginId,
145
+ plan: settings.plan,
146
+ language: settings.language ?? 'pt',
147
+ },
148
+ },
149
+ opts,
150
+ )
151
+ }
152
+
153
+ public cancelPluginSubscription(
154
+ tenantId: string,
155
+ pluginId: string,
156
+ settings?: {
157
+ language?: BlipLanguage
158
+ },
159
+ opts?: ConsumeOptions,
160
+ ): Promise<void> {
161
+ return this.sendCommand(
162
+ {
163
+ method: 'delete',
164
+ uri: uri`/tenants/${tenantId}/plugins/${pluginId}/cancel?${{ language: settings?.language ?? 'pt' }}`,
165
+ },
166
+ opts,
167
+ )
168
+ }
169
+
170
+ /**
171
+ * @param settings.language - The language to send the communication emails (default value: pt)
172
+ * @param settings.applicationIdentity - The application identity to install the plugin
173
+ */
174
+ public installPlugin(
175
+ tenantId: string,
176
+ pluginId: string,
177
+ settings: {
178
+ applicationIdentity: Identity
179
+ language?: BlipLanguage
180
+ },
181
+ opts?: ConsumeOptions,
182
+ ): Promise<void> {
183
+ return this.sendCommand(
184
+ {
185
+ method: 'set',
186
+ type: 'application/vnd.iris.plugins.install+json',
187
+ uri: uri`/tenants/${tenantId}/plugins/${pluginId}/install`,
188
+ resource: {
189
+ applicationIdentity: settings.applicationIdentity,
190
+ language: settings.language ?? 'pt',
191
+ },
192
+ },
193
+ opts,
194
+ )
195
+ }
196
+
197
+ /**
198
+ * @param settings.language - The language to send the communication emails (default value: pt)
199
+ * @param settings.applicationIdentity - The application identity to uninstall the plugin
200
+ */
201
+ public uninstallPlugin(
202
+ tenantId: string,
203
+ pluginId: string,
204
+ settings: {
205
+ applicationIdentity: Identity
206
+ language?: BlipLanguage
207
+ },
208
+ opts?: ConsumeOptions,
209
+ ): Promise<void> {
210
+ return this.sendCommand(
211
+ {
212
+ method: 'set',
213
+ type: 'application/vnd.iris.plugins.install+json',
214
+ uri: uri`/tenants/${tenantId}/plugins/${pluginId}/uninstall`,
215
+ resource: {
216
+ applicationIdentity: settings.applicationIdentity,
217
+ language: settings.language ?? 'pt',
218
+ },
219
+ },
220
+ opts,
221
+ )
222
+ }
223
+ }