Skip to content

Commit 46a9b82

Browse files
author
hitesh.baldaniya
committed
contentstack-javascript 3.0.0 pushed
0 parents  commit 46a9b82

File tree

12 files changed

+791
-0
lines changed

12 files changed

+791
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/*
2+
reports/*
3+
apidoc-templates/*

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 built.io
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

config.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"opts": {
3+
"template": "apidocs-templates",
4+
"encoding": "utf8",
5+
"destination": "./api-reference/",
6+
"recurse": true
7+
},
8+
"source": {
9+
"include": [
10+
"contentstack.js",
11+
"src/stack.js",
12+
"src/entry/entry.js",
13+
"src/entry/query.js",
14+
"src/entry/result.js"
15+
],
16+
"exclude": [
17+
"src/cache.js"
18+
]
19+
},
20+
"tags": {
21+
"dictionaries": ["jsdoc","closure"]
22+
}
23+
}

gulpfile.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var path = require("path")
2+
3+
var gulp = require("gulp")
4+
var minify = require("gulp-minify")
5+
var gutil = require("gulp-util")
6+
var replace = require('gulp-replace')
7+
var clean = require('gulp-clean')
8+
9+
var webpack = require("webpack")
10+
11+
var pkg = require("./package.json")
12+
var webpackConfig = require("./webpack.config.js")
13+
14+
// config
15+
var config = {
16+
src: path.join(__dirname),
17+
configPath: path.join(__dirname, 'config.js'),
18+
sdkPath: path.join(__dirname, "examples", "browser", "scripts")
19+
};
20+
21+
gulp.task("copy-to-sdk", function(callback) {
22+
// copy the generated files to sdk quick start
23+
gulp
24+
.src(path.join(webpackConfig.output.path, webpackConfig.output.filename))
25+
.pipe(gulp.dest(config.sdkPath))
26+
gulp
27+
.src(path.join(webpackConfig.output.path, webpackConfig.output.filename.replace('.js', '.min.js')))
28+
.pipe(gulp.dest(config.sdkPath))
29+
})
30+
31+
gulp.task("watch", function(callback) {
32+
gulp.watch(config.src + '/**/*.js', ["build"])
33+
})
34+
35+
gulp.task("webpack:clean", function(callback) {
36+
return gulp.src([
37+
path.join(webpackConfig.output.path, webpackConfig.output.filename),
38+
path.join(webpackConfig.output.path, webpackConfig.output.filename.replace('.js', '.min.js')),
39+
path.join(config.sdkPath, webpackConfig.output.filename),
40+
path.join(config.sdkPath, webpackConfig.output.filename.replace('.js', '.min.js'))
41+
])
42+
.pipe(clean({force: true}))
43+
.pipe(gulp.dest(webpackConfig.output.path))
44+
callback()
45+
})
46+
47+
// Production build
48+
gulp.task("build", ["webpack:minbuild", "copy-to-sdk"]);
49+
50+
gulp.task("webpack:minbuild", ["webpack:build"], function(callback) {
51+
var _config = JSON.parse(JSON.stringify(webpackConfig));
52+
gulp.src(path.join(_config.output.path, _config.output.filename.replace('.js', '.min.js')))
53+
.pipe(replace("stag-api.contentstack.io", "api.contentstack.io"))
54+
.pipe(gulp.dest(_config.output.path));
55+
56+
gulp
57+
.src(path.join(_config.output.path, _config.output.filename))
58+
.pipe(replace("stag-api.contentstack.io", "api.contentstack.io"))
59+
.pipe(minify({
60+
ext:{
61+
src:'.js',
62+
min:'.min.js'
63+
},
64+
mangle: false
65+
}))
66+
.pipe(gulp.dest(_config.output.path));
67+
});
68+
69+
gulp.task("webpack:build", ["webpack:clean"], function(callback) {
70+
// run webpack
71+
webpack(webpackConfig, function(err, stats) {
72+
if(err) throw new gutil.PluginError("webpack:build", err);
73+
gutil.log("[webpack:build]", stats.toString({
74+
colors: true
75+
}));
76+
callback();
77+
});
78+
});
79+
80+
gulp.task("default", ["watch"]);

lib/request.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
/**
3+
* Module dependencies.
4+
*/
5+
var HTTPRequest;
6+
var Utils = require('./utils');
7+
var when = require('when');
8+
9+
// if script is running under node.js then use node-XMLHttpRequest node modules for the built-in http client to emulate the browser XMLHttpRequest object.
10+
// else assign XMLHttpRequest object
11+
if (Utils.isBrowser() && XMLHttpRequest) {
12+
HTTPRequest = XMLHttpRequest;
13+
} else {
14+
HTTPRequest = require('xmlhttprequest').XMLHttpRequest;
15+
}
16+
17+
function Request(options) {
18+
var serialize = function(obj, prefix) {
19+
var str = [], p;
20+
for(p in obj) {
21+
if (obj.hasOwnProperty(p)) {
22+
var k = prefix ? prefix + "[" + p + "]" : p,
23+
v = obj[p];
24+
str.push((v !== null && typeof v === "object" && p !== 'query') ?
25+
serialize(v, k) :
26+
encodeURIComponent(k) + "=" + (p !== 'query' ? encodeURIComponent(v) : JSON.stringify(v)));
27+
}
28+
}
29+
return str.join("&");
30+
}
31+
32+
var deferred = when.defer();
33+
var xhr = new HTTPRequest(),
34+
method = "GET",
35+
url = options.url,
36+
headers = options.headers;
37+
38+
if(options.body && typeof options.body === 'object'){
39+
delete options.body._method;
40+
var queryParams = serialize(options.body);
41+
}
42+
43+
44+
//make all calls as GET instead of POST
45+
xhr.open(method, url+'?'+queryParams, true);
46+
// set headers
47+
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
48+
for (var header in headers) {
49+
xhr.setRequestHeader(header, headers[header]);
50+
}
51+
52+
// send stringify data
53+
if (options.body && method == "POST" || method == "PUT") {
54+
if (typeof options.body === 'object') {
55+
xhr.send(JSON.stringify(options.body));
56+
} else {
57+
xhr.send(options.body);
58+
}
59+
} else {
60+
xhr.send();
61+
}
62+
63+
// collect response
64+
xhr.onreadystatechange = function() {
65+
if (xhr.readyState === 4) {
66+
var data = xhr.responseText,
67+
error;
68+
try {
69+
data = JSON.parse(data);
70+
} catch (e) {
71+
error = {error_code: 141, message: 'Could not parse the response received from the server.'};
72+
}
73+
if (xhr.status >= 200 && xhr.status < 300) {
74+
deferred.resolve(data);
75+
} else {
76+
deferred.reject(data || error);
77+
}
78+
}
79+
};
80+
return deferred.promise;
81+
}
82+
83+
/**
84+
* Expose `Request`.
85+
*/
86+
module.exports = Request;

0 commit comments

Comments
 (0)