This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c32b8e1
commit ec2beee
Showing
5 changed files
with
143 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** api attachment */ | ||
export interface api_attachment { | ||
/** filename */ | ||
filename: string; | ||
/** file type */ | ||
mime: string; | ||
/** file size */ | ||
size: number; | ||
/** image height */ | ||
height?: number; | ||
/** image width */ | ||
width?: number; | ||
/** file id */ | ||
id: string; | ||
} | ||
|
||
/** uploads class construction options */ | ||
export interface uploads_opts { | ||
/** base url for uploads */ | ||
base_url: string; | ||
/** an api token */ | ||
token: string; | ||
} | ||
|
||
/** upload types */ | ||
export enum upload_types { | ||
attachment = 'attachments', | ||
icon = 'icons', | ||
} | ||
|
||
/** access to meower uploads */ | ||
export class uploads { | ||
private opts: uploads_opts; | ||
|
||
constructor(opts: uploads_opts) { | ||
this.opts = opts; | ||
} | ||
|
||
/** upload a file */ | ||
async upload_file( | ||
file: Blob, | ||
upload_type: upload_types = upload_types.attachment, | ||
): Promise<api_attachment> { | ||
const form = new FormData(); | ||
form.append('file', file); | ||
const res = await fetch(`${this.opts.base_url}/${upload_type}`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${this.opts.token}`, | ||
}, | ||
body: form, | ||
}); | ||
if (!res.ok) { | ||
throw new Error('failed to upload file', { | ||
cause: await res.json(), | ||
}); | ||
} | ||
return await res.json(); | ||
} | ||
|
||
/** get the url for an attachment */ | ||
get_file_url( | ||
file: api_attachment, | ||
upload_type: upload_types = upload_types.attachment, | ||
): string { | ||
return `${this.opts.base_url}/${upload_type}/${file.id}/${file.filename}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { uploads } from '../src/api/uploads.ts'; | ||
import { assertEquals, assertRejects, mockFetch } from './internal/deps.ts'; | ||
|
||
Deno.test('attachment file url', () => { | ||
const u = new uploads({ | ||
base_url: 'http://localhost:8080', | ||
token: 'test', | ||
}); | ||
|
||
assertEquals( | ||
u.get_file_url({ | ||
filename: 'test.txt', | ||
id: '1234', | ||
mime: 'text/plain', | ||
size: 1234, | ||
}), | ||
'http://localhost:8080/attachments/1234/test.txt', | ||
); | ||
}); | ||
|
||
Deno.test('attachment upload', async (i) => { | ||
const u = new uploads({ | ||
base_url: 'http://localhost:8080', | ||
token: 'test', | ||
}); | ||
|
||
await i.step('successful', async () => { | ||
mockFetch('http://localhost:8080/attachments', { | ||
body: JSON.stringify({ | ||
filename: 'string', | ||
mime: 'string', | ||
size: 1, | ||
id: 'string', | ||
}), | ||
}); | ||
|
||
const file = new Blob(['test'], { type: 'text/plain' }); | ||
|
||
const res = await u.upload_file(file); | ||
|
||
assertEquals(res, { | ||
filename: 'string', | ||
mime: 'string', | ||
size: 1, | ||
id: 'string', | ||
}); | ||
}); | ||
|
||
await i.step('failure', async () => { | ||
mockFetch('http://localhost:8080/attachment', { | ||
status: 500, | ||
body: JSON.stringify({ | ||
error: true, | ||
message: 'notFound', | ||
}), | ||
}); | ||
|
||
const file = new Blob(['test'], { type: 'text/plain' }); | ||
|
||
await assertRejects(async () => { | ||
await u.upload_file(file); | ||
}); | ||
}); | ||
}); |