Skip to content

Added support for fetching entry references #254

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

Merged
merged 2 commits into from
Jan 22, 2025
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [v1.19.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.1) (2025-01-27)
- Feature
- Added support for get entry references
- Added delay sanity testcases
- Axios, webpack, form-data, qs version bump

## [v1.19.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.0) (2025-01-13)
- Feature
- Release 2.0 feature
Expand Down
29 changes: 29 additions & 0 deletions lib/stack/contentType/entry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,35 @@ export function Entry (http, data) {
}
}

/**
* @description The get references request allows to get the references of an entry.
* @memberof Entry
* @func references
* @returns {Promise<Object>} Response Object.
* @example 1
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('uid').references()
* .then((response) => console.log(response));
* @example 2
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('uid').references({locale: 'en-us'})
* .then((response) => console.log(response));
*/
this.references = async (param = {}) => {
const headers = {}
if (this.stackHeaders) {
headers.headers = this.stackHeaders
}
try {
const response = await http.get(`${this.urlPath}/references`, {...headers, params: param,})
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}

} else {
/**
* @description The Create an entry call creates a new entry for the selected content type.
Expand Down
13 changes: 13 additions & 0 deletions test/sanity-check/api/entry-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ describe('Entry api Test', () => {
.catch(done)
})

it('should get references of the given Entry uid', done => {
makeEntry(singlepageCT.content_type.uid, entryUTD).references()
.then((reference) => {
reference.references.forEach((references) => {
expect(references.entry_uid).to.be.not.equal(null)
expect(references.content_type_uid).to.be.not.equal(null)
expect(references.content_type_title).to.be.not.equal(null)
})
done()
})
.catch(done)
})

it('should unpublish localized entry', done => {
makeEntry(singlepageCT.content_type.uid, entryUTD)
.unpublish({
Expand Down
25 changes: 25 additions & 0 deletions test/unit/entry-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,31 @@ describe('Contentstack Entry test', () => {
})
.catch(done)
})
it('should get references of the given Entry uid', done => {
var mock = new MockAdapter(Axios)
const references = [
{
entry_uid: "entry_uid",
content_type_uid: "referred_content_type",
locale: "en-us",
title: "Jeff Goins",
content_type_title: "Referred Content Type"
}
]
mock.onGet('/content_types/content_type_uid/entries/UID/references').reply(200, {
references
})

makeEntry({ entry: { ...systemUidMock }, stackHeaders: stackHeadersMock }).references()
.then((reference) => {
expect(reference.references[0].entry_uid).to.be.equal('entry_uid')
reference.references.forEach((references) => {
expect(references.entry_uid).to.be.not.equal(null)
})
done()
})
.catch(done)
})
})

function makeEntry (data) {
Expand Down
13 changes: 13 additions & 0 deletions types/stack/contentType/entry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Entry extends Publishable, Unpublishable, SystemFields, SystemF
variants(uid: string): Variant
setWorkflowStage(data: { workflow_stage: WorkflowStage, locale?:string}): Promise<Response>
locales(): Promise<Locales>
references(param: object): Promise<References>
}

export interface Entries extends Queryable<Entry, {entry: EntryData}> {
Expand Down Expand Up @@ -38,4 +39,16 @@ export interface Locales {
export interface Code extends AnyProperty {
code: string;
localized: boolean;
}

export interface References {
references: EntryReferences[];
}

export interface EntryReferences extends AnyProperty {
title: string;
entry_uid: string;
locale: string;
content_type_uid: string;
content_type_title: string;
}
Loading