Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(recommend): export method types #1287

Merged
merged 2 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/recommend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ const algoliarecommend = require('@algolia/recommend');
const client = algoliarecommend('YourApplicationID', 'YourAdminAPIKey');

client
.getFrequentlyBoughtTogether({
indexName: 'your_index_name',
objectID: 'your_object_id',
})
.getFrequentlyBoughtTogether([
{
indexName: 'your_index_name',
objectID: 'your_object_id',
},
])
.then(({ results }) => {
console.log(results);
})
Expand All @@ -53,10 +55,12 @@ client
});

client
.getRelatedProducts({
indexName: 'your_index_name',
objectID: 'your_object_id',
})
.getRelatedProducts([
{
indexName: 'your_index_name',
objectID: 'your_object_id',
},
])
.then(({ results }) => {
console.log(results);
})
Expand Down
6 changes: 4 additions & 2 deletions packages/recommend/src/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { createUserAgent } from '@algolia/transporter';

import { createRecommendClient } from '../createRecommendClient';
import { getFrequentlyBoughtTogether, getRecommendations, getRelatedProducts } from '../methods';
import { RecommendClient, RecommendOptions, WithRecommendMethods } from '../types';
import { BaseRecommendClient, RecommendOptions, WithRecommendMethods } from '../types';

export default function recommend(
appId: string,
apiKey: string,
options?: RecommendOptions
): WithRecommendMethods<RecommendClient> {
): RecommendClient {
const commonOptions = {
appId,
apiKey,
Expand Down Expand Up @@ -54,4 +54,6 @@ export default function recommend(
// eslint-disable-next-line functional/immutable-data
recommend.version = version;

export type RecommendClient = WithRecommendMethods<BaseRecommendClient>;

export * from '../types';
10 changes: 3 additions & 7 deletions packages/recommend/src/builds/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ import { createUserAgent } from '@algolia/transporter';

import { createRecommendClient } from '../createRecommendClient';
import { getFrequentlyBoughtTogether, getRecommendations, getRelatedProducts } from '../methods';
import {
RecommendClient as BaseRecommendClient,
RecommendOptions,
WithRecommendMethods,
} from '../types';
import { BaseRecommendClient, RecommendOptions, WithRecommendMethods } from '../types';

export default function recommend(
appId: string,
apiKey: string,
options?: RecommendOptions
): WithRecommendMethods<RecommendClient> {
): RecommendClient {
const commonOptions = {
appId,
apiKey,
Expand Down Expand Up @@ -52,6 +48,6 @@ export default function recommend(
// eslint-disable-next-line functional/immutable-data
recommend.version = version;

export type RecommendClient = BaseRecommendClient & Destroyable;
export type RecommendClient = WithRecommendMethods<BaseRecommendClient> & Destroyable;

export * from '../types';
4 changes: 2 additions & 2 deletions packages/recommend/src/createRecommendClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
} from '@algolia/client-common';
import { CallEnum, createTransporter, HostOptions } from '@algolia/transporter';

import { RecommendClient, RecommendClientOptions } from './types';
import { BaseRecommendClient, RecommendClientOptions } from './types';

export const createRecommendClient: CreateClient<
RecommendClient,
BaseRecommendClient,
RecommendClientOptions & ClientTransporterOptions
> = options => {
const appId = options.appId;
Expand Down
15 changes: 5 additions & 10 deletions packages/recommend/src/methods/getFrequentlyBoughtTogether.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { RecommendClient, WithRecommendMethods } from '../types';
import { getRecommendations, GetRecommendationsQuery } from './getRecommendations';

export type GetFrequentlyBoughtTogetherQuery = Omit<
GetRecommendationsQuery,
'model' | 'fallbackParameters'
>;
import { BaseRecommendClient, FrequentlyBoughtTogetherQuery, WithRecommendMethods } from '../types';
import { getRecommendations } from './getRecommendations';

type GetFrequentlyBoughtTogether = (
base: RecommendClient
) => WithRecommendMethods<RecommendClient>['getFrequentlyBoughtTogether'];
base: BaseRecommendClient
) => WithRecommendMethods<BaseRecommendClient>['getFrequentlyBoughtTogether'];

export const getFrequentlyBoughtTogether: GetFrequentlyBoughtTogether = base => {
return (queries, requestOptions) => {
return (queries: readonly FrequentlyBoughtTogetherQuery[], requestOptions) => {
return getRecommendations(base)(
queries.map(query => ({
...query,
Expand Down
25 changes: 5 additions & 20 deletions packages/recommend/src/methods/getRecommendations.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import { MethodEnum } from '@algolia/requester-common';

import {
RecommendClient,
RecommendModel,
RecommendSearchOptions,
WithRecommendMethods,
} from '../types';

export type GetRecommendationsQuery = {
readonly indexName: string;
readonly model: RecommendModel;
readonly objectID: string;
readonly threshold?: number;
readonly maxRecommendations?: number;
readonly queryParameters?: RecommendSearchOptions;
readonly fallbackParameters?: RecommendSearchOptions;
};
import { BaseRecommendClient, RecommendationsQuery, WithRecommendMethods } from '../types';

type GetRecommendations = (
base: RecommendClient
) => WithRecommendMethods<RecommendClient>['getRecommendations'];
base: BaseRecommendClient
) => WithRecommendMethods<BaseRecommendClient>['getRecommendations'];

export const getRecommendations: GetRecommendations = base => {
return (queries, requestOptions) => {
const requests: readonly GetRecommendationsQuery[] = queries.map(query => ({
return (queries: readonly RecommendationsQuery[], requestOptions) => {
const requests: readonly RecommendationsQuery[] = queries.map(query => ({
// The `threshold` param is required by the endpoint to make it easier
// to provide a default value later, so we default it in the client
// so that users don't have to provide a value.
Expand Down
12 changes: 5 additions & 7 deletions packages/recommend/src/methods/getRelatedProducts.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { RecommendClient, WithRecommendMethods } from '../types';
import { getRecommendations, GetRecommendationsQuery } from './getRecommendations';

export type GetRelatedProductsQuery = Omit<GetRecommendationsQuery, 'model'>;
import { BaseRecommendClient, RelatedProductsQuery, WithRecommendMethods } from '../types';
import { getRecommendations } from './getRecommendations';

type GetRelatedProducts = (
base: RecommendClient
) => WithRecommendMethods<RecommendClient>['getRelatedProducts'];
base: BaseRecommendClient
) => WithRecommendMethods<BaseRecommendClient>['getRelatedProducts'];

export const getRelatedProducts: GetRelatedProducts = base => {
return (queries, requestOptions) => {
return (queries: readonly RelatedProductsQuery[], requestOptions) => {
return getRecommendations(base)(
queries.map(query => ({
...query,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Transporter } from '@algolia/transporter';

export type RecommendClient = {
export type BaseRecommendClient = {
/**
* The application id.
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/recommend/src/types/FrequentlyBoughtTogetherQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RecommendationsQuery } from './RecommendationsQuery';

export type FrequentlyBoughtTogetherQuery = Omit<
RecommendationsQuery,
'model' | 'fallbackParameters'
>;
12 changes: 12 additions & 0 deletions packages/recommend/src/types/RecommendationsQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RecommendModel } from './RecommendModel';
import { RecommendSearchOptions } from './RecommendSearchOptions';

export type RecommendationsQuery = {
readonly indexName: string;
readonly model: RecommendModel;
readonly objectID: string;
readonly threshold?: number;
readonly maxRecommendations?: number;
readonly queryParameters?: RecommendSearchOptions;
readonly fallbackParameters?: RecommendSearchOptions;
};
3 changes: 3 additions & 0 deletions packages/recommend/src/types/RelatedProductsQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { RecommendationsQuery } from './RecommendationsQuery';

export type RelatedProductsQuery = Omit<RecommendationsQuery, 'model'>;
14 changes: 6 additions & 8 deletions packages/recommend/src/types/WithRecommendMethods.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import { MultipleQueriesResponse, SearchOptions } from '@algolia/client-search';
import { RequestOptions } from '@algolia/transporter';

import {
GetFrequentlyBoughtTogetherQuery,
GetRecommendationsQuery,
GetRelatedProductsQuery,
} from '../methods';
import { FrequentlyBoughtTogetherQuery } from './FrequentlyBoughtTogetherQuery';
import { RecommendationsQuery } from './RecommendationsQuery';
import { RelatedProductsQuery } from './RelatedProductsQuery';

export type WithRecommendMethods<TType> = TType & {
/**
* Returns recommendations.
*/
readonly getRecommendations: <TObject>(
queries: readonly GetRecommendationsQuery[],
queries: readonly RecommendationsQuery[],
requestOptions?: RequestOptions & SearchOptions
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;

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

/**
* Returns [Frequently Bought Together](https://algolia.com/doc/guides/algolia-ai/recommend/#frequently-bought-together) products.
*/
readonly getFrequentlyBoughtTogether: <TObject>(
queries: readonly GetFrequentlyBoughtTogetherQuery[],
queries: readonly FrequentlyBoughtTogetherQuery[],
requestOptions?: RequestOptions & SearchOptions
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
};
5 changes: 4 additions & 1 deletion packages/recommend/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
* @file Automatically generated by barrelsby.
*/

export * from './RecommendClient';
export * from './BaseRecommendClient';
export * from './FrequentlyBoughtTogetherQuery';
export * from './RecommendationsQuery';
export * from './RecommendClientOptions';
export * from './RecommendModel';
export * from './RecommendOptions';
export * from './RecommendSearchOptions';
export * from './RelatedProductsQuery';
export * from './WithRecommendMethods';