Skip to content

Commit f4c9fd0

Browse files
Merge pull request #1 from builtio-contentstack/add_param_method
Add param method
2 parents 4b62c7b + 9360e59 commit f4c9fd0

File tree

5 files changed

+66
-20
lines changed

5 files changed

+66
-20
lines changed

csio-templates/tmpl/layout.tmpl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,31 @@
4040
<p>You need Node.js version 4.4.7 or later installed to use the Contentstack JavaScript SDK.</p>
4141
<h3><a id="Setup_and_Installation_13"></a>Setup and Installation</h3>
4242
<h4><a id="For_JavaScript_Browser_15"></a>For JavaScript (Browser)</h4>
43-
<p>To use the JavaScript SDK, download it from <a href="https://contentstack.com/docs/platforms/javascript-browser/javascript_sdk_latest">here</a> and include it in the &lt;script&gt; tag:</p>
44-
<pre><code>&lt;script type="text/javascript" src="/path/to/contentstack.min.js"&gt;&lt;/script&gt;;
43+
<p>To use the JavaScript SDK, download it from <a href="https://www.contentstack.com/docs/platforms/javascript-browser/javascript_sdk_latest">here</a> and include it in the &lt;script&gt; tag:</p>
44+
<pre class="prettyprint"><code>&lt;script type="text/javascript" src="/path/to/contentstack.min.js"&gt;&lt;/script&gt;;
4545
</code></pre>
4646
<p>To initialize the SDK, you will need to specify the API Key, Access Token, and Environment Name of your stack.</p>
47-
<pre><code>const Stack = Contentstack.Stack("api_key", "access_token", "environment_name");
47+
<pre class="prettyprint"><code>const Stack = Contentstack.Stack("api_key", "access_token", "environment_name");
4848
</code></pre>
4949
<h4><a id="For_Nodejs_25"></a>For Node.js</h4>
50-
<p>Node.js uses the Javascript SDK to create apps. To use the JavaScript SDK, download it from <a href="https://contentstack.com/docs/platforms/javascript-browser/javascript_sdk_latest">here</a>, OR install it via npm:</p>
51-
<pre><code>npm -i contentstack
50+
<p>Node.js uses the Javascript SDK to create apps. To use the JavaScript SDK, download it from <a href="https://www.contentstack.com/docs/platforms/javascript-browser/javascript_sdk_latest">here</a>, OR install it via npm:</p>
51+
<pre class="prettyprint"><code>npm -i contentstack
5252
</code></pre>
5353
<p>To import the SDK in your project, use the following command:</p>
54-
<pre><code>import contentstack from ‘contentstack’
54+
<pre class="prettyprint"><code>import contentstack from ‘contentstack’
5555
</code></pre>
5656
<p>To initialize the SDK, you will need to specify the API Key, Access Token, and Environment Name of your stack.</p>
57-
<pre><code>const Stack = Contentstack.Stack("api_key","access_token","environment_name");
57+
<pre class="prettyprint"><code>const Stack = Contentstack.Stack("api_key","access_token","environment_name");
5858
</code></pre>
5959
<h4><a id="For_React_Native_39"></a>For React Native</h4>
60-
<p>React Native uses the Javascript SDK to create apps. To use the JavaScript SDK, download it from <a href="https://contentstack.com/docs/platforms/javascript-browser/javascript_sdk_latest">here</a>, OR install ist via npm:</p>
61-
<pre><code>npm -i contentstack
60+
<p>React Native uses the Javascript SDK to create apps. To use the JavaScript SDK, download it from <a href="https://www.contentstack.com/docs/platforms/javascript-browser/javascript_sdk_latest">here</a>, OR install ist via npm:</p>
61+
<pre class="prettyprint"><code>npm -i contentstack
6262
</code></pre>
6363
<p>To import the SDK in your project, use the following command:</p>
64-
<pre><code>import contentstack from `contentstack/react-native`
64+
<pre class="prettyprint" class="prettyprint"><code>import contentstack from `contentstack/react-native`
6565
</code></pre>
6666
<p>To initialize the SDK, you will need to specify the API Key, Access Token, and Environment Name of your stack.</p>
67-
<pre><code>const Stack = Contentstack.Stack("api_key", "access_token", "environment_name");
67+
<pre class="prettyprint"><code>const Stack = Contentstack.Stack("api_key", "access_token", "environment_name");
6868
</code></pre>
6969
<h3><a id="Key_Concepts_for_using_Contentstack_54"></a>Key Concepts for using Contentstack</h3>
7070
<h4><a id="Stack_56"></a>Stack</h4>
@@ -80,12 +80,12 @@
8080
<h3><a id="Contentstack_JavaScript_SDK_5minute_Quickstart_78"></a>Contentstack JavaScript SDK: 5-minute Quickstart</h3>
8181
<h4><a id="Initializing_your_SDK_80"></a>Initializing your SDK</h4>
8282
<p>You will need to specify the API key, Access token, and Environment Name of your stack to initialize the SDK:</p>
83-
<pre><code>const Stack = Contentstack.Stack("api_key", "access_token", "environment_name");
83+
<pre class="prettyprint"><code>const Stack = Contentstack.Stack("api_key", "access_token", "environment_name");
8484
</code></pre>
8585
<p>Once you have initialized the SDK, you can start getting content in your app.</p>
8686
<h4><a id="Querying_content_from_your_stack_90"></a>Querying content from your stack</h4>
8787
<p>To get a single entry, you need to specify the content type as well as the ID of the entry.</p>
88-
<pre><code>const Query = Stack.ContentType('blog').Entry("blt123something");
88+
<pre class="prettyprint"><code>const Query = Stack.ContentType('blog').Entry("blt123something");
8989

9090
Query.fetch()
9191
.then(function success(entry) {
@@ -96,7 +96,7 @@
9696
});
9797
</code></pre>
9898
<p>To retrieve multiple entries of a content type, you need to specify the content type uid. You can also specify search parameters to filter results.</p>
99-
<pre><code>const Query = Stack.ContentType('blog').Query();
99+
<pre class="prettyprint"><code>const Query = Stack.ContentType('blog').Query();
100100

101101
Query
102102
.where("title", "welcome")
@@ -117,17 +117,17 @@
117117
<p>You can set a cache policy on a stack and/or query object.</p>
118118
<h5><a id="Setting_a_cache_policy_on_a_stack_127"></a>Setting a cache policy on a stack</h5>
119119
<p>This option allows you to globalize a cache policy. This means the cache policy you set will be applied to all the query objects of the stack.</p>
120-
<pre><code>//Setting a cache policy on a stack
120+
<pre class="prettyprint"><code>//Setting a cache policy on a stack
121121
Stack.setCachePolicy(Contentstack.CachePolicy.NETWORK_ELSE_CACHE)
122122
</code></pre>
123123
<h5><a id="Setting_a_cache_policy_on_a_query_object_134"></a>Setting a cache policy on a query object</h5>
124124
<p>This option allows you to set/override a cache policy on a specific query object.</p>
125-
<pre><code>// setting a cache policy on a queryobject
125+
<pre class="prettyprint"><code>// setting a cache policy on a queryobject
126126
Query.setCachePolicy(Contentstack.CachePolicy.CACHE_THEN_NETWORK)
127127
</code></pre>
128128
<h3><a id="Advanced_Queries_141"></a>Advanced Queries</h3>
129129
<p>You can query for content types, entries, assets and more using our JavaScript API Reference.</p>
130-
<p><a href="https://contentstack.com/docs/platforms/javascript-browser/api-reference">JavaScript API Reference Doc</a></p>
130+
<p><a href="https://www.contentstack.com/docs/platforms/javascript-browser/api-reference/">JavaScript API Reference Doc</a></p>
131131
<h3><a id="Working_with_Images_147"></a>Working with Images</h3>
132132
<p>We have introduced Image Delivery APIs that let you retrieve images and then manipulate and optimize them for your digital properties. It lets you perform a host of other actions such as crop, trim, resize, rotate, overlay, and so on.</p>
133133
<p>For example, if you want to crop an image (with width as 300 and height as 400), you simply need to append query parameters at the end of the image URL, such as, <a href="https://images.contentstack.io/v3/assets/blteae40eb499811073/bltc5064f36b5855343/59e0c41ac0eddd140d5a8e3e/download?crop=300,400">https://images.contentstack.io/v3/assets/blteae40eb499811073/bltc5064f36b5855343/59e0c41ac0eddd140d5a8e3e/download?crop=300,400</a>. There are several more parameters that you can use for your images.</p>
@@ -136,8 +136,8 @@
136136
<h3><a id="Helpful_Links_157"></a>Helpful Links</h3>
137137
<ul>
138138
<li><a href="https://www.contentstack.com">Contentstack Website</a></li>
139-
<li><a href="https://contentstack.com/docs">Official Documentation</a></li>
140-
<li><a href="https://contentstack.com/docs/apis/content-delivery-api/">Content Delivery API Docs</a></li>
139+
<li><a href="https://www.contentstack.com/docs">Official Documentation</a></li>
140+
<li><a href="https://www.contentstack.com/docs/apis/content-delivery-api/">Content Delivery API Docs</a></li>
141141
</ul>
142142
<h3><a id="The_MIT_License_MIT_163"></a>The MIT License (MIT)</h3>
143143
<p>Copyright © 2012-2017 <a href="https://www.built.io/">Built.io</a>. All Rights Reserved</p>

src/core/lib/request.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default function Request(options) {
88
return new Promise(function(resolve, reject) {
99
let queryParams;
1010
let serialize = function(obj, prefix) {
11+
1112
let str = [],
1213
p;
1314
if (typeof obj === "object" && obj.length !== undefined) {
@@ -20,7 +21,7 @@ export default function Request(options) {
2021
v = obj[p];
2122
str.push((v !== null && typeof v === "object" && p !== 'query') ?
2223
serialize(v, k) :
23-
k + "=" + (p !== 'query' ? encodeURIComponent(v) : JSON.stringify(v)));
24+
k + "=" + encodeURIComponent(p !== 'query' ? v : JSON.stringify(v)));
2425
}
2526
}
2627
return str.join("&");

src/core/modules/assets.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Query from './query';
1313
*/
1414
export default class Assets {
1515
constructor() {
16+
this._query = {};
1617
/**
1718
* @method only
1819
* @description This method is use to show the selected fields of the assets in resultset.
@@ -62,6 +63,19 @@ export default class Assets {
6263
return this;
6364
}
6465

66+
/**
67+
* @method AddParam
68+
* @description This method includes query parameter in query.
69+
* @example Stack.Assets('bltsomething123').addParam('include_dimension', 'true').fetch()
70+
*/
71+
addParam(key, value) {
72+
if (key && typeof key === 'string' && value && typeof value === 'string') {
73+
this._query[key] = value;
74+
return this;
75+
} else {
76+
console.error("Kindly provide a valid parameters.");
77+
}
78+
}
6579

6680
/**
6781
* @method fetch
@@ -80,6 +94,7 @@ export default class Assets {
8094
query: this._query
8195
}
8296
}
97+
8398
return Utils.sendRequest(this);
8499
} else {
85100
console.error("Kindly provide an asset uid. e.g. .Assets('bltsomething123')");

src/core/modules/entry.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ export default class Entry {
191191
return this;
192192
}
193193

194+
/**
195+
* @method AddParam
196+
* @description This method includes query parameter in query.
197+
* @example blogQuery.addParam('include_count', 'true').fetch()
198+
*/
199+
addParam(key, value) {
200+
if (key && value && typeof key === 'string' && typeof value === 'string') {
201+
this._query[key] = value;
202+
return this;
203+
} else {
204+
console.error("Kindly provide valid parameters.");
205+
}
206+
}
207+
208+
194209
/**
195210
* @method fetch
196211
* @description fetch entry of requested content_type of defined query if present.

src/core/modules/query.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,21 @@ export default class Query extends Entry {
366366
return this;
367367
}
368368

369+
/**
370+
* @method AddParam
371+
* @description This method includes query parameter in query.
372+
* @example blogQuery.addParam('include_count', 'true')
373+
* @returns {Query}
374+
*/
375+
addParam(key, value) {
376+
if (key && value && typeof key === 'string' && typeof value === 'string') {
377+
this._query[key] = value;
378+
return this;
379+
} else {
380+
console.error("Kindly provide valid parameters.");
381+
}
382+
}
383+
369384
/**
370385
* @method getQuery
371386
* @summary returns the raw query which can be used for futher calls(.and/.or).

0 commit comments

Comments
 (0)