<commit_before>import View from './view';
import { ServerResponse } from 'http';
import Action, { RenderOptions } from '../runtime/action';

export interface RelationshipConfig {
  strategy?: 'embed' | 'id' | string;
  key?: string;
  serializer?: string;
}

/**
 * Serializers allow you to customize what data is returned in the response and apply simple
 * transformations to it. They allow you to decouple what data is sent from how that data is
 * structured / rendered.
 *
 * @package data
 */
export default abstract class Serializer extends View {

  protected contentType = 'application/json';

  /**
   * The list of attribute names that should be serialized. Attributes not included in this list
   * will be omitted from the final rendered payload.
   */
  protected abstract attributes: string[];

  /**
   * An object with configuration on how to serialize relationships. Relationships that have no
   * configuration present are omitted from the final rendered payload.
   *
   * Out of the box, one option is supported:
   *
   * **strategy**
   *
   * It has one of two possible values:
   *
   *   * `embed`: embed all related records in the response payload
   *   * `id`: include only the id of the related record(s)
   *
   * What the embedded records or ids look like is up to each serializer to determine.
   */
  protected abstract relationships: { [ relationshipName: string ]: RelationshipConfig };

  async render(action: Action, response: ServerResponse, body: any, options: RenderOptions): Promise<void> {
    response.setHeader('Content-type', this.contentType);
    body = await this.serialize(action, body, options);
    let isProduction = this.container.lookup('config:environment').environment === 'production';
    response.write(JSON.stringify(body , null, isProduction ? 0 : 2) || '');
    response.end();
  }

  protected abstract async serialize(action: Action, body: any, options: RenderOptions): Promise<any>;

}<commit_msg>fix(runtime): use the status code of any rendered errors as the response status code<commit_after>import View from './view';
import { ServerResponse } from 'http';
import Action, { RenderOptions } from '../runtime/action';
import Errors from '../runtime/errors';

export interface RelationshipConfig {
  strategy?: 'embed' | 'id' | string;
  key?: string;
  serializer?: string;
}

/**
 * Serializers allow you to customize what data is returned in the response and apply simple
 * transformations to it. They allow you to decouple what data is sent from how that data is
 * structured / rendered.
 *
 * @package data
 */
export default abstract class Serializer extends View {

  protected contentType = 'application/json';

  /**
   * The list of attribute names that should be serialized. Attributes not included in this list
   * will be omitted from the final rendered payload.
   */
  protected abstract attributes: string[];

  /**
   * An object with configuration on how to serialize relationships. Relationships that have no
   * configuration present are omitted from the final rendered payload.
   *
   * Out of the box, one option is supported:
   *
   * **strategy**
   *
   * It has one of two possible values:
   *
   *   * `embed`: embed all related records in the response payload
   *   * `id`: include only the id of the related record(s)
   *
   * What the embedded records or ids look like is up to each serializer to determine.
   */
  protected abstract relationships: { [ relationshipName: string ]: RelationshipConfig };

  async render(action: Action, response: ServerResponse, body: any, options: RenderOptions): Promise<void> {
    response.setHeader('Content-type', this.contentType);
    if (body instanceof Errors.HttpError) {
      response.statusCode = body.status;
    }
    body = await this.serialize(action, body, options);
    let isProduction = this.container.lookup('config:environment').environment === 'production';
    response.write(JSON.stringify(body , null, isProduction ? 0 : 2) || '');
    response.end();
  }

  protected abstract async serialize(action: Action, body: any, options: RenderOptions): Promise<any>;

}
{'fraction_non_alphanumeric': 0.0990314769975787, 'fraction_numerical': 0.0009685230024213075, 'mean_word_length': 3.8034883720930233, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5866802', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1052, 'n_words': 445}