Updates a Rule

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

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