Skip to content

Commit ffcfd89

Browse files
committed
refactor(connect): use fetch shim so as to not pass a Request object to fetch #566
1 parent 398f260 commit ffcfd89

File tree

4 files changed

+126
-37
lines changed

4 files changed

+126
-37
lines changed

packages/connect/deno/dev_deps.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export { assert, assertEquals } from 'https://deno.land/std@0.177.0/testing/asserts.ts'
1+
export {
2+
assert,
3+
assertEquals,
4+
assertObjectMatch,
5+
} from 'https://deno.land/std@0.178.0/testing/asserts.ts'

packages/connect/deno/mod.ts

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as search from './services/search.ts'
66
import * as info from './services/info.ts'
77
import * as storage from './services/storage.ts'
88
import * as queue from './services/queue.ts'
9-
import { hyper } from './utils/hyper-request.ts'
9+
import { fetchWithShim, hyper } from './utils/hyper-request.ts'
1010

1111
import type {
1212
HyperCache,
@@ -60,174 +60,183 @@ export function connect(CONNECTION_STRING: string, domain = 'default'): Hyper {
6060
.then((r) => (response.ok ? r : assoc('status', response.status, r)))
6161
.then((r) => (response.status >= 500 ? Promise.reject(r) : r))
6262

63+
/**
64+
* This is a shim to support https://github.com/hyper63/hyper/issues/566
65+
*/
66+
const $fetch = fetchWithShim(fetch)
67+
6368
return {
6469
data: {
6570
add: (body) =>
6671
Promise.resolve(h)
6772
.then(data.add(body))
68-
.then(fetch)
73+
.then($fetch)
6974
.then(handleResponse),
70-
get: (id) => Promise.resolve(h).then(data.get(id)).then(fetch).then(handleResponse),
75+
get: (id) => Promise.resolve(h).then(data.get(id)).then($fetch).then(handleResponse),
7176
list: (options) =>
7277
Promise.resolve(h)
7378
.then(data.list(options))
74-
.then(fetch)
79+
.then($fetch)
7580
.then(handleResponse),
7681
update: (id, doc) =>
7782
Promise.resolve(h)
7883
.then(data.update(id, doc))
79-
.then(fetch)
84+
.then($fetch)
8085
.then(handleResponse),
8186
remove: (id) =>
8287
Promise.resolve(h)
8388
.then(data.remove(id))
84-
.then(fetch)
89+
.then($fetch)
8590
.then(handleResponse),
8691
query: (selector, options) =>
8792
Promise.resolve(h)
8893
.then(data.query(selector, options))
89-
.then(fetch)
94+
.then($fetch)
9095
.then(handleResponse),
9196
bulk: (docs) =>
9297
Promise.resolve(h)
9398
.then(data.bulk(docs))
94-
.then(fetch)
99+
.then($fetch)
95100
.then(handleResponse),
96101
index: (indexName, fields) =>
97102
Promise.resolve(h)
98103
.then(data.index(indexName, fields))
99-
.then(fetch)
104+
.then($fetch)
105+
.then(handleResponse),
106+
create: () =>
107+
Promise.resolve(h)
108+
.then(data.create())
109+
.then($fetch)
100110
.then(handleResponse),
101-
create: () => Promise.resolve(h).then(data.create()).then(fetch).then(handleResponse),
102111
destroy: (confirm) =>
103112
Promise.resolve(h)
104113
.then(data.destroy(confirm))
105-
.then(fetch)
114+
.then($fetch)
106115
.then(handleResponse),
107116
},
108117
cache: {
109118
add: (key, value, ttl) =>
110119
Promise.resolve(h)
111120
.then(cache.add(key, value, ttl))
112-
.then(fetch)
121+
.then($fetch)
113122
.then(handleResponse),
114123
get: (key) =>
115124
Promise.resolve(h)
116125
.then(cache.get(key))
117-
.then(fetch)
126+
.then($fetch)
118127
.then(handleResponse),
119128
remove: (key) =>
120129
Promise.resolve(h)
121130
.then(cache.remove(key))
122-
.then(fetch)
131+
.then($fetch)
123132
.then(handleResponse),
124133
set: (key, value, ttl) =>
125134
Promise.resolve(h)
126135
.then(cache.set(key, value, ttl))
127-
.then(fetch)
136+
.then($fetch)
128137
.then(handleResponse),
129138
query: (pattern) =>
130139
Promise.resolve(h)
131140
.then(cache.query(pattern))
132-
.then(fetch)
141+
.then($fetch)
133142
.then(handleResponse),
134143
create: () =>
135144
Promise.resolve(h)
136145
.then(cache.create())
137-
.then(fetch)
146+
.then($fetch)
138147
.then(handleResponse),
139148
destroy: (confirm) =>
140149
Promise.resolve(h)
141150
.then(cache.destroy(confirm))
142-
.then(fetch)
151+
.then($fetch)
143152
.then(handleResponse),
144153
},
145154
search: {
146155
add: (key, doc) =>
147156
Promise.resolve(h)
148157
.then(search.add(key, doc))
149-
.then(fetch)
158+
.then($fetch)
150159
.then(handleResponse),
151160
remove: (key) =>
152161
Promise.resolve(h)
153162
.then(search.remove(key))
154-
.then(fetch)
163+
.then($fetch)
155164
.then(handleResponse),
156165
get: (key) =>
157166
Promise.resolve(h)
158167
.then(search.get(key))
159-
.then(fetch)
168+
.then($fetch)
160169
.then(handleResponse),
161170
update: (key, doc) =>
162171
Promise.resolve(h)
163172
.then(search.update(key, doc))
164-
.then(fetch)
173+
.then($fetch)
165174
.then(handleResponse),
166175
query: (query, options) =>
167176
Promise.resolve(h)
168177
.then(search.query(query, options))
169-
.then(fetch)
178+
.then($fetch)
170179
.then(handleResponse),
171180
load: (docs) =>
172181
Promise.resolve(h)
173182
.then(search.load(docs))
174-
.then(fetch)
183+
.then($fetch)
175184
.then(handleResponse),
176185
create: (fields, storeFields) =>
177186
Promise.resolve(h)
178187
.then(search.create(fields, storeFields))
179-
.then(fetch)
188+
.then($fetch)
180189
.then(handleResponse),
181190
destroy: (confirm) =>
182191
Promise.resolve(h)
183192
.then(search.destroy(confirm))
184-
.then(fetch)
193+
.then($fetch)
185194
.then(handleResponse),
186195
},
187196
storage: {
188197
upload: (name, data) =>
189198
Promise.resolve(h)
190199
.then(storage.upload(name, data))
191-
.then(fetch)
200+
.then($fetch)
192201
.then(handleResponse),
193202
download: (name) =>
194203
Promise.resolve(h)
195204
.then(storage.download(name))
196-
.then(fetch)
205+
.then($fetch)
197206
.then((res) => res.body as ReadableStream),
198207
signedUrl: (name, options) =>
199208
Promise.resolve(h)
200209
.then(storage.signedUrl(name, options))
201-
.then(fetch)
210+
.then($fetch)
202211
.then(handleResponse),
203212
remove: (name) =>
204213
Promise.resolve(h)
205214
.then(storage.remove(name))
206-
.then(fetch)
215+
.then($fetch)
207216
.then(handleResponse),
208217
},
209218
queue: {
210219
enqueue: (job) =>
211220
Promise.resolve(h)
212221
.then(queue.enqueue(job))
213-
.then(fetch)
222+
.then($fetch)
214223
.then(handleResponse),
215224
errors: () =>
216225
Promise.resolve(h)
217226
.then(queue.errors())
218-
.then(fetch)
227+
.then($fetch)
219228
.then(handleResponse),
220229
queued: () =>
221230
Promise.resolve(h)
222231
.then(queue.queued())
223-
.then(fetch)
232+
.then($fetch)
224233
.then(handleResponse),
225234
},
226235
info: {
227236
services: () =>
228237
Promise.resolve(h)
229238
.then(info.services())
230-
.then(fetch)
239+
.then($fetch)
231240
.then(handleResponse),
232241
},
233242
}

packages/connect/deno/tests/hyper-request.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { assert, assertEquals } from '../dev_deps.ts'
1+
import { assert, assertEquals, assertObjectMatch } from '../dev_deps.ts'
22

33
import { generateToken } from '../deps.deno.ts'
4-
import { hyper, HYPER_LEGACY_GET_HEADER } from '../utils/hyper-request.ts'
4+
import { fetchWithShim, hyper, HYPER_LEGACY_GET_HEADER } from '../utils/hyper-request.ts'
55
import { HyperRequest } from '../types.ts'
66

77
Deno.test('hyper-request', async (t) => {
@@ -18,6 +18,65 @@ Deno.test('hyper-request', async (t) => {
1818
})
1919
})
2020

21+
await t.step('fetchWithShim', async (t) => {
22+
const $fetch = fetchWithShim(
23+
// deno-lint-ignore require-await
24+
async (url, init) => {
25+
return new Response(init?.body, {
26+
headers: {
27+
...Object.fromEntries(init?.headers as Headers),
28+
url: url as string,
29+
method: init?.method as string,
30+
},
31+
})
32+
},
33+
)
34+
35+
await t.step(
36+
'should make the fetch call with the parts of the providedRequest object',
37+
async () => {
38+
await $fetch(
39+
new Request('https://foo.bar', {
40+
headers: {
41+
Authorization: 'Bearer foo',
42+
'Content-Type': 'application/json',
43+
},
44+
method: 'PUT',
45+
body: JSON.stringify({ foo: 'bar' }),
46+
}),
47+
)
48+
.then((res) => {
49+
const headers = res.headers
50+
assertEquals(headers.get('content-type'), 'application/json')
51+
assertEquals(headers.get('url'), 'https://foo.bar/')
52+
assertEquals(headers.get('method'), 'PUT')
53+
return res.json()
54+
})
55+
.then((body) => {
56+
assertObjectMatch(body, { foo: 'bar' })
57+
})
58+
},
59+
)
60+
61+
await t.step('should work with an empty body', async () => {
62+
await $fetch(
63+
new Request('https://foo.bar?fizz=buzz', {
64+
headers: {
65+
Authorization: 'Bearer foo',
66+
'Content-Type': 'application/json',
67+
},
68+
method: 'PUT',
69+
}),
70+
).then((res) => {
71+
const headers = res.headers
72+
assertEquals(headers.get('content-type'), 'application/json')
73+
assertEquals(headers.get('url'), 'https://foo.bar/?fizz=buzz')
74+
assertEquals(headers.get('method'), 'PUT')
75+
assertEquals(res.body, null)
76+
})
77+
})
78+
})
79+
2180
await t.step('hyper', async (t) => {
2281
const req: HyperRequest = {
2382
service: 'data',

packages/connect/deno/utils/hyper-request.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ interface HyperRequestParams {
1616

1717
export const HYPER_LEGACY_GET_HEADER = 'X-HYPER-LEGACY-GET'
1818

19+
/**
20+
* This is a shim to support https://github.com/hyper63/hyper/issues/566
21+
*
22+
* We take fetch, and a Request object and split the Request object into
23+
* it url and RequestInit pieces to pass to Request
24+
*/
25+
export const fetchWithShim = (f: typeof fetch) =>
26+
/**
27+
* Technically we should be able to pass the Request
28+
* as the RequestInit https://github.com/whatwg/fetch/issues/1486
29+
*
30+
* But I am honestly worried that wouldn't work when transpiled to Node,
31+
* so just manually build out the RequestInit for now.
32+
*/
33+
(req: Request): Promise<Response> =>
34+
f(req.url, { headers: req.headers, method: req.method, body: req.body })
35+
1936
export const hyper = (conn: URL, domain: string) =>
2037
async ({
2138
service,

0 commit comments

Comments
 (0)