@rivascva/dt-idl 1.1.77 → 1.1.78

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/go/log/log.go CHANGED
@@ -6,23 +6,19 @@ import (
6
6
  "log"
7
7
  "maps"
8
8
  "os"
9
-
10
- "github.com/RivasCVA/dt-idl/go/utils"
11
9
  )
12
10
 
13
11
  type Logger interface {
14
12
  // Info prints an info log message.
15
- Info(m any)
13
+ Info(ctx context.Context, m any)
16
14
  // Warn prints a warning log message.
17
- Warn(m any)
15
+ Warn(ctx context.Context, m any)
18
16
  // Error prints an error log message.
19
- Error(m any)
17
+ Error(ctx context.Context, m any)
20
18
  // Fatal prints an error log message and exits the program.
21
- Fatal(m any)
19
+ Fatal(ctx context.Context, m any)
22
20
  // With returns a new log with the given key-value pairs.
23
21
  With(kvs ...any) Logger
24
- // WithContext returns a new log with the given context.
25
- WithContext(ctx context.Context) Logger
26
22
  }
27
23
 
28
24
  type Log struct {
@@ -42,20 +38,20 @@ func NewLog() *Log {
42
38
  }
43
39
  }
44
40
 
45
- func (l *Log) Info(m any) {
46
- l.info.Println(toJson(m, l.kvs, "info"))
41
+ func (l *Log) Info(ctx context.Context, m any) {
42
+ l.info.Println(toJson(m, kvsWithContext(ctx, l.kvs), "info"))
47
43
  }
48
44
 
49
- func (l *Log) Warn(m any) {
50
- l.warn.Println(toJson(m, l.kvs, "warn"))
45
+ func (l *Log) Warn(ctx context.Context, m any) {
46
+ l.warn.Println(toJson(m, kvsWithContext(ctx, l.kvs), "warn"))
51
47
  }
52
48
 
53
- func (l *Log) Error(m any) {
54
- l.error.Println(toJson(m, l.kvs, "error"))
49
+ func (l *Log) Error(ctx context.Context, m any) {
50
+ l.error.Println(toJson(m, kvsWithContext(ctx, l.kvs), "error"))
55
51
  }
56
52
 
57
- func (l *Log) Fatal(m any) {
58
- l.error.Fatal(toJson(m, l.kvs, "error"))
53
+ func (l *Log) Fatal(ctx context.Context, m any) {
54
+ l.error.Fatal(toJson(m, kvsWithContext(ctx, l.kvs), "error"))
59
55
  }
60
56
 
61
57
  func (l *Log) With(kvs ...any) Logger {
@@ -81,28 +77,5 @@ func (l *Log) With(kvs ...any) Logger {
81
77
  }
82
78
  }
83
79
 
84
- func (l *Log) WithContext(ctx context.Context) Logger {
85
- // copy the existing key-value pairs into a new map
86
- newkvs := make(map[string]any, len(l.kvs)+10)
87
- maps.Copy(newkvs, l.kvs)
88
-
89
- // add the user id to the key-value pairs
90
- if userId, err := utils.GetUserIdFromContext(ctx); err == nil {
91
- newkvs["userId"] = userId
92
- }
93
-
94
- // add the request path to the key-value pairs
95
- if path, err := utils.GetPathFromContext(ctx); err == nil {
96
- newkvs["path"] = path
97
- }
98
-
99
- return &Log{
100
- kvs: newkvs,
101
- info: l.info,
102
- warn: l.warn,
103
- error: l.error,
104
- }
105
- }
106
-
107
80
  //exhaustruct:ignore - check for interface implementation
108
81
  var _ Logger = &Log{}
package/go/log/utils.go CHANGED
@@ -1,16 +1,21 @@
1
1
  package log
2
2
 
3
3
  import (
4
+ "context"
4
5
  "encoding/json"
5
6
  "maps"
6
7
  "time"
8
+
9
+ "github.com/RivasCVA/dt-idl/go/utils"
7
10
  )
8
11
 
9
12
  // toJson converts the message and key-value pairs to a JSON string.
10
13
  func toJson(m any, kvs map[string]any, level string) string {
11
- // copy the key-value pairs into a new map and add the message
12
- out := make(map[string]any, len(kvs)+1)
14
+ // copy the key-value pairs into a new map
15
+ out := make(map[string]any, len(kvs)+3)
13
16
  maps.Copy(out, kvs)
17
+
18
+ // add the message
14
19
  out["msg"] = m
15
20
 
16
21
  // add the log level
@@ -27,3 +32,22 @@ func toJson(m any, kvs map[string]any, level string) string {
27
32
 
28
33
  return string(data)
29
34
  }
35
+
36
+ // kvsWithContext returns a new kvs map with added context metadata.
37
+ func kvsWithContext(ctx context.Context, kvs map[string]any) map[string]any {
38
+ // copy the existing key-value pairs into a new map
39
+ newkvs := make(map[string]any, len(kvs)+2)
40
+ maps.Copy(newkvs, kvs)
41
+
42
+ // add the user id to the key-value pairs
43
+ if userId, err := utils.GetUserIdFromContext(ctx); err == nil {
44
+ newkvs["userId"] = userId
45
+ }
46
+
47
+ // add the request path to the key-value pairs
48
+ if path, err := utils.GetPathFromContext(ctx); err == nil {
49
+ newkvs["path"] = path
50
+ }
51
+
52
+ return newkvs
53
+ }
@@ -30,14 +30,14 @@ func GetAuthMiddleware(secret string) func(http.Handler) http.Handler {
30
30
  authorizationHeader := r.Header.Get("Authorization")
31
31
  arr := strings.Split(authorizationHeader, " ")
32
32
  if len(arr) != 2 || arr[0] != "Bearer" {
33
- write.ResponseWithError(w, http.StatusUnauthorized, "invalid authorization header")
33
+ write.ResponseWithError(r.Context(), w, http.StatusUnauthorized, "invalid authorization header")
34
34
  return
35
35
  }
36
36
 
37
37
  // validate the token
38
38
  token, err := jwt.ValidateToken(arr[1], secret)
39
39
  if err != nil {
40
- write.ResponseWithError(w, http.StatusUnauthorized, "invalid token")
40
+ write.ResponseWithError(r.Context(), w, http.StatusUnauthorized, "invalid token")
41
41
  return
42
42
  }
43
43
 
package/go/write/write.go CHANGED
@@ -1,6 +1,7 @@
1
1
  package write
2
2
 
3
3
  import (
4
+ "context"
4
5
  "encoding/json"
5
6
  "net/http"
6
7
 
@@ -10,13 +11,13 @@ import (
10
11
  type Writer[T ~string] interface {
11
12
  // Response writes a JSON response to the given response writer.
12
13
  // The given data is converted to JSON. An error response is written if the marshal fails.
13
- Response(w http.ResponseWriter, status int, data any)
14
+ Response(ctx context.Context, w http.ResponseWriter, status int, data any)
14
15
  // ResponseWithError writes a JSON error response to the given response writer.
15
16
  // The response is based on the shared Error schema.
16
- ResponseWithError(w http.ResponseWriter, status int, message string)
17
+ ResponseWithError(ctx context.Context, w http.ResponseWriter, status int, message string)
17
18
  // ResponseWithErrorCode writes a JSON error response with a specific code to the given response writer.
18
19
  // The response is based on the shared Error schema.
19
- ResponseWithErrorCode(w http.ResponseWriter, status int, code T, message string)
20
+ ResponseWithErrorCode(ctx context.Context, w http.ResponseWriter, status int, code T, message string)
20
21
  }
21
22
 
22
23
  type Write[T ~string] struct {
@@ -31,12 +32,12 @@ func NewWrite[T ~string](baseError T, log log.Logger) *Write[T] {
31
32
  }
32
33
  }
33
34
 
34
- func (r *Write[T]) Response(w http.ResponseWriter, status int, data any) {
35
+ func (r *Write[T]) Response(ctx context.Context, w http.ResponseWriter, status int, data any) {
35
36
  // encode the given data object
36
37
  out, err := json.Marshal(data)
37
38
  if err != nil {
38
- r.log.With("err", err).Error("failed to marshal the data")
39
- r.ResponseWithError(w, http.StatusInternalServerError, "error encoding the data")
39
+ r.log.With("err", err).Error(ctx, "failed to marshal the data")
40
+ r.ResponseWithError(ctx, w, http.StatusInternalServerError, "error encoding the data")
40
41
  return
41
42
  }
42
43
 
@@ -44,15 +45,15 @@ func (r *Write[T]) Response(w http.ResponseWriter, status int, data any) {
44
45
  w.WriteHeader(status)
45
46
  _, err = w.Write(out)
46
47
  if err != nil {
47
- r.log.With("err", err).Fatal("failed to write the response")
48
+ r.log.With("err", err).Fatal(ctx, "failed to write the response")
48
49
  }
49
50
  }
50
51
 
51
- func (r *Write[T]) ResponseWithError(w http.ResponseWriter, status int, message string) {
52
- r.ResponseWithErrorCode(w, status, r.baseError, message)
52
+ func (r *Write[T]) ResponseWithError(ctx context.Context, w http.ResponseWriter, status int, message string) {
53
+ r.ResponseWithErrorCode(ctx, w, status, r.baseError, message)
53
54
  }
54
55
 
55
- func (r *Write[T]) ResponseWithErrorCode(w http.ResponseWriter, status int, code T, message string) {
56
+ func (r *Write[T]) ResponseWithErrorCode(ctx context.Context, w http.ResponseWriter, status int, code T, message string) {
56
57
  // encode an error object with the given message
57
58
  out, err := json.Marshal(Error[T]{
58
59
  Status: int64(status),
@@ -60,14 +61,14 @@ func (r *Write[T]) ResponseWithErrorCode(w http.ResponseWriter, status int, code
60
61
  Message: message,
61
62
  })
62
63
  if err != nil {
63
- r.log.With("err", err).Fatal("failed to marshal the error")
64
+ r.log.With("err", err).Fatal(ctx, "failed to marshal the error")
64
65
  }
65
66
 
66
67
  // write the data to the given writer
67
68
  w.WriteHeader(status)
68
69
  _, err = w.Write(out)
69
70
  if err != nil {
70
- r.log.With("err", err).Fatal("failed to write the response")
71
+ r.log.With("err", err).Fatal(ctx, "failed to write the response")
71
72
  }
72
73
  }
73
74
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivascva/dt-idl",
3
- "version": "1.1.77",
3
+ "version": "1.1.78",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",