fix(utils/proxy): accept nested options in destructure
This commit is contained in:
parent
74fef6a3e4
commit
e48e45226f
2 changed files with 40 additions and 20 deletions
17
lib/externals/src/react-native-clipboard.ts
vendored
17
lib/externals/src/react-native-clipboard.ts
vendored
|
|
@ -49,9 +49,14 @@ let ClipboardModule: typeof import('@react-native-clipboard/clipboard') =
|
|||
},
|
||||
)!
|
||||
|
||||
export const { useClipboard } = destructure(ClipboardModule, {
|
||||
hint: () => {},
|
||||
})
|
||||
export const { default: Clipboard } = destructure(ClipboardModule, {
|
||||
hint: {},
|
||||
})
|
||||
export const { default: Clipboard, useClipboard } = destructure(
|
||||
ClipboardModule,
|
||||
{
|
||||
default: {
|
||||
hint: {},
|
||||
},
|
||||
useClipboard: {
|
||||
hint: () => {},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -181,6 +181,17 @@ function unproxifyFromHint(hint: object) {
|
|||
return meta[0]() as any
|
||||
}
|
||||
|
||||
export type DestructureOptions<T extends object> = {
|
||||
[K in keyof T]?: ProxifyOptions
|
||||
}
|
||||
|
||||
export type DestructureResult<
|
||||
T extends object,
|
||||
O extends DestructureOptions<T>,
|
||||
> = {
|
||||
[K in keyof T]: O[K] extends ProxifyOptions ? T[K] : never
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructure a proxified value.
|
||||
*
|
||||
|
|
@ -208,25 +219,29 @@ function unproxifyFromHint(hint: object) {
|
|||
* z // TypeError: Cannot destructure and proxify null (reading 'z')
|
||||
* ```
|
||||
*/
|
||||
export function destructure<T extends object>(
|
||||
proxified: T,
|
||||
options?: ProxifyOptions,
|
||||
): T {
|
||||
export function destructure<
|
||||
T extends object,
|
||||
const O extends DestructureOptions<T>,
|
||||
>(proxified: T, options?: O): DestructureResult<T, O> {
|
||||
return new Proxy({} as T, {
|
||||
get: (_, p, r) =>
|
||||
proxify(() => {
|
||||
const v = Reflect.get(unproxify(proxified), p, r)
|
||||
proxify(
|
||||
() => {
|
||||
const v = Reflect.get(unproxify(proxified), p, r)
|
||||
|
||||
if (v == null)
|
||||
if (v == null)
|
||||
throw new TypeError(
|
||||
`Cannot destructure and proxify ${v} (reading '${String(p)}')`,
|
||||
)
|
||||
if (typeof v === 'function' || typeof v === 'object')
|
||||
return v
|
||||
throw new TypeError(
|
||||
`Cannot destructure and proxify ${v} (reading '${String(p)}')`,
|
||||
`Cannot destructure and proxify a primitive (reading '${String(p)}')`,
|
||||
)
|
||||
if (typeof v === 'function' || typeof v === 'object') return v
|
||||
throw new TypeError(
|
||||
`Cannot destructure and proxify a primitive (reading '${String(p)}')`,
|
||||
)
|
||||
}, options),
|
||||
})
|
||||
},
|
||||
options?.[p as keyof T],
|
||||
),
|
||||
}) as DestructureResult<T, O>
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue