diff --git a/src/lib/global-field-query.ts b/src/lib/global-field-query.ts new file mode 100644 index 00000000..2f8611d0 --- /dev/null +++ b/src/lib/global-field-query.ts @@ -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; + } +} \ No newline at end of file diff --git a/src/lib/global-field.ts b/src/lib/global-field.ts new file mode 100644 index 00000000..743b35ce --- /dev/null +++ b/src/lib/global-field.ts @@ -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(): Promise { + const response = await getData(this._client, this._urlPath, this._queryParams); + + return response.global_field as T; + } +} \ No newline at end of file diff --git a/src/lib/stack.ts b/src/lib/stack.ts index dbf7ba4e..b87f72ed 100644 --- a/src/lib/stack.ts +++ b/src/lib/stack.ts @@ -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; @@ -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 diff --git a/test/unit/global-field-query.spec.ts b/test/unit/global-field-query.spec.ts new file mode 100644 index 00000000..feca38f6 --- /dev/null +++ b/test/unit/global-field-query.spec.ts @@ -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); + }); +}); diff --git a/test/unit/global-field.spec.ts b/test/unit/global-field.spec.ts new file mode 100644 index 00000000..cc53b9bf --- /dev/null +++ b/test/unit/global-field.spec.ts @@ -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); + }); +}); diff --git a/test/utils/mocks.ts b/test/utils/mocks.ts index 784b0a58..537c3ed8 100644 --- a/test/utils/mocks.ts +++ b/test/utils/mocks.ts @@ -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 { @@ -1599,5 +1686,7 @@ export { contentTypeQueryFindResponseDataMock, contentTypeResponseMock, entryFindMock, - entryFetchMock + entryFetchMock, + gfieldFetchDataMock, + gfieldQueryFindResponseDataMock };