@smithy/core 3.3.3 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/dist-cjs/submodules/cbor/index.js +212 -2
  2. package/dist-cjs/submodules/protocols/index.js +667 -2
  3. package/dist-cjs/submodules/schema/index.js +771 -0
  4. package/dist-cjs/submodules/serde/index.js +680 -2
  5. package/dist-es/submodules/cbor/CborCodec.js +136 -0
  6. package/dist-es/submodules/cbor/SmithyRpcV2CborProtocol.js +74 -0
  7. package/dist-es/submodules/cbor/index.js +3 -1
  8. package/dist-es/submodules/cbor/parseCborBody.js +3 -2
  9. package/dist-es/submodules/protocols/HttpBindingProtocol.js +183 -0
  10. package/dist-es/submodules/protocols/HttpProtocol.js +164 -0
  11. package/dist-es/submodules/protocols/RpcProtocol.js +68 -0
  12. package/dist-es/submodules/protocols/index.js +7 -0
  13. package/dist-es/submodules/protocols/serde/FromStringShapeDeserializer.js +64 -0
  14. package/dist-es/submodules/protocols/serde/HttpInterceptingShapeDeserializer.js +38 -0
  15. package/dist-es/submodules/protocols/serde/HttpInterceptingShapeSerializer.js +30 -0
  16. package/dist-es/submodules/protocols/serde/ToStringShapeSerializer.js +87 -0
  17. package/dist-es/submodules/protocols/serde/determineTimestampFormat.js +20 -0
  18. package/dist-es/submodules/schema/TypeRegistry.js +49 -0
  19. package/dist-es/submodules/schema/deref.js +6 -0
  20. package/dist-es/submodules/schema/index.js +12 -0
  21. package/dist-es/submodules/schema/middleware/getSchemaSerdePlugin.js +23 -0
  22. package/dist-es/submodules/schema/middleware/schema-middleware-types.js +1 -0
  23. package/dist-es/submodules/schema/middleware/schemaDeserializationMiddleware.js +60 -0
  24. package/dist-es/submodules/schema/middleware/schemaSerializationMiddleware.js +16 -0
  25. package/dist-es/submodules/schema/schemas/ErrorSchema.js +17 -0
  26. package/dist-es/submodules/schema/schemas/ListSchema.js +15 -0
  27. package/dist-es/submodules/schema/schemas/MapSchema.js +16 -0
  28. package/dist-es/submodules/schema/schemas/NormalizedSchema.js +291 -0
  29. package/dist-es/submodules/schema/schemas/OperationSchema.js +16 -0
  30. package/dist-es/submodules/schema/schemas/Schema.js +6 -0
  31. package/dist-es/submodules/schema/schemas/SimpleSchema.js +15 -0
  32. package/dist-es/submodules/schema/schemas/StructureSchema.js +22 -0
  33. package/dist-es/submodules/schema/schemas/sentinels.js +16 -0
  34. package/dist-es/submodules/serde/copyDocumentWithTransform.js +53 -0
  35. package/dist-es/submodules/serde/date-utils.js +190 -0
  36. package/dist-es/submodules/serde/index.js +7 -0
  37. package/dist-es/submodules/serde/lazy-json.js +24 -0
  38. package/dist-es/submodules/serde/parse-utils.js +230 -0
  39. package/dist-es/submodules/serde/quote-header.js +6 -0
  40. package/dist-es/submodules/serde/split-every.js +27 -0
  41. package/dist-es/submodules/serde/split-header.js +37 -0
  42. package/dist-types/submodules/cbor/CborCodec.d.ts +29 -0
  43. package/dist-types/submodules/cbor/SmithyRpcV2CborProtocol.d.ts +22 -0
  44. package/dist-types/submodules/cbor/index.d.ts +3 -1
  45. package/dist-types/submodules/protocols/HttpBindingProtocol.d.ts +15 -0
  46. package/dist-types/submodules/protocols/HttpProtocol.d.ts +29 -0
  47. package/dist-types/submodules/protocols/RpcProtocol.d.ts +11 -0
  48. package/dist-types/submodules/protocols/index.d.ts +7 -0
  49. package/dist-types/submodules/protocols/serde/FromStringShapeDeserializer.d.ts +14 -0
  50. package/dist-types/submodules/protocols/serde/HttpInterceptingShapeDeserializer.d.ts +18 -0
  51. package/dist-types/submodules/protocols/serde/HttpInterceptingShapeSerializer.d.ts +20 -0
  52. package/dist-types/submodules/protocols/serde/ToStringShapeSerializer.d.ts +15 -0
  53. package/dist-types/submodules/protocols/serde/determineTimestampFormat.d.ts +9 -0
  54. package/dist-types/submodules/schema/TypeRegistry.d.ts +55 -0
  55. package/dist-types/submodules/schema/deref.d.ts +6 -0
  56. package/dist-types/submodules/schema/index.d.ts +12 -0
  57. package/dist-types/submodules/schema/middleware/getSchemaSerdePlugin.d.ts +14 -0
  58. package/dist-types/submodules/schema/middleware/schema-middleware-types.d.ts +8 -0
  59. package/dist-types/submodules/schema/middleware/schemaDeserializationMiddleware.d.ts +9 -0
  60. package/dist-types/submodules/schema/middleware/schemaSerializationMiddleware.d.ts +6 -0
  61. package/dist-types/submodules/schema/schemas/ErrorSchema.d.ts +38 -0
  62. package/dist-types/submodules/schema/schemas/ListSchema.d.ts +20 -0
  63. package/dist-types/submodules/schema/schemas/MapSchema.d.ts +25 -0
  64. package/dist-types/submodules/schema/schemas/NormalizedSchema.d.ts +122 -0
  65. package/dist-types/submodules/schema/schemas/OperationSchema.d.ts +20 -0
  66. package/dist-types/submodules/schema/schemas/Schema.d.ts +11 -0
  67. package/dist-types/submodules/schema/schemas/SimpleSchema.d.ts +20 -0
  68. package/dist-types/submodules/schema/schemas/StructureSchema.d.ts +21 -0
  69. package/dist-types/submodules/schema/schemas/sentinels.d.ts +21 -0
  70. package/dist-types/submodules/serde/copyDocumentWithTransform.d.ts +5 -0
  71. package/dist-types/submodules/serde/date-utils.d.ts +73 -0
  72. package/dist-types/submodules/serde/index.d.ts +7 -0
  73. package/dist-types/submodules/serde/lazy-json.d.ts +45 -0
  74. package/dist-types/submodules/serde/parse-utils.d.ts +270 -0
  75. package/dist-types/submodules/serde/quote-header.d.ts +6 -0
  76. package/dist-types/submodules/serde/split-every.d.ts +11 -0
  77. package/dist-types/submodules/serde/split-header.d.ts +5 -0
  78. package/dist-types/ts3.4/submodules/cbor/CborCodec.d.ts +29 -0
  79. package/dist-types/ts3.4/submodules/cbor/SmithyRpcV2CborProtocol.d.ts +22 -0
  80. package/dist-types/ts3.4/submodules/cbor/index.d.ts +3 -1
  81. package/dist-types/ts3.4/submodules/protocols/HttpBindingProtocol.d.ts +15 -0
  82. package/dist-types/ts3.4/submodules/protocols/HttpProtocol.d.ts +29 -0
  83. package/dist-types/ts3.4/submodules/protocols/RpcProtocol.d.ts +11 -0
  84. package/dist-types/ts3.4/submodules/protocols/index.d.ts +7 -0
  85. package/dist-types/ts3.4/submodules/protocols/serde/FromStringShapeDeserializer.d.ts +14 -0
  86. package/dist-types/ts3.4/submodules/protocols/serde/HttpInterceptingShapeDeserializer.d.ts +18 -0
  87. package/dist-types/ts3.4/submodules/protocols/serde/HttpInterceptingShapeSerializer.d.ts +20 -0
  88. package/dist-types/ts3.4/submodules/protocols/serde/ToStringShapeSerializer.d.ts +15 -0
  89. package/dist-types/ts3.4/submodules/protocols/serde/determineTimestampFormat.d.ts +9 -0
  90. package/dist-types/ts3.4/submodules/schema/TypeRegistry.d.ts +55 -0
  91. package/dist-types/ts3.4/submodules/schema/deref.d.ts +6 -0
  92. package/dist-types/ts3.4/submodules/schema/index.d.ts +12 -0
  93. package/dist-types/ts3.4/submodules/schema/middleware/getSchemaSerdePlugin.d.ts +14 -0
  94. package/dist-types/ts3.4/submodules/schema/middleware/schema-middleware-types.d.ts +11 -0
  95. package/dist-types/ts3.4/submodules/schema/middleware/schemaDeserializationMiddleware.d.ts +9 -0
  96. package/dist-types/ts3.4/submodules/schema/middleware/schemaSerializationMiddleware.d.ts +6 -0
  97. package/dist-types/ts3.4/submodules/schema/schemas/ErrorSchema.d.ts +38 -0
  98. package/dist-types/ts3.4/submodules/schema/schemas/ListSchema.d.ts +20 -0
  99. package/dist-types/ts3.4/submodules/schema/schemas/MapSchema.d.ts +25 -0
  100. package/dist-types/ts3.4/submodules/schema/schemas/NormalizedSchema.d.ts +125 -0
  101. package/dist-types/ts3.4/submodules/schema/schemas/OperationSchema.d.ts +20 -0
  102. package/dist-types/ts3.4/submodules/schema/schemas/Schema.d.ts +11 -0
  103. package/dist-types/ts3.4/submodules/schema/schemas/SimpleSchema.d.ts +20 -0
  104. package/dist-types/ts3.4/submodules/schema/schemas/StructureSchema.d.ts +24 -0
  105. package/dist-types/ts3.4/submodules/schema/schemas/sentinels.d.ts +21 -0
  106. package/dist-types/ts3.4/submodules/serde/copyDocumentWithTransform.d.ts +5 -0
  107. package/dist-types/ts3.4/submodules/serde/date-utils.d.ts +73 -0
  108. package/dist-types/ts3.4/submodules/serde/index.d.ts +7 -0
  109. package/dist-types/ts3.4/submodules/serde/lazy-json.d.ts +45 -0
  110. package/dist-types/ts3.4/submodules/serde/parse-utils.d.ts +270 -0
  111. package/dist-types/ts3.4/submodules/serde/quote-header.d.ts +6 -0
  112. package/dist-types/ts3.4/submodules/serde/split-every.d.ts +11 -0
  113. package/dist-types/ts3.4/submodules/serde/split-header.d.ts +5 -0
  114. package/package.json +18 -7
  115. package/schema.d.ts +7 -0
  116. package/schema.js +6 -0
@@ -0,0 +1,64 @@
1
+ import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
2
+ import { LazyJsonString, NumericValue, parseEpochTimestamp, parseRfc3339DateTimeWithOffset, parseRfc7231DateTime, splitHeader, } from "@smithy/core/serde";
3
+ import { fromBase64 } from "@smithy/util-base64";
4
+ import { toUtf8 } from "@smithy/util-utf8";
5
+ import { determineTimestampFormat } from "./determineTimestampFormat";
6
+ export class FromStringShapeDeserializer {
7
+ constructor(settings) {
8
+ this.settings = settings;
9
+ }
10
+ setSerdeContext(serdeContext) {
11
+ this.serdeContext = serdeContext;
12
+ }
13
+ read(_schema, data) {
14
+ const ns = NormalizedSchema.of(_schema);
15
+ if (ns.isListSchema()) {
16
+ return splitHeader(data).map((item) => this.read(ns.getValueSchema(), item));
17
+ }
18
+ if (ns.isBlobSchema()) {
19
+ return (this.serdeContext?.base64Decoder ?? fromBase64)(data);
20
+ }
21
+ if (ns.isTimestampSchema()) {
22
+ const format = determineTimestampFormat(ns, this.settings);
23
+ switch (format) {
24
+ case SCHEMA.TIMESTAMP_DATE_TIME:
25
+ return parseRfc3339DateTimeWithOffset(data);
26
+ case SCHEMA.TIMESTAMP_HTTP_DATE:
27
+ return parseRfc7231DateTime(data);
28
+ case SCHEMA.TIMESTAMP_EPOCH_SECONDS:
29
+ return parseEpochTimestamp(data);
30
+ default:
31
+ console.warn("Missing timestamp format, parsing value with Date constructor:", data);
32
+ return new Date(data);
33
+ }
34
+ }
35
+ if (ns.isStringSchema()) {
36
+ const mediaType = ns.getMergedTraits().mediaType;
37
+ let intermediateValue = data;
38
+ if (mediaType) {
39
+ if (ns.getMergedTraits().httpHeader) {
40
+ intermediateValue = this.base64ToUtf8(intermediateValue);
41
+ }
42
+ const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
43
+ if (isJson) {
44
+ intermediateValue = LazyJsonString.from(intermediateValue);
45
+ }
46
+ return intermediateValue;
47
+ }
48
+ }
49
+ switch (true) {
50
+ case ns.isNumericSchema():
51
+ return Number(data);
52
+ case ns.isBigIntegerSchema():
53
+ return BigInt(data);
54
+ case ns.isBigDecimalSchema():
55
+ return new NumericValue(data, "bigDecimal");
56
+ case ns.isBooleanSchema():
57
+ return String(data).toLowerCase() === "true";
58
+ }
59
+ return data;
60
+ }
61
+ base64ToUtf8(base64String) {
62
+ return (this.serdeContext?.utf8Encoder ?? toUtf8)((this.serdeContext?.base64Decoder ?? fromBase64)(base64String));
63
+ }
64
+ }
@@ -0,0 +1,38 @@
1
+ import { NormalizedSchema } from "@smithy/core/schema";
2
+ import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
3
+ import { FromStringShapeDeserializer } from "./FromStringShapeDeserializer";
4
+ export class HttpInterceptingShapeDeserializer {
5
+ constructor(codecDeserializer, codecSettings) {
6
+ this.codecDeserializer = codecDeserializer;
7
+ this.stringDeserializer = new FromStringShapeDeserializer(codecSettings);
8
+ }
9
+ setSerdeContext(serdeContext) {
10
+ this.stringDeserializer.setSerdeContext(serdeContext);
11
+ this.codecDeserializer.setSerdeContext(serdeContext);
12
+ this.serdeContext = serdeContext;
13
+ }
14
+ read(schema, data) {
15
+ const ns = NormalizedSchema.of(schema);
16
+ const traits = ns.getMergedTraits();
17
+ const toString = this.serdeContext?.utf8Encoder ?? toUtf8;
18
+ if (traits.httpHeader || traits.httpResponseCode) {
19
+ return this.stringDeserializer.read(ns, toString(data));
20
+ }
21
+ if (traits.httpPayload) {
22
+ if (ns.isBlobSchema()) {
23
+ const toBytes = this.serdeContext?.utf8Decoder ?? fromUtf8;
24
+ if (typeof data === "string") {
25
+ return toBytes(data);
26
+ }
27
+ return data;
28
+ }
29
+ else if (ns.isStringSchema()) {
30
+ if ("byteLength" in data) {
31
+ return toString(data);
32
+ }
33
+ return data;
34
+ }
35
+ }
36
+ return this.codecDeserializer.read(ns, data);
37
+ }
38
+ }
@@ -0,0 +1,30 @@
1
+ import { NormalizedSchema } from "@smithy/core/schema";
2
+ import { ToStringShapeSerializer } from "./ToStringShapeSerializer";
3
+ export class HttpInterceptingShapeSerializer {
4
+ constructor(codecSerializer, codecSettings, stringSerializer = new ToStringShapeSerializer(codecSettings)) {
5
+ this.codecSerializer = codecSerializer;
6
+ this.stringSerializer = stringSerializer;
7
+ }
8
+ setSerdeContext(serdeContext) {
9
+ this.codecSerializer.setSerdeContext(serdeContext);
10
+ this.stringSerializer.setSerdeContext(serdeContext);
11
+ }
12
+ write(schema, value) {
13
+ const ns = NormalizedSchema.of(schema);
14
+ const traits = ns.getMergedTraits();
15
+ if (traits.httpHeader || traits.httpLabel || traits.httpQuery) {
16
+ this.stringSerializer.write(ns, value);
17
+ this.buffer = this.stringSerializer.flush();
18
+ return;
19
+ }
20
+ return this.codecSerializer.write(ns, value);
21
+ }
22
+ flush() {
23
+ if (this.buffer !== undefined) {
24
+ const buffer = this.buffer;
25
+ this.buffer = undefined;
26
+ return buffer;
27
+ }
28
+ return this.codecSerializer.flush();
29
+ }
30
+ }
@@ -0,0 +1,87 @@
1
+ import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
2
+ import { dateToUtcString, LazyJsonString, quoteHeader } from "@smithy/core/serde";
3
+ import { toBase64 } from "@smithy/util-base64";
4
+ import { determineTimestampFormat } from "./determineTimestampFormat";
5
+ export class ToStringShapeSerializer {
6
+ constructor(settings) {
7
+ this.settings = settings;
8
+ this.stringBuffer = "";
9
+ this.serdeContext = undefined;
10
+ }
11
+ setSerdeContext(serdeContext) {
12
+ this.serdeContext = serdeContext;
13
+ }
14
+ write(schema, value) {
15
+ const ns = NormalizedSchema.of(schema);
16
+ switch (typeof value) {
17
+ case "object":
18
+ if (value === null) {
19
+ this.stringBuffer = "null";
20
+ return;
21
+ }
22
+ if (ns.isTimestampSchema()) {
23
+ if (!(value instanceof Date)) {
24
+ throw new Error(`@smithy/core/protocols - received non-Date value ${value} when schema expected Date in ${ns.getName(true)}`);
25
+ }
26
+ const format = determineTimestampFormat(ns, this.settings);
27
+ switch (format) {
28
+ case SCHEMA.TIMESTAMP_DATE_TIME:
29
+ this.stringBuffer = value.toISOString().replace(".000Z", "Z");
30
+ break;
31
+ case SCHEMA.TIMESTAMP_HTTP_DATE:
32
+ this.stringBuffer = dateToUtcString(value);
33
+ break;
34
+ case SCHEMA.TIMESTAMP_EPOCH_SECONDS:
35
+ this.stringBuffer = String(value.getTime() / 1000);
36
+ break;
37
+ default:
38
+ console.warn("Missing timestamp format, using epoch seconds", value);
39
+ this.stringBuffer = String(value.getTime() / 1000);
40
+ }
41
+ return;
42
+ }
43
+ if (ns.isBlobSchema() && "byteLength" in value) {
44
+ this.stringBuffer = (this.serdeContext?.base64Encoder ?? toBase64)(value);
45
+ return;
46
+ }
47
+ if (ns.isListSchema() && Array.isArray(value)) {
48
+ let buffer = "";
49
+ for (const item of value) {
50
+ this.write([ns.getValueSchema(), ns.getMergedTraits()], item);
51
+ const headerItem = this.flush();
52
+ const serialized = ns.getValueSchema().isTimestampSchema() ? headerItem : quoteHeader(headerItem);
53
+ if (buffer !== "") {
54
+ buffer += ", ";
55
+ }
56
+ buffer += serialized;
57
+ }
58
+ this.stringBuffer = buffer;
59
+ return;
60
+ }
61
+ this.stringBuffer = JSON.stringify(value, null, 2);
62
+ break;
63
+ case "string":
64
+ const mediaType = ns.getMergedTraits().mediaType;
65
+ let intermediateValue = value;
66
+ if (mediaType) {
67
+ const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
68
+ if (isJson) {
69
+ intermediateValue = LazyJsonString.from(intermediateValue);
70
+ }
71
+ if (ns.getMergedTraits().httpHeader) {
72
+ this.stringBuffer = (this.serdeContext?.base64Encoder ?? toBase64)(intermediateValue.toString());
73
+ return;
74
+ }
75
+ }
76
+ this.stringBuffer = value;
77
+ break;
78
+ default:
79
+ this.stringBuffer = String(value);
80
+ }
81
+ }
82
+ flush() {
83
+ const buffer = this.stringBuffer;
84
+ this.stringBuffer = "";
85
+ return buffer;
86
+ }
87
+ }
@@ -0,0 +1,20 @@
1
+ import { SCHEMA } from "@smithy/core/schema";
2
+ export function determineTimestampFormat(ns, settings) {
3
+ if (settings.timestampFormat.useTrait) {
4
+ if (ns.isTimestampSchema() &&
5
+ (ns.getSchema() === SCHEMA.TIMESTAMP_DATE_TIME ||
6
+ ns.getSchema() === SCHEMA.TIMESTAMP_HTTP_DATE ||
7
+ ns.getSchema() === SCHEMA.TIMESTAMP_EPOCH_SECONDS)) {
8
+ return ns.getSchema();
9
+ }
10
+ }
11
+ const { httpLabel, httpPrefixHeaders, httpHeader, httpQuery } = ns.getMergedTraits();
12
+ const bindingFormat = settings.httpBindings
13
+ ? typeof httpPrefixHeaders === "string" || Boolean(httpHeader)
14
+ ? SCHEMA.TIMESTAMP_HTTP_DATE
15
+ : Boolean(httpQuery) || Boolean(httpLabel)
16
+ ? SCHEMA.TIMESTAMP_DATE_TIME
17
+ : undefined
18
+ : undefined;
19
+ return bindingFormat ?? settings.timestampFormat.default;
20
+ }
@@ -0,0 +1,49 @@
1
+ export class TypeRegistry {
2
+ constructor(namespace, schemas = new Map()) {
3
+ this.namespace = namespace;
4
+ this.schemas = schemas;
5
+ }
6
+ static for(namespace) {
7
+ if (!TypeRegistry.registries.has(namespace)) {
8
+ TypeRegistry.registries.set(namespace, new TypeRegistry(namespace));
9
+ }
10
+ return TypeRegistry.registries.get(namespace);
11
+ }
12
+ register(shapeId, schema) {
13
+ const qualifiedName = this.normalizeShapeId(shapeId);
14
+ const registry = TypeRegistry.for(this.getNamespace(shapeId));
15
+ registry.schemas.set(qualifiedName, schema);
16
+ }
17
+ getSchema(shapeId) {
18
+ const id = this.normalizeShapeId(shapeId);
19
+ if (!this.schemas.has(id)) {
20
+ throw new Error(`@smithy/core/schema - schema not found for ${id}`);
21
+ }
22
+ return this.schemas.get(id);
23
+ }
24
+ getBaseException() {
25
+ for (const [id, schema] of this.schemas.entries()) {
26
+ if (id.startsWith("smithyts.client.synthetic.") && id.endsWith("ServiceException")) {
27
+ return schema;
28
+ }
29
+ }
30
+ return undefined;
31
+ }
32
+ find(predicate) {
33
+ return [...this.schemas.values()].find(predicate);
34
+ }
35
+ destroy() {
36
+ TypeRegistry.registries.delete(this.namespace);
37
+ this.schemas.clear();
38
+ }
39
+ normalizeShapeId(shapeId) {
40
+ if (shapeId.includes("#")) {
41
+ return shapeId;
42
+ }
43
+ return this.namespace + "#" + shapeId;
44
+ }
45
+ getNamespace(shapeId) {
46
+ return this.normalizeShapeId(shapeId).split("#")[0];
47
+ }
48
+ }
49
+ TypeRegistry.registries = new Map();
@@ -0,0 +1,6 @@
1
+ export const deref = (schemaRef) => {
2
+ if (typeof schemaRef === "function") {
3
+ return schemaRef();
4
+ }
5
+ return schemaRef;
6
+ };
@@ -0,0 +1,12 @@
1
+ export * from "./deref";
2
+ export * from "./middleware/getSchemaSerdePlugin";
3
+ export * from "./schemas/ListSchema";
4
+ export * from "./schemas/MapSchema";
5
+ export * from "./schemas/OperationSchema";
6
+ export * from "./schemas/ErrorSchema";
7
+ export * from "./schemas/NormalizedSchema";
8
+ export * from "./schemas/Schema";
9
+ export * from "./schemas/SimpleSchema";
10
+ export * from "./schemas/StructureSchema";
11
+ export * from "./schemas/sentinels";
12
+ export * from "./TypeRegistry";
@@ -0,0 +1,23 @@
1
+ import { schemaDeserializationMiddleware } from "./schemaDeserializationMiddleware";
2
+ import { schemaSerializationMiddleware } from "./schemaSerializationMiddleware";
3
+ export const deserializerMiddlewareOption = {
4
+ name: "deserializerMiddleware",
5
+ step: "deserialize",
6
+ tags: ["DESERIALIZER"],
7
+ override: true,
8
+ };
9
+ export const serializerMiddlewareOption = {
10
+ name: "serializerMiddleware",
11
+ step: "serialize",
12
+ tags: ["SERIALIZER"],
13
+ override: true,
14
+ };
15
+ export function getSchemaSerdePlugin(config) {
16
+ return {
17
+ applyToStack: (commandStack) => {
18
+ commandStack.add(schemaSerializationMiddleware(config), serializerMiddlewareOption);
19
+ commandStack.add(schemaDeserializationMiddleware(config), deserializerMiddlewareOption);
20
+ config.protocol.setSerdeContext(config);
21
+ },
22
+ };
23
+ }
@@ -0,0 +1,60 @@
1
+ import { HttpResponse } from "@smithy/protocol-http";
2
+ import { getSmithyContext } from "@smithy/util-middleware";
3
+ export const schemaDeserializationMiddleware = (config) => (next, context) => async (args) => {
4
+ const { response } = await next(args);
5
+ const { operationSchema } = getSmithyContext(context);
6
+ try {
7
+ const parsed = await config.protocol.deserializeResponse(operationSchema, {
8
+ ...config,
9
+ ...context,
10
+ }, response);
11
+ return {
12
+ response,
13
+ output: parsed,
14
+ };
15
+ }
16
+ catch (error) {
17
+ Object.defineProperty(error, "$response", {
18
+ value: response,
19
+ });
20
+ if (!("$metadata" in error)) {
21
+ const hint = `Deserialization error: to see the raw response, inspect the hidden field {error}.$response on this object.`;
22
+ try {
23
+ error.message += "\n " + hint;
24
+ }
25
+ catch (e) {
26
+ if (!context.logger || context.logger?.constructor?.name === "NoOpLogger") {
27
+ console.warn(hint);
28
+ }
29
+ else {
30
+ context.logger?.warn?.(hint);
31
+ }
32
+ }
33
+ if (typeof error.$responseBodyText !== "undefined") {
34
+ if (error.$response) {
35
+ error.$response.body = error.$responseBodyText;
36
+ }
37
+ }
38
+ try {
39
+ if (HttpResponse.isInstance(response)) {
40
+ const { headers = {} } = response;
41
+ const headerEntries = Object.entries(headers);
42
+ error.$metadata = {
43
+ httpStatusCode: response.statusCode,
44
+ requestId: findHeader(/^x-[\w-]+-request-?id$/, headerEntries),
45
+ extendedRequestId: findHeader(/^x-[\w-]+-id-2$/, headerEntries),
46
+ cfId: findHeader(/^x-[\w-]+-cf-id$/, headerEntries),
47
+ };
48
+ }
49
+ }
50
+ catch (e) {
51
+ }
52
+ }
53
+ throw error;
54
+ }
55
+ };
56
+ const findHeader = (pattern, headers) => {
57
+ return (headers.find(([k]) => {
58
+ return k.match(pattern);
59
+ }) || [void 0, void 1])[1];
60
+ };
@@ -0,0 +1,16 @@
1
+ import { getSmithyContext } from "@smithy/util-middleware";
2
+ export const schemaSerializationMiddleware = (config) => (next, context) => async (args) => {
3
+ const { operationSchema } = getSmithyContext(context);
4
+ const endpoint = context.endpointV2?.url && config.urlParser
5
+ ? async () => config.urlParser(context.endpointV2.url)
6
+ : config.endpoint;
7
+ const request = await config.protocol.serializeRequest(operationSchema, args.input, {
8
+ ...config,
9
+ ...context,
10
+ endpoint,
11
+ });
12
+ return next({
13
+ ...args,
14
+ request,
15
+ });
16
+ };
@@ -0,0 +1,17 @@
1
+ import { TypeRegistry } from "../TypeRegistry";
2
+ import { StructureSchema } from "./StructureSchema";
3
+ export class ErrorSchema extends StructureSchema {
4
+ constructor(name, traits, memberNames, memberList, ctor) {
5
+ super(name, traits, memberNames, memberList);
6
+ this.name = name;
7
+ this.traits = traits;
8
+ this.memberNames = memberNames;
9
+ this.memberList = memberList;
10
+ this.ctor = ctor;
11
+ }
12
+ }
13
+ export function error(namespace, name, traits = {}, memberNames, memberList, ctor) {
14
+ const schema = new ErrorSchema(namespace + "#" + name, traits, memberNames, memberList, ctor);
15
+ TypeRegistry.for(namespace).register(name, schema);
16
+ return schema;
17
+ }
@@ -0,0 +1,15 @@
1
+ import { TypeRegistry } from "../TypeRegistry";
2
+ import { Schema } from "./Schema";
3
+ export class ListSchema extends Schema {
4
+ constructor(name, traits, valueSchema) {
5
+ super(name, traits);
6
+ this.name = name;
7
+ this.traits = traits;
8
+ this.valueSchema = valueSchema;
9
+ }
10
+ }
11
+ export function list(namespace, name, traits = {}, valueSchema) {
12
+ const schema = new ListSchema(namespace + "#" + name, traits, typeof valueSchema === "function" ? valueSchema() : valueSchema);
13
+ TypeRegistry.for(namespace).register(name, schema);
14
+ return schema;
15
+ }
@@ -0,0 +1,16 @@
1
+ import { TypeRegistry } from "../TypeRegistry";
2
+ import { Schema } from "./Schema";
3
+ export class MapSchema extends Schema {
4
+ constructor(name, traits, keySchema, valueSchema) {
5
+ super(name, traits);
6
+ this.name = name;
7
+ this.traits = traits;
8
+ this.keySchema = keySchema;
9
+ this.valueSchema = valueSchema;
10
+ }
11
+ }
12
+ export function map(namespace, name, traits = {}, keySchema, valueSchema) {
13
+ const schema = new MapSchema(namespace + "#" + name, traits, keySchema, typeof valueSchema === "function" ? valueSchema() : valueSchema);
14
+ TypeRegistry.for(namespace).register(name, schema);
15
+ return schema;
16
+ }