Skip to content

Commit 76a3c50

Browse files
authored
fix(client): Support browsers without Array.prototype.at (#4057)
1 parent 882e75a commit 76a3c50

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Diff for: src/client/client.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,29 @@ export const hc = <T extends Hono<any, any, any>>(
125125
) =>
126126
createProxy(function proxyCallback(opts) {
127127
const parts = [...opts.path]
128+
const lastParts = parts.slice(-3).reverse()
128129

129130
// allow calling .toString() and .valueOf() on the proxy
130-
if (parts.at(-1) === 'toString') {
131-
if (parts.at(-2) === 'name') {
131+
if (lastParts[0] === 'toString') {
132+
if (lastParts[1] === 'name') {
132133
// e.g. hc().somePath.name.toString() -> "somePath"
133-
return parts.at(-3) || ''
134+
return lastParts[2] || ''
134135
}
135136
// e.g. hc().somePath.toString()
136137
return proxyCallback.toString()
137138
}
138139

139-
if (parts.at(-1) === 'valueOf') {
140-
if (parts.at(-2) === 'name') {
140+
if (lastParts[0] === 'valueOf') {
141+
if (lastParts[1] === 'name') {
141142
// e.g. hc().somePath.name.valueOf() -> "somePath"
142-
return parts.at(-3) || ''
143+
return lastParts[2] || ''
143144
}
144145
// e.g. hc().somePath.valueOf()
145146
return proxyCallback
146147
}
147148

148149
let method = ''
149-
if (/^\$/.test(parts.at(-1) as string)) {
150+
if (/^\$/.test(lastParts[0] as string)) {
150151
const last = parts.pop()
151152
if (last) {
152153
method = last.replace(/^\$/, '')

0 commit comments

Comments
 (0)