Skip to content

Commit

Permalink
Merge pull request #138 from contentstack/staging
Browse files Browse the repository at this point in the history
DX | Release | 24-02-2025
  • Loading branch information
cs-raj authored Feb 25, 2025
2 parents 45e1342 + cf76749 commit 3d5c42e
Show file tree
Hide file tree
Showing 9 changed files with 1,318 additions and 490 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: '22.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish --tag latest --access public
Expand All @@ -22,10 +22,10 @@ jobs:
publish-git:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: '22.x'
registry-url: 'https://npm.pkg.github.com'
scope: '@contentstack'
- run: npm ci
Expand Down
1,682 changes: 1,212 additions & 470 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/delivery-sdk",
"version": "4.5.0",
"version": "4.5.1",
"type": "module",
"license": "MIT",
"main": "./dist/legacy/index.cjs",
Expand Down
5 changes: 3 additions & 2 deletions src/lib/contentstack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { httpClient, retryRequestHandler, retryResponseErrorHandler, retryResponseHandler } from '@contentstack/core';
import { InternalAxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from 'axios';
import { AxiosRequestHeaders } from 'axios';
import { handleRequest } from './cache';
import { Stack as StackClass } from './stack';
import { Policy, StackConfig } from './types';
Expand Down Expand Up @@ -37,7 +37,8 @@ export function stack(config: StackConfig): StackClass {
defaultHostname: 'cdn.contentstack.io',
headers: {} as AxiosRequestHeaders,
params: {} as any,
live_preview: {} as any
live_preview: {} as any,
port: config.port as number,
};

defaultConfig.defaultHostname = config.host || Utility.getHost(config.region, config.host);
Expand Down
19 changes: 18 additions & 1 deletion src/lib/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export class Entry {
private _urlPath: string;
protected _variants: string;
_queryParams: { [key: string]: string | number | string[] } = {};

constructor(client: AxiosInstance, contentTypeUid: string, entryUid: string) {
this._client = client;
this._contentTypeUid = contentTypeUid;
Expand Down Expand Up @@ -195,4 +194,22 @@ export class Entry {

return response;
}

/**
* @method addParams
* @memberof Entry
* @description Adds a query parameter to the query.
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = stack.contentType("contentTypeUid").entry().addParams({"key": "value"}).fetch()
*
* @returns {Entry}
*/
addParams(paramObj: { [key: string]: string | number | string[] }): Entry {
this._queryParams = { ...this._queryParams, ...paramObj };

return this;
}
}
59 changes: 50 additions & 9 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StackConfig, SyncStack, SyncType, LivePreviewQuery } from './types';
import { AxiosInstance } from '@contentstack/core';
import { AxiosInstance, getData } from '@contentstack/core';
import { Asset } from './asset';
import { AssetQuery } from './asset-query';
import { ContentType } from './content-type';
Expand Down Expand Up @@ -78,8 +78,8 @@ export class Stack {
* const taxonomy = stack.taxonomy() // For taxonomy query object
*/
taxonomy(): TaxonomyQuery {
return new TaxonomyQuery(this._client)
};
return new TaxonomyQuery(this._client);
}

/**
* @method GlobalField
Expand Down Expand Up @@ -170,20 +170,61 @@ export class Stack {
this._client.stackConfig.live_preview = livePreviewParams;
}

if (query.hasOwnProperty('release_id')) {
this._client.defaults.headers['release_id'] = query.release_id;
if (query.hasOwnProperty("release_id")) {
this._client.defaults.headers["release_id"] = query.release_id;
} else {
delete this._client.defaults.headers['release_id'];
delete this._client.defaults.headers["release_id"];
}

if (query.hasOwnProperty('preview_timestamp')) {
this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp;
if (query.hasOwnProperty("preview_timestamp")) {
this._client.defaults.headers["preview_timestamp"] =
query.preview_timestamp;
} else {
delete this._client.defaults.headers['preview_timestamp'];
delete this._client.defaults.headers["preview_timestamp"];
}
}

getClient(): any {
return this._client;
}

async getLastActivities() {
try {
const result = await getData(this._client, '/content_types', {
params: {
only_last_activity: true,
environment: this.config.environment,
},
});
return result;
} catch (error) {
throw new Error("Error fetching last activities");
}
}

/**
* @method setPort
* @memberOf Stack
* @description Sets the port of the host
* @param {Number} port - Port Number
* @return {Stack}
* @instance
* */
setPort(port: number) {
if (typeof port === "number") this.config.port = port;
return this;
}

/**
* @method setDebug
* @memberOf Stack
* @description Sets the debug option
* @param {Number} debug - Debug value
* @return {Stack}
* @instance
* */
setDebug(debug: boolean) {
if (typeof debug === "boolean") this.config.debug = debug;
return this;
}
}
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface StackConfig extends HttpClientParams {
logHandler?: (level: string, data: any) => void;
cacheOptions?: CacheOptions;
live_preview?: LivePreview;
port?: number;
debug?: boolean;
}
export interface CacheOptions extends PersistanceStoreOptions {
policy: Policy;
Expand Down
12 changes: 12 additions & 0 deletions test/api/stack.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { stackInstance } from '../utils/stack-instance';

const stack = stackInstance();

describe('Stack methods tests', () => {
it('should check last activities', async () => {
const result = await stack.getLastActivities();
expect(result).toBeDefined();
expect(result.content_types).toBeDefined();
expect(Array.isArray(result.content_types)).toBe(true);
});
});
15 changes: 14 additions & 1 deletion test/unit/stack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Stack } from '../../src/lib/stack';
import { Asset } from '../../src/lib/asset';
import { ContentType } from '../../src/lib/content-type';
import { HOST_URL, LOCALE } from '../utils/constant';
import { syncResult } from '../utils/mocks';
import { contentTypeQueryFindResponseDataMock, syncResult } from '../utils/mocks';
import { synchronization } from '../../src/lib/synchronization';
import { ContentTypeQuery } from '../../src/lib/contenttype-query';
import { AssetQuery } from '../../src/lib/asset-query';
Expand Down Expand Up @@ -144,5 +144,18 @@ describe('Stack class tests', () => {

expect(stack.getClient().defaults.headers['preview_timestamp']).toBeUndefined();
});

it('should return last activities', async () => {
mockClient.onGet('/content_types').reply(200, contentTypeQueryFindResponseDataMock);
const response = await stack.getLastActivities();
expect(response).toEqual(contentTypeQueryFindResponseDataMock);
expect(response.content_types).toBeDefined();
expect(Array.isArray(response.content_types)).toBe(true);
});

it('should set port to 3000', () => {
stack.setPort(3000);
expect(stack.config.port).toEqual(3000);
});
});

0 comments on commit 3d5c42e

Please sign in to comment.