Skip to content

Hotfix | 16-01-2025 | setLocale | Release #113

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 15 commits into from
Jan 17, 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
310 changes: 61 additions & 249 deletions package-lock.json

Large diffs are not rendered by default.

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.4.4",
"version": "4.4.5",
"type": "module",
"license": "MIT",
"main": "./dist/legacy/index.cjs",
Expand Down Expand Up @@ -33,7 +33,7 @@
"build:types": "node tools/cleanup types && tsc -p config/tsconfig.types.json"
},
"dependencies": {
"@contentstack/core": "^1.1.3",
"@contentstack/core": "^1.1.4",
"@contentstack/utils": "^1.3.15",
"@types/humps": "^2.0.6",
"axios": "^1.7.9",
Expand Down
12 changes: 6 additions & 6 deletions src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export async function handleRequest(
const apiResponse = await defaultAdapter(config);

if (apiResponse.data) {
cacheStore.setItem(apiKey, apiResponse.data, config.contentTypeUid, cacheOptions.maxAge);
cacheStore.setItem(apiKey, JSON.parse(apiResponse.data), config.contentTypeUid, cacheOptions.maxAge);

return resolve(apiResponse.data);
return resolve({data: JSON.parse(apiResponse.data)});
} else {
const cacheResponse = cacheStore.getItem(apiKey, config.contentTypeUid);
if (cacheResponse)
Expand Down Expand Up @@ -48,9 +48,9 @@ export async function handleRequest(
const apiResponse = await defaultAdapter(config);

if (apiResponse.data) {
cacheStore.setItem(apiKey, apiResponse.data, config.contentTypeUid, cacheOptions.maxAge);
cacheStore.setItem(apiKey, JSON.parse(apiResponse.data), config.contentTypeUid, cacheOptions.maxAge);

return resolve(apiResponse.data);
return resolve({data: JSON.parse(apiResponse.data)});
} else {
return reject(apiResponse);
}
Expand All @@ -70,9 +70,9 @@ export async function handleRequest(
const apiResponse = await defaultAdapter(config);

if (apiResponse.data) {
cacheStore.setItem(apiKey, apiResponse.data, config.contentTypeUid, cacheOptions.maxAge);
cacheStore.setItem(apiKey, JSON.parse(apiResponse.data), config.contentTypeUid, cacheOptions.maxAge);

return resolve(apiResponse.data);
return resolve({data: JSON.parse(apiResponse.data)});
} else {
return reject(apiResponse);
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/contentstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export function stack(config: StackConfig): StackClass {
throw new Error('Environment for Stack is required');
}

if (config.locale) {
defaultConfig.params.locale = config.locale;
}

if (config.live_preview) {
if (Utility.isBrowser()) {
const params = new URL(document.location.toString()).searchParams;
Expand Down
10 changes: 6 additions & 4 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class Stack {
*/
setLocale(locale: string) {
this.config.locale = locale;
this._client.defaults.params.locale = locale;
}

/**
Expand Down Expand Up @@ -158,10 +159,11 @@ export class Stack {
};
} else {
livePreviewParams = {
live_preview: null,
contentTypeUid: null,
entryUid: null,
preview_timestamp: null,
...livePreviewParams,
live_preview: "",
contentTypeUid: "",
entryUid: "",
preview_timestamp: "",
include_applied_variants: false,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/persistance/config/persistance-storage-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { StorageType } from '../types/storage-type';

export type PersistanceStoreConfig =
| (Store & PersistanceStoreOptions)
| ({ storageType?: StorageType } & PersistanceStoreOptions);
| ({ storeType?: StorageType } & PersistanceStoreOptions);
export interface Store {
storage: Storage;
storageType: 'customStorage';
storeType: 'customStorage';
}
export interface PersistanceStoreOptions {
maxAge?: number; // default 24 hrs
Expand Down
4 changes: 2 additions & 2 deletions src/persistance/persistance-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class PersistanceStore {

constructor(config?: PersistanceStoreConfig) {
let defaultConfig: PersistanceStoreConfig = {
storageType: 'localStorage',
storeType: 'localStorage',
maxAge: 1000 * 60 * 60 * 24,
serializer: JSON.stringify,
deserializer: JSON.parse,
Expand All @@ -20,7 +20,7 @@ export class PersistanceStore {
...defaultConfig,
...config,
};
this.setStore(defaultConfig.storageType, (defaultConfig as unknown as Store).storage);
this.setStore(defaultConfig.storeType, (defaultConfig as unknown as Store).storage);
this.config = defaultConfig;
this.name = ''; // TODO add stack api key to name
}
Expand Down
12 changes: 6 additions & 6 deletions test/unit/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ describe('Cache handleRequest function', () => {
describe('NETWORK_ELSE_CACHE policy', () => {
it('should return network response when proper response is received', async () => {
const cacheOptions = { policy: Policy.NETWORK_ELSE_CACHE, maxAge: 3600 };
const defaultAdapter = jest.fn((_config) => ({ data: 'foo' }));
const defaultAdapter = jest.fn((_config) => ({ data: JSON.stringify('foo') }));
const cacheStore = new PersistanceStore(cacheOptions);

await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);

expect(defaultAdapter).toHaveBeenCalledWith(config);
expect(resolve).toBeCalledWith('foo');
expect(resolve).toBeCalledWith({"data": "foo"});
expect(reject).not.toBeCalled();

cacheStore.removeItem(apiKey, config.contentTypeUid);
Expand Down Expand Up @@ -97,14 +97,14 @@ describe('Cache handleRequest function', () => {
});
it('should return api response when proper cache is not available', async () => {
const cacheOptions = { policy: Policy.CACHE_THEN_NETWORK, maxAge: 3600 };
const defaultAdapter = jest.fn((_config) => ({ data: 'foo' }));
const defaultAdapter = jest.fn((_config) => ({ data: JSON.stringify('foo') }));

const cacheStore = new PersistanceStore(cacheOptions);

await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);

expect(defaultAdapter).toHaveBeenCalled();
expect(resolve).toBeCalledWith('foo');
expect(resolve).toBeCalledWith({"data": "foo"});
expect(reject).not.toBeCalled();

cacheStore.removeItem(apiKey, config.contentTypeUid);
Expand Down Expand Up @@ -150,13 +150,13 @@ describe('Cache handleRequest function', () => {

it('should return network response data when cache is not available', async () => {
const cacheOptions = { policy: Policy.CACHE_ELSE_NETWORK, maxAge: 3600 };
const defaultAdapter = jest.fn((_config) => ({ data: 'foo' }));
const defaultAdapter = jest.fn((_config) => ({ data: JSON.stringify('foo') }));
const cacheStore = new PersistanceStore(cacheOptions);

await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);

expect(defaultAdapter).toHaveBeenCalledWith(config);
expect(resolve).toBeCalledWith('foo');
expect(resolve).toBeCalledWith({"data": "foo"});
expect(reject).not.toBeCalled();

cacheStore.removeItem(apiKey, config.contentTypeUid);
Expand Down
26 changes: 13 additions & 13 deletions test/unit/persistance/preference-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ describe('persistance store intiialization test', () => {
expect(persistance).toBeDefined();
expect(persistance.config).toBeDefined();
expect(persistance.config.maxAge).toEqual(86400000);
expect(persistance.config.storageType).toEqual('localStorage');
expect(persistance.config.storeType).toEqual('localStorage');
});

it('should initialize persistance with name and local storage type ', () => {
const storageType = 'localStorage';
const persistance = makePersistance({ storageType });
const storeType = 'localStorage';
const persistance = makePersistance({ storeType });
expect(persistance).toBeDefined();
expect(persistance.config).toBeDefined();
expect(persistance.config.maxAge).toEqual(86400000);
expect(persistance.config.storageType).toEqual(storageType);
expect(persistance.config.storeType).toEqual(storeType);
});
it('should initialize persistance with name and memory storage type ', () => {
const storageType = 'memoryStorage';
const persistance = makePersistance({ storageType });
const storeType = 'memoryStorage';
const persistance = makePersistance({ storeType });
expect(persistance).toBeDefined();
expect(persistance.config).toBeDefined();
expect(persistance.config.maxAge).toEqual(86400000);
expect(persistance.config.storageType).toEqual(storageType);
expect(persistance.config.storeType).toEqual(storeType);
});
it('should initialize persistance with name and local storage type ', () => {
const storageType = 'customStorage';
const persistance = makePersistance({ storageType });
const storeType = 'customStorage';
const persistance = makePersistance({ storeType });
expect(persistance).toBeDefined();
expect(persistance.config).toBeDefined();
expect(persistance.config.maxAge).toEqual(86400000);
expect(persistance.config.storageType).toEqual(storageType);
expect(persistance.config.storeType).toEqual(storeType);
});
it('should throw error on custom storage without storage', () => {
const config: any = { name: 'foobar', storageType: 'customStorage' };
const config: any = { name: 'foobar', storeType: 'customStorage' };
config.storage = '';
const persistance = () => {
new PersistanceStore(config);
Expand Down Expand Up @@ -162,6 +162,6 @@ describe('persistance with 0 maxAge', () => {
});
});

function makePersistance(config: { storageType: StorageType | 'customStorage' }) {
return new PersistanceStore({ storageType: config.storageType, storage: memoryStorage });
function makePersistance(config: { storeType: StorageType | 'customStorage' }) {
return new PersistanceStore({ storeType: config.storeType, storage: memoryStorage });
}
10 changes: 6 additions & 4 deletions test/unit/stack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Stack class tests', () => {
});

stack = new Stack(client, config() as StackConfig);
client.defaults.params = {};
});
it('should test import of class Stack', (done) => {
expect(stack).toBeInstanceOf(Stack);
Expand Down Expand Up @@ -95,10 +96,11 @@ describe('Stack class tests', () => {
stack.livePreviewQuery(query);

expect(stack.getClient().stackConfig.live_preview).toEqual({
live_preview: null,
contentTypeUid: null,
entryUid: null,
preview_timestamp: null,
live_preview: '',
contentTypeUid: '',
entryUid: '',
enable: false,
preview_timestamp: '',
include_applied_variants: false,
});
});
Expand Down
Loading