1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | // Licensed under the MIT License.
|
3 |
|
4 | import { RpcNullableBool, RpcNullableDouble, RpcNullableString, RpcNullableTimestamp } from '@azure/functions-core';
|
5 | import { AzFuncSystemError } from '../errors';
|
6 | import { isDefined } from '../utils/nonNull';
|
7 |
|
8 | /**
|
9 | * Converts boolean input to an 'INullableBool' to be sent through the RPC layer.
|
10 | * Input that is not a boolean but is also not null or undefined logs a function app level warning.
|
11 | * @param nullable Input to be converted to an INullableBool if it is a valid boolean
|
12 | * @param propertyName The name of the property that the caller will assign the output to. Used for debugging.
|
13 | */
|
14 | export function toNullableBool(nullable: boolean | undefined, propertyName: string): undefined | RpcNullableBool {
|
15 | if (typeof nullable === 'boolean') {
|
16 | return <RpcNullableBool>{
|
17 | value: nullable,
|
18 | };
|
19 | }
|
20 |
|
21 | if (isDefined(nullable)) {
|
22 | throw new AzFuncSystemError(
|
23 | `A 'boolean' type was expected instead of a '${typeof nullable}' type. Cannot parse value of '${propertyName}'.`
|
24 | );
|
25 | }
|
26 |
|
27 | return undefined;
|
28 | }
|
29 |
|
30 | /**
|
31 | * Converts number or string that parses to a number to an 'INullableDouble' to be sent through the RPC layer.
|
32 | * Input that is not a valid number but is also not null or undefined logs a function app level warning.
|
33 | * @param nullable Input to be converted to an INullableDouble if it is a valid number
|
34 | * @param propertyName The name of the property that the caller will assign the output to. Used for debugging.
|
35 | */
|
36 | export function toNullableDouble(
|
37 | nullable: number | string | undefined,
|
38 | propertyName: string
|
39 | ): undefined | RpcNullableDouble {
|
40 | if (typeof nullable === 'number') {
|
41 | return <RpcNullableDouble>{
|
42 | value: nullable,
|
43 | };
|
44 | } else if (typeof nullable === 'string') {
|
45 | if (!isNaN(Number(nullable))) {
|
46 | const parsedNumber = parseFloat(nullable);
|
47 | return <RpcNullableDouble>{
|
48 | value: parsedNumber,
|
49 | };
|
50 | }
|
51 | }
|
52 |
|
53 | if (isDefined(nullable)) {
|
54 | throw new AzFuncSystemError(
|
55 | `A 'number' type was expected instead of a '${typeof nullable}' type. Cannot parse value of '${propertyName}'.`
|
56 | );
|
57 | }
|
58 |
|
59 | return undefined;
|
60 | }
|
61 |
|
62 | /**
|
63 | * Converts string input to an 'INullableString' to be sent through the RPC layer.
|
64 | * Input that is not a string but is also not null or undefined logs a function app level warning.
|
65 | * @param nullable Input to be converted to an INullableString if it is a valid string
|
66 | * @param propertyName The name of the property that the caller will assign the output to. Used for debugging.
|
67 | */
|
68 | export function toRpcString(nullable: string | undefined, propertyName: string): string {
|
69 | if (typeof nullable === 'string') {
|
70 | return nullable;
|
71 | }
|
72 |
|
73 | if (isDefined(nullable)) {
|
74 | throw new AzFuncSystemError(
|
75 | `A 'string' type was expected instead of a '${typeof nullable}' type. Cannot parse value of '${propertyName}'.`
|
76 | );
|
77 | }
|
78 |
|
79 | return '';
|
80 | }
|
81 |
|
82 | /**
|
83 | * Converts string input to an 'INullableString' to be sent through the RPC layer.
|
84 | * Input that is not a string but is also not null or undefined logs a function app level warning.
|
85 | * @param nullable Input to be converted to an INullableString if it is a valid string
|
86 | * @param propertyName The name of the property that the caller will assign the output to. Used for debugging.
|
87 | */
|
88 | export function toNullableString(nullable: string | undefined, propertyName: string): undefined | RpcNullableString {
|
89 | if (typeof nullable === 'string') {
|
90 | return <RpcNullableString>{
|
91 | value: nullable,
|
92 | };
|
93 | }
|
94 |
|
95 | if (isDefined(nullable)) {
|
96 | throw new AzFuncSystemError(
|
97 | `A 'string' type was expected instead of a '${typeof nullable}' type. Cannot parse value of '${propertyName}'.`
|
98 | );
|
99 | }
|
100 |
|
101 | return undefined;
|
102 | }
|
103 |
|
104 | /**
|
105 | * Converts Date or number input to an 'INullableTimestamp' to be sent through the RPC layer.
|
106 | * Input that is not a Date or number but is also not null or undefined logs a function app level warning.
|
107 | * @param nullable Input to be converted to an INullableTimestamp if it is valid input
|
108 | * @param propertyName The name of the property that the caller will assign the output to. Used for debugging.
|
109 | */
|
110 | export function toNullableTimestamp(
|
111 | dateTime: Date | number | undefined,
|
112 | propertyName: string
|
113 | ): RpcNullableTimestamp | undefined {
|
114 | if (isDefined(dateTime)) {
|
115 | try {
|
116 | const timeInMilliseconds = typeof dateTime === 'number' ? dateTime : dateTime.getTime();
|
117 |
|
118 | if (timeInMilliseconds && timeInMilliseconds >= 0) {
|
119 | return {
|
120 | value: {
|
121 | seconds: Math.round(timeInMilliseconds / 1000),
|
122 | },
|
123 | };
|
124 | }
|
125 | } catch {
|
126 | throw new AzFuncSystemError(
|
127 | `A 'number' or 'Date' input was expected instead of a '${typeof dateTime}'. Cannot parse value of '${propertyName}'.`
|
128 | );
|
129 | }
|
130 | }
|
131 | return undefined;
|
132 | }
|