11import error from './core/contentstackError'
22import cloneDeep from 'lodash/cloneDeep'
33import Query from './query/index'
4+ import ContentstackCollection from './contentstackCollection'
45
56export 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 }) => {
119120export 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
212243export function parseData ( response , stackHeaders , contentTypeUID ) {
213244 const data = response . data || { }
0 commit comments