Skip to content

Commit

Permalink
feat: generic attachment handler
Browse files Browse the repository at this point in the history
fixed fct declaration and henceforth tests

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

Signed-off-by: morrieinmaas <moritz@animo.id>
  • Loading branch information
morrieinmaas committed Dec 15, 2021
1 parent 92acbb6 commit 5dd353b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 19 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/decorators/attachment/Attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export class AttachmentData {
}
}

public getDataAsJson = () => {
if (this.base64 !== null && typeof this.base64 === 'string') {
public getDataAsJson<T>(): T {
if (this.base64 && typeof this.base64 === 'string') {
return JsonEncoder.fromBase64(this.base64)
} else if (this.json !== null) {
return this.json
} else if (this.json) {
return this.json as T
} else {
throw new AriesFrameworkError('No data available.')
throw new AriesFrameworkError('No attachment data found in `json` or `base64` data fields.')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class IssueCredentialMessage extends AgentMessage {
const attachment = this.credentialAttachments.find((attachment) => attachment.id === INDY_CREDENTIAL_ATTACHMENT_ID)

// Extract credential from attachment
const credentialJson = attachment ? attachment.data.getDataAsJson() : null
const credentialJson = attachment?.data?.getDataAsJson()

return credentialJson
return credentialJson as Cred
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export class OfferCredentialMessage extends AgentMessage {
const attachment = this.offerAttachments.find((attachment) => attachment.id === INDY_CREDENTIAL_OFFER_ATTACHMENT_ID)

// Extract credential offer from attachment
const credentialOfferJson = attachment ? attachment.data.getDataAsJson() : null
const credentialOfferJson = attachment?.data?.getDataAsJson()

return credentialOfferJson
return credentialOfferJson as CredOffer
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class RequestCredentialMessage extends AgentMessage {
(attachment) => attachment.id === INDY_CREDENTIAL_REQUEST_ATTACHMENT_ID
)
// Extract proof request from attachment
const credentialReqJson = attachment ? attachment.data.getDataAsJson() : null
const credentialReqJson = attachment?.data?.getDataAsJson()

return credentialReqJson
return credentialReqJson as CredReq
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,8 @@ export class PresentationMessage extends AgentMessage {
public get indyProof(): IndyProof | null {
const attachment = this.presentationAttachments.find((attachment) => attachment.id === INDY_PROOF_ATTACHMENT_ID)

// Return null if attachment is not found
if (!attachment?.data?.base64) {
return null
}

const proofJson = JsonEncoder.fromBase64(attachment.data.base64)
const proofJson = attachment?.data?.getDataAsJson()

return proofJson
return proofJson as IndyProof
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class RequestPresentationMessage extends AgentMessage {
(attachment) => attachment.id === INDY_PROOF_REQUEST_ATTACHMENT_ID
)
// Extract proof request from attachment
const proofRequestJson = attachment ? attachment.data.getDataAsJson() : null
const proofRequestJson = attachment?.data?.getDataAsJson()
const proofRequest = JsonTransformer.fromJSON(proofRequestJson, ProofRequest)

return proofRequest
Expand Down

0 comments on commit 5dd353b

Please sign in to comment.