1
1
import error from './core/contentstackError'
2
2
import cloneDeep from 'lodash/cloneDeep'
3
3
import Query from './query/index'
4
+ import ContentstackCollection from './contentstackCollection'
4
5
5
6
export const publish = ( http , type ) => {
6
7
return async function ( { publishDetails, locale = null , version = null , scheduledAt = null } ) {
@@ -83,7 +84,7 @@ export const create = ({ http, params }) => {
83
84
try {
84
85
const response = await http . post ( this . urlPath , data , headers )
85
86
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 ) )
87
88
} else {
88
89
throw error ( response )
89
90
}
@@ -106,7 +107,7 @@ export const exportObject = ({ http }) => {
106
107
try {
107
108
const response = await http . get ( this . urlPath , headers )
108
109
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 ) )
110
111
} else {
111
112
throw error ( response )
112
113
}
@@ -119,8 +120,15 @@ export const exportObject = ({ http }) => {
119
120
export const query = ( { http, wrapperCollection } ) => {
120
121
return function ( params = { } ) {
121
122
if ( this . organization_uid ) {
123
+ if ( ! params . query ) {
124
+ params . query = { }
125
+ }
122
126
params . query [ 'org_uid' ] = this . organization_uid
123
127
}
128
+
129
+ if ( this . content_type_uid ) {
130
+ params . content_type_uid = this . content_type_uid
131
+ }
124
132
return Query ( http , this . urlPath , params , this . stackHeaders , wrapperCollection )
125
133
}
126
134
}
@@ -208,6 +216,29 @@ export const fetch = (http, type) => {
208
216
}
209
217
}
210
218
}
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
+ }
211
242
212
243
export function parseData ( response , stackHeaders , contentTypeUID ) {
213
244
const data = response . data || { }
0 commit comments