UNPKG

1.68 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4import { RpcTypedData } from '@azure/functions-core';
5import { HttpRequest } from '../http/HttpRequest';
6import { isDefined } from '../utils/nonNull';
7
8export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown {
9 if (!data) {
10 return undefined;
11 } else if (isDefined(data.string)) {
12 return tryJsonParse(data.string);
13 } else if (isDefined(data.json)) {
14 return JSON.parse(data.json);
15 } else if (isDefined(data.bytes)) {
16 return Buffer.from(data.bytes);
17 } else if (isDefined(data.stream)) {
18 return Buffer.from(data.stream);
19 } else if (isDefined(data.http)) {
20 return new HttpRequest(data.http);
21 } else if (isDefined(data.int)) {
22 return data.int;
23 } else if (isDefined(data.double)) {
24 return data.double;
25 } else if (data.collectionBytes && isDefined(data.collectionBytes.bytes)) {
26 return data.collectionBytes.bytes.map((d) => Buffer.from(d));
27 } else if (data.collectionString && isDefined(data.collectionString.string)) {
28 return data.collectionString.string.map(tryJsonParse);
29 } else if (data.collectionDouble && isDefined(data.collectionDouble.double)) {
30 return data.collectionDouble.double;
31 } else if (data.collectionSint64 && isDefined(data.collectionSint64.sint64)) {
32 return data.collectionSint64.sint64;
33 } else {
34 return undefined;
35 }
36}
37
38function tryJsonParse(data: string): unknown {
39 try {
40 return JSON.parse(data);
41 } catch {
42 return data;
43 }
44}