1 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
2 | // Licensed under the MIT License. See License.txt in the project root for license information.
|
3 |
|
4 | import { QueryCollectionFormat } from "./queryCollectionFormat";
|
5 | import { Mapper } from "./serializer";
|
6 |
|
7 | export type ParameterPath = string | string[] | { [propertyName: string]: ParameterPath };
|
8 |
|
9 | /**
|
10 | * A common interface that all Operation parameter's extend.
|
11 | */
|
12 | export interface OperationParameter {
|
13 | /**
|
14 | * The path to this parameter's value in OperationArguments or the object that contains paths for
|
15 | * each property's value in OperationArguments.
|
16 | */
|
17 | parameterPath: ParameterPath;
|
18 |
|
19 | /**
|
20 | * The mapper that defines how to validate and serialize this parameter's value.
|
21 | */
|
22 | mapper: Mapper;
|
23 | }
|
24 |
|
25 | /**
|
26 | * A parameter for an operation that will be substituted into the operation's request URL.
|
27 | */
|
28 | export interface OperationURLParameter extends OperationParameter {
|
29 | /**
|
30 | * Whether or not to skip encoding the URL parameter's value before adding it to the URL.
|
31 | */
|
32 | skipEncoding?: boolean;
|
33 | }
|
34 |
|
35 | /**
|
36 | * A parameter for an operation that will be added as a query parameter to the operation's HTTP
|
37 | * request.
|
38 | */
|
39 | export interface OperationQueryParameter extends OperationParameter {
|
40 | /**
|
41 | * Whether or not to skip encoding the query parameter's value before adding it to the URL.
|
42 | */
|
43 | skipEncoding?: boolean;
|
44 |
|
45 | /**
|
46 | * If this query parameter's value is a collection, what type of format should the value be
|
47 | * converted to.
|
48 | */
|
49 | collectionFormat?: QueryCollectionFormat;
|
50 | }
|
51 |
|
52 | /**
|
53 | * Get the path to this parameter's value as a dotted string (a.b.c).
|
54 | * @param parameter The parameter to get the path string for.
|
55 | * @returns The path to this parameter's value as a dotted string.
|
56 | */
|
57 | export function getPathStringFromParameter(parameter: OperationParameter): string {
|
58 | return getPathStringFromParameterPath(parameter.parameterPath, parameter.mapper);
|
59 | }
|
60 |
|
61 | export function getPathStringFromParameterPath(
|
62 | parameterPath: ParameterPath,
|
63 | mapper: Mapper
|
64 | ): string {
|
65 | let result: string;
|
66 | if (typeof parameterPath === "string") {
|
67 | result = parameterPath;
|
68 | } else if (Array.isArray(parameterPath)) {
|
69 | result = parameterPath.join(".");
|
70 | } else {
|
71 | result = mapper.serializedName!;
|
72 | }
|
73 | return result;
|
74 | }
|