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(core): introduce metadata #774

Merged
merged 14 commits into from
Oct 19, 2021
Prev Previous commit
feat: pass extra options more generically
sarahdayan committed Oct 19, 2021
commit e76ddf04411fe1488dd0f8ec4a294607884b8f5e
5 changes: 2 additions & 3 deletions packages/autocomplete-core/src/__tests__/metadata.test.ts
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ describe('metadata', () => {
).content
)
).toEqual({
options: { aa_core: ['environment'], aa_js: [] },
options: { 'autocomplete-core': ['environment'] },
plugins: [],
ua: [{ segment: 'autocomplete-core', version }],
});
@@ -88,8 +88,7 @@ describe('metadata', () => {
).content
).options
).toEqual({
aa_core: ['openOnFocus', 'placeholder', 'environment'],
aa_js: [],
'autocomplete-core': ['openOnFocus', 'placeholder', 'environment'],
});
});

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

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

@@ -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) || []
4 changes: 2 additions & 2 deletions packages/autocomplete-js/src/__tests__/metadata.test.ts
Original file line number Diff line number Diff line change
@@ -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'],
});
});
});