-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
128 lines (126 loc) · 3.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import error from '../core/contentstackError'
import cloneDeep from 'lodash/cloneDeep'
import ContentstackCollection from '../contentstackCollection'
export default function Query (http, urlPath, param, stackHeaders = null, wrapperCollection) {
const headers = {}
if (stackHeaders) {
headers.headers = stackHeaders
}
var contentTypeUid = null
if (param) {
if (param.content_type_uid) {
contentTypeUid = param.content_type_uid
delete param.content_type_uid
}
headers.params = {
...cloneDeep(param)
}
}
/**
* @description This method will fetch content of query on specified module.
* @returns {ContentstackCollection} Result collection of content of specified module.
* @example All Stack
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().query().find()
* .then((collection) => console.log(collection))
*
* @example Query on stack
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().query( { query: { name: 'Stack name' } }).find()
* .then((collection) => console.log(collection))
*
*/
const find = async () => {
try {
const response = await http.get(urlPath, headers)
if (response.data) {
if (contentTypeUid) {
response.data.content_type_uid = contentTypeUid
}
return new ContentstackCollection(response, http, stackHeaders, wrapperCollection)
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
/**
* @description This method will fetch count of content for query on specified module.
* @returns {Object} Result is Object of content of specified module.
* @example All Stack
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().query().count()
* .then((response) => console.log(response))
*
* @example Query on Asset
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).query({ query: { title: 'Stack name' } }).count()
* .then((response) => console.log(response))
*
*/
const count = async () => {
headers.params = {
...headers.params,
count: true
}
try {
const response = await http.get(urlPath, headers)
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
/**
* @description This method will fetch content of query on specified module.
* @returns {ContentstackCollection} Result content of specified module.
* @example Stack
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().query().findOne()
* .then((collection) => console.log(collection))
*
* @example Query on stack
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().query({ query: { title: 'Stack name' } }).findOne()
* .then((collection) => console.log(collection))
*
*/
const findOne = async () => {
const limitHeader = headers
limitHeader.params.limit = 1
try {
const response = await http.get(urlPath, limitHeader)
if (response.data) {
if (contentTypeUid) {
response.data.content_type_uid = contentTypeUid
}
return new ContentstackCollection(response, http, stackHeaders, wrapperCollection)
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
return {
count: count,
find: find,
findOne: findOne
}
}