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

feat(analytics-reco): allow creation of clients with their own credentials #1223

Merged
merged 2 commits into from Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/algoliasearch/src/__tests__/default.test.ts
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
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>;
Comment on lines +11 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if someone can find a cleaner way to implement this, I'm all ears


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
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