Skip to content

Commit 854b509

Browse files
committed
API Mock folder created
1 parent d2cad5b commit 854b509

23 files changed

Lines changed: 163 additions & 170 deletions

File tree

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ import contentstack from ‘@contentstack/management’
2424
```
2525
To initialize the SDK, you will need to pass ```axios``` instance as follows:
2626
```
27-
import axios from 'axios'
28-
contentstackClient = contentstack.client(axios, {})
27+
contentstackClient = contentstack.client()
2928
```
3029

3130
### Authentication
3231
To use this SDK, you need to authenticate your users by using the Authtoken, credentials, or Management Token (stack-level token).
3332
### Authtoken
3433
An [Authtoken](https://www.contentstack.com/docs/developers/create-tokens/types-of-tokens/#authentication-tokens-authtokens-) is a read-write token used to make authorized CMA requests, and it is a **user-specific** token.
3534
```
36-
import axios from 'axios'
37-
contentstackClient = contentstack.client(axios, { authtoken: 'AUTHTOKEN' })
35+
contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' })
3836
```
3937
### Login
4038
To Login to Contentstack by using credentials, you can use the following lines of code:
@@ -49,7 +47,7 @@ contentstackClient.login({ email: 'EMAIL', password: 'PASSWORD'})
4947
### Management Token
5048
[Management Tokens](https://www.contentstack.com/docs/developers/create-tokens/about-management-tokens/) are **stack-level** tokens, with no users attached to them.
5149
```
52-
contentstackClient.stack('API_KEY', 'MANAGEMENT_TOKEN')
50+
contentstackClient.stack({ apiKey: 'API_KEY', managementToken: 'MANAGEMENT_TOKEN' })
5351
.fetch()
5452
.then((stack) => {
5553
console.log(stack)
@@ -60,14 +58,13 @@ contentstackClient.stack('API_KEY', 'MANAGEMENT_TOKEN')
6058
To use the JavaScript CMA SDK, you need to first initialize it. To do this, use the following code:
6159
```
6260
import contentstack from ‘@contentstack/management’
63-
import axios from 'axios'
6461
65-
var contentstackClient = contentstack.client(axios, { authtoken: 'AUTHTOKEN' })
62+
var contentstackClient = contentstack.client({ authtoken: 'AUTHTOKEN' })
6663
```
6764
#### Fetch Stack Detail
6865
Use the following lines of code to fetch your stack detail using this SDK:
6966
```
70-
contentstackClient.stack('API_KEY')
67+
contentstackClient.stack({ apiKey: 'API_KEY' })
7168
.fetch()
7269
.then((stack) => {
7370
console.log(stack)
@@ -82,7 +79,7 @@ var entry = {
8279
url: '/sampleEntry'
8380
}
8481
85-
contentstackClient.stack('API_KEY').contentType('CONTENT_TYPE_UID').entry().create({ entry })
82+
contentstackClient.stack({ apiKey: 'API_KEY' }).contentType('CONTENT_TYPE_UID').entry().create({ entry })
8683
.then((entry) => {
8784
console.log(entry)
8885
})
@@ -96,7 +93,7 @@ var asset = {
9693
title: 'Asset Title'
9794
}
9895
99-
contentstackClient.stack('API_KEY').asset().create({ asset })
96+
contentstackClient.stack({ apiKey: 'API_KEY' }).asset().create({ asset })
10097
.then((asset) => {
10198
console.log(asset)
10299
})

lib/contentstack.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
* The Content Management API (CMA) is used to manage the content of your Contentstack account. This includes creating, updating, deleting, and fetching content of your account.
33
* @namespace Contentstack
44
*/
5-
import httpClient from './core/contentstackHTTPClient.js'
5+
import packages from '../package.json'
66
import clonedeep from 'lodash/cloneDeep'
77
import getUserAgent from './core/Util.js'
88
import contentstackClient from './contentstackClient.js'
9-
import packages from '../package.json'
9+
import httpClient from './core/contentstackHTTPClient.js'
1010

1111
/**
1212
* Create client instance
1313
* @name client
1414
* @memberof Contentstack
15-
* @param {Object} axios - Axios Object
1615
* @param {object} params - Client initialization parameters
17-
* @prop {string} params.host - API host (default: api.contentstack.com)
16+
* @param {Object} param.proxy -
17+
* @prop {string} params.host - API host (default: api.contentstack.io)
1818
* @prop {object} params.headers - Optional additional headers
1919
* @prop {number} params.timeout - Optional number of milliseconds before the request times out. Default is 30000
2020
* @prop {number} params.retryLimit - Optional number of retries before failure. Default is 5
@@ -24,11 +24,9 @@ import packages from '../package.json'
2424
* @returns Contentstack.Client
2525
* @example
2626
* import * as contentstack from '@contentstack/management'
27-
* const client = contentstack.client({
28-
*
29-
* })
27+
* const client = contentstack.client()
3028
*/
31-
export function client (axios, params) {
29+
export function client (params = {}) {
3230
const defaultParameter = {
3331
defaultHostName: 'api.contentstack.io'
3432
}
@@ -56,7 +54,7 @@ export function client (axios, params) {
5654
...params.headers,
5755
...requiredHeaders
5856
}
59-
const http = httpClient(axios, params)
57+
const http = httpClient(params)
6058
const api = contentstackClient({
6159
http: http
6260
})

lib/contentstackClient.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { Stack } from './stack/index.js'
55
import { Organization } from './organization/index'
6-
// import { create, query } from './entity'
6+
import cloneDeep from 'lodash/cloneDeep'
77
import { User } from './user/index'
88
import error from './core/contentstackError'
99

@@ -19,7 +19,7 @@ export default function contentstackClient ({ http }) {
1919
* @returns {Promise}
2020
* @example
2121
* import * as contentstack from '@contentstack/management'
22-
* const client = contentstack.client({})
22+
* const client = contentstack.client()
2323
*
2424
* client.login({ email: <emailid>, password: <password> })
2525
* .then(() => console.log('Logged in successfully'))
@@ -44,7 +44,7 @@ export default function contentstackClient ({ http }) {
4444
* @returns {Promise}
4545
* @example
4646
* import * as contentstack from '@contentstack/management'
47-
* const client = contentstack.client({})
47+
* const client = contentstack.client()
4848
*
4949
* client.getUser()
5050
* .then((user) => console.log(user))
@@ -60,39 +60,34 @@ export default function contentstackClient ({ http }) {
6060
* @description Get Stack instance. A stack is a space that stores the content of a project.
6161
* @memberof ContentstackClient
6262
* @func stack
63-
* @param {String} api_key - Stack API Key
63+
* @param {String} apiKey - Stack API Key
64+
* @param {String} managementToken - Stack API Key
6465
* @returns {Stack} Instance of Stack
6566
*
6667
* @example
6768
* import * as contentstack from '@contentstack/management'
68-
* const client = contentstack.client({})
69+
* const client = contentstack.client()
6970
* const stack = {name: 'My New Stack'}
7071
* client.stack().create({ stack }, { organization_uid: 'org_uid' })
7172
* .then((stack) => console.log(stack))
7273
*
7374
* @example
7475
* import * as contentstack from '@contentstack/management'
75-
* const client = contentstack.client({})
76+
* const client = contentstack.client()
7677
*
7778
* client.stack('api_key').fetch()
7879
* .then((stack) => console.log(stack))
7980
*
8081
* @example
8182
* import * as contentstack from '@contentstack/management'
82-
* const client = contentstack.client({})
83+
* const client = contentstack.client()
8384
*
8485
* client.stack('api_key', 'management_token').fetch()
8586
* .then((stack) => console.log(stack))
8687
*
8788
*/
88-
function stack (apiKey = null, managementToken = null) {
89-
var stack = {}
90-
if (apiKey && apiKey !== undefined) {
91-
stack.api_key = apiKey
92-
}
93-
if (managementToken && managementToken !== undefined) {
94-
stack.authorization = managementToken
95-
}
89+
function stack (params = {}) {
90+
var stack = { ...cloneDeep(params) }
9691
return new Stack(http, { stack })
9792
}
9893

@@ -105,14 +100,14 @@ export default function contentstackClient ({ http }) {
105100
*
106101
* @example
107102
* import * as contentstack from '@contentstack/management'
108-
* const client = contentstack.client({})
103+
* const client = contentstack.client()
109104
*
110105
* client.organization().query().find()
111106
* .then((organization) => console.log(organization))
112107
*
113108
* @example
114109
* import * as contentstack from '@contentstack/management'
115-
* const client = contentstack.client({})
110+
* const client = contentstack.client()
116111
*
117112
* client.organization('org_uid').fetch()
118113
* .then((organization) => console.log(organization))
@@ -131,7 +126,7 @@ export default function contentstackClient ({ http }) {
131126
*
132127
* @example
133128
* import * as contentstack from '@contentstack/management'
134-
* const client = contentstack.client({})
129+
* const client = contentstack.client()
135130
* const notice = client.logout()
136131
*
137132
*/

lib/core/contentstack-retry.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
export default function contentstckRetry (axios, defaultOptions, retryLimit = 5, retryDelay = 2) {
33
var networkError = 0
44
axios.interceptors.request.use(function (config) {
5-
// var currentState = config['contentstack'] || {}
6-
// config['contentstack'] = currentState
75
if (config.headers.authorization && config.headers.authorization !== undefined) {
86
delete config.headers.authtoken
97
}
@@ -22,19 +20,21 @@ export default function contentstckRetry (axios, defaultOptions, retryLimit = 5,
2220
var response = error.response
2321
if (!response) {
2422
retryErrorType = `Server connection`
23+
return Promise.reject(error)
24+
}
25+
26+
if (response && response.status === 429) {
27+
retryErrorType = 'Rate Limit'
2528
networkError++
2629
if (networkError > retryLimit) {
30+
networkError = 0
2731
return Promise.reject(error)
2832
}
33+
wait = retryDelay
2934
} else {
3035
networkError = 0
3136
}
3237

33-
if (response && response.status === 429) {
34-
retryErrorType = 'Rate Limit'
35-
wait = retryDelay
36-
}
37-
3838
if (retryErrorType && error.config !== undefined) {
3939
var config = error.config
4040
defaultOptions.logHandler('warning', `${retryErrorType} error occurred. Waiting for ${wait} ms before retrying...`)

lib/core/contentstackHTTPClient.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import clonedeep from 'lodash/cloneDeep'
22
import Qs from 'qs'
3-
// import axiosRetry from 'axios-retry'
3+
import axios from 'axios'
44
import contentstackRetry from './contentstack-retry'
55
const HOST_REGEX = /^(?!\w+:\/\/)([^\s:]+\.[^\s:]+)(?::(\d+))?(?!:)$/
66

7-
export default function contentstackHttpClient (axios, options) {
7+
export default function contentstackHttpClient (options) {
88
const defaultConfig = {
99
insecure: false,
1010
retryOnError: true,
@@ -65,6 +65,7 @@ export default function contentstackHttpClient (axios, options) {
6565
timeout: config.timeout,
6666
adapter: config.adapter,
6767
maxContentLength: config.maxContentLength,
68+
maxBodyLength: config.maxBodyLength,
6869
// Contentstack
6970
logHandler: config.logHandler,
7071
responseLogger: config.responseLogger,

lib/organization/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function Organization (http, data) {
2424
* @returns {Promise<Organization.Organization>} Promise for Organization instance
2525
* @example
2626
* import * as contentstack from '@contentstack/management'
27-
* const client = contentstack.client({})
27+
* const client = contentstack.client()
2828
*
2929
* client.organization('organization_uid).fetch()
3030
* .then((organization) => console.log(organization))
@@ -46,7 +46,7 @@ export function Organization (http, data) {
4646
* @returns {ContentstackCollection} Instance of ContentstackCollection.
4747
* @example
4848
* import * as contentstack from '@contentstack/management'
49-
* const client = contentstack.client({})
49+
* const client = contentstack.client()
5050
*
5151
* client.organization('organization_uid).stacks({ include_count: true })
5252
* .then((organization) => console.log(organization))
@@ -73,7 +73,7 @@ export function Organization (http, data) {
7373
* @returns {Object} Response Object.
7474
* @example
7575
* import * as contentstack from '@contentstack/management'
76-
* const client = contentstack.client({})
76+
* const client = contentstack.client()
7777
*
7878
* client.stack('api_key').transferOwnership('emailId')
7979
* .then((notice) => console.log(notice))
@@ -99,7 +99,7 @@ export function Organization (http, data) {
9999
* @returns {ContentstackCollection} ContentstackCollection of instance.
100100
* @example
101101
* import * as contentstack from '@contentstack/management'
102-
* const client = contentstack.client({})
102+
* const client = contentstack.client()
103103
*
104104
* client.organization('organization_uid).addUser({ users: { 'abc@test.com': ['org_uid1', 'org_uid2' ]}, stacks: { 'abc@test.com': { 'api_key1': [ 'stack_role_id' ] } } })
105105
* .then((response) => console.log(response))
@@ -125,7 +125,7 @@ export function Organization (http, data) {
125125
* @returns {String} Success message of invitation send.
126126
* @example
127127
* import * as contentstack from '@contentstack/management'
128-
* const client = contentstack.client({})
128+
* const client = contentstack.client()
129129
*
130130
* client.organization('organization_uid).getInvitations()
131131
* .then((notice) => console.log(notice))
@@ -151,7 +151,7 @@ export function Organization (http, data) {
151151
* @returns {Object} Response Object.
152152
* @example
153153
* import * as contentstack from '@contentstack/management'
154-
* const client = contentstack.client({})
154+
* const client = contentstack.client()
155155
*
156156
* client.organization('organization_uid).resendInvitition('invitation_uid')
157157
* .then((notice) => console.log(notice))
@@ -183,7 +183,7 @@ export function Organization (http, data) {
183183
* @returns {Array<Role>} Array of Role instance
184184
* @example
185185
* import * as contentstack from '@contentstack/management'
186-
* const client = contentstack.client({})
186+
* const client = contentstack.client()
187187
*
188188
* client.organization('organization_uid).roles()
189189
* .then((roles) => console.log(roles))
@@ -216,7 +216,7 @@ export function Organization (http, data) {
216216
* @returns {ContentstackCollection} Result collection of content of specified module.
217217
* @example
218218
* import * as contentstack from '@contentstack/management'
219-
* const client = contentstack.client({})
219+
* const client = contentstack.client()
220220
*
221221
* client.organization().fetchAll()
222222
* .then((collection) => console.log(collection))

lib/query/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ export default function Query (http, urlPath, param, stackHeaders = null, wrappe
1717
* @returns {ContentstackCollection} Result collection of content of specified module.
1818
* @example All Stack
1919
* import * as contentstack from '@contentstack/management'
20-
* const client = contentstack.client({})
20+
* const client = contentstack.client()
2121
*
2222
* client.stack().query().find()
2323
* .then((collection) => console.log(collection))
2424
*
2525
* @example Query on stack
2626
* import * as contentstack from '@contentstack/management'
27-
* const client = contentstack.client({})
27+
* const client = contentstack.client()
2828
*
2929
* client.stack().query( { query: { name: 'Stack name' } }).find()
3030
* .then((collection) => console.log(collection))
@@ -47,14 +47,14 @@ export default function Query (http, urlPath, param, stackHeaders = null, wrappe
4747
* @returns {Object} Result is Object of content of specified module.
4848
* @example All Stack
4949
* import * as contentstack from '@contentstack/management'
50-
* const client = contentstack.client({})
50+
* const client = contentstack.client()
5151
*
5252
* client.stack().query().count()
5353
* .then((response) => console.log(response))
5454
*
5555
* @example Query on Asset
5656
* import * as contentstack from '@contentstack/management'
57-
* const client = contentstack.client({})
57+
* const client = contentstack.client()
5858
*
5959
* client.stack('api_key').query({ query: { title: 'Stack name' } }).count()
6060
* .then((response) => console.log(response))
@@ -81,14 +81,14 @@ export default function Query (http, urlPath, param, stackHeaders = null, wrappe
8181
* @returns {ContentstackCollection} Result content of specified module.
8282
* @example Stack
8383
* import * as contentstack from '@contentstack/management'
84-
* const client = contentstack.client({})
84+
* const client = contentstack.client()
8585
*
8686
* client.stack().query().findOne()
8787
* .then((collection) => console.log(collection))
8888
*
8989
* @example Query on stack
9090
* import * as contentstack from '@contentstack/management'
91-
* const client = contentstack.client({})
91+
* const client = contentstack.client()
9292
*
9393
* client.stack().query({ query: { title: 'Stack name' } }).findOne()
9494
* .then((collection) => console.log(collection))

lib/stack/asset/folders/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function Folder (http, data = {}) {
2222
*
2323
* @example
2424
* import * as contentstack from '@contentstack/management'
25-
* const client = contentstack.client({})
25+
* const client = contentstack.client()
2626
* const asset = {name: 'My New contentType'}
2727
* client.stack().asset().folders().create({ asset })
2828
* .then((folder) => console.log(folder))

0 commit comments

Comments
 (0)