Skip to content

Commit

Permalink
feat: pass extra options more generically
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan committed Oct 19, 2021
1 parent 80fe74c commit e76ddf0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
5 changes: 2 additions & 3 deletions packages/autocomplete-core/src/__tests__/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('metadata', () => {
).content
)
).toEqual({
options: { aa_core: ['environment'], aa_js: [] },
options: { 'autocomplete-core': ['environment'] },
plugins: [],
ua: [{ segment: 'autocomplete-core', version }],
});
Expand Down Expand Up @@ -88,8 +88,7 @@ describe('metadata', () => {
).content
).options
).toEqual({
aa_core: ['openOnFocus', 'placeholder', 'environment'],
aa_js: [],
'autocomplete-core': ['openOnFocus', 'placeholder', 'environment'],
});
});

Expand Down
24 changes: 16 additions & 8 deletions packages/autocomplete-core/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UserAgent, userAgents } from '@algolia/autocomplete-shared';

import {
AutocompleteEnvironment,
AutocompleteOptions,
AutocompleteOptionsWithMetadata,
AutocompletePlugin,
BaseItem,
Expand All @@ -12,10 +13,7 @@ type AutocompleteMetadata = {
name: string | undefined;
options: string[];
}>;
options: {
aa_core: string[];
aa_js: string[];
};
options: Record<string, string[]>;
ua: UserAgent[];
};

Expand All @@ -28,16 +26,26 @@ export function getMetadata<TItem extends BaseItem, TData = unknown>({
plugins,
options,
}: GetMetadataParams<TItem, TData>) {
const optionsKey = ((options.__autocomplete_metadata
?.userAgents as UserAgent[]) || [])[0]?.segment;

const extraOptions = optionsKey
? {
[optionsKey]: Object.keys(
(options.__autocomplete_metadata
?.options as AutocompleteOptions<TItem>) || {}
),
}
: {};

return {
plugins: plugins.map((plugin) => ({
name: plugin.name,
options: Object.keys(plugin.__autocomplete_pluginOptions || []),
})),
options: {
aa_core: Object.keys(options),
aa_js: Object.keys(
(options.__autocomplete_metadata?.options as any) || []
),
'autocomplete-core': Object.keys(options),
...extraOptions,
},
ua: userAgents.concat(
(options.__autocomplete_metadata?.userAgents as any) || []
Expand Down
4 changes: 2 additions & 2 deletions packages/autocomplete-js/src/__tests__/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ describe('metadata', () => {
).content
).options
).toEqual({
aa_core: [
'autocomplete-core': [
'id',
'getSources',
'environment',
'onStateChange',
'shouldPanelOpen',
'__autocomplete_metadata',
],
aa_js: ['id', 'container', 'getSources'],
'autocomplete-js': ['id', 'container', 'getSources'],
});
});
});

0 comments on commit e76ddf0

Please sign in to comment.