Skip to content

Commit fd5ace8

Browse files
Merge pull request #146 from contentstack/feat/cs-43910-containedIn-query-implementation
Feat/cs 43910 containedIn query implementation
2 parents e09c48a + f8e332f commit fd5ace8

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

src/lib/content-type.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AxiosInstance, getData } from '@contentstack/core';
22
import { Entry } from './entry';
33
import { Entries } from './entries';
4+
import { Query } from './query';
45

56
interface ContentTypeResponse<T> {
67
content_type: T;
@@ -18,6 +19,21 @@ export class ContentType {
1819
this._urlPath = `/content_types/${this._contentTypeUid}`;
1920
}
2021

22+
/**
23+
* @method Query
24+
* @memberof ContentType
25+
* @description queries get all entries that satisfy the condition of the following function
26+
* @returns {Query}
27+
* @example
28+
* import contentstack from '@contentstack/delivery-sdk'
29+
*
30+
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
31+
* const entries = stack.contentType("contentTypeUid").Query().containedIn('fieldUid', ['value1','value2'])
32+
*/
33+
Query(): Query {
34+
return new Query(this._client, this._contentTypeUid);
35+
};
36+
2137
/**
2238
* @method entry
2339
* @memberof ContentType

src/lib/query.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BaseQuery } from './base-query';
33
import { BaseQueryParameters, QueryOperation, QueryOperator, TaxonomyQueryOperation } from './types';
44
export class Query extends BaseQuery {
55
private _contentTypeUid?: string;
6-
6+
override _queryParams: { [key: string]: any} = {};
77
constructor(client: AxiosInstance, uid: string, queryObj?: { [key: string]: any }) {
88
super();
99
this._client = client;
@@ -164,4 +164,22 @@ export class Query extends BaseQuery {
164164
getQuery(): { [key: string]: any } {
165165
return this._parameters;
166166
}
167+
168+
/**
169+
* @method containedIn
170+
* @memberof Query
171+
* @description Returns the raw (JSON) query based on the filters applied on Query object.
172+
* @example
173+
* import contentstack from '@contentstack/delivery-sdk'
174+
*
175+
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
176+
* const query = stack.contentType("contentTypeUid").Query;
177+
* const result = containedIn('fieldUid', ['value1', 'value2']).find()
178+
*
179+
* @returns {Query}
180+
*/
181+
containedIn(key: string, value: (string | number | boolean)[]): Query {
182+
this._queryParams[key] = value;
183+
return this;
184+
}
167185
}

test/api/contenttype.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22
/* eslint-disable promise/always-return */
3-
import { BaseContentType, BaseEntry } from 'src';
3+
import { BaseContentType, BaseEntry, FindResponse } from 'src';
44
import { ContentType } from '../../src/lib/content-type';
55
import { stackInstance } from '../utils/stack-instance';
66
import { TContentType, TEntry } from './types';
@@ -26,6 +26,17 @@ describe('ContentType API test cases', () => {
2626
expect(result.schema).toBeDefined();
2727
});
2828
});
29+
describe('ContentType Query API test cases', () => {
30+
it('should test for contained In', async () => {
31+
const query = await makeContentType('contenttype_uid').Query().containedIn('title', ['value']).find<TEntry>()
32+
if (query.entries) {
33+
expect(query.entries[0]._version).toBeDefined();
34+
expect(query.entries[0].title).toBeDefined();
35+
expect(query.entries[0].uid).toBeDefined();
36+
expect(query.entries[0].created_at).toBeDefined();
37+
}
38+
});
39+
});
2940
function makeContentType(uid = ''): ContentType {
3041
const contentType = stack.ContentType(uid);
3142

test/unit/contenttype.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Entry } from '../../src/lib/entry';
55
import { contentTypeResponseMock } from '../utils/mocks';
66
import { Entries } from '../../src/lib/entries';
77
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';
8+
import { Query } from 'src/lib/query';
89

910
describe('ContentType class', () => {
1011
let contentType: ContentType;
@@ -41,3 +42,22 @@ describe('ContentType class', () => {
4142
expect(response).toEqual(contentTypeResponseMock.content_type);
4243
});
4344
});
45+
46+
describe('ContentType Query class', () => {
47+
let contentType: ContentType;
48+
let client: AxiosInstance;
49+
let mockClient: MockAdapter;
50+
51+
beforeAll(() => {
52+
client = httpClient(MOCK_CLIENT_OPTIONS);
53+
mockClient = new MockAdapter(client as any);
54+
});
55+
56+
beforeEach(() => {
57+
contentType = new ContentType(client, 'contentTypeUid');
58+
});
59+
it('should test for contained In', () => {
60+
const query = contentType.Query().containedIn('fieldUID', ['value']);
61+
expect(query._queryParams).toStrictEqual({'fieldUID': ['value']});
62+
});
63+
});

0 commit comments

Comments
 (0)