Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'algoliasearch' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
/* eslint-disable no-console */
import Promise from 'bluebird';
import algoliasearch from 'algoliasearch';
import { Pub, Page } from '../server/models';
import { getPubSearchData, getPageSearchData } from '../workers/utils/searchUtils';
const client = algoliasearch(process.env.ALGOLIA_ID, process.env.ALGOLIA_KEY);
const pubsIndex = client.initIndex('pubs');
const pagesIndex = client.initIndex('pages');
console.log('Beginning search sync');
let records = 0;
const findAndIndexPubs = (pubIds) => {
return getPubSearchData(pubIds).then((pubSyncData) => {
records += pubSyncData.length;
return pubsIndex.addObjects(pubSyncData);
});
};
const findAndIndexPages = (pageIds) => {
return getPageSearchData(pageIds).then((pageSyncData) => {
records += pageSyncData.length;
import * as algoliasearchProxy from 'algoliasearch/index';
import * as encodeProxy from 'querystring-es3/encode';
import { VERSION } from './version';
// AOT + Rollup workaround
// https://github.com/rollup/rollup/issues/1267#issuecomment-296395734
const algoliasearch = algoliasearchProxy.default || algoliasearchProxy;
const encode = encodeProxy.default || encodeProxy;
export function createSSRAlgoliaClient({
httpClient,
HttpHeaders,
transferState,
makeStateKey,
}) {
console.warn(
'`createSSRAlgoliaClient` is deprecated in favor of `createSSRSearchClient` to be plugged to `searchClient`.'
);
return (_, appId, apiKey) =>
createSSRSearchClient({
appId,
apiKey,
? autocompleteOptions.debug
: false;
// eslint-disable-next-line no-param-reassign
autocompleteOptions.debug = debug || autocompleteOptionsDebug;
this.autocompleteOptions = autocompleteOptions;
this.autocompleteOptions.cssClasses =
this.autocompleteOptions.cssClasses || {};
this.autocompleteOptions.cssClasses.prefix =
this.autocompleteOptions.cssClasses.prefix || 'ds';
// eslint-disable-next-line no-param-reassign
handleSelected = handleSelected || this.handleSelected;
this.isSimpleLayout = layout === 'simple';
this.client = algoliasearch(this.appId, this.apiKey);
this.client.addAlgoliaAgent(`docsearch.js ${version}`);
if (enhancedSearchInput) {
this.input = DocSearch.injectSearchBox(this.input);
}
this.autocomplete = autocomplete(this.input, autocompleteOptions, [
{
source: this.getAutocompleteSource(transformData, queryHook),
templates: {
suggestion: DocSearch.getSuggestionTemplate(this.isSimpleLayout),
footer: templates.footer,
empty: DocSearch.getEmptyTemplate(),
},
},
]);
function fetchJob(jobName: string, jobId: string): Promise {
const algoliaClient = algoliasearch(JobSuggest.algoliaApp, JobSuggest.algoliaApiKey)
const jobIndex = algoliaClient.initIndex(JobSuggest.algoliaIndex)
// First fetch by name.
return jobIndex.search(jobName).
// Check the code OGR is right (if given)
then(getFirstHitWithCodeOgr(jobId)).
// If nothing found, fetch by code OGR.
then((hit): Promise => hit ? Promise.resolve(hit) :
jobIndex.search(jobId).then(getFirstHitWithCodeOgr(jobId))).
then(jobFromSuggestion)
}
if (__DEV__) {
// This becomes handy to debug network requests in Chrome DevTools
XMLHttpRequest = GLOBAL.originalXMLHttpRequest
? GLOBAL.originalXMLHttpRequest
: GLOBAL.XMLHttpRequest;
}
/* eslint-enable */
// Create new Apollo client
const client = new ApolloClient({
link: new HttpLink({ uri: config.API_URL }),
cache: new InMemoryCache(),
});
// Algolia client
const algolia = algoliasearch(config.ALGOLIA_APP_ID, config.ALGOLIA_API_KEY);
export default class Store {
ui = new UI();
algolia = {
client: algolia,
apps: algolia.initIndex('apps'),
};
async setup() {
return true;
}
}
/**
public componentDidMount(): void {
const {
algoliaApp,
algoliaApiKey,
algoliaIndex,
display,
displayValue,
hitsPerPage,
onChange,
onSuggestSelect,
suggestionTemplate,
...extraProps
} = this.props
const algoliaClient = algoliasearch(algoliaApp, algoliaApiKey)
const displayFunc = display ||
((suggestion): React.ReactNode => this.props.displayKey && suggestion[this.props.displayKey])
const handleSelected = (event, suggestion): void => {
const displaySuggestion = displayFunc(suggestion)
onSuggestSelect && onSuggestSelect(event, displaySuggestion, suggestion)
onChange && onChange(event, displaySuggestion, suggestion)
}
// TODO(pascal): Rething this pattern as this is not compatible with React.
// Modifying the DOM without React is somewhat OK, but here it changes the
// main DOM element of this component which confuses React when trying to
// update the components before it.
const suggest = autocomplete(this.node.current, extraProps, [
{
display: displayFunc,
source: autocomplete.sources.hits(
algoliaClient.initIndex(algoliaIndex), {hitsPerPage: hitsPerPage}),
function fetchFirstSuggestedJob(jobName: string): Promise {
const algoliaClient = algoliasearch(JobSuggest.algoliaApp, JobSuggest.algoliaApiKey)
const jobIndex = algoliaClient.initIndex(JobSuggest.algoliaIndex)
return jobIndex.search(jobName).then((results): bayes.bob.Job|null => {
const firstJobSuggestion = results.hits && results.hits[0]
if (!firstJobSuggestion) {
return null
}
const firstSuggestedJob = jobFromSuggestion(firstJobSuggestion)
// TODO(florian): Check that the job has the expected RomeId.
return firstSuggestedJob
})
}
OnDestroy,
Output,
EventEmitter,
Inject,
PLATFORM_ID,
} from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import * as algoliasearchProxy from 'algoliasearch/lite';
import instantsearch from 'instantsearch.js/es';
import { AlgoliaSearchHelper } from 'algoliasearch-helper';
import { Widget } from '../base-widget';
import { VERSION } from '../version';
const algoliasearch = algoliasearchProxy.default || algoliasearchProxy;
export type SearchRequest = {
indexName: string;
params: SearchRequestParameters;
};
export type SearchForFacetValuesRequest = {
indexName: string;
params: SearchForFacetValuesRequestParameters;
};
// Documentation: https://www.algolia.com/doc/api-reference/search-api-parameters/
export type SearchParameters = {
// Attributes
attributesToRetrieve?: string[];
restrictSearchableAttributes?: string[];
test('can create a Store instance from an algolia client', () => {
const client = algoliaClient('appId', 'apiKey');
const store = createFromAlgoliaClient(client);
expect(store).toBeInstanceOf(Store);
expect(store.algoliaClient).toBe(client);
});
it('passes user agents', () => {
const addAlgoliaAgent = jest.fn();
algoliasearch.mockImplementation(() => {
return {
addAlgoliaAgent,
};
});
const ssrSearchClient = createSSRSearchClient({
appId: 'test',
apiKey: 'test',
httpClient: null,
HttpHeaders: null,
makeStateKey: null,
transferState: null,
});
expect(addAlgoliaAgent).toHaveBeenCalledTimes(3);
expect(addAlgoliaAgent).toHaveBeenCalledWith(