Skip to content

Commit 263c7ee

Browse files
algolia-botmillotpFluf22
committed
feat(clients): add helper to check if an index exists (generated)
algolia/api-clients-automation#3646 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Pierre Millot <[email protected]> Co-authored-by: Thomas Raffray <[email protected]>
1 parent f5df266 commit 263c7ee

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

packages/client-search/src/searchClient.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
22

3-
import { createAuth, createTransporter, getAlgoliaAgent, shuffle, createIterablePromise } from '@algolia/client-common';
3+
import {
4+
createAuth,
5+
createTransporter,
6+
getAlgoliaAgent,
7+
shuffle,
8+
ApiError,
9+
createIterablePromise,
10+
} from '@algolia/client-common';
411
import type {
512
CreateClientOptions,
613
Headers,
714
Host,
815
QueryParameters,
916
Request,
1017
RequestOptions,
11-
ApiError,
1218
IterableOptions,
1319
} from '@algolia/client-common';
1420

@@ -643,6 +649,19 @@ export function createSearchClient({
643649
return { copyOperationResponse, batchResponses, moveOperationResponse };
644650
},
645651

652+
async indexExists({ indexName }: GetSettingsProps): Promise<boolean> {
653+
try {
654+
await this.getSettings({ indexName });
655+
} catch (error) {
656+
if (error instanceof ApiError && error.status === 404) {
657+
return false;
658+
}
659+
throw error;
660+
}
661+
662+
return true;
663+
},
664+
646665
/**
647666
* Helper: calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets.
648667
* Disclaimer: We don't assert that the parameters you pass to this method only contains `hits` requests to prevent impacting search performances, this helper is purely for typing purposes.
@@ -1045,7 +1064,6 @@ export function createSearchClient({
10451064
const requestPath = '/1/indexes/{indexName}/rules/clear'.replace('{indexName}', encodeURIComponent(indexName));
10461065
const headers: Headers = {};
10471066
const queryParameters: QueryParameters = {};
1048-
10491067
if (forwardToReplicas !== undefined) {
10501068
queryParameters.forwardToReplicas = forwardToReplicas.toString();
10511069
}
@@ -1082,7 +1100,6 @@ export function createSearchClient({
10821100
const requestPath = '/1/indexes/{indexName}/synonyms/clear'.replace('{indexName}', encodeURIComponent(indexName));
10831101
const headers: Headers = {};
10841102
const queryParameters: QueryParameters = {};
1085-
10861103
if (forwardToReplicas !== undefined) {
10871104
queryParameters.forwardToReplicas = forwardToReplicas.toString();
10881105
}
@@ -1454,6 +1471,7 @@ export function createSearchClient({
14541471
.replace('{objectID}', encodeURIComponent(objectID));
14551472
const headers: Headers = {};
14561473
const queryParameters: QueryParameters = {};
1474+
14571475
if (forwardToReplicas !== undefined) {
14581476
queryParameters.forwardToReplicas = forwardToReplicas.toString();
14591477
}
@@ -1595,7 +1613,6 @@ export function createSearchClient({
15951613
if (length !== undefined) {
15961614
queryParameters.length = length.toString();
15971615
}
1598-
15991616
if (indexName !== undefined) {
16001617
queryParameters.indexName = indexName.toString();
16011618
}
@@ -1642,6 +1659,7 @@ export function createSearchClient({
16421659
.replace('{objectID}', encodeURIComponent(objectID));
16431660
const headers: Headers = {};
16441661
const queryParameters: QueryParameters = {};
1662+
16451663
if (attributesToRetrieve !== undefined) {
16461664
queryParameters.attributesToRetrieve = attributesToRetrieve.toString();
16471665
}
@@ -2381,10 +2399,10 @@ export function createSearchClient({
23812399
const requestPath = '/1/indexes/{indexName}/rules/batch'.replace('{indexName}', encodeURIComponent(indexName));
23822400
const headers: Headers = {};
23832401
const queryParameters: QueryParameters = {};
2402+
23842403
if (forwardToReplicas !== undefined) {
23852404
queryParameters.forwardToReplicas = forwardToReplicas.toString();
23862405
}
2387-
23882406
if (clearExistingRules !== undefined) {
23892407
queryParameters.clearExistingRules = clearExistingRules.toString();
23902408
}
@@ -2487,7 +2505,6 @@ export function createSearchClient({
24872505
if (forwardToReplicas !== undefined) {
24882506
queryParameters.forwardToReplicas = forwardToReplicas.toString();
24892507
}
2490-
24912508
if (replaceExistingSynonyms !== undefined) {
24922509
queryParameters.replaceExistingSynonyms = replaceExistingSynonyms.toString();
24932510
}

0 commit comments

Comments
 (0)