Skip to content

Commit b002fe7

Browse files
authored
Merge pull request #16 from contentstack/bug/orgs-stack-and-entry-fetch
Bug/orgs stack and entry fetch
2 parents 18e9cba + 80d85fc commit b002fe7

File tree

12 files changed

+158
-188
lines changed

12 files changed

+158
-188
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# Changelog
2+
3+
## [v1.2.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.2.2) (2021-05-26)
4+
- Bug Fix
5+
- Organization Specific get all Stack: Get Stack for specific organization from org_uid
6+
- Resolved: Entry Publish and Update not work after find function
7+
- Resolved: Workflow update issue on fetchAll function
8+
- Document Update
9+
- `update` Entry example code update
10+
211
## [v1.2.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.2.1) (2021-03-19)
312
- Bug Fix
413
- User get details: Include organization functions for `is_owner` of the organization

lib/contentstack.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import httpClient from './core/contentstackHTTPClient.js'
4444
* import * as contentstack from '@contentstack/management'
4545
* const client = contentstack.client({ maxRequests: 5 })
4646
*
47-
* @prop {boolean=} params.retryOnError - Optional boolean for retry on failuer. Default is true
47+
* @prop {boolean=} params.retryOnError - Optional boolean for retry on failure. Default is true
4848
* @example //Set the `retryOnError` to false
4949
* import * as contentstack from '@contentstack/management'
5050
* const client = contentstack.client({ retryOnError: false })
@@ -54,7 +54,7 @@ import httpClient from './core/contentstackHTTPClient.js'
5454
* import * as contentstack from '@contentstack/management'
5555
* const client = contentstack.client({ retryLimit: 2 })
5656
*
57-
* @prop {number=} params.retryDelay - The number of miliseconds to use for operation retries. Default is 300ms
57+
* @prop {number=} params.retryDelay - The number of milliseconds to use for operation retries. Default is 300ms
5858
* @example //Set the `retryDelay` to 500ms
5959
* import * as contentstack from '@contentstack/management'
6060
* const client = contentstack.client({ retryDelay: 500 })
@@ -93,7 +93,7 @@ import httpClient from './core/contentstackHTTPClient.js'
9393
* const client = contentstack.client({ maxContentLength: 1024 ** 3 })
9494
*
9595
* @prop {number=} params.maxBodyLength - Optional maximum body length in bytes (default: 10 MB)
96-
* @example //Set the `maxContentLength` to 1024 ** 2 * 10 // 10 MB
96+
* @example //Set the `maxBodyLength` to 1024 ** 2 * 10 // 10 MB
9797
* import * as contentstack from '@contentstack/management'
9898
* const client = contentstack.client({ maxBodyLength: 1024 ** 2 * 10 })
9999
*

lib/entity.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import error from './core/contentstackError'
22
import cloneDeep from 'lodash/cloneDeep'
33
import Query from './query/index'
4+
import ContentstackCollection from './contentstackCollection'
45

56
export const publish = (http, type) => {
67
return async function ({ publishDetails, locale = null, version = null, scheduledAt = null }) {
@@ -83,7 +84,7 @@ export const create = ({ http, params }) => {
8384
try {
8485
const response = await http.post(this.urlPath, data, headers)
8586
if (response.data) {
86-
return new this.constructor(http, parseData(response, this.stackHeaders))
87+
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
8788
} else {
8889
throw error(response)
8990
}
@@ -106,7 +107,7 @@ export const exportObject = ({ http }) => {
106107
try {
107108
const response = await http.get(this.urlPath, headers)
108109
if (response.data) {
109-
return new this.constructor(http, parseData(response, this.stackHeaders))
110+
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
110111
} else {
111112
throw error(response)
112113
}
@@ -119,8 +120,15 @@ export const exportObject = ({ http }) => {
119120
export const query = ({ http, wrapperCollection }) => {
120121
return function (params = {}) {
121122
if (this.organization_uid) {
123+
if (!params.query) {
124+
params.query = {}
125+
}
122126
params.query['org_uid'] = this.organization_uid
123127
}
128+
129+
if (this.content_type_uid) {
130+
params.content_type_uid = this.content_type_uid
131+
}
124132
return Query(http, this.urlPath, params, this.stackHeaders, wrapperCollection)
125133
}
126134
}
@@ -208,6 +216,29 @@ export const fetch = (http, type) => {
208216
}
209217
}
210218
}
219+
export const fetchAll = (http, wrapperCollection) => {
220+
return async function (params = {}) {
221+
const headers = {}
222+
if (this.stackHeaders) {
223+
headers.headers = this.stackHeaders
224+
}
225+
if (params) {
226+
headers.params = {
227+
...cloneDeep(params)
228+
}
229+
}
230+
try {
231+
const response = await http.get(this.urlPath, headers)
232+
if (response.data) {
233+
return new ContentstackCollection(response, http, this.stackHeaders, wrapperCollection)
234+
} else {
235+
throw error(response)
236+
}
237+
} catch (err) {
238+
throw error(err)
239+
}
240+
}
241+
}
211242

212243
export function parseData (response, stackHeaders, contentTypeUID) {
213244
const data = response.data || {}

lib/organization/index.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import error from '../core/contentstackError'
3-
import { fetch } from '../entity'
3+
import { fetch, fetchAll } from '../entity'
44
import ContentstackCollection from '../contentstackCollection'
55
import { RoleCollection } from '../stack/roles'
66
import { StackCollection } from '../stack'
@@ -222,25 +222,13 @@ export function Organization (http, data) {
222222
* .then((collection) => console.log(collection))
223223
*
224224
*/
225-
this.fetchAll = async (parmas) => {
226-
try {
227-
const response = await http.get(this.urlPath, { params: parmas })
228-
if (response.data) {
229-
return new ContentstackCollection(response, http, null, OrganizationCollection)
230-
} else {
231-
throw error(response)
232-
}
233-
} catch (err) {
234-
throw error(err)
235-
}
236-
}
225+
this.fetchAll = fetchAll(http, OrganizationCollection)
237226
}
238227
}
239228

240229
export function OrganizationCollection (http, data) {
241230
const obj = cloneDeep(data.organizations || [])
242-
const organizationCollection = obj.map((userdata) => {
231+
return obj.map((userdata) => {
243232
return new Organization(http, { organization: userdata })
244233
})
245-
return organizationCollection
246234
}

lib/query/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ export default function Query (http, urlPath, param, stackHeaders = null, wrappe
77
if (stackHeaders) {
88
headers.headers = stackHeaders
99
}
10+
var contentTypeUid = null
1011
if (param) {
12+
if (param.content_type_uid) {
13+
contentTypeUid = param.content_type_uid
14+
delete param.content_type_uid
15+
}
1116
headers.params = {
1217
...cloneDeep(param)
1318
}
@@ -34,6 +39,9 @@ export default function Query (http, urlPath, param, stackHeaders = null, wrappe
3439
try {
3540
const response = await http.get(urlPath, headers)
3641
if (response.data) {
42+
if (contentTypeUid) {
43+
response.data.content_type_uid = contentTypeUid
44+
}
3745
return new ContentstackCollection(response, http, stackHeaders, wrapperCollection)
3846
} else {
3947
throw error(response)
@@ -100,6 +108,9 @@ export default function Query (http, urlPath, param, stackHeaders = null, wrappe
100108
try {
101109
const response = await http.get(urlPath, limitHeader)
102110
if (response.data) {
111+
if (contentTypeUid) {
112+
response.data.content_type_uid = contentTypeUid
113+
}
103114
return new ContentstackCollection(response, http, stackHeaders, wrapperCollection)
104115
} else {
105116
throw error(response)

lib/stack/contentType/entry/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function Entry (http, data) {
4141
* .then((entry) => {
4242
* entry.title = 'My New Entry'
4343
* entry.description = 'Entry description'
44-
* return Entry.update()
44+
* return entry.update()
4545
* })
4646
* .then((entry) => console.log(entry))
4747
*
@@ -54,7 +54,7 @@ export function Entry (http, data) {
5454
* .then((entry) => {
5555
* entry.title = 'My New Entry'
5656
* entry.description = 'Entry description'
57-
* return Entry.update({ locale: 'en-at' })
57+
* return entry.update({ locale: 'en-at' })
5858
* })
5959
* .then((entry) => console.log(entry))
6060
*
@@ -152,13 +152,13 @@ export function Entry (http, data) {
152152
* @example
153153
* import * as contentstack from '@contentstack/management'
154154
* const client = contentstack.client()
155-
*
155+
*
156156
* const publishing_rule = {
157-
* "uid": "blt9b9253297f117e84",
158-
* "action": "publish", //(‘publish’, ‘unpublish’, or ’both’)
159-
* "status": 1, //(this could be ‘0’ for Approval Requested, ‘1’ for ‘Approval Accepted’, and ‘-1’ for ‘Approval Rejected’),
160-
* "notify": false,
161-
* "comment": "Please review this."
157+
* "uid": "blt9b9253297f117e84",
158+
* "action": "publish" //(‘publish’, ‘unpublish’, or ’both’)
159+
* "status": 1, //(this could be ‘0’ for Approval Requested, ‘1’ for ‘Approval Accepted’, and ‘-1’ for ‘Approval Rejected’),
160+
* "notify": false,
161+
* comment": "Please review this."
162162
* }
163163
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('uid').publishRequest({ publishing_rule, locale: 'en-us'})
164164
* .then((response) => console.log(response.notice))
@@ -271,7 +271,7 @@ export function Entry (http, data) {
271271
export function EntryCollection (http, data) {
272272
const obj = cloneDeep(data.entries) || []
273273
const entryCollection = obj.map((entry) => {
274-
return new Entry(http, { entry: entry, content_type_uid: 'uid', stackHeaders: data.stackHeaders })
274+
return new Entry(http, { entry: entry, content_type_uid: data.content_type_uid, stackHeaders: data.stackHeaders })
275275
})
276276
return entryCollection
277277
}

lib/stack/roles/index.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cloneDeep from 'lodash/cloneDeep'
2-
import { create, update, deleteEntity, fetch, query } from '../../entity'
2+
import { create, update, deleteEntity, fetch, query, fetchAll } from '../../entity'
33
import ContentstackCollection from '../../contentstackCollection'
44
import error from '../../core/contentstackError'
55
/**
@@ -133,27 +133,7 @@ export function Role (http, data) {
133133
* client.stack().role().findAll()
134134
* .then((collection) => console.log(collection))
135135
*/
136-
this.fetchAll = async (params = {}) => {
137-
const headers = {}
138-
if (this.stackHeaders) {
139-
headers.headers = this.stackHeaders
140-
}
141-
if (params) {
142-
headers.params = {
143-
...cloneDeep(params)
144-
}
145-
}
146-
try {
147-
const response = await http.get(this.urlPath, headers)
148-
if (response.data) {
149-
return new ContentstackCollection(response, http, this.stackHeaders, RoleCollection)
150-
} else {
151-
throw error(response)
152-
}
153-
} catch (err) {
154-
throw error(err)
155-
}
156-
}
136+
this.fetchAll = fetchAll(http, RoleCollection)
157137

158138
/**
159139
* @description The Query on Role will allow to fetch details of all or specific role.

lib/stack/webhook/index.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
deleteEntity,
66
fetch,
77
upload,
8-
parseData
8+
parseData,
9+
fetchAll
910
} from '../../entity'
1011
import error from '../../core/contentstackError'
1112
import FormData from 'form-data'
@@ -179,27 +180,7 @@ export function Webhook (http, data = {}) {
179180
* .then((collection) => console.log(collection))
180181
*
181182
*/
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-
}
192-
try {
193-
const response = await http.get(this.urlPath, headers)
194-
if (response.data) {
195-
return new ContentstackCollection(response, http, null, WebhookCollection)
196-
} else {
197-
throw error(response)
198-
}
199-
} catch (err) {
200-
throw error(err)
201-
}
202-
}
183+
this.fetchAll = fetchAll(http, WebhookCollection)
203184
}
204185

205186
/**

lib/stack/workflow/index.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
create,
44
update,
55
deleteEntity,
6-
fetch
6+
fetch,
7+
fetchAll
78
} from '../../entity'
89
import error from '../../core/contentstackError'
910
import ContentstackCollection from '../../contentstackCollection'
@@ -265,27 +266,7 @@ export function Workflow (http, data = {}) {
265266
* .then((collection) => console.log(collection))
266267
*
267268
*/
268-
this.fetchAll = async (params) => {
269-
const headers = {}
270-
if (this.stackHeaders) {
271-
headers.headers = this.stackHeaders
272-
}
273-
if (params) {
274-
headers.params = {
275-
...cloneDeep(params)
276-
}
277-
}
278-
try {
279-
const response = await http.get(this.urlPath, headers)
280-
if (response.data) {
281-
return new ContentstackCollection(response, http, null, WorkflowCollection)
282-
} else {
283-
throw error(response)
284-
}
285-
} catch (err) {
286-
throw error(err)
287-
}
288-
}
269+
this.fetchAll = fetchAll(http, WorkflowCollection)
289270

290271
/**
291272
* @description The Publish rule allow you to create, fetch, delete, update the publish rules.

0 commit comments

Comments
 (0)