Skip to content

Commit

Permalink
feat: add custom request function (#1312)
Browse files Browse the repository at this point in the history
increase bundle size for lite package to 4.4ko
  • Loading branch information
millotp authored Oct 19, 2021
1 parent 7c38e64 commit 3cea192
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
},
{
"path": "packages/algoliasearch/dist/algoliasearch-lite.umd.js",
"maxSize": "4.35KB"
"maxSize": "4.4KB"
},
{
"path": "packages/recommend/dist/recommend.umd.js",
Expand Down
8 changes: 7 additions & 1 deletion packages/algoliasearch/src/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
copySettings,
copySynonyms,
createSearchClient,
customRequest,
deleteApiKey,
DeleteApiKeyResponse,
deleteBy,
Expand Down Expand Up @@ -190,7 +191,7 @@ import {
import { LogLevelEnum } from '@algolia/logger-common';
import { createConsoleLogger } from '@algolia/logger-console';
import { createBrowserXhrRequester } from '@algolia/requester-browser-xhr';
import { createUserAgent, RequestOptions } from '@algolia/transporter';
import { createUserAgent, Request, RequestOptions } from '@algolia/transporter';

import { AlgoliaSearchOptions, InitAnalyticsOptions, InitPersonalizationOptions } from '../types';

Expand Down Expand Up @@ -273,6 +274,7 @@ export default function algoliasearch(
searchDictionaryEntries,
setDictionarySettings,
waitAppTask,
customRequest,
initIndex: base => (indexName: string): SearchIndex => {
return initIndex(base)(indexName, {
methods: {
Expand Down Expand Up @@ -677,6 +679,10 @@ export type SearchClient = BaseSearchClient & {
taskID: number,
requestOptions?: RequestOptions
) => Readonly<Promise<TaskStatusResponse>>;
readonly customRequest: <TResponse>(
request: Request,
requestOptions?: RequestOptions
) => Readonly<Promise<TResponse>>;
readonly initAnalytics: (options?: InitAnalyticsOptions) => AnalyticsClient;
readonly initPersonalization: (options?: InitPersonalizationOptions) => PersonalizationClient;
/**
Expand Down
8 changes: 7 additions & 1 deletion packages/algoliasearch/src/builds/browserLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createInMemoryCache } from '@algolia/cache-in-memory';
import { AuthMode, version } from '@algolia/client-common';
import {
createSearchClient,
customRequest,
findAnswers,
FindAnswersOptions,
FindAnswersResponse,
Expand All @@ -25,7 +26,7 @@ import {
import { LogLevelEnum } from '@algolia/logger-common';
import { createConsoleLogger } from '@algolia/logger-console';
import { createBrowserXhrRequester } from '@algolia/requester-browser-xhr';
import { createUserAgent, RequestOptions } from '@algolia/transporter';
import { createUserAgent, Request, RequestOptions } from '@algolia/transporter';

import { AlgoliaSearchOptions } from '../types';

Expand Down Expand Up @@ -67,6 +68,7 @@ export default function algoliasearch(
searchForFacetValues: multipleSearchForFacetValues,
multipleQueries,
multipleSearchForFacetValues,
customRequest,
initIndex: base => (indexName: string): SearchIndex => {
return initIndex(base)(indexName, {
methods: { search, searchForFacetValues, findAnswers },
Expand Down Expand Up @@ -109,6 +111,10 @@ export type SearchClient = BaseSearchClient & {
}>,
requestOptions?: RequestOptions
) => Readonly<Promise<readonly SearchForFacetValuesResponse[]>>;
readonly customRequest: <TResponse>(
request: Request,
requestOptions?: RequestOptions
) => Readonly<Promise<TResponse>>;
};

export { WithoutCredentials, AlgoliaSearchOptions } from '../types';
8 changes: 7 additions & 1 deletion packages/algoliasearch/src/builds/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
copySettings,
copySynonyms,
createSearchClient,
customRequest,
deleteApiKey,
DeleteApiKeyResponse,
deleteBy,
Expand Down Expand Up @@ -192,7 +193,7 @@ import {
import { createNullLogger } from '@algolia/logger-common';
import { Destroyable } from '@algolia/requester-common';
import { createNodeHttpRequester } from '@algolia/requester-node-http';
import { createUserAgent, RequestOptions } from '@algolia/transporter';
import { createUserAgent, Request, RequestOptions } from '@algolia/transporter';

import { AlgoliaSearchOptions, InitAnalyticsOptions, InitPersonalizationOptions } from '../types';

Expand Down Expand Up @@ -276,6 +277,7 @@ export default function algoliasearch(
searchDictionaryEntries,
setDictionarySettings,
waitAppTask,
customRequest,
initIndex: base => (indexName: string): SearchIndex => {
return initIndex(base)(indexName, {
methods: {
Expand Down Expand Up @@ -685,6 +687,10 @@ export type SearchClient = BaseSearchClient & {
taskID: number,
requestOptions?: RequestOptions
) => Readonly<Promise<TaskStatusResponse>>;
readonly customRequest: <TResponse>(
request: Request,
requestOptions?: RequestOptions
) => Readonly<Promise<TResponse>>;
readonly initAnalytics: (options?: InitAnalyticsOptions) => AnalyticsClient;
readonly initPersonalization: (options?: InitPersonalizationOptions) => PersonalizationClient;
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createObjectNotFoundError, SearchResponse } from '../..';
import { createObjectNotFoundError, ListApiKeysResponse, SearchResponse } from '../..';
import { waitResponses } from '../../../../client-common/src/__tests__/helpers';
import { TestSuite } from '../../../../client-common/src/__tests__/TestSuite';

Expand Down Expand Up @@ -263,3 +263,9 @@ test('decompounding at query time', async () => {

expect(results.explain).toMatchObject(explain);
});

it('allows custom request', async () => {
const client = testSuite.makeSearchClient();
const res = await client.customRequest<ListApiKeysResponse>({ method: 'GET', path: '/1/keys' });
expect(res.keys.length).toBeGreaterThan(0);
});
14 changes: 14 additions & 0 deletions packages/client-search/src/methods/client/customRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MethodEnum } from '@algolia/requester-common';
import { Request, RequestOptions } from '@algolia/transporter';

import { SearchClient } from '../..';

export const customRequest = <TResponse = any>(base: SearchClient) => {
return (request: Request, requestOptions?: RequestOptions): Readonly<Promise<TResponse>> => {
if (request.method === MethodEnum.Get) {
return base.transporter.read(request, requestOptions);
}

return base.transporter.write(request, requestOptions);
};
};
1 change: 1 addition & 0 deletions packages/client-search/src/methods/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './copyIndex';
export * from './copyRules';
export * from './copySettings';
export * from './copySynonyms';
export * from './customRequest';
export * from './deleteApiKey';
export * from './deleteDictionaryEntries';
export * from './generateSecuredApiKey';
Expand Down

0 comments on commit 3cea192

Please sign in to comment.