@allanfsouza/aether-sdk 2.0.0 → 2.2.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/dist/auth.d.ts +55 -21
- package/dist/auth.js +87 -29
- package/dist/database.d.ts +95 -3
- package/dist/database.js +123 -1
- package/dist/errors.d.ts +12 -0
- package/dist/errors.js +42 -0
- package/dist/functions.d.ts +0 -6
- package/dist/functions.js +0 -8
- package/dist/http-client.d.ts +3 -5
- package/dist/http-client.js +11 -7
- package/dist/index.d.ts +14 -8
- package/dist/index.js +13 -10
- package/dist/push.d.ts +72 -0
- package/dist/push.js +53 -0
- package/errors.ts +46 -0
- package/package.json +10 -8
- package/src/auth.ts +125 -33
- package/src/database.ts +160 -7
- package/src/errors.ts +50 -0
- package/src/functions.ts +1 -11
- package/src/http-client.ts +17 -9
- package/src/index.ts +39 -16
- package/src/push.ts +157 -0
- package/tsconfig.json +16 -9
package/dist/auth.d.ts
CHANGED
|
@@ -1,24 +1,37 @@
|
|
|
1
1
|
import type { AxiosInstance } from "axios";
|
|
2
2
|
import type { PlataformaClient } from "./index.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
export interface LoginResponse {
|
|
4
|
+
accessToken: string;
|
|
5
|
+
refreshToken: string;
|
|
6
|
+
user: {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
email: string;
|
|
10
|
+
avatarUrl?: string;
|
|
11
|
+
role?: string;
|
|
12
|
+
aetherRole?: string;
|
|
13
|
+
planCode?: string;
|
|
14
|
+
emailVerified?: boolean;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface Session {
|
|
18
|
+
id: string;
|
|
19
|
+
userAgent?: string;
|
|
20
|
+
ip?: string;
|
|
21
|
+
createdAt: string;
|
|
22
|
+
expiresAt: string;
|
|
23
|
+
}
|
|
7
24
|
export declare class AuthModule {
|
|
8
25
|
private client;
|
|
9
26
|
private http;
|
|
27
|
+
private refreshToken;
|
|
10
28
|
constructor(client: PlataformaClient, http: AxiosInstance);
|
|
11
29
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param email O e-mail do usuário
|
|
14
|
-
* @param password A senha do usuário
|
|
15
|
-
* @returns O objeto do usuário e o token
|
|
30
|
+
* Login com email e senha
|
|
16
31
|
*/
|
|
17
|
-
login(email: string, password: string): Promise<
|
|
32
|
+
login(email: string, password: string): Promise<LoginResponse>;
|
|
18
33
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @param credentials Nome, email e senha
|
|
21
|
-
* @returns O objeto do usuário e o token
|
|
34
|
+
* Registrar novo usuário
|
|
22
35
|
*/
|
|
23
36
|
register(credentials: {
|
|
24
37
|
name: string;
|
|
@@ -26,20 +39,41 @@ export declare class AuthModule {
|
|
|
26
39
|
password: string;
|
|
27
40
|
}): Promise<any>;
|
|
28
41
|
/**
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
* Renovar access token usando refresh token
|
|
43
|
+
*/
|
|
44
|
+
refresh(): Promise<{
|
|
45
|
+
accessToken: string;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* Obter URL de autenticação do Google
|
|
49
|
+
*/
|
|
50
|
+
getGoogleAuthUrl(): string;
|
|
51
|
+
/**
|
|
52
|
+
* Logout da sessão atual
|
|
53
|
+
*/
|
|
54
|
+
logout(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Logout de todas as sessões
|
|
57
|
+
*/
|
|
58
|
+
logoutAll(): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Listar sessões ativas
|
|
61
|
+
*/
|
|
62
|
+
getSessions(): Promise<Session[]>;
|
|
63
|
+
/**
|
|
64
|
+
* Esqueci minha senha
|
|
32
65
|
*/
|
|
33
66
|
forgotPassword(email: string): Promise<any>;
|
|
34
67
|
/**
|
|
35
|
-
*
|
|
36
|
-
* @param token O código/token recebido
|
|
37
|
-
* @param newPassword A nova senha desejada
|
|
38
|
-
* @returns A resposta da API (mensagem de sucesso)
|
|
68
|
+
* Redefinir senha
|
|
39
69
|
*/
|
|
40
70
|
resetPassword(token: string, newPassword: string): Promise<any>;
|
|
41
71
|
/**
|
|
42
|
-
*
|
|
72
|
+
* Obter refresh token atual (para armazenamento)
|
|
73
|
+
*/
|
|
74
|
+
getRefreshToken(): string | null;
|
|
75
|
+
/**
|
|
76
|
+
* Definir refresh token (para restaurar sessão)
|
|
43
77
|
*/
|
|
44
|
-
|
|
78
|
+
setRefreshToken(token: string | null): void;
|
|
45
79
|
}
|
package/dist/auth.js
CHANGED
|
@@ -1,63 +1,115 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Módulo de Autenticação
|
|
3
|
-
* Lida com login, registro, gerenciamento de token e recuperação de senha.
|
|
4
|
-
*/
|
|
5
1
|
export class AuthModule {
|
|
6
2
|
constructor(client, http) {
|
|
3
|
+
this.refreshToken = null;
|
|
7
4
|
this.client = client;
|
|
8
5
|
this.http = http;
|
|
9
6
|
}
|
|
10
7
|
/**
|
|
11
|
-
*
|
|
12
|
-
* @param email O e-mail do usuário
|
|
13
|
-
* @param password A senha do usuário
|
|
14
|
-
* @returns O objeto do usuário e o token
|
|
8
|
+
* Login com email e senha
|
|
15
9
|
*/
|
|
16
10
|
async login(email, password) {
|
|
17
11
|
try {
|
|
18
|
-
const { data } = await this.http.post("/auth/login", {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
const { data } = await this.http.post("/auth/login", {
|
|
13
|
+
email,
|
|
14
|
+
password,
|
|
15
|
+
});
|
|
16
|
+
if (data.accessToken) {
|
|
17
|
+
this.client.setToken(data.accessToken);
|
|
18
|
+
this.refreshToken = data.refreshToken;
|
|
19
|
+
}
|
|
20
|
+
return data;
|
|
22
21
|
}
|
|
23
22
|
catch (e) {
|
|
24
|
-
this.client.setToken(null);
|
|
23
|
+
this.client.setToken(null);
|
|
24
|
+
this.refreshToken = null;
|
|
25
25
|
throw e;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
30
|
-
* @param credentials Nome, email e senha
|
|
31
|
-
* @returns O objeto do usuário e o token
|
|
29
|
+
* Registrar novo usuário
|
|
32
30
|
*/
|
|
33
31
|
async register(credentials) {
|
|
34
32
|
try {
|
|
35
33
|
const { data } = await this.http.post("/auth/register", credentials);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
throw e;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Renovar access token usando refresh token
|
|
42
|
+
*/
|
|
43
|
+
async refresh() {
|
|
44
|
+
if (!this.refreshToken) {
|
|
45
|
+
throw new Error("Nenhum refresh token disponível");
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const { data } = await this.http.post("/auth/refresh", {
|
|
49
|
+
refreshToken: this.refreshToken,
|
|
50
|
+
});
|
|
51
|
+
if (data.accessToken) {
|
|
52
|
+
this.client.setToken(data.accessToken);
|
|
39
53
|
}
|
|
40
54
|
return data;
|
|
41
55
|
}
|
|
42
56
|
catch (e) {
|
|
43
57
|
this.client.setToken(null);
|
|
58
|
+
this.refreshToken = null;
|
|
44
59
|
throw e;
|
|
45
60
|
}
|
|
46
61
|
}
|
|
47
62
|
/**
|
|
48
|
-
*
|
|
49
|
-
|
|
50
|
-
|
|
63
|
+
* Obter URL de autenticação do Google
|
|
64
|
+
*/
|
|
65
|
+
getGoogleAuthUrl() {
|
|
66
|
+
return `${this.client.apiUrl}/v1/auth/google`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Logout da sessão atual
|
|
70
|
+
*/
|
|
71
|
+
async logout() {
|
|
72
|
+
if (this.refreshToken) {
|
|
73
|
+
try {
|
|
74
|
+
await this.http.post("/auth/logout", {
|
|
75
|
+
refreshToken: this.refreshToken,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
// Ignora erro de logout
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
this.client.setToken(null);
|
|
83
|
+
this.refreshToken = null;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Logout de todas as sessões
|
|
87
|
+
*/
|
|
88
|
+
async logoutAll() {
|
|
89
|
+
try {
|
|
90
|
+
await this.http.post("/auth/logout-all");
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
this.client.setToken(null);
|
|
94
|
+
this.refreshToken = null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Listar sessões ativas
|
|
99
|
+
*/
|
|
100
|
+
async getSessions() {
|
|
101
|
+
const { data } = await this.http.get("/auth/sessions");
|
|
102
|
+
return data.sessions;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Esqueci minha senha
|
|
51
106
|
*/
|
|
52
107
|
async forgotPassword(email) {
|
|
53
108
|
const { data } = await this.http.post("/auth/forgot-password", { email });
|
|
54
109
|
return data;
|
|
55
110
|
}
|
|
56
111
|
/**
|
|
57
|
-
*
|
|
58
|
-
* @param token O código/token recebido
|
|
59
|
-
* @param newPassword A nova senha desejada
|
|
60
|
-
* @returns A resposta da API (mensagem de sucesso)
|
|
112
|
+
* Redefinir senha
|
|
61
113
|
*/
|
|
62
114
|
async resetPassword(token, newPassword) {
|
|
63
115
|
const { data } = await this.http.post("/auth/reset-password", {
|
|
@@ -67,9 +119,15 @@ export class AuthModule {
|
|
|
67
119
|
return data;
|
|
68
120
|
}
|
|
69
121
|
/**
|
|
70
|
-
*
|
|
122
|
+
* Obter refresh token atual (para armazenamento)
|
|
71
123
|
*/
|
|
72
|
-
|
|
73
|
-
this.
|
|
124
|
+
getRefreshToken() {
|
|
125
|
+
return this.refreshToken;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Definir refresh token (para restaurar sessão)
|
|
129
|
+
*/
|
|
130
|
+
setRefreshToken(token) {
|
|
131
|
+
this.refreshToken = token;
|
|
74
132
|
}
|
|
75
133
|
}
|
package/dist/database.d.ts
CHANGED
|
@@ -1,11 +1,85 @@
|
|
|
1
1
|
import type { AxiosInstance } from "axios";
|
|
2
2
|
import type { PlataformaClient } from "./index.js";
|
|
3
3
|
export type ListOptions<T> = {
|
|
4
|
-
filter?: Partial<T>;
|
|
4
|
+
filter?: Partial<T> | Record<string, any>;
|
|
5
5
|
sort?: {
|
|
6
6
|
field: keyof T & string;
|
|
7
7
|
order: "ASC" | "DESC";
|
|
8
8
|
};
|
|
9
|
+
limit?: number;
|
|
10
|
+
offset?: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Builder para construção fluente de queries.
|
|
14
|
+
*/
|
|
15
|
+
export declare class QueryBuilder<T> {
|
|
16
|
+
private collectionRef;
|
|
17
|
+
private filter;
|
|
18
|
+
private sort?;
|
|
19
|
+
private limitVal?;
|
|
20
|
+
private offsetVal?;
|
|
21
|
+
constructor(collectionRef: CollectionReference<T>);
|
|
22
|
+
/**
|
|
23
|
+
* Adiciona um filtro de igualdade.
|
|
24
|
+
*/
|
|
25
|
+
eq(column: keyof T & string, value: any): this;
|
|
26
|
+
/**
|
|
27
|
+
* Adiciona um filtro de desigualdade ($ne).
|
|
28
|
+
*/
|
|
29
|
+
neq(column: keyof T & string, value: any): this;
|
|
30
|
+
/**
|
|
31
|
+
* Adiciona um filtro maior que ($gt).
|
|
32
|
+
*/
|
|
33
|
+
gt(column: keyof T & string, value: number | string): this;
|
|
34
|
+
/**
|
|
35
|
+
* Adiciona um filtro maior ou igual ($gte).
|
|
36
|
+
*/
|
|
37
|
+
gte(column: keyof T & string, value: number | string): this;
|
|
38
|
+
/**
|
|
39
|
+
* Adiciona um filtro menor que ($lt).
|
|
40
|
+
*/
|
|
41
|
+
lt(column: keyof T & string, value: number | string): this;
|
|
42
|
+
/**
|
|
43
|
+
* Adiciona um filtro menor ou igual ($lte).
|
|
44
|
+
*/
|
|
45
|
+
lte(column: keyof T & string, value: number | string): this;
|
|
46
|
+
/**
|
|
47
|
+
* Adiciona um filtro LIKE ($like).
|
|
48
|
+
*/
|
|
49
|
+
like(column: keyof T & string, value: string): this;
|
|
50
|
+
/**
|
|
51
|
+
* Define a ordenação.
|
|
52
|
+
*/
|
|
53
|
+
order(column: keyof T & string, direction?: "ASC" | "DESC"): this;
|
|
54
|
+
/**
|
|
55
|
+
* Define o limite de registros.
|
|
56
|
+
*/
|
|
57
|
+
limit(count: number): this;
|
|
58
|
+
/**
|
|
59
|
+
* Define o deslocamento (paginação).
|
|
60
|
+
*/
|
|
61
|
+
offset(count: number): this;
|
|
62
|
+
/**
|
|
63
|
+
* Executa a query e retorna os resultados.
|
|
64
|
+
*/
|
|
65
|
+
get(): Promise<T[]>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Operação em lote.
|
|
69
|
+
*/
|
|
70
|
+
export type BatchOperation = {
|
|
71
|
+
type: "create";
|
|
72
|
+
collection: string;
|
|
73
|
+
data: any;
|
|
74
|
+
} | {
|
|
75
|
+
type: "update";
|
|
76
|
+
collection: string;
|
|
77
|
+
id: string;
|
|
78
|
+
data: any;
|
|
79
|
+
} | {
|
|
80
|
+
type: "delete";
|
|
81
|
+
collection: string;
|
|
82
|
+
id: string;
|
|
9
83
|
};
|
|
10
84
|
/**
|
|
11
85
|
* Módulo de Banco de Dados
|
|
@@ -20,17 +94,36 @@ export declare class DatabaseModule {
|
|
|
20
94
|
* * @example client.db.collection<Product>('products')
|
|
21
95
|
*/
|
|
22
96
|
collection<T = any>(collectionName: string): CollectionReference<T>;
|
|
97
|
+
/**
|
|
98
|
+
* Executa múltiplas operações em uma única transação.
|
|
99
|
+
* Se uma falhar, todas são revertidas.
|
|
100
|
+
*/
|
|
101
|
+
batch(operations: BatchOperation[]): Promise<any[]>;
|
|
23
102
|
}
|
|
24
103
|
/**
|
|
25
104
|
* Referência a uma coleção específica.
|
|
26
105
|
* O <T> define o formato dos dados (ex: interface User).
|
|
27
106
|
*/
|
|
28
|
-
declare class CollectionReference<T> {
|
|
107
|
+
export declare class CollectionReference<T> {
|
|
29
108
|
private client;
|
|
30
109
|
private http;
|
|
31
110
|
private collectionName;
|
|
32
111
|
private wsUrl;
|
|
33
112
|
constructor(client: PlataformaClient, http: AxiosInstance, name: string);
|
|
113
|
+
/**
|
|
114
|
+
* Inicia o QueryBuilder.
|
|
115
|
+
* Atalho para .eq()
|
|
116
|
+
*/
|
|
117
|
+
eq(column: keyof T & string, value: any): QueryBuilder<T>;
|
|
118
|
+
/**
|
|
119
|
+
* Inicia o QueryBuilder.
|
|
120
|
+
* Atalho para .gt()
|
|
121
|
+
*/
|
|
122
|
+
gt(column: keyof T & string, value: number | string): QueryBuilder<T>;
|
|
123
|
+
/**
|
|
124
|
+
* Retorna uma nova instância do QueryBuilder.
|
|
125
|
+
*/
|
|
126
|
+
query(): QueryBuilder<T>;
|
|
34
127
|
/**
|
|
35
128
|
* Lista documentos da coleção com filtros opcionais.
|
|
36
129
|
* @param options Filtros e Ordenação
|
|
@@ -59,4 +152,3 @@ declare class CollectionReference<T> {
|
|
|
59
152
|
*/
|
|
60
153
|
subscribe(callback: (action: "create" | "update" | "delete", data: T) => void): () => void;
|
|
61
154
|
}
|
|
62
|
-
export {};
|
package/dist/database.js
CHANGED
|
@@ -1,4 +1,94 @@
|
|
|
1
1
|
import WebSocket from "ws";
|
|
2
|
+
/**
|
|
3
|
+
* Builder para construção fluente de queries.
|
|
4
|
+
*/
|
|
5
|
+
export class QueryBuilder {
|
|
6
|
+
constructor(collectionRef) {
|
|
7
|
+
this.filter = {};
|
|
8
|
+
this.collectionRef = collectionRef;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Adiciona um filtro de igualdade.
|
|
12
|
+
*/
|
|
13
|
+
eq(column, value) {
|
|
14
|
+
this.filter[column] = value;
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Adiciona um filtro de desigualdade ($ne).
|
|
19
|
+
*/
|
|
20
|
+
neq(column, value) {
|
|
21
|
+
this.filter[column] = { ...this.filter[column], $ne: value };
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Adiciona um filtro maior que ($gt).
|
|
26
|
+
*/
|
|
27
|
+
gt(column, value) {
|
|
28
|
+
this.filter[column] = { ...this.filter[column], $gt: value };
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Adiciona um filtro maior ou igual ($gte).
|
|
33
|
+
*/
|
|
34
|
+
gte(column, value) {
|
|
35
|
+
this.filter[column] = { ...this.filter[column], $gte: value };
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Adiciona um filtro menor que ($lt).
|
|
40
|
+
*/
|
|
41
|
+
lt(column, value) {
|
|
42
|
+
this.filter[column] = { ...this.filter[column], $lt: value };
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Adiciona um filtro menor ou igual ($lte).
|
|
47
|
+
*/
|
|
48
|
+
lte(column, value) {
|
|
49
|
+
this.filter[column] = { ...this.filter[column], $lte: value };
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Adiciona um filtro LIKE ($like).
|
|
54
|
+
*/
|
|
55
|
+
like(column, value) {
|
|
56
|
+
this.filter[column] = { ...this.filter[column], $like: value };
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Define a ordenação.
|
|
61
|
+
*/
|
|
62
|
+
order(column, direction = "ASC") {
|
|
63
|
+
this.sort = { field: column, order: direction };
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Define o limite de registros.
|
|
68
|
+
*/
|
|
69
|
+
limit(count) {
|
|
70
|
+
this.limitVal = count;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Define o deslocamento (paginação).
|
|
75
|
+
*/
|
|
76
|
+
offset(count) {
|
|
77
|
+
this.offsetVal = count;
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Executa a query e retorna os resultados.
|
|
82
|
+
*/
|
|
83
|
+
async get() {
|
|
84
|
+
return this.collectionRef.list({
|
|
85
|
+
filter: this.filter,
|
|
86
|
+
sort: this.sort,
|
|
87
|
+
limit: this.limitVal,
|
|
88
|
+
offset: this.offsetVal,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
2
92
|
/**
|
|
3
93
|
* Módulo de Banco de Dados
|
|
4
94
|
*/
|
|
@@ -15,12 +105,20 @@ export class DatabaseModule {
|
|
|
15
105
|
collection(collectionName) {
|
|
16
106
|
return new CollectionReference(this.client, this.http, collectionName);
|
|
17
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Executa múltiplas operações em uma única transação.
|
|
110
|
+
* Se uma falhar, todas são revertidas.
|
|
111
|
+
*/
|
|
112
|
+
async batch(operations) {
|
|
113
|
+
const { data } = await this.http.post("/db/batch", { operations });
|
|
114
|
+
return data.results;
|
|
115
|
+
}
|
|
18
116
|
}
|
|
19
117
|
/**
|
|
20
118
|
* Referência a uma coleção específica.
|
|
21
119
|
* O <T> define o formato dos dados (ex: interface User).
|
|
22
120
|
*/
|
|
23
|
-
class CollectionReference {
|
|
121
|
+
export class CollectionReference {
|
|
24
122
|
constructor(client, http, name) {
|
|
25
123
|
this.client = client;
|
|
26
124
|
this.http = http;
|
|
@@ -29,6 +127,27 @@ class CollectionReference {
|
|
|
29
127
|
const protocol = client.apiUrl.startsWith("https") ? "wss" : "ws";
|
|
30
128
|
this.wsUrl = client.apiUrl.replace(/^https?/, protocol);
|
|
31
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Inicia o QueryBuilder.
|
|
132
|
+
* Atalho para .eq()
|
|
133
|
+
*/
|
|
134
|
+
eq(column, value) {
|
|
135
|
+
return new QueryBuilder(this).eq(column, value);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Inicia o QueryBuilder.
|
|
139
|
+
* Atalho para .gt()
|
|
140
|
+
*/
|
|
141
|
+
gt(column, value) {
|
|
142
|
+
return new QueryBuilder(this).gt(column, value);
|
|
143
|
+
}
|
|
144
|
+
// ... Outros atalhos podem ser adicionados conforme necessidade ...
|
|
145
|
+
/**
|
|
146
|
+
* Retorna uma nova instância do QueryBuilder.
|
|
147
|
+
*/
|
|
148
|
+
query() {
|
|
149
|
+
return new QueryBuilder(this);
|
|
150
|
+
}
|
|
32
151
|
/**
|
|
33
152
|
* Lista documentos da coleção com filtros opcionais.
|
|
34
153
|
* @param options Filtros e Ordenação
|
|
@@ -43,6 +162,9 @@ class CollectionReference {
|
|
|
43
162
|
// Backend espera formato array: ["campo", "DESC"]
|
|
44
163
|
params.sort = JSON.stringify([options.sort.field, options.sort.order]);
|
|
45
164
|
}
|
|
165
|
+
// TODO: Backend precisa implementar limit/offset na rota GET
|
|
166
|
+
// if (options?.limit) params.limit = options.limit;
|
|
167
|
+
// if (options?.offset) params.offset = options.offset;
|
|
46
168
|
const { data } = await this.http.get(`/db/${this.collectionName}`, {
|
|
47
169
|
params,
|
|
48
170
|
});
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class AetherError extends Error {
|
|
2
|
+
code: string;
|
|
3
|
+
message: string;
|
|
4
|
+
status?: number | undefined;
|
|
5
|
+
details?: any | undefined;
|
|
6
|
+
constructor(code: string, message: string, status?: number | undefined, details?: any | undefined);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Converte qualquer erro vindo do Axios em um AetherError padronizado.
|
|
10
|
+
* Essa função é pensada para ser usada no interceptor de responses.
|
|
11
|
+
*/
|
|
12
|
+
export declare function handleAxiosError(error: any): never;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
export class AetherError extends Error {
|
|
3
|
+
constructor(code, message, status, details) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.message = message;
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.details = details;
|
|
9
|
+
this.name = "AetherError";
|
|
10
|
+
// Garante que instanceof AetherError funcione mesmo em cenários com transpile/bundler
|
|
11
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Converte qualquer erro vindo do Axios em um AetherError padronizado.
|
|
16
|
+
* Essa função é pensada para ser usada no interceptor de responses.
|
|
17
|
+
*/
|
|
18
|
+
export function handleAxiosError(error) {
|
|
19
|
+
// Erro com resposta do servidor (4xx, 5xx)
|
|
20
|
+
if (error && error.response) {
|
|
21
|
+
const data = error.response.data;
|
|
22
|
+
const message = data?.error || data?.message || "Erro desconhecido na API";
|
|
23
|
+
const status = error.response.status;
|
|
24
|
+
let code = "api_error";
|
|
25
|
+
if (status === 401)
|
|
26
|
+
code = "unauthorized";
|
|
27
|
+
if (status === 403)
|
|
28
|
+
code = "permission_denied";
|
|
29
|
+
if (status === 404)
|
|
30
|
+
code = "not_found";
|
|
31
|
+
if (status === 429)
|
|
32
|
+
code = "rate_limit_exceeded";
|
|
33
|
+
throw new AetherError(code, message, status, data);
|
|
34
|
+
}
|
|
35
|
+
// Erro de rede (sem resposta do servidor)
|
|
36
|
+
if (error && error.request && !error.response) {
|
|
37
|
+
throw new AetherError("network_error", "Não foi possível conectar ao servidor Aether.", 0);
|
|
38
|
+
}
|
|
39
|
+
// Erro de configuração / código do cliente
|
|
40
|
+
const fallbackMessage = (error && error.message) || "Erro interno no cliente SDK.";
|
|
41
|
+
throw new AetherError("client_error", fallbackMessage);
|
|
42
|
+
}
|
package/dist/functions.d.ts
CHANGED
|
@@ -4,11 +4,5 @@ export declare class FunctionsModule {
|
|
|
4
4
|
private client;
|
|
5
5
|
private http;
|
|
6
6
|
constructor(client: PlataformaClient, http: AxiosInstance);
|
|
7
|
-
/**
|
|
8
|
-
* Chama uma função HTTP Serverless.
|
|
9
|
-
* @param functionName O nome da função (ou rota, ex: 'pedidos/123')
|
|
10
|
-
* @param body (Opcional) Dados para enviar no corpo (POST)
|
|
11
|
-
* @param method (Opcional) Método HTTP (padrão POST se tiver body, GET se não)
|
|
12
|
-
*/
|
|
13
7
|
invoke<T = any>(functionName: string, body?: any, method?: "GET" | "POST" | "PUT" | "DELETE"): Promise<T>;
|
|
14
8
|
}
|
package/dist/functions.js
CHANGED
|
@@ -3,17 +3,9 @@ export class FunctionsModule {
|
|
|
3
3
|
this.client = client;
|
|
4
4
|
this.http = http;
|
|
5
5
|
}
|
|
6
|
-
/**
|
|
7
|
-
* Chama uma função HTTP Serverless.
|
|
8
|
-
* @param functionName O nome da função (ou rota, ex: 'pedidos/123')
|
|
9
|
-
* @param body (Opcional) Dados para enviar no corpo (POST)
|
|
10
|
-
* @param method (Opcional) Método HTTP (padrão POST se tiver body, GET se não)
|
|
11
|
-
*/
|
|
12
6
|
async invoke(functionName, body, method) {
|
|
13
7
|
const projectId = this.client.projectId;
|
|
14
|
-
// Remove barras iniciais para evitar URL malformada
|
|
15
8
|
const cleanName = functionName.replace(/^\//, "");
|
|
16
|
-
// Define método automaticamente se não informado
|
|
17
9
|
const finalMethod = method || (body ? "POST" : "GET");
|
|
18
10
|
const response = await this.http.request({
|
|
19
11
|
url: `/functions/http/${projectId}/${cleanName}`,
|
package/dist/http-client.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { type AxiosInstance } from "axios";
|
|
2
|
-
import { PlataformaClient } from "./index.js";
|
|
2
|
+
import type { PlataformaClient } from "./index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Cria uma instância do Axios pré-configurada.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* * @param client A instância principal do PlataformaClient
|
|
8
|
-
* @returns Uma instância configurada do Axios
|
|
5
|
+
* - Injeta automaticamente headers de Auth e Projeto.
|
|
6
|
+
* - Converte erros em AetherError.
|
|
9
7
|
*/
|
|
10
8
|
export declare function createHttpClient(client: PlataformaClient): AxiosInstance;
|
package/dist/http-client.js
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
// src/http-client.ts
|
|
2
2
|
import axios from "axios";
|
|
3
|
+
import { handleAxiosError } from "./errors.js";
|
|
3
4
|
/**
|
|
4
5
|
* Cria uma instância do Axios pré-configurada.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* * @param client A instância principal do PlataformaClient
|
|
8
|
-
* @returns Uma instância configurada do Axios
|
|
6
|
+
* - Injeta automaticamente headers de Auth e Projeto.
|
|
7
|
+
* - Converte erros em AetherError.
|
|
9
8
|
*/
|
|
10
9
|
export function createHttpClient(client) {
|
|
11
10
|
const http = axios.create({
|
|
12
|
-
|
|
11
|
+
// Adiciona o /v1 automaticamente em todas as chamadas do SDK
|
|
12
|
+
baseURL: `${client.apiUrl}/v1`,
|
|
13
|
+
headers: {
|
|
14
|
+
"Content-Type": "application/json",
|
|
15
|
+
},
|
|
13
16
|
});
|
|
17
|
+
// Interceptor de REQUEST
|
|
14
18
|
http.interceptors.request.use((config) => {
|
|
15
|
-
// 1. Pega o token atual do cliente
|
|
16
19
|
const token = client.getToken();
|
|
17
20
|
if (token) {
|
|
18
21
|
config.headers.Authorization = `Bearer ${token}`;
|
|
19
22
|
}
|
|
20
|
-
// 2. Pega o ID do projeto
|
|
21
23
|
if (client.projectId) {
|
|
22
24
|
config.headers["X-Project-ID"] = client.projectId;
|
|
23
25
|
}
|
|
24
26
|
return config;
|
|
25
27
|
});
|
|
28
|
+
// Interceptor de RESPONSE
|
|
29
|
+
http.interceptors.response.use((response) => response, (error) => handleAxiosError(error));
|
|
26
30
|
return http;
|
|
27
31
|
}
|