Skip to content

Commit 2bb4ecc

Browse files
authored
Merge pull request #62 from contentstack/fix/DX-1492
Fix: Variants headers to be added only on specific call
2 parents d34a184 + fd245e4 commit 2bb4ecc

File tree

11 files changed

+48
-21
lines changed

11 files changed

+48
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Version: 4.4.1
2+
#### Date: October-21-2024
3+
Fix: getData to receive params and headers both in data
4+
15
### Version: 4.4.0
26
#### Date: October-21-2024
37
Enh: Node version bump

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.4.0",
3+
"version": "4.4.1",
44
"type": "module",
55
"main": "./dist/cjs/src/index.js",
66
"types": "./dist/types/src/index.d.ts",

src/lib/asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class Asset {
141141
* const result = await stack.asset('asset_uid').fetch();
142142
*/
143143
async fetch<T>(): Promise<T> {
144-
const response = await getData(this._client, this._urlPath, this._queryParams);
144+
const response = await getData(this._client, this._urlPath, { params: this._queryParams});
145145

146146
if (response.asset) return response.asset as T;
147147

src/lib/base-query.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class BaseQuery extends Pagination {
88

99
protected _client!: AxiosInstance;
1010
protected _urlPath!: string;
11+
protected _variants!: string;
1112

1213
/**
1314
* @method includeCount
@@ -203,9 +204,21 @@ export class BaseQuery extends Pagination {
203204

204205
async find<T>(): Promise<FindResponse<T>> {
205206
let requestParams: { [key: string]: any } = this._queryParams;
206-
if (Object.keys(this._parameters)) requestParams = { ...this._queryParams, query: { ...this._parameters } };
207207

208-
const response = await getData(this._client, this._urlPath, requestParams);
208+
if (Object.keys(this._parameters).length > 0) {
209+
requestParams = { ...this._queryParams, query: { ...this._parameters } };
210+
}
211+
212+
const getRequestOptions: any = { params: requestParams };
213+
214+
if (this._variants) {
215+
getRequestOptions.headers = {
216+
...getRequestOptions.headers,
217+
'x-cs-variant-uid': this._variants
218+
};
219+
}
220+
221+
const response = await getData(this._client, this._urlPath, getRequestOptions);
209222

210223
return response as FindResponse<T>;
211224
}

src/lib/contenttype-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ContentTypeQuery {
4040
* const result = await contentTypeQuery.find();
4141
*/
4242
async find<T>(): Promise<FindResponse<T>> {
43-
const response = await getData(this._client, this._urlPath, this._queryParams);
43+
const response = await getData(this._client, this._urlPath, { params: this._queryParams });
4444

4545
return response as FindResponse<T>;
4646
}

src/lib/entries.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class Entries extends EntryQueryable {
1010
this._client = client;
1111
this._contentTypeUid = contentTypeUid;
1212
this._urlPath = `/content_types/${this._contentTypeUid}/entries`;
13+
this._variants = '';
1314
}
1415

1516
/**
@@ -206,11 +207,10 @@ export class Entries extends EntryQueryable {
206207
*/
207208
variants(variants: string | string[]): Entries {
208209
if (Array.isArray(variants) && variants.length > 0) {
209-
this._client.defaults.headers['x-cs-variant-uid'] = variants.join(',');
210+
this._variants = variants.join(',');
210211
} else if (typeof variants == 'string' && variants.length > 0) {
211-
this._client.defaults.headers['x-cs-variant-uid'] = variants;
212+
this._variants = variants;
212213
}
213-
214214
return this;
215215
}
216216
}

src/lib/entry.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ export class Entry {
88
private _contentTypeUid: string;
99
private _entryUid: string;
1010
private _urlPath: string;
11+
protected _variants: string;
1112
_queryParams: { [key: string]: string | number | string[] } = {};
1213

1314
constructor(client: AxiosInstance, contentTypeUid: string, entryUid: string) {
1415
this._client = client;
1516
this._contentTypeUid = contentTypeUid;
1617
this._entryUid = entryUid;
1718
this._urlPath = `/content_types/${this._contentTypeUid}/entries/${this._entryUid}`;
19+
this._variants = '';
1820
}
1921

2022
/**
@@ -47,9 +49,9 @@ export class Entry {
4749
*/
4850
variants(variants: string | string[]): Entry {
4951
if (Array.isArray(variants) && variants.length > 0) {
50-
this._client.defaults.headers['x-cs-variant-uid'] = variants.join(',');
52+
this._variants = variants.join(',');
5153
} else if (typeof variants == 'string' && variants.length > 0) {
52-
this._client.defaults.headers['x-cs-variant-uid'] = variants;
54+
this._variants = variants;
5355
}
5456

5557
return this;
@@ -152,7 +154,15 @@ export class Entry {
152154
* const result = await stack.contentType(contentType_uid).entry(entry_uid).fetch();
153155
*/
154156
async fetch<T>(): Promise<T> {
155-
const response = await getData(this._client, this._urlPath, this._queryParams);
157+
const getRequestOptions: any = { params: this._queryParams};
158+
if (this._variants) {
159+
getRequestOptions.headers = {
160+
...getRequestOptions.headers,
161+
'x-cs-variant-uid': this._variants
162+
};
163+
}
164+
165+
const response = await getData(this._client, this._urlPath, getRequestOptions);
156166

157167
if (response.entry) return response.entry as T;
158168

src/lib/global-field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class GlobalField {
3535
* const result = globalField.fetch();
3636
*/
3737
async fetch<T>(): Promise<T> {
38-
const response = await getData(this._client, this._urlPath, this._queryParams);
38+
const response = await getData(this._client, this._urlPath, { params: this._queryParams });
3939

4040
return response.global_field as T;
4141
}

src/lib/synchronization.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function synchronization(client: AxiosInstance, params: SyncStack |
1515
config.params = { ...config.params, type: type.join(',') };
1616
}
1717

18-
let response: AxiosResponse = await getData(client, '/sync', humps.decamelizeKeys(config));
18+
let response: AxiosResponse = await getData(client, '/sync', { params: humps.decamelizeKeys(config) });
1919
const data = response.data;
2020

2121
while (recursive && 'pagination_token' in response.data) {

test/unit/entries.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ class TestVariants extends Entries {
145145
this._client = client;
146146
}
147147

148-
getVariantsHeaders(): string {
149-
return this._client.defaults.headers['x-cs-variant-uid'] || "";
148+
setAndGetVariantsHeaders(): string {
149+
this.variants(['variant1', 'variant2']);
150+
return this._variants || "";
150151
}
151152
}
152153

@@ -161,7 +162,6 @@ describe('Variants test', () => {
161162
it('should get the correct variant headers added to client', async () => {
162163
const testVariantObj = new TestVariants(httpClient(MOCK_CLIENT_OPTIONS))
163164

164-
testVariantObj.variants(['variant1', 'variant2']);
165-
expect(testVariantObj.getVariantsHeaders()).toBe('variant1,variant2');
165+
expect(testVariantObj.setAndGetVariantsHeaders()).toBe('variant1,variant2');
166166
});
167167
})

0 commit comments

Comments
 (0)