@rivascva/dt-idl 1.1.49 → 1.1.50

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,26 +9,32 @@ 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
+ log 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](log logger.Logger, defaultCode T) *StandardResponder[T] {
23
32
  return &StandardResponder[T]{
24
- log: log,
25
- baseErrorCode: baseErrorCode,
33
+ log: log,
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)
@@ -46,18 +52,10 @@ func (r *StandardResponder[T]) Write(w http.ResponseWriter, status int, data any
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]{
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.50",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",