PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / utils / urls.ts

urls.ts in Code Snippets 4.0.0-beta.2, at js/utils/urls.ts

89 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { addQueryArgs } from '@wordpress/url'
2
3 export type UrlQueryArgs = Record<string, boolean | number | string | undefined | null>
4
5 export const fetchQueryParam = (name: string): string | undefined => {
6 const urlParams = new URLSearchParams(window.location.search)
7 return urlParams.get(name) ?? undefined
8 }
9
10 export const fetchConstQueryParam = <T extends string>(name: string, values: readonly T[]): T | undefined => {
11 const isT = (value: string): value is T =>
12 (<readonly string[]> values).includes(value)
13
14 const value = fetchQueryParam(name)
15 return value && isT(value) ? value : undefined
16 }
17
18 export const updateQueryParams = (params: Record<string, string | number | undefined>) => {
19 if ('URLSearchParams' in window) {
20 const searchParams = new URLSearchParams(window.location.search)
21
22 for (const [name, value] of Object.entries(params)) {
23 if (value) {
24 searchParams.set(name, String(value))
25 } else {
26 searchParams.delete(name)
27 }
28 }
29
30 const newUrl = window.location.toString().replace(window.location.search, `?${searchParams.toString()}`)
31 window.history.replaceState({}, document.title, newUrl)
32 }
33 }
34
35 const normaliseQueryArg = (value: unknown): string | undefined => {
36 switch (true) {
37 case value === undefined || null === value:
38 return undefined
39
40 case 'boolean' === typeof value:
41 return value ? '1' : '0'
42
43 case 'number' === typeof value:
44 case 'string' === typeof value:
45 return String(value)
46
47 default:
48 throw new Error(`Unsupported query arg type: ${typeof value}`)
49 }
50 }
51
52 export const buildAdminUrl = (
53 queryArgs?: UrlQueryArgs,
54 preserveQueryArgs: string[] = []
55 ) => {
56 const searchParams = new URLSearchParams(window.location.search)
57
58 if (queryArgs) {
59 for (const queryArgName of ['page', ...preserveQueryArgs]) {
60 const value = searchParams.get(queryArgName)
61 if (value && queryArgs[queryArgName] === undefined) {
62 queryArgs[queryArgName] = value
63 }
64 }
65 }
66
67 return buildUrl(
68 `${window.location.origin}${window.location.pathname}`,
69 { page: searchParams.get('page'), ...queryArgs }
70 )
71 }
72
73 export const buildUrl = (
74 base: string | undefined,
75 queryArgs: UrlQueryArgs
76 ): string => {
77 const processedArgs: Record<string, string> = {}
78
79 for (const [key, value] of Object.entries(queryArgs)) {
80 const processedArg = normaliseQueryArg(value)
81
82 if (processedArg !== undefined) {
83 processedArgs[key] = processedArg
84 }
85 }
86
87 return addQueryArgs(base, processedArgs)
88 }
89