diff --git a/.changeset/weak-jokes-guess.md b/.changeset/weak-jokes-guess.md new file mode 100644 index 0000000..107e010 --- /dev/null +++ b/.changeset/weak-jokes-guess.md @@ -0,0 +1,5 @@ +--- +'cf-bindings-proxy': minor +--- + +Support for `writeHttpMetadata` for R2. diff --git a/README.md b/README.md index 6ab6e85..71fb574 100644 --- a/README.md +++ b/README.md @@ -96,9 +96,9 @@ 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 @@ -106,9 +106,9 @@ Note: Functionality and bindings not listed below may still work but have not be - [ ] body - [ ] bodyUsed - [x] head - - [ ] writeHttpMetadata + - [x] writeHttpMetadata - [x] list - - [ ] writeHttpMetadata + - [x] writeHttpMetadata - [x] delete - [ ] createMultipartUpload - [ ] resumeMultipartUpload diff --git a/src/proxy.ts b/src/proxy.ts index 0470899..2908991 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -90,6 +90,21 @@ const createResponseProxy = ( 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(bindingId, true); diff --git a/tests/proxy.spec.ts b/tests/proxy.spec.ts index ff05277..f7eca92 100644 --- a/tests/proxy.spec.ts +++ b/tests/proxy.spec.ts @@ -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('R2').put('first-key', 'first-value', { @@ -312,6 +312,18 @@ suite('bindings', () => { expect(await blob?.text()).toEqual(JSON.stringify({ value: 'test' })); }); + test('get -> writeHttpMetadata', async () => { + await binding('R2').put('json-key', JSON.stringify({ value: 'test' }), { + httpMetadata: { contentType: 'application/json' }, + }); + const value = await binding('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('R2').list();