Skip to content

Commit

Permalink
feat(clients): add helper to check if an index exists (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3646

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 4, 2024
1 parent f5df266 commit 263c7ee
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions packages/client-search/src/searchClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// 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.

import { createAuth, createTransporter, getAlgoliaAgent, shuffle, createIterablePromise } from '@algolia/client-common';
import {
createAuth,
createTransporter,
getAlgoliaAgent,
shuffle,
ApiError,
createIterablePromise,
} from '@algolia/client-common';
import type {
CreateClientOptions,
Headers,
Host,
QueryParameters,
Request,
RequestOptions,
ApiError,
IterableOptions,
} from '@algolia/client-common';

Expand Down Expand Up @@ -643,6 +649,19 @@ export function createSearchClient({
return { copyOperationResponse, batchResponses, moveOperationResponse };
},

async indexExists({ indexName }: GetSettingsProps): Promise<boolean> {
try {
await this.getSettings({ indexName });
} catch (error) {
if (error instanceof ApiError && error.status === 404) {
return false;
}
throw error;
}

return true;
},

/**
* Helper: calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets.
* 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.
Expand Down Expand Up @@ -1045,7 +1064,6 @@ export function createSearchClient({
const requestPath = '/1/indexes/{indexName}/rules/clear'.replace('{indexName}', encodeURIComponent(indexName));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down Expand Up @@ -1082,7 +1100,6 @@ export function createSearchClient({
const requestPath = '/1/indexes/{indexName}/synonyms/clear'.replace('{indexName}', encodeURIComponent(indexName));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down Expand Up @@ -1454,6 +1471,7 @@ export function createSearchClient({
.replace('{objectID}', encodeURIComponent(objectID));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down Expand Up @@ -1595,7 +1613,6 @@ export function createSearchClient({
if (length !== undefined) {
queryParameters.length = length.toString();
}

if (indexName !== undefined) {
queryParameters.indexName = indexName.toString();
}
Expand Down Expand Up @@ -1642,6 +1659,7 @@ export function createSearchClient({
.replace('{objectID}', encodeURIComponent(objectID));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (attributesToRetrieve !== undefined) {
queryParameters.attributesToRetrieve = attributesToRetrieve.toString();
}
Expand Down Expand Up @@ -2381,10 +2399,10 @@ export function createSearchClient({
const requestPath = '/1/indexes/{indexName}/rules/batch'.replace('{indexName}', encodeURIComponent(indexName));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}

if (clearExistingRules !== undefined) {
queryParameters.clearExistingRules = clearExistingRules.toString();
}
Expand Down Expand Up @@ -2487,7 +2505,6 @@ export function createSearchClient({
if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}

if (replaceExistingSynonyms !== undefined) {
queryParameters.replaceExistingSynonyms = replaceExistingSynonyms.toString();
}
Expand Down

0 comments on commit 263c7ee

Please sign in to comment.