feat: move api-client as core's monorepo
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
53
packages/api-client/utils/index.ts
Normal file
53
packages/api-client/utils/index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { SortOrder } from '~/interfaces/options'
|
||||
|
||||
export const isPlainObject = (obj: any) =>
|
||||
isObject(obj) &&
|
||||
Object.prototype.toString.call(obj) === '[object Object]' &&
|
||||
Object.getPrototypeOf(obj) === Object.prototype
|
||||
|
||||
export const sortOrderToNumber = (order: SortOrder) => {
|
||||
return (
|
||||
{
|
||||
asc: 1,
|
||||
desc: -1,
|
||||
}[order] || 1
|
||||
)
|
||||
}
|
||||
const isObject = (obj: any) => obj && typeof obj === 'object'
|
||||
export const destructureData = (payload: any) => {
|
||||
if (typeof payload !== 'object') {
|
||||
return payload
|
||||
}
|
||||
if (payload === null) {
|
||||
return payload
|
||||
}
|
||||
|
||||
const data = payload.data
|
||||
|
||||
const dataIsPlainObject = isPlainObject(data)
|
||||
|
||||
if (dataIsPlainObject && Object.keys(payload).length === 1) {
|
||||
const d = Object.assign({}, data)
|
||||
// attach raw onto new data
|
||||
attachRawFromOneToAnthor(payload, d)
|
||||
return d
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
export const attachRawFromOneToAnthor = (from: any, to: any) => {
|
||||
if (!from || !isObject(to)) {
|
||||
return
|
||||
}
|
||||
from.$raw &&
|
||||
Object.defineProperty(to, '$raw', {
|
||||
value: { ...from.$raw },
|
||||
enumerable: false,
|
||||
})
|
||||
from.$request &&
|
||||
Object.defineProperty(to, '$request', {
|
||||
value: { ...from.$request },
|
||||
enumerable: false,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user