@docusaurus/plugin-client-redirects 2.0.0-beta.ff31de0ff → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/lib/collectRedirects.d.ts +2 -2
  2. package/lib/collectRedirects.js +45 -47
  3. package/lib/createRedirectPageContent.d.ts +2 -4
  4. package/lib/createRedirectPageContent.js +2 -4
  5. package/lib/extensionRedirects.d.ts +11 -3
  6. package/lib/extensionRedirects.js +22 -17
  7. package/lib/index.d.ts +5 -3
  8. package/lib/index.js +11 -8
  9. package/lib/options.d.ts +35 -0
  10. package/lib/options.js +36 -0
  11. package/lib/redirectValidation.d.ts +2 -2
  12. package/lib/types.d.ts +14 -16
  13. package/lib/writeRedirectFiles.d.ts +5 -5
  14. package/lib/writeRedirectFiles.js +41 -14
  15. package/package.json +16 -13
  16. package/src/collectRedirects.ts +76 -83
  17. package/src/createRedirectPageContent.ts +8 -10
  18. package/src/deps.d.ts +16 -0
  19. package/src/extensionRedirects.ts +32 -25
  20. package/src/index.ts +21 -14
  21. package/src/options.ts +75 -0
  22. package/src/redirectValidation.ts +3 -3
  23. package/src/types.ts +16 -30
  24. package/src/writeRedirectFiles.ts +53 -25
  25. package/lib/.tsbuildinfo +0 -1970
  26. package/lib/normalizePluginOptions.d.ts +0 -9
  27. package/lib/normalizePluginOptions.js +0 -44
  28. package/src/__tests__/__snapshots__/collectRedirects.test.ts.snap +0 -27
  29. package/src/__tests__/__snapshots__/createRedirectPageContent.test.ts.snap +0 -29
  30. package/src/__tests__/__snapshots__/normalizePluginOptions.test.ts.snap +0 -35
  31. package/src/__tests__/__snapshots__/redirectValidation.test.ts.snap +0 -11
  32. package/src/__tests__/__snapshots__/writeRedirectFiles.test.ts.snap +0 -71
  33. package/src/__tests__/collectRedirects.test.ts +0 -255
  34. package/src/__tests__/createRedirectPageContent.test.ts +0 -26
  35. package/src/__tests__/extensionRedirects.test.ts +0 -109
  36. package/src/__tests__/normalizePluginOptions.test.ts +0 -76
  37. package/src/__tests__/redirectValidation.test.ts +0 -66
  38. package/src/__tests__/writeRedirectFiles.test.ts +0 -146
  39. package/src/normalizePluginOptions.ts +0 -55
  40. package/tsconfig.json +0 -10
@@ -1,76 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import normalizePluginOptions, {
9
- DefaultPluginOptions,
10
- } from '../normalizePluginOptions';
11
- import {CreateRedirectsFnOption} from '../types';
12
-
13
- describe('normalizePluginOptions', () => {
14
- test('should return default options for undefined user options', () => {
15
- expect(normalizePluginOptions()).toEqual(DefaultPluginOptions);
16
- });
17
-
18
- test('should return default options for empty user options', () => {
19
- expect(normalizePluginOptions()).toEqual(DefaultPluginOptions);
20
- });
21
-
22
- test('should override one default options with valid user options', () => {
23
- expect(
24
- normalizePluginOptions({
25
- toExtensions: ['html'],
26
- }),
27
- ).toEqual({...DefaultPluginOptions, toExtensions: ['html']});
28
- });
29
-
30
- test('should override all default options with valid user options', () => {
31
- const createRedirects: CreateRedirectsFnOption = (_routePath: string) => {
32
- return [];
33
- };
34
- expect(
35
- normalizePluginOptions({
36
- fromExtensions: ['exe', 'zip'],
37
- toExtensions: ['html'],
38
- createRedirects,
39
- redirects: [{from: '/x', to: '/y'}],
40
- }),
41
- ).toEqual({
42
- id: 'default',
43
- fromExtensions: ['exe', 'zip'],
44
- toExtensions: ['html'],
45
- createRedirects,
46
- redirects: [{from: '/x', to: '/y'}],
47
- });
48
- });
49
-
50
- test('should reject bad fromExtensions user inputs', () => {
51
- expect(() =>
52
- normalizePluginOptions({
53
- fromExtensions: ([null, undefined, 123, true] as unknown) as string[],
54
- }),
55
- ).toThrowErrorMatchingSnapshot();
56
- });
57
-
58
- test('should reject bad toExtensions user inputs', () => {
59
- expect(() =>
60
- normalizePluginOptions({
61
- toExtensions: ([null, undefined, 123, true] as unknown) as string[],
62
- }),
63
- ).toThrowErrorMatchingSnapshot();
64
- });
65
-
66
- test('should reject bad createRedirects user inputs', () => {
67
- expect(() =>
68
- normalizePluginOptions({
69
- createRedirects: ([
70
- 'bad',
71
- 'value',
72
- ] as unknown) as CreateRedirectsFnOption,
73
- }),
74
- ).toThrowErrorMatchingSnapshot();
75
- });
76
- });
@@ -1,66 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import {validateRedirect} from '../redirectValidation';
9
-
10
- describe('validateRedirect', () => {
11
- test('validate good redirects without throwing', () => {
12
- validateRedirect({
13
- from: '/fromSomePath',
14
- to: '/toSomePath',
15
- });
16
- validateRedirect({
17
- from: '/from/Some/Path',
18
- to: '/toSomePath',
19
- });
20
- validateRedirect({
21
- from: '/fromSomePath',
22
- to: '/toSomePath',
23
- });
24
- validateRedirect({
25
- from: '/fromSomePath',
26
- to: '/to/Some/Path',
27
- });
28
- });
29
-
30
- test('throw for bad redirects', () => {
31
- expect(() =>
32
- validateRedirect({
33
- from: 'https://fb.com/fromSomePath',
34
- to: '/toSomePath',
35
- }),
36
- ).toThrowErrorMatchingSnapshot();
37
-
38
- expect(() =>
39
- validateRedirect({
40
- from: '/fromSomePath',
41
- to: 'https://fb.com/toSomePath',
42
- }),
43
- ).toThrowErrorMatchingSnapshot();
44
-
45
- expect(() =>
46
- validateRedirect({
47
- from: '/fromSomePath',
48
- to: '/toSomePath?queryString=xyz',
49
- }),
50
- ).toThrowErrorMatchingSnapshot();
51
-
52
- expect(() =>
53
- validateRedirect({
54
- from: (null as unknown) as string,
55
- to: '/toSomePath?queryString=xyz',
56
- }),
57
- ).toThrowErrorMatchingSnapshot();
58
-
59
- expect(() =>
60
- validateRedirect({
61
- from: (['heyho'] as unknown) as string,
62
- to: '/toSomePath?queryString=xyz',
63
- }),
64
- ).toThrowErrorMatchingSnapshot();
65
- });
66
- });
@@ -1,146 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- import fs from 'fs-extra';
8
- import path from 'path';
9
-
10
- import writeRedirectFiles, {
11
- toRedirectFilesMetadata,
12
- createToUrl,
13
- } from '../writeRedirectFiles';
14
-
15
- // Test to fix toUrl bugs we had:
16
- // - https://github.com/facebook/docusaurus/issues/3886
17
- // - https://github.com/facebook/docusaurus/issues/3925
18
- describe('createToUrl', () => {
19
- test('should create appropriate redirect urls', async () => {
20
- expect(createToUrl('/', '/docs/something/else')).toEqual(
21
- '/docs/something/else',
22
- );
23
- expect(createToUrl('/', '/docs/something/else/')).toEqual(
24
- '/docs/something/else/',
25
- );
26
- expect(createToUrl('/', 'docs/something/else')).toEqual(
27
- '/docs/something/else',
28
- );
29
- });
30
-
31
- test('should create appropriate redirect urls with baseUrl', async () => {
32
- expect(createToUrl('/baseUrl/', '/docs/something/else')).toEqual(
33
- '/baseUrl/docs/something/else',
34
- );
35
- expect(createToUrl('/baseUrl/', '/docs/something/else/')).toEqual(
36
- '/baseUrl/docs/something/else/',
37
- );
38
- expect(createToUrl('/baseUrl/', 'docs/something/else')).toEqual(
39
- '/baseUrl/docs/something/else',
40
- );
41
- });
42
- });
43
-
44
- describe('toRedirectFilesMetadata', () => {
45
- test('should create appropriate metadatas', async () => {
46
- const pluginContext = {
47
- outDir: '/tmp/someFixedOutDir',
48
- baseUrl: 'https://docusaurus.io',
49
- };
50
-
51
- const redirectFiles = toRedirectFilesMetadata(
52
- [
53
- {from: '/abc.html', to: '/abc'},
54
- {from: '/def', to: '/def.html'},
55
- {from: '/xyz', to: '/'},
56
- ],
57
- pluginContext,
58
- );
59
-
60
- expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
61
- path.join(pluginContext.outDir, '/abc.html/index.html'),
62
- path.join(pluginContext.outDir, '/def/index.html'),
63
- path.join(pluginContext.outDir, '/xyz/index.html'),
64
- ]);
65
-
66
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
67
- 'fileContent',
68
- );
69
- });
70
-
71
- test('should create appropriate metadatas for root baseUrl', async () => {
72
- const pluginContext = {
73
- outDir: '/tmp/someFixedOutDir',
74
- baseUrl: '/',
75
- };
76
- const redirectFiles = toRedirectFilesMetadata(
77
- [{from: '/abc.html', to: '/abc'}],
78
- pluginContext,
79
- );
80
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
81
- 'fileContent baseUrl=/',
82
- );
83
- });
84
-
85
- test('should create appropriate metadatas for empty baseUrl', async () => {
86
- const pluginContext = {
87
- outDir: '/tmp/someFixedOutDir',
88
- baseUrl: '',
89
- };
90
- const redirectFiles = toRedirectFilesMetadata(
91
- [{from: '/abc.html', to: '/abc'}],
92
- pluginContext,
93
- );
94
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
95
- 'fileContent baseUrl=empty',
96
- );
97
- });
98
- });
99
-
100
- describe('writeRedirectFiles', () => {
101
- test('write the files', async () => {
102
- const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
103
-
104
- const filesMetadata = [
105
- {
106
- fileAbsolutePath: path.join(outDir, 'someFileName'),
107
- fileContent: 'content 1',
108
- },
109
- {
110
- fileAbsolutePath: path.join(outDir, '/some/nested/filename'),
111
- fileContent: 'content 2',
112
- },
113
- ];
114
-
115
- await writeRedirectFiles(filesMetadata);
116
-
117
- await expect(
118
- fs.readFile(filesMetadata[0].fileAbsolutePath, 'utf8'),
119
- ).resolves.toEqual('content 1');
120
-
121
- await expect(
122
- fs.readFile(filesMetadata[1].fileAbsolutePath, 'utf8'),
123
- ).resolves.toEqual('content 2');
124
- });
125
-
126
- test('avoid overwriting existing files', async () => {
127
- const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
128
-
129
- const filesMetadata = [
130
- {
131
- fileAbsolutePath: path.join(outDir, 'someFileName/index.html'),
132
- fileContent: 'content 1',
133
- },
134
- ];
135
-
136
- await fs.ensureDir(path.dirname(filesMetadata[0].fileAbsolutePath));
137
- await fs.writeFile(
138
- filesMetadata[0].fileAbsolutePath,
139
- 'file already exists!',
140
- );
141
-
142
- await expect(writeRedirectFiles(filesMetadata)).rejects.toThrowError(
143
- `Redirect file creation error for path=${filesMetadata[0].fileAbsolutePath}: Error: The redirect plugin is not supposed to override existing files`,
144
- );
145
- });
146
- });
@@ -1,55 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import {PluginOptions, RedirectOption, UserPluginOptions} from './types';
9
- import {Joi, PathnameSchema} from '@docusaurus/utils-validation';
10
- import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
11
-
12
- export const DefaultPluginOptions: PluginOptions = {
13
- id: DEFAULT_PLUGIN_ID, // TODO temporary
14
- fromExtensions: [],
15
- toExtensions: [],
16
- redirects: [],
17
- };
18
-
19
- const RedirectPluginOptionValidation = Joi.object<RedirectOption>({
20
- to: PathnameSchema.required(),
21
- from: Joi.alternatives().try(
22
- PathnameSchema.required(),
23
- Joi.array().items(PathnameSchema.required()),
24
- ),
25
- });
26
-
27
- const isString = Joi.string().required().not(null);
28
-
29
- const UserOptionsSchema = Joi.object<UserPluginOptions>({
30
- id: Joi.string().optional(), // TODO remove once validation migrated to new system
31
- fromExtensions: Joi.array().items(isString),
32
- toExtensions: Joi.array().items(isString),
33
- redirects: Joi.array().items(RedirectPluginOptionValidation),
34
- createRedirects: Joi.function().arity(1),
35
- });
36
-
37
- function validateUserOptions(userOptions: UserPluginOptions) {
38
- const {error} = UserOptionsSchema.validate(userOptions, {
39
- abortEarly: true,
40
- allowUnknown: false,
41
- });
42
- if (error) {
43
- throw new Error(
44
- `Invalid @docusaurus/plugin-client-redirects options: ${error.message}
45
- ${JSON.stringify(userOptions, null, 2)}`,
46
- );
47
- }
48
- }
49
-
50
- export default function normalizePluginOptions(
51
- userPluginOptions: UserPluginOptions = {},
52
- ): PluginOptions {
53
- validateUserOptions(userPluginOptions);
54
- return {...DefaultPluginOptions, ...userPluginOptions};
55
- }
package/tsconfig.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "incremental": true,
5
- "tsBuildInfoFile": "./lib/.tsbuildinfo",
6
- "rootDir": "src",
7
- "outDir": "lib",
8
- "typeRoots": ["./types", "./node_modules/@types"]
9
- }
10
- }