Skip to content

Commit

Permalink
Merge pull request #37 from contentstack/next
Browse files Browse the repository at this point in the history
DX | 07-08-2024
  • Loading branch information
nadeem-cs authored Aug 8, 2024
2 parents 747dd34 + 5f3cc35 commit fd63ce1
Show file tree
Hide file tree
Showing 14 changed files with 983 additions and 1,175 deletions.
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fileignoreconfig:
- filename: test/utils/mocks.ts
checksum: a1cb4b1890a584f1facd30f2a0974c97a66f91417022be79d00516338e244227
- filename: package-lock.json
checksum: 6c9beae17fc270641d855026896e49a1458a6b3a026df30265c51420d364d18a
checksum: 6e6c9c6f379ec75ce259a49f405c49a6b24dfbfd68406d6d49642042db2bf188
- filename: test/typescript/taxonomy.test.ts
checksum: e4bdf633e147fd60d929d379f20c814eed5f68b11421d7b53ec8826e9142de37
- filename: src/core/modules/taxonomy.js
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## Change log

### Version: 4.0.5
### Version: 4.1.0
#### Date: August-07-2024
Feat: Live Preview configuration changes
Feat: fetch asset by any field-uid other that asset-uid

### Version: 4.0.6
#### Date: July-30-2024
Fix: Live preview endpoint issue

Expand Down
1,883 changes: 813 additions & 1,070 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/delivery-sdk",
"version": "4.0.6",
"version": "4.1.0",
"type": "commonjs",
"main": "./dist/cjs/src/index.js",
"types": "./dist/types/src/index.d.ts",
Expand All @@ -20,7 +20,7 @@
"build:types": "node tools/cleanup types && tsc -p config/tsconfig.types.json"
},
"dependencies": {
"@contentstack/core": "^1.0.3",
"@contentstack/core": "^1.1.0",
"@contentstack/utils": "^1.3.8",
"@types/humps": "^2.0.6",
"axios": "^1.7.2",
Expand Down
15 changes: 15 additions & 0 deletions src/lib/asset-query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseQuery } from './base-query';
import { AxiosInstance } from '@contentstack/core';
import { Query } from './query';

export class AssetQuery extends BaseQuery {
constructor(client: AxiosInstance) {
Expand Down Expand Up @@ -125,4 +126,18 @@ export class AssetQuery extends BaseQuery {

return this;
}
/**
* @method query
* @memberof Query
* @description Fetches the asset data on the basis of the query
* @returns {Query}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.asset().query().where('fieldUid', queryOperators, 'value').find();
*/
query() {
return new Query(this._client, this._parameters, this._queryParams);
}
}
15 changes: 3 additions & 12 deletions src/lib/contentstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,8 @@ export function stack(config: StackConfig): StackClass {
live_preview: {} as any
};

if (config.live_preview?.enable === true) {
if (config.live_preview?.management_token != null && config.live_preview?.preview_token == null) {
config.host = config.live_preview.host
} else if (config.live_preview?.preview_token != null && config.live_preview?.management_token == null) {
config.host = config.live_preview.host
}
} else {
defaultConfig.defaultHostname = config.host ? config.host : getHost(config.region, config.host);
config.host = config.host || defaultConfig.defaultHostname;
defaultConfig.live_preview = config.live_preview
}
defaultConfig.defaultHostname = config.host || getHost(config.region, config.host);
config.host = defaultConfig.defaultHostname;

if (config.apiKey) {
defaultConfig.headers.api_key = config.apiKey;
Expand Down Expand Up @@ -78,7 +69,7 @@ export function stack(config: StackConfig): StackClass {

defaultConfig.headers['X-User-Agent'] = 'contentstack-delivery-typescript-{{PLATFORM}}/' + version;

// return new Stack(httpClient(defaultConfig), config);

const client = httpClient(defaultConfig as any);

if (config.logHandler) client.defaults.logHandler = config.logHandler;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export class Query extends BaseQuery {
this._parameters = params || {};
this._queryParams = queryParams || {};

if (!uid) {
this._urlPath = `/assets`;
}
if (queryObj) {
this._parameters = { ...this._parameters, ...queryObj };
}
Expand Down
11 changes: 8 additions & 3 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class Stack {
constructor(client: AxiosInstance, config: StackConfig) {
this._client = client;
this.config = config;
this._client.stackConfig = this.config;
}

/**
Expand Down Expand Up @@ -144,9 +145,13 @@ export class Stack {

livePreviewQuery(query: LivePreviewQuery) {
if (this.config.live_preview) {
this.config.live_preview.live_preview = query.live_preview || 'init';
this.config.live_preview.contentTypeUid = query.contentTypeUid;
this.config.live_preview.entryUid = query.entryUid
const livePreviewParams: any = {
...this.config.live_preview,
live_preview: query.live_preview || 'init',
contentTypeUid: query.contentTypeUid,
entryUid: query.entryUid
}
this._client.stackConfig.live_preview = livePreviewParams;
}
}
}
189 changes: 110 additions & 79 deletions test/api/asset-query.spec.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,129 @@
/* eslint-disable no-console */
/* eslint-disable promise/always-return */
import { AssetQuery } from '../../src/lib/asset-query';
import { stackInstance } from '../utils/stack-instance';
import { TAssets } from './types';
import { QueryOperation } from "../../src/lib/types";
import { AssetQuery } from "../../src/lib/asset-query";
import { stackInstance } from "../utils/stack-instance";
import { TAsset } from "./types";

const stack = stackInstance();

describe('AssetQuery API tests', () => {
it('should check for assets is defined', async () => {
const result = await makeAssetQuery().find<TAssets>();
expect(result.assets).toBeDefined();
expect(result.assets[0]._version).toEqual(1);
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
describe("AssetQuery API tests", () => {
it("should check for assets is defined", async () => {
const result = await makeAssetQuery().find<TAsset>();
if (result.assets) {
expect(result.assets).toBeDefined();
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for include dimensions', async () => {
const result = await makeAssetQuery().includeDimension().find<TAssets>();
expect(result.assets[0].dimension).toBeDefined();
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
it("should check for include dimensions", async () => {
const result = await makeAssetQuery().includeDimension().find<TAsset>();
if (result.assets) {
expect(result.assets[0].dimension).toBeDefined();
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for include fallback', async () => {
const result = await makeAssetQuery().includeFallback().find<TAssets>();
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
it("should check for include fallback", async () => {
const result = await makeAssetQuery().includeFallback().find<TAsset>();
if (result.assets) {
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for relative urls', async () => {
const result = await makeAssetQuery().relativeUrls().find<TAssets>();
expect(result.assets[0].url).not.toEqual(undefined);
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
it("should check for relative urls", async () => {
const result = await makeAssetQuery().relativeUrls().find<TAsset>();
if (result.assets) {
expect(result.assets[0].url).not.toEqual(undefined);
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for include branch', async () => {
const result = await makeAssetQuery().includeBranch().find<TAssets>();
expect(result.assets[0]._branch).not.toEqual(undefined);
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
it("should check for include branch", async () => {
const result = await makeAssetQuery().includeBranch().find<TAsset>();
if (result.assets) {
expect(result.assets[0]._branch).not.toEqual(undefined);
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for include metadata', async () => {
const result = await makeAssetQuery().includeMetadata().find<TAssets>();
expect(result.assets[0]._metadata).not.toEqual(undefined);
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
it("should check for include metadata", async () => {
const result = await makeAssetQuery().includeMetadata().find<TAsset>();
if (result.assets) {
expect(result.assets[0]._metadata).not.toEqual(undefined);
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for version', async () => {
const result = await makeAssetQuery().version(1).find<TAssets>();
expect(result.assets[0]._version).toEqual(1);
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
it("should check for version", async () => {
const result = await makeAssetQuery().version(1).find<TAsset>();
if (result.assets) {
expect(result.assets[0]._version).toEqual(1);
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for locale', async () => {
const result = await makeAssetQuery().locale('en-us').find<TAssets>();
expect(result.assets[0]._version).toEqual(1);
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
it("should check for locale", async () => {
const result = await makeAssetQuery().locale("en-us").find<TAsset>();
if (result.assets) {
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for limit', async () => {
it("should check for limit", async () => {
const query = makeAssetQuery();
const result = await query.limit(2).find<TAssets>();
expect(query._queryParams).toEqual({limit: 2});
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
const result = await query.limit(2).find<TAsset>();
if (result.assets) {
expect(query._queryParams).toEqual({ limit: 2 });
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it('should check for skip', async () => {
it("should check for skip", async () => {
const query = makeAssetQuery();
const result = await query.skip(2).find<TAssets>();
expect(query._queryParams).toEqual({skip: 2});
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
const result = await query.skip(2).find<TAsset>();
if (result.assets) {
expect(query._queryParams).toEqual({ skip: 2 });
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
it("should check assets for which title matches", async () => {
const result = await makeAssetQuery().query().where("title", QueryOperation.EQUALS, "AlbertEinstein.jpeg").find<TAsset>();
if (result.assets) {
expect(result.assets[0]._version).toBeDefined();
expect(result.assets[0].uid).toBeDefined();
expect(result.assets[0].content_type).toBeDefined();
expect(result.assets[0].created_by).toBeDefined();
expect(result.assets[0].updated_by).toBeDefined();
}
});
});
function makeAssetQuery(): AssetQuery {
Expand Down
8 changes: 5 additions & 3 deletions test/api/contenttype-query.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// @ts-nocheck
import { ContentTypeQuery } from '../../src/lib/contenttype-query';
import { stackInstance } from '../utils/stack-instance';
import { TContentTypes } from './types';
import { TContentType, TContentTypes } from './types';

const stack = stackInstance();
describe('ContentTypeQuery API test cases', () => {
it('should check for content_types are defined', async () => {
const result = await makeContentTypeQuery().find<TContentTypes>();
it.only('should check for content_types are defined', async () => {
const result = await makeContentTypeQuery().find<TContentType>();

expect(result.content_types).toBeDefined();
expect(result.content_types[0]._version).toBeDefined();
expect(result.content_types[0].uid).toBeDefined();
Expand Down
4 changes: 2 additions & 2 deletions test/api/live-preview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Live preview tests', () => {
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('api.contentstack.io');
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is disabled and management token is provided', () => {
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('Live preview tests', () => {
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('rest-preview.contentstack.com');
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is disabled and preview token is provided', () => {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/asset-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { httpClient, AxiosInstance } from '@contentstack/core';
import MockAdapter from 'axios-mock-adapter';
import { assetQueryFindResponseDataMock } from '../utils/mocks';
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';
import { QueryOperation } from '../../src/lib/types';

describe('AssetQuery class', () => {
let assetQuery: AssetQuery;
Expand Down Expand Up @@ -58,4 +59,11 @@ describe('AssetQuery class', () => {
const response = await assetQuery.find();
expect(response).toEqual(assetQueryFindResponseDataMock);
});

it('should add "query" in queryParameter when query method is called', async () => {
const returnedValue = await assetQuery.query().where('fieldUid', QueryOperation.EQUALS, 'value')
if (returnedValue) {
expect(returnedValue._parameters).toEqual({ fieldUid: 'value' });
}
});
});
Loading

0 comments on commit fd63ce1

Please sign in to comment.