Skip to content

Commit 12ab0f3

Browse files
authored
lib/route: Remove redundant implementations (#37224)
* Remove redundant implementations * Remove unused page-notifier
1 parent fc871dd commit 12ab0f3

File tree

9 files changed

+31
-140
lines changed

9 files changed

+31
-140
lines changed

client/lib/route/add-query-args.js renamed to client/lib/route/add-query-args.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
/** @format */
2-
31
/**
42
* External dependencies
53
*/
6-
74
import urlModule from 'url';
85
import { pickBy } from 'lodash';
6+
import { Primitive } from 'utility-types';
7+
8+
/**
9+
* Internal dependencies
10+
*/
11+
import { URL } from 'types';
12+
13+
interface QueryArgs {
14+
[ key: string ]: Primitive;
15+
}
916

10-
export default function( args, url ) {
17+
export default ( args: QueryArgs, url: URL ): URL => {
1118
if ( 'object' !== typeof args ) {
1219
throw new Error( 'addQueryArgs expects the first argument to be an object.' );
1320
}
@@ -33,4 +40,4 @@ export default function( args, url ) {
3340
} );
3441

3542
return urlModule.format( updatedUrlObject );
36-
}
43+
};

client/lib/route/index.ts

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,6 @@
1-
/**
2-
* External dependencies
3-
*/
4-
import page from 'page';
5-
import urlModule from 'url';
6-
import { pickBy } from 'lodash';
7-
import { Primitive } from 'utility-types';
8-
9-
/**
10-
* Internal dependencies
11-
*/
12-
import { URL } from 'types';
13-
141
export * from './path';
15-
16-
const appendQueryString = ( basepath: string, querystring: string ): string =>
17-
basepath + ( querystring ? '?' + querystring : '' );
18-
19-
interface QueryArgs {
20-
[ key: string ]: Primitive;
21-
}
22-
export const addQueryArgs = ( args: QueryArgs, url: URL ): URL => {
23-
if ( 'object' !== typeof args ) {
24-
throw new Error( 'addQueryArgs expects the first argument to be an object.' );
25-
}
26-
27-
if ( 'string' !== typeof url ) {
28-
throw new Error( 'addQueryArgs expects the second argument to be a string.' );
29-
}
30-
31-
// Remove any undefined query args
32-
args = pickBy( args, arg => arg != null );
33-
34-
// Build new query object for url
35-
const parsedUrl = urlModule.parse( url, true );
36-
let query = parsedUrl.query || {};
37-
query = Object.assign( query, args );
38-
39-
// Build new url object
40-
//
41-
// Note: we set search to false here to that our query object is processed
42-
const updatedUrlObject = Object.assign( parsedUrl, {
43-
query,
44-
search: false,
45-
} );
46-
47-
return urlModule.format( updatedUrlObject );
48-
};
49-
50-
export const trailingslashit = ( path: string ): string => path.replace( /(\/)?$/, '/' );
51-
52-
export const untrailingslashit = ( path: string ): string =>
53-
path === '/' ? path : path.replace( /\/$/, '' );
54-
55-
export const normalize: PageJS.Callback = ( context, next ) => {
56-
const normalizedPathName = untrailingslashit( context.pathname );
57-
if ( normalizedPathName !== context.pathname ) {
58-
page.redirect( appendQueryString( normalizedPathName, context.querystring ) );
59-
} else {
60-
next();
61-
}
62-
};
63-
64-
export function redirect( path: string ): void {
65-
if ( process.env.NODE_ENV === 'development' ) {
66-
throw 'route.redirect() is deprecated, use page.redirect()';
67-
}
68-
69-
// Have to wrap the page.replace call in a defer due to
70-
// https://github.com/visionmedia/page.js/issues/50
71-
setTimeout( function() {
72-
page.replace( path );
73-
}, 0 );
74-
}
2+
export { default as addQueryArgs } from './add-query-args';
3+
export { default as normalize } from './normalize';
4+
export { default as trailingslashit } from './trailingslashit';
5+
export { default as untrailingslashit } from './untrailingslashit';
6+
export { default as redirect } from './redirect';
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
/** @format */
1+
/**
2+
* External dependencies
3+
*/
4+
import page from 'page';
25

36
/**
47
* Internal dependencies
58
*/
6-
79
import untrailingslashit from './untrailingslashit';
8-
import page from 'page';
910

10-
function appendQueryString( basepath, querystring ) {
11-
return basepath + ( querystring ? '?' + querystring : '' );
12-
}
11+
const appendQueryString = ( basepath: string, querystring: string ): string =>
12+
basepath + ( querystring ? '?' + querystring : '' );
1313

14-
export default function normalize( context, next ) {
14+
const normalize: PageJS.Callback = ( context, next ) => {
1515
const normalizedPathName = untrailingslashit( context.pathname );
1616
if ( normalizedPathName !== context.pathname ) {
1717
page.redirect( appendQueryString( normalizedPathName, context.querystring ) );
1818
} else {
1919
next();
2020
}
21-
}
21+
};
22+
23+
export default normalize;

client/lib/route/page-notifier.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

client/lib/route/redirect.js renamed to client/lib/route/redirect.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/** @format */
21
/**
3-
* Internal dependencies
2+
* External dependencies
43
*/
54
import page from 'page';
65

7-
export default function redirect( path ) {
6+
export function redirect( path: string ): void {
87
if ( process.env.NODE_ENV === 'development' ) {
98
throw 'route.redirect() is deprecated, use page.redirect()';
109
}

client/lib/route/trailingslashit.js

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default ( path: string ): string => path.replace( /(\/)?$/, '/' );

client/lib/route/untrailingslashit.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default ( path: string ): string => ( path === '/' ? path : path.replace( /\/$/, '' ) );

0 commit comments

Comments
 (0)