Skip to content
Myr

fix(client): Allow calling toString and valueOf on the proxy object (#2510)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #2509 When a hono client is instantiated in expo and then console.log(client) is called, an error is thrown: `TypeError: Cannot determine default value of object` This is because the hermes javascript engine expects `new String(val)` to always return a string. But the proxy object would return a ClientRequestImpl object instead. This commit makes a string / function returned in the cases where .toString() and .valueOf() is called on the function.name or function respectively. Also see https://github.com/facebook/hermes/issues/205

authored by Islam Sharabash · committed by GitHub · parent 13c3156 · Browse files · 3 files+64 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+64 added−2 removed

  • tests:+24 added−0 removed
  • other:+40 added−2 removed

3 files+64 added−2 removed

modified:deno_dist/client/client.ts+20 added-1 removed
modified:src/client/client.test.ts+24 added
740 unmodified lines
741 client.index.$ws()742 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')743 })744})745746describe('Client can be console.log in react native', () => {747 it('Returns a function name with function.name.toString', async () => {748 const client = hc('http://localhost')749 // @ts-ignore750 expect(client.posts.name.toString()).toEqual('posts')751 })752753 it('Returns a function name with function.name.valueOf', async () => {754 const client = hc('http://localhost')755 // @ts-ignore756 expect(client.posts.name.valueOf()).toEqual('posts')757 })758759 it('Returns a function with function.valueOf', async () => {760 const client = hc('http://localhost')761 expect(typeof client.posts.valueOf()).toEqual('function')762 })763764 it('Returns a function source with function.toString', async () => {765 const client = hc('http://localhost')766 expect(client.posts.toString()).toMatch('function proxyCallback')767 })768})
modified:src/client/client.ts+20 added-1 removed
130 unmodified lines
131export const hc = <T extends Hono<any, any, any>>(132 baseUrl: string,133 options?: ClientRequestOptions134) =>135 createProxy((opts) => {135 createProxy(function proxyCallback(opts) {136 const parts = [...opts.path]137138 // allow calling .toString() and .valueOf() on the proxy139 if (parts[parts.length - 1] === 'toString') {140 if (parts[parts.length - 2] === 'name') {141 // e.g. hc().somePath.name.toString() -> "somePath"142 return parts[parts.length - 3] || ''143 }144 // e.g. hc().somePath.toString()145 return proxyCallback.toString()146 }147148 if (parts[parts.length - 1] === 'valueOf') {149 if (parts[parts.length - 2] === 'name') {150 // e.g. hc().somePath.name.valueOf() -> "somePath"151 return parts[parts.length - 3] || ''152 }153 // e.g. hc().somePath.valueOf()154 return proxyCallback155 }156157 let method = ''158 if (/^\$/.test(parts[parts.length - 1])) {159 const last = parts.pop()160 if (last) {
29 unmodified lines