Skip to content

Commit

Permalink
Change way to reject usage of custom plugin id with Vercel Analytics …
Browse files Browse the repository at this point in the history
…plugin
  • Loading branch information
slorber committed Feb 14, 2024
1 parent f85345b commit 42bb53b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-vercel-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"license": "MIT",
"dependencies": {
"@docusaurus/core": "3.0.0",
"@docusaurus/logger": "3.0.0",
"@docusaurus/types": "3.0.0",
"@docusaurus/utils-validation": "3.0.0",
"@docusaurus/utils": "3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ describe('validateOptions', () => {
expect(testValidateOptions(undefined)).toEqual(validationResult(undefined));
});

// TODO: fix this test
// it('throws for custom id', () => {
// const config: Options = {id: 'custom', mode: 'auto', debug: false};
// expect(() =>
// testValidateOptions(config),
// ).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`);
// });
it('throws for custom id', () => {
const config: Options = {id: 'custom', mode: 'auto', debug: false};
expect(() => testValidateOptions(config))
.toThrowErrorMatchingInlineSnapshot(`
"You site uses the Vercel Analytics plugin with a custom plugin id (custom).
But this plugin is only supposed to be used at most once per site. Therefore providing a custom plugin id is unsupported."
`);
});

it('accept for default id', () => {
const config: Options = {id: 'default', mode: 'auto', debug: false};
Expand Down
7 changes: 0 additions & 7 deletions packages/docusaurus-plugin-vercel-analytics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions, Options} from './options';

export default function pluginVercelAnalytics(
context: LoadContext,
options: PluginOptions,
): Plugin {
if (options.id !== DEFAULT_PLUGIN_ID) {
throw new Error(
`You cannot use a custom plugin id option with the Vercel Analytics plugin`,
);
}

const isProd = process.env.NODE_ENV === 'production';

return {
Expand Down
14 changes: 13 additions & 1 deletion packages/docusaurus-plugin-vercel-analytics/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
import {Joi} from '@docusaurus/utils-validation';
import type {OptionValidationContext} from '@docusaurus/types';

Expand All @@ -17,14 +18,25 @@ export type PluginOptions = {
export type Options = Partial<PluginOptions>;

const pluginOptionsSchema = Joi.object<PluginOptions>({
id: Joi.string().valid(DEFAULT_PLUGIN_ID).default(DEFAULT_PLUGIN_ID),
mode: Joi.string().valid('auto', 'production', 'development').optional(),
debug: Joi.boolean().optional(),
});

// We can't validate this through the schema
// Docusaurus core auto registers the id field to the schema already
function ensureNoMultiInstance(options: Options) {
if (options?.id && options.id !== DEFAULT_PLUGIN_ID) {
throw new Error(
logger.interpolate`You site uses the Vercel Analytics plugin with a custom plugin id (name=${options.id}).
But this plugin is only supposed to be used at most once per site. Therefore providing a custom plugin id is unsupported.`,
);
}
}

export function validateOptions({
validate,
options,
}: OptionValidationContext<Options, PluginOptions>): PluginOptions {
ensureNoMultiInstance(options);
return validate(pluginOptionsSchema, options);
}

0 comments on commit 42bb53b

Please sign in to comment.