Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ query function accepts queryObj #110

Merged
merged 2 commits into from
Jan 9, 2024
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
4 changes: 3 additions & 1 deletion src/lib/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ export class Entries extends EntryQueryable {
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType("contentTypeUid").entry().query();
*/
query() {
query(queryObj?: { [key: string]: any }) {
if (queryObj) return new Query(this._client, this._contentTypeUid, queryObj);

return new Query(this._client, this._contentTypeUid);
}
}
27 changes: 5 additions & 22 deletions src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { BaseQueryParameters, QueryOperation, QueryOperator } from './types';
export class Query extends BaseQuery {
private _contentTypeUid?: string;

constructor(client: AxiosInstance, uid: string) {
constructor(client: AxiosInstance, uid: string, queryObj?: { [key: string]: any }) {
super();
this._client = client;
this._contentTypeUid = uid;
this._urlPath = `/content_types/${this._contentTypeUid}/entries`;

if (queryObj) {
this._parameters = { ...this._parameters, ...queryObj };
}
}

/**
Expand Down Expand Up @@ -137,27 +141,6 @@ export class Query extends BaseQuery {
return this;
}

/**
* @method query
* @memberof Query
* @description Adds multiple query parameters to the query.
* @example
* import contentstack from '@contentstack/typescript'
*
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const query = stack.contentType("contentTypeUid").entry().query();
* const result = await query.query({'brand': {'$nin_query': {'title': 'Apple Inc.'}}}).find()
* // OR
* const asset = await stack.asset().query({'brand': {'$nin_query': {'title': 'Apple Inc.'}}}).find()
*
* @returns {Query}
*/
query(queryObj: { [key: string]: any }): Query {
this._parameters = { ...this._parameters, ...queryObj };

return this;
}

/**
* @method getQuery
* @memberof Query
Expand Down
5 changes: 4 additions & 1 deletion test/api/asset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import { Asset } from '../../src/lib/asset';
import { stackInstance } from '../utils/stack-instance';
import { TAsset } from './types';
import dotenv from 'dotenv';

dotenv.config();

const stack = stackInstance();
const assetUid = 'blt122ea52c5d4ddf19';
const assetUid = process.env.ASSET_UID;
describe('Asset API tests', () => {
it('should check for asset is defined', async () => {
const result = await makeAsset(assetUid).fetch<TAsset>();
Expand Down
5 changes: 4 additions & 1 deletion test/api/contenttype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import { ContentType } from '../../src/lib/content-type';
import { stackInstance } from '../utils/stack-instance';
import { TContentType, TEntry } from './types';
import dotenv from 'dotenv';

dotenv.config()

const stack = stackInstance();
describe('ContentType API test cases', () => {
it('should give Entry instance when entry method is called with entryUid', async () => {
const result = await makeContentType('header').Entry('blt657fe7db362fea22').fetch<TEntry>();
const result = await makeContentType('author').Entry(process.env.ENTRY_UID as string).fetch<TEntry>();
expect(result.entry).toBeDefined();
});
it('should check for content_types of the given contentTypeUid', async () => {
Expand Down
11 changes: 6 additions & 5 deletions test/api/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { stackInstance } from '../utils/stack-instance';
import { TEntry } from './types';

const stack = stackInstance();
const entryUid = process.env.ENTRY_UID;

describe('Entry API tests', () => {
it('should check for entry is defined', async () => {
const result = await makeEntry('blt09f7d2d46afe6dc6').fetch<TEntry>();
const result = await makeEntry(entryUid).fetch<TEntry>();
expect(result.entry).toBeDefined();
expect(result.entry._version).toBeDefined();
expect(result.entry.locale).toEqual('en-us');
Expand All @@ -21,20 +22,20 @@ describe('Entry API tests', () => {
bio: string;
age: string;
}
const result = await makeEntry('blt09f7d2d46afe6dc6').fetch<MyEntry>();
const result = await makeEntry(entryUid).fetch<MyEntry>();

console.log('🚀 ~ file: entry.spec.ts:25 ~ it ~ result:', result);
expect(result.entry).toBeDefined();
});
it('should check for include branch', async () => {
const result = await makeEntry('blt09f7d2d46afe6dc6').includeBranch().fetch<TEntry>();
const result = await makeEntry(entryUid).includeBranch().fetch<TEntry>();
expect(result.entry._branch).not.toEqual(undefined);
expect(result.entry.uid).toBeDefined();
expect(result.entry.created_by).toBeDefined();
expect(result.entry.updated_by).toBeDefined();
});
it('should check for locale', async () => {
const result = await makeEntry('blt09f7d2d46afe6dc6').locale('fr-fr').fetch<TEntry>();
const result = await makeEntry(entryUid).locale('fr-fr').fetch<TEntry>();
expect(result.entry).toBeDefined();
expect(result.entry._version).toBeDefined();
expect(result.entry.publish_details.locale).toEqual('fr-fr');
Expand All @@ -43,7 +44,7 @@ describe('Entry API tests', () => {
expect(result.entry.updated_by).toBeDefined();
});
it('should check for include fallback', async () => {
const result = await makeEntry('blt09f7d2d46afe6dc6').includeFallback().fetch<TEntry>();
const result = await makeEntry(entryUid).includeFallback().fetch<TEntry>();
expect(result.entry).toBeDefined();
expect(result.entry._version).toBeDefined();
expect(result.entry.locale).toEqual('en-us');
Expand Down
25 changes: 16 additions & 9 deletions test/api/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { stackInstance } from '../utils/stack-instance';
import { TEntries } from './types';

const stack = stackInstance();
const entryUid:string = process.env.ENTRY_UID || '';
const userUid:string = process.env.USER_UID || '';
describe('Query API tests', () => {
it('should add a where filter to the query parameters', async () => {
const query = await makeQuery('blog_post')
Expand All @@ -14,40 +16,45 @@ describe('Query API tests', () => {
const query = await makeQuery('blog_post').where('_version', QueryOperation.IS_LESS_THAN, 3).find<TEntries>();
expect(query.entries[0].title).toEqual('The future of business with AI');
});
it('should add a where filter to the query parameters when object is passed to query method', async () => {
const query = await makeQuery('blog_post', {'_version': { '$lt': 3 }}).find<TEntries>();
expect(query.entries[0].title).toEqual('The future of business with AI');
});
it('should add a where-in filter to the query parameters', async () => {
const query = await makeQuery('blog_post')
.whereIn('author', makeQuery('author').where('uid', QueryOperation.EQUALS, 'blt09f7d2d46afe6dc6'))
.whereIn('author', makeQuery('author').where('uid', QueryOperation.EQUALS, entryUid))
.find<TEntries>();
expect(query.entries[0].author[0].uid).toEqual('blt09f7d2d46afe6dc6');
expect(query.entries[0].author[0].uid).toEqual(entryUid);
expect(query.entries[0].title).toBeDefined();
expect(query.entries[0].url).toBeDefined();
expect(query.entries[0]._version).toBeDefined();
expect(query.entries[0].publish_details).toBeDefined();
});
it('should add a whereNotIn filter to the query parameters', async () => {
const query = await makeQuery('blog_post')
.whereNotIn('author', makeQuery('author').where('uid', QueryOperation.EQUALS, 'blt09f7d2d46afe6dc6'))
.whereNotIn('author', makeQuery('author').where('uid', QueryOperation.EQUALS, entryUid))
.find<TEntries>();
expect(query.entries[0].author[0]).not.toEqual('blt09f7d2d46afe6dc6');
expect(query.entries[0].author[0]).not.toEqual(entryUid);
expect(query.entries[0].title).toBeDefined();
expect(query.entries[0].url).toBeDefined();
expect(query.entries[0]._version).toBeDefined();
expect(query.entries[0].publish_details).toBeDefined();
});
it('should add a query operator to the query parameters', async () => {
const query1 = makeQuery('blog_post').where('locale', QueryOperation.EQUALS, 'en-us');
const query2 = makeQuery('blog_post').where('created_by', QueryOperation.EQUALS, 'blt79e6de1c5230a991');
const query2 = makeQuery('blog_post').where('created_by', QueryOperation.EQUALS, userUid);
const query = await makeQuery('blog_post').queryOperator(QueryOperator.AND, query1, query2).find<TEntries>();
expect(query.entries[0].locale).toEqual('en-us');
expect(query.entries[0].created_by).toEqual('blt79e6de1c5230a991');
expect(query.entries[0].created_by).toEqual(userUid);
expect(query.entries[0].title).not.toEqual(undefined);
expect(query.entries[0].url).not.toEqual(undefined);
expect(query.entries[0]._version).not.toEqual(undefined);
expect(query.entries[0].publish_details).not.toEqual(undefined);
});
});
function makeQuery(ctUid: string) {
const query = stack.ContentType(ctUid).Entry().query();
function makeQuery(ctUid: string, queryObj?: { [key: string]: any }) {
const entryInstance = stack.ContentType(ctUid).Entry();

return query;
if (queryObj) return entryInstance.query(queryObj);
return entryInstance.query();
}
2 changes: 1 addition & 1 deletion test/unit/image-transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('ImageTransform class', () => {
});

it('should return valid object when overlay method is called with valid params', () => {
const overlayImgURL = '/v3/assets/blteae40eb499811073/bltb21dacdd20d0e24c/59e0c401462a293417405f34/circle.png';
const overlayImgURL = '/v3/assets/circle.png';
expect(getBuild(new ImageTransform().overlay({ relativeURL: overlayImgURL }))).toEqual({ overlay: overlayImgURL });
expect(getBuild(new ImageTransform().overlay({ relativeURL: overlayImgURL, align: OverlayAlign.BOTTOM }))).toEqual({
overlay: overlayImgURL,
Expand Down
11 changes: 5 additions & 6 deletions test/unit/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ describe('Query class', () => {
});

it('should set a parameter correctly', () => {
query.query({ key1: 'value1' });
expect(query._parameters).toEqual({ key1: 'value1' });

query.query({ key2: 'value2' });
expect(query._parameters).toEqual({ key1: 'value1', key2: 'value2' });
const _query = getQueryObject(client, 'contentTypeUid', { key1: 'value1' })
expect(_query._parameters).toEqual({ key1: 'value1' });
});

it('should add an equality parameter to _parameters when queryOperation is EQUALS', () => {
Expand Down Expand Up @@ -92,6 +89,8 @@ describe('Query class', () => {
});
});

function getQueryObject(client: AxiosInstance, uid: string) {
function getQueryObject(client: AxiosInstance, uid: string, queryObj?: { [key: string]: any }) {
if (queryObj) return new Query(client, uid, queryObj);

return new Query(client, uid);
}
Loading