Skip to content

Commit bbdbf75

Browse files
algolia-botkai687shortcutsmillotp
committed
fix(specs): partial update operation (generated)
algolia/api-clients-automation#3486 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Kai Welke <[email protected]> Co-authored-by: shortcuts <[email protected]> Co-authored-by: Pierre Millot <[email protected]>
1 parent 3864afb commit bbdbf75

33 files changed

+159
-424
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build:many": "lerna run build --skip-nx-cache --include-dependencies --scope ${0:-'{@algolia/*,algoliasearch}'}",
1111
"clean": "lerna run clean --include-dependencies",
1212
"release:bump": "lerna version ${0:-patch} --no-changelog --no-git-tag-version --no-push --exact --force-publish --yes",
13-
"release:publish": "tsc --project tsconfig.script.json && node dist/scripts/publish.js",
13+
"release:publish": "tsc --project scripts/tsconfig.json && node scripts/dist/scripts/publish.js",
1414
"test": "lerna run test $*",
1515
"test:size": "bundlesize"
1616
},

packages/client-common/src/__tests__/cache/browser-local-storage-cache.test.ts

+14-31
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@ describe('browser local storage cache', () => {
2626
const cache = createBrowserLocalStorageCache({ key: version });
2727
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 1 });
2828

29-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
30-
{ bar: 1 }
31-
);
29+
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject({ bar: 1 });
3230
expect(missMock.mock.calls.length).toBe(1);
3331

3432
await cache.set({ key: 'foo' }, { foo: 2 });
3533

36-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
37-
{ foo: 2 }
38-
);
34+
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject({ foo: 2 });
3935
expect(missMock.mock.calls.length).toBe(1);
4036
});
4137

@@ -51,7 +47,7 @@ describe('browser local storage cache', () => {
5147
expect(
5248
await cache.get({ key: 'foo' }, defaultValue, {
5349
miss: () => Promise.resolve(missMock()),
54-
})
50+
}),
5551
).toMatchObject({ bar: 1 });
5652

5753
expect(missMock.mock.calls.length).toBe(0);
@@ -65,9 +61,7 @@ describe('browser local storage cache', () => {
6561

6662
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 2 });
6763

68-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
69-
{ bar: 2 }
70-
);
64+
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject({ bar: 2 });
7165
expect(missMock.mock.calls.length).toBe(1);
7266
});
7367

@@ -83,7 +77,7 @@ describe('browser local storage cache', () => {
8377
expect(
8478
await cache.get({ key: 'foo' }, defaultValue, {
8579
miss: () => Promise.resolve(missMock()),
86-
})
80+
}),
8781
).toMatchObject({ bar: 2 });
8882

8983
expect(missMock.mock.calls.length).toBe(1);
@@ -102,7 +96,7 @@ describe('browser local storage cache', () => {
10296
expect(
10397
await cache.get({ key: 'foo' }, defaultValue, {
10498
miss: () => Promise.resolve(missMock()),
105-
})
99+
}),
106100
).toMatchObject({ bar: 2 });
107101

108102
expect(missMock.mock.calls.length).toBe(1);
@@ -111,8 +105,7 @@ describe('browser local storage cache', () => {
111105
});
112106

113107
it('do throws localstorage exceptions on access', async () => {
114-
const message =
115-
"Failed to read the 'localStorage' property from 'Window': Access is denied for this document.";
108+
const message = "Failed to read the 'localStorage' property from 'Window': Access is denied for this document.";
116109
const cache = createBrowserLocalStorageCache(
117110
new Proxy(
118111
{ key: 'foo' },
@@ -125,20 +118,16 @@ describe('browser local storage cache', () => {
125118
// Simulates a window.localStorage access.
126119
throw new DOMException(message);
127120
},
128-
}
129-
)
121+
},
122+
),
130123
);
131124
const key = { foo: 'bar' };
132125
const value = 'foo';
133126
const fallback = 'bar';
134127

135128
await expect(cache.delete(key)).rejects.toEqual(new DOMException(message));
136-
await expect(cache.set(key, value)).rejects.toEqual(
137-
new DOMException(message)
138-
);
139-
await expect(
140-
cache.get(key, () => Promise.resolve(fallback))
141-
).rejects.toEqual(new DOMException(message));
129+
await expect(cache.set(key, value)).rejects.toEqual(new DOMException(message));
130+
await expect(cache.get(key, () => Promise.resolve(fallback))).rejects.toEqual(new DOMException(message));
142131
});
143132

144133
it('do throws localstorage exceptions after access', async () => {
@@ -153,9 +142,7 @@ describe('browser local storage cache', () => {
153142

154143
await expect(cache.delete(key)).rejects.toEqual(new Error(message));
155144
await expect(cache.set(key, value)).rejects.toEqual(new Error(message));
156-
await expect(
157-
cache.get(key, () => Promise.resolve(fallback))
158-
).rejects.toEqual(new Error(message));
145+
await expect(cache.get(key, () => Promise.resolve(fallback))).rejects.toEqual(new Error(message));
159146
});
160147

161148
it('creates a namespace within local storage', async () => {
@@ -175,12 +162,8 @@ describe('browser local storage cache', () => {
175162
},
176163
});
177164

178-
const localStorageValue = localStorage.getItem(
179-
`algolia-client-js-${version}`
180-
);
165+
const localStorageValue = localStorage.getItem(`algolia-client-js-${version}`);
181166

182-
expect(JSON.parse(localStorageValue ? localStorageValue : '{}')).toEqual(
183-
expectedValue
184-
);
167+
expect(JSON.parse(localStorageValue ? localStorageValue : '{}')).toEqual(expectedValue);
185168
});
186169
});

packages/client-common/src/__tests__/cache/memory-cache.test.ts

+7-17
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ describe('memory cache', () => {
1616
const cache = createMemoryCache();
1717
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 1 });
1818

19-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
20-
{
21-
bar: 1,
22-
}
23-
);
19+
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject({
20+
bar: 1,
21+
});
2422

2523
await cache.set({ key: 'foo' }, { foo: 2 });
2624

2725
expect(missMock.mock.calls.length).toBe(1);
28-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
29-
{ foo: 2 }
30-
);
26+
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject({ foo: 2 });
3127
expect(missMock.mock.calls.length).toBe(1);
3228
});
3329

@@ -54,9 +50,7 @@ describe('memory cache', () => {
5450

5551
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 2 });
5652

57-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
58-
{ bar: 2 }
59-
);
53+
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject({ bar: 2 });
6054
expect(missMock.mock.calls.length).toBe(1);
6155
});
6256

@@ -68,9 +62,7 @@ describe('memory cache', () => {
6862

6963
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 2 });
7064

71-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
72-
{ bar: 2 }
73-
);
65+
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject({ bar: 2 });
7466
expect(missMock.mock.calls.length).toBe(1);
7567
});
7668

@@ -82,9 +74,7 @@ describe('memory cache', () => {
8274

8375
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 2 });
8476

85-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
86-
{ bar: 2 }
87-
);
77+
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject({ bar: 2 });
8878
expect(missMock.mock.calls.length).toBe(1);
8979
});
9080
});

packages/client-common/src/__tests__/cache/null-cache.test.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,19 @@ describe('null cache', () => {
1818

1919
await cache.set({ key: 'key' }, { foo: 10 });
2020

21-
expect(await cache.get({ key: 'key' }, defaultValue, events)).toMatchObject(
22-
{
23-
bar: 12,
24-
}
25-
);
21+
expect(await cache.get({ key: 'key' }, defaultValue, events)).toMatchObject({
22+
bar: 12,
23+
});
2624

2725
expect(missMock.mock.calls.length).toBe(1);
2826
});
2927

3028
it('returns default value', async () => {
3129
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 12 });
3230

33-
expect(await cache.get({ foo: 'foo' }, defaultValue, events)).toMatchObject(
34-
{
35-
bar: 12,
36-
}
37-
);
31+
expect(await cache.get({ foo: 'foo' }, defaultValue, events)).toMatchObject({
32+
bar: 12,
33+
});
3834

3935
expect(missMock.mock.calls.length).toBe(1);
4036
});

packages/client-common/src/__tests__/create-iterable-promise.test.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ describe('createIterablePromise', () => {
156156
validate: () => false,
157157
});
158158

159-
await expect(promise).rejects.toEqual(
160-
expect.objectContaining({ message: 'nope' })
161-
);
159+
await expect(promise).rejects.toEqual(expect.objectContaining({ message: 'nope' }));
162160
});
163161

164162
it('gets the rejection of the given promise via throw', async () => {
@@ -178,9 +176,7 @@ describe('createIterablePromise', () => {
178176
validate: () => false,
179177
});
180178

181-
await expect(promise).rejects.toEqual(
182-
expect.objectContaining({ message: 'nope' })
183-
);
179+
await expect(promise).rejects.toEqual(expect.objectContaining({ message: 'nope' }));
184180
});
185181

186182
it('rejects with the given `message` when `validate` hits', async () => {
@@ -204,7 +200,7 @@ describe('createIterablePromise', () => {
204200
await expect(promise).rejects.toEqual(
205201
expect.objectContaining({
206202
message: 'Error is thrown: 3/3',
207-
})
203+
}),
208204
);
209205
expect(calls).toBe(MAX_RETRIES);
210206
});
@@ -230,7 +226,7 @@ describe('createIterablePromise', () => {
230226
await expect(promise).rejects.toEqual(
231227
expect.objectContaining({
232228
message: 'Error is thrown: 3/3',
233-
})
229+
}),
234230
);
235231
expect(calls).toBe(MAX_RETRIES);
236232
});

packages/client-common/src/cache/createBrowserLocalStorageCache.ts

+14-31
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import type {
2-
BrowserLocalStorageCacheItem,
3-
BrowserLocalStorageOptions,
4-
Cache,
5-
CacheEvents,
6-
} from '../types';
7-
8-
export function createBrowserLocalStorageCache(
9-
options: BrowserLocalStorageOptions
10-
): Cache {
1+
import type { BrowserLocalStorageCacheItem, BrowserLocalStorageOptions, Cache, CacheEvents } from '../types';
2+
3+
export function createBrowserLocalStorageCache(options: BrowserLocalStorageOptions): Cache {
114
let storage: Storage;
125
// We've changed the namespace to avoid conflicts with v4, as this version is a huge breaking change
136
const namespaceKey = `algolia-client-js-${options.key}`;
@@ -35,7 +28,7 @@ export function createBrowserLocalStorageCache(
3528
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(
3629
Object.entries(namespace).filter(([, cacheItem]) => {
3730
return cacheItem.timestamp !== undefined;
38-
})
31+
}),
3932
);
4033

4134
setNamespace(filteredNamespaceWithoutOldFormattedCacheItems);
@@ -45,14 +38,12 @@ export function createBrowserLocalStorageCache(
4538
}
4639

4740
const filteredNamespaceWithoutExpiredItems = Object.fromEntries(
48-
Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(
49-
([, cacheItem]) => {
50-
const currentTimestamp = new Date().getTime();
51-
const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp;
52-
53-
return !isExpired;
54-
}
55-
)
41+
Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(([, cacheItem]) => {
42+
const currentTimestamp = new Date().getTime();
43+
const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp;
44+
45+
return !isExpired;
46+
}),
5647
);
5748

5849
setNamespace(filteredNamespaceWithoutExpiredItems);
@@ -64,32 +55,24 @@ export function createBrowserLocalStorageCache(
6455
defaultValue: () => Promise<TValue>,
6556
events: CacheEvents<TValue> = {
6657
miss: () => Promise.resolve(),
67-
}
58+
},
6859
): Promise<TValue> {
6960
return Promise.resolve()
7061
.then(() => {
7162
removeOutdatedCacheItems();
7263

73-
return getNamespace<Promise<BrowserLocalStorageCacheItem>>()[
74-
JSON.stringify(key)
75-
];
64+
return getNamespace<Promise<BrowserLocalStorageCacheItem>>()[JSON.stringify(key)];
7665
})
7766
.then((value) => {
78-
return Promise.all([
79-
value ? value.value : defaultValue(),
80-
value !== undefined,
81-
]);
67+
return Promise.all([value ? value.value : defaultValue(), value !== undefined]);
8268
})
8369
.then(([value, exists]) => {
8470
return Promise.all([value, exists || events.miss(value)]);
8571
})
8672
.then(([value]) => value);
8773
},
8874

89-
set<TValue>(
90-
key: Record<string, any> | string,
91-
value: TValue
92-
): Promise<TValue> {
75+
set<TValue>(key: Record<string, any> | string, value: TValue): Promise<TValue> {
9376
return Promise.resolve().then(() => {
9477
const namespace = getNamespace();
9578

packages/client-common/src/cache/createFallbackableCache.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import type { FallbackableCacheOptions, Cache, CacheEvents } from '../types';
22

33
import { createNullCache } from './createNullCache';
44

5-
export function createFallbackableCache(
6-
options: FallbackableCacheOptions
7-
): Cache {
5+
export function createFallbackableCache(options: FallbackableCacheOptions): Cache {
86
const caches = [...options.caches];
97
const current = caches.shift();
108

@@ -18,21 +16,14 @@ export function createFallbackableCache(
1816
defaultValue: () => Promise<TValue>,
1917
events: CacheEvents<TValue> = {
2018
miss: (): Promise<void> => Promise.resolve(),
21-
}
19+
},
2220
): Promise<TValue> {
2321
return current.get(key, defaultValue, events).catch(() => {
24-
return createFallbackableCache({ caches }).get(
25-
key,
26-
defaultValue,
27-
events
28-
);
22+
return createFallbackableCache({ caches }).get(key, defaultValue, events);
2923
});
3024
},
3125

32-
set<TValue>(
33-
key: Record<string, any> | string,
34-
value: TValue
35-
): Promise<TValue> {
26+
set<TValue>(key: Record<string, any> | string, value: TValue): Promise<TValue> {
3627
return current.set(key, value).catch(() => {
3728
return createFallbackableCache({ caches }).set(key, value);
3829
});

0 commit comments

Comments
 (0)