@opble/types
v1.4.3
Published
Provide interface, types or foundational logic.
Readme
@opble/types
Core TypeScript types, interfaces, and HTTP error utilities shared across all @opble packages.
Installation
npm install @opble/typesHTTP Errors
Pre-defined HttpError instances for common HTTP status codes:
| Export | Status | Code |
|--------|--------|------|
| BadRequestError | 400 | BAD_REQUEST |
| UnauthorizedError | 401 | UNAUTHORIZED |
| ForbiddenError | 403 | FORBIDDEN |
| RequestDeniedError | 403 | REQUEST_DENIED |
| UnsupportedOperationError | 403 | UNSUPPORTED_OPERATION |
| ResourceNotFoundError | 404 | RESOURCE_NOT_FOUND |
| RequestNotFoundError | 404 | REQUEST_NOT_FOUND |
| InternalServerError | 500 | INTERNAL_SERVER_ERROR |
Helper functions
import { isHttpError, createHttpError, createCustomHttpError } from '@opble/types';
// Type guard
if (isHttpError(err)) {
console.log(err.status, err.code, err.message);
}
// Create a new HttpError
const err = createHttpError(422, 'UNPROCESSABLE', 'Validation failed');
// Derive from an existing error with a custom message
const err2 = createCustomHttpError(ResourceNotFoundError, 'User not found');Interfaces
HttpError
interface HttpError {
status: number;
code: string;
message: string;
cause?: unknown;
}Repository interfaces
| Interface | Methods |
|-----------|---------|
| ReadableRepository<T> | get(id) |
| CreatableRepository<T> | create(item) |
| UpdatableRepository<T> | update(id, item) |
| DeletableRepository<T> | delete(id) |
| SavableRepository<T> | save(item) |
| CrudRepository<T> | All of the above |
Other interfaces
HashMap/StringHashMap— generic key-value objectsLogger—debug,info,warn,errorlogging contractFactory<T, C>—create(config): Tfactory patternCipher/Decipher— encryption/decryption contractsEmailTransporter—send(command): Promise<SendEmailResult>
License
UNLICENSED
