Skip to content

Commit

Permalink
feat(connect): add create and destroy to storage apis
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Sep 7, 2023
1 parent dc5072e commit fbb7b18
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/connect/deno/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ export function connect(CONNECTION_STRING: string, domain = 'default'): Hyper {
.then(storage.remove(name))
.then($fetch)
.then(handleResponse),
create: () =>
Promise.resolve(h)
.then(storage.create())
.then($fetch)
.then(handleResponse),
destroy: (confirm) =>
Promise.resolve(h)
.then(storage.destroy(confirm))
.then($fetch)
.then(handleResponse),
},
queue: {
enqueue: (job) =>
Expand Down
7 changes: 7 additions & 0 deletions packages/connect/deno/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ export const signedUrl =

export const remove = (name: string) => (h: HyperRequestFunction) =>
h({ service, method: Method.DELETE, resource: name })

export const create = () => (hyper: HyperRequestFunction) => hyper({ service, method: Method.PUT })

export const destroy = (confirm?: boolean) => (hyper: HyperRequestFunction) =>
confirm
? hyper({ service, method: Method.DELETE })
: Promise.reject({ ok: false, msg: 'request not confirmed!' })
32 changes: 30 additions & 2 deletions packages/connect/deno/tests/storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HyperRequest } from '../types.ts'
import { assertEquals } from '../dev_deps.ts'
import { assert, assertEquals } from '../dev_deps.ts'

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

const test = Deno.test

Expand Down Expand Up @@ -99,3 +99,31 @@ test('storage.remove', async () => {
const req = await remove('avatar.png')(mockRequest)
assertEquals(req.url, 'http://localhost/storage/bucket/avatar.png')
})

test('storage.create', async () => {
const mockRequest = (h: HyperRequest) => {
assertEquals(h.service, 'storage')
assertEquals(h.method, 'PUT')
return Promise.resolve(new Request('http://localhost', { method: 'PUT' }))
}

await create()(mockRequest)
})

test('storage.destroy', async () => {
const mockRequest = (h: HyperRequest) => {
assertEquals(h.service, 'storage')
assertEquals(h.method, 'DELETE')
return Promise.resolve(
new Request('http://localhost', { method: 'DELETE' }),
)
}

await destroy(true)(mockRequest)

const noConfirmRequest = (_h: HyperRequest) => {
assert(false, 'unreachable')
}

await destroy()(noConfirmRequest).catch(assert)
})
2 changes: 2 additions & 0 deletions packages/connect/deno/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ export interface HyperStorage {
options: StorageSignedUrlOptions,
) => Promise<OkUrlResult | NotOkResult>
remove: (name: string) => Promise<Result>
create: () => Promise<IdResult>
destroy: (confirm: boolean) => Promise<Result>
}

export interface HyperQueue {
Expand Down

0 comments on commit fbb7b18

Please sign in to comment.