Skip to content

Commit 346ef12

Browse files
authored
Merge pull request #63 from contentstack/feature/live-preview
Feature/live preview
2 parents e28559f + ec502d9 commit 346ef12

File tree

16 files changed

+2564
-3812
lines changed

16 files changed

+2564
-3812
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
*.DS_Store
2-
examples/*
3-
node_modules/*
2+
**/node_modules/*
43
.idea/*
54
reports/*
65
apidocs-templates/*
@@ -10,4 +9,5 @@ test/sync_config.js/*
109
test/report.json/*
1110
tap-html.html
1211
*html-report
13-
coverage
12+
coverage
13+
.env

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
## Change log
2+
### Version: 3.14.0
3+
#### Date: Oct-19-2021
4+
##### New Features:
5+
- Live preview feature support added
26

37
### Version: 3.13.2
48
#### Date: May-26-2021

index.d.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,25 @@ export interface StackConfig {
6161

6262
// ContentTypeCollection
6363
export interface ContentTypeCollection{
64-
contentTypes: Array<any>
64+
content_types: Array<any>
6565
count?: number
6666
}
6767

68+
export interface LivePreview {
69+
host: string
70+
authorization: string
71+
enable: boolean
72+
}
73+
74+
export interface LivePreviewQuery {
75+
hash: string
76+
content_type_uid: string
77+
}
78+
6879
// Stack
6980
export class Stack {
7081
constructor(config: Config);
71-
constructor(api_key: string, delivery_token: string, environment_name: string, region?: Region, fetchOptions?: any);
82+
constructor(api_key: string, delivery_token: string, environment_name: string, region?: Region, fetchOptions?: any, live_preview?: LivePreview);
7283

7384
environment: string;
7485
cachePolicy: CachePolicy;
@@ -84,6 +95,7 @@ export class Stack {
8495
setHost(host: string): Stack;
8596
setCachePolicy(policy: CachePolicy): Stack;
8697
setCacheProvider(provider: object): Stack;
98+
livePreviewQuery(query: LivePreviewQuery): void;
8799
clearByQuery(): Stack;
88100
clearByContentType(): Stack;
89101
clearAll(): Stack;
@@ -219,4 +231,4 @@ export class Query extends Entry {
219231

220232
find(fetchOptions?: object): Promise<any>;
221233
findOne(): Promise<any>;
222-
}
234+
}

package-lock.json

Lines changed: 2467 additions & 3746 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "contentstack",
3-
"version": "3.13.3",
3+
"version": "3.14.0",
44
"description": "Contentstack Javascript SDK",
55
"homepage": "https://www.contentstack.com/",
66
"author": {
@@ -10,14 +10,14 @@
1010
"main": "dist/node/contentstack.js",
1111
"browser": "dist/web/contentstack.js",
1212
"types": "./index.d.ts",
13-
"_id": "contentstack@3.13.2",
13+
"_id": "contentstack@3.14.0",
1414
"scripts": {
1515
"test": "npm run test:e2e && npm run test:typescript",
1616
"test:e2e": "istanbul cover tape test/index.js | tap-html --out ./tap-html.html",
1717
"test:typescript": "jest --testPathPattern=test/typescript",
1818
"automate": "node test.js",
1919
"build:node": "webpack --config webpack/webpack.node.js",
20-
"build:web": "webpack -p --config webpack/webpack.web.js",
20+
"build:web": "webpack --config webpack/webpack.web.js",
2121
"build:react-native": "webpack --config webpack/webpack.react-native.js",
2222
"build:native-script": "webpack --config webpack/webpack.nativescript.js",
2323
"build": "NODE_ENV=production && npm run build:node && npm run build:web && npm run build:react-native && npm run build:native-script",
@@ -54,14 +54,14 @@
5454
],
5555
"dist": {
5656
"shasum": "a328ed07240476a26b31a23261355dc929e1da63",
57-
"tarball": "https://registry.npmjs.org/contentstack/-/contentstack-3.13.2.tgz"
57+
"tarball": "https://registry.npmjs.org/contentstack/-/contentstack-3.14.0.tgz"
5858
},
5959
"license": "MIT",
6060
"directories": {},
61-
"_resolved": "https://registry.npmjs.org/contentstack/-/contentstack-3.13.2.tgz",
61+
"_resolved": "https://registry.npmjs.org/contentstack/-/contentstack-3.14.0.tgz",
6262
"_npmOperationalInternal": {
6363
"host": "packages-18-east.internal.npmjs.com",
64-
"tmp": "tmp/contentstack-3.13.2.tgz_1477830884275_0.9869455888401717"
64+
"tmp": "tmp/contentstack-3.14.0.tgz_1477830884275_0.9869455888401717"
6565
},
6666
"devDependencies": {
6767
"@types/jest": "^26.0.24",
@@ -73,33 +73,34 @@
7373
"babel-preset-es2016": "6.24.1",
7474
"babel-preset-stage-1": "6.24.1",
7575
"babel-runtime": "6.26.0",
76+
"clean-webpack-plugin": "^4.0.0",
7677
"compression-webpack-plugin": "1.0.1",
7778
"es3ify-loader": "0.2.0",
7879
"fetch-mock-jest": "^1.3.0",
7980
"http-proxy-agent": "^3.0.0",
8081
"istanbul": "^0.4.5",
81-
"jest": "^26.4.2",
82+
"jest": "^27.2.5",
8283
"jest-html-reporters": "^2.1.6",
8384
"jsdoc": "^3.6.7",
84-
"jshint": "^2.13.0",
85-
"nodemailer": "^6.6.3",
85+
"jshint": "^2.13.1",
86+
"nodemailer": "^6.6.5",
8687
"request": "^2.88.2",
8788
"string-replace-loader": "1.3.0",
8889
"string-replace-webpack-plugin": "0.1.3",
8990
"tap-html": "^1.0.1",
9091
"tap-json": "1.0.0",
9192
"tape": "4.8.0",
9293
"ts-jest": "^26.5.6",
93-
"typescript": "^4.3.5",
94+
"typescript": "^4.4.3",
9495
"uglify-js": "2.8.29",
95-
"webpack": "^5.44.0",
96-
"webpack-cli": "^3.3.12",
96+
"webpack": "^5.56.1",
97+
"webpack-cli": "^4.9.0",
9798
"webpack-md5-hash": "0.0.5",
9899
"webpack-merge": "4.1.0"
99100
},
100101
"dependencies": {
101102
"@contentstack/utils": "^1.0.2",
102-
"es6-promise": "4.1.1",
103+
"es6-promise": "^4.1.1",
103104
"isomorphic-fetch": "^3.0.0",
104105
"localStorage": "1.0.3"
105106
}

src/core/lib/request.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ export default function Request(options, fetchOptions) {
4343

4444
return fetchRetry(url + '?' + queryParams,
4545
headers,
46-
fetchOptions.retryDelay,
47-
fetchOptions.retryLimit,
4846
fetchOptions,
4947
resolve,
50-
reject)
48+
reject,
49+
fetchOptions.retryDelay,
50+
fetchOptions.retryLimit)
5151

5252
});
5353
}
@@ -57,10 +57,8 @@ function wait(retryDelay) {
5757
setTimeout(resolve, retryDelay)
5858
});
5959
}
60-
async function safeParseJSON(response) {
61-
const body = await response.text();
62-
}
63-
function fetchRetry(url, headers, retryDelay = 300, retryLimit = 5, fetchOptions, resolve, reject) {
60+
61+
function fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay = 300, retryLimit = 5) {
6462
var option = Utils.mergeDeep({
6563
method: 'GET',
6664
headers: headers,

src/core/lib/utils.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Request from './request';
22
import Result from '../modules/result';
3-
import config from '../../../config';
3+
44
/**
55
* @method addSpread
66
* @description method to add the spread.
@@ -175,21 +175,6 @@ export function resultWrapper(result) {
175175
return result;
176176
};
177177

178-
// // spread the result object
179-
// export function spreadResult(result) {
180-
// let _results = [];
181-
// if (result && Object.keys(result).length) {
182-
// if (typeof result.entries !== 'undefined') _results.push(result.entries);
183-
// if (typeof result.assets !== 'undefined') _results.push(result.assets);
184-
// if (typeof result.content_type !== 'undefined' || typeof result.schema !== 'undefined') _results.push(result.content_type || result.schema);
185-
// if (typeof result.count !== 'undefined') _results.push(result.count);
186-
// if (typeof result.entry !== 'undefined') _results = result.entry;
187-
// if (typeof result.asset !== 'undefined') _results = result.asset;
188-
// if (typeof result.items !== 'undefined') _results.push(result);
189-
// }
190-
// return _results;
191-
// };
192-
193178
// spread the result object
194179
export function spreadResult(result) {
195180
let _results = [];
@@ -233,7 +218,6 @@ export function sendRequest(queryObject, options) {
233218
}
234219

235220
let self = queryObject;
236-
let continueFlag = false;
237221
let cachePolicy = (typeof self.queryCachePolicy !== 'undefined') ? self.queryCachePolicy : self.cachePolicy;
238222
let tojson = (typeof self.tojson !== 'undefined') ? self.tojson : false;
239223
let isSingle = (self.entry_uid || self.singleEntry || self.asset_uid) ? true : false;
@@ -249,14 +233,29 @@ export function sendRequest(queryObject, options) {
249233
}
250234
delete queryObject.requestParams.body.query;
251235
queryObject.requestParams.body = merge(queryObject.requestParams.body, cloneQueryObj);
252-
}
253236

237+
if (queryObject.live_preview && queryObject.live_preview.enable === true && queryObject.live_preview.hash && queryObject.live_preview.hash !== "init") {
238+
if(queryObject.live_preview.content_type_uid === queryObject.content_type_uid) {
239+
queryObject.requestParams.body = merge(queryObject.requestParams.body, {live_preview: queryObject.live_preview.hash || "init"});
240+
cachePolicy = 2; // network else cache
241+
if(queryObject.requestParams.body['environment']) {
242+
delete queryObject.requestParams.body['environment'];
243+
}
244+
if(queryObject.requestParams.headers['access_token'])
245+
delete queryObject.requestParams.headers['access_token'];
246+
247+
queryObject.requestParams.headers['authorization'] = queryObject.live_preview.management_token
248+
} else if(queryObject.live_preview.hash) {
249+
cachePolicy = 1; // cache then network
250+
}
251+
}
252+
}
254253

255254
let getCacheCallback = function() {
256255
return function(err, entries) {
257256
return new Promise(function(resolve, reject) {
258257
try {
259-
if (err) throw err;
258+
if (err) reject(err);
260259
if (!tojson) entries = resultWrapper(entries);
261260
resolve(spreadResult(entries));
262261
} catch (e) {
@@ -288,10 +287,10 @@ export function sendRequest(queryObject, options) {
288287
} else {
289288
if (cachePolicy === 2 && self.provider !== null) {
290289
self.provider.get(hashQuery, getCacheCallback());
290+
return
291291
} else {
292292
return reject({ error_code: 141, error_message: 'The requested entry doesn\'t exist.' });
293293
}
294-
return;
295294
}
296295
}
297296
else if(data.items) {
@@ -308,7 +307,7 @@ export function sendRequest(queryObject, options) {
308307
if (cachePolicy !== -1 && self.provider !== null) {
309308
self.provider.set(hashQuery, entries, function(err) {
310309
try {
311-
if (err) throw err;
310+
if (err) reject(err);
312311
if (!tojson) entries = resultWrapper(entries);
313312
return resolve(spreadResult(entries));
314313
} catch (e) {
@@ -362,7 +361,6 @@ export function sendRequest(queryObject, options) {
362361
}
363362

364363
});
365-
break;
366364
case 2:
367365
case 0:
368366
case undefined:
@@ -380,7 +378,6 @@ export function sendRequest(queryObject, options) {
380378
try {
381379
if (err || !_data) {
382380
reject(err);
383-
//reject(Error("It broke"));
384381
} else {
385382
if (!tojson) _data = resultWrapper(_data);
386383
resolve(spreadResult(_data));
@@ -393,14 +390,13 @@ export function sendRequest(queryObject, options) {
393390
});
394391

395392
return promise.then(function() {
396-
return new Promise(function(resolve, reject) {
393+
return new Promise(function(resolve, reject) {
397394
callback(true, resolve, reject);
398395
});
399396
}).catch((error) => {
400-
return new Promise(function(resolve, reject) {
397+
return new Promise(function(resolve, reject) {
401398
callback(true, resolve, reject);
402399
});
403-
console.error(error)
404400
})
405401
}
406402
};

src/core/modules/assets.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import * as Utils from '../lib/utils';
2-
import Stack from '../stack';
3-
import Query from './query';
42

53
/**
64
* @class

src/core/modules/entry.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,15 @@ export default class Entry {
331331
* @instance
332332
*/
333333
fetch(fetchOptions) {
334+
var host = this.config.host + ':' + this.config.port
335+
if(this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
336+
host = this.live_preview.host
337+
}
334338
if (this.entry_uid) {
335339
this.requestParams = {
336340
method: 'POST',
337341
headers: this.headers,
338-
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries + this.entry_uid,
342+
url: this.config.protocol + "://" + host + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries + this.entry_uid,
339343
body: {
340344
_method: 'GET',
341345
query: this._query

src/core/modules/query.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,12 @@ export default class Query extends Entry {
777777
* @instance
778778
*/
779779
findOne() {
780-
const host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version,
781-
url = (this.type && this.type === 'asset') ? host + this.config.urls.assets : host + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries;
780+
let host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version
781+
if(this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
782+
host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version
783+
}
784+
const url = (this.type && this.type === 'asset') ? host + this.config.urls.assets : host + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries;
785+
782786
this.singleEntry = true;
783787
this._query.limit = 1;
784788
this.requestParams = {

0 commit comments

Comments
 (0)