Skip to content

Commit 3d5c42e

Browse files
authored
Merge pull request #138 from contentstack/staging
DX | Release | 24-02-2025
2 parents 45e1342 + cf76749 commit 3d5c42e

File tree

9 files changed

+1318
-490
lines changed

9 files changed

+1318
-490
lines changed

.github/workflows/npm-publish.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jobs:
1010
publish-npm:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
14-
- uses: actions/setup-node@v3
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
1515
with:
16-
node-version: '20.x'
16+
node-version: '22.x'
1717
registry-url: 'https://registry.npmjs.org'
1818
- run: npm ci
1919
- run: npm publish --tag latest --access public
@@ -22,10 +22,10 @@ jobs:
2222
publish-git:
2323
runs-on: ubuntu-latest
2424
steps:
25-
- uses: actions/checkout@v3
26-
- uses: actions/setup-node@v3
25+
- uses: actions/checkout@v4
26+
- uses: actions/setup-node@v4
2727
with:
28-
node-version: '20.x'
28+
node-version: '22.x'
2929
registry-url: 'https://npm.pkg.github.com'
3030
scope: '@contentstack'
3131
- run: npm ci

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.5.0",
3+
"version": "4.5.1",
44
"type": "module",
55
"license": "MIT",
66
"main": "./dist/legacy/index.cjs",

src/lib/contentstack.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { httpClient, retryRequestHandler, retryResponseErrorHandler, retryResponseHandler } from '@contentstack/core';
2-
import { InternalAxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from 'axios';
2+
import { AxiosRequestHeaders } from 'axios';
33
import { handleRequest } from './cache';
44
import { Stack as StackClass } from './stack';
55
import { Policy, StackConfig } from './types';
@@ -37,7 +37,8 @@ export function stack(config: StackConfig): StackClass {
3737
defaultHostname: 'cdn.contentstack.io',
3838
headers: {} as AxiosRequestHeaders,
3939
params: {} as any,
40-
live_preview: {} as any
40+
live_preview: {} as any,
41+
port: config.port as number,
4142
};
4243

4344
defaultConfig.defaultHostname = config.host || Utility.getHost(config.region, config.host);

src/lib/entry.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export class Entry {
1010
private _urlPath: string;
1111
protected _variants: string;
1212
_queryParams: { [key: string]: string | number | string[] } = {};
13-
1413
constructor(client: AxiosInstance, contentTypeUid: string, entryUid: string) {
1514
this._client = client;
1615
this._contentTypeUid = contentTypeUid;
@@ -195,4 +194,22 @@ export class Entry {
195194

196195
return response;
197196
}
197+
198+
/**
199+
* @method addParams
200+
* @memberof Entry
201+
* @description Adds a query parameter to the query.
202+
* @example
203+
* import contentstack from '@contentstack/delivery-sdk'
204+
*
205+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
206+
* const result = stack.contentType("contentTypeUid").entry().addParams({"key": "value"}).fetch()
207+
*
208+
* @returns {Entry}
209+
*/
210+
addParams(paramObj: { [key: string]: string | number | string[] }): Entry {
211+
this._queryParams = { ...this._queryParams, ...paramObj };
212+
213+
return this;
214+
}
198215
}

src/lib/stack.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StackConfig, SyncStack, SyncType, LivePreviewQuery } from './types';
2-
import { AxiosInstance } from '@contentstack/core';
2+
import { AxiosInstance, getData } from '@contentstack/core';
33
import { Asset } from './asset';
44
import { AssetQuery } from './asset-query';
55
import { ContentType } from './content-type';
@@ -78,8 +78,8 @@ export class Stack {
7878
* const taxonomy = stack.taxonomy() // For taxonomy query object
7979
*/
8080
taxonomy(): TaxonomyQuery {
81-
return new TaxonomyQuery(this._client)
82-
};
81+
return new TaxonomyQuery(this._client);
82+
}
8383

8484
/**
8585
* @method GlobalField
@@ -170,20 +170,61 @@ export class Stack {
170170
this._client.stackConfig.live_preview = livePreviewParams;
171171
}
172172

173-
if (query.hasOwnProperty('release_id')) {
174-
this._client.defaults.headers['release_id'] = query.release_id;
173+
if (query.hasOwnProperty("release_id")) {
174+
this._client.defaults.headers["release_id"] = query.release_id;
175175
} else {
176-
delete this._client.defaults.headers['release_id'];
176+
delete this._client.defaults.headers["release_id"];
177177
}
178178

179-
if (query.hasOwnProperty('preview_timestamp')) {
180-
this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp;
179+
if (query.hasOwnProperty("preview_timestamp")) {
180+
this._client.defaults.headers["preview_timestamp"] =
181+
query.preview_timestamp;
181182
} else {
182-
delete this._client.defaults.headers['preview_timestamp'];
183+
delete this._client.defaults.headers["preview_timestamp"];
183184
}
184185
}
185186

186187
getClient(): any {
187188
return this._client;
188189
}
190+
191+
async getLastActivities() {
192+
try {
193+
const result = await getData(this._client, '/content_types', {
194+
params: {
195+
only_last_activity: true,
196+
environment: this.config.environment,
197+
},
198+
});
199+
return result;
200+
} catch (error) {
201+
throw new Error("Error fetching last activities");
202+
}
203+
}
204+
205+
/**
206+
* @method setPort
207+
* @memberOf Stack
208+
* @description Sets the port of the host
209+
* @param {Number} port - Port Number
210+
* @return {Stack}
211+
* @instance
212+
* */
213+
setPort(port: number) {
214+
if (typeof port === "number") this.config.port = port;
215+
return this;
216+
}
217+
218+
/**
219+
* @method setDebug
220+
* @memberOf Stack
221+
* @description Sets the debug option
222+
* @param {Number} debug - Debug value
223+
* @return {Stack}
224+
* @instance
225+
* */
226+
setDebug(debug: boolean) {
227+
if (typeof debug === "boolean") this.config.debug = debug;
228+
return this;
229+
}
189230
}

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export interface StackConfig extends HttpClientParams {
2222
logHandler?: (level: string, data: any) => void;
2323
cacheOptions?: CacheOptions;
2424
live_preview?: LivePreview;
25+
port?: number;
26+
debug?: boolean;
2527
}
2628
export interface CacheOptions extends PersistanceStoreOptions {
2729
policy: Policy;

test/api/stack.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { stackInstance } from '../utils/stack-instance';
2+
3+
const stack = stackInstance();
4+
5+
describe('Stack methods tests', () => {
6+
it('should check last activities', async () => {
7+
const result = await stack.getLastActivities();
8+
expect(result).toBeDefined();
9+
expect(result.content_types).toBeDefined();
10+
expect(Array.isArray(result.content_types)).toBe(true);
11+
});
12+
});

test/unit/stack.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Stack } from '../../src/lib/stack';
55
import { Asset } from '../../src/lib/asset';
66
import { ContentType } from '../../src/lib/content-type';
77
import { HOST_URL, LOCALE } from '../utils/constant';
8-
import { syncResult } from '../utils/mocks';
8+
import { contentTypeQueryFindResponseDataMock, syncResult } from '../utils/mocks';
99
import { synchronization } from '../../src/lib/synchronization';
1010
import { ContentTypeQuery } from '../../src/lib/contenttype-query';
1111
import { AssetQuery } from '../../src/lib/asset-query';
@@ -144,5 +144,18 @@ describe('Stack class tests', () => {
144144

145145
expect(stack.getClient().defaults.headers['preview_timestamp']).toBeUndefined();
146146
});
147+
148+
it('should return last activities', async () => {
149+
mockClient.onGet('/content_types').reply(200, contentTypeQueryFindResponseDataMock);
150+
const response = await stack.getLastActivities();
151+
expect(response).toEqual(contentTypeQueryFindResponseDataMock);
152+
expect(response.content_types).toBeDefined();
153+
expect(Array.isArray(response.content_types)).toBe(true);
154+
});
155+
156+
it('should set port to 3000', () => {
157+
stack.setPort(3000);
158+
expect(stack.config.port).toEqual(3000);
159+
});
147160
});
148161

0 commit comments

Comments
 (0)