Skip to content

Commit

Permalink
feat: generic attachment handler
Browse files Browse the repository at this point in the history
Adds method getDataAsJson to Attachment data and therefore also to Attachment.data
This return the attachmnt data as JSON if .json or .base64 are present in attachmentdata.
Otherwise it throws an error.

TODO:
* Add unit test for base64 data
* Change all calls in the code to use the new method instead of decoding base64 in place

Co-authored-by: annelein <anneleinvanreijen@gmail.com>

Signed-off-by: morrieinmaas <moritz@animo.id>
  • Loading branch information
morrieinmaas committed Dec 14, 2021
1 parent aa49f99 commit 31cac90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/core/src/decorators/attachment/Attachment.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JsonTransformer } from '../..'

import { Attachment } from './Attachment'
import { Attachment, AttachmentData } from './Attachment'

const mockJson = {
'@id': 'ceffce22-6471-43e4-8945-b604091981c9',
Expand Down Expand Up @@ -29,6 +29,7 @@ const data = {
},
sha256: '00d7b2068a0b237f14a7979bbfc01ad62f60792e459467bfc4a7d3b9a6dbbe3e',
}
const dataInstance = new AttachmentData(data)

describe('Decorators | Attachment', () => {
it('should correctly transform Json to Attachment class', () => {
Expand All @@ -39,7 +40,7 @@ describe('Decorators | Attachment', () => {
expect(decorator.filename).toBe(mockJson.filename)
expect(decorator.lastmodTime).toEqual(mockJson.lastmod_time)
expect(decorator.byteCount).toEqual(mockJson.byte_count)
expect(decorator.data).toEqual(mockJson.data)
expect(decorator.data).toMatchObject(mockJson.data)
})

it('should correctly transform Attachment class to Json', () => {
Expand All @@ -50,7 +51,7 @@ describe('Decorators | Attachment', () => {
mimeType,
lastmodTime,
byteCount,
data,
data: dataInstance,
})

const json = JsonTransformer.toJSON(decorator)
Expand All @@ -64,6 +65,13 @@ describe('Decorators | Attachment', () => {
data,
}

expect(json).toEqual(transformed)
expect(json).toMatchObject(transformed)
})

it('should return the data correctly if only JSON exists', () => {
const decorator = JsonTransformer.fromJSON(mockJson, Attachment)

const gotData = decorator.data.getDataAsJson()
expect(decorator.data.json).toEqual(gotData)
})
})
12 changes: 12 additions & 0 deletions packages/core/src/decorators/attachment/Attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
ValidateNested,
} from 'class-validator'

import { AriesFrameworkError } from '../../error'
import { JsonEncoder } from '../../utils/JsonEncoder'
import { uuid } from '../../utils/uuid'

export interface AttachmentOptions {
Expand Down Expand Up @@ -45,6 +47,16 @@ export class AttachmentData {
}
}

public getDataAsJson = () => {
if (this.base64 !== null && typeof this.base64 === 'string') {
return JsonEncoder.fromBase64(this.base64)
} else if (this.json !== null) {
return this.json
} else {
throw new AriesFrameworkError('No data available.')
}
}

/**
* Base64-encoded data, when representing arbitrary content inline instead of via links. Optional.
*/
Expand Down

0 comments on commit 31cac90

Please sign in to comment.