function convert2CMDHttpResponse()

in src/typespec-aaz/src/convertor.ts [535:608]


function convert2CMDHttpResponse(
  context: AAZOperationEmitterContext,
  response: HttpOperationResponse,
  statusCodes: string[] | undefined,
  isError: boolean,
): CMDHttpResponse {
  const res: CMDHttpResponse = {
    statusCode: statusCodes?.map((code) => Number(code)),
    isError: isError ? true : undefined,
    description: response.description ?? getResponseDescriptionForStatusCodes(statusCodes),
  };

  const contentTypes: string[] = [];
  let body: HttpOperationBody | HttpOperationMultipartBody | undefined;
  for (const data of response.responses) {
    if (data.headers && Object.keys(data.headers).length > 0) {
      res.header ??= {
        items: [],
      };
      for (const name of Object.keys(data.headers)) {
        res.header.items.push({ name });
      }
    }
    if (data.body) {
      if (body && body.type !== data.body.type) {
        reportDiagnostic(context.program, {
          code: "duplicate-body-types",
          target: response.type,
        });
      }
      body = data.body;
      contentTypes.push(...data.body.contentTypes);
    }
  }
  if (body) {
    const isBinary = contentTypes.every((t) => isBinaryPayload(body!.type, t));
    if (isBinary) {
      throw new Error("NotImplementedError: Binary response are not supported.");
    }
    if (body.bodyKind === "multipart") {
      throw new Error("NotImplementedError: Multipart form data responses are not supported.");
    }
    let schema;
    if (isError) {
      const errorFormat = classifyErrorFormat(context, body.type);
      if (errorFormat === undefined) {
        throw new Error("Error response schema is not supported yet.");
      }
      schema = {
        readOnly: true,
        type: `@${errorFormat}`,
      };
    } else {
      schema = convert2CMDSchemaBase(
        {
          ...context,
          supportClsSchema: true,
          visibility: Visibility.Read,
        },
        body.type,
      );
    }
    if (!schema) {
      throw new Error("Invalid Response Schema. It's None.");
    }

    res.body = {
      json: {
        schema: schema,
      },
    };
  }
  return res;
}