Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/lib/content-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AxiosInstance, getData } from '@contentstack/core';
import { Entry } from './entry';
import { Entries } from './entries';
import { Query } from './query';

interface ContentTypeResponse<T> {
content_type: T;
Expand All @@ -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
Expand Down
20 changes: 19 additions & 1 deletion src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
13 changes: 12 additions & 1 deletion test/api/contenttype.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<TEntry>()
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);

Expand Down
20 changes: 20 additions & 0 deletions test/unit/contenttype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']});
});
});