@slates/provider-handler 1.0.0-rc.29 → 1.0.0-rc.31

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/src/spec.ts CHANGED
@@ -2,11 +2,13 @@ import { badRequestError, notFoundError, ServiceError } from '@lowerdeck/error';
2
2
  import type {
3
3
  SlateAuthenticationMethod,
4
4
  SlatesAction,
5
- SlateAdapter as SlatesAdapter
5
+ SlateAdapter as SlatesAdapter,
6
+ SlatesTriggerGroup
6
7
  } from '@slates/proto';
7
8
  import {
8
9
  type Slate,
9
10
  type SlateAdapter,
11
+ type SlateTrigger,
10
12
  SlateDefaultPollingIntervalSeconds
11
13
  } from '@slates/provider';
12
14
  import z from 'zod';
@@ -149,18 +151,89 @@ export let mapAction = <ConfigType extends {}, AuthType extends {}>(
149
151
  ...base,
150
152
  type: 'action.trigger',
151
153
  capabilities: {},
152
-
153
- invocation:
154
- a.source === 'polling'
155
- ? {
156
- type: 'polling',
157
- intervalSeconds: a.polling.intervalInSeconds ?? SlateDefaultPollingIntervalSeconds
158
- }
159
- : {
160
- type: 'webhook',
161
- autoRegistration: !!a.autoRegisterWebhook,
162
- autoUnregistration: !!a.autoUnregisterWebhook,
163
- http: a.http
164
- }
154
+ triggerGroupId: a.triggerGroup.key
165
155
  };
166
156
  };
157
+
158
+ export let getTriggerGroup = <ConfigType extends {}, AuthType extends {}>(
159
+ slate: Slate<ConfigType, AuthType>,
160
+ triggerGroupId: string
161
+ ) => {
162
+ let group = slate.triggerGroups.find(g => g.key === triggerGroupId);
163
+ if (!group) {
164
+ throw new ServiceError(notFoundError(`trigger_group`, triggerGroupId));
165
+ }
166
+
167
+ return group;
168
+ };
169
+
170
+ export let getTriggersForGroup = <ConfigType extends {}, AuthType extends {}>(
171
+ slate: Slate<ConfigType, AuthType>,
172
+ triggerGroupId: string
173
+ ): SlateTrigger<ConfigType, AuthType, any, any>[] =>
174
+ slate.actions.filter(
175
+ (action): action is SlateTrigger<ConfigType, AuthType, any, any> =>
176
+ action.type === 'trigger' && action.triggerGroup.key === triggerGroupId
177
+ );
178
+
179
+ export let evaluateTriggerMatches = <ConfigType extends {}, AuthType extends {}>(
180
+ slate: Slate<ConfigType, AuthType>,
181
+ triggerGroupId: string,
182
+ payload: unknown
183
+ ): string[] =>
184
+ getTriggersForGroup(slate, triggerGroupId)
185
+ .filter(trigger => trigger.matches(payload))
186
+ .map(trigger => trigger.key);
187
+
188
+ export let getWebhookAutoRegistration = <ConfigType extends {}, AuthType extends {}>(
189
+ group: ReturnType<typeof getTriggerGroup<ConfigType, AuthType>>
190
+ ) => {
191
+ if (group.source !== 'webhook' || !group.webhook?.autoRegistration) {
192
+ throw new ServiceError(
193
+ badRequestError({
194
+ message: `Trigger group does not support webhook auto-registration: ${group.key}`
195
+ })
196
+ );
197
+ }
198
+
199
+ return group.webhook.autoRegistration;
200
+ };
201
+
202
+ export let getWebhookManualRegistration = <ConfigType extends {}, AuthType extends {}>(
203
+ group: ReturnType<typeof getTriggerGroup<ConfigType, AuthType>>
204
+ ) => {
205
+ if (group.source !== 'webhook' || !group.webhook?.manualRegistration) {
206
+ throw new ServiceError(
207
+ badRequestError({
208
+ message: `Trigger group does not support manual webhook registration: ${group.key}`
209
+ })
210
+ );
211
+ }
212
+
213
+ return group.webhook.manualRegistration;
214
+ };
215
+
216
+ export let mapTriggerGroup = <ConfigType extends {}, AuthType extends {}>(
217
+ group: ReturnType<typeof getTriggerGroup<ConfigType, AuthType>>
218
+ ): SlatesTriggerGroup => ({
219
+ id: group.key,
220
+ name: group.name,
221
+ description: group.description,
222
+ metadata: group.metadata,
223
+ invocation:
224
+ group.source === 'polling'
225
+ ? {
226
+ type: 'polling',
227
+ intervalSeconds: group.polling?.intervalSeconds ?? SlateDefaultPollingIntervalSeconds
228
+ }
229
+ : {
230
+ type: 'webhook',
231
+ registration: group.webhook?.manualRegistration
232
+ ? {
233
+ mode: 'manual',
234
+ userConfigSchema: toJsonSchema(group.webhook.manualRegistration.userConfigSchema),
235
+ fullConfigSchema: toJsonSchema(group.webhook.manualRegistration.fullConfigSchema)
236
+ }
237
+ : { mode: 'auto' }
238
+ }
239
+ });