Skip to content

Commit 95281fe

Browse files
fix(recommend): export method types (#1287)
1 parent 2fefd78 commit 95281fe

13 files changed

+68
-66
lines changed

packages/recommend/README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ const algoliarecommend = require('@algolia/recommend');
4141
const client = algoliarecommend('YourApplicationID', 'YourAdminAPIKey');
4242

4343
client
44-
.getFrequentlyBoughtTogether({
45-
indexName: 'your_index_name',
46-
objectID: 'your_object_id',
47-
})
44+
.getFrequentlyBoughtTogether([
45+
{
46+
indexName: 'your_index_name',
47+
objectID: 'your_object_id',
48+
},
49+
])
4850
.then(({ results }) => {
4951
console.log(results);
5052
})
@@ -53,10 +55,12 @@ client
5355
});
5456

5557
client
56-
.getRelatedProducts({
57-
indexName: 'your_index_name',
58-
objectID: 'your_object_id',
59-
})
58+
.getRelatedProducts([
59+
{
60+
indexName: 'your_index_name',
61+
objectID: 'your_object_id',
62+
},
63+
])
6064
.then(({ results }) => {
6165
console.log(results);
6266
})

packages/recommend/src/builds/browser.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import { createUserAgent } from '@algolia/transporter';
99

1010
import { createRecommendClient } from '../createRecommendClient';
1111
import { getFrequentlyBoughtTogether, getRecommendations, getRelatedProducts } from '../methods';
12-
import { RecommendClient, RecommendOptions, WithRecommendMethods } from '../types';
12+
import { BaseRecommendClient, RecommendOptions, WithRecommendMethods } from '../types';
1313

1414
export default function recommend(
1515
appId: string,
1616
apiKey: string,
1717
options?: RecommendOptions
18-
): WithRecommendMethods<RecommendClient> {
18+
): RecommendClient {
1919
const commonOptions = {
2020
appId,
2121
apiKey,
@@ -54,4 +54,6 @@ export default function recommend(
5454
// eslint-disable-next-line functional/immutable-data
5555
recommend.version = version;
5656

57+
export type RecommendClient = WithRecommendMethods<BaseRecommendClient>;
58+
5759
export * from '../types';

packages/recommend/src/builds/node.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ import { createUserAgent } from '@algolia/transporter';
88

99
import { createRecommendClient } from '../createRecommendClient';
1010
import { getFrequentlyBoughtTogether, getRecommendations, getRelatedProducts } from '../methods';
11-
import {
12-
RecommendClient as BaseRecommendClient,
13-
RecommendOptions,
14-
WithRecommendMethods,
15-
} from '../types';
11+
import { BaseRecommendClient, RecommendOptions, WithRecommendMethods } from '../types';
1612

1713
export default function recommend(
1814
appId: string,
1915
apiKey: string,
2016
options?: RecommendOptions
21-
): WithRecommendMethods<RecommendClient> {
17+
): RecommendClient {
2218
const commonOptions = {
2319
appId,
2420
apiKey,
@@ -52,6 +48,6 @@ export default function recommend(
5248
// eslint-disable-next-line functional/immutable-data
5349
recommend.version = version;
5450

55-
export type RecommendClient = BaseRecommendClient & Destroyable;
51+
export type RecommendClient = WithRecommendMethods<BaseRecommendClient> & Destroyable;
5652

5753
export * from '../types';

packages/recommend/src/createRecommendClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
} from '@algolia/client-common';
99
import { CallEnum, createTransporter, HostOptions } from '@algolia/transporter';
1010

11-
import { RecommendClient, RecommendClientOptions } from './types';
11+
import { BaseRecommendClient, RecommendClientOptions } from './types';
1212

1313
export const createRecommendClient: CreateClient<
14-
RecommendClient,
14+
BaseRecommendClient,
1515
RecommendClientOptions & ClientTransporterOptions
1616
> = options => {
1717
const appId = options.appId;

packages/recommend/src/methods/getFrequentlyBoughtTogether.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import { RecommendClient, WithRecommendMethods } from '../types';
2-
import { getRecommendations, GetRecommendationsQuery } from './getRecommendations';
3-
4-
export type GetFrequentlyBoughtTogetherQuery = Omit<
5-
GetRecommendationsQuery,
6-
'model' | 'fallbackParameters'
7-
>;
1+
import { BaseRecommendClient, FrequentlyBoughtTogetherQuery, WithRecommendMethods } from '../types';
2+
import { getRecommendations } from './getRecommendations';
83

94
type GetFrequentlyBoughtTogether = (
10-
base: RecommendClient
11-
) => WithRecommendMethods<RecommendClient>['getFrequentlyBoughtTogether'];
5+
base: BaseRecommendClient
6+
) => WithRecommendMethods<BaseRecommendClient>['getFrequentlyBoughtTogether'];
127

138
export const getFrequentlyBoughtTogether: GetFrequentlyBoughtTogether = base => {
14-
return (queries, requestOptions) => {
9+
return (queries: readonly FrequentlyBoughtTogetherQuery[], requestOptions) => {
1510
return getRecommendations(base)(
1611
queries.map(query => ({
1712
...query,

packages/recommend/src/methods/getRecommendations.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
11
import { MethodEnum } from '@algolia/requester-common';
22

3-
import {
4-
RecommendClient,
5-
RecommendModel,
6-
RecommendSearchOptions,
7-
WithRecommendMethods,
8-
} from '../types';
9-
10-
export type GetRecommendationsQuery = {
11-
readonly indexName: string;
12-
readonly model: RecommendModel;
13-
readonly objectID: string;
14-
readonly threshold?: number;
15-
readonly maxRecommendations?: number;
16-
readonly queryParameters?: RecommendSearchOptions;
17-
readonly fallbackParameters?: RecommendSearchOptions;
18-
};
3+
import { BaseRecommendClient, RecommendationsQuery, WithRecommendMethods } from '../types';
194

205
type GetRecommendations = (
21-
base: RecommendClient
22-
) => WithRecommendMethods<RecommendClient>['getRecommendations'];
6+
base: BaseRecommendClient
7+
) => WithRecommendMethods<BaseRecommendClient>['getRecommendations'];
238

249
export const getRecommendations: GetRecommendations = base => {
25-
return (queries, requestOptions) => {
26-
const requests: readonly GetRecommendationsQuery[] = queries.map(query => ({
10+
return (queries: readonly RecommendationsQuery[], requestOptions) => {
11+
const requests: readonly RecommendationsQuery[] = queries.map(query => ({
2712
// The `threshold` param is required by the endpoint to make it easier
2813
// to provide a default value later, so we default it in the client
2914
// so that users don't have to provide a value.

packages/recommend/src/methods/getRelatedProducts.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import { RecommendClient, WithRecommendMethods } from '../types';
2-
import { getRecommendations, GetRecommendationsQuery } from './getRecommendations';
3-
4-
export type GetRelatedProductsQuery = Omit<GetRecommendationsQuery, 'model'>;
1+
import { BaseRecommendClient, RelatedProductsQuery, WithRecommendMethods } from '../types';
2+
import { getRecommendations } from './getRecommendations';
53

64
type GetRelatedProducts = (
7-
base: RecommendClient
8-
) => WithRecommendMethods<RecommendClient>['getRelatedProducts'];
5+
base: BaseRecommendClient
6+
) => WithRecommendMethods<BaseRecommendClient>['getRelatedProducts'];
97

108
export const getRelatedProducts: GetRelatedProducts = base => {
11-
return (queries, requestOptions) => {
9+
return (queries: readonly RelatedProductsQuery[], requestOptions) => {
1210
return getRecommendations(base)(
1311
queries.map(query => ({
1412
...query,

packages/recommend/src/types/RecommendClient.ts renamed to packages/recommend/src/types/BaseRecommendClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Transporter } from '@algolia/transporter';
22

3-
export type RecommendClient = {
3+
export type BaseRecommendClient = {
44
/**
55
* The application id.
66
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { RecommendationsQuery } from './RecommendationsQuery';
2+
3+
export type FrequentlyBoughtTogetherQuery = Omit<
4+
RecommendationsQuery,
5+
'model' | 'fallbackParameters'
6+
>;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { RecommendModel } from './RecommendModel';
2+
import { RecommendSearchOptions } from './RecommendSearchOptions';
3+
4+
export type RecommendationsQuery = {
5+
readonly indexName: string;
6+
readonly model: RecommendModel;
7+
readonly objectID: string;
8+
readonly threshold?: number;
9+
readonly maxRecommendations?: number;
10+
readonly queryParameters?: RecommendSearchOptions;
11+
readonly fallbackParameters?: RecommendSearchOptions;
12+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { RecommendationsQuery } from './RecommendationsQuery';
2+
3+
export type RelatedProductsQuery = Omit<RecommendationsQuery, 'model'>;
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
import { MultipleQueriesResponse, SearchOptions } from '@algolia/client-search';
22
import { RequestOptions } from '@algolia/transporter';
33

4-
import {
5-
GetFrequentlyBoughtTogetherQuery,
6-
GetRecommendationsQuery,
7-
GetRelatedProductsQuery,
8-
} from '../methods';
4+
import { FrequentlyBoughtTogetherQuery } from './FrequentlyBoughtTogetherQuery';
5+
import { RecommendationsQuery } from './RecommendationsQuery';
6+
import { RelatedProductsQuery } from './RelatedProductsQuery';
97

108
export type WithRecommendMethods<TType> = TType & {
119
/**
1210
* Returns recommendations.
1311
*/
1412
readonly getRecommendations: <TObject>(
15-
queries: readonly GetRecommendationsQuery[],
13+
queries: readonly RecommendationsQuery[],
1614
requestOptions?: RequestOptions & SearchOptions
1715
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
1816

1917
/**
2018
* Returns [Related Products](https://algolia.com/doc/guides/algolia-ai/recommend/#related-products).
2119
*/
2220
readonly getRelatedProducts: <TObject>(
23-
queries: readonly GetRelatedProductsQuery[],
21+
queries: readonly RelatedProductsQuery[],
2422
requestOptions?: RequestOptions & SearchOptions
2523
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
2624

2725
/**
2826
* Returns [Frequently Bought Together](https://algolia.com/doc/guides/algolia-ai/recommend/#frequently-bought-together) products.
2927
*/
3028
readonly getFrequentlyBoughtTogether: <TObject>(
31-
queries: readonly GetFrequentlyBoughtTogetherQuery[],
29+
queries: readonly FrequentlyBoughtTogetherQuery[],
3230
requestOptions?: RequestOptions & SearchOptions
3331
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
3432
};

packages/recommend/src/types/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
* @file Automatically generated by barrelsby.
33
*/
44

5-
export * from './RecommendClient';
5+
export * from './BaseRecommendClient';
6+
export * from './FrequentlyBoughtTogetherQuery';
7+
export * from './RecommendationsQuery';
68
export * from './RecommendClientOptions';
79
export * from './RecommendModel';
810
export * from './RecommendOptions';
911
export * from './RecommendSearchOptions';
12+
export * from './RelatedProductsQuery';
1013
export * from './WithRecommendMethods';

0 commit comments

Comments
 (0)