Skip to content

Added support for preview token and optimize the create method #367

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 3 commits into from
Jun 3, 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
4 changes: 4 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ fileignoreconfig:
- filename: test/sanity-check/api/stack-test.js
checksum: 198d5cf7ead33b079249dc3ecdee61a9c57453e93f1073ed0341400983e5aa53
version: "1.0"
fileignoreconfig:
- filename: test/sanity-check/api/previewToken-test.js
checksum: 9a42e079b7c71f76932896a0d2390d86ac626678ab20d36821dcf962820a886c
version: "1.0"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## [v1.21.5](https://github.com/contentstack/contentstack-management-javascript/tree/v1.21.5) (2025-06-09)
- Enhancement
- Preview token support added

## [v1.21.4](https://github.com/contentstack/contentstack-management-javascript/tree/v1.21.4) (2025-06-02)
- Enhancement
- Retry Logic modification on x-ratelimit-remaining Header
Expand Down
11 changes: 5 additions & 6 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,20 @@ export const upload = async ({ http, urlPath, stackHeaders, formData, params, me
}
}

export const create = ({ http, params = {}, createWithPreviewToken = false }) => {
export const create = ({ http, params }) => {
return async function (data, param) {
this.stackHeaders = {
...this.stackHeaders
}
const queryParams = {
...(createWithPreviewToken ? { create_with_preview_token: true } : {}),
...cloneDeep(param) // user param can override default
}

const headers = {
headers: {
...cloneDeep(params),
...cloneDeep(this.stackHeaders)
},
params: queryParams
params: {
...cloneDeep(param)
}
} || {}

try {
Expand Down
19 changes: 18 additions & 1 deletion lib/stack/deliveryToken/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cloneDeep from 'lodash/cloneDeep'
import { create, update, deleteEntity, fetch, query } from '../../entity'
import { PreviewToken } from './previewToken'

/**
* Delivery tokens provide read-only access to the associated environments. Read more about <a href='https://www.contentstack.com/docs/developers/create-tokens/about-delivery-tokens'>DeliveryToken</a>.
Expand Down Expand Up @@ -59,6 +60,22 @@ export function DeliveryToken (http, data = {}) {
*
*/
this.fetch = fetch(http, 'token')

/**
* @description The Create a PreviewToken call creates a new previewToken in a particular stack of your Contentstack account.
* @memberof DeliveryToken
* @func previewToken
* @returns {PreviewToken} Instance of PreviewToken.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const deliveryToken = client.stack({ api_key: 'api_key'}).deliveryToken('delivery_token_uid')
* const previewToken = deliveryToken.previewToken()
* console.log(previewToken)
*/
this.previewToken = () => {
return new PreviewToken(http, { stackHeaders: this.stackHeaders, token: { uid: this.uid } })
}
} else {
/**
* @description The Create a DeliveryToken call creates a new deliveryToken in a particular stack of your Contentstack account.
Expand All @@ -84,7 +101,7 @@ export function DeliveryToken (http, data = {}) {
* client.stack().deliveryToken().create({ token })
* .then((deliveryToken) => console.log(deliveryToken))
*/
this.create = create({ http: http, createWithPreviewToken: true })
this.create = create({ http: http })

/**
* @description The ‘Get all deliveryToken’ request returns comprehensive information about all deliveryToken created in a stack.
Expand Down
50 changes: 50 additions & 0 deletions lib/stack/deliveryToken/previewToken/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import cloneDeep from 'lodash/cloneDeep'
import { create, deleteEntity } from '../../../entity'

/**
* Preview tokens provide read-only access to the associated environments. Read more about <a href='https://www.contentstack.com/docs/developers/create-tokens/about-preview-tokens'>PreviewToken</a>.
* @namespace PreviewToken
*/
export function PreviewToken (http, data = {}) {
this.stackHeaders = data.stackHeaders
if (data.token) {
Object.assign(this, cloneDeep(data.token))
this.urlPath = `/stacks/delivery_tokens/${this.uid}/preview_token`

/**
* @description The Delete PreviewToken call is used to delete an existing PreviewToken permanently from your Stack.
* @memberof PreviewToken
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).deliveryToken('delivery_token_uid').previewToken().delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)

/**
* @description The Create a PreviewToken call creates a new previewToken in a particular stack of your Contentstack account.
* @memberof PreviewToken
* @func create
* @returns {Promise<PreviewToken.PreviewToken>} Promise for PreviewToken instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* client.stack().deliveryToken('delivery_token_uid').previewToken().create()
* .then((previewToken) => console.log(previewToken))
*/
this.create = create({ http: http })
}
}

export function PreviewTokenCollection (http, data) {
const obj = cloneDeep(data.tokens) || []
const previewTokenCollection = obj.map((userdata) => {
return new PreviewToken(http, { token: userdata, stackHeaders: data.stackHeaders })
})
return previewTokenCollection
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/management",
"version": "1.21.4",
"version": "1.21.5",
"description": "The Content Management API is used to manage the content of your Contentstack account",
"main": "./dist/node/contentstack-management.js",
"browser": "./dist/web/contentstack-management.js",
Expand Down
91 changes: 91 additions & 0 deletions test/sanity-check/api/previewToken-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { expect } from 'chai'
import { describe, it, setup } from 'mocha'
import { jsonReader } from '../utility/fileOperations/readwrite'
import { createDeliveryToken3 } from '../mock/deliveryToken.js'
import { contentstackClient } from '../utility/ContentstackClient.js'
import dotenv from 'dotenv'

dotenv.config()
let client = {}

let tokenUID = ''
describe('Preview Token api Test', () => {
setup(() => {
const user = jsonReader('loggedinuser.json')
client = contentstackClient(user.authtoken)
})

it('should add a Delivery Token for development', (done) => {
makeDeliveryToken()
.create(createDeliveryToken3)
.then((token) => {
tokenUID = token.uid
expect(token.name).to.be.equal(createDeliveryToken3.token.name)
expect(token.description).to.be.equal(
createDeliveryToken3.token.description
)
expect(token.scope[0].environments[0].name).to.be.equal(
createDeliveryToken3.token.scope[0].environments[0]
)
expect(token.scope[0].module).to.be.equal(
createDeliveryToken3.token.scope[0].module
)
expect(token.uid).to.be.not.equal(null)
expect(token.preview_token).to.be.not.equal(null)
done()
})
.catch(done)
})

it('should add a Preview Token', (done) => {
makePreviewToken(tokenUID)
.create()
.then((token) => {
expect(token.name).to.be.equal(createDeliveryToken3.token.name)
expect(token.description).to.be.equal(
createDeliveryToken3.token.description
)
expect(token.scope[0].environments[0].name).to.be.equal(
createDeliveryToken3.token.scope[0].environments[0]
)
expect(token.scope[0].module).to.be.equal(
createDeliveryToken3.token.scope[0].module
)
expect(token.uid).to.be.not.equal(null)
expect(token.preview_token).to.be.not.equal(null)
done()
})
.catch(done)
})

it('should delete a Preview Token from uid', (done) => {
makePreviewToken(tokenUID)
.delete()
.then((data) => {
expect(data.notice).to.be.equal('Preview token deleted successfully.')
done()
})
.catch(done)
})

it('should delete a Delivery Token from uid', (done) => {
makeDeliveryToken(tokenUID)
.delete()
.then((data) => {
expect(data.notice).to.be.equal('Delivery Token deleted successfully.')
done()
})
.catch(done)
})
})

function makePreviewToken (uid = null) {
return client
.stack({ api_key: process.env.API_KEY })
.deliveryToken(uid)
.previewToken()
}

function makeDeliveryToken (uid = null) {
return client.stack({ api_key: process.env.API_KEY }).deliveryToken(uid)
}
28 changes: 27 additions & 1 deletion test/sanity-check/mock/deliveryToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,31 @@ const createDeliveryToken2 = {
]
}
}
const createDeliveryToken3 = {
token: {
name: 'preview token test',
description: 'This is a demo token.',
scope: [
{
module: 'environment',
environments: [
'development'
],
acl: {
read: true
}
},
{
module: 'branch',
branches: [
'main'
],
acl: {
read: true
}
}
]
}
}

export { createDeliveryToken, createDeliveryToken2 }
export { createDeliveryToken, createDeliveryToken2, createDeliveryToken3 }
78 changes: 78 additions & 0 deletions types/stack/deliveryToken/previewToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { AnyProperty, SystemFields } from "../../utility/fields";
import { Creatable, SystemFunction } from "../../utility/operations";

// Main preview token interface
export interface PreviewToken
extends SystemFields,
Creatable<PreviewToken, PreviewToken>,
SystemFunction<PreviewToken> {
name: string;
description: string;
scope: Scope[];
uid: string;
created_by: string;
updated_by: string;
created_at: string;
updated_at: string;
token: string;
type: string;
preview_token: string;
}

// API response shape for creating a preview token
export interface PreviewTokenResponse {
notice: string;
token: PreviewTokenData;
}

// Data inside the response `token`
export interface PreviewTokenData extends AnyProperty {
name: string;
description: string;
scope: Scope[];
uid: string;
created_by: string;
updated_by: string;
created_at: string;
updated_at: string;
token: string;
type: string;
preview_token: string;
}

export interface Scope {
module: string;
environments?: Environment[];
branches?: string[];
locales?: string[];
acl: ACL;
_metadata?: {
uid: string;
};
}

export interface Environment extends AnyProperty {
name: string;
uid: string;
urls?: UrlLocale[];
_version?: number;
app_user_object_uid?: string;
created_by?: string;
updated_by?: string;
created_at?: string;
updated_at?: string;
ACL?: unknown[];
tags?: string[];
}

export interface UrlLocale {
url: string;
locale: string;
}

export interface ACL extends AnyProperty {
read?: boolean;
write?: boolean;
create?: boolean;
update?: boolean;
}
3 changes: 0 additions & 3 deletions types/stack/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ export interface Stack extends SystemFields {
globalField(options: object): GlobalFields;
globalField(uidOrOptions?: string | object, option?: object): GlobalFields | GlobalField;




asset(): Assets
asset(uid: string): Asset

Expand Down
Loading