crypta-client 0.1.2 → 0.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crypta-client",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Crypta Secret Manager client library for Javascript Application",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/src/client.js CHANGED
@@ -5,7 +5,9 @@ const { CryptaError } = require('./errors')
5
5
  class CryptaClient {
6
6
  constructor({ baseUrl, clientId, privateKeyPem }) {
7
7
  if (!baseUrl || !clientId || !privateKeyPem) {
8
- throw new Error('baseUrl, clientId, dan privateKeyPem wajib diisi')
8
+ throw new Error(
9
+ 'Missing required parameters: baseUrl, clientId, privateKeyPem'
10
+ )
9
11
  }
10
12
 
11
13
  this.baseUrl = baseUrl.replace(/\/+$/, '')
@@ -18,27 +20,41 @@ class CryptaClient {
18
20
  http: this.http,
19
21
  clientId: this.clientId,
20
22
  privateKeyPem: this.privateKeyPem,
21
- audience: `${this.baseUrl}/public-api/auth/token`
23
+ audience: `${this.baseUrl}/client/auth/token`
22
24
  })
23
25
  }
24
26
 
25
27
  async getSecret(name) {
26
- if (!name) throw new Error('secret name wajib diisi')
27
-
28
- const token = await this.tokenManager.getToken()
28
+ if (!name) throw new Error('Secret name is required')
29
29
 
30
30
  try {
31
+ const token = await this.tokenManager.getToken()
32
+
31
33
  const res = await this.http.get(
32
- `/v1/secrets/${name}/versions/latest:access`,
34
+ `/client/latest-secret/v1/secrets/${name}/versions/latest:access`,
33
35
  {
34
36
  headers: { Authorization: `Bearer ${token}` }
35
37
  }
36
38
  )
37
39
  return res.data.data
38
40
  } catch (err) {
39
- const status = err.response?.status
40
- const message = err.response?.data?.message || 'Failed to get secret'
41
- throw new CryptaError(message, status)
41
+ // Handle HTTP errors
42
+ if (err.response) {
43
+ const status = err.response.status
44
+ const message =
45
+ err.response.data?.message ||
46
+ err.response.statusText ||
47
+ 'Failed to get secret'
48
+ throw new CryptaError(message, status)
49
+ }
50
+
51
+ // Handle token or network errors
52
+ if (err.message) {
53
+ throw new CryptaError(err.message, err.statusCode)
54
+ }
55
+
56
+ // Handle unknown errors
57
+ throw new CryptaError('An unexpected error occurred', 500)
42
58
  }
43
59
  }
44
60
  }
package/src/errors.js CHANGED
@@ -1,8 +1,9 @@
1
1
  class CryptaError extends Error {
2
- constructor(message, status) {
2
+ constructor(message, statusCode) {
3
3
  super(message)
4
4
  this.name = 'CryptaError'
5
- this.status = status
5
+ this.statusCode = statusCode
6
+ this.status = statusCode // Keep both for backwards compatibility
6
7
  }
7
8
  }
8
9
 
package/src/token.js CHANGED
@@ -22,10 +22,9 @@ class TokenManager {
22
22
  audience: this.audience
23
23
  })
24
24
 
25
- const res = await this.http.post('/public-api/auth/token', { assertion })
25
+ const res = await this.http.post('/client/auth/token', { assertion })
26
26
 
27
27
  this.accessToken = res.data.access_token
28
- // 1 menit buffer
29
28
  this.expiresAt = Date.now() + 9 * 60 * 1000
30
29
  }
31
30