/*
 * Copyright (C) Sapphirecode - All Rights Reserved
 * This file is part of Auth-Server-Helper which is released under MIT.
 * See file 'LICENSE' for full license details.
 * Created by Timo Hocker <timo@scode.ovh>, January 2021
 */

import http from 'http';

export class Response extends http.IncomingMessage {
  body?: string;
}

export function get (
  // eslint-disable-next-line default-param-last
  headers: http.OutgoingHttpHeaders = {},
  body?: string
): Promise<Response> {
  return new Promise ((resolve) => {
    const req = http.request ('http://localhost:3000', {
      headers,
      method: typeof body === 'string' ? 'POST' : 'GET'
    }, (res: Response) => {
      let data = '';
      res.on ('data', (d) => {
        data += d;
      });
      res.on ('end', () => {
        res.body = data;
        resolve (res);
      });
    });
    if (typeof body === 'string')
      req.write (body);
    req.end ();
  });
}

export function modify_signature (signature: string): string {
  const dec = signature.split ('.');
  dec[1] = '';
  return dec.join ('.');
}