Skip to content

Instantly share code, notes, and snippets.

@ebeloded
Created April 2, 2021 02:36
Show Gist options
  • Save ebeloded/f4a79a3cdf17a38aca299abe7c70d328 to your computer and use it in GitHub Desktop.
Save ebeloded/f4a79a3cdf17a38aca299abe7c70d328 to your computer and use it in GitHub Desktop.
proxify
function proxify<T extends Object>(promise: Promise<T>): T {
const proxy = (path: Array<string> = []) =>
new Proxy(() => {}, {
get: (_target, prop: string) => proxy([...path, prop]),
apply: (_, _this, argumentsList) =>
promise.then((resolvedAPI) =>
path
.reduce<any>((p, c) => p[c], resolvedAPI)
.apply(_this, argumentsList),
),
})
return proxy()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment