Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion deno_dist/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,28 @@ export const hc = <T extends Hono<any, any, any>>(
baseUrl: string,
options?: ClientRequestOptions
) =>
createProxy((opts) => {
createProxy(function proxyCallback(opts) {
const parts = [...opts.path]

// allow calling .toString() and .valueOf() on the proxy
if (parts[parts.length - 1] === 'toString') {
if (parts[parts.length - 2] === 'name') {
// e.g. hc().somePath.name.toString() -> "somePath"
return parts[parts.length - 3] || ''
}
// e.g. hc().somePath.toString()
return proxyCallback.toString()
}

if (parts[parts.length - 1] === 'valueOf') {
if (parts[parts.length - 2] === 'name') {
// e.g. hc().somePath.name.valueOf() -> "somePath"
return parts[parts.length - 3] || ''
}
// e.g. hc().somePath.valueOf()
return proxyCallback
}

let method = ''
if (/^\$/.test(parts[parts.length - 1])) {
const last = parts.pop()
Expand Down
24 changes: 24 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,27 @@ describe('WebSocket URL Protocol Translation', () => {
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')
})
})

describe('Client can be console.log in react native', () => {
it('Returns a function name with function.name.toString', async () => {
const client = hc('http://localhost')
// @ts-ignore
expect(client.posts.name.toString()).toEqual('posts')
})

it('Returns a function name with function.name.valueOf', async () => {
const client = hc('http://localhost')
// @ts-ignore
expect(client.posts.name.valueOf()).toEqual('posts')
})

it('Returns a function with function.valueOf', async () => {
const client = hc('http://localhost')
expect(typeof client.posts.valueOf()).toEqual('function')
})

it('Returns a function source with function.toString', async () => {
const client = hc('http://localhost')
expect(client.posts.toString()).toMatch('function proxyCallback')
})
})
21 changes: 20 additions & 1 deletion src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,28 @@ export const hc = <T extends Hono<any, any, any>>(
baseUrl: string,
options?: ClientRequestOptions
) =>
createProxy((opts) => {
createProxy(function proxyCallback(opts) {
const parts = [...opts.path]

// allow calling .toString() and .valueOf() on the proxy
if (parts[parts.length - 1] === 'toString') {
if (parts[parts.length - 2] === 'name') {
// e.g. hc().somePath.name.toString() -> "somePath"
return parts[parts.length - 3] || ''
}
// e.g. hc().somePath.toString()
return proxyCallback.toString()
}

if (parts[parts.length - 1] === 'valueOf') {
if (parts[parts.length - 2] === 'name') {
// e.g. hc().somePath.name.valueOf() -> "somePath"
return parts[parts.length - 3] || ''
}
// e.g. hc().somePath.valueOf()
return proxyCallback
}

let method = ''
if (/^\$/.test(parts[parts.length - 1])) {
const last = parts.pop()
Expand Down