@intelicity/gates-sdk 0.1.5 → 0.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.
@@ -1,3 +1,4 @@
1
+ import { MissingParameterError, ApiRequestError, InvalidResponseError, } from "../errors/error.js";
1
2
  export class GatesUserService {
2
3
  baseUrl;
3
4
  system;
@@ -22,7 +23,7 @@ export class GatesUserService {
22
23
  // options: GetAllUsersOptions = {}
23
24
  ) {
24
25
  if (!idToken) {
25
- throw new Error("ID Token is required");
26
+ throw new MissingParameterError("idToken");
26
27
  }
27
28
  try {
28
29
  const queryParams = new URLSearchParams();
@@ -43,9 +44,12 @@ export class GatesUserService {
43
44
  });
44
45
  if (!response.ok) {
45
46
  const errorText = await response.text();
46
- throw new Error(`HTTP ${response.status}: ${errorText}`);
47
+ throw new ApiRequestError(`Falha ao buscar usuários: ${errorText}`, response.status);
47
48
  }
48
49
  const data = (await response.json());
50
+ if (!data.profiles || !Array.isArray(data.profiles)) {
51
+ throw new InvalidResponseError("Resposta da API não contém o array 'profiles'");
52
+ }
49
53
  // Adicionar total baseado no length se não vier da API
50
54
  if (!data.total && data.profiles) {
51
55
  data.total = data.profiles.length;
@@ -53,53 +57,69 @@ export class GatesUserService {
53
57
  return data;
54
58
  }
55
59
  catch (error) {
60
+ // Re-throw known errors
61
+ if (error instanceof MissingParameterError ||
62
+ error instanceof ApiRequestError ||
63
+ error instanceof InvalidResponseError) {
64
+ throw error;
65
+ }
66
+ // Handle fetch errors
56
67
  if (error instanceof Error) {
57
- throw new Error(`Failed to fetch users: ${error.message}`);
68
+ throw new ApiRequestError(`Falha ao buscar usuários: ${error.message}`);
58
69
  }
59
- throw new Error("Failed to fetch users: Unknown error");
70
+ throw new ApiRequestError("Falha ao buscar usuários: Erro desconhecido");
60
71
  }
61
72
  }
62
73
  /**
63
74
  * Busca um usuário específico por ID
64
- * @param accessToken Token de autenticação
75
+ * @param idToken Token de autenticação
65
76
  * @returns Dados do usuário
66
77
  */
67
- async login(accessToken) {
68
- if (!accessToken) {
69
- throw new Error("Access Token is required");
78
+ async getProfile(idToken) {
79
+ if (!idToken) {
80
+ throw new MissingParameterError("idToken");
70
81
  }
71
82
  try {
72
- const response = await fetch(`${this.baseUrl}/login`, {
73
- method: "POST",
83
+ const response = await fetch(`${this.baseUrl}/get-profile?system_name=${this.system}`, {
84
+ method: "GET",
74
85
  headers: {
75
86
  ...this.defaultHeaders,
76
- Authorization: `${accessToken}`,
87
+ Authorization: `${idToken}`,
77
88
  },
78
- body: JSON.stringify({ system_name: this.system }),
79
89
  });
80
90
  if (!response.ok) {
81
91
  const errorText = await response.text();
82
- throw new Error(`HTTP ${response.status}: ${errorText}`);
92
+ throw new ApiRequestError(`Falha ao buscar perfil do usuário: ${errorText}`, response.status);
83
93
  }
84
94
  const data = (await response.json());
85
- // A API retorna { user: {...}, message: "..." }
86
- if (!data.user) {
87
- throw new Error("Invalid response format: missing user data");
95
+ // A API retorna { profile: {...}, email: ..., name: ..., message: "..." }
96
+ if (!data.profile) {
97
+ throw new InvalidResponseError("Resposta da API não contém os dados do 'profile'");
98
+ }
99
+ if (!data.email || !data.name) {
100
+ throw new InvalidResponseError("Resposta da API não contém os campos obrigatórios do usuário (email ou name)");
88
101
  }
89
102
  const userProfile = {
90
- user_id: data.user.user_id,
91
- email: data.user.email,
92
- name: data.user.name,
93
- enabled: data.user.enabled,
94
- profile_attributes: data.user.profile_attributes || [],
103
+ user_id: data.profile.user_id,
104
+ email: data.email,
105
+ name: data.name,
106
+ enabled: data.profile.enabled,
107
+ profile_attributes: data.profile.profile_attributes || [],
95
108
  };
96
109
  return userProfile;
97
110
  }
98
111
  catch (error) {
112
+ // Re-throw known errors
113
+ if (error instanceof MissingParameterError ||
114
+ error instanceof ApiRequestError ||
115
+ error instanceof InvalidResponseError) {
116
+ throw error;
117
+ }
118
+ // Handle fetch errors
99
119
  if (error instanceof Error) {
100
- throw new Error(`Failed to fetch user: ${error.message}`);
120
+ throw new ApiRequestError(`Falha ao buscar perfil do usuário: ${error.message}`);
101
121
  }
102
- throw new Error("Failed to fetch user: Unknown error");
122
+ throw new ApiRequestError("Falha ao buscar perfil do usuário: Erro desconhecido");
103
123
  }
104
124
  }
105
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intelicity/gates-sdk",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "description": "Simple SDK for authenticating users with AWS Cognito JWT tokens",
5
5
  "type": "module",
6
6
  "exports": "./dist/index.js",
@@ -18,6 +18,8 @@
18
18
  "scripts": {
19
19
  "build": "tsc && tsc-alias",
20
20
  "typecheck": "tsc --noEmit",
21
+ "test": "vitest run",
22
+ "test:watch": "vitest",
21
23
  "test:token": "tsx scripts/test-token.ts",
22
24
  "prepublishOnly": "npm run build"
23
25
  },
@@ -37,7 +39,8 @@
37
39
  "@types/node": "^24.1.0",
38
40
  "tsc-alias": "^1.8.16",
39
41
  "tsx": "^4.20.3",
40
- "typescript": "^5.9.2"
42
+ "typescript": "^5.9.2",
43
+ "vitest": "^4.1.0"
41
44
  },
42
45
  "dependencies": {
43
46
  "jose": "^6.1.1"