operationParameter.ts 2.2 KB

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