Skip to content

Commit 2ec2d21

Browse files
authored
dataFormat is a mutable property (#892)
`dataFormat` is a mutable record property
1 parent 3782386 commit 2ec2d21

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

.changeset/fuzzy-apes-cheer.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@web5/api": patch
3+
---
4+
5+
dataFormat is a mutible record property

packages/api/src/record.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ export type RecordUpdateParams = {
140140
*/
141141
dataCid?: DwnMessageDescriptor[DwnInterface.RecordsWrite]['dataCid'];
142142

143+
/** The data format/MIME type of the supplied data */
144+
dataFormat?: string;
145+
143146
/** The size of the data in bytes. */
144147
dataSize?: DwnMessageDescriptor[DwnInterface.RecordsWrite]['dataSize'];
145148

@@ -155,6 +158,7 @@ export type RecordUpdateParams = {
155158
/** The published status of the record. */
156159
published?: DwnMessageDescriptor[DwnInterface.RecordsWrite]['published'];
157160

161+
158162
/** The tags associated with the updated record */
159163
tags?: DwnMessageDescriptor[DwnInterface.RecordsWrite]['tags'];
160164
}
@@ -730,7 +734,7 @@ export class Record implements RecordModel {
730734

731735
// Throw an error if an attempt is made to modify immutable properties.
732736
// Note: `data` and `dateModified` have already been handled.
733-
const mutableDescriptorProperties = new Set(['data', 'dataCid', 'dataSize', 'datePublished', 'messageTimestamp', 'published', 'tags']);
737+
const mutableDescriptorProperties = new Set(['data', 'dataCid', 'dataFormat', 'dataSize', 'datePublished', 'messageTimestamp', 'published', 'tags']);
734738
Record.verifyPermittedMutation(Object.keys(params), mutableDescriptorProperties);
735739

736740
// If `published` is set to false, ensure that `datePublished` is undefined. Otherwise, DWN SDK's schema validation

packages/api/tests/record.spec.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,7 @@ describe('Record', () => {
27562756

27572757
try {
27582758
// @ts-expect-error because this test intentionally specifies an immutable property that is not present in RecordUpdateOptions.
2759-
await record!.update({ dataFormat: 'application/json' });
2759+
await record!.update({ schema: 'bar/baz' });
27602760
expect.fail('Expected an exception to be thrown');
27612761
} catch (error: any) {
27622762
expect(error.message).to.include('is an immutable property. Its value cannot be changed.');
@@ -2876,6 +2876,36 @@ describe('Record', () => {
28762876
expect(updateResultWithNullTags.status.code).to.equal(202);
28772877
expect(record.tags).to.not.exist; // removed
28782878
});
2879+
2880+
it('should allow updating the dataFormat of a record', async () => {
2881+
// alice writes a record with the data format set to text/plain
2882+
const { status, record } = await dwnAlice.records.write({
2883+
data : 'Hello, world!',
2884+
message : {
2885+
protocol : protocolDefinition.protocol,
2886+
protocolPath : 'thread',
2887+
schema : protocolDefinition.types.thread.schema,
2888+
dataFormat : 'text/plain'
2889+
}
2890+
});
2891+
2892+
expect(status.code).to.equal(202);
2893+
expect(record).to.not.be.undefined;
2894+
expect(record.dataFormat).to.equal('text/plain');
2895+
expect(await record.data.text()).to.equal('Hello, world!');
2896+
2897+
// update the record to JSON
2898+
const updateResult = await record!.update({ dataFormat: 'application/json', data: { subject: 'some subject', body: 'some body' } });
2899+
expect(updateResult.status.code).to.equal(202);
2900+
expect(record.dataFormat).to.equal('application/json');
2901+
expect(await record.data.json()).to.deep.equal({ subject: 'some subject', body: 'some body' });
2902+
2903+
// update again without changing the dataFormat
2904+
const updateResult2 = await record!.update({ data: { subject: 'another subject', body: 'another body' } });
2905+
expect(updateResult2.status.code).to.equal(202);
2906+
expect(record.dataFormat).to.equal('application/json');
2907+
expect(await record.data.json()).to.deep.equal({ subject: 'another subject', body: 'another body' });
2908+
});
28792909
});
28802910

28812911
describe('delete()', () => {

0 commit comments

Comments
 (0)