function addResponses()

in packages/typespec-powershell/src/convertor/convertor.ts [253:308]


function addResponses(psContext: SdkContext, op: HttpOperation, newOperation: Operation, model: PwshModel, emitterOptions: PSOptions) {
  const responses = op.responses;
  newOperation.responses = newOperation.responses || [];
  newOperation.exceptions = newOperation.exceptions || [];
  if (responses) {
    for (const response of responses) {
      const newResponse = new Response();
      // newOperation.responses[response.statusCode] || newOperation.responses.default;
      // if (!newResponse) {
      //   newOperation.responses[response.statusCode] = newResponse;
      // }
      newResponse.language.default.name = '';
      newResponse.language.default.description = response.description || "";
      const statusCode = response.statusCodes.toString();
      // Add schema for response body
      if (response.responses[0].body) {
        const schema = getSchemaForType(psContext, response.responses[0].body.type);
        (<any>newResponse).schema = schema;
      }
      // Add headers
      // we merge headers here, if the same header is defined in multiple responses, we only add it once.
      // This is aligned with the behavior of typescript emitter and typespec-autorest emitter.
      newResponse.protocol.http = newResponse.protocol.http ?? new Protocol();
      const lroHeaders = ["location", "retry-after", "azure-asyncoperation"];
      const addedKeys: string[] = [];
      for (const innerResponse of response.responses) {
        if (innerResponse.headers) {
          for (const key in innerResponse.headers) {
            if (addedKeys.includes(key) || (emitterOptions["remove-lro-headers"] && lroHeaders.includes(key.toLowerCase()))) {
              continue;
            } else {
              addedKeys.push(key);
            }
            newResponse.protocol.http.headers = newResponse.protocol.http.headers || [];
            const header = innerResponse.headers[key];
            const headerSchema = getSchemaForType(psContext, header.type);
            const headerResponse = new HttpHeader(key, headerSchema);
            headerResponse.language = new Languages();
            headerResponse.language.default = new Language();
            headerResponse.language.default.description = getDoc(psContext.program, header) || "";
            headerResponse.language.default.name = pascalCase(deconstruct(key));
            newResponse.protocol.http.headers.push(headerResponse);
          }
        }
      }
      newResponse.protocol.http.statusCodes = statusCode === "*" ? ["default"] : [statusCode];
      newResponse.protocol.http.knownMediaType = "json";
      newResponse.protocol.http.mediaTypes = ["application/json"];
      if (statusCode.startsWith("2")) {
        newOperation.responses.push(newResponse);
      } else {
        newOperation.exceptions.push(newResponse);
      }
    }
  }
}