@onerb/core 2.0.1 → 2.0.2
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/README.md +6 -125
- package/dist/index.cjs +3 -74
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -32
- package/dist/index.d.ts +1 -32
- package/dist/index.js +2 -64
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
# @onerb/core
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Requisitos
|
|
6
|
-
|
|
7
|
-
- Node.js com suporte a ESM.
|
|
8
|
-
- TypeScript (opcional, recomendado para tipagem).
|
|
3
|
+
Pacote principal do ecossistema Onerb. Ele agrega os subpacotes oficiais e reexporta suas APIs, permitindo instalar tudo com uma única dependência.
|
|
9
4
|
|
|
10
5
|
## Instalação
|
|
11
6
|
|
|
@@ -21,130 +16,16 @@ yarn add @onerb/core
|
|
|
21
16
|
pnpm add @onerb/core
|
|
22
17
|
```
|
|
23
18
|
|
|
24
|
-
##
|
|
25
|
-
|
|
26
|
-
- `AppError`: erro de aplicação com `code` e `detail`.
|
|
27
|
-
- `Either`, `Left`, `Right`, `left`, `right`: utilitários para fluxo de sucesso/erro sem `throw` no domínio.
|
|
28
|
-
|
|
29
|
-
## Importação
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
import { AppError, Either, left, right } from '@onerb/core';
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## Uso
|
|
36
|
-
|
|
37
|
-
### 1) Domínio: só repassa o erro (sem `throw`)
|
|
38
|
-
|
|
39
|
-
Exemplo de caso de uso no domínio que valida dados e retorna `Either`. Se ocorrer erro, ele é repassado (retornando `Left`).
|
|
40
|
-
|
|
41
|
-
```ts
|
|
42
|
-
import { AppError, Either, left, right } from '@onerb/core';
|
|
43
|
-
|
|
44
|
-
type CreateUserInput = {
|
|
45
|
-
email: string;
|
|
46
|
-
name: string;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
type User = {
|
|
50
|
-
id: string;
|
|
51
|
-
email: string;
|
|
52
|
-
name: string;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
function createUser(input: CreateUserInput): Either<AppError, User> {
|
|
56
|
-
if (!input.email.includes('@')) {
|
|
57
|
-
return left(
|
|
58
|
-
new AppError('Email inválido', {
|
|
59
|
-
code: 'USER_EMAIL_INVALID',
|
|
60
|
-
detail: `email recebido: ${input.email}`,
|
|
61
|
-
})
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const user: User = {
|
|
66
|
-
id: crypto.randomUUID(),
|
|
67
|
-
email: input.email,
|
|
68
|
-
name: input.name,
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
return right(user);
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### 2) Aplicação: coordena e pode lançar `throw`
|
|
76
|
-
|
|
77
|
-
Na camada de aplicação, você pode coordenar chamadas do domínio e decidir transformar `Left` em exceção (por exemplo para integração com frameworks HTTP).
|
|
78
|
-
|
|
79
|
-
```ts
|
|
80
|
-
import { AppError } from '@onerb/core';
|
|
81
|
-
|
|
82
|
-
async function handleCreateUser(input: { email: string; name: string }) {
|
|
83
|
-
const result = createUser(input);
|
|
19
|
+
## Subpacotes
|
|
84
20
|
|
|
85
|
-
|
|
86
|
-
// Aqui a aplicação decide lançar o erro
|
|
87
|
-
throw result.fold((err) => err, (user) => user);
|
|
88
|
-
}
|
|
21
|
+
- `@onerb/error`: utilitários de erro e `Either`. Veja o README em [`packages/error/README.md`](../error/README.md).
|
|
89
22
|
|
|
90
|
-
|
|
91
|
-
() => {
|
|
92
|
-
// Este ramo não será chamado porque já checamos isLeft
|
|
93
|
-
throw new AppError('Erro inesperado', { code: 'UNEXPECTED' });
|
|
94
|
-
},
|
|
95
|
-
(user) => user
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
```
|
|
23
|
+
## Reexportações
|
|
99
24
|
|
|
100
|
-
|
|
25
|
+
O `@onerb/core` reexporta todo o conteúdo de `@onerb/error`. Se você já usa `@onerb/core`, não precisa instalar o pacote de erro separadamente.
|
|
101
26
|
|
|
102
|
-
|
|
27
|
+
## Importação
|
|
103
28
|
|
|
104
29
|
```ts
|
|
105
30
|
import { AppError, Either, left, right } from '@onerb/core';
|
|
106
|
-
|
|
107
|
-
function validateEmail(email: string): Either<AppError, true> {
|
|
108
|
-
if (!email.includes('@')) {
|
|
109
|
-
return left(new AppError('Email inválido', { code: 'USER_EMAIL_INVALID' }));
|
|
110
|
-
}
|
|
111
|
-
return right(true);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function ensureName(name: string): Either<AppError, true> {
|
|
115
|
-
if (name.trim().length === 0) {
|
|
116
|
-
return left(new AppError('Nome obrigatório', { code: 'USER_NAME_REQUIRED' }));
|
|
117
|
-
}
|
|
118
|
-
return right(true);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function createUserDomain(input: { email: string; name: string }): Either<AppError, { id: string; email: string; name: string }> {
|
|
122
|
-
const emailCheck = validateEmail(input.email);
|
|
123
|
-
if (emailCheck.isLeft()) return emailCheck;
|
|
124
|
-
|
|
125
|
-
const nameCheck = ensureName(input.name);
|
|
126
|
-
if (nameCheck.isLeft()) return nameCheck;
|
|
127
|
-
|
|
128
|
-
return right({ id: crypto.randomUUID(), email: input.email, name: input.name });
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async function createUserApp(input: { email: string; name: string }) {
|
|
132
|
-
const result = createUserDomain(input);
|
|
133
|
-
|
|
134
|
-
if (result.isLeft()) {
|
|
135
|
-
throw result.fold((err) => err, () => new Error('Inalcançável'));
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return result.fold(
|
|
139
|
-
() => {
|
|
140
|
-
throw new AppError('Erro inesperado', { code: 'UNEXPECTED' });
|
|
141
|
-
},
|
|
142
|
-
(user) => user
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
31
|
```
|
|
146
|
-
|
|
147
|
-
## Notas
|
|
148
|
-
|
|
149
|
-
- O `Either` ajuda a manter o domínio livre de exceções, favorecendo retornos explícitos.
|
|
150
|
-
- A decisão de lançar erros (`throw`) fica na camada de aplicação, onde há integração com infraestrutura (HTTP, filas, etc.).
|
package/dist/index.cjs
CHANGED
|
@@ -3,10 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
6
|
var __copyProps = (to, from, except, desc) => {
|
|
11
7
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
8
|
for (let key of __getOwnPropNames(from))
|
|
@@ -15,82 +11,15 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
11
|
}
|
|
16
12
|
return to;
|
|
17
13
|
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
15
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
16
|
|
|
20
17
|
// src/index.ts
|
|
21
18
|
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
AppError: () => AppError,
|
|
24
|
-
Left: () => Left,
|
|
25
|
-
Right: () => Right,
|
|
26
|
-
left: () => left,
|
|
27
|
-
right: () => right
|
|
28
|
-
});
|
|
29
19
|
module.exports = __toCommonJS(index_exports);
|
|
30
|
-
|
|
31
|
-
// src/errors/app-error.ts
|
|
32
|
-
var AppError = class extends Error {
|
|
33
|
-
code;
|
|
34
|
-
detail;
|
|
35
|
-
constructor(message, options) {
|
|
36
|
-
super(message);
|
|
37
|
-
this.name = "AppError";
|
|
38
|
-
this.code = options.code;
|
|
39
|
-
this.detail = options.detail;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// src/errors/either.ts
|
|
44
|
-
var Left = class _Left {
|
|
45
|
-
_value;
|
|
46
|
-
constructor(value) {
|
|
47
|
-
this._value = value;
|
|
48
|
-
}
|
|
49
|
-
isLeft() {
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
isRight() {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
map(_fn) {
|
|
56
|
-
return new _Left(this._value);
|
|
57
|
-
}
|
|
58
|
-
mapLeft(fn) {
|
|
59
|
-
return new _Left(fn(this._value));
|
|
60
|
-
}
|
|
61
|
-
fold(fl, _fr) {
|
|
62
|
-
return fl(this._value);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
var Right = class _Right {
|
|
66
|
-
_value;
|
|
67
|
-
constructor(value) {
|
|
68
|
-
this._value = value;
|
|
69
|
-
}
|
|
70
|
-
isLeft() {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
isRight() {
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
map(fn) {
|
|
77
|
-
return new _Right(fn(this._value));
|
|
78
|
-
}
|
|
79
|
-
mapLeft(_fn) {
|
|
80
|
-
return new _Right(this._value);
|
|
81
|
-
}
|
|
82
|
-
fold(_fl, fr) {
|
|
83
|
-
return fr(this._value);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
var left = (l) => new Left(l);
|
|
87
|
-
var right = (r) => new Right(r);
|
|
20
|
+
__reExport(index_exports, require("@onerb/error"), module.exports);
|
|
88
21
|
// Annotate the CommonJS export names for ESM import in node:
|
|
89
22
|
0 && (module.exports = {
|
|
90
|
-
|
|
91
|
-
Left,
|
|
92
|
-
Right,
|
|
93
|
-
left,
|
|
94
|
-
right
|
|
23
|
+
...require("@onerb/error")
|
|
95
24
|
});
|
|
96
25
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from '@onerb/error';\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,0BAAc,yBAAd;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,32 +1 @@
|
|
|
1
|
-
|
|
2
|
-
readonly code: string;
|
|
3
|
-
readonly detail?: string;
|
|
4
|
-
constructor(message: string, options: {
|
|
5
|
-
code: string;
|
|
6
|
-
detail?: string;
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type Either<L, R> = Left<L, R> | Right<R, L>;
|
|
11
|
-
declare class Left<L, R = never> {
|
|
12
|
-
private readonly _value;
|
|
13
|
-
constructor(value: L);
|
|
14
|
-
isLeft(): this is Left<L, R>;
|
|
15
|
-
isRight(): this is Left<L, R>;
|
|
16
|
-
map<T>(_fn: (v: R) => T): Left<L, T>;
|
|
17
|
-
mapLeft<T>(fn: (l: L) => T): Left<T, R>;
|
|
18
|
-
fold<T>(fl: (l: L) => T, _fr: (r: R) => R): T;
|
|
19
|
-
}
|
|
20
|
-
declare class Right<R, L = never> {
|
|
21
|
-
private readonly _value;
|
|
22
|
-
constructor(value: R);
|
|
23
|
-
isLeft(): this is Right<R, L>;
|
|
24
|
-
isRight(): this is Right<R, L>;
|
|
25
|
-
map<T>(fn: (v: R) => T): Right<T, L>;
|
|
26
|
-
mapLeft<T>(_fn: (l: L) => T): Right<R, T>;
|
|
27
|
-
fold<T>(_fl: (l: L) => L, fr: (r: R) => T): T;
|
|
28
|
-
}
|
|
29
|
-
declare const left: <L, R = never>(l: L) => Left<L, R>;
|
|
30
|
-
declare const right: <R, L = never>(r: R) => Right<R, L>;
|
|
31
|
-
|
|
32
|
-
export { AppError, type Either, Left, Right, left, right };
|
|
1
|
+
export * from '@onerb/error';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1 @@
|
|
|
1
|
-
|
|
2
|
-
readonly code: string;
|
|
3
|
-
readonly detail?: string;
|
|
4
|
-
constructor(message: string, options: {
|
|
5
|
-
code: string;
|
|
6
|
-
detail?: string;
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type Either<L, R> = Left<L, R> | Right<R, L>;
|
|
11
|
-
declare class Left<L, R = never> {
|
|
12
|
-
private readonly _value;
|
|
13
|
-
constructor(value: L);
|
|
14
|
-
isLeft(): this is Left<L, R>;
|
|
15
|
-
isRight(): this is Left<L, R>;
|
|
16
|
-
map<T>(_fn: (v: R) => T): Left<L, T>;
|
|
17
|
-
mapLeft<T>(fn: (l: L) => T): Left<T, R>;
|
|
18
|
-
fold<T>(fl: (l: L) => T, _fr: (r: R) => R): T;
|
|
19
|
-
}
|
|
20
|
-
declare class Right<R, L = never> {
|
|
21
|
-
private readonly _value;
|
|
22
|
-
constructor(value: R);
|
|
23
|
-
isLeft(): this is Right<R, L>;
|
|
24
|
-
isRight(): this is Right<R, L>;
|
|
25
|
-
map<T>(fn: (v: R) => T): Right<T, L>;
|
|
26
|
-
mapLeft<T>(_fn: (l: L) => T): Right<R, T>;
|
|
27
|
-
fold<T>(_fl: (l: L) => L, fr: (r: R) => T): T;
|
|
28
|
-
}
|
|
29
|
-
declare const left: <L, R = never>(l: L) => Left<L, R>;
|
|
30
|
-
declare const right: <R, L = never>(r: R) => Right<R, L>;
|
|
31
|
-
|
|
32
|
-
export { AppError, type Either, Left, Right, left, right };
|
|
1
|
+
export * from '@onerb/error';
|
package/dist/index.js
CHANGED
|
@@ -1,65 +1,3 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
code;
|
|
4
|
-
detail;
|
|
5
|
-
constructor(message, options) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.name = "AppError";
|
|
8
|
-
this.code = options.code;
|
|
9
|
-
this.detail = options.detail;
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// src/errors/either.ts
|
|
14
|
-
var Left = class _Left {
|
|
15
|
-
_value;
|
|
16
|
-
constructor(value) {
|
|
17
|
-
this._value = value;
|
|
18
|
-
}
|
|
19
|
-
isLeft() {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
isRight() {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
map(_fn) {
|
|
26
|
-
return new _Left(this._value);
|
|
27
|
-
}
|
|
28
|
-
mapLeft(fn) {
|
|
29
|
-
return new _Left(fn(this._value));
|
|
30
|
-
}
|
|
31
|
-
fold(fl, _fr) {
|
|
32
|
-
return fl(this._value);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
var Right = class _Right {
|
|
36
|
-
_value;
|
|
37
|
-
constructor(value) {
|
|
38
|
-
this._value = value;
|
|
39
|
-
}
|
|
40
|
-
isLeft() {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
isRight() {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
map(fn) {
|
|
47
|
-
return new _Right(fn(this._value));
|
|
48
|
-
}
|
|
49
|
-
mapLeft(_fn) {
|
|
50
|
-
return new _Right(this._value);
|
|
51
|
-
}
|
|
52
|
-
fold(_fl, fr) {
|
|
53
|
-
return fr(this._value);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
var left = (l) => new Left(l);
|
|
57
|
-
var right = (r) => new Right(r);
|
|
58
|
-
export {
|
|
59
|
-
AppError,
|
|
60
|
-
Left,
|
|
61
|
-
Right,
|
|
62
|
-
left,
|
|
63
|
-
right
|
|
64
|
-
};
|
|
1
|
+
// src/index.ts
|
|
2
|
+
export * from "@onerb/error";
|
|
65
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from '@onerb/error';\n"],"mappings":";AAAA,cAAc;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onerb/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Core utilities for Onerb projects.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
"require": "./dist/index.cjs"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@onerb/error": "^2.0.0"
|
|
20
|
+
},
|
|
18
21
|
"files": ["dist"],
|
|
19
22
|
"scripts": {
|
|
20
23
|
"build": "tsup",
|