-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
42 lines (38 loc) · 1.05 KB
/
utils.ts
File metadata and controls
42 lines (38 loc) · 1.05 KB
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
import type { DevupApiRequestInit } from '@devup-api/core'
export function isPlainObject(obj: unknown): obj is object {
if (obj === null || typeof obj !== 'object') return false
const proto = Object.getPrototypeOf(obj)
return proto === Object.prototype
}
export function getApiEndpoint(
baseUrl: string,
path: string,
params?: object,
): string {
let ret = `${baseUrl}${path}`
for (const [key, value] of Object.entries(params ?? {})) {
ret = ret.replace(`{${key}}`, value)
}
return ret
}
export function getQueryString(
query: NonNullable<DevupApiRequestInit['query']>,
): URLSearchParams {
if (typeof query === 'string') {
return new URLSearchParams(query)
}
if (isPlainObject(query)) {
const params = new URLSearchParams()
for (const [key, value] of Object.entries(query)) {
if (Array.isArray(value)) {
for (const v of value) {
params.append(key, String(v))
}
} else {
params.append(key, String(value))
}
}
return params
}
return new URLSearchParams(query)
}