deno.land / x / hono@v4.2.5 / client / utils.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { ObjectType } from './types.ts'
export const mergePath = (base: string, path: string) => { base = base.replace(/\/+$/, '') base = base + '/' path = path.replace(/^\/+/, '') return base + path}
export const replaceUrlParam = (urlString: string, params: Record<string, string>) => { for (const [k, v] of Object.entries(params)) { const reg = new RegExp('/:' + k + '(?:{[^/]+})?') urlString = urlString.replace(reg, `/${v}`) } return urlString}
export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') => { switch (protocol) { case 'ws': return urlString.replace(/^http/, 'ws') case 'http': return urlString.replace(/^ws/, 'http') }}
export const removeIndexString = (urlSting: string) => { if (/^https?:\/\/[^\/]+?\/index$/.test(urlSting)) { return urlSting.replace(/\/index$/, '/') } return urlSting.replace(/\/index$/, '')}
function isObject(item: unknown): item is ObjectType { return typeof item === 'object' && item !== null && !Array.isArray(item)}
export function deepMerge<T>(target: T, source: Record<string, unknown>): T { if (!isObject(target) && !isObject(source)) { return source as T } const merged = { ...target } as ObjectType<T>
for (const key in source) { const value = source[key] if (isObject(merged[key]) && isObject(value)) { merged[key] = deepMerge(merged[key], value) } else { merged[key] = value as T[keyof T] & T } }
return merged as T}
hono

Version Info

Tagged at
a month ago