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

feat: ✨ adds global field classes #125

Merged
merged 1 commit into from
Jan 18, 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
26 changes: 26 additions & 0 deletions src/lib/global-field-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BaseQuery } from './base-query';
import { AxiosInstance } from '@contentstack/core';

export class GlobalFieldQuery extends BaseQuery {

constructor(client: AxiosInstance) {
super();
this._client = client;
this._urlPath = '/global_fields';
}
/**
* @method includeBranch
* @memberof GlobalFieldQuery
* @description Includes the _branch top-level key in the response
* @returns {GlobalFieldQuery}
* @example
* const stack = contentstack.Stack('apiKey','deliveryToken','environment');
* const globalFields = stack.globalFields();
* const result = globalFields.includeBranch().find();
*/
includeBranch(): GlobalFieldQuery {
this._queryParams.include_branch = 'true';

return this;
}
}
42 changes: 42 additions & 0 deletions src/lib/global-field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { AxiosInstance, getData } from '@contentstack/core';

export class GlobalField {
private _client: AxiosInstance;
private _urlPath: string;
_queryParams: { [key: string]: string | number } = {};

constructor(clientConfig: AxiosInstance, globalFieldUid: string) {
this._client = clientConfig;
this._urlPath = `/global_fields/${globalFieldUid}`;
}
/**
* @method includeBranch
* @memberof GlobalField
* @description Includes the _branch top-level key in the response
* @returns {GlobalField}
* @example
* const stack = contentstack.Stack('apiKey','deliveryToken','environment');
* const globalField = stack.globalField('global_field_uid');
* const result = globalField.includeBranch().fetch();
*/
includeBranch(): GlobalField {
this._queryParams.include_branch = 'true';

return this;
}
/**
* @method find
* @memberof GlobalField
* @description Fetches comprehensive details of a specific global field
* @returns {GlobalField}
* @example
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const globalField = stack.globalField('global_field_uid');
* const result = globalField.fetch();
*/
async fetch<T>(): Promise<T> {
const response = await getData(this._client, this._urlPath, this._queryParams);

return response.global_field as T;
}
}
25 changes: 23 additions & 2 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ContentType } from './content-type';
import { ContentTypeQuery } from './contenttype-query';
import { synchronization } from './synchronization';
import {TaxonomyQuery} from './taxonomy-query';
import { GlobalFieldQuery } from './global-field-query';
import { GlobalField } from './global-field';

export class Stack {
readonly config: StackConfig;
Expand Down Expand Up @@ -67,17 +69,36 @@ export class Stack {
* @memberOf Stack
* @description Sets the url to /taxonomies/entries. Pass a query to fetch entries with taxonomies
*
* @returns {TaxonomyQuery}
* @example
* @returns {TaxonomyQuery} * @example
* import contentstack from '@contentstack/typescript'
*
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });

* const taxonomy = stack.Taxonomy() // For taxonomy query object
*/
Taxonomy(): TaxonomyQuery {
return new TaxonomyQuery(this._client)
};

/**
* @method GlobalField
* @memberOf Stack
* @param {String} uid - uid of the asset
* @description Retrieves all contentTypes of a stack by default. To retrieve a single asset, specify its UID.
*
* @returns {ContentType}
* const contentType = stack.contentType() // For collection of contentType
* // OR
* const contentType = stack.contentType('contentTypeUid') // For a single contentType with uid 'contentTypeUid'
*/
GlobalField(): GlobalFieldQuery;
GlobalField(uid: string): GlobalField;
GlobalField(uid?: string): GlobalField | GlobalFieldQuery {
if (uid) return new GlobalField(this._client, uid);

return new GlobalFieldQuery(this._client);
}

/**
* @method setLocale
* @memberOf Stack
Expand Down
32 changes: 32 additions & 0 deletions test/unit/global-field-query.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { GlobalFieldQuery } from '../../src/lib/global-field-query';
import { httpClient, AxiosInstance } from '@contentstack/core';
import MockAdapter from 'axios-mock-adapter';
import { gfieldQueryFindResponseDataMock } from '../utils/mocks';
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';

describe('GlobalFieldQuery class', () => {
let gfieldQuery: GlobalFieldQuery;
let client: AxiosInstance;
let mockClient: MockAdapter;

beforeAll(() => {
client = httpClient(MOCK_CLIENT_OPTIONS);
mockClient = new MockAdapter(client as any);
});

beforeEach(() => {
gfieldQuery = new GlobalFieldQuery(client);
});

it('should add "include_branch" in queryParameter when includeBranch method is called', () => {
const returnedValue = gfieldQuery.includeBranch();
expect(returnedValue).toBeInstanceOf(GlobalFieldQuery);
expect(gfieldQuery._queryParams.include_branch).toBe('true');
});

it('should return response data when successful', async () => {
mockClient.onGet('/global_fields').reply(200, gfieldQueryFindResponseDataMock);
const response = await gfieldQuery.find();
expect(response).toEqual(gfieldQueryFindResponseDataMock);
});
});
32 changes: 32 additions & 0 deletions test/unit/global-field.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { AxiosInstance, httpClient } from '@contentstack/core';
import { GlobalField } from 'src/lib/global-field';
import MockAdapter from 'axios-mock-adapter';
import { gfieldFetchDataMock } from '../utils/mocks';
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';

describe('GlobalField class', () => {
let gfield: GlobalField;
let client: AxiosInstance;
let mockClient: MockAdapter;

beforeAll(() => {
client = httpClient(MOCK_CLIENT_OPTIONS);
mockClient = new MockAdapter(client as any);
});

beforeEach(() => {
gfield = new GlobalField(client, 'gfieldUid');
});

it('should add "include_branch" in _queryParams when includeBranch method is called', () => {
const returnedValue = gfield.includeBranch();
expect(returnedValue).toBeInstanceOf(GlobalField);
expect(gfield._queryParams.include_branch).toBe('true');
});

it('should add "fetch" in _queryParams when fetch method is called', async () => {
mockClient.onGet(`/global_fields/gfieldUid`).reply(200, gfieldFetchDataMock);
const returnedValue = await gfield.fetch();
expect(returnedValue).toEqual(gfieldFetchDataMock.global_field);
});
});
91 changes: 90 additions & 1 deletion test/utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,93 @@ const entryFetchMock = {
}
}
}

const gfieldFetchDataMock = {
"global_field": {
"created_at": "2019-09-06T11:30:02.108Z",
"updated_at": "2019-09-06T11:30:02.108Z",
"title": "Servlet",
"uid": "servlet",
"_version": 1,
"inbuilt_class": false,
"schema": [
{
"display_name": "Name",
"uid": "name",
"data_type": "text",
"multiple": false,
"mandatory": false,
"unique": false,
"non_localizable": false
},
{
"data_type": "text",
"display_name": "Rich text editor",
"uid": "description",
"field_metadata": {
"allow_rich_text": true,
"description": "",
"multiline": false,
"rich_text_type": "advanced",
"options": [],
"version": 3
},
"multiple": false,
"mandatory": false,
"unique": false,
"non_localizable": false
}
],
"last_activity": {},
"maintain_revisions": true,
"description": ""
}
}

const gfieldQueryFindResponseDataMock = {
"global_fields": [
{
"created_at": "2019-09-06T11:30:02.108Z",
"updated_at": "2019-09-06T11:30:02.108Z",
"title": "Servlet",
"uid": "servlet",
"_version": 1,
"inbuilt_class": false,
"schema": [
{
"display_name": "Name",
"uid": "name",
"data_type": "text",
"multiple": false,
"mandatory": false,
"unique": false,
"non_localizable": false
},
{
"data_type": "text",
"display_name": "Rich text editor",
"uid": "description",
"field_metadata": {
"allow_rich_text": true,
"description": "",
"multiline": false,
"rich_text_type": "advanced",
"options": [],
"version": 3
},
"multiple": false,
"mandatory": false,
"unique": false,
"non_localizable": false
}
],
"last_activity": {},
"maintain_revisions": true,
"description": ""
}
]
}

const syncResult: any = { ...axiosGetMock.data };

export {
Expand All @@ -1599,5 +1686,7 @@ export {
contentTypeQueryFindResponseDataMock,
contentTypeResponseMock,
entryFindMock,
entryFetchMock
entryFetchMock,
gfieldFetchDataMock,
gfieldQueryFindResponseDataMock
};
Loading