Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ describe('Replicate client', () => {
});
expect(prediction.id).toBe('ufawqhfynnddngldkgtslldrkq');
});

test('Throws an error if webhook URL is invalid', async () => {
await expect(async () => {
await client.predictions.create({
version: '5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa',
input: {
text: 'Alice',
},
webhook: 'invalid-url',
});
}).rejects.toThrow('Invalid webhook URL');
});
// Add more tests for error handling, edge cases, etc.
});

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

test('Throws an error if webhook is not a valid URL', async () => {
await expect(
client.trainings.create(
'owner',
'model',
'632231d0d49d34d5c4633bd838aee3d81d936e59a886fbf28524702003b4c532',
{
destination: 'new_owner/new_model',
input: {
text: '...',
},
webhook: 'invalid-url',
}
)
).rejects.toThrow('Invalid webhook URL');
});

// Add more tests for error handling, edge cases, etc.
});

Expand Down Expand Up @@ -499,6 +528,18 @@ describe('Replicate client', () => {
// @ts-expect-error
await expect(client.run(':abc123', options)).rejects.toThrow();
});

test('Throws an error if webhook URL is invalid', async () => {
await expect(async () => {
await client.run(
'owner/model:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa', {
input: {
text: 'Alice',
},
webhook: 'invalid-url',
});
}).rejects.toThrow('Invalid webhook URL');
});
});

// Continue with tests for other methods
Expand Down
9 changes: 9 additions & 0 deletions lib/predictions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
async function createPrediction(options) {
const { wait, ...data } = options;

if (data.webhook) {
try {
// eslint-disable-next-line no-new
new URL(data.webhook);
} catch (err) {
throw new Error('Invalid webhook URL');
}
}

const prediction = this.request('/predictions', {
method: 'POST',
data,
Expand Down
9 changes: 9 additions & 0 deletions lib/trainings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
async function createTraining(model_owner, model_name, version_id, options) {
const { ...data } = options;

if (data.webhook) {
try {
// eslint-disable-next-line no-new
new URL(data.webhook);
} catch (err) {
throw new Error('Invalid webhook URL');
}
}

const training = this.request(`/models/${model_owner}/${model_name}/versions/${version_id}/trainings`, {
method: 'POST',
data,
Expand Down