Skip to content

Commit

Permalink
feat: support for writeHttpMetadata for R2 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Aug 27, 2023
1 parent 68da632 commit 61d928a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-jokes-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cf-bindings-proxy': minor
---

Support for `writeHttpMetadata` for R2.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ Note: Functionality and bindings not listed below may still work but have not be
#### R2

- [x] put
- [ ] writeHttpMetadata
- [x] writeHttpMetadata
- [x] get
- [ ] writeHttpMetadata
- [x] writeHttpMetadata
- [x] text
- [x] json
- [x] arrayBuffer
- [x] blob
- [ ] body
- [ ] bodyUsed
- [x] head
- [ ] writeHttpMetadata
- [x] writeHttpMetadata
- [x] list
- [ ] writeHttpMetadata
- [x] writeHttpMetadata
- [x] delete
- [ ] createMultipartUpload
- [ ] resumeMultipartUpload
15 changes: 15 additions & 0 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ const createResponseProxy = <T extends object>(

if (['toJSON'].includes(prop as string)) return data;

// special handling for `writeHttpMetadata`
if (prop === 'writeHttpMetadata' && data && typeof data === 'object') {
// @ts-expect-error - this is fine
const metadata = (data.httpMetadata || {}) as R2HTTPMetadata;
return (headers: Headers) => {
if (metadata.cacheControl) headers.set('cache-control', metadata.cacheControl);
if (metadata.cacheExpiry) headers.set('expires', metadata.cacheExpiry.toUTCString());
if (metadata.contentDisposition)
headers.set('content-disposition', metadata.contentDisposition);
if (metadata.contentEncoding) headers.set('content-encoding', metadata.contentEncoding);
if (metadata.contentLanguage) headers.set('content-language', metadata.contentLanguage);
if (metadata.contentType) headers.set('content-type', metadata.contentType);
};
}

// eslint-disable-next-line @typescript-eslint/no-use-before-define
const newProxy = createBindingProxy<BindingRequest>(bindingId, true);

Expand Down
14 changes: 13 additions & 1 deletion tests/proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ suite('bindings', () => {
});

suite('r2', () => {
// TODO: writeHttpMetadata, uploadPart: non-string
// TODO: uploadPart: non-string

test('put -> string', async () => {
const firstFile = await binding<R2Bucket>('R2').put('first-key', 'first-value', {
Expand Down Expand Up @@ -312,6 +312,18 @@ suite('bindings', () => {
expect(await blob?.text()).toEqual(JSON.stringify({ value: 'test' }));
});

test('get -> writeHttpMetadata', async () => {
await binding<R2Bucket>('R2').put('json-key', JSON.stringify({ value: 'test' }), {
httpMetadata: { contentType: 'application/json' },
});
const value = await binding<R2Bucket>('R2').get('json-key');

const headers = new Headers();
value?.writeHttpMetadata(headers);

expect(headers.get('content-type')).toEqual('application/json');
});

test('list', async () => {
const list = await binding<R2Bucket>('R2').list();

Expand Down

0 comments on commit 61d928a

Please sign in to comment.