Skip to content

Commit

Permalink
improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 22, 2023
1 parent 59fbdc5 commit cdd119c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ function mergeMeta(ctx, newMeta) {
/**
* Context class for action calls
*
* @type import("./context").Context
* @typedef {import("./context")} ContextInterface
* @implements {ContextInterface}
*/
class Context {
/**
Expand Down
24 changes: 12 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
/*
* moleculer
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
Expand Down Expand Up @@ -37,9 +38,9 @@ class TimeoutError extends ExtendableError {}
/**
* Circular replacing of unsafe properties in object
*
* @param {Object=} options List of options to change circularReplacer behaviour
* @param {number=} options.maxSafeObjectSize Maximum size of objects for safe object converting
* @return {function(...[*]=)}
* @param {object} options List of options to change circularReplacer behaviour
* @param {number?} [options.maxSafeObjectSize = Infinity] Maximum size of objects for safe object converting
* @return {(key: string, value: any) => any}
*/
function circularReplacer(options = { maxSafeObjectSize: Infinity }) {
const seen = new WeakSet();
Expand Down Expand Up @@ -172,7 +173,7 @@ const utils = {
const interfaces = os.networkInterfaces();
for (let iface in interfaces) {
for (let i in interfaces[iface]) {
const f = interfaces[iface][i];
const f = interfaces[iface]?.[i];
if (f.family === "IPv4") {
if (f.internal) {
ilist.push(f.address);
Expand Down Expand Up @@ -200,8 +201,7 @@ const utils = {
/**
* Polyfill a Promise library with missing Bluebird features.
*
*
* @param {PromiseClass} P
* @param {typeof Promise} P
*/
polyfillPromise(P) {
if (!utils.isFunction(P.method)) {
Expand Down Expand Up @@ -381,8 +381,8 @@ const utils = {
* Remove circular references & Functions from the JS object
*
* @param {Object|Array} obj
* @param {Object=} options List of options to change circularReplacer behaviour
* @param {number=} options.maxSafeObjectSize List of options to change circularReplacer behaviour
* @param {object} options List of options to change circularReplacer behaviour
* @param {number?} [options.maxSafeObjectSize = Infinity] Maximum size of objects for safe object converting
* @returns {Object|Array}
*/
safetyObject(obj, options) {
Expand All @@ -392,15 +392,15 @@ const utils = {
/**
* Sets a variable on an object based on its dot path.
*
* @param {Object} obj
* @param {Record<string,any>} obj
* @param {String} path
* @param {*} value
* @returns {Object}
*/
dotSet(obj, path, value) {
const parts = path.split(".");
const part = parts.shift();
if (parts.length > 0) {
if (part && parts.length > 0) {
if (!Object.prototype.hasOwnProperty.call(obj, part)) {
obj[part] = {};
} else if (obj[part] == null) {
Expand Down Expand Up @@ -436,7 +436,7 @@ const utils = {
* Credits: https://github.com/visionmedia/bytes.js
*
* @param {String} v
* @returns {Number}
* @returns {Number|null}
*/
parseByteString(v) {
if (typeof v === "number" && !isNaN(v)) {
Expand Down Expand Up @@ -471,7 +471,7 @@ const utils = {
* Get the name of constructor of an object.
*
* @param {Object} obj
* @returns {String}
* @returns {String|undefined}
*/
getConstructorName(obj) {
if (obj == null) return undefined;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"lib": ["ES2022"],
},
"include": ["**.js"],
"exclude": ["node_modules", ".eslintrc.js", "prettier.config.js", "types"]
"exclude": ["node_modules", ".eslintrc.js", "prettier.config.js"]
}
10 changes: 10 additions & 0 deletions types/extends.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
interface Promise<T> {
delay<T>(ms: number): Promise<T>;
method<T>(fn: Function): Promise<T>;
timeout<T>(ms: number, message: String): Promise<T>;
mapSeries<T>(arr: Array<any>, fn: Function): Promise<T>;
}

interface PromiseConstructor<T> {
delay<T>(ms: number): Promise<T>;
method<T>(fn: Function): Promise<T>;
timeout<T>(ms: number, message: String): Promise<T>;
mapSeries<T>(arr: Array<any>, fn: Function): Promise<T>;
}

0 comments on commit cdd119c

Please sign in to comment.