Skip to content

Commit

Permalink
type fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Oct 11, 2023
1 parent bc8738c commit 8178ad7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"eslint.enable": true,
"typescript.surveys.enabled": false,
"typescript.tsdk": "node_modules/typescript/lib"
}
"typescript.surveys.enabled": false,
"typescript.tsdk": "node_modules/typescript/lib"
}
8 changes: 0 additions & 8 deletions jsconfig.json

This file was deleted.

33 changes: 24 additions & 9 deletions src/context.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { BulkheadOptions } from "./middlewares";
import Endpoint = require("./registry/endpoint");
import ActionEndpoint = require("./registry/endpoint-action");
import EventEndpoint = require("./registry/endpoint-event");
import type { CallingOptions, MCallDefinition, MCallCallingOptions } from "./service-broker";
import Service = require("./service");
import Span = require("./tracing/span");
import type { ActionHandler, ActionParams, ActionSchema, TracingEventOptions } from "./service";
import type ServiceBroker = require("./service-broker");
import { Stream } from "stream";
import util = require("util");

declare namespace Context {
export interface EventSchema {
Expand All @@ -27,22 +30,22 @@ declare namespace Context {
}
}

declare class Context<TParams = unknown, TMeta extends object = {}, TLocals = Record<string, any>> {
declare class Context<TParams = unknown, TMeta extends object = {}, TLocals = Record<string, any>, THeaders = Record<string, any>> {
static create(
broker: ServiceBroker,
endpoint: Endpoint,
endpoint: ActionEndpoint|EventEndpoint,
params: Record<string, any>,
opts: Record<string, any>
): Context;
static create(broker: ServiceBroker, endpoint: Endpoint, params: Record<string, any>): Context;
static create(broker: ServiceBroker, endpoint: Endpoint): Context;
static create(broker: ServiceBroker, endpoint: ActionEndpoint|EventEndpoint, params: Record<string, any>): Context;
static create(broker: ServiceBroker, endpoint: ActionEndpoint|EventEndpoint): Context;
static create(broker: ServiceBroker): Context;

id: string;

broker: ServiceBroker;

endpoint: Endpoint | null;
endpoint: ActionEndpoint | EventEndpoint | null;

action: ActionSchema | null;

Expand Down Expand Up @@ -80,13 +83,19 @@ declare class Context<TParams = unknown, TMeta extends object = {}, TLocals = Re

meta: TMeta;

headers: THeaders;

responseHeaders: THeaders;

requestID: string | null;

stream: Stream | null;

cachedResult: boolean;

constructor(broker: ServiceBroker, endpoint: Endpoint);
constructor(broker: ServiceBroker, endpoint: ActionEndpoint|EventEndpoint);

setEndpoint(endpoint: Endpoint): void;
setEndpoint(endpoint: ActionEndpoint|EventEndpoint): void;

setParams(newParams: TParams, cloning?: boolean): void;

Expand Down Expand Up @@ -115,13 +124,19 @@ declare class Context<TParams = unknown, TMeta extends object = {}, TLocals = Re
broadcast<D>(eventName: string, data: D): Promise<void>;
broadcast(eventName: string): Promise<void>;

copy(endpoint: Endpoint): this;
copy(endpoint: ActionEndpoint|EventEndpoint): this;
copy(): this;

startSpan(name: string, opts?: Record<string, any>): Span;

finishSpan(span: Span, time?: number): void;

toJSON(): Record<string, any>;

startHrTime: [number, number] | null;

_spanStack: Span[];

[util.inspect.custom](depth?: number, options?: Record<string, any>): string;
}
export = Context;
44 changes: 20 additions & 24 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ const util = require("util");
const { pick } = require("lodash");
const { RequestSkippedError, MaxCallLevelError } = require("./errors");

/**
* @typedef {import("./service-broker")} ServiceBroker Moleculer Service Broker instance
* @typedef {import("./service-broker").CallingOptions} CallingOptions Calling Options
* @typedef {import("./registry/endpoint")} Endpoint Registry Endpoint
* @typedef {import("./registry/endpoint-action")} ActionEndpoint Registry Action Endpoint
* @typedef {import("./registry/endpoint-event")} EventEndpoint Registry Event Endpoint
* @typedef {import("./tracing/span")} Span Tracing Span
*/

/**
* Merge metadata
*
* @param {Context} ctx
* @param {Object} newMeta
*
* @private
* @memberof Context
*/
function mergeMeta(ctx, newMeta) {
if (newMeta) Object.assign(ctx.meta, newMeta);
Expand All @@ -26,22 +33,14 @@ function mergeMeta(ctx, newMeta) {
/**
* Context class for action calls
*
* @property {String} id - Context ID
* @property {ServiceBroker} broker - Broker instance
* @property {Action} action - Action definition
* @property {String} [nodeID=null] - Node ID
* @property {String} parentID - Parent Context ID
* @property {Boolean} tracing - Enable tracing
* @property {Number} [level=1] - Level of context
*
* @class Context
*/
class Context {
/**
* Creates an instance of Context.
*
* @param {ServiceBroker} broker - Broker instance
* @param {Endpoint} endpoint
* @param {ActionEndpoint|EventEndpoint} endpoint
*
* @memberof Context
*/
Expand Down Expand Up @@ -70,6 +69,7 @@ class Context {
// The groups of event
this.eventGroups = null;

/** @type {CallingOptions} */
this.options = {
timeout: null,
retries: null
Expand Down Expand Up @@ -97,22 +97,18 @@ class Context {
this.needAck = null;
this.ackID = null;

//this.startTime = null;
//his.startHrTime = null;
//this.stopTime = null;
//this.duration = null;
this.startHrTime = null;

//this.error = null;
this.cachedResult = false;
}

/**
* Create a new Context instance
*
* @param {ServiceBroker} broker
* @param {Endpoint} endpoint
* @param {ActionEndpoint|EventEndpoint} endpoint
* @param {Object?} params
* @param {Object} opts
* @param {CallingOptions} opts
* @returns {Context}
*
* @static
Expand Down Expand Up @@ -184,7 +180,7 @@ class Context {
* @returns {Context}
*/
copy(ep) {
const newCtx = new this.constructor(this.broker);
const newCtx = this.constructor(this.broker);

newCtx.nodeID = this.nodeID;
newCtx.setEndpoint(ep || this.endpoint);
Expand Down Expand Up @@ -215,7 +211,7 @@ class Context {
/**
* Set endpoint of context
*
* @param {Endpoint} endpoint
* @param {ActionEndpoint|EventEndpoint} endpoint
* @memberof Context
*/
setEndpoint(endpoint) {
Expand Down Expand Up @@ -252,7 +248,7 @@ class Context {
*
* @param {String} actionName
* @param {Object?} params
* @param {Object?} opts
* @param {Object?} _opts
* @returns {Promise}
*
* @example <caption>Call an other service with params & options</caption>
Expand Down Expand Up @@ -369,7 +365,7 @@ class Context {
* Emit an event (grouped & balanced global event)
*
* @param {string} eventName
* @param {any?} payload
* @param {any?} data
* @param {Object?} opts
* @returns {Promise}
*
Expand All @@ -391,7 +387,7 @@ class Context {
* Emit an event for all local & remote services
*
* @param {string} eventName
* @param {any?} payload
* @param {any?} data
* @param {Object?} opts
* @returns {Promise}
*
Expand Down
5 changes: 3 additions & 2 deletions src/service-broker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ declare namespace ServiceBroker {
export type FallbackResponseHandler = (ctx: Context, err: MoleculerError) => Promise<any>;

export interface CallingOptions {
timeout?: number;
retries?: number;
timeout?: number | null;
retries?: number | null;
fallbackResponse?: FallbackResponse | FallbackResponse[] | FallbackResponseHandler;
nodeID?: string;
meta?: Record<string, any>;
Expand All @@ -228,6 +228,7 @@ declare namespace ServiceBroker {
tracking?: boolean;
paramsCloning?: boolean;
caller?: string;
headers?: Record<string, any>;
}

export interface MCallCallingOptions extends CallingOptions {
Expand Down
5 changes: 3 additions & 2 deletions src/tracing/span.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ declare class Span {
logs: Span.SpanLogEntry[];
tags: Record<string, any>;

start(time?: number): Span;
start(time?: number | null): Span;
addTags(obj: Record<string, any>): Span;
log(name: string, fields?: Record<string, any>, time?: number): Span;
setError(err: Error): Span;
finish(time?: number): Span;
finish(time?: number | null): Span;
startSpan(name: string, opts?: Record<string, any>): Span;
isActive(): boolean;
}

export = Span;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"outDir": "dist",
"target": "ES2022",
"moduleResolution": "node",
"noEmit": true
"noEmit": true,
},
"include": ["**.js"],
"exclude": ["node_modules", ".eslintrc.js", "prettier.config.js", "types"]
Expand Down

0 comments on commit 8178ad7

Please sign in to comment.