@azure/core-client 1.8.1-alpha.20240202.3 → 1.8.1-alpha.20240223.2

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 (281) hide show
  1. package/README.md +0 -4
  2. package/dist/browser/authorizeRequestOnClaimChallenge.d.ts +43 -0
  3. package/dist/browser/authorizeRequestOnClaimChallenge.d.ts.map +1 -0
  4. package/{dist-esm/src → dist/browser}/authorizeRequestOnClaimChallenge.js +3 -3
  5. package/dist/browser/authorizeRequestOnClaimChallenge.js.map +1 -0
  6. package/dist/browser/authorizeRequestOnTenantChallenge.d.ts +8 -0
  7. package/dist/browser/authorizeRequestOnTenantChallenge.d.ts.map +1 -0
  8. package/{dist-esm/src → dist/browser}/authorizeRequestOnTenantChallenge.js +5 -2
  9. package/dist/browser/authorizeRequestOnTenantChallenge.js.map +1 -0
  10. package/dist/browser/base64-browser.d.mts.map +1 -0
  11. package/dist/browser/base64-browser.mjs.map +1 -0
  12. package/dist/browser/base64.d.ts +29 -0
  13. package/{dist-esm/src/base64.browser.js → dist/browser/base64.js} +5 -1
  14. package/dist/browser/deserializationPolicy.d.ts +45 -0
  15. package/dist/browser/deserializationPolicy.d.ts.map +1 -0
  16. package/{dist-esm/src → dist/browser}/deserializationPolicy.js +16 -19
  17. package/dist/browser/deserializationPolicy.js.map +1 -0
  18. package/dist/browser/httpClientCache.d.ts +3 -0
  19. package/dist/browser/httpClientCache.d.ts.map +1 -0
  20. package/dist/browser/index.d.ts +9 -0
  21. package/dist/browser/index.d.ts.map +1 -0
  22. package/dist/browser/index.js +11 -0
  23. package/dist/browser/index.js.map +1 -0
  24. package/dist/browser/interfaceHelpers.d.ts +14 -0
  25. package/dist/browser/interfaceHelpers.d.ts.map +1 -0
  26. package/{dist-esm/src → dist/browser}/interfaceHelpers.js +1 -1
  27. package/dist/browser/interfaceHelpers.js.map +1 -0
  28. package/dist/browser/interfaces.d.ts +664 -0
  29. package/dist/browser/interfaces.d.ts.map +1 -0
  30. package/dist/browser/log.d.ts +2 -0
  31. package/dist/browser/log.d.ts.map +1 -0
  32. package/dist/browser/operationHelpers.d.ts +14 -0
  33. package/dist/browser/operationHelpers.d.ts.map +1 -0
  34. package/dist/browser/operationHelpers.js.map +1 -0
  35. package/dist/browser/package.json +3 -0
  36. package/dist/browser/pipeline.d.ts +34 -0
  37. package/dist/browser/pipeline.d.ts.map +1 -0
  38. package/{dist-esm/src → dist/browser}/pipeline.js +3 -3
  39. package/dist/browser/pipeline.js.map +1 -0
  40. package/dist/browser/serializationPolicy.d.ts +33 -0
  41. package/dist/browser/serializationPolicy.d.ts.map +1 -0
  42. package/{dist-esm/src → dist/browser}/serializationPolicy.js +12 -14
  43. package/dist/browser/serializationPolicy.js.map +1 -0
  44. package/dist/browser/serializer.d.ts +31 -0
  45. package/dist/browser/serializer.d.ts.map +1 -0
  46. package/{dist-esm/src → dist/browser}/serializer.js +25 -26
  47. package/dist/browser/serializer.js.map +1 -0
  48. package/dist/browser/serviceClient.d.ts +82 -0
  49. package/dist/browser/serviceClient.d.ts.map +1 -0
  50. package/{dist-esm/src → dist/browser}/serviceClient.js +38 -14
  51. package/dist/browser/serviceClient.js.map +1 -0
  52. package/dist/browser/urlHelpers.d.ts +7 -0
  53. package/dist/browser/urlHelpers.d.ts.map +1 -0
  54. package/{dist-esm/src → dist/browser}/urlHelpers.js +4 -6
  55. package/dist/browser/urlHelpers.js.map +1 -0
  56. package/dist/browser/utils.d.ts +37 -0
  57. package/dist/browser/utils.d.ts.map +1 -0
  58. package/{dist-esm/src → dist/browser}/utils.js +23 -10
  59. package/dist/browser/utils.js.map +1 -0
  60. package/dist/commonjs/authorizeRequestOnClaimChallenge.d.ts +43 -0
  61. package/dist/commonjs/authorizeRequestOnClaimChallenge.d.ts.map +1 -0
  62. package/dist/commonjs/authorizeRequestOnClaimChallenge.js +74 -0
  63. package/dist/commonjs/authorizeRequestOnClaimChallenge.js.map +1 -0
  64. package/dist/commonjs/authorizeRequestOnTenantChallenge.d.ts +8 -0
  65. package/dist/commonjs/authorizeRequestOnTenantChallenge.d.ts.map +1 -0
  66. package/dist/commonjs/authorizeRequestOnTenantChallenge.js +106 -0
  67. package/dist/commonjs/authorizeRequestOnTenantChallenge.js.map +1 -0
  68. package/dist/commonjs/base64.d.ts +25 -0
  69. package/dist/commonjs/base64.d.ts.map +1 -0
  70. package/dist/commonjs/base64.js +43 -0
  71. package/dist/commonjs/base64.js.map +1 -0
  72. package/dist/commonjs/deserializationPolicy.d.ts +45 -0
  73. package/dist/commonjs/deserializationPolicy.d.ts.map +1 -0
  74. package/dist/commonjs/deserializationPolicy.js +232 -0
  75. package/dist/commonjs/deserializationPolicy.js.map +1 -0
  76. package/dist/commonjs/httpClientCache.d.ts +3 -0
  77. package/dist/commonjs/httpClientCache.d.ts.map +1 -0
  78. package/dist/commonjs/httpClientCache.js +15 -0
  79. package/dist/commonjs/httpClientCache.js.map +1 -0
  80. package/dist/commonjs/index.d.ts +9 -0
  81. package/dist/commonjs/index.d.ts.map +1 -0
  82. package/dist/commonjs/index.js +26 -0
  83. package/dist/commonjs/index.js.map +1 -0
  84. package/dist/commonjs/interfaceHelpers.d.ts +14 -0
  85. package/dist/commonjs/interfaceHelpers.d.ts.map +1 -0
  86. package/dist/commonjs/interfaceHelpers.js +44 -0
  87. package/dist/commonjs/interfaceHelpers.js.map +1 -0
  88. package/dist/commonjs/interfaces.d.ts +664 -0
  89. package/dist/commonjs/interfaces.d.ts.map +1 -0
  90. package/dist/commonjs/interfaces.js +14 -0
  91. package/dist/commonjs/interfaces.js.map +1 -0
  92. package/dist/commonjs/log.d.ts +2 -0
  93. package/dist/commonjs/log.d.ts.map +1 -0
  94. package/dist/commonjs/log.js +8 -0
  95. package/dist/commonjs/log.js.map +1 -0
  96. package/dist/commonjs/operationHelpers.d.ts +14 -0
  97. package/dist/commonjs/operationHelpers.d.ts.map +1 -0
  98. package/dist/commonjs/operationHelpers.js +99 -0
  99. package/dist/commonjs/operationHelpers.js.map +1 -0
  100. package/dist/commonjs/package.json +3 -0
  101. package/dist/commonjs/pipeline.d.ts +34 -0
  102. package/dist/commonjs/pipeline.d.ts.map +1 -0
  103. package/dist/commonjs/pipeline.js +30 -0
  104. package/dist/commonjs/pipeline.js.map +1 -0
  105. package/dist/commonjs/serializationPolicy.d.ts +33 -0
  106. package/dist/commonjs/serializationPolicy.d.ts.map +1 -0
  107. package/dist/commonjs/serializationPolicy.js +157 -0
  108. package/dist/commonjs/serializationPolicy.js.map +1 -0
  109. package/dist/commonjs/serializer.d.ts +31 -0
  110. package/dist/commonjs/serializer.d.ts.map +1 -0
  111. package/dist/commonjs/serializer.js +926 -0
  112. package/dist/commonjs/serializer.js.map +1 -0
  113. package/dist/commonjs/serviceClient.d.ts +82 -0
  114. package/dist/commonjs/serviceClient.d.ts.map +1 -0
  115. package/dist/commonjs/serviceClient.js +177 -0
  116. package/dist/commonjs/serviceClient.js.map +1 -0
  117. package/dist/commonjs/tsdoc-metadata.json +11 -0
  118. package/dist/commonjs/urlHelpers.d.ts +7 -0
  119. package/dist/commonjs/urlHelpers.d.ts.map +1 -0
  120. package/dist/commonjs/urlHelpers.js +238 -0
  121. package/dist/commonjs/urlHelpers.js.map +1 -0
  122. package/dist/commonjs/utils.d.ts +37 -0
  123. package/dist/commonjs/utils.d.ts.map +1 -0
  124. package/dist/commonjs/utils.js +135 -0
  125. package/dist/commonjs/utils.js.map +1 -0
  126. package/dist/esm/authorizeRequestOnClaimChallenge.d.ts +43 -0
  127. package/dist/esm/authorizeRequestOnClaimChallenge.d.ts.map +1 -0
  128. package/dist/esm/authorizeRequestOnClaimChallenge.js +69 -0
  129. package/dist/esm/authorizeRequestOnClaimChallenge.js.map +1 -0
  130. package/dist/esm/authorizeRequestOnTenantChallenge.d.ts +8 -0
  131. package/dist/esm/authorizeRequestOnTenantChallenge.d.ts.map +1 -0
  132. package/dist/esm/authorizeRequestOnTenantChallenge.js +102 -0
  133. package/dist/esm/authorizeRequestOnTenantChallenge.js.map +1 -0
  134. package/dist/esm/base64.d.ts +25 -0
  135. package/dist/esm/base64.d.ts.map +1 -0
  136. package/{dist-esm/src → dist/esm}/base64.js +0 -2
  137. package/dist/esm/base64.js.map +1 -0
  138. package/dist/esm/deserializationPolicy.d.ts +45 -0
  139. package/dist/esm/deserializationPolicy.d.ts.map +1 -0
  140. package/dist/esm/deserializationPolicy.js +228 -0
  141. package/dist/esm/deserializationPolicy.js.map +1 -0
  142. package/dist/esm/httpClientCache.d.ts +3 -0
  143. package/dist/esm/httpClientCache.d.ts.map +1 -0
  144. package/dist/esm/httpClientCache.js +11 -0
  145. package/dist/esm/httpClientCache.js.map +1 -0
  146. package/dist/esm/index.d.ts +9 -0
  147. package/dist/esm/index.d.ts.map +1 -0
  148. package/dist/esm/index.js +11 -0
  149. package/dist/esm/index.js.map +1 -0
  150. package/dist/esm/interfaceHelpers.d.ts +14 -0
  151. package/dist/esm/interfaceHelpers.d.ts.map +1 -0
  152. package/dist/esm/interfaceHelpers.js +39 -0
  153. package/dist/esm/interfaceHelpers.js.map +1 -0
  154. package/dist/esm/interfaces.d.ts +664 -0
  155. package/dist/esm/interfaces.d.ts.map +1 -0
  156. package/dist/esm/interfaces.js +11 -0
  157. package/dist/esm/interfaces.js.map +1 -0
  158. package/dist/esm/log.d.ts +2 -0
  159. package/dist/esm/log.d.ts.map +1 -0
  160. package/dist/esm/log.js +5 -0
  161. package/dist/esm/log.js.map +1 -0
  162. package/dist/esm/operationHelpers.d.ts +14 -0
  163. package/dist/esm/operationHelpers.d.ts.map +1 -0
  164. package/dist/esm/operationHelpers.js +94 -0
  165. package/dist/esm/operationHelpers.js.map +1 -0
  166. package/dist/esm/package.json +3 -0
  167. package/dist/esm/pipeline.d.ts +34 -0
  168. package/dist/esm/pipeline.d.ts.map +1 -0
  169. package/dist/esm/pipeline.js +26 -0
  170. package/dist/esm/pipeline.js.map +1 -0
  171. package/dist/esm/serializationPolicy.d.ts +33 -0
  172. package/dist/esm/serializationPolicy.d.ts.map +1 -0
  173. package/dist/esm/serializationPolicy.js +151 -0
  174. package/dist/esm/serializationPolicy.js.map +1 -0
  175. package/dist/esm/serializer.d.ts +31 -0
  176. package/dist/esm/serializer.d.ts.map +1 -0
  177. package/dist/esm/serializer.js +921 -0
  178. package/dist/esm/serializer.js.map +1 -0
  179. package/dist/esm/serviceClient.d.ts +82 -0
  180. package/dist/esm/serviceClient.d.ts.map +1 -0
  181. package/dist/esm/serviceClient.js +173 -0
  182. package/dist/esm/serviceClient.js.map +1 -0
  183. package/dist/esm/urlHelpers.d.ts +7 -0
  184. package/dist/esm/urlHelpers.d.ts.map +1 -0
  185. package/dist/esm/urlHelpers.js +233 -0
  186. package/dist/esm/urlHelpers.js.map +1 -0
  187. package/dist/esm/utils.d.ts +37 -0
  188. package/dist/esm/utils.d.ts.map +1 -0
  189. package/dist/esm/utils.js +128 -0
  190. package/dist/esm/utils.js.map +1 -0
  191. package/dist/react-native/authorizeRequestOnClaimChallenge.d.ts +43 -0
  192. package/dist/react-native/authorizeRequestOnClaimChallenge.d.ts.map +1 -0
  193. package/dist/react-native/authorizeRequestOnClaimChallenge.js +69 -0
  194. package/dist/react-native/authorizeRequestOnClaimChallenge.js.map +1 -0
  195. package/dist/react-native/authorizeRequestOnTenantChallenge.d.ts +8 -0
  196. package/dist/react-native/authorizeRequestOnTenantChallenge.d.ts.map +1 -0
  197. package/dist/react-native/authorizeRequestOnTenantChallenge.js +102 -0
  198. package/dist/react-native/authorizeRequestOnTenantChallenge.js.map +1 -0
  199. package/dist/react-native/base64.d.ts +25 -0
  200. package/dist/react-native/base64.d.ts.map +1 -0
  201. package/dist/react-native/base64.js +36 -0
  202. package/dist/react-native/base64.js.map +1 -0
  203. package/dist/react-native/deserializationPolicy.d.ts +45 -0
  204. package/dist/react-native/deserializationPolicy.d.ts.map +1 -0
  205. package/dist/react-native/deserializationPolicy.js +228 -0
  206. package/dist/react-native/deserializationPolicy.js.map +1 -0
  207. package/dist/react-native/httpClientCache.d.ts +3 -0
  208. package/dist/react-native/httpClientCache.d.ts.map +1 -0
  209. package/dist/react-native/httpClientCache.js +11 -0
  210. package/dist/react-native/httpClientCache.js.map +1 -0
  211. package/dist/react-native/index.d.ts +9 -0
  212. package/dist/react-native/index.d.ts.map +1 -0
  213. package/dist/react-native/index.js +11 -0
  214. package/dist/react-native/index.js.map +1 -0
  215. package/dist/react-native/interfaceHelpers.d.ts +14 -0
  216. package/dist/react-native/interfaceHelpers.d.ts.map +1 -0
  217. package/dist/react-native/interfaceHelpers.js +39 -0
  218. package/dist/react-native/interfaceHelpers.js.map +1 -0
  219. package/dist/react-native/interfaces.d.ts +664 -0
  220. package/dist/react-native/interfaces.d.ts.map +1 -0
  221. package/dist/react-native/interfaces.js +11 -0
  222. package/dist/react-native/interfaces.js.map +1 -0
  223. package/dist/react-native/log.d.ts +2 -0
  224. package/dist/react-native/log.d.ts.map +1 -0
  225. package/dist/react-native/log.js +5 -0
  226. package/dist/react-native/log.js.map +1 -0
  227. package/dist/react-native/operationHelpers.d.ts +14 -0
  228. package/dist/react-native/operationHelpers.d.ts.map +1 -0
  229. package/dist/react-native/operationHelpers.js +94 -0
  230. package/dist/react-native/operationHelpers.js.map +1 -0
  231. package/dist/react-native/package.json +3 -0
  232. package/dist/react-native/pipeline.d.ts +34 -0
  233. package/dist/react-native/pipeline.d.ts.map +1 -0
  234. package/dist/react-native/pipeline.js +26 -0
  235. package/dist/react-native/pipeline.js.map +1 -0
  236. package/dist/react-native/serializationPolicy.d.ts +33 -0
  237. package/dist/react-native/serializationPolicy.d.ts.map +1 -0
  238. package/dist/react-native/serializationPolicy.js +151 -0
  239. package/dist/react-native/serializationPolicy.js.map +1 -0
  240. package/dist/react-native/serializer.d.ts +31 -0
  241. package/dist/react-native/serializer.d.ts.map +1 -0
  242. package/dist/react-native/serializer.js +921 -0
  243. package/dist/react-native/serializer.js.map +1 -0
  244. package/dist/react-native/serviceClient.d.ts +82 -0
  245. package/dist/react-native/serviceClient.d.ts.map +1 -0
  246. package/dist/react-native/serviceClient.js +173 -0
  247. package/dist/react-native/serviceClient.js.map +1 -0
  248. package/dist/react-native/urlHelpers.d.ts +7 -0
  249. package/dist/react-native/urlHelpers.d.ts.map +1 -0
  250. package/dist/react-native/urlHelpers.js +233 -0
  251. package/dist/react-native/urlHelpers.js.map +1 -0
  252. package/dist/react-native/utils.d.ts +37 -0
  253. package/dist/react-native/utils.d.ts.map +1 -0
  254. package/dist/react-native/utils.js +128 -0
  255. package/dist/react-native/utils.js.map +1 -0
  256. package/package.json +74 -61
  257. package/dist/index.js +0 -2185
  258. package/dist/index.js.map +0 -1
  259. package/dist-esm/src/authorizeRequestOnClaimChallenge.js.map +0 -1
  260. package/dist-esm/src/authorizeRequestOnTenantChallenge.js.map +0 -1
  261. package/dist-esm/src/base64.browser.js.map +0 -1
  262. package/dist-esm/src/base64.js.map +0 -1
  263. package/dist-esm/src/deserializationPolicy.js.map +0 -1
  264. package/dist-esm/src/index.js +0 -11
  265. package/dist-esm/src/index.js.map +0 -1
  266. package/dist-esm/src/interfaceHelpers.js.map +0 -1
  267. package/dist-esm/src/operationHelpers.js.map +0 -1
  268. package/dist-esm/src/pipeline.js.map +0 -1
  269. package/dist-esm/src/serializationPolicy.js.map +0 -1
  270. package/dist-esm/src/serializer.js.map +0 -1
  271. package/dist-esm/src/serviceClient.js.map +0 -1
  272. package/dist-esm/src/urlHelpers.js.map +0 -1
  273. package/dist-esm/src/utils.js.map +0 -1
  274. /package/{dist-esm/src → dist/browser}/httpClientCache.js +0 -0
  275. /package/{dist-esm/src → dist/browser}/httpClientCache.js.map +0 -0
  276. /package/{dist-esm/src → dist/browser}/interfaces.js +0 -0
  277. /package/{dist-esm/src → dist/browser}/interfaces.js.map +0 -0
  278. /package/{dist-esm/src → dist/browser}/log.js +0 -0
  279. /package/{dist-esm/src → dist/browser}/log.js.map +0 -0
  280. /package/{dist-esm/src → dist/browser}/operationHelpers.js +0 -0
  281. /package/{types/latest → dist}/core-client.d.ts +0 -0
@@ -0,0 +1,921 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ import * as base64 from "./base64.js";
4
+ import { XML_ATTRKEY, XML_CHARKEY, } from "./interfaces.js";
5
+ import { isDuration, isValidUuid } from "./utils.js";
6
+ class SerializerImpl {
7
+ modelMappers;
8
+ isXML;
9
+ constructor(modelMappers = {}, isXML = false) {
10
+ this.modelMappers = modelMappers;
11
+ this.isXML = isXML;
12
+ }
13
+ /**
14
+ * @deprecated Removing the constraints validation on client side.
15
+ */
16
+ validateConstraints(mapper, value, objectName) {
17
+ const failValidation = (constraintName, constraintValue) => {
18
+ throw new Error(`"${objectName}" with value "${value}" should satisfy the constraint "${constraintName}": ${constraintValue}.`);
19
+ };
20
+ if (mapper.constraints && value !== undefined && value !== null) {
21
+ const { ExclusiveMaximum, ExclusiveMinimum, InclusiveMaximum, InclusiveMinimum, MaxItems, MaxLength, MinItems, MinLength, MultipleOf, Pattern, UniqueItems, } = mapper.constraints;
22
+ if (ExclusiveMaximum !== undefined && value >= ExclusiveMaximum) {
23
+ failValidation("ExclusiveMaximum", ExclusiveMaximum);
24
+ }
25
+ if (ExclusiveMinimum !== undefined && value <= ExclusiveMinimum) {
26
+ failValidation("ExclusiveMinimum", ExclusiveMinimum);
27
+ }
28
+ if (InclusiveMaximum !== undefined && value > InclusiveMaximum) {
29
+ failValidation("InclusiveMaximum", InclusiveMaximum);
30
+ }
31
+ if (InclusiveMinimum !== undefined && value < InclusiveMinimum) {
32
+ failValidation("InclusiveMinimum", InclusiveMinimum);
33
+ }
34
+ if (MaxItems !== undefined && value.length > MaxItems) {
35
+ failValidation("MaxItems", MaxItems);
36
+ }
37
+ if (MaxLength !== undefined && value.length > MaxLength) {
38
+ failValidation("MaxLength", MaxLength);
39
+ }
40
+ if (MinItems !== undefined && value.length < MinItems) {
41
+ failValidation("MinItems", MinItems);
42
+ }
43
+ if (MinLength !== undefined && value.length < MinLength) {
44
+ failValidation("MinLength", MinLength);
45
+ }
46
+ if (MultipleOf !== undefined && value % MultipleOf !== 0) {
47
+ failValidation("MultipleOf", MultipleOf);
48
+ }
49
+ if (Pattern) {
50
+ const pattern = typeof Pattern === "string" ? new RegExp(Pattern) : Pattern;
51
+ if (typeof value !== "string" || value.match(pattern) === null) {
52
+ failValidation("Pattern", Pattern);
53
+ }
54
+ }
55
+ if (UniqueItems &&
56
+ value.some((item, i, ar) => ar.indexOf(item) !== i)) {
57
+ failValidation("UniqueItems", UniqueItems);
58
+ }
59
+ }
60
+ }
61
+ /**
62
+ * Serialize the given object based on its metadata defined in the mapper
63
+ *
64
+ * @param mapper - The mapper which defines the metadata of the serializable object
65
+ *
66
+ * @param object - A valid Javascript object to be serialized
67
+ *
68
+ * @param objectName - Name of the serialized object
69
+ *
70
+ * @param options - additional options to serialization
71
+ *
72
+ * @returns A valid serialized Javascript object
73
+ */
74
+ serialize(mapper, object, objectName, options = { xml: {} }) {
75
+ const updatedOptions = {
76
+ xml: {
77
+ rootName: options.xml.rootName ?? "",
78
+ includeRoot: options.xml.includeRoot ?? false,
79
+ xmlCharKey: options.xml.xmlCharKey ?? XML_CHARKEY,
80
+ },
81
+ };
82
+ let payload = {};
83
+ const mapperType = mapper.type.name;
84
+ if (!objectName) {
85
+ objectName = mapper.serializedName;
86
+ }
87
+ if (mapperType.match(/^Sequence$/i) !== null) {
88
+ payload = [];
89
+ }
90
+ if (mapper.isConstant) {
91
+ object = mapper.defaultValue;
92
+ }
93
+ // This table of allowed values should help explain
94
+ // the mapper.required and mapper.nullable properties.
95
+ // X means "neither undefined or null are allowed".
96
+ // || required
97
+ // || true | false
98
+ // nullable || ==========================
99
+ // true || null | undefined/null
100
+ // false || X | undefined
101
+ // undefined || X | undefined/null
102
+ const { required, nullable } = mapper;
103
+ if (required && nullable && object === undefined) {
104
+ throw new Error(`${objectName} cannot be undefined.`);
105
+ }
106
+ if (required && !nullable && (object === undefined || object === null)) {
107
+ throw new Error(`${objectName} cannot be null or undefined.`);
108
+ }
109
+ if (!required && nullable === false && object === null) {
110
+ throw new Error(`${objectName} cannot be null.`);
111
+ }
112
+ if (object === undefined || object === null) {
113
+ payload = object;
114
+ }
115
+ else {
116
+ if (mapperType.match(/^any$/i) !== null) {
117
+ payload = object;
118
+ }
119
+ else if (mapperType.match(/^(Number|String|Boolean|Object|Stream|Uuid)$/i) !== null) {
120
+ payload = serializeBasicTypes(mapperType, objectName, object);
121
+ }
122
+ else if (mapperType.match(/^Enum$/i) !== null) {
123
+ const enumMapper = mapper;
124
+ payload = serializeEnumType(objectName, enumMapper.type.allowedValues, object);
125
+ }
126
+ else if (mapperType.match(/^(Date|DateTime|TimeSpan|DateTimeRfc1123|UnixTime)$/i) !== null) {
127
+ payload = serializeDateTypes(mapperType, object, objectName);
128
+ }
129
+ else if (mapperType.match(/^ByteArray$/i) !== null) {
130
+ payload = serializeByteArrayType(objectName, object);
131
+ }
132
+ else if (mapperType.match(/^Base64Url$/i) !== null) {
133
+ payload = serializeBase64UrlType(objectName, object);
134
+ }
135
+ else if (mapperType.match(/^Sequence$/i) !== null) {
136
+ payload = serializeSequenceType(this, mapper, object, objectName, Boolean(this.isXML), updatedOptions);
137
+ }
138
+ else if (mapperType.match(/^Dictionary$/i) !== null) {
139
+ payload = serializeDictionaryType(this, mapper, object, objectName, Boolean(this.isXML), updatedOptions);
140
+ }
141
+ else if (mapperType.match(/^Composite$/i) !== null) {
142
+ payload = serializeCompositeType(this, mapper, object, objectName, Boolean(this.isXML), updatedOptions);
143
+ }
144
+ }
145
+ return payload;
146
+ }
147
+ /**
148
+ * Deserialize the given object based on its metadata defined in the mapper
149
+ *
150
+ * @param mapper - The mapper which defines the metadata of the serializable object
151
+ *
152
+ * @param responseBody - A valid Javascript entity to be deserialized
153
+ *
154
+ * @param objectName - Name of the deserialized object
155
+ *
156
+ * @param options - Controls behavior of XML parser and builder.
157
+ *
158
+ * @returns A valid deserialized Javascript object
159
+ */
160
+ deserialize(mapper, responseBody, objectName, options = { xml: {} }) {
161
+ const updatedOptions = {
162
+ xml: {
163
+ rootName: options.xml.rootName ?? "",
164
+ includeRoot: options.xml.includeRoot ?? false,
165
+ xmlCharKey: options.xml.xmlCharKey ?? XML_CHARKEY,
166
+ },
167
+ ignoreUnknownProperties: options.ignoreUnknownProperties ?? false,
168
+ };
169
+ if (responseBody === undefined || responseBody === null) {
170
+ if (this.isXML && mapper.type.name === "Sequence" && !mapper.xmlIsWrapped) {
171
+ // Edge case for empty XML non-wrapped lists. xml2js can't distinguish
172
+ // between the list being empty versus being missing,
173
+ // so let's do the more user-friendly thing and return an empty list.
174
+ responseBody = [];
175
+ }
176
+ // specifically check for undefined as default value can be a falsey value `0, "", false, null`
177
+ if (mapper.defaultValue !== undefined) {
178
+ responseBody = mapper.defaultValue;
179
+ }
180
+ return responseBody;
181
+ }
182
+ let payload;
183
+ const mapperType = mapper.type.name;
184
+ if (!objectName) {
185
+ objectName = mapper.serializedName;
186
+ }
187
+ if (mapperType.match(/^Composite$/i) !== null) {
188
+ payload = deserializeCompositeType(this, mapper, responseBody, objectName, updatedOptions);
189
+ }
190
+ else {
191
+ if (this.isXML) {
192
+ const xmlCharKey = updatedOptions.xml.xmlCharKey;
193
+ /**
194
+ * If the mapper specifies this as a non-composite type value but the responseBody contains
195
+ * both header ("$" i.e., XML_ATTRKEY) and body ("#" i.e., XML_CHARKEY) properties,
196
+ * then just reduce the responseBody value to the body ("#" i.e., XML_CHARKEY) property.
197
+ */
198
+ if (responseBody[XML_ATTRKEY] !== undefined && responseBody[xmlCharKey] !== undefined) {
199
+ responseBody = responseBody[xmlCharKey];
200
+ }
201
+ }
202
+ if (mapperType.match(/^Number$/i) !== null) {
203
+ payload = parseFloat(responseBody);
204
+ if (isNaN(payload)) {
205
+ payload = responseBody;
206
+ }
207
+ }
208
+ else if (mapperType.match(/^Boolean$/i) !== null) {
209
+ if (responseBody === "true") {
210
+ payload = true;
211
+ }
212
+ else if (responseBody === "false") {
213
+ payload = false;
214
+ }
215
+ else {
216
+ payload = responseBody;
217
+ }
218
+ }
219
+ else if (mapperType.match(/^(String|Enum|Object|Stream|Uuid|TimeSpan|any)$/i) !== null) {
220
+ payload = responseBody;
221
+ }
222
+ else if (mapperType.match(/^(Date|DateTime|DateTimeRfc1123)$/i) !== null) {
223
+ payload = new Date(responseBody);
224
+ }
225
+ else if (mapperType.match(/^UnixTime$/i) !== null) {
226
+ payload = unixTimeToDate(responseBody);
227
+ }
228
+ else if (mapperType.match(/^ByteArray$/i) !== null) {
229
+ payload = base64.decodeString(responseBody);
230
+ }
231
+ else if (mapperType.match(/^Base64Url$/i) !== null) {
232
+ payload = base64UrlToByteArray(responseBody);
233
+ }
234
+ else if (mapperType.match(/^Sequence$/i) !== null) {
235
+ payload = deserializeSequenceType(this, mapper, responseBody, objectName, updatedOptions);
236
+ }
237
+ else if (mapperType.match(/^Dictionary$/i) !== null) {
238
+ payload = deserializeDictionaryType(this, mapper, responseBody, objectName, updatedOptions);
239
+ }
240
+ }
241
+ if (mapper.isConstant) {
242
+ payload = mapper.defaultValue;
243
+ }
244
+ return payload;
245
+ }
246
+ }
247
+ /**
248
+ * Method that creates and returns a Serializer.
249
+ * @param modelMappers - Known models to map
250
+ * @param isXML - If XML should be supported
251
+ */
252
+ export function createSerializer(modelMappers = {}, isXML = false) {
253
+ return new SerializerImpl(modelMappers, isXML);
254
+ }
255
+ function trimEnd(str, ch) {
256
+ let len = str.length;
257
+ while (len - 1 >= 0 && str[len - 1] === ch) {
258
+ --len;
259
+ }
260
+ return str.substr(0, len);
261
+ }
262
+ function bufferToBase64Url(buffer) {
263
+ if (!buffer) {
264
+ return undefined;
265
+ }
266
+ if (!(buffer instanceof Uint8Array)) {
267
+ throw new Error(`Please provide an input of type Uint8Array for converting to Base64Url.`);
268
+ }
269
+ // Uint8Array to Base64.
270
+ const str = base64.encodeByteArray(buffer);
271
+ // Base64 to Base64Url.
272
+ return trimEnd(str, "=").replace(/\+/g, "-").replace(/\//g, "_");
273
+ }
274
+ function base64UrlToByteArray(str) {
275
+ if (!str) {
276
+ return undefined;
277
+ }
278
+ if (str && typeof str.valueOf() !== "string") {
279
+ throw new Error("Please provide an input of type string for converting to Uint8Array");
280
+ }
281
+ // Base64Url to Base64.
282
+ str = str.replace(/-/g, "+").replace(/_/g, "/");
283
+ // Base64 to Uint8Array.
284
+ return base64.decodeString(str);
285
+ }
286
+ function splitSerializeName(prop) {
287
+ const classes = [];
288
+ let partialclass = "";
289
+ if (prop) {
290
+ const subwords = prop.split(".");
291
+ for (const item of subwords) {
292
+ if (item.charAt(item.length - 1) === "\\") {
293
+ partialclass += item.substr(0, item.length - 1) + ".";
294
+ }
295
+ else {
296
+ partialclass += item;
297
+ classes.push(partialclass);
298
+ partialclass = "";
299
+ }
300
+ }
301
+ }
302
+ return classes;
303
+ }
304
+ function dateToUnixTime(d) {
305
+ if (!d) {
306
+ return undefined;
307
+ }
308
+ if (typeof d.valueOf() === "string") {
309
+ d = new Date(d);
310
+ }
311
+ return Math.floor(d.getTime() / 1000);
312
+ }
313
+ function unixTimeToDate(n) {
314
+ if (!n) {
315
+ return undefined;
316
+ }
317
+ return new Date(n * 1000);
318
+ }
319
+ function serializeBasicTypes(typeName, objectName, value) {
320
+ if (value !== null && value !== undefined) {
321
+ if (typeName.match(/^Number$/i) !== null) {
322
+ if (typeof value !== "number") {
323
+ throw new Error(`${objectName} with value ${value} must be of type number.`);
324
+ }
325
+ }
326
+ else if (typeName.match(/^String$/i) !== null) {
327
+ if (typeof value.valueOf() !== "string") {
328
+ throw new Error(`${objectName} with value "${value}" must be of type string.`);
329
+ }
330
+ }
331
+ else if (typeName.match(/^Uuid$/i) !== null) {
332
+ if (!(typeof value.valueOf() === "string" && isValidUuid(value))) {
333
+ throw new Error(`${objectName} with value "${value}" must be of type string and a valid uuid.`);
334
+ }
335
+ }
336
+ else if (typeName.match(/^Boolean$/i) !== null) {
337
+ if (typeof value !== "boolean") {
338
+ throw new Error(`${objectName} with value ${value} must be of type boolean.`);
339
+ }
340
+ }
341
+ else if (typeName.match(/^Stream$/i) !== null) {
342
+ const objectType = typeof value;
343
+ if (objectType !== "string" &&
344
+ typeof value.pipe !== "function" && // NodeJS.ReadableStream
345
+ typeof value.tee !== "function" && // browser ReadableStream
346
+ !(value instanceof ArrayBuffer) &&
347
+ !ArrayBuffer.isView(value) &&
348
+ // File objects count as a type of Blob, so we want to use instanceof explicitly
349
+ !((typeof Blob === "function" || typeof Blob === "object") && value instanceof Blob) &&
350
+ objectType !== "function") {
351
+ throw new Error(`${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, ReadableStream, or () => ReadableStream.`);
352
+ }
353
+ }
354
+ }
355
+ return value;
356
+ }
357
+ function serializeEnumType(objectName, allowedValues, value) {
358
+ if (!allowedValues) {
359
+ throw new Error(`Please provide a set of allowedValues to validate ${objectName} as an Enum Type.`);
360
+ }
361
+ const isPresent = allowedValues.some((item) => {
362
+ if (typeof item.valueOf() === "string") {
363
+ return item.toLowerCase() === value.toLowerCase();
364
+ }
365
+ return item === value;
366
+ });
367
+ if (!isPresent) {
368
+ throw new Error(`${value} is not a valid value for ${objectName}. The valid values are: ${JSON.stringify(allowedValues)}.`);
369
+ }
370
+ return value;
371
+ }
372
+ function serializeByteArrayType(objectName, value) {
373
+ if (value !== undefined && value !== null) {
374
+ if (!(value instanceof Uint8Array)) {
375
+ throw new Error(`${objectName} must be of type Uint8Array.`);
376
+ }
377
+ value = base64.encodeByteArray(value);
378
+ }
379
+ return value;
380
+ }
381
+ function serializeBase64UrlType(objectName, value) {
382
+ if (value !== undefined && value !== null) {
383
+ if (!(value instanceof Uint8Array)) {
384
+ throw new Error(`${objectName} must be of type Uint8Array.`);
385
+ }
386
+ value = bufferToBase64Url(value);
387
+ }
388
+ return value;
389
+ }
390
+ function serializeDateTypes(typeName, value, objectName) {
391
+ if (value !== undefined && value !== null) {
392
+ if (typeName.match(/^Date$/i) !== null) {
393
+ if (!(value instanceof Date ||
394
+ (typeof value.valueOf() === "string" && !isNaN(Date.parse(value))))) {
395
+ throw new Error(`${objectName} must be an instanceof Date or a string in ISO8601 format.`);
396
+ }
397
+ value =
398
+ value instanceof Date
399
+ ? value.toISOString().substring(0, 10)
400
+ : new Date(value).toISOString().substring(0, 10);
401
+ }
402
+ else if (typeName.match(/^DateTime$/i) !== null) {
403
+ if (!(value instanceof Date ||
404
+ (typeof value.valueOf() === "string" && !isNaN(Date.parse(value))))) {
405
+ throw new Error(`${objectName} must be an instanceof Date or a string in ISO8601 format.`);
406
+ }
407
+ value = value instanceof Date ? value.toISOString() : new Date(value).toISOString();
408
+ }
409
+ else if (typeName.match(/^DateTimeRfc1123$/i) !== null) {
410
+ if (!(value instanceof Date ||
411
+ (typeof value.valueOf() === "string" && !isNaN(Date.parse(value))))) {
412
+ throw new Error(`${objectName} must be an instanceof Date or a string in RFC-1123 format.`);
413
+ }
414
+ value = value instanceof Date ? value.toUTCString() : new Date(value).toUTCString();
415
+ }
416
+ else if (typeName.match(/^UnixTime$/i) !== null) {
417
+ if (!(value instanceof Date ||
418
+ (typeof value.valueOf() === "string" && !isNaN(Date.parse(value))))) {
419
+ throw new Error(`${objectName} must be an instanceof Date or a string in RFC-1123/ISO8601 format ` +
420
+ `for it to be serialized in UnixTime/Epoch format.`);
421
+ }
422
+ value = dateToUnixTime(value);
423
+ }
424
+ else if (typeName.match(/^TimeSpan$/i) !== null) {
425
+ if (!isDuration(value)) {
426
+ throw new Error(`${objectName} must be a string in ISO 8601 format. Instead was "${value}".`);
427
+ }
428
+ }
429
+ }
430
+ return value;
431
+ }
432
+ function serializeSequenceType(serializer, mapper, object, objectName, isXml, options) {
433
+ if (!Array.isArray(object)) {
434
+ throw new Error(`${objectName} must be of type Array.`);
435
+ }
436
+ let elementType = mapper.type.element;
437
+ if (!elementType || typeof elementType !== "object") {
438
+ throw new Error(`element" metadata for an Array must be defined in the ` +
439
+ `mapper and it must of type "object" in ${objectName}.`);
440
+ }
441
+ // Quirk: Composite mappers referenced by `element` might
442
+ // not have *all* properties declared (like uberParent),
443
+ // so let's try to look up the full definition by name.
444
+ if (elementType.type.name === "Composite" && elementType.type.className) {
445
+ elementType = serializer.modelMappers[elementType.type.className] ?? elementType;
446
+ }
447
+ const tempArray = [];
448
+ for (let i = 0; i < object.length; i++) {
449
+ const serializedValue = serializer.serialize(elementType, object[i], objectName, options);
450
+ if (isXml && elementType.xmlNamespace) {
451
+ const xmlnsKey = elementType.xmlNamespacePrefix
452
+ ? `xmlns:${elementType.xmlNamespacePrefix}`
453
+ : "xmlns";
454
+ if (elementType.type.name === "Composite") {
455
+ tempArray[i] = { ...serializedValue };
456
+ tempArray[i][XML_ATTRKEY] = { [xmlnsKey]: elementType.xmlNamespace };
457
+ }
458
+ else {
459
+ tempArray[i] = {};
460
+ tempArray[i][options.xml.xmlCharKey] = serializedValue;
461
+ tempArray[i][XML_ATTRKEY] = { [xmlnsKey]: elementType.xmlNamespace };
462
+ }
463
+ }
464
+ else {
465
+ tempArray[i] = serializedValue;
466
+ }
467
+ }
468
+ return tempArray;
469
+ }
470
+ function serializeDictionaryType(serializer, mapper, object, objectName, isXml, options) {
471
+ if (typeof object !== "object") {
472
+ throw new Error(`${objectName} must be of type object.`);
473
+ }
474
+ const valueType = mapper.type.value;
475
+ if (!valueType || typeof valueType !== "object") {
476
+ throw new Error(`"value" metadata for a Dictionary must be defined in the ` +
477
+ `mapper and it must of type "object" in ${objectName}.`);
478
+ }
479
+ const tempDictionary = {};
480
+ for (const key of Object.keys(object)) {
481
+ const serializedValue = serializer.serialize(valueType, object[key], objectName, options);
482
+ // If the element needs an XML namespace we need to add it within the $ property
483
+ tempDictionary[key] = getXmlObjectValue(valueType, serializedValue, isXml, options);
484
+ }
485
+ // Add the namespace to the root element if needed
486
+ if (isXml && mapper.xmlNamespace) {
487
+ const xmlnsKey = mapper.xmlNamespacePrefix ? `xmlns:${mapper.xmlNamespacePrefix}` : "xmlns";
488
+ const result = tempDictionary;
489
+ result[XML_ATTRKEY] = { [xmlnsKey]: mapper.xmlNamespace };
490
+ return result;
491
+ }
492
+ return tempDictionary;
493
+ }
494
+ /**
495
+ * Resolves the additionalProperties property from a referenced mapper
496
+ * @param serializer - the serializer containing the entire set of mappers
497
+ * @param mapper - the composite mapper to resolve
498
+ * @param objectName - name of the object being serialized
499
+ */
500
+ function resolveAdditionalProperties(serializer, mapper, objectName) {
501
+ const additionalProperties = mapper.type.additionalProperties;
502
+ if (!additionalProperties && mapper.type.className) {
503
+ const modelMapper = resolveReferencedMapper(serializer, mapper, objectName);
504
+ return modelMapper?.type.additionalProperties;
505
+ }
506
+ return additionalProperties;
507
+ }
508
+ /**
509
+ * Finds the mapper referenced by className
510
+ * @param serializer - the serializer containing the entire set of mappers
511
+ * @param mapper - the composite mapper to resolve
512
+ * @param objectName - name of the object being serialized
513
+ */
514
+ function resolveReferencedMapper(serializer, mapper, objectName) {
515
+ const className = mapper.type.className;
516
+ if (!className) {
517
+ throw new Error(`Class name for model "${objectName}" is not provided in the mapper "${JSON.stringify(mapper, undefined, 2)}".`);
518
+ }
519
+ return serializer.modelMappers[className];
520
+ }
521
+ /**
522
+ * Resolves a composite mapper's modelProperties.
523
+ * @param serializer - the serializer containing the entire set of mappers
524
+ * @param mapper - the composite mapper to resolve
525
+ */
526
+ function resolveModelProperties(serializer, mapper, objectName) {
527
+ let modelProps = mapper.type.modelProperties;
528
+ if (!modelProps) {
529
+ const modelMapper = resolveReferencedMapper(serializer, mapper, objectName);
530
+ if (!modelMapper) {
531
+ throw new Error(`mapper() cannot be null or undefined for model "${mapper.type.className}".`);
532
+ }
533
+ modelProps = modelMapper?.type.modelProperties;
534
+ if (!modelProps) {
535
+ throw new Error(`modelProperties cannot be null or undefined in the ` +
536
+ `mapper "${JSON.stringify(modelMapper)}" of type "${mapper.type.className}" for object "${objectName}".`);
537
+ }
538
+ }
539
+ return modelProps;
540
+ }
541
+ function serializeCompositeType(serializer, mapper, object, objectName, isXml, options) {
542
+ if (getPolymorphicDiscriminatorRecursively(serializer, mapper)) {
543
+ mapper = getPolymorphicMapper(serializer, mapper, object, "clientName");
544
+ }
545
+ if (object !== undefined && object !== null) {
546
+ const payload = {};
547
+ const modelProps = resolveModelProperties(serializer, mapper, objectName);
548
+ for (const key of Object.keys(modelProps)) {
549
+ const propertyMapper = modelProps[key];
550
+ if (propertyMapper.readOnly) {
551
+ continue;
552
+ }
553
+ let propName;
554
+ let parentObject = payload;
555
+ if (serializer.isXML) {
556
+ if (propertyMapper.xmlIsWrapped) {
557
+ propName = propertyMapper.xmlName;
558
+ }
559
+ else {
560
+ propName = propertyMapper.xmlElementName || propertyMapper.xmlName;
561
+ }
562
+ }
563
+ else {
564
+ const paths = splitSerializeName(propertyMapper.serializedName);
565
+ propName = paths.pop();
566
+ for (const pathName of paths) {
567
+ const childObject = parentObject[pathName];
568
+ if ((childObject === undefined || childObject === null) &&
569
+ ((object[key] !== undefined && object[key] !== null) ||
570
+ propertyMapper.defaultValue !== undefined)) {
571
+ parentObject[pathName] = {};
572
+ }
573
+ parentObject = parentObject[pathName];
574
+ }
575
+ }
576
+ if (parentObject !== undefined && parentObject !== null) {
577
+ if (isXml && mapper.xmlNamespace) {
578
+ const xmlnsKey = mapper.xmlNamespacePrefix
579
+ ? `xmlns:${mapper.xmlNamespacePrefix}`
580
+ : "xmlns";
581
+ parentObject[XML_ATTRKEY] = {
582
+ ...parentObject[XML_ATTRKEY],
583
+ [xmlnsKey]: mapper.xmlNamespace,
584
+ };
585
+ }
586
+ const propertyObjectName = propertyMapper.serializedName !== ""
587
+ ? objectName + "." + propertyMapper.serializedName
588
+ : objectName;
589
+ let toSerialize = object[key];
590
+ const polymorphicDiscriminator = getPolymorphicDiscriminatorRecursively(serializer, mapper);
591
+ if (polymorphicDiscriminator &&
592
+ polymorphicDiscriminator.clientName === key &&
593
+ (toSerialize === undefined || toSerialize === null)) {
594
+ toSerialize = mapper.serializedName;
595
+ }
596
+ const serializedValue = serializer.serialize(propertyMapper, toSerialize, propertyObjectName, options);
597
+ if (serializedValue !== undefined && propName !== undefined && propName !== null) {
598
+ const value = getXmlObjectValue(propertyMapper, serializedValue, isXml, options);
599
+ if (isXml && propertyMapper.xmlIsAttribute) {
600
+ // XML_ATTRKEY, i.e., $ is the key attributes are kept under in xml2js.
601
+ // This keeps things simple while preventing name collision
602
+ // with names in user documents.
603
+ parentObject[XML_ATTRKEY] = parentObject[XML_ATTRKEY] || {};
604
+ parentObject[XML_ATTRKEY][propName] = serializedValue;
605
+ }
606
+ else if (isXml && propertyMapper.xmlIsWrapped) {
607
+ parentObject[propName] = { [propertyMapper.xmlElementName]: value };
608
+ }
609
+ else {
610
+ parentObject[propName] = value;
611
+ }
612
+ }
613
+ }
614
+ }
615
+ const additionalPropertiesMapper = resolveAdditionalProperties(serializer, mapper, objectName);
616
+ if (additionalPropertiesMapper) {
617
+ const propNames = Object.keys(modelProps);
618
+ for (const clientPropName in object) {
619
+ const isAdditionalProperty = propNames.every((pn) => pn !== clientPropName);
620
+ if (isAdditionalProperty) {
621
+ payload[clientPropName] = serializer.serialize(additionalPropertiesMapper, object[clientPropName], objectName + '["' + clientPropName + '"]', options);
622
+ }
623
+ }
624
+ }
625
+ return payload;
626
+ }
627
+ return object;
628
+ }
629
+ function getXmlObjectValue(propertyMapper, serializedValue, isXml, options) {
630
+ if (!isXml || !propertyMapper.xmlNamespace) {
631
+ return serializedValue;
632
+ }
633
+ const xmlnsKey = propertyMapper.xmlNamespacePrefix
634
+ ? `xmlns:${propertyMapper.xmlNamespacePrefix}`
635
+ : "xmlns";
636
+ const xmlNamespace = { [xmlnsKey]: propertyMapper.xmlNamespace };
637
+ if (["Composite"].includes(propertyMapper.type.name)) {
638
+ if (serializedValue[XML_ATTRKEY]) {
639
+ return serializedValue;
640
+ }
641
+ else {
642
+ const result = { ...serializedValue };
643
+ result[XML_ATTRKEY] = xmlNamespace;
644
+ return result;
645
+ }
646
+ }
647
+ const result = {};
648
+ result[options.xml.xmlCharKey] = serializedValue;
649
+ result[XML_ATTRKEY] = xmlNamespace;
650
+ return result;
651
+ }
652
+ function isSpecialXmlProperty(propertyName, options) {
653
+ return [XML_ATTRKEY, options.xml.xmlCharKey].includes(propertyName);
654
+ }
655
+ function deserializeCompositeType(serializer, mapper, responseBody, objectName, options) {
656
+ const xmlCharKey = options.xml.xmlCharKey ?? XML_CHARKEY;
657
+ if (getPolymorphicDiscriminatorRecursively(serializer, mapper)) {
658
+ mapper = getPolymorphicMapper(serializer, mapper, responseBody, "serializedName");
659
+ }
660
+ const modelProps = resolveModelProperties(serializer, mapper, objectName);
661
+ let instance = {};
662
+ const handledPropertyNames = [];
663
+ for (const key of Object.keys(modelProps)) {
664
+ const propertyMapper = modelProps[key];
665
+ const paths = splitSerializeName(modelProps[key].serializedName);
666
+ handledPropertyNames.push(paths[0]);
667
+ const { serializedName, xmlName, xmlElementName } = propertyMapper;
668
+ let propertyObjectName = objectName;
669
+ if (serializedName !== "" && serializedName !== undefined) {
670
+ propertyObjectName = objectName + "." + serializedName;
671
+ }
672
+ const headerCollectionPrefix = propertyMapper.headerCollectionPrefix;
673
+ if (headerCollectionPrefix) {
674
+ const dictionary = {};
675
+ for (const headerKey of Object.keys(responseBody)) {
676
+ if (headerKey.startsWith(headerCollectionPrefix)) {
677
+ dictionary[headerKey.substring(headerCollectionPrefix.length)] = serializer.deserialize(propertyMapper.type.value, responseBody[headerKey], propertyObjectName, options);
678
+ }
679
+ handledPropertyNames.push(headerKey);
680
+ }
681
+ instance[key] = dictionary;
682
+ }
683
+ else if (serializer.isXML) {
684
+ if (propertyMapper.xmlIsAttribute && responseBody[XML_ATTRKEY]) {
685
+ instance[key] = serializer.deserialize(propertyMapper, responseBody[XML_ATTRKEY][xmlName], propertyObjectName, options);
686
+ }
687
+ else if (propertyMapper.xmlIsMsText) {
688
+ if (responseBody[xmlCharKey] !== undefined) {
689
+ instance[key] = responseBody[xmlCharKey];
690
+ }
691
+ else if (typeof responseBody === "string") {
692
+ // The special case where xml parser parses "<Name>content</Name>" into JSON of
693
+ // `{ name: "content"}` instead of `{ name: { "_": "content" }}`
694
+ instance[key] = responseBody;
695
+ }
696
+ }
697
+ else {
698
+ const propertyName = xmlElementName || xmlName || serializedName;
699
+ if (propertyMapper.xmlIsWrapped) {
700
+ /* a list of <xmlElementName> wrapped by <xmlName>
701
+ For the xml example below
702
+ <Cors>
703
+ <CorsRule>...</CorsRule>
704
+ <CorsRule>...</CorsRule>
705
+ </Cors>
706
+ the responseBody has
707
+ {
708
+ Cors: {
709
+ CorsRule: [{...}, {...}]
710
+ }
711
+ }
712
+ xmlName is "Cors" and xmlElementName is"CorsRule".
713
+ */
714
+ const wrapped = responseBody[xmlName];
715
+ const elementList = wrapped?.[xmlElementName] ?? [];
716
+ instance[key] = serializer.deserialize(propertyMapper, elementList, propertyObjectName, options);
717
+ handledPropertyNames.push(xmlName);
718
+ }
719
+ else {
720
+ const property = responseBody[propertyName];
721
+ instance[key] = serializer.deserialize(propertyMapper, property, propertyObjectName, options);
722
+ handledPropertyNames.push(propertyName);
723
+ }
724
+ }
725
+ }
726
+ else {
727
+ // deserialize the property if it is present in the provided responseBody instance
728
+ let propertyInstance;
729
+ let res = responseBody;
730
+ // traversing the object step by step.
731
+ let steps = 0;
732
+ for (const item of paths) {
733
+ if (!res)
734
+ break;
735
+ steps++;
736
+ res = res[item];
737
+ }
738
+ // only accept null when reaching the last position of object otherwise it would be undefined
739
+ if (res === null && steps < paths.length) {
740
+ res = undefined;
741
+ }
742
+ propertyInstance = res;
743
+ const polymorphicDiscriminator = mapper.type.polymorphicDiscriminator;
744
+ // checking that the model property name (key)(ex: "fishtype") and the
745
+ // clientName of the polymorphicDiscriminator {metadata} (ex: "fishtype")
746
+ // instead of the serializedName of the polymorphicDiscriminator (ex: "fish.type")
747
+ // is a better approach. The generator is not consistent with escaping '\.' in the
748
+ // serializedName of the property (ex: "fish\.type") that is marked as polymorphic discriminator
749
+ // and the serializedName of the metadata polymorphicDiscriminator (ex: "fish.type"). However,
750
+ // the clientName transformation of the polymorphicDiscriminator (ex: "fishtype") and
751
+ // the transformation of model property name (ex: "fishtype") is done consistently.
752
+ // Hence, it is a safer bet to rely on the clientName of the polymorphicDiscriminator.
753
+ if (polymorphicDiscriminator &&
754
+ key === polymorphicDiscriminator.clientName &&
755
+ (propertyInstance === undefined || propertyInstance === null)) {
756
+ propertyInstance = mapper.serializedName;
757
+ }
758
+ let serializedValue;
759
+ // paging
760
+ if (Array.isArray(responseBody[key]) && modelProps[key].serializedName === "") {
761
+ propertyInstance = responseBody[key];
762
+ const arrayInstance = serializer.deserialize(propertyMapper, propertyInstance, propertyObjectName, options);
763
+ // Copy over any properties that have already been added into the instance, where they do
764
+ // not exist on the newly de-serialized array
765
+ for (const [k, v] of Object.entries(instance)) {
766
+ if (!Object.prototype.hasOwnProperty.call(arrayInstance, k)) {
767
+ arrayInstance[k] = v;
768
+ }
769
+ }
770
+ instance = arrayInstance;
771
+ }
772
+ else if (propertyInstance !== undefined || propertyMapper.defaultValue !== undefined) {
773
+ serializedValue = serializer.deserialize(propertyMapper, propertyInstance, propertyObjectName, options);
774
+ instance[key] = serializedValue;
775
+ }
776
+ }
777
+ }
778
+ const additionalPropertiesMapper = mapper.type.additionalProperties;
779
+ if (additionalPropertiesMapper) {
780
+ const isAdditionalProperty = (responsePropName) => {
781
+ for (const clientPropName in modelProps) {
782
+ const paths = splitSerializeName(modelProps[clientPropName].serializedName);
783
+ if (paths[0] === responsePropName) {
784
+ return false;
785
+ }
786
+ }
787
+ return true;
788
+ };
789
+ for (const responsePropName in responseBody) {
790
+ if (isAdditionalProperty(responsePropName)) {
791
+ instance[responsePropName] = serializer.deserialize(additionalPropertiesMapper, responseBody[responsePropName], objectName + '["' + responsePropName + '"]', options);
792
+ }
793
+ }
794
+ }
795
+ else if (responseBody && !options.ignoreUnknownProperties) {
796
+ for (const key of Object.keys(responseBody)) {
797
+ if (instance[key] === undefined &&
798
+ !handledPropertyNames.includes(key) &&
799
+ !isSpecialXmlProperty(key, options)) {
800
+ instance[key] = responseBody[key];
801
+ }
802
+ }
803
+ }
804
+ return instance;
805
+ }
806
+ function deserializeDictionaryType(serializer, mapper, responseBody, objectName, options) {
807
+ /* jshint validthis: true */
808
+ const value = mapper.type.value;
809
+ if (!value || typeof value !== "object") {
810
+ throw new Error(`"value" metadata for a Dictionary must be defined in the ` +
811
+ `mapper and it must of type "object" in ${objectName}`);
812
+ }
813
+ if (responseBody) {
814
+ const tempDictionary = {};
815
+ for (const key of Object.keys(responseBody)) {
816
+ tempDictionary[key] = serializer.deserialize(value, responseBody[key], objectName, options);
817
+ }
818
+ return tempDictionary;
819
+ }
820
+ return responseBody;
821
+ }
822
+ function deserializeSequenceType(serializer, mapper, responseBody, objectName, options) {
823
+ let element = mapper.type.element;
824
+ if (!element || typeof element !== "object") {
825
+ throw new Error(`element" metadata for an Array must be defined in the ` +
826
+ `mapper and it must of type "object" in ${objectName}`);
827
+ }
828
+ if (responseBody) {
829
+ if (!Array.isArray(responseBody)) {
830
+ // xml2js will interpret a single element array as just the element, so force it to be an array
831
+ responseBody = [responseBody];
832
+ }
833
+ // Quirk: Composite mappers referenced by `element` might
834
+ // not have *all* properties declared (like uberParent),
835
+ // so let's try to look up the full definition by name.
836
+ if (element.type.name === "Composite" && element.type.className) {
837
+ element = serializer.modelMappers[element.type.className] ?? element;
838
+ }
839
+ const tempArray = [];
840
+ for (let i = 0; i < responseBody.length; i++) {
841
+ tempArray[i] = serializer.deserialize(element, responseBody[i], `${objectName}[${i}]`, options);
842
+ }
843
+ return tempArray;
844
+ }
845
+ return responseBody;
846
+ }
847
+ function getIndexDiscriminator(discriminators, discriminatorValue, typeName) {
848
+ const typeNamesToCheck = [typeName];
849
+ while (typeNamesToCheck.length) {
850
+ const currentName = typeNamesToCheck.shift();
851
+ const indexDiscriminator = discriminatorValue === currentName
852
+ ? discriminatorValue
853
+ : currentName + "." + discriminatorValue;
854
+ if (Object.prototype.hasOwnProperty.call(discriminators, indexDiscriminator)) {
855
+ return discriminators[indexDiscriminator];
856
+ }
857
+ else {
858
+ for (const [name, mapper] of Object.entries(discriminators)) {
859
+ if (name.startsWith(currentName + ".") &&
860
+ mapper.type.uberParent === currentName &&
861
+ mapper.type.className) {
862
+ typeNamesToCheck.push(mapper.type.className);
863
+ }
864
+ }
865
+ }
866
+ }
867
+ return undefined;
868
+ }
869
+ function getPolymorphicMapper(serializer, mapper, object, polymorphicPropertyName) {
870
+ const polymorphicDiscriminator = getPolymorphicDiscriminatorRecursively(serializer, mapper);
871
+ if (polymorphicDiscriminator) {
872
+ let discriminatorName = polymorphicDiscriminator[polymorphicPropertyName];
873
+ if (discriminatorName) {
874
+ // The serializedName might have \\, which we just want to ignore
875
+ if (polymorphicPropertyName === "serializedName") {
876
+ discriminatorName = discriminatorName.replace(/\\/gi, "");
877
+ }
878
+ const discriminatorValue = object[discriminatorName];
879
+ const typeName = mapper.type.uberParent ?? mapper.type.className;
880
+ if (typeof discriminatorValue === "string" && typeName) {
881
+ const polymorphicMapper = getIndexDiscriminator(serializer.modelMappers.discriminators, discriminatorValue, typeName);
882
+ if (polymorphicMapper) {
883
+ mapper = polymorphicMapper;
884
+ }
885
+ }
886
+ }
887
+ }
888
+ return mapper;
889
+ }
890
+ function getPolymorphicDiscriminatorRecursively(serializer, mapper) {
891
+ return (mapper.type.polymorphicDiscriminator ||
892
+ getPolymorphicDiscriminatorSafely(serializer, mapper.type.uberParent) ||
893
+ getPolymorphicDiscriminatorSafely(serializer, mapper.type.className));
894
+ }
895
+ function getPolymorphicDiscriminatorSafely(serializer, typeName) {
896
+ return (typeName &&
897
+ serializer.modelMappers[typeName] &&
898
+ serializer.modelMappers[typeName].type.polymorphicDiscriminator);
899
+ }
900
+ /**
901
+ * Known types of Mappers
902
+ */
903
+ export const MapperTypeNames = {
904
+ Base64Url: "Base64Url",
905
+ Boolean: "Boolean",
906
+ ByteArray: "ByteArray",
907
+ Composite: "Composite",
908
+ Date: "Date",
909
+ DateTime: "DateTime",
910
+ DateTimeRfc1123: "DateTimeRfc1123",
911
+ Dictionary: "Dictionary",
912
+ Enum: "Enum",
913
+ Number: "Number",
914
+ Object: "Object",
915
+ Sequence: "Sequence",
916
+ String: "String",
917
+ Stream: "Stream",
918
+ TimeSpan: "TimeSpan",
919
+ UnixTime: "UnixTime",
920
+ };
921
+ //# sourceMappingURL=serializer.js.map