Skip to content

Commit

Permalink
v4: types fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
madimp committed Nov 13, 2024
1 parent a8d955a commit 5c1122b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/httpBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class HttpBlock<
return x;
}

protected logger: Logger;
protected logger: Logger<Context>;

constructor({ block, options }: {
block?: DescriptHttpBlockDescription<ParamsOut, Context, HttpResult>;
Expand Down Expand Up @@ -334,7 +334,7 @@ class HttpBlock<
let error;

try {
result = await request(options, this.logger, blockCancel);
result = await request(options, this.logger, context, blockCancel);
headers = result.headers;

} catch (e) {
Expand Down
12 changes: 6 additions & 6 deletions lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface StartLoggerEvent extends BaseLoggerEvent {
export type LoggerEvent = SuccessLoggerEvent | ErrorLoggerEvent | StartLoggerEvent;


class Logger {
class Logger<Context> {
static EVENT = EVENT;

private _debug = false;
Expand All @@ -61,13 +61,13 @@ class Logger {
this._debug = config.debug || false;
}

log(event: LoggerEvent) {
log(event: LoggerEvent, context: Context) {
switch (event.type) {
case EVENT.REQUEST_START: {
if (this._debug) {
const message = `[DEBUG] ${ event.requestOptions.httpOptions.method } ${ event.requestOptions.url }`;

logToStream(process.stdout, message);
logToStream(process.stdout, message, context);
}

break;
Expand All @@ -85,7 +85,7 @@ class Logger {
}
}

logToStream(process.stdout, message);
logToStream(process.stdout, message, context);

break;
}
Expand All @@ -110,7 +110,7 @@ class Logger {
}
message += ` ${ total(event) } ${ event.requestOptions.httpOptions.method } ${ event.requestOptions.url }`;

logToStream(process.stderr, message);
logToStream(process.stderr, message, context);

break;
}
Expand All @@ -124,7 +124,7 @@ class Logger {

// --------------------------------------------------------------------------------------------------------------- //

function logToStream(stream: typeof process.stderr | typeof process.stdout, message: string) {
function logToStream<Context>(stream: typeof process.stderr | typeof process.stdout, message: string, context: Context) {

Check failure on line 127 in lib/logger.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'context' is defined but never used
const date = new Date().toISOString();

stream.write(`${ date } ${ message }\n`);
Expand Down
15 changes: 8 additions & 7 deletions lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ export class RequestOptions {

// --------------------------------------------------------------------------------------------------------------- //

class DescriptRequest {

class DescriptRequest<Context> {
context: Context;
options: RequestOptions;
logger: Logger;
logger: Logger<Context>;
cancel: Cancel;
timestamps: EventTimestamps;
hTimeout: number | null;
Expand All @@ -293,10 +293,11 @@ class DescriptRequest {

deferred: Deffered<DescriptHttpResult, DescriptError>;

constructor(options: RequestOptions, logger: Logger, cancel: Cancel) {
constructor(options: RequestOptions, logger: Logger<Context>, context: Context, cancel: Cancel) {
this.options = options;
this.logger = logger;
this.cancel = cancel;
this.context = context;

this.timestamps = {};
this.hTimeout = null;
Expand Down Expand Up @@ -542,20 +543,20 @@ class DescriptRequest {
log(event: LoggerEvent) {
if (this.logger) {
event.request = this.req;
this.logger.log(event);
this.logger.log(event, this.context);
}
}

}

// --------------------------------------------------------------------------------------------------------------- //

async function request(options: DescriptRequestOptions, logger: Logger, cancel: Cancel): Promise<DescriptHttpResult> {
async function request<Context>(options: DescriptRequestOptions, logger: Logger<Context>, context: Context, cancel: Cancel): Promise<DescriptHttpResult> {
const requestOptions = new RequestOptions(options);

// eslint-disable-next-line no-constant-condition
while (true) {
const req = new DescriptRequest(requestOptions, logger, cancel);
const req = new DescriptRequest(requestOptions, logger, context, cancel);

try {
const result = await req.start();
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,5 @@ export interface DescriptBlockOptions<

required?: boolean;

logger?: DescriptLogger;
logger?: DescriptLogger<Context>;
}
18 changes: 10 additions & 8 deletions tests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import type Logger from '../lib/logger';

// --------------------------------------------------------------------------------------------------------------- //

function getDoRequest(defaultOptions: DescriptRequestOptions) {
return function doRequest(options: DescriptRequestOptions = {}, logger?: Logger, cancel?: Cancel) {
function getDoRequest<Context>(defaultOptions: DescriptRequestOptions, context: Context) {
return function doRequest(options: DescriptRequestOptions = {}, logger?: Logger<Context>, cancel?: Cancel) {
logger = logger || new de.Logger({ debug: true });
cancel = cancel || new de.Cancel();

return request({ ...defaultOptions, ...options }, logger, cancel);
return request({ ...defaultOptions, ...options }, logger, context, cancel);
};
}

const context = {};

// --------------------------------------------------------------------------------------------------------------- //

describe('request', () => {
Expand All @@ -44,7 +46,7 @@ describe('request', () => {
hostname: '127.0.0.1',
port: PORT,
pathname: '/',
});
}, context);

const fake = new Server({
module: http,
Expand Down Expand Up @@ -966,7 +968,7 @@ describe('request', () => {
hostname: '127.0.0.1',
port: PORT,
pathname: '/',
});
}, context);

let serverKey;
let serverCert;
Expand Down Expand Up @@ -1070,7 +1072,7 @@ describe('request', () => {
hostname: '127.0.0.1',
port: PORT,
pathname: '/',
});
}, context);

const path = getPath();

Expand Down Expand Up @@ -1100,7 +1102,7 @@ describe('request', () => {
protocol: 'http:',
hostname: '127.0.0.1',
port: PORT,
});
}, context);

beforeAll(() => serverListen(server, PORT));
afterAll(() => serverClose(server));
Expand Down Expand Up @@ -1148,7 +1150,7 @@ describe('request', () => {
protocol: 'http:',
hostname: '127.0.0.1',
port: PORT,
});
}, context);

beforeAll(() => serverListen(server, PORT));
afterAll(() => serverClose(server));
Expand Down

0 comments on commit 5c1122b

Please sign in to comment.