Skip to content

Commit

Permalink
feat: add client method
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnritzdoge committed Mar 21, 2021
1 parent 53b257f commit b257087
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/lib/CentraRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join } from 'path';
import { stringify } from 'querystring';
import { URL } from 'url';
import { CentraResponse } from './CentraResponse';
import type { IncomingHttpHeaders } from 'http'
import Client from 'undici/lib/core/client';
import type ClientType from 'undici/types/client';

Expand All @@ -11,9 +12,11 @@ export class CentraRequest {
public url: URL;
public data: string | Buffer | null = null;
public sendDataAs: 'form' | 'json' | 'buffer' | null = null;
public reqHeaders: { [header: string]: string } = {};
public reqHeaders: IncomingHttpHeaders = {};
public coreOptions: ClientType.Options = {};
public timeoutOptions: { bodyTimeout?: number, headersTimeout?: number, keepAliveTimeout?: number } = {};
public kClient?: Client;
public keepClient?: boolean;

/**
* Creates an instance of CentraRequest.
Expand All @@ -28,6 +31,22 @@ export class CentraRequest {

return this;
}

/**
*
*
* @param {Client} client
* @param {boolean} [keepAlive]
* @return {*} {this}
* @memberof CentraRequest
*/
public client(client: Client, keepAlive?: boolean): this {
this.client = client;

if (keepAlive) this.keepClient = true;

return this;
}

/**
*
Expand Down Expand Up @@ -196,17 +215,18 @@ export class CentraRequest {
body: this.data
};

const client = new Client(`${this.url.protocol}//${this.url.host}`, this.coreOptions);
const client = this.kClient ?? new Client(this.url.origin, this.coreOptions);

let centraRes: CentraResponse = new CentraResponse();

client.dispatch(options, {
onData: ((data: Buffer) => {
return void centraRes._addChunk(data);

}),
onError: ((err: Error) => reject(err)),
onComplete: () => {
client.close();
if (!this.keepClient) client.close();
resolve(centraRes)
},
onConnect: () => { },
Expand Down

0 comments on commit b257087

Please sign in to comment.