oliang 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 thefordz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # oliang
2
+
3
+ Tiny error-handling core for Express + TypeScript apps ☕
4
+
5
+ > Looking to start a new project? Use the scaffolder instead:
6
+ >
7
+ > ```bash
8
+ > npm create oliang
9
+ > ```
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install oliang
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import express from "express";
21
+ import { errorHandler, NotFoundException, HTTPSTATUS } from "oliang";
22
+
23
+ const app = express();
24
+
25
+ app.get("/health", (_req, res) => {
26
+ res.status(HTTPSTATUS.OK).json({ status: "ok" });
27
+ });
28
+
29
+ app.get("/users/:id", (req, _res) => {
30
+ throw new NotFoundException(`User ${req.params.id} not found`);
31
+ });
32
+
33
+ app.use(errorHandler);
34
+ ```
35
+
36
+ ## Exports
37
+
38
+ - `AppError` — base error class carrying `statusCode` and `errorCode`
39
+ - `InternalServerException`, `NotFoundException`, `BadRequestException`, `UnauthorizedException` — ready-made subclasses
40
+ - `ErrorCodes` / `ErrorCodeType` — error code constants
41
+ - `errorHandler` — Express error middleware that maps `AppError` to JSON responses
42
+ - `HTTPSTATUS` / `HttpStatusCodeType` — HTTP status constants
43
+ - `getEnv` — env reader that throws at startup when a required variable is missing
@@ -0,0 +1,27 @@
1
+ import { HttpStatusCodeType } from "./http-status.js";
2
+ export declare const ErrorCodes: {
3
+ readonly ERROR_INTERNAL: "ERROR_INTERNAL";
4
+ readonly ERROR_BAD_REQUEST: "ERROR_BAD_REQUEST";
5
+ readonly ERROR_UNAUTHORIZED: "ERROR_UNAUTHORIZED";
6
+ readonly ERROR_FORBIDDEN: "ERROR_FORBIDDEN";
7
+ readonly ERROR_NOT_FOUND: "ERROR_NOT_FOUND";
8
+ readonly ERROR_VALIDATION_ERROR: "ERROR_VALIDATION_ERROR";
9
+ };
10
+ export type ErrorCodeType = keyof typeof ErrorCodes;
11
+ export declare class AppError extends Error {
12
+ statusCode: HttpStatusCodeType;
13
+ errorCode: ErrorCodeType;
14
+ constructor(message: string, statusCode?: HttpStatusCodeType, errorCode?: ErrorCodeType);
15
+ }
16
+ export declare class InternalServerException extends AppError {
17
+ constructor(message?: string);
18
+ }
19
+ export declare class NotFoundException extends AppError {
20
+ constructor(message?: string);
21
+ }
22
+ export declare class BadRequestException extends AppError {
23
+ constructor(message?: string);
24
+ }
25
+ export declare class UnauthorizedException extends AppError {
26
+ constructor(message?: string);
27
+ }
@@ -0,0 +1,39 @@
1
+ import { HTTPSTATUS } from "./http-status.js";
2
+ export const ErrorCodes = {
3
+ ERROR_INTERNAL: "ERROR_INTERNAL",
4
+ ERROR_BAD_REQUEST: "ERROR_BAD_REQUEST",
5
+ ERROR_UNAUTHORIZED: "ERROR_UNAUTHORIZED",
6
+ ERROR_FORBIDDEN: "ERROR_FORBIDDEN",
7
+ ERROR_NOT_FOUND: "ERROR_NOT_FOUND",
8
+ ERROR_VALIDATION_ERROR: "ERROR_VALIDATION_ERROR",
9
+ };
10
+ export class AppError extends Error {
11
+ statusCode;
12
+ errorCode;
13
+ constructor(message, statusCode = HTTPSTATUS.INTERNAL_SERVER_ERROR, errorCode = ErrorCodes.ERROR_INTERNAL) {
14
+ super(message);
15
+ this.statusCode = statusCode;
16
+ this.errorCode = errorCode;
17
+ Error.captureStackTrace(this, this.constructor);
18
+ }
19
+ }
20
+ export class InternalServerException extends AppError {
21
+ constructor(message = "Internal Server Error") {
22
+ super(message, HTTPSTATUS.INTERNAL_SERVER_ERROR, ErrorCodes.ERROR_INTERNAL);
23
+ }
24
+ }
25
+ export class NotFoundException extends AppError {
26
+ constructor(message = "Resource Not Found") {
27
+ super(message, HTTPSTATUS.NOT_FOUND, ErrorCodes.ERROR_NOT_FOUND);
28
+ }
29
+ }
30
+ export class BadRequestException extends AppError {
31
+ constructor(message = "Bad Request") {
32
+ super(message, HTTPSTATUS.BAD_REQUEST, ErrorCodes.ERROR_BAD_REQUEST);
33
+ }
34
+ }
35
+ export class UnauthorizedException extends AppError {
36
+ constructor(message = "Unauthorized Access") {
37
+ super(message, HTTPSTATUS.UNAUTHORIZED, ErrorCodes.ERROR_UNAUTHORIZED);
38
+ }
39
+ }
@@ -0,0 +1,2 @@
1
+ import { ErrorRequestHandler } from "express";
2
+ export declare const errorHandler: ErrorRequestHandler;
@@ -0,0 +1,20 @@
1
+ import { HTTPSTATUS } from "./http-status.js";
2
+ import { AppError } from "./app-error.js";
3
+ export const errorHandler = (error, req, res, next) => {
4
+ console.log(`Error occured: ${req.path} ${error}`);
5
+ if (error instanceof SyntaxError) {
6
+ return res.status(HTTPSTATUS.BAD_REQUEST).json({
7
+ message: "Invalid JSON format. Please check your request body.",
8
+ });
9
+ }
10
+ if (error instanceof AppError) {
11
+ return res.status(error.statusCode).json({
12
+ message: error.message,
13
+ errorCode: error.errorCode,
14
+ });
15
+ }
16
+ return res.status(HTTPSTATUS.INTERNAL_SERVER_ERROR).json({
17
+ message: "Internal Server Error",
18
+ error: error?.message || "Something went wrong",
19
+ });
20
+ };
@@ -0,0 +1 @@
1
+ export declare const getEnv: (key: string, defaultValue?: string) => string;
@@ -0,0 +1,6 @@
1
+ export const getEnv = (key, defaultValue = "") => {
2
+ const value = process.env[key] ?? defaultValue;
3
+ if (!value)
4
+ throw new Error(`Missing env variable:${key}`);
5
+ return value;
6
+ };
@@ -0,0 +1,10 @@
1
+ export declare const HTTPSTATUS: {
2
+ readonly OK: 200;
3
+ readonly CREATED: 201;
4
+ readonly BAD_REQUEST: 400;
5
+ readonly UNAUTHORIZED: 401;
6
+ readonly FORBIDDEN: 403;
7
+ readonly NOT_FOUND: 404;
8
+ readonly INTERNAL_SERVER_ERROR: 500;
9
+ };
10
+ export type HttpStatusCodeType = (typeof HTTPSTATUS)[keyof typeof HTTPSTATUS];
@@ -0,0 +1,9 @@
1
+ export const HTTPSTATUS = {
2
+ OK: 200,
3
+ CREATED: 201,
4
+ BAD_REQUEST: 400,
5
+ UNAUTHORIZED: 401,
6
+ FORBIDDEN: 403,
7
+ NOT_FOUND: 404,
8
+ INTERNAL_SERVER_ERROR: 500,
9
+ };
@@ -0,0 +1,4 @@
1
+ export { HTTPSTATUS, type HttpStatusCodeType } from "./http-status.js";
2
+ export { AppError, BadRequestException, ErrorCodes, InternalServerException, NotFoundException, UnauthorizedException, type ErrorCodeType, } from "./app-error.js";
3
+ export { errorHandler } from "./error-handler.js";
4
+ export { getEnv } from "./get-env.js";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { HTTPSTATUS } from "./http-status.js";
2
+ export { AppError, BadRequestException, ErrorCodes, InternalServerException, NotFoundException, UnauthorizedException, } from "./app-error.js";
3
+ export { errorHandler } from "./error-handler.js";
4
+ export { getEnv } from "./get-env.js";
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "oliang",
3
+ "version": "0.1.0",
4
+ "description": "Tiny error-handling core for Express + TypeScript apps. Scaffold a full app with `npm create oliang` ☕",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "prepublishOnly": "npm run build"
20
+ },
21
+ "engines": {
22
+ "node": ">=18"
23
+ },
24
+ "keywords": [
25
+ "express",
26
+ "typescript",
27
+ "error-handling",
28
+ "oliang"
29
+ ],
30
+ "author": "thefordz",
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/thefordz/oliang.git",
35
+ "directory": "packages/oliang"
36
+ },
37
+ "peerDependencies": {
38
+ "express": ">=4"
39
+ },
40
+ "devDependencies": {
41
+ "@types/express": "^5.0.6",
42
+ "typescript": "^5.9.2"
43
+ }
44
+ }