Skip to content

Commit e0ccab2

Browse files
authored
Merge pull request #130 from nuxt-modules/feat/useAsyncAlgoliaSearch
Feat/use async algolia search
2 parents 4ccba1c + 910849c commit e0ccab2

File tree

7 files changed

+94
-55
lines changed

7 files changed

+94
-55
lines changed

README.md

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515

1616
## Features
1717

18-
- Nuxt 3 ready
1918
- Easy integration with Algolia
20-
- Handy composables like useAlgolia, useSearch, etc
19+
- Handy composables like useAlgoliaSearch, useAsyncAlgoliaSearch
2120
- Support for Vue Instantsearch components
2221
- Support for Algolia Recommend
2322
- Support for Docsearch
2423
- Support for Automatic Indexing
2524
- Support for caching the requests and responses
2625
- Support for SSR requests
27-
- TypeScript support
2826

2927
[📖  Read the documentation](https://algolia.nuxtjs.org)
3028

@@ -43,38 +41,37 @@ npm i @nuxtjs/algolia # npm
4341

4442
Firstly, you need to add `@nuxtjs/algolia` to your Nuxt config.
4543

46-
```javascript
47-
// nuxt.config.js
48-
49-
{
50-
modules: [
51-
"@nuxtjs/algolia",
52-
],
53-
algolia: {
54-
apiKey: "<YOUR_SEARCH_API_KEY>",
55-
applicationId: "<YOUR_APPLICATION_ID>",
56-
}
57-
}
44+
```js
45+
export default defineNuxtConfig({
46+
modules: ['@nuxtjs/algolia']
47+
})
48+
```
49+
50+
Furthermore, add `ALGOLIA_API_KEY` and `ALGOLIA_APPLICATION_ID` environment variables to .env file.
51+
52+
```env
53+
ALGOLIA_API_KEY="0fd1c4eba2d831788333e77c9d855f1d"
54+
ALGOLIA_APPLICATION_ID="AGN9HEEKF3"
5855
```
5956

60-
Then you can start using `@nuxtjs/algolia` in your setup function!
57+
Now you can start using `@nuxtjs/algolia` in your app!
6158

6259
```vue
63-
<script setup>
64-
const { result, search } = useAlgoliaSearch("test_index"); // pass your index as param
60+
<script setup lang="ts">
61+
// Client side
62+
const { result: data, search } = useAlgoliaSearch('test_index')
6563
6664
onMounted(async () => {
67-
await search({ query: "Samsung" });
68-
});
69-
70-
// Or SSR
71-
onServerPrefetch(async () => {
7265
await search({ query: 'Samsung' })
7366
})
7467
75-
// Or by using useAsyncData
76-
const { data } = await useAsyncData('ssr-search-results', () => search({ query: 'Samsung' }))
68+
// Or SSR
69+
const { data } = await useAsyncAlgoliaSearch({ indexName: 'test_index', query: 'Samsung' })
7770
</script>
71+
72+
<template>
73+
<div>{{ data.value.hits }}</div>
74+
</template>
7875
```
7976

8077
## Development

docs/content/1.getting-started/1.quick-start.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default defineNuxtConfig({
2828
You can find more about configuring `@nuxtjs/algolia` [here](/getting-started/configuration).
2929
::
3030

31-
3. **Lastly, add [ALGOLIA_API_KEY](https://www.algolia.com/doc/guides/security/api-keys/) and [ALGOLIA_APPLICATION_ID](https://www.algolia.com/account/api-keys/) to the .env**
31+
3. **Add [ALGOLIA_API_KEY](https://www.algolia.com/doc/guides/security/api-keys/) and [ALGOLIA_APPLICATION_ID](https://www.algolia.com/account/api-keys/) to the .env**
3232

3333
```env
3434
ALGOLIA_API_KEY="0fd1c4eba2d831788333e77c9d855f1d"
@@ -38,22 +38,31 @@ ALGOLIA_APPLICATION_ID="AGN9HEEKF3"
3838
4. **Use Algolia in your application**
3939

4040
```vue
41-
<template>
42-
<div>{{ result }}</div>
43-
</template>
44-
45-
<script setup>
46-
const { result, search } = useAlgoliaSearch('test_index') // pass your index name as param
41+
<script setup lang="ts">
42+
// Client side
43+
const { result: data, search } = useAlgoliaSearch('test_index')
4744
4845
onMounted(async () => {
49-
await search({ query: 'Samsung' });
46+
await search({ query: 'Samsung' })
5047
})
5148
52-
// Or SSR by using useAsyncData
53-
const { data } = await useAsyncData('ssr-search-results', () => search({ query: 'Samsung' }))
49+
// Or SSR
50+
const { data } = await useAsyncAlgoliaSearch({ indexName: 'test_index', query: 'Samsung' })
5451
</script>
52+
53+
<template>
54+
<div>{{ data.value.hits }}</div>
55+
</template>
5556
```
5657

57-
5. **You're good to go!**
58+
::alert{type="success"}
59+
60+
That's it! You can now use Algolia in your Nuxt app ✨
61+
62+
::
63+
64+
::alert{type="info"}
5865

5966
Check out [how to use Algolia module further](/getting-started/usage){ .text-primary-500 }.
67+
68+
::

docs/content/1.getting-started/2.configuration.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,21 @@ description: ''
88
You can add custom configuration to the module like following:
99

1010
```js{}[nuxt.config.js]
11-
import { defineNuxtConfig } from 'nuxt'
12-
1311
export default defineNuxtConfig({
1412
modules: ['@nuxtjs/algolia'],
1513
algolia: {
16-
apiKey: 'MY_API_KEY',
17-
applicationId: 'MY_APPLICATION_ID'
14+
// options
1815
}
1916
})
2017
```
2118

2219
Or
2320

2421
```js{}[nuxt.config.js]
25-
import { defineNuxtConfig } from 'nuxt'
26-
2722
export default defineNuxtConfig({
2823
modules: [
2924
['@nuxtjs/algolia', {
30-
apiKey: 'MY_API_KEY',
31-
applicationId: 'MY_APPLICATION_ID'
25+
// options
3226
}]
3327
]
3428
})

docs/content/1.getting-started/3.usage.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ This composable works also for SSR applications and requests like this:
2727
<script setup lang="ts">
2828
const { result, search } = useAlgoliaSearch('test_index') // pass your index name as param
2929
30-
onServerPrefetch(async () => {
31-
await search({ query: 'Samsung' })
32-
})
33-
34-
// Or by using useAsyncData
3530
const { data } = await useAsyncData('ssr-search-results', () => search({ query: 'Samsung' }))
36-
// You can access the search result by either `result` or `data`
3731
</script>
3832
```
3933

@@ -42,6 +36,26 @@ const { data } = await useAsyncData('ssr-search-results', () => search({ query:
4236

4337
For more details check out the official documentation of this method [here](https://www.algolia.com/doc/api-client/methods/search/)
4438

39+
## `useAsyncAlgoliaSearch`
40+
41+
Async wrapper around `useAlgoliaSearch` composable that decreases the number of steps needed to use Algolia with `useAsyncData` while maintaining the same functionality. The response will have a form described [here](https://www.algolia.com/doc/api-reference/api-methods/search/#response) and will be wrapped around `useAsyncData` so you will have access to `data`, `error`, and other properties.
42+
43+
```vue
44+
<script setup lang="ts">
45+
const { data, error } = await useAsyncAlgoliaSearch({ indexName: 'test_index', query: 'Samsung' })
46+
</script>
47+
48+
<template>
49+
<div>{{ data.value.hits }}</div>
50+
</template>
51+
```
52+
53+
This composable accepts an object as a param with following properties:
54+
55+
* `indexName` - the name of you index in Algolia dashboard. For more details about initializing index check out the official documentation [here](https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/javascript/?client=javascript#initialize-an-index). If you passed a global index in your module configuration, this property can be skipped.
56+
* `query` - a keywoard, sentence, or text that you want to search the index with.
57+
* `requestOptions` optional object with options for the request like filters. You can check more about is [here](https://www.algolia.com/doc/api-reference/api-methods/search/#method-param-requestoptions)
58+
4559
## `useAlgoliaFacetedSearch`
4660

4761
Use this composable to search using facet values like `category`, `phone`.

docs/content/1.index.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ Nuxt module for first class integration with [Algolia](https://algolia.com).
2424

2525
#extra
2626
::list
27-
- Nuxt 3 ready
28-
- Easy integration with Algolia
29-
- Handy composables like useAlgolia, useSearch
27+
- Handy composables like useAlgoliaSearch, useAsyncAlgoliaSearch
3028
- Support for Vue Instantsearch components
3129
- Support for Algolia Recommend
3230
- Support for Docsearch
33-
- Support for Automatic Indexing
31+
- Support for Automatic Indexing with Storyblok
3432
- Support for caching the requests and responses
3533
- Support for SSR requests
36-
- TypeScript support
3734
::
3835
::

playground/pages/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const { algolia: { docSearch } } = useRuntimeConfig()
2727
// Used to try the refresh of the component on options changes
2828
const indexName = ref('test_index')
2929
30+
// AlgoliaSearch wrapped in useAsyncData
31+
const asyncResult = await useAsyncAlgoliaSearch({ query: 'Samsung', indexName: indexName.value })
32+
3033
const { result, search } = useAlgoliaSearch(indexName.value)
3134
const { result: searchForFacetValuesResult, search: searchForFacetValues } = useAlgoliaFacetedSearch(indexName.value)
3235
const algolia = useAlgoliaRef()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* eslint-disable no-redeclare */
2+
import type { RequestOptionsObject } from '../../types'
3+
import { useAlgoliaInitIndex } from './useAlgoliaInitIndex'
4+
import { useNuxtApp, useAsyncData, useRuntimeConfig } from '#imports'
5+
6+
export type SearchParams = { query: string, indexName?: string } & RequestOptionsObject;
7+
8+
export async function useAsyncAlgoliaSearch ({ query, requestOptions, indexName }: SearchParams) {
9+
const config = useRuntimeConfig();
10+
const index = indexName || config.algolia.globalIndex
11+
12+
if (!index) throw new Error('`[@nuxtjs/algolia]` Cannot search in Algolia without `indexName`')
13+
14+
const algoliaIndex = useAlgoliaInitIndex(index)
15+
16+
const result = await useAsyncData(`${index}-async-search-result`, async () => {
17+
if (process.server) {
18+
const nuxtApp = useNuxtApp()
19+
nuxtApp.$algolia.transporter.requester = (await import('@algolia/requester-node-http').then(lib => lib.default || lib)).createNodeHttpRequester()
20+
}
21+
return await algoliaIndex.search(query, requestOptions)
22+
})
23+
24+
return result
25+
}

0 commit comments

Comments
 (0)