Creates a rule

Creates a rule for the application with the specified application ID.

Script ably Verified

by hugo697 ยท 10/17/2025

The script

Submitted by hugo697 Bun
Verified 220 days ago
1
//native
2
type Ably = {
3
  accessToken: string;
4
};
5
/**
6
 * Creates a rule
7
 * Creates a rule for the application with the specified application ID.
8
 */
9
export async function main(
10
  auth: Ably,
11
  app_id: string,
12
  body:
13
    | {
14
        status?: string;
15
        ruleType: "http";
16
        requestMode: string;
17
        source: { channelFilter: string; type: string };
18
        target: {
19
          url: string;
20
          headers?: { name?: string; value?: string }[];
21
          signingKeyId?: string;
22
          enveloped?: false | true;
23
          format: string;
24
        };
25
      }
26
    | {
27
        status?: string;
28
        ruleType: "http/ifttt";
29
        requestMode: string;
30
        source: { channelFilter: string; type: string };
31
        target: { webhookKey: string; eventName: string };
32
      }
33
    | {
34
        status?: string;
35
        ruleType: "http/zapier";
36
        requestMode: string;
37
        source: { channelFilter: string; type: string };
38
        target: {
39
          url: string;
40
          headers?: { name?: string; value?: string }[];
41
          signingKeyId?: string;
42
        };
43
      }
44
    | {
45
        status?: string;
46
        ruleType: "http/cloudflare-worker";
47
        requestMode: string;
48
        source: { channelFilter: string; type: string };
49
        target: {
50
          url: string;
51
          headers?: { name?: string; value?: string }[];
52
          signingKeyId?: string;
53
        };
54
      }
55
    | {
56
        status?: string;
57
        ruleType: "http/azure-function";
58
        requestMode: string;
59
        source: { channelFilter: string; type: string };
60
        target: {
61
          azureAppId: string;
62
          azureFunctionName: string;
63
          headers?: { name?: string; value?: string }[];
64
          signingKeyId?: string;
65
          enveloped?: false | true;
66
          format?: string;
67
        };
68
      }
69
    | {
70
        ruleType: "http/google-cloud-function";
71
        requestMode: string;
72
        source: { channelFilter: string; type: string };
73
        target: {
74
          region: string;
75
          projectId: string;
76
          functionName: string;
77
          headers?: { name?: string; value?: string }[];
78
          signingKeyId?: string;
79
          enveloped?: false | true;
80
          format?: string;
81
        };
82
      }
83
    | {
84
        status?: string;
85
        ruleType: "aws/lambda";
86
        requestMode: string;
87
        source: { channelFilter: string; type: string };
88
        target: {
89
          region: string;
90
          functionName: string;
91
          authentication:
92
            | {
93
                authenticationMode?: "credentials";
94
                accessKeyId: string;
95
                secretAccessKey: string;
96
              }
97
            | { authenticationMode?: "assumeRole"; assumeRoleArn: string };
98
          enveloped?: false | true;
99
        };
100
      }
101
    | {
102
        status?: string;
103
        ruleType: "aws/kinesis";
104
        requestMode: string;
105
        source: { channelFilter: string; type: string };
106
        target: {
107
          region: string;
108
          streamName: string;
109
          partitionKey: string;
110
          authentication:
111
            | {
112
                authenticationMode?: "credentials";
113
                accessKeyId: string;
114
                secretAccessKey: string;
115
              }
116
            | { authenticationMode?: "assumeRole"; assumeRoleArn: string };
117
          enveloped?: false | true;
118
          format: string;
119
        };
120
      }
121
    | {
122
        status?: string;
123
        ruleType: "aws/sqs";
124
        requestMode: string;
125
        source: { channelFilter: string; type: string };
126
        target: {
127
          region: string;
128
          awsAccountId: string;
129
          queueName: string;
130
          authentication:
131
            | {
132
                authenticationMode?: "credentials";
133
                accessKeyId: string;
134
                secretAccessKey: string;
135
              }
136
            | { authenticationMode?: "assumeRole"; assumeRoleArn: string };
137
          enveloped?: false | true;
138
          format?: string;
139
        };
140
      }
141
    | {
142
        status?: string;
143
        ruleType: "amqp";
144
        requestMode: string;
145
        source: { channelFilter: string; type: string };
146
        target: {
147
          queueId: string;
148
          headers?: { name?: string; value?: string }[];
149
          enveloped?: false | true;
150
          format?: string;
151
        };
152
      }
153
    | {
154
        ruleType: "amqp/external";
155
        requestMode: string;
156
        source: { channelFilter: string; type: string };
157
        target: {
158
          url: string;
159
          routingKey: string;
160
          mandatoryRoute: false | true;
161
          persistentMessages: false | true;
162
          messageTtl?: number;
163
          headers?: { name?: string; value?: string }[];
164
          enveloped?: false | true;
165
          format?: string;
166
        };
167
      }
168
    | {
169
        status?: string;
170
        ruleType: "kafka";
171
        requestMode: string;
172
        source: { channelFilter: string; type: string };
173
        target: {
174
          routingKey: string;
175
          brokers: string[];
176
          auth: {
177
            sasl?: {
178
              mechanism?: "plain" | "scram-sha-256" | "scram-sha-512";
179
              username?: string;
180
              password?: string;
181
            };
182
          };
183
          enveloped?: false | true;
184
          format?: string;
185
        };
186
      }
187
    | {
188
        status?: string;
189
        ruleType: "pulsar";
190
        requestMode: string;
191
        source: { channelFilter: string; type: string };
192
        target: {
193
          routingKey?: string;
194
          topic: string;
195
          serviceUrl: string;
196
          tlsTrustCerts?: string[];
197
          authentication: { authenticationMode: "token"; token: string };
198
          enveloped?: false | true;
199
          format?: string;
200
        };
201
      }
202
    | {
203
        status?: string;
204
        ruleType: "ingress-postgres-outbox";
205
        target: {
206
          url: string;
207
          outboxTableSchema: string;
208
          outboxTableName: string;
209
          nodesTableSchema: string;
210
          nodesTableName: string;
211
          sslMode: "prefer" | "require" | "verify-ca" | "verify-full";
212
          sslRootCert?: string;
213
        };
214
      },
215
) {
216
  const url = new URL(`https://control.ably.net/v1/apps/${app_id}/rules`);
217

218
  const response = await fetch(url, {
219
    method: "POST",
220
    headers: {
221
      "Content-Type": "application/json",
222
      Authorization: "Bearer " + auth.accessToken,
223
    },
224
    body: JSON.stringify(body),
225
  });
226
  if (!response.ok) {
227
    const text = await response.text();
228
    throw new Error(`${response.status} ${text}`);
229
  }
230
  return await response.json();
231
}
232