Skip to content

Add more manual type declarations #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
.husky/_
packages/crash-handler/**/*.d.*
packages/errors/**/*.d.*
packages/fetch-error-handler/**/*.d.*
packages/log-error/**/*.d.*
packages/logger/**/*.d.*
packages/middleware-log-errors/**/*.d.*
packages/middleware-render-error-info/**/*.d.*
packages/opentelemetry/**/*.d.*
coverage
node_modules/
resources/logos/dist
6 changes: 1 addition & 5 deletions jsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
"removeComments": true
},
"include": [
"packages/crash-handler/**/*.js",
"packages/errors/**/*.js",
"packages/fetch-error-handler/**/*.js",
"packages/log-error/**/*.js",
"packages/logger/**/*.js",
"packages/middleware-log-errors/**/*.js",
"packages/middleware-render-error-info/**/*.js",
"packages/opentelemetry/**/*.js"
"packages/middleware-render-error-info/**/*.js"
]
}
2 changes: 0 additions & 2 deletions packages/crash-handler/.npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
!*.d.ts
!*.d.ts.map
CHANGELOG.md
docs
test
12 changes: 1 addition & 11 deletions packages/crash-handler/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@ const {
logUnhandledError
} = require('@dotcom-reliability-kit/log-error');

/**
* @typedef {object} CrashHandlerOptions
* @property {import('@dotcom-reliability-kit/log-error').Logger & {[key: string]: any}} [logger]
* The logger to use to output errors. Defaults to Reliability Kit logger.
* @property {import('process')} [process]
* The Node.js process to add crash handlers for.
*/

/**
* Register a crash handler on a process.
*
* @public
* @param {CrashHandlerOptions} [options]
* Options to configure the crash handler.
* @param {import('@dotcom-reliability-kit/crash-handler').CrashHandlerOptions} [options]
*/
function registerCrashHandler(options = {}) {
const process = options.process || global.process;
Expand Down
3 changes: 2 additions & 1 deletion packages/crash-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"node": "18.x || 20.x || 22.x",
"npm": "8.x || 9.x || 10.x"
},
"main": "lib",
"main": "lib/index.js",
"types": "types/index.d.ts",
"dependencies": {
"@dotcom-reliability-kit/log-error": "^4.1.2"
}
Expand Down
13 changes: 13 additions & 0 deletions packages/crash-handler/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Logger } from '@dotcom-reliability-kit/logger';

declare module '@dotcom-reliability-kit/crash-handler' {
export type CrashHandlerOptions = {
logger?: Logger & { [key: string]: any };
process?: NodeJS.Process;
};

declare function registerCrashHandler(options?: CrashHandlerOptions): void;

export default registerCrashHandler;
export = registerCrashHandler;
}
2 changes: 0 additions & 2 deletions packages/errors/.npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
!*.d.ts
!*.d.ts.map
CHANGELOG.md
docs
test
35 changes: 7 additions & 28 deletions packages/errors/lib/base-error.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
/**
* @typedef {object} ErrorStrictData
* @property {string} [code]
* A machine-readable error code which identifies the specific type of error.
* @property {string} [message]
* A human readable message which describes the error.
* @property {Error | null} [cause]
* The root cause error instance.
*/

/**
* @typedef {ErrorStrictData & Record<string, any>} ErrorData
* @typedef {import('@dotcom-reliability-kit/errors').BaseErrorData} ErrorData
*/

/**
Expand All @@ -19,44 +9,39 @@ class BaseError extends Error {
/**
* @override
* @readonly
* @public
* @type {string}
* @type {import('@dotcom-reliability-kit/errors').BaseError['name']}
*/
name = 'BaseError';

/**
* Whether the error is operational.
*
* @readonly
* @public
* @type {boolean}
* @type {import('@dotcom-reliability-kit/errors').BaseError['isOperational']}
*/
isOperational = false;

/**
* A machine-readable error code which identifies the specific type of error.
*
* @readonly
* @public
* @type {string}
* @type {import('@dotcom-reliability-kit/errors').BaseError['code']}
*/
code = BaseError.defaultCode;

/**
* The root cause error instance.
*
* @readonly
* @public
* @type {Error | null}
* @type {import('@dotcom-reliability-kit/errors').BaseError['cause']}
*/
cause = null;

/**
* Additional error information.
*
* @readonly
* @public
* @type {{[key: string]: any}}
* @type {import('@dotcom-reliability-kit/errors').BaseError['data']}
*/
data = {};

Expand Down Expand Up @@ -134,13 +119,7 @@ class BaseError extends Error {
static defaultMessage = 'An error occurred';

/**
* Get whether an error object is marked as operational (it has a truthy `isOperational` property).
*
* @public
* @param {Error} error
* The error object to check.
* @returns {boolean}
* Returns whether the error is operational.
* @type {(typeof import('@dotcom-reliability-kit/errors').BaseError)['isErrorMarkedAsOperational']}
*/
static isErrorMarkedAsOperational(error) {
// @ts-ignore Error.prototype.isOperational does not exist, but it's OK to check in this
Expand Down
3 changes: 1 addition & 2 deletions packages/errors/lib/data-store-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class DataStoreError extends OperationalError {
/**
* @override
* @readonly
* @public
* @type {string}
* @type {import('@dotcom-reliability-kit/errors').DataStoreError['name']}
*/
name = 'DataStoreError';
}
Expand Down
30 changes: 10 additions & 20 deletions packages/errors/lib/http-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ const OperationalError = require('./operational-error');
const STATUS_CODES = require('http').STATUS_CODES;

/**
* @typedef {object} HttpErrorStrictData
* @property {number} [statusCode]
* An HTTP status code.
*/

/**
* @typedef {HttpErrorStrictData & OperationalError.OperationalErrorData} HttpErrorData
* @typedef {import('@dotcom-reliability-kit/errors').HttpErrorData} ErrorData
*/

/**
Expand All @@ -27,28 +21,24 @@ class HttpError extends OperationalError {
/**
* @override
* @readonly
* @public
* @type {string}
* @type {import('@dotcom-reliability-kit/errors').HttpError['name']}
*/
name = 'HttpError';

/**
* @readonly
* @public
* @type {number}
* @type {import('@dotcom-reliability-kit/errors').HttpError['statusCode']}
*/
statusCode;

/**
* @readonly
* @public
* @type {string}
* @type {import('@dotcom-reliability-kit/errors').HttpError['statusMessage']}
*/
statusMessage;

/**
* @public
* @type {number}
* @type {import('@dotcom-reliability-kit/errors').HttpError['status']}
*/
get status() {
return this.statusCode;
Expand All @@ -63,7 +53,7 @@ class HttpError extends OperationalError {
* Create an error with error data.
*
* @overload
* @param {HttpErrorData} data
* @param {ErrorData} data
* Additional error information.
*/
/**
Expand All @@ -72,7 +62,7 @@ class HttpError extends OperationalError {
* @overload
* @param {string} message
* The error message.
* @param {HttpErrorData} [data]
* @param {ErrorData} [data]
* Additional error information.
*/
/**
Expand All @@ -81,14 +71,14 @@ class HttpError extends OperationalError {
* @overload
* @param {number} status
* The error HTTP status code.
* @param {HttpErrorData} [data]
* @param {ErrorData} [data]
* Additional error information.
*/
/**
* @param {string | number | HttpErrorData} [message]
* @param {string | number | ErrorData} [message]
* The error message if it's a string, the HTTP status code if it's a number, or full error
* information if an object.
* @param {HttpErrorData} [data]
* @param {ErrorData} [data]
* Additional error information if `message` is a string or number.
*/
constructor(message, data = {}) {
Expand Down
25 changes: 8 additions & 17 deletions packages/errors/lib/operational-error.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const BaseError = require('./base-error');

/**
* @typedef {object} OperationalErrorStrictData
* @property {string[]} [relatesToSystems]
* An array of FT system codes which are related to this error.
*/

/**
* @typedef {OperationalErrorStrictData & BaseError.ErrorData} OperationalErrorData
* @typedef {import('@dotcom-reliability-kit/errors').OperationalErrorData} ErrorData
*/

/**
Expand All @@ -17,8 +11,7 @@ class OperationalError extends BaseError {
/**
* @override
* @readonly
* @public
* @type {string}
* @type {import('@dotcom-reliability-kit/errors').OperationalError['name']}
*/
name = 'OperationalError';

Expand All @@ -27,8 +20,7 @@ class OperationalError extends BaseError {
*
* @override
* @readonly
* @public
* @type {boolean}
* @type {import('@dotcom-reliability-kit/errors').OperationalError['isOperational']}
*/
isOperational = true;

Expand All @@ -37,8 +29,7 @@ class OperationalError extends BaseError {
* If this error is caused by one or more dependencies, include their system code here.
*
* @readonly
* @public
* @type {string[]}
* @type {import('@dotcom-reliability-kit/errors').OperationalError['relatesToSystems']}
*/
relatesToSystems = [];

Expand All @@ -51,7 +42,7 @@ class OperationalError extends BaseError {
* Create an error with error data.
*
* @overload
* @param {OperationalErrorData} data
* @param {ErrorData} data
* Additional error information.
*/
/**
Expand All @@ -60,13 +51,13 @@ class OperationalError extends BaseError {
* @overload
* @param {string} message
* The error message.
* @param {OperationalErrorData} [data]
* @param {ErrorData} [data]
* Additional error information.
*/
/**
* @param {string | OperationalErrorData} [message]
* @param {string | ErrorData} [message]
* The error message if it's a string, or full error information if an object.
* @param {OperationalErrorData} [data]
* @param {ErrorData} [data]
* Additional error information if `message` is a string.
*/
constructor(message, data = {}) {
Expand Down
3 changes: 1 addition & 2 deletions packages/errors/lib/upstream-service-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class UpstreamServiceError extends HttpError {
/**
* @override
* @readonly
* @public
* @type {string}
* @type {import('@dotcom-reliability-kit/errors').UpstreamServiceError['name']}
*/
name = 'UpstreamServiceError';

Expand Down
3 changes: 1 addition & 2 deletions packages/errors/lib/user-input-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class UserInputError extends HttpError {
/**
* @override
* @readonly
* @public
* @type {string}
* @type {import('@dotcom-reliability-kit/errors').UserInputError['name']}
*/
name = 'UserInputError';

Expand Down
3 changes: 2 additions & 1 deletion packages/errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"node": "18.x || 20.x || 22.x",
"npm": "8.x || 9.x || 10.x"
},
"main": "lib"
"main": "lib/index.js",
"types": "types/index.d.ts"
}
Loading
Loading