function extractHttpRequest()

in src/typespec-aaz/src/convertor.ts [283:424]


function extractHttpRequest(
  context: AAZOperationEmitterContext,
  operation: HttpOperation,
  hostParameters: Record<string, CMDSchema>,
): CMDHttpRequest | undefined {
  const request: CMDHttpRequest = {
    method: operation.verb,
  };

  let schemaContext;
  const methodParams = operation.parameters;
  const paramModels: Record<string, Record<string, CMDSchema>> = {};
  // add host parameters from the host path to path parameters
  if (hostParameters && Object.keys(hostParameters).length > 0) {
    paramModels["path"] = {
      ...hostParameters,
    };
  }

  let clientRequestIdName;
  for (const httpProperty of methodParams.properties) {
    if (!["header", "query", "path"].includes(httpProperty.kind)) {
      continue;
    }
    schemaContext = buildSchemaEmitterContext(context, httpProperty);
    const schema = convert2CMDSchema(
      schemaContext,
      httpProperty.property,
      "options" in httpProperty ? httpProperty.options.name : "",
    );
    if (!schema) {
      continue;
    }

    schema.required = !httpProperty.property.optional;

    if (paramModels[httpProperty.kind] === undefined) {
      paramModels[httpProperty.kind] = {};
    }
    paramModels[httpProperty.kind][schema.name] = schema;
    if (httpProperty.kind === "header" && schema.name === "x-ms-client-request-id") {
      clientRequestIdName = schema.name;
    }
  }

  if (paramModels["path"]) {
    request.path = {
      params: [],
    };
    // sort by param name
    for (const name of Object.keys(paramModels["path"]).sort()) {
      request.path.params!.push(paramModels["path"][name]);
    }
  }

  if (paramModels["query"]) {
    request.query = {
      params: [],
    };
    // sort by param name
    for (const name of Object.keys(paramModels["query"]).sort()) {
      request.query.params!.push(paramModels["query"][name]);
    }
  }

  if (paramModels["header"]) {
    request.header = {
      params: [],
    };
    // sort by param name
    for (const name of Object.keys(paramModels["header"]).sort()) {
      if (name === clientRequestIdName) {
        continue;
      }
      request.header.params!.push(paramModels["header"][name]);
    }
    if (clientRequestIdName) {
      request.header.clientRequestId = clientRequestIdName;
    }
  }

  if (methodParams.body && !isVoidType(methodParams.body.type)) {
    const body = methodParams.body!;
    const consumes: string[] = body.contentTypes ?? [];
    if (consumes.length === 0) {
      // we didn't find an explicit content type anywhere, so infer from body.
      if (getModelOrScalarTypeIfNullable(body.type)) {
        consumes.push("application/json");
      }
    }
    if (body.bodyKind === "multipart") {
      throw new Error("NotImplementedError: Multipart form data payloads are not supported.");
    }
    if (isBinaryPayload(body.type, consumes)) {
      throw new Error("NotImplementedError: Binary payloads are not supported.");
    }
    if (consumes.includes("multipart/form-data")) {
      throw new Error("NotImplementedError: Multipart form data payloads are not supported.");
    }

    let schema: CMDSchema | undefined;
    if (body.property) {
      context.tracer.trace("RetrieveBody", context.visibility.toString());
      schema = convert2CMDSchema(
        {
          ...context,
          supportClsSchema: true,
        },
        body.property,
        getJsonName(context, body.property),
      )!;
      schema.required = !body.property.optional;
    } else {
      schema = {
        ...convert2CMDSchemaBase(
          {
            ...context,
            supportClsSchema: true,
          },
          body.type,
        )!,
        name: "body",
        required: true,
      };
    }
    if (schema !== undefined) {
      // schema.required = !methodParams.body.type.optional;
      if (schema.type === "object") {
        schema = {
          ...schema,
          clientFlatten: true,
        } as CMDObjectSchema;
      }
      request.body = {
        json: {
          schema,
        },
      };
    }
  }
  return request;
}