@@ -2,6 +2,7 @@ import { Request, Response } from 'express';
22import { defaultTo , get } from 'lodash' ;
33import { parse as parseQueryString } from 'query-string' ;
44import streamToString from 'stream-to-string' ;
5+ import { StatementProcessingPriority } from '../../enums/statementProcessingPriority.enum' ;
56import InvalidContentType from '../../errors/InvalidContentType' ;
67import InvalidMethod from '../../errors/InvalidMethod' ;
78import parseJson from '../../utils/parseJson' ;
@@ -13,6 +14,7 @@ import getStatements from '../utils/getStatements';
1314import getUrlPath from '../utils/getUrlPath' ;
1415import storeStatement from '../utils/storeStatement' ;
1516import validateVersionHeader from '../utils/validateHeaderVersion' ;
17+ import { validateStatementProcessingPriority } from '../utils/validateStatementProcessingPriority' ;
1618import storeStatements from './storeStatements' ;
1719
1820export interface Options {
@@ -24,15 +26,16 @@ export interface Options {
2426
2527const checkContentType = ( bodyParams : any ) => {
2628 const contentType = get ( bodyParams , 'Content-Type' , 'application/json' ) ;
29+
2730 if ( ! jsonContentTypePattern . test ( contentType ) ) {
2831 throw new InvalidContentType ( contentType ) ;
2932 }
3033} ;
3134
3235const getBodyContent = ( bodyParams : any ) => {
3336 const unparsedBody = get ( bodyParams , 'content' , '' ) ;
34- const body = parseJson ( unparsedBody , [ 'body' , 'content' ] ) ;
35- return body ;
37+
38+ return parseJson ( unparsedBody , [ ' body' , 'content' ] ) ;
3639} ;
3740
3841const getHeader = ( bodyParams : any , req : Request , name : string ) : string => {
@@ -41,22 +44,31 @@ const getHeader = (bodyParams: any, req: Request, name: string): string => {
4144
4245const getBodyParams = async ( stream : NodeJS . ReadableStream ) => {
4346 const body = await streamToString ( stream ) ;
44- const decodedBody = parseQueryString ( body ) ;
45- return decodedBody ;
47+
48+ return parseQueryString ( body ) ;
4649} ;
4750
4851export default async ( { config, method, req, res } : Options ) => {
4952 checkUnknownParams ( req . query , [ 'method' ] ) ;
5053
54+ validateStatementProcessingPriority ( req . query . priority as string | undefined ) ;
55+ const priority =
56+ ( req . query . priority as StatementProcessingPriority ) || StatementProcessingPriority . MEDIUM ;
57+
5158 if ( method === 'POST' || ( method === undefined && config . allowUndefinedMethod ) ) {
5259 const bodyParams = await getBodyParams ( req ) ;
60+
5361 checkContentType ( bodyParams ) ;
62+
5463 const auth = getHeader ( bodyParams , req , 'Authorization' ) ;
5564 const client = await getClient ( config , auth ) ;
5665 const version = getHeader ( bodyParams , req , 'X-Experience-API-Version' ) ;
66+
5767 validateVersionHeader ( version ) ;
68+
5869 const body = getBodyContent ( bodyParams ) ;
59- return storeStatements ( { config, client, body, attachments : [ ] , res } ) ;
70+
71+ return storeStatements ( { config, client, priority, body, attachments : [ ] , res } ) ;
6072 }
6173
6274 if ( method === 'GET' ) {
@@ -65,22 +77,30 @@ export default async ({ config, method, req, res }: Options) => {
6577 const auth = getHeader ( bodyParams , req , 'Authorization' ) ;
6678 const client = await getClient ( config , auth ) ;
6779 const version = getHeader ( bodyParams , req , 'X-Experience-API-Version' ) ;
80+
6881 validateVersionHeader ( version ) ;
82+
6983 const acceptedLangs = defaultTo < string > ( req . header ( 'Accept-Language' ) , '' ) ;
7084 const queryParams = bodyParams ;
85+
7186 return getStatements ( { config, res, client, queryParams, urlPath, acceptedLangs } ) ;
7287 }
7388
7489 if ( method === 'PUT' ) {
7590 const bodyParams = await getBodyParams ( req ) ;
91+
7692 checkContentType ( bodyParams ) ;
93+
7794 const auth = getHeader ( bodyParams , req , 'Authorization' ) ;
7895 const client = await getClient ( config , auth ) ;
7996 const version = getHeader ( bodyParams , req , 'X-Experience-API-Version' ) ;
97+
8098 validateVersionHeader ( version ) ;
99+
81100 const body = getBodyContent ( bodyParams ) ;
82- const queryParams = bodyParams ;
83- return storeStatement ( { config, client, body, attachments : [ ] , queryParams, res } ) ;
101+ const statementId = bodyParams . statementId as string | undefined ;
102+
103+ return storeStatement ( { config, client, body, priority, attachments : [ ] , statementId, res } ) ;
84104 }
85105
86106 throw new InvalidMethod ( method ) ;
0 commit comments