33 HttpInterceptingShapeDeserializer ,
44 HttpInterceptingShapeSerializer ,
55} from "@smithy/core/protocols" ;
6- import { ErrorSchema , NormalizedSchema , SCHEMA , TypeRegistry } from "@smithy/core/schema" ;
7- import {
6+ import { NormalizedSchema , SCHEMA } from "@smithy/core/schema" ;
7+ import type {
88 EndpointBearer ,
99 HandlerExecutionContext ,
1010 HttpRequest ,
@@ -15,8 +15,8 @@ import {
1515 ShapeDeserializer ,
1616 ShapeSerializer ,
1717} from "@smithy/types" ;
18- import { calculateBodyLength } from "@smithy/util-body-length-browser" ;
1918
19+ import { ProtocolLib } from "../ProtocolLib" ;
2020import { JsonCodec , JsonSettings } from "./JsonCodec" ;
2121import { loadRestJsonErrorCode } from "./parseJsonBody" ;
2222
@@ -27,6 +27,7 @@ export class AwsRestJsonProtocol extends HttpBindingProtocol {
2727 protected serializer : ShapeSerializer < string | Uint8Array > ;
2828 protected deserializer : ShapeDeserializer < string | Uint8Array > ;
2929 private readonly codec : JsonCodec ;
30+ private readonly mixin = new ProtocolLib ( ) ;
3031
3132 public constructor ( { defaultNamespace } : { defaultNamespace : string } ) {
3233 super ( {
@@ -65,32 +66,11 @@ export class AwsRestJsonProtocol extends HttpBindingProtocol {
6566 ) : Promise < HttpRequest > {
6667 const request = await super . serializeRequest ( operationSchema , input , context ) ;
6768 const inputSchema = NormalizedSchema . of ( operationSchema . input ) ;
68- const members = inputSchema . getMemberSchemas ( ) ;
6969
7070 if ( ! request . headers [ "content-type" ] ) {
71- const httpPayloadMember = Object . values ( members ) . find ( ( m ) => {
72- return ! ! m . getMergedTraits ( ) . httpPayload ;
73- } ) ;
74-
75- if ( httpPayloadMember ) {
76- const mediaType = httpPayloadMember . getMergedTraits ( ) . mediaType as string ;
77- if ( mediaType ) {
78- request . headers [ "content-type" ] = mediaType ;
79- } else if ( httpPayloadMember . isStringSchema ( ) ) {
80- request . headers [ "content-type" ] = "text/plain" ;
81- } else if ( httpPayloadMember . isBlobSchema ( ) ) {
82- request . headers [ "content-type" ] = "application/octet-stream" ;
83- } else {
84- request . headers [ "content-type" ] = this . getDefaultContentType ( ) ;
85- }
86- } else if ( ! inputSchema . isUnitSchema ( ) ) {
87- const hasBody = Object . values ( members ) . find ( ( m ) => {
88- const { httpQuery, httpQueryParams, httpHeader, httpLabel, httpPrefixHeaders } = m . getMergedTraits ( ) ;
89- return ! httpQuery && ! httpQueryParams && ! httpHeader && ! httpLabel && httpPrefixHeaders === void 0 ;
90- } ) ;
91- if ( hasBody ) {
92- request . headers [ "content-type" ] = this . getDefaultContentType ( ) ;
93- }
71+ const contentType = this . mixin . resolveRestContentType ( this . getDefaultContentType ( ) , inputSchema ) ;
72+ if ( contentType ) {
73+ request . headers [ "content-type" ] = contentType ;
9474 }
9575 }
9676
@@ -100,8 +80,7 @@ export class AwsRestJsonProtocol extends HttpBindingProtocol {
10080
10181 if ( request . body ) {
10282 try {
103- // todo(schema): use config.bodyLengthChecker or move that into serdeContext.
104- request . headers [ "content-length" ] = String ( calculateBodyLength ( request . body ) ) ;
83+ request . headers [ "content-length" ] = this . mixin . calculateContentLength ( request . body , this . serdeContext ) ;
10584 } catch ( e ) { }
10685 }
10786
@@ -117,33 +96,13 @@ export class AwsRestJsonProtocol extends HttpBindingProtocol {
11796 ) : Promise < never > {
11897 const errorIdentifier = loadRestJsonErrorCode ( response , dataObject ) ?? "Unknown" ;
11998
120- let namespace = this . options . defaultNamespace ;
121- let errorName = errorIdentifier ;
122- if ( errorIdentifier . includes ( "#" ) ) {
123- [ namespace , errorName ] = errorIdentifier . split ( "#" ) ;
124- }
125-
126- const errorMetadata = {
127- $metadata : metadata ,
128- $response : response ,
129- $fault : response . statusCode <= 500 ? ( "client" as const ) : ( "server" as const ) ,
130- } ;
131-
132- const registry = TypeRegistry . for ( namespace ) ;
133- let errorSchema : ErrorSchema ;
134- try {
135- errorSchema = registry . getSchema ( errorIdentifier ) as ErrorSchema ;
136- } catch ( e ) {
137- if ( dataObject . Message ) {
138- dataObject . message = dataObject . Message ;
139- }
140- const baseExceptionSchema = TypeRegistry . for ( "smithy.ts.sdk.synthetic." + namespace ) . getBaseException ( ) ;
141- if ( baseExceptionSchema ) {
142- const ErrorCtor = baseExceptionSchema . ctor ;
143- throw Object . assign ( new ErrorCtor ( { name : errorName } ) , errorMetadata , dataObject ) ;
144- }
145- throw Object . assign ( new Error ( errorName ) , errorMetadata , dataObject ) ;
146- }
99+ const { errorSchema, errorMetadata } = await this . mixin . getErrorSchemaOrThrowBaseException (
100+ errorIdentifier ,
101+ this . options . defaultNamespace ,
102+ response ,
103+ dataObject ,
104+ metadata
105+ ) ;
147106
148107 const ns = NormalizedSchema . of ( errorSchema ) ;
149108 const message = dataObject . message ?? dataObject . Message ?? "Unknown" ;
0 commit comments