Skip to content

Commit 2e64893

Browse files
committed
Webhook test case
1 parent a848862 commit 2e64893

1 file changed

Lines changed: 34 additions & 26 deletions

File tree

lib/stack/webhook/index.js

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import cloneDeep from 'lodash/cloneDeep'
2-
import { create, update, deleteEntity, fetch, upload, parseData } from '../../entity'
2+
import {
3+
create,
4+
update,
5+
deleteEntity,
6+
fetch,
7+
upload,
8+
parseData
9+
} from '../../entity'
310
import error from '../../core/contentstackError'
411
import FormData from 'form-data'
512
import { createReadStream } from 'fs'
@@ -80,15 +87,16 @@ export function Webhook (http, data = {}) {
8087
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').executions()
8188
* .then((webhook) => console.log(webhook))
8289
*/
83-
this.executions = async function (param) {
84-
const headers = {
85-
headers: {
86-
...cloneDeep(this.stackHeaders)
87-
},
88-
params: {
89-
...cloneDeep(param)
90+
this.executions = async (params) => {
91+
const headers = {}
92+
if (this.stackHeaders) {
93+
headers.headers = this.stackHeaders
94+
}
95+
if (params) {
96+
headers.params = {
97+
...cloneDeep(params)
9098
}
91-
} || {}
99+
}
92100
try {
93101
const response = await http.get(`${this.urlPath}/executions`, headers)
94102
if (response.data) {
@@ -105,12 +113,11 @@ export function Webhook (http, data = {}) {
105113
*
106114
* @param {String} executionUid execution UID that you receive when you execute the 'Get executions of webhooks' call.
107115
*/
108-
this.retry = async function (executionUid) {
109-
const headers = {
110-
headers: {
111-
...cloneDeep(this.stackHeaders)
112-
}
113-
} || {}
116+
this.retry = async (executionUid) => {
117+
const headers = {}
118+
if (this.stackHeaders) {
119+
headers.headers = this.stackHeaders
120+
}
114121
try {
115122
const response = await http.post(`${this.urlPath}/retry`, { execution_uid: executionUid }, headers)
116123
if (response.data) {
@@ -172,16 +179,17 @@ export function Webhook (http, data = {}) {
172179
* .then((collection) => console.log(collection))
173180
*
174181
*/
175-
this.fetchAll = async (parmas) => {
182+
this.fetchAll = async (params) => {
183+
const headers = {}
184+
if (this.stackHeaders) {
185+
headers.headers = this.stackHeaders
186+
}
187+
if (params) {
188+
headers.params = {
189+
...cloneDeep(params)
190+
}
191+
}
176192
try {
177-
const headers = {
178-
headers: {
179-
...cloneDeep(this.stackHeaders)
180-
},
181-
params: {
182-
...cloneDeep(parmas)
183-
}
184-
} || {}
185193
const response = await http.get(this.urlPath, headers)
186194
if (response.data) {
187195
return new ContentstackCollection(response, http, null, WebhookCollection)
@@ -214,7 +222,7 @@ export function Webhook (http, data = {}) {
214222
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').import(data)
215223
* .then((webhook) => console.log(webhook))
216224
*/
217-
this.import = async function (data) {
225+
this.import = async (data) => {
218226
try {
219227
const response = await upload({
220228
http: http,
@@ -235,7 +243,7 @@ export function Webhook (http, data = {}) {
235243
}
236244

237245
export function WebhookCollection (http, data) {
238-
const obj = cloneDeep(data.webhooks)
246+
const obj = cloneDeep(data.webhooks) || []
239247
const webhookCollection = obj.map((userdata) => {
240248
return new Webhook(http, { webhook: userdata, stackHeaders: data.stackHeaders })
241249
})

0 commit comments

Comments
 (0)