11import { parseXML } from "../deps.ts" ;
2- import type { SendMessageResponse } from "./types.ts" ;
2+ import type {
3+ SendMessageResponse ,
4+ ReceiveMessageResponse ,
5+ Message ,
6+ } from "./types.ts" ;
37import { SQSError } from "./error.ts" ;
48
59interface Document {
@@ -17,40 +21,69 @@ interface Xml {
1721}
1822
1923export function parseSendMessageResponse ( xml : string ) : SendMessageResponse {
20- const data : Document = parseXML ( xml ) ;
21- const { root } = data ;
22- if ( ! root || root . name !== "SendMessageResponse" ) {
23- throw new SQSError (
24- "Malformed sendMessage response. Missing SendMessageResponse field." ,
25- xml ,
26- ) ;
27- }
28- const sendMessageResult = root . children . find ( ( d ) =>
29- d . name === "SendMessageResult"
30- ) ;
31- if ( ! sendMessageResult ) {
24+ const doc : Document = parseXML ( xml ) ;
25+ const root = extractRoot ( doc , "SendMessageResponse" ) ;
26+ const sendMessageResult = extractField ( root , "SendMessageResult" ) ;
27+
28+ const messageID = extractContent ( sendMessageResult , "MessageId" ) ;
29+ const md5OfBody = extractContent ( sendMessageResult , "MD5OfMessageBody" ) ;
30+
31+ return { messageID, md5OfBody } ;
32+ }
33+
34+ export function parseReceiveMessageBody ( xml : string ) : ReceiveMessageResponse {
35+ const doc : Document = parseXML ( xml ) ;
36+ const root = extractRoot ( doc , "ReceiveMessageResponse" ) ;
37+ const receiveMessageResult = extractField ( root , "ReceiveMessageResult" ) ;
38+
39+ const messages = receiveMessageResult . children . map < Message > ( ( message ) => {
40+ if ( message . name !== "Message" ) {
41+ throw new SQSError (
42+ "Malformed field. Field type is not Message." ,
43+ JSON . stringify ( message , undefined , 2 ) ,
44+ ) ;
45+ }
46+
47+ const messageID = extractContent ( message , "MessageId" ) ;
48+ const receiptHandle = extractContent ( message , "ReceiptHandle" ) ;
49+ const md5OfBody = extractContent ( message , "MD5OfBody" ) ;
50+ const body = extractContent ( message , "Body" ) ;
51+
52+ return { messageID, md5OfBody, receiptHandle, body } ;
53+ } ) ;
54+
55+ return { messages } ;
56+ }
57+
58+ function extractRoot ( doc : Document , name : string ) : Xml {
59+ if ( ! doc . root || doc . root . name !== name ) {
3260 throw new SQSError (
33- " Malformed sendMessage response . Missing SendMessageResult field." ,
34- xml ,
61+ ` Malformed XML document . Missing ${ name } field.` ,
62+ JSON . stringify ( doc , undefined , 2 ) ,
3563 ) ;
3664 }
37- const messageIDField = sendMessageResult . children . find ( ( d ) =>
38- d . name === "MessageId"
39- ) ;
40- if ( ! messageIDField ) {
65+ return doc . root ;
66+ }
67+
68+ function extractField ( node : Xml , name : string ) : Xml {
69+ const bodyField = node . children . find ( ( node ) => node . name === name ) ;
70+ if ( ! bodyField ) {
4171 throw new SQSError (
42- "Malformed sendMessage response. Missing MessageId field." ,
43- xml ,
72+ `Missing ${ name } field in ${ node . name } node.` ,
73+ JSON . stringify ( node , undefined , 2 ) ,
4474 ) ;
4575 }
46- const messageID = messageIDField . content ;
47- if ( ! messageID ) {
76+ return bodyField ;
77+ }
78+
79+ function extractContent ( node : Xml , name : string ) : string {
80+ const field = extractField ( node , name ) ;
81+ const content = field . content ;
82+ if ( ! content ) {
4883 throw new SQSError (
49- "Malformed sendMessage response. Missing content in MessageId field." ,
50- xml ,
84+ ` Missing content in ${ node . name } node.` ,
85+ JSON . stringify ( node , undefined , 2 ) ,
5186 ) ;
5287 }
53- return {
54- messageID,
55- } ;
88+ return content ;
5689}
0 commit comments