Skip to content

Commit f0fd512

Browse files
committed
feat(connect): add create and destroy to storage apis
1 parent 05e444d commit f0fd512

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

packages/connect/deno/mod.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ export function connect(CONNECTION_STRING: string, domain = 'default'): Hyper {
214214
.then(storage.remove(name))
215215
.then($fetch)
216216
.then(handleResponse),
217+
create: () =>
218+
Promise.resolve(h)
219+
.then(storage.create())
220+
.then($fetch)
221+
.then(handleResponse),
222+
destroy: (confirm) =>
223+
Promise.resolve(h)
224+
.then(storage.destroy(confirm))
225+
.then($fetch)
226+
.then(handleResponse),
217227
},
218228
queue: {
219229
enqueue: (job) =>

packages/connect/deno/services/storage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ export const signedUrl =
6363

6464
export const remove = (name: string) => (h: HyperRequestFunction) =>
6565
h({ service, method: Method.DELETE, resource: name })
66+
67+
export const create = () => (hyper: HyperRequestFunction) => hyper({ service, method: Method.PUT })
68+
69+
export const destroy = (confirm?: boolean) => (hyper: HyperRequestFunction) =>
70+
confirm
71+
? hyper({ service, method: Method.DELETE })
72+
: Promise.reject({ ok: false, msg: 'request not confirmed!' })

packages/connect/deno/tests/storage.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HyperRequest } from '../types.ts'
2-
import { assertEquals } from '../dev_deps.ts'
2+
import { assert, assertEquals } from '../dev_deps.ts'
33

4-
import { download, remove, signedUrl, upload } from '../services/storage.ts'
4+
import { create, destroy, download, remove, signedUrl, upload } from '../services/storage.ts'
55

66
const test = Deno.test
77

@@ -99,3 +99,31 @@ test('storage.remove', async () => {
9999
const req = await remove('avatar.png')(mockRequest)
100100
assertEquals(req.url, 'http://localhost/storage/bucket/avatar.png')
101101
})
102+
103+
test('storage.create', async () => {
104+
const mockRequest = (h: HyperRequest) => {
105+
assertEquals(h.service, 'storage')
106+
assertEquals(h.method, 'PUT')
107+
return Promise.resolve(new Request('http://localhost', { method: 'PUT' }))
108+
}
109+
110+
await create()(mockRequest)
111+
})
112+
113+
test('storage.destroy', async () => {
114+
const mockRequest = (h: HyperRequest) => {
115+
assertEquals(h.service, 'storage')
116+
assertEquals(h.method, 'DELETE')
117+
return Promise.resolve(
118+
new Request('http://localhost', { method: 'DELETE' }),
119+
)
120+
}
121+
122+
await destroy(true)(mockRequest)
123+
124+
const noConfirmRequest = (_h: HyperRequest) => {
125+
assert(false, 'unreachable')
126+
}
127+
128+
await destroy()(noConfirmRequest).catch(assert)
129+
})

packages/connect/deno/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ export interface HyperStorage {
164164
options: StorageSignedUrlOptions,
165165
) => Promise<OkUrlResult | NotOkResult>
166166
remove: (name: string) => Promise<Result>
167+
create: () => Promise<IdResult>
168+
destroy: (confirm: boolean) => Promise<Result>
167169
}
168170

169171
export interface HyperQueue {

0 commit comments

Comments
 (0)