Skip to content

Commit d5c1cbe

Browse files
authored
feat: add header to the source object (#73)
1 parent 9b316af commit d5c1cbe

9 files changed

+36
-4
lines changed

Diff for: docs/api/ts-japi.errorserializer.defaultoptions.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ErrorSerializer {
2121
source: {
2222
pointer: string;
2323
parameter: undefined;
24+
header: undefined;
2425
};
2526
};
2627
metaizers: {};

Diff for: docs/api/ts-japi.errorserializer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class ErrorSerializer<ErrorType>
2828

2929
| Property | Modifiers | Type | Description |
3030
| ------------------------------------------------------------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
31-
| [defaultOptions](./ts-japi.errorserializer.defaultoptions.md) | <code>static</code> | { version: string; attributes: { id: string; status: string; code: string; title: string; detail: string; source: { pointer: string; parameter: undefined; }; }; metaizers: {}; linkers: {}; } | Default options. Can be edited to change default options globally. |
31+
| [defaultOptions](./ts-japi.errorserializer.defaultoptions.md) | <code>static</code> | { version: string; attributes: { id: string; status: string; code: string; title: string; detail: string; source: { pointer: string; parameter: undefined; header: undefined; }; }; metaizers: {}; linkers: {}; } | Default options. Can be edited to change default options globally. |
3232

3333
## Methods
3434

Diff for: docs/api/ts-japi.japierror.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class JapiError
2525
| [id?](./ts-japi.japierror.id.md) | | string | <i>(Optional)</i> A unique identifier for this particular occurrence of the problem. |
2626
| [links?](./ts-japi.japierror.links.md) | | [Dictionary](./ts-japi.dictionary.md)&lt;Link \| [nullish](./ts-japi.nullish.md)&gt; | <i>(Optional)</i> A links object |
2727
| [meta?](./ts-japi.japierror.meta.md) | | Meta | <i>(Optional)</i> A meta object containing non-standard meta information about the error. |
28-
| [source?](./ts-japi.japierror.source.md) | | { pointer?: string; parameter?: string; } | <i>(Optional)</i> An object containing references to the source of the error, optionally including any of the following members. |
28+
| [source?](./ts-japi.japierror.source.md) | | { pointer?: string; parameter?: string; header?: string; } | <i>(Optional)</i> An object containing references to the source of the error, optionally including any of the following members. |
2929
| [status?](./ts-japi.japierror.status.md) | | string | <i>(Optional)</i> The HTTP status code applicable to this problem, expressed as a string value. |
3030
| [title?](./ts-japi.japierror.title.md) | | string | <i>(Optional)</i> A short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization. |
3131

Diff for: docs/api/ts-japi.japierror.source.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class JapiError {
1414
source?: {
1515
pointer?: string;
1616
parameter?: string;
17+
header?: string;
1718
};
1819
}
1920
```

Diff for: src/classes/error-serializer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default class ErrorSerializer<ErrorType extends Dictionary<any>> {
2828
source: {
2929
pointer: 'location',
3030
parameter: undefined,
31+
header: undefined,
3132
},
3233
},
3334
metaizers: {},
@@ -101,6 +102,9 @@ export default class ErrorSerializer<ErrorType extends Dictionary<any>> {
101102
if (attributes.source.parameter && e[attributes.source.parameter]) {
102103
eo.source.parameter = String(e[attributes.source.parameter]);
103104
}
105+
if (attributes.source.header && e[attributes.source.header]) {
106+
eo.source.header = String(e[attributes.source.header]);
107+
}
104108
if (Object.keys(eo.source).length === 0) {
105109
delete eo.source;
106110
}

Diff for: src/interfaces/error-serializer.interface.ts

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ export interface ErrorSourceAttribute<T> {
6565
* @defaultValue `undefined`
6666
*/
6767
parameter: keyof T;
68+
69+
/**
70+
* A string indicating the name of a single request header which caused
71+
* the error.
72+
*
73+
* @defaultValue `undefined`
74+
*/
75+
header: keyof T;
6876
}
6977

7078
export interface ErrorSerializerOptions<T extends Dictionary<any>> {

Diff for: src/interfaces/error.interface.ts

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export interface ErrorSource {
1010
* A string indicating which URI query parameter caused the error.
1111
*/
1212
parameter?: string;
13+
14+
/**
15+
* A string indicating the name of a single request header which caused
16+
* the error.
17+
*/
18+
header?: string;
1319
}
1420

1521
export interface ErrorOptions {

Diff for: src/models/error.model.ts

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ export default class JapiError {
8181
* A string indicating which URI query parameter caused the error.
8282
*/
8383
parameter?: string;
84+
85+
/**
86+
* A string indicating the name of a single request header which caused
87+
* the error.
88+
*/
89+
header?: string;
8490
};
8591

8692
/**

Diff for: test/error-serializer.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CustomForbiddenError extends JapiError {
1515
source: {
1616
pointer: 'perhaps/in/response/body',
1717
parameter: 'some-query-param',
18+
header: 'some-header',
1819
},
1920
});
2021
}
@@ -40,6 +41,7 @@ describe('Error Serializer Tests', () => {
4041
source: {
4142
pointer: 'name',
4243
parameter: 'name',
44+
header: 'name',
4345
},
4446
},
4547
},
@@ -51,7 +53,7 @@ describe('Error Serializer Tests', () => {
5153
code: 'Error',
5254
detail: 'This is a test.',
5355
id: 'Error',
54-
source: { parameter: 'Error', pointer: 'Error' },
56+
source: { header: 'Error', parameter: 'Error', pointer: 'Error' },
5557
status: 'Error',
5658
title: 'Error',
5759
},
@@ -102,6 +104,7 @@ describe('Error Serializer Tests', () => {
102104
source: {
103105
parameter: 'some-query-param',
104106
pointer: 'perhaps/in/response/body',
107+
header: 'some-header',
105108
},
106109
},
107110
],
@@ -110,15 +113,18 @@ describe('Error Serializer Tests', () => {
110113
],
111114
])('With Options %o', (options, ErrorType, expectedFrom) => {
112115
let PrimitiveErrorSerializer: ErrorSerializer<any>;
116+
113117
it('should construct a ErrorSerializer', () => {
114118
expect(() => (PrimitiveErrorSerializer = new ErrorSerializer(options))).not.toThrow();
115119
});
120+
116121
it('tests a ErrorSerializer on User ID %s', () => {
117122
// Get dummy data.
118123
const error = new ErrorType('This is a test.');
119124

120125
// Testing methods
121-
let document: ErrorDocument;
126+
let document: ErrorDocument | undefined;
127+
122128
expect(() => (document = PrimitiveErrorSerializer.serialize(error))).not.toThrow();
123129
expect(() => (document = PrimitiveErrorSerializer.serialize([error]))).not.toThrow();
124130

0 commit comments

Comments
 (0)