34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { parseQuery, stringifyQuery } from '@/core/serializer/query-serializer'
|
|
import { deepClone } from '@/shared/helpers/deep-clone'
|
|
import { parseUrl } from './serializer/url-serializer'
|
|
|
|
export function normalizeConfig(config?: FluentUrlConfig): FluentUrlConfig {
|
|
return {
|
|
parseQuery: config?.parseQuery ?? parseQuery,
|
|
stringifyQuery: config?.stringifyQuery ?? stringifyQuery,
|
|
}
|
|
}
|
|
|
|
export function normalizeOptions(args: {
|
|
urlOptions?: Partial<FLuentUrlPlainObject> | string
|
|
parseQuery: QuerySerializer['parseQuery']
|
|
}): FLuentUrlPlainObject {
|
|
const { urlOptions: urlOrOptions, parseQuery } = args
|
|
|
|
if (typeof urlOrOptions === 'string') {
|
|
return parseUrl({
|
|
str: urlOrOptions,
|
|
parseQuery: parseQuery,
|
|
})
|
|
}
|
|
|
|
return {
|
|
protocol: urlOrOptions?.protocol,
|
|
hostname: urlOrOptions?.hostname,
|
|
paths: deepClone(urlOrOptions?.paths) ?? [],
|
|
port: urlOrOptions?.port,
|
|
fragment: urlOrOptions?.fragment,
|
|
queries: deepClone(urlOrOptions?.queries) ?? Object.create(null),
|
|
}
|
|
}
|