Skip to content

Commit 5207d68

Browse files
authored
chore(TypeScript): avoid ts-ignore (#1190)
* chore(TypeScript): avoid ts-ignore - addMethods with `as` - updateApiKey with type guard - browseRules & browseSynonyms with `as any` (the key exists, but isn't supposed to be shown to the user, thus isn't in the type * any & use returned type directly for some reason if you do let x = something, you can't change the type anymore in a reassignment, except if you return directly, since that makes a new type * fix typo
1 parent f82bd48 commit 5207d68

File tree

6 files changed

+38
-33
lines changed

6 files changed

+38
-33
lines changed

packages/client-common/src/__tests__/TestSuite.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class TestSuite {
4141
appIdEnv: string = 'ALGOLIA_APPLICATION_ID_1',
4242
apiKeyEnv: string = 'ALGOLIA_ADMIN_KEY_1'
4343
) {
44-
let client = this.algoliasearch(`${process.env[appIdEnv]}`, `${process.env[apiKeyEnv]}`);
44+
const client = this.algoliasearch(`${process.env[appIdEnv]}`, `${process.env[apiKeyEnv]}`);
4545

4646
// To ensure `Consistency` during the Common Test Suite, we
4747
// force the transporter to work with a single host in the
@@ -59,8 +59,7 @@ export class TestSuite {
5959
};
6060

6161
if (testing.isBrowserLite()) {
62-
// @ts-ignore
63-
client = addMethods(client, {
62+
return addMethods(client, {
6463
multipleBatch,
6564
multipleGetObjects,
6665
});
@@ -77,11 +76,10 @@ export class TestSuite {
7776
}
7877

7978
public makeIndex(indexName?: string) {
80-
let index = this.makeSearchClient().initIndex(indexName || this.makeIndexName());
79+
const index = this.makeSearchClient().initIndex(indexName || this.makeIndexName());
8180

8281
if (testing.isBrowserLite()) {
83-
// @ts-ignore
84-
index = addMethods(index, {
82+
return addMethods(index, {
8583
saveObjects,
8684
setSettings,
8785
delete: deleteIndex,

packages/client-common/src/helpers.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,30 @@ export function shuffle<TData>(array: TData[]): TData[] {
1313
return array;
1414
}
1515

16-
export function addMethods<
17-
TBase,
18-
TMethods extends {
19-
readonly [key: string]: (base: TBase) => (...args: any) => any;
20-
}
21-
>(
16+
type Methods<TBase> = {
17+
// eslint-disable-next-line functional/prefer-readonly-type
18+
readonly [key: string]: (base: TBase) => (...args: any[]) => any;
19+
};
20+
21+
type AddedMethods<TBase, TMethods extends Methods<TBase>> = TBase &
22+
{
23+
[TKey in keyof TMethods extends string ? keyof TMethods : never]: ReturnType<TMethods[TKey]>;
24+
};
25+
26+
export function addMethods<TBase extends {}, TMethods extends Methods<TBase>>(
2227
base: TBase,
2328
methods?: TMethods
24-
): TBase &
25-
{
26-
// eslint-disable-next-line @typescript-eslint/generic-type-naming
27-
[key in keyof TMethods extends string ? keyof TMethods : never]: ReturnType<TMethods[key]>;
28-
} {
29-
Object.keys(methods !== undefined ? methods : {}).forEach(key => {
30-
// @ts-ignore
29+
): AddedMethods<TBase, TMethods> {
30+
if (!methods) {
31+
return base as AddedMethods<TBase, TMethods>;
32+
}
33+
34+
Object.keys(methods).forEach(key => {
3135
// eslint-disable-next-line functional/immutable-data, no-param-reassign
32-
base[key] = methods[key](base);
36+
(base as any)[key] = methods[key](base);
3337
});
3438

35-
// @ts-ignore
36-
return base;
39+
return base as AddedMethods<TBase, TMethods>;
3740
}
3841

3942
export function encode(format: string, ...args: readonly any[]): string {

packages/client-search/src/methods/client/updateApiKey.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,16 @@ export const updateApiKey = (base: SearchClient) => {
3535
'description',
3636
'maxQueriesPerIPPerHour',
3737
'maxHitsPerQuery',
38-
];
38+
] as const;
3939

4040
const hasChanged = (getApiKeyResponse: GetApiKeyResponse): boolean => {
4141
return Object.keys(updatedFields)
42-
.filter(updatedField => apiKeyFields.indexOf(updatedField) !== -1)
42+
.filter(
43+
(updatedField: any): updatedField is typeof apiKeyFields[number] =>
44+
apiKeyFields.indexOf(updatedField) !== -1
45+
)
4346
.every(updatedField => {
44-
return (
45-
// @ts-ignore
46-
47-
getApiKeyResponse[updatedField] === updatedFields[updatedField]
48-
);
47+
return getApiKeyResponse[updatedField] === updatedFields[updatedField];
4948
});
5049
};
5150

packages/client-search/src/methods/index/browseRules.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ export const browseRules = (base: SearchIndex) => {
2828
return {
2929
...response,
3030
hits: response.hits.map(rule => {
31-
// @ts-ignore
3231
// eslint-disable-next-line functional/immutable-data,no-param-reassign
33-
delete rule._highlightResult;
32+
delete (rule as any)._highlightResult;
3433

3534
return rule;
3635
}),

packages/client-search/src/methods/index/browseSynonyms.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ export const browseSynonyms = (base: SearchIndex) => {
2828
return {
2929
...response,
3030
hits: response.hits.map(synonym => {
31-
// @ts-ignore
3231
// eslint-disable-next-line functional/immutable-data,no-param-reassign
33-
delete synonym._highlightResult;
32+
delete (synonym as any)._highlightResult;
3433

3534
return synonym;
3635
}),

packages/client-search/src/types/GetApiKeyResponse.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ export type GetApiKeyResponse = {
4242
*/
4343
referers?: string[];
4444

45+
/**
46+
* IPv4 network allowed to use the generated key.
47+
* This is used for more protection against API key leaking and reuse.
48+
* Note that you can only provide a single source, but you can specify a range of IPs (e.g., 192.168.1.0/24).
49+
*/
50+
restrictSources?: string;
51+
4552
/**
4653
* Specify the list of query parameters. You can force the query parameters for a query using the url string format.
4754
*/

0 commit comments

Comments
 (0)