Skip to content

Commit

Permalink
Fix webhook endpoint methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Nov 15, 2023
1 parent 440e24c commit 6c46971
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
48 changes: 48 additions & 0 deletions packages/cma-client-node/__tests__/webhook.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { generateNewCmaClient } from '../../../jest-helpers/generateNewCmaClient';

describe('Webhooks', () => {
it.concurrent('create, find, all, destroy', async () => {
const client = await generateNewCmaClient();

const webhook = await client.webhooks.create({
name: 'test',
url: 'https://www.google.com',
http_basic_user: null,
http_basic_password: null,
custom_payload: null,
headers: {},
events: [
{
event_types: ['create', 'update'],
entity_type: 'item_type',
filters: [
{
entity_ids: ['brand', 'tag'],
entity_type: 'item_type',
},
],
},
],
});

const foundWebhook = await client.webhooks.find(webhook);
expect(foundWebhook.id).toEqual(webhook.id);

const allWebhooks = await client.webhooks.list();
expect(allWebhooks).toHaveLength(3);

await client.webhooks.update(foundWebhook, {
events: [
{
event_types: ['create'],
filters: [],
entity_type: 'item_type',
},
],
});

await client.webhooks.destroy(foundWebhook);

expect(await client.webhooks.list()).toHaveLength(2);
});
});
24 changes: 12 additions & 12 deletions packages/cma-client/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -2408,13 +2408,13 @@
"returnsCollection": false,
"docUrl": "https://www.datocms.com/docs/content-management-api/resources/webhook/update",
"rel": "update",
"urlTemplate": "/webhooks/${userId}",
"urlTemplate": "/webhooks/${webhookId}",
"method": "PUT",
"comment": "Update a webhook",
"urlPlaceholder": {
"variableName": "userId",
"isEntityId": false,
"relType": "UserData"
"variableName": "webhookId",
"isEntityId": true,
"relType": "WebhookData"
},
"requestBodyType": "WebhookUpdateSchema",
"optionalRequestBody": false,
Expand Down Expand Up @@ -2457,13 +2457,13 @@
"returnsCollection": false,
"docUrl": "https://www.datocms.com/docs/content-management-api/resources/webhook/self",
"rel": "self",
"urlTemplate": "/webhooks/${userId}",
"urlTemplate": "/webhooks/${webhookId}",
"method": "GET",
"comment": "Retrieve a webhook",
"urlPlaceholder": {
"variableName": "userId",
"isEntityId": false,
"relType": "UserData"
"variableName": "webhookId",
"isEntityId": true,
"relType": "WebhookData"
},
"optionalRequestBody": false,
"queryParamsRequired": false,
Expand All @@ -2475,13 +2475,13 @@
"returnsCollection": false,
"docUrl": "https://www.datocms.com/docs/content-management-api/resources/webhook/destroy",
"rel": "destroy",
"urlTemplate": "/webhooks/${userId}",
"urlTemplate": "/webhooks/${webhookId}",
"method": "DELETE",
"comment": "Delete a webhook",
"urlPlaceholder": {
"variableName": "userId",
"isEntityId": false,
"relType": "UserData"
"variableName": "webhookId",
"isEntityId": true,
"relType": "WebhookData"
},
"optionalRequestBody": false,
"queryParamsRequired": false,
Expand Down
27 changes: 15 additions & 12 deletions packages/cma-client/src/generated/resources/Webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ export default class Webhook extends BaseResource {
* @throws {TimeoutError}
*/
update(
userId: string | SimpleSchemaTypes.UserData,
webhookId: string | SimpleSchemaTypes.WebhookData,
body: SimpleSchemaTypes.WebhookUpdateSchema,
) {
return this.rawUpdate(
Utils.toId(userId),
Utils.toId(webhookId),
Utils.serializeRequestBody<SchemaTypes.WebhookUpdateSchema>(body, {
id: Utils.toId(webhookId),
type: 'webhook',
attributes: [
'name',
Expand Down Expand Up @@ -103,12 +104,12 @@ export default class Webhook extends BaseResource {
* @throws {TimeoutError}
*/
rawUpdate(
userId: string,
webhookId: string,
body: SchemaTypes.WebhookUpdateSchema,
): Promise<SchemaTypes.WebhookUpdateTargetSchema> {
return this.client.request<SchemaTypes.WebhookUpdateTargetSchema>({
method: 'PUT',
url: `/webhooks/${userId}`,
url: `/webhooks/${webhookId}`,
body,
});
}
Expand Down Expand Up @@ -152,8 +153,8 @@ export default class Webhook extends BaseResource {
* @throws {ApiError}
* @throws {TimeoutError}
*/
find(userId: string | SimpleSchemaTypes.UserData) {
return this.rawFind(Utils.toId(userId)).then((body) =>
find(webhookId: string | SimpleSchemaTypes.WebhookData) {
return this.rawFind(Utils.toId(webhookId)).then((body) =>
Utils.deserializeResponseBody<SimpleSchemaTypes.WebhookSelfTargetSchema>(
body,
),
Expand All @@ -168,10 +169,10 @@ export default class Webhook extends BaseResource {
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawFind(userId: string): Promise<SchemaTypes.WebhookSelfTargetSchema> {
rawFind(webhookId: string): Promise<SchemaTypes.WebhookSelfTargetSchema> {
return this.client.request<SchemaTypes.WebhookSelfTargetSchema>({
method: 'GET',
url: `/webhooks/${userId}`,
url: `/webhooks/${webhookId}`,
});
}

Expand All @@ -183,8 +184,8 @@ export default class Webhook extends BaseResource {
* @throws {ApiError}
* @throws {TimeoutError}
*/
destroy(userId: string | SimpleSchemaTypes.UserData) {
return this.rawDestroy(Utils.toId(userId)).then((body) =>
destroy(webhookId: string | SimpleSchemaTypes.WebhookData) {
return this.rawDestroy(Utils.toId(webhookId)).then((body) =>
Utils.deserializeResponseBody<SimpleSchemaTypes.WebhookDestroyTargetSchema>(
body,
),
Expand All @@ -199,10 +200,12 @@ export default class Webhook extends BaseResource {
* @throws {ApiError}
* @throws {TimeoutError}
*/
rawDestroy(userId: string): Promise<SchemaTypes.WebhookDestroyTargetSchema> {
rawDestroy(
webhookId: string,
): Promise<SchemaTypes.WebhookDestroyTargetSchema> {
return this.client.request<SchemaTypes.WebhookDestroyTargetSchema>({
method: 'DELETE',
url: `/webhooks/${userId}`,
url: `/webhooks/${webhookId}`,
});
}
}

0 comments on commit 6c46971

Please sign in to comment.