Skip to content

Commit 28c5d1b

Browse files
committed
fix: added back stack/webhook apis and fixed some routing issues
1 parent 093c5d2 commit 28c5d1b

File tree

19 files changed

+604
-8
lines changed

19 files changed

+604
-8
lines changed

.jsdoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"lib/marketplace/index.js",
3939
"lib/marketplace/installation/index.js",
4040
"lib/marketplace/installation/webhooks/index.js",
41-
"lib/marketplace/request/index.js",
41+
"lib/marketplace/apprequest/index.js",
4242
"lib/marketplace/authorization/index.js"
4343

4444
],

lib/marketplace/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { App } from './app'
33
import { Installation } from './installation'
44
import error from '../core/contentstackError'
55
import { fetchAll } from '../entity'
6-
import { AppRequest } from './request'
6+
import { AppRequest } from './apprequest'
77

88
export function Marketplace (http, data) {
99
http.defaults.versioningStrategy = undefined

lib/stack/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Environment } from './environment'
1010
import { Asset } from './asset'
1111
import { Locale } from './locale'
1212
import { Extension } from './extension'
13+
import { Webhook } from './webhook'
1314
import { Workflow } from './workflow'
1415
import { Release } from './release'
1516
import { BulkOperation } from './bulkOperation'
@@ -298,6 +299,28 @@ export function Stack (http, data) {
298299
return new Workflow(http, data)
299300
}
300301

302+
/**
303+
* @description Webhooks allow you to specify a URL to which you would like Contentstack to post data when an event happens.
304+
* @param {String} webhookUid The UID of the Webhook you want to get details.
305+
* @returns {Webhook} Instance of Webhook.
306+
* @example
307+
* import * as contentstack from '@contentstack/management'
308+
* const client = contentstack.client()
309+
*
310+
* client.stack({ api_key: 'api_key'}).webhook().create()
311+
* .then((webhook) => console.log(webhook))
312+
*
313+
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').fetch()
314+
* .then((webhook) => console.log(webhook))
315+
*/
316+
this.webhook = (webhookUid = null) => {
317+
const data = { stackHeaders: this.stackHeaders }
318+
if (webhookUid) {
319+
data.webhook = { uid: webhookUid }
320+
}
321+
return new Webhook(http, data)
322+
}
323+
301324
/**
302325
* @description Labels allow you to group a collection of content within a stack. Using labels you can group content types that need to work together
303326
* @param {String} uid The UID of the Label you want to get details.

lib/stack/webhook/index.js

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import {
3+
create,
4+
update,
5+
deleteEntity,
6+
fetch,
7+
upload,
8+
parseData,
9+
fetchAll
10+
} from '../../entity'
11+
import error from '../../core/contentstackError'
12+
import FormData from 'form-data'
13+
import { createReadStream } from 'fs'
14+
15+
/**
16+
* A webhook is a mechanism that sends real-time information to any third-party app or service to keep your application in sync with your Contentstack account. Webhooks allow you to specify a URL to which you would like Contentstack to post data when an event happens. Read more about <a href='https://www.contentstack.com/docs/developers/set-up-webhooks'>Webhooks</a>.
17+
* @namespace Webhook
18+
*/
19+
20+
export function Webhook (http, data = {}) {
21+
this.stackHeaders = data.stackHeaders
22+
this.urlPath = `/webhooks`
23+
24+
if (data.webhook) {
25+
Object.assign(this, cloneDeep(data.webhook))
26+
this.urlPath = `/webhooks/${this.uid}`
27+
/**
28+
* @description The Update Webhook call lets you update the name and description of an existing Webhook.
29+
* @memberof Webhook
30+
* @func update
31+
* @returns {Promise<Webhook.Webhook>} Promise for Webhook instance
32+
* @example
33+
* import * as contentstack from '@contentstack/management'
34+
* const client = contentstack.client()
35+
*
36+
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').fetch()
37+
* .then((webhook) => {
38+
* webhook.title = 'My New Webhook'
39+
* webhook.description = 'Webhook description'
40+
* return webhook.update()
41+
* })
42+
* .then((webhook) => console.log(webhook))
43+
*
44+
*/
45+
this.update = update(http, 'webhook')
46+
47+
/**
48+
* @description The Delete Webhook call is used to delete an existing Webhook permanently from your Stack.
49+
* @memberof Webhook
50+
* @func delete
51+
* @returns {Object} Response Object.
52+
* @example
53+
* import * as contentstack from '@contentstack/management'
54+
* const client = contentstack.client()
55+
*
56+
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').delete()
57+
* .then((response) => console.log(response.notice))
58+
*/
59+
this.delete = deleteEntity(http)
60+
61+
/**
62+
* @description The fetch Webhook call fetches Webhook details.
63+
* @memberof Webhook
64+
* @func fetch
65+
* @returns {Promise<Webhook.Webhook>} Promise for Webhook instance
66+
* @param {Int} version Enter the unique ID of the content type of which you want to retrieve the details. The UID is generated based on the title of the content type. The unique ID of a content type is unique across a stack.
67+
* @example
68+
* import * as contentstack from '@contentstack/management'
69+
* const client = contentstack.client()
70+
*
71+
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').fetch()
72+
* .then((webhook) => console.log(webhook))
73+
*
74+
*/
75+
this.fetch = fetch(http, 'webhook')
76+
77+
/**
78+
* @description The Get executions of a webhook call will provide the execution details of a specific webhook, which includes the execution UID.
79+
* @memberof Webhook
80+
* @returns {Promise<JSON.JSON>} JSON response for webhook execution
81+
* @func executions
82+
* @param {*} param.skip - to skip number of data
83+
* @example
84+
* import * as contentstack from '@contentstack/management'
85+
* const client = contentstack.client()
86+
*
87+
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').executions()
88+
* .then((webhook) => console.log(webhook))
89+
*/
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)
98+
}
99+
}
100+
try {
101+
const response = await http.get(`${this.urlPath}/executions`, headers)
102+
if (response.data) {
103+
return response.data
104+
} else {
105+
throw error(response)
106+
}
107+
} catch (err) {
108+
throw error(err)
109+
}
110+
}
111+
112+
/**
113+
*
114+
* @param {String} executionUid execution UID that you receive when you execute the 'Get executions of webhooks' call.
115+
*/
116+
this.retry = async (executionUid) => {
117+
const headers = {}
118+
if (this.stackHeaders) {
119+
headers.headers = this.stackHeaders
120+
}
121+
try {
122+
const response = await http.post(`${this.urlPath}/retry`, { execution_uid: executionUid }, headers)
123+
if (response.data) {
124+
return response.data
125+
} else {
126+
throw error(response)
127+
}
128+
} catch (err) {
129+
throw error(err)
130+
}
131+
}
132+
} else {
133+
/**
134+
* @description The Create a webhook request allows you to create a new webhook in a specific stack.
135+
* @memberof Webhook
136+
* @func create
137+
* @returns {Promise<Webhook.Webhook>} Promise for Webhook instance
138+
*
139+
* @example
140+
* import * as contentstack from '@contentstack/management'
141+
* const client = contentstack.client()
142+
* const webhook = {
143+
* name: 'Test',
144+
* destinations: [{
145+
* target_url: 'http://example.com',
146+
* http_basic_auth: 'basic',
147+
* http_basic_password: 'test',
148+
* custom_header: [{
149+
* header_name: 'Custom',
150+
* value: 'testing'
151+
* }]
152+
* }],
153+
* channels: [
154+
* 'assets.create'
155+
* ],
156+
* retry_policy: 'manual',
157+
* disabled: false
158+
* }
159+
* client.stack().webhook().create({ webhook })
160+
* .then((webhook) => console.log(webhook))
161+
*/
162+
this.create = create({ http: http })
163+
164+
/**
165+
* @description The Get all Webhook call lists all Webhooks from Stack.
166+
* @memberof Webhook
167+
* @func fetchAll
168+
* @param {Int} limit The limit parameter will return a specific number of webhooks in the output.
169+
* @param {Int} skip The skip parameter will skip a specific number of webhooks in the output.
170+
* @param {String} asc When fetching webhooks, you can sort them in the ascending order with respect to the value of a specific field in the response body.
171+
* @param {String} desc When fetching webhooks, you can sort them in the decending order with respect to the value of a specific field in the response body.
172+
* @param {Boolean}include_count To retrieve the count of webhooks.
173+
* @returns {ContentstackCollection} Result collection of content of specified module.
174+
* @example
175+
* import * as contentstack from '@contentstack/management'
176+
* const client = contentstack.client()
177+
*
178+
* client.stack({ api_key: 'api_key'}).webhook().fetchAll()
179+
* .then((collection) => console.log(collection))
180+
*
181+
*/
182+
this.fetchAll = fetchAll(http, WebhookCollection)
183+
}
184+
185+
/**
186+
* @description The 'Import Webhook' section consists of the following two requests that will help you to import new Webhooks or update existing ones by uploading JSON files.
187+
* @memberof Webhook
188+
* @func import
189+
* @param {String} data.webhook path to file
190+
* @example
191+
* import * as contentstack from '@contentstack/management'
192+
* const client = contentstack.client()
193+
*
194+
* const data = {
195+
* webhook: 'path/to/file.png',
196+
* }
197+
* // Import a Webhook
198+
* client.stack({ api_key: 'api_key'}).webhook().import(data)
199+
* .then((webhook) => console.log(webhook))
200+
*
201+
* // Import an Existing Webhook
202+
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').import(data)
203+
* .then((webhook) => console.log(webhook))
204+
*/
205+
this.import = async (data) => {
206+
try {
207+
const response = await upload({
208+
http: http,
209+
urlPath: `${this.urlPath}/import`,
210+
stackHeaders: this.stackHeaders,
211+
formData: createFormData(data)
212+
})
213+
if (response.data) {
214+
return new this.constructor(http, parseData(response, this.stackHeaders))
215+
} else {
216+
throw error(response)
217+
}
218+
} catch (err) {
219+
throw error(err)
220+
}
221+
}
222+
return this
223+
}
224+
225+
export function WebhookCollection (http, data) {
226+
const obj = cloneDeep(data.webhooks) || []
227+
const webhookCollection = obj.map((userdata) => {
228+
return new Webhook(http, { webhook: userdata, stackHeaders: data.stackHeaders })
229+
})
230+
return webhookCollection
231+
}
232+
233+
export function createFormData (data) {
234+
return () => {
235+
const formData = new FormData()
236+
const uploadStream = createReadStream(data.webhook)
237+
formData.append('webhook', uploadStream)
238+
return formData
239+
}
240+
}

test/typescript/app-request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai'
22
import * as dotenv from 'dotenv'
3-
import { AppRequest } from '../../types/app/request'
3+
import { AppRequest } from '../../types/marketplace/request'
44
dotenv.config()
55
let requestUID = ''
66

test/typescript/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai';
22
import * as dotenv from 'dotenv'
3-
import { AppData, AppOAuth, Apps } from '../../types/app'
3+
import { AppData, AppOAuth, Apps } from '../../types/marketplace'
44
import { Organization } from '../../types/organization';
55
dotenv.config()
66
let appUid = ''

test/typescript/authorization.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai'
22
import * as dotenv from 'dotenv'
3-
import { Authorization } from '../../types/app/authorization'
3+
import { Authorization } from '../../types/marketplace/authorization'
44
dotenv.config()
55

66

test/typescript/hosting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai'
22
import * as dotenv from 'dotenv'
3-
import { Hosting } from '../../types/app/hosting'
3+
import { Hosting } from '../../types/marketplace/hosting'
44
dotenv.config()
55
let uploadUid = ''
66
let deploymentUid = ''

test/unit/app-request-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expect } from 'chai'
33
import { describe, it } from 'mocha'
44
import MockAdapter from 'axios-mock-adapter'
55
import { appMock } from './mock/objects'
6-
import { AppRequest } from '../../lib/marketplace/request'
6+
import { AppRequest } from '../../lib/marketplace/apprequest'
77
import { requestMock } from './mock/request-mock'
88

99
describe('Contentstack apps request test', () => {

0 commit comments

Comments
 (0)