Skip to content

Commit

Permalink
feat(analytics-reco): allow creation of clients with their own creden…
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv authored Nov 16, 2020
1 parent 8d962ea commit a7938b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
28 changes: 28 additions & 0 deletions packages/algoliasearch/src/__tests__/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,34 @@ describe('default preset', () => {
expect(recommendation.transporter.userAgent).toBe(client.transporter.userAgent);
});

test('allows clients to override credentials', () => {
const clientWithOptions = algoliasearch('appId', 'apiKey');

expect(clientWithOptions.transporter.headers['x-algolia-api-key']).toBe('apiKey');

const analytics = clientWithOptions.initAnalytics({
apiKey: 'analytics',
});
const recommendation = clientWithOptions.initRecommendation({
apiKey: 'recommendation',
});

expect(analytics.transporter.headers['x-algolia-api-key']).toBe('analytics');
expect(recommendation.transporter.headers['x-algolia-api-key']).toBe('recommendation');
});

test('allows clients to keep default credentials', () => {
const clientWithOptions = algoliasearch('appId', 'apiKey');

expect(clientWithOptions.transporter.headers['x-algolia-api-key']).toBe('apiKey');

const analytics = clientWithOptions.initAnalytics();
const recommendation = clientWithOptions.initRecommendation();

expect(analytics.transporter.headers['x-algolia-api-key']).toBe('apiKey');
expect(recommendation.transporter.headers['x-algolia-api-key']).toBe('apiKey');
});

it('can be destroyed', () => {
if (!testing.isBrowser()) {
expect(client).toHaveProperty('destroy');
Expand Down
15 changes: 12 additions & 3 deletions packages/algoliasearch/src/types/AlgoliaSearchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import { ClientTransporterOptions } from '@algolia/client-common';
import { RecommendationClientOptions } from '@algolia/client-recommendation';
import { SearchClientOptions } from '@algolia/client-search';

export type WithoutCredentials<TClient> = Omit<TClient, 'appId' | 'apiKey'>;
type Credentials = { readonly appId: string; readonly apiKey: string };
export type WithoutCredentials<TClientOptions extends Credentials> = Omit<
TClientOptions,
keyof Credentials
>;
export type OptionalCredentials<TClientOptions extends Credentials> = Omit<
TClientOptions,
keyof Credentials
> &
Pick<Partial<TClientOptions>, keyof Credentials>;

export type AlgoliaSearchOptions = Partial<ClientTransporterOptions> &
WithoutCredentials<SearchClientOptions>;

export type InitAnalyticsOptions = Partial<ClientTransporterOptions> &
WithoutCredentials<AnalyticsClientOptions>;
OptionalCredentials<AnalyticsClientOptions>;

export type InitRecommendationOptions = Partial<ClientTransporterOptions> &
WithoutCredentials<RecommendationClientOptions>;
OptionalCredentials<RecommendationClientOptions>;
2 changes: 1 addition & 1 deletion packages/client-common/src/__tests__/TestSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class TestSuite {
client.transporter.hosts = [client.transporter.hosts[2]];

// Also, since we are targeting always the same host, the
// server may take a litle more than expected to answer.
// server may take a little more than expected to answer.
// To avoid timeouts we increase the timeouts duration
// @ts-ignore
client.transporter.timeouts = {
Expand Down

0 comments on commit a7938b0

Please sign in to comment.