Skip to content

Commit 82a8aad

Browse files
committed
Global Fields test case
1 parent 99f7509 commit 82a8aad

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

lib/stack/contentType/index.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import {
44
update,
55
deleteEntity,
66
fetch,
7-
query
7+
query,
8+
upload,
9+
parseData
810
} from '../../entity'
911
import { Entry } from './entry/index'
12+
import error from '../../core/contentstackError'
1013

14+
import FormData from 'form-data'
15+
import { createReadStream } from 'fs'
1116
/**
1217
* Content type defines the structure or schema of a page or a section of your web or mobile property. To create content for your application, you are required to first create a content type, and then create entries using the content type. Read more about <a href='https://www.contentstack.com/docs/guide/content-types'>Content Types</a>.
1318
* @namespace ContentType
@@ -146,6 +151,40 @@ export function ContentType (http, data = {}) {
146151
* .then((contentTypes) => console.log(contentTypes))
147152
*/
148153
this.query = query({ http: http, wrapperCollection: ContentTypeCollection })
154+
155+
/**
156+
* @description The Import a content type call imports a content type into a stack.
157+
* @memberof ContentType
158+
* @func import
159+
* @param {String} data.content_type path to file
160+
* @example
161+
* import * as contentstack from '@contentstack/management'
162+
* const client = contentstack.client()
163+
*
164+
* const data = {
165+
* content_type: 'path/to/file.png',
166+
* }
167+
* client.stack({ api_key: 'api_key'}).contentType().import(data)
168+
* .then((contentType) => console.log(contentType))
169+
*
170+
*/
171+
this.import = async function (data) {
172+
try {
173+
const response = await upload({
174+
http: http,
175+
urlPath: `${this.urlPath}/import`,
176+
stackHeaders: this.stackHeaders,
177+
formData: createFormData(data)
178+
})
179+
if (response.data) {
180+
return new this.constructor(http, parseData(response, this.stackHeaders))
181+
} else {
182+
throw error(response)
183+
}
184+
} catch (err) {
185+
throw error(err)
186+
}
187+
}
149188
}
150189
return this
151190
}
@@ -157,3 +196,10 @@ export function ContentTypeCollection (http, data) {
157196
})
158197
return contentTypeCollection
159198
}
199+
200+
function createFormData (data) {
201+
const formData = new FormData()
202+
const uploadStream = createReadStream(data.content_type)
203+
formData.append('content_type', uploadStream)
204+
return formData
205+
}

lib/stack/globalField/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export function GlobalField (http, data = {}) {
141141
}
142142

143143
export function GlobalFieldCollection (http, data) {
144-
const obj = cloneDeep(data.global_fields)
144+
const obj = cloneDeep(data.global_fields) || []
145145
const globalFieldCollection = obj.map((userdata) => {
146146
return new GlobalField(http, { global_field: userdata, stackHeaders: data.stackHeaders })
147147
})

0 commit comments

Comments
 (0)