Skip to content

Commit 7ed6bb1

Browse files
authored
Throw an error if webhook URL is invalid (#84)
1 parent edbac16 commit 7ed6bb1

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

index.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ describe('Replicate client', () => {
151151
});
152152
expect(prediction.id).toBe('ufawqhfynnddngldkgtslldrkq');
153153
});
154+
155+
test('Throws an error if webhook URL is invalid', async () => {
156+
await expect(async () => {
157+
await client.predictions.create({
158+
version: '5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa',
159+
input: {
160+
text: 'Alice',
161+
},
162+
webhook: 'invalid-url',
163+
});
164+
}).rejects.toThrow('Invalid webhook URL');
165+
});
154166
// Add more tests for error handling, edge cases, etc.
155167
});
156168

@@ -321,6 +333,23 @@ describe('Replicate client', () => {
321333
expect(training.id).toBe('zz4ibbonubfz7carwiefibzgga');
322334
});
323335

336+
test('Throws an error if webhook is not a valid URL', async () => {
337+
await expect(
338+
client.trainings.create(
339+
'owner',
340+
'model',
341+
'632231d0d49d34d5c4633bd838aee3d81d936e59a886fbf28524702003b4c532',
342+
{
343+
destination: 'new_owner/new_model',
344+
input: {
345+
text: '...',
346+
},
347+
webhook: 'invalid-url',
348+
}
349+
)
350+
).rejects.toThrow('Invalid webhook URL');
351+
});
352+
324353
// Add more tests for error handling, edge cases, etc.
325354
});
326355

@@ -499,6 +528,18 @@ describe('Replicate client', () => {
499528
// @ts-expect-error
500529
await expect(client.run(':abc123', options)).rejects.toThrow();
501530
});
531+
532+
test('Throws an error if webhook URL is invalid', async () => {
533+
await expect(async () => {
534+
await client.run(
535+
'owner/model:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa', {
536+
input: {
537+
text: 'Alice',
538+
},
539+
webhook: 'invalid-url',
540+
});
541+
}).rejects.toThrow('Invalid webhook URL');
542+
});
502543
});
503544

504545
// Continue with tests for other methods

lib/predictions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
async function createPrediction(options) {
1515
const { wait, ...data } = options;
1616

17+
if (data.webhook) {
18+
try {
19+
// eslint-disable-next-line no-new
20+
new URL(data.webhook);
21+
} catch (err) {
22+
throw new Error('Invalid webhook URL');
23+
}
24+
}
25+
1726
const prediction = this.request('/predictions', {
1827
method: 'POST',
1928
data,

lib/trainings.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
async function createTraining(model_owner, model_name, version_id, options) {
1515
const { ...data } = options;
1616

17+
if (data.webhook) {
18+
try {
19+
// eslint-disable-next-line no-new
20+
new URL(data.webhook);
21+
} catch (err) {
22+
throw new Error('Invalid webhook URL');
23+
}
24+
}
25+
1726
const training = this.request(`/models/${model_owner}/${model_name}/versions/${version_id}/trainings`, {
1827
method: 'POST',
1928
data,

0 commit comments

Comments
 (0)