Skip to content

Commit

Permalink
⚙️ Chore(oh-my-error,tutorials): add Tutorials, change myError, add v…
Browse files Browse the repository at this point in the history
…ersion to every function, add version to every exported element
  • Loading branch information
INeedJobToStartWork committed Nov 25, 2024
1 parent 2050271 commit a8dce84
Show file tree
Hide file tree
Showing 24 changed files with 855 additions and 183 deletions.
3 changes: 2 additions & 1 deletion .changeset/honest-jeans-own.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
---

- Add passing Throwed Error to our error at `myErrorWrapper`
- Add types `ValueOrFunctionAll` and `ValueOrFunction`
- Add types `TValueOrFunctionAll` and `TValueOrFunction`
- Add missing TSDocs to types
- Change myErrorHandler arg[0] type (added unknown)
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pnpm format
pnpm lint:fix
pnpm lint
148 changes: 76 additions & 72 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/oh-my-error/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Add passing Throwed Error to our error at `myErrorWrapper`
- Add types `ValueOrFunctionAll` and `ValueOrFunction`
- Add missing TSDocs to types
- Change myErrorHandler arg[0] type (added unknown)

## 2.0.0-prerelease.2

Expand Down
148 changes: 76 additions & 72 deletions packages/oh-my-error/README.md

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions packages/oh-my-error/clean-package.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
"replace": {
"scripts": {
"npm:postpack": "clean-package restore"
},
"exports": {
".": {
"types": {
"import": "./index.d.ts",
"require": "./index.d.cts"
},
"import": "./index.js",
"require": "./index.cjs",
"*": "./*"
}
}
}
}
8 changes: 4 additions & 4 deletions packages/oh-my-error/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"exports": {
".": {
"types": {
"import": "./index.d.ts",
"require": "./index.d.cts"
"import": "./dist/index.d.ts",
"require": "./dist/index.d.cts"
},
"import": "./index.js",
"require": "./index.cjs",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"*": "./*"
}
},
Expand Down
2 changes: 2 additions & 0 deletions packages/oh-my-error/src/functions/myError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type ErrorArgs<T> = {
* message:"nameInputed is on black list!"
* }
* ```
*
* @version 2.0.0
*/

export const myError = <T extends NonNullable<unknown> = IMyError>(errorObj: T, args?: ErrorArgs<T>): T => {
Expand Down
2 changes: 2 additions & 0 deletions packages/oh-my-error/src/functions/myErrorCatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import type { arrowFunction } from "../types/internal";
* throw new Error("Can't read file!");
* });
* ```
* @version 2.0.0
*/

export const myErrorCatcher =
<T extends arrowFunction<T>>(functionThatMayThrow: T) =>
async (...args: Parameters<T>): Promise<ReturnType<T>> =>
Expand Down
5 changes: 3 additions & 2 deletions packages/oh-my-error/src/functions/myErrorHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ describe("[FUNCTION] myErrorHandler", () => {
console.error("ERROR");
return true;
}
};
} as const;

const test = myErrorHandler("FS001", MyErrorHandlerList)();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const test = myErrorHandler((data as any).code, MyErrorHandlerList)();
expect(test).toBe(true);
});
});
4 changes: 2 additions & 2 deletions packages/oh-my-error/src/functions/myErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const ErrorList = {
* };
* if (isError) myErrorHandler(data.code, MyErrorHandlerList)();
* ```
*
* @version 2.0.0
*/
export const myErrorHandler =
<T extends keyof K, K extends Record<T, K[T]>>(errorCode: T, errorSolutions: K) =>
<T extends unknown & keyof K, K extends Record<T, K[T]>>(errorCode: T & unknown, errorSolutions: K) =>
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
(...args: Parameters<K[T]>) => {
if (!(errorCode in errorSolutions)) {
Expand Down
1 change: 1 addition & 0 deletions packages/oh-my-error/src/functions/myErrorWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type MyErrorWrapperReturn<Fn extends AnyFunction, ErrorType = undefined> = Error
* // With Passing Throwed Error to our error
* const data = myErrorWrapper(readFile,err => new Error(`ERROR MESSAGE: ${err.message}}`))("path...");
* ```
* @version 2.0.0
*/
export const myErrorWrapper =
<Fn extends TFunction<Fn>, ErrorType = undefined>(
Expand Down
53 changes: 28 additions & 25 deletions packages/oh-my-error/src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ import type { Prettify } from "./internal";
//----------------

// eslint-disable-next-line @typescript-eslint/no-explicit-any
/** Transform whole object properties type at `ValueOrFunctionAll` */
export type ValueOrFunctionAll<T> = T extends object
? { [K in keyof T]: ValueOrFunctionAll<T[K]> }
: ValueOrFunction<T>;
/** Transform whole object properties type at `TValueOrFunctionAll` */
export type TValueOrFunctionAll<T> = T extends object
? { [K in keyof T]: TValueOrFunctionAll<T[K]> }
: TValueOrFunction<T>;

/** Represents a type that can be either a value of type T or a function that returns a value of type T. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ValueOrFunction<T> = T | ((...params: any[]) => T);
export type TValueOrFunction<T> = T | ((...params: any[]) => T);

//----------------
// TYPES ATOMS
//----------------

/** Represents the severity level of an error or warning.*/
/** Represents the severity level of an error or warning. @version 2.0.0*/
export type TSeverity = "ERROR" | "WARNING";

/** Represents a more granular severity level. */
/** Represents a more granular severity level. @version 2.0.0*/
export type TSeverity2 = "CRITICAL" | "HIGH" | "LOW" | "MEDIUM";

//----------------
// TYPES MOLECULES
//----------------

/** Validation field structure */
/** Validation field structure @version 2.0.0*/
export type TValidationError = {
fields: Array<{
[K: string]: unknown;
Expand All @@ -44,7 +44,7 @@ export type TValidationError = {
}>;
};

/** API error structure */
/** API error structure @version 2.0.0*/
export type TApiError = {
endpoint?: string;
path?: string;
Expand All @@ -53,7 +53,7 @@ export type TApiError = {
timestamp?: Date;
};

/** API rate limit structure */
/** API rate limit structure @version 2.0.0*/
export type TApiRateLimit = {
limit?: number;
remaining?: number;
Expand Down Expand Up @@ -88,26 +88,27 @@ export type TApiRateLimit = {
* }
* }
* ```
* @version 2.0.0
*/

export type TErrorMessagesExt =
| Prettify<ValueOrFunction<string>>
| Prettify<TValueOrFunction<string>>
| {
dev?: ValueOrFunction<string>;
user?: ValueOrFunction<string>;
dev?: TValueOrFunction<string>;
user?: TValueOrFunction<string>;
};

/** Error collection type */
/** Error collection type @version 2.0.0*/
export type TErrorList<T> = {
errors?: T[];
};

/** Basic error cause type */
/** Basic error cause type @version 2.0.0*/
export type TCauseError = {
cause?: unknown;
};

/** Basic details container */
/** Basic details container @version 2.0.0*/
export type TDetails<T = unknown> = {
details?: T;
};
Expand All @@ -119,6 +120,7 @@ export type TDetails<T = unknown> = {
/**
* Basic Error Template for Error
* @author oh-my-error
* @version 2.0.0
*/
export interface IMyError {
code?: number | string;
Expand All @@ -127,28 +129,29 @@ export interface IMyError {
name?: string;
}

/** Basic Error Template for **API** */
/** Basic Error Template for **API** @version 2.0.0*/
export interface IMyErrorAPI extends IMyError, TApiError {}

/** Basic Error Template for **API Rate limit** */
/** Basic Error Template for **API Rate limit** @version 2.0.0*/
export interface IMyErrorRateLimit extends IMyError, TApiRateLimit {}

/** Basic Error Template for **Validation** */
/** Basic Error Template for **Validation** @version 2.0.0*/
export interface IMyErrorValidation extends IMyError, TValidationError {}

/** Every Error Template with return type Function or Value option exclude TBaseError & TBaseErrorExt */
export type TAllMyErrorTypesExt = ValueOrFunctionAll<TAllMyErrorTypes>;
/** Every Error Template with return type Function or Value option exclude TBaseError & TBaseErrorExt @version 2.0.0*/
export type TAllMyErrorTypesExt = TValueOrFunctionAll<TAllMyErrorTypes>;

/** Every Error Template exclude TBaseError & TBaseErrorExt */
/** Every Error Template exclude TBaseError & TBaseErrorExt @version 2.0.0*/
export type TAllMyErrorTypes = IMyError | IMyErrorAPI | IMyErrorRateLimit | IMyErrorValidation;

/** Predefined type for `Error` with Extension */
export type TBaseErrorExt = ValueOrFunctionAll<TBaseError>;
/** Predefined type for `Error` with Extension @version 2.0.0*/
export type TBaseErrorExt = TValueOrFunctionAll<TBaseError>;

/** Predefined type for `Error` */
/** Predefined type for `Error` @version 2.0.0*/
export type TBaseError = {
message?: Parameters<typeof Error>[0];
options?: Parameters<typeof Error>[1];
};

/** Predefined type for `Error` @version 2.0.0*/
export type TMyErrorList<CustomError = TAllMyErrorTypesExt> = Record<string, CustomError>;
2 changes: 1 addition & 1 deletion packages/oh-my-error/src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type TErrorReturn<CustomError = NonNullable<unknown>> = [CustomError & IM
export type TFunctionReturn<T> = Prettify<TDataReturn<T>> | Prettify<TErrorReturn>;

// export type TMyErrorList<CustomError = NonNullable<unknown>> = Record<string, Required<IMyError<CustomError>>>;

/** @version 2.0.0 */
export type TMyHandler<
CustomError,
T extends Record<number | string, { code: string & unknown }> & TMyErrorList<CustomError>
Expand Down
1 change: 1 addition & 0 deletions packages/oh-my-error/src/types/statusCodes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* HTTP Status Codes
* @version 2.0.0
*/
export const StatusCode = {
// 1xx Informational
Expand Down
Loading

0 comments on commit a8dce84

Please sign in to comment.