From 037ab5a380dc7a92c45997bad77b9ea45e18f837 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 29 Feb 2024 18:04:53 +0530 Subject: [PATCH 1/2] feat: containedIn function implementation --- src/lib/content-type.ts | 16 ++++++++++++++++ src/lib/query.ts | 20 +++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/lib/content-type.ts b/src/lib/content-type.ts index de2c3806..c2cbb2fe 100644 --- a/src/lib/content-type.ts +++ b/src/lib/content-type.ts @@ -1,6 +1,7 @@ import { AxiosInstance, getData } from '@contentstack/core'; import { Entry } from './entry'; import { Entries } from './entries'; +import { Query } from './query'; interface ContentTypeResponse { content_type: T; @@ -18,6 +19,21 @@ export class ContentType { this._urlPath = `/content_types/${this._contentTypeUid}`; } + /** + * @method Query + * @memberof ContentType + * @description queries get all entries that satisfy the condition of the following function + * @returns {Query} + * @example + * import contentstack from '@contentstack/delivery-sdk' + * + * const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" }); + * const entries = stack.contentType("contentTypeUid").Query().containedIn('fieldUid', ['value1','value2']) + */ + Query(): Query { + return new Query(this._client, this._contentTypeUid); + }; + /** * @method entry * @memberof ContentType diff --git a/src/lib/query.ts b/src/lib/query.ts index f13569c5..dd4d0eee 100644 --- a/src/lib/query.ts +++ b/src/lib/query.ts @@ -3,7 +3,7 @@ import { BaseQuery } from './base-query'; import { BaseQueryParameters, QueryOperation, QueryOperator, TaxonomyQueryOperation } from './types'; export class Query extends BaseQuery { private _contentTypeUid?: string; - + override _queryParams: { [key: string]: any} = {}; constructor(client: AxiosInstance, uid: string, queryObj?: { [key: string]: any }) { super(); this._client = client; @@ -164,4 +164,22 @@ export class Query extends BaseQuery { getQuery(): { [key: string]: any } { return this._parameters; } + + /** + * @method containedIn + * @memberof Query + * @description Returns the raw (JSON) query based on the filters applied on Query object. + * @example + * import contentstack from '@contentstack/delivery-sdk' + * + * const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" }); + * const query = stack.contentType("contentTypeUid").Query; + * const result = containedIn('fieldUid', ['value1', 'value2']).find() + * + * @returns {Query} + */ + containedIn(key: string, value: (string | number | boolean)[]): Query { + this._queryParams[key] = value; + return this; + } } From 847a015d2d899bbca842c4b6b4256c5c83fa485c Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 29 Feb 2024 18:05:25 +0530 Subject: [PATCH 2/2] test: :white_check_mark: test case for conatinedIn function --- test/api/contenttype.spec.ts | 13 ++++++++++++- test/unit/contenttype.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/test/api/contenttype.spec.ts b/test/api/contenttype.spec.ts index 540c687b..0ecac96a 100644 --- a/test/api/contenttype.spec.ts +++ b/test/api/contenttype.spec.ts @@ -1,6 +1,6 @@ /* eslint-disable no-console */ /* eslint-disable promise/always-return */ -import { BaseContentType, BaseEntry } from 'src'; +import { BaseContentType, BaseEntry, FindResponse } from 'src'; import { ContentType } from '../../src/lib/content-type'; import { stackInstance } from '../utils/stack-instance'; import { TContentType, TEntry } from './types'; @@ -26,6 +26,17 @@ describe('ContentType API test cases', () => { expect(result.schema).toBeDefined(); }); }); +describe('ContentType Query API test cases', () => { + it('should test for contained In', async () => { + const query = await makeContentType('contenttype_uid').Query().containedIn('title', ['value']).find() + if (query.entries) { + expect(query.entries[0]._version).toBeDefined(); + expect(query.entries[0].title).toBeDefined(); + expect(query.entries[0].uid).toBeDefined(); + expect(query.entries[0].created_at).toBeDefined(); + } + }); +}); function makeContentType(uid = ''): ContentType { const contentType = stack.ContentType(uid); diff --git a/test/unit/contenttype.spec.ts b/test/unit/contenttype.spec.ts index d4544b15..a97dfb20 100644 --- a/test/unit/contenttype.spec.ts +++ b/test/unit/contenttype.spec.ts @@ -5,6 +5,7 @@ import { Entry } from '../../src/lib/entry'; import { contentTypeResponseMock } from '../utils/mocks'; import { Entries } from '../../src/lib/entries'; import { MOCK_CLIENT_OPTIONS } from '../utils/constant'; +import { Query } from 'src/lib/query'; describe('ContentType class', () => { let contentType: ContentType; @@ -41,3 +42,22 @@ describe('ContentType class', () => { expect(response).toEqual(contentTypeResponseMock.content_type); }); }); + +describe('ContentType Query class', () => { + let contentType: ContentType; + let client: AxiosInstance; + let mockClient: MockAdapter; + + beforeAll(() => { + client = httpClient(MOCK_CLIENT_OPTIONS); + mockClient = new MockAdapter(client as any); + }); + + beforeEach(() => { + contentType = new ContentType(client, 'contentTypeUid'); + }); + it('should test for contained In', () => { + const query = contentType.Query().containedIn('fieldUID', ['value']); + expect(query._queryParams).toStrictEqual({'fieldUID': ['value']}); + }); +}); \ No newline at end of file