Skip to content

Commit ac79f67

Browse files
authored
fix(types): fix collision between js/core and plugins (#532)
* fix(js): Fix exposed types * Fix example types
1 parent 4b3329a commit ac79f67

26 files changed

Lines changed: 118 additions & 77 deletions

File tree

examples/playground/app.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import '@algolia/autocomplete-theme-classic';
1818

1919
import { createCategoriesPlugin } from './categoriesPlugin';
2020
import { shortcutsPlugin } from './shortcutsPlugin';
21-
import { ProductHit, ProductRecord } from './types';
21+
import { ProductHit } from './types';
2222

2323
const appId = 'latency';
2424
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
2525
const searchClient = algoliasearch(appId, apiKey);
26+
27+
// @ts-expect-error type error in search-insights
2628
insightsClient('init', { appId, apiKey });
2729

2830
const algoliaInsightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
@@ -49,7 +51,7 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
4951
});
5052
const categoriesPlugin = createCategoriesPlugin({ searchClient });
5153

52-
autocomplete({
54+
autocomplete<ProductHit>({
5355
container: '#autocomplete',
5456
placeholder: 'Search',
5557
debug: true,
@@ -70,7 +72,7 @@ autocomplete({
7072
{
7173
sourceId: 'products',
7274
getItems() {
73-
return getAlgoliaHits<ProductRecord>({
75+
return getAlgoliaHits<ProductHit>({
7476
searchClient,
7577
queries: [
7678
{
@@ -91,7 +93,7 @@ autocomplete({
9193
// eslint-disable-next-line @typescript-eslint/camelcase
9294
sale_price: hit.free_shipping
9395
? (hit.price - hit.price / 10).toFixed(2)
94-
: hit.price,
96+
: hit.price.toString(),
9597
}));
9698
});
9799
},

examples/playground/categoriesPlugin.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@ import {
66
import { SearchClient } from 'algoliasearch/lite';
77
import { h, Fragment } from 'preact';
88

9-
import { Highlighted } from './types';
10-
119
type CategoryRecord = {
1210
label: string;
1311
count: number;
1412
};
1513

16-
type CategoryHit = Highlighted<CategoryRecord>;
17-
1814
type CreateCategoriesPluginProps = {
1915
searchClient: SearchClient;
2016
};
2117

2218
export function createCategoriesPlugin({
2319
searchClient,
24-
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryHit, undefined> {
20+
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryRecord, undefined> {
2521
return {
2622
getSources({ query }) {
2723
return [
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hit } from '@algolia/client-search';
22

3-
export type ProductRecord = {
3+
type ProductRecord = {
44
brand: string;
55
categories: string[];
66
comments: number;
@@ -22,8 +22,9 @@ export type ProductRecord = {
2222
prince_range: string;
2323
rating: number;
2424
sale: boolean;
25-
sale_price: number;
25+
sale_price: string;
2626
type: string;
27+
url: string;
2728
};
2829

2930
type WithAutocompleteAnalytics<THit> = THit & {

examples/query-suggestions-with-categories/app.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
2222
'exact_matches',
2323
'categories',
2424
],
25-
categoriesPerItem: 1,
26-
categoriesLimit: 2,
25+
itemsWithCategories: 1,
26+
categoriesPerItem: 2,
2727
});
2828

2929
autocomplete({

examples/query-suggestions-with-hits/app.tsx

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,19 @@ import {
99
createAlgoliaInsightsPlugin,
1010
} from '@algolia/autocomplete-plugin-algolia-insights';
1111
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';
12-
import { Hit } from '@algolia/client-search';
1312
import algoliasearch from 'algoliasearch';
1413
import { h, Fragment } from 'preact';
1514
import insightsClient from 'search-insights';
1615

1716
import '@algolia/autocomplete-theme-classic';
1817

19-
type Product = {
20-
brand: string;
21-
categories: string[];
22-
description: string;
23-
image: string;
24-
name: string;
25-
price: number;
26-
rating: number;
27-
__autocomplete_indexName: string;
28-
__autocomplete_queryID: string;
29-
};
30-
type ProductHit = Hit<Product>;
18+
import { ProductHit } from './types';
3119

3220
const appId = 'latency';
3321
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
3422
const searchClient = algoliasearch(appId, apiKey);
23+
24+
// @ts-expect-error type error in search-insights
3525
insightsClient('init', { appId, apiKey });
3626

3727
const algoliaInsightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
@@ -46,10 +36,9 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
4636
},
4737
});
4838

49-
autocomplete({
39+
autocomplete<ProductHit>({
5040
container: '#autocomplete',
5141
placeholder: 'Search',
52-
debug: true,
5342
openOnFocus: true,
5443
plugins: [algoliaInsightsPlugin, querySuggestionsPlugin],
5544
getSources({ query, state }) {
@@ -61,7 +50,7 @@ autocomplete({
6150
{
6251
sourceId: 'products',
6352
getItems() {
64-
return getAlgoliaHits<Product>({
53+
return getAlgoliaHits<ProductHit>({
6554
searchClient,
6655
queries: [
6756
{
@@ -111,7 +100,7 @@ type ProductItemProps = {
111100

112101
function ProductItem({ hit, insights, components }: ProductItemProps) {
113102
return (
114-
<div className="aa-ItemWrapper">
103+
<a href={hit.url} className="aa-ItemLink">
115104
<div className="aa-ItemContent">
116105
<div className="aa-ItemIcon aa-ItemIcon--picture aa-ItemIcon--alignTop">
117106
<img src={hit.image} alt={hit.name} width="40" height="40" />
@@ -185,6 +174,6 @@ function ProductItem({ hit, insights, components }: ProductItemProps) {
185174
</svg>
186175
</button>
187176
</div>
188-
</div>
177+
</a>
189178
);
190179
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Hit } from '@algolia/client-search';
2+
3+
type ProductRecord = {
4+
brand: string;
5+
categories: string[];
6+
description: string;
7+
image: string;
8+
name: string;
9+
price: number;
10+
rating: number;
11+
url: string;
12+
};
13+
14+
type WithAutocompleteAnalytics<THit> = THit & {
15+
__autocomplete_indexName: string;
16+
__autocomplete_queryID: string;
17+
};
18+
19+
export type ProductHit = WithAutocompleteAnalytics<Hit<ProductRecord>>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ProductHit';

examples/recently-viewed-items/app.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { h, Fragment } from 'preact';
1010
import '@algolia/autocomplete-theme-classic';
1111

1212
import { createLocalStorageRecentlyViewedItems } from './recentlyViewedItemsPlugin';
13-
import { ProductItem, ProductHit } from './types/ProductHit';
13+
import { ProductHit } from './types';
1414

1515
const appId = 'latency';
1616
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
@@ -21,7 +21,7 @@ const recentlyViewedItems = createLocalStorageRecentlyViewedItems({
2121
limit: 5,
2222
});
2323

24-
autocomplete({
24+
autocomplete<ProductHit>({
2525
container: '#autocomplete',
2626
placeholder: 'Search',
2727
openOnFocus: true,
@@ -35,7 +35,7 @@ autocomplete({
3535
{
3636
sourceId: 'products',
3737
getItems() {
38-
return getAlgoliaHits<ProductItem>({
38+
return getAlgoliaHits<ProductHit>({
3939
searchClient,
4040
queries: [
4141
{
@@ -55,6 +55,7 @@ autocomplete({
5555
id: item.objectID,
5656
label: item.name,
5757
image: item.image,
58+
url: item.url,
5859
});
5960
},
6061
templates: {

0 commit comments

Comments
 (0)