@rivascva/dt-idl 1.1.49 → 1.1.51

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.
@@ -9,31 +9,37 @@ import (
9
9
  )
10
10
 
11
11
  type Responder[T ~string] interface {
12
+ // Writes a JSON response to the given response writer.
13
+ //
14
+ // The given data is converted to JSON. An error response is written if the marshal fails.
12
15
  Write(w http.ResponseWriter, status int, data any)
16
+ // Writes a JSON error response to the given response writer.
17
+ //
18
+ // The response is based on the shared "Error" schema.
13
19
  WriteError(w http.ResponseWriter, status int, message string)
20
+ // Writes a JSON error response with a specific code to the given response writer.
21
+ //
22
+ // The response is based on the shared "Error" schema.
14
23
  WriteErrorWithCode(w http.ResponseWriter, status int, code T, message string)
15
24
  }
16
25
 
17
26
  type StandardResponder[T ~string] struct {
18
- log logger.Logger
19
- baseErrorCode T
27
+ logger logger.Logger
28
+ defaultCode T
20
29
  }
21
30
 
22
- func NewStandardResponder[T ~string](log logger.Logger, baseErrorCode T) *StandardResponder[T] {
31
+ func NewStandardResponder[T ~string](logger logger.Logger, defaultCode T) *StandardResponder[T] {
23
32
  return &StandardResponder[T]{
24
- log: log,
25
- baseErrorCode: baseErrorCode,
33
+ logger: logger,
34
+ defaultCode: defaultCode,
26
35
  }
27
36
  }
28
37
 
29
- // Writes a JSON response to the given response writer.
30
- //
31
- // The given data is converted to JSON. An error response is written if the marshal fails.
32
38
  func (r *StandardResponder[T]) Write(w http.ResponseWriter, status int, data any) {
33
39
  // encode the given data object
34
40
  out, err := json.Marshal(data)
35
41
  if err != nil {
36
- r.log.Error(fmt.Errorf("Write: failed to marshal the data: %w", err))
42
+ r.logger.Error(fmt.Errorf("Write: failed to marshal the data: %w", err))
37
43
  r.WriteError(w, http.StatusInternalServerError, "error encoding the data")
38
44
  return
39
45
  }
@@ -42,22 +48,14 @@ func (r *StandardResponder[T]) Write(w http.ResponseWriter, status int, data any
42
48
  w.WriteHeader(status)
43
49
  _, err = w.Write(out)
44
50
  if err != nil {
45
- r.log.Fatal(fmt.Errorf("Write: failed to write a response: %w", err))
51
+ r.logger.Fatal(fmt.Errorf("Write: failed to write a response: %w", err))
46
52
  }
47
53
  }
48
54
 
49
- // Writes a JSON error response to the given response writer.
50
- //
51
- // The response is based on the generated "api.Error" schema.
52
55
  func (r *StandardResponder[T]) WriteError(w http.ResponseWriter, status int, message string) {
53
- r.WriteErrorWithCode(w, status, r.baseErrorCode, message)
56
+ r.WriteErrorWithCode(w, status, r.defaultCode, message)
54
57
  }
55
58
 
56
- // Writes a JSON error response with a specific code to the given response writer.
57
- //
58
- // The code is based on the generated "api.ErrorCode" enum.
59
- //
60
- // The response is based on the generated "api.Error" schema.
61
59
  func (r *StandardResponder[T]) WriteErrorWithCode(w http.ResponseWriter, status int, code T, message string) {
62
60
  // encode an error object with the given message
63
61
  out, err := json.Marshal(Error[T]{
@@ -66,13 +64,13 @@ func (r *StandardResponder[T]) WriteErrorWithCode(w http.ResponseWriter, status
66
64
  Message: message,
67
65
  })
68
66
  if err != nil {
69
- r.log.Fatal(fmt.Errorf("WriteErrorWithCode: failed to marshal the error: %w", err))
67
+ r.logger.Fatal(fmt.Errorf("WriteErrorWithCode: failed to marshal the error: %w", err))
70
68
  }
71
69
 
72
70
  // write the data to the given writer
73
71
  w.WriteHeader(status)
74
72
  _, err = w.Write(out)
75
73
  if err != nil {
76
- r.log.Fatal(fmt.Errorf("WriteErrorWithCode: failed to write a response: %w", err))
74
+ r.logger.Fatal(fmt.Errorf("WriteErrorWithCode: failed to write a response: %w", err))
77
75
  }
78
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivascva/dt-idl",
3
- "version": "1.1.49",
3
+ "version": "1.1.51",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",