-
Notifications
You must be signed in to change notification settings - Fork 97
/
webhook.js
183 lines (173 loc) · 6.33 KB
/
webhook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Webhook instances
* @namespace Webhook
*/
import cloneDeep from 'lodash/cloneDeep'
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
import enhanceWithMethods from '../enhance-with-methods'
import errorHandler from '../error-handler'
import {
createUpdateEntity,
createDeleteEntity
} from '../instance-actions'
/**
* @memberof Webhook
* @typedef Webhook
* @prop {Meta.Sys} sys - System metadata
* @prop {string} name
* @prop {string} url - Url which the webhook will call
* @prop {string} httpBasicUsername - Username for basic HTTP authentication
* @prop {string} httpBasicPassword - Password for basic HTTP authentication
* @prop {object} headers - Key value pairs of additional headers to be sent with every webhook call.
* @prop {array} topics - Topics which this webhook should be subscribed to. See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/webhooks/create-a-webhook for more details
* @prop {function(): Object} toPlainObject() - Returns this Webhook as a plain JS object
*/
function createWebhookApi (http) {
return {
/**
* Sends an update to the server with any changes made to the object's properties
* @memberof Webhook
* @func update
* @return {Promise<Webhook>} Object returned from the server with updated changes.
* @example
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => space.getWebhook('<webhook_id>'))
* .then((webhook) => {
* webhook.name = 'new webhook name'
* return webhook.update()
* })
* .then((webhook) => console.log(`webhook ${webhook.sys.id} updated.`))
* .catch(console.error)
*/
update: createUpdateEntity({
http: http,
entityPath: 'webhook_definitions',
wrapperMethod: wrapWebhook
}),
/**
* Deletes this object on the server.
* @memberof Webhook
* @func delete
* @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled.
* @example
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => space.getWebhook('<webhook_id>'))
* .then((webhook) => webhook.delete())
* .then((webhook) => console.log(`webhook ${webhook.sys.id} updated.`))
* .catch(console.error)
*/
delete: createDeleteEntity({
http: http,
entityPath: 'webhook_definitions'
}),
/**
* List of the most recent webhook calls. See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/webhook-calls/webhook-call-overviews for more details.
* @memberof Webhook
* @func getCalls
* @return {Promise<object>} Promise for list of calls
* @example
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => space.getWebhook('<webhook_id>'))
* .then((webhook) => webhook.getCalls())
* .then((response) => console.log(response.items)) // webhook calls
* .catch(console.error)
*/
getCalls: function () {
return http.get('webhooks/' + this.sys.id + '/calls')
.then((response) => response.data, errorHandler)
},
/**
* Webhook call with specific id. See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/webhook-calls/webhook-call-overviews for more details
* @memberof Webhook
* @func getCalls
* @return {Promise<object>} Promise for call details
* @example
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => space.getWebhook('<webhook_id>'))
* .then((webhook) => webhook.getCall(<call-id>))
* .then((webhookCall) => console.log(webhookCall))
* .catch(console.error)
*/
getCall: function (id) {
return http.get('webhooks/' + this.sys.id + '/calls/' + id)
.then((response) => response.data, errorHandler)
},
/**
* Overview of the health of webhook calls. See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/webhook-calls/webhook-call-overviews for more details.
* @memberof Webhook
* @func getHealth
* @return {Promise<object>} Promise for health info
* @example
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => space.getWebhook('<webhook_id>'))
* .then((webhook) => webhook.getHealth())
* .then((webhookHealth) => console.log(webhookHealth))
* .catch(console.error)
*/
getHealth: function () {
return http.get('webhooks/' + this.sys.id + '/health')
.then((response) => response.data, errorHandler)
}
}
}
/**
* @private
* @param {Object} http - HTTP client instance
* @param {Object} data - Raw webhook data
* @return {Webhook} Wrapped webhook data
*/
export function wrapWebhook (http, data) {
const webhook = toPlainObject(cloneDeep(data))
enhanceWithMethods(webhook, createWebhookApi(http))
return freezeSys(webhook)
}
/**
* @memberof Webhook
* @typedef WebhookCollection
* @prop {number} total
* @prop {number} skip
* @prop {number} limit
* @prop {Array<Webhook.Webhook>} items
* @prop {function(): Object} toPlainObject() - Returns this Webhook collection as a plain JS object
*/
/**
* @private
* @param {Object} http - HTTP client instance
* @param {Object} data - Raw webhook collection data
* @return {WebhookCollection} Wrapped webhook collection data
*/
export function wrapWebhookCollection (http, data) {
const webhooks = toPlainObject(cloneDeep(data))
webhooks.items = webhooks.items.map((entity) => wrapWebhook(http, entity))
return freezeSys(webhooks)
}