Skip to content

Implemented variants support #45

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 5 commits into from
Sep 4, 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
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
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
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