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

Next #43

Merged
merged 11 commits into from
Sep 5, 2024
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Change log

### Version: 4.2.0
#### Date: Septmber-04-2024
Feat: Variants support added

### Version: 4.1.0
#### Date: August-07-2024
Feat: Live Preview configuration changes
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/delivery-sdk",
"version": "4.1.0",
"version": "4.2.0",
"type": "commonjs",
"main": "./dist/cjs/src/index.js",
"types": "./dist/types/src/index.d.ts",
Expand All @@ -23,7 +23,7 @@
"@contentstack/core": "^1.1.0",
"@contentstack/utils": "^1.3.8",
"@types/humps": "^2.0.6",
"axios": "^1.7.2",
"axios": "^1.7.4",
"dotenv": "^16.3.1",
"humps": "^2.0.1"
},
Expand Down
19 changes: 17 additions & 2 deletions src/lib/contentstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InternalAxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from '
import { handleRequest } from './cache';
import { Stack as StackClass } from './stack';
import { Policy, StackConfig } from './types';
import { getHost } from './utils';
import * as Utility from './utils';
export * as Utils from '@contentstack/utils';

let version = '{{VERSION}}';
Expand Down Expand Up @@ -40,7 +40,7 @@ export function stack(config: StackConfig): StackClass {
live_preview: {} as any
};

defaultConfig.defaultHostname = config.host || getHost(config.region, config.host);
defaultConfig.defaultHostname = config.host || Utility.getHost(config.region, config.host);
config.host = defaultConfig.defaultHostname;

if (config.apiKey) {
Expand All @@ -59,6 +59,21 @@ export function stack(config: StackConfig): StackClass {
throw new Error('Environment for Stack is required');
}

if (config.live_preview) {
if (Utility.isBrowser()) {
const params = new URL(document.location.toString()).searchParams;
if (params.has('live_preview')) {
config.live_preview.live_preview = params.get('live_preview') || config.live_preview.live_preview;
}
if (params.has('release_id')) {
defaultConfig.headers['release_id'] = params.get('release_id');
}
if (params.has('preview_timestamp')) {
defaultConfig.headers['preview_timestamp'] = params.get('preview_timestamp');
}
}
}

if (config.branch) {
defaultConfig.headers.branch = config.branch;
}
Expand Down
21 changes: 21 additions & 0 deletions src/lib/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,25 @@ export class Entries extends EntryQueryable {

return new Query(this._client, this._parameters, this._queryParams, this._contentTypeUid);
}

/**
* @method variants
* @memberof Entry
* @description The variant header will be added to axios client
* @returns {Entry}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType('abc').entry().variant('xyz').find();
*/
variants(variants: string | string[]): Entries {
if (Array.isArray(variants) && variants.length > 0) {
this._client.defaults.headers['x-cs-variant-uid'] = variants.join(',');
} else if (typeof variants == 'string' && variants.length > 0) {
this._client.defaults.headers['x-cs-variant-uid'] = variants;
}

return this;
}
}
23 changes: 22 additions & 1 deletion src/lib/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface EntryResponse<T> {
entry: T;
}
export class Entry {
private _client: AxiosInstance;
protected _client: AxiosInstance;
private _contentTypeUid: string;
private _entryUid: string;
private _urlPath: string;
Expand Down Expand Up @@ -34,6 +34,27 @@ export class Entry {
return this;
}

/**
* @method variants
* @memberof Entry
* @description The variant header will be added to axios client
* @returns {Entry}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType('abc').entry('entry_uid').variant('xyz').fetch();
*/
variants(variants: string | string[]): Entry {
if (Array.isArray(variants) && variants.length > 0) {
this._client.defaults.headers['x-cs-variant-uid'] = variants.join(',');
} else if (typeof variants == 'string' && variants.length > 0) {
this._client.defaults.headers['x-cs-variant-uid'] = variants;
}

return this;
}

/**
* @method includeMetadata
* @memberof Entry
Expand Down
7 changes: 7 additions & 0 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,12 @@ export class Stack {
}
this._client.stackConfig.live_preview = livePreviewParams;
}

if (query.hasOwnProperty('release_id')) {
this._client.defaults.headers['release_id'] = query.release_id;
}
if (query.hasOwnProperty('preview_timestamp')) {
this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp;
}
}
}
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ export interface LivePreviewQuery {
live_preview: string
contentTypeUid: string
entryUid?: any;
preview_timestamp?: string
release_id?: string
}

export type LivePreview = {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export function getHost(region: Region = Region.US, host?: string) {

return url;
}

export function isBrowser() {
return (typeof window !== "undefined");
}
28 changes: 28 additions & 0 deletions test/unit/entries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,31 @@ describe('Entries class', () => {
expect(query._parameters).toEqual({"taxonomies.taxonomy_uid": {"$above": "term_uid", "levels": 4 }});
});
});

class TestVariants extends Entries {

constructor(client: AxiosInstance) {
super(client, 'xyz');
this._client = client;
}

getVariantsHeaders(): string {
return this._client.defaults.headers['x-cs-variant-uid'] || "";
}
}

describe('Variants test', () => {
let client: AxiosInstance;
let mockClient: MockAdapter;

beforeAll(() => {
client = httpClient(MOCK_CLIENT_OPTIONS);
mockClient = new MockAdapter(client as any);
});
it('should get the correct variant headers added to client', async () => {
const testVariantObj = new TestVariants(httpClient(MOCK_CLIENT_OPTIONS))

testVariantObj.variants(['variant1', 'variant2']);
expect(testVariantObj.getVariantsHeaders()).toBe('variant1,variant2');
});
})
27 changes: 27 additions & 0 deletions test/unit/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,30 @@ describe('Entry class', () => {
expect(returnedValue).toEqual(entryFetchMock.entry);
});
});

class TestVariants extends Entry {
constructor(client: AxiosInstance) {
super(client, 'xyz', 'abc');
this._client = client;
}

getVariantsHeaders(): string {
return this._client.defaults.headers['x-cs-variant-uid'] || "";
}
}

describe('Variants test', () => {
let client: AxiosInstance;
let mockClient: MockAdapter;

beforeAll(() => {
client = httpClient(MOCK_CLIENT_OPTIONS);
mockClient = new MockAdapter(client as any);
});
it('should get the correct variant headers added to client', async () => {
const testVariantObj = new TestVariants(httpClient(MOCK_CLIENT_OPTIONS))

testVariantObj.variants(['variant1', 'variant2']);
expect(testVariantObj.getVariantsHeaders()).toBe('variant1,variant2');
});
})
Loading