Skip to content

Commit

Permalink
Merge branch 'mathematic-inc:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
egmacke authored Apr 8, 2024
2 parents 58b7b6f + e045ade commit 769e3cf
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ lib/
.DS_STORE

# API extractor
ts-japi.api.json
ts-japi.api.json
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "1.9.0"
".": "1.10.1"
}
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## [1.10.1](https://github.com/mathematic-inc/ts-japi/compare/v1.10.0...v1.10.1) (2024-04-06)


### Bug Fixes

* bump for PR ([173364f](https://github.com/mathematic-inc/ts-japi/commit/173364f99041229747d53e9603c0f84985614156))

## [1.10.0](https://github.com/mathematic-inc/ts-japi/compare/v1.9.1...v1.10.0) (2024-03-21)


### Features

* add header to the source object ([#73](https://github.com/mathematic-inc/ts-japi/issues/73)) ([d5c1cbe](https://github.com/mathematic-inc/ts-japi/commit/d5c1cbefa7231336bde9436f8c3805e1cbd3e366))

## [1.9.1](https://github.com/mathematic-inc/ts-japi/compare/v1.9.0...v1.9.1) (2023-10-11)


### Bug Fixes

* fix logic where nested includes were not calculated correctly. Fixes [#68](https://github.com/mathematic-inc/ts-japi/issues/68) ([#69](https://github.com/mathematic-inc/ts-japi/issues/69)) ([701a7e6](https://github.com/mathematic-inc/ts-japi/commit/701a7e61d8239abbe54ba88bcf65f8fb7552ad79))

## [1.9.0](https://github.com/mathematic-inc/ts-japi/compare/v1.8.1...v1.9.0) (2023-09-04)


Expand Down
1 change: 1 addition & 0 deletions docs/api/ts-japi.errorserializer.defaultoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ErrorSerializer {
source: {
pointer: string;
parameter: undefined;
header: undefined;
};
};
metaizers: {};
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ts-japi.errorserializer.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class ErrorSerializer<ErrorType>

| Property | Modifiers | Type | Description |
| ------------------------------------------------------------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
| [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. |
| [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. |

## Methods

Expand Down
2 changes: 1 addition & 1 deletion docs/api/ts-japi.japierror.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class JapiError
| [id?](./ts-japi.japierror.id.md) | | string | <i>(Optional)</i> A unique identifier for this particular occurrence of the problem. |
| [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 |
| [meta?](./ts-japi.japierror.meta.md) | | Meta | <i>(Optional)</i> A meta object containing non-standard meta information about the error. |
| [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. |
| [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. |
| [status?](./ts-japi.japierror.status.md) | | string | <i>(Optional)</i> The HTTP status code applicable to this problem, expressed as a string value. |
| [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. |

Expand Down
1 change: 1 addition & 0 deletions docs/api/ts-japi.japierror.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class JapiError {
source?: {
pointer?: string;
parameter?: string;
header?: string;
};
}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-japi",
"version": "1.9.0",
"version": "1.10.1",
"description": "A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification",
"main": "lib/index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/classes/error-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class ErrorSerializer<ErrorType extends Dictionary<any>> {
source: {
pointer: 'location',
parameter: undefined,
header: undefined,
},
},
metaizers: {},
Expand Down Expand Up @@ -101,6 +102,9 @@ export default class ErrorSerializer<ErrorType extends Dictionary<any>> {
if (attributes.source.parameter && e[attributes.source.parameter]) {
eo.source.parameter = String(e[attributes.source.parameter]);
}
if (attributes.source.header && e[attributes.source.header]) {
eo.source.header = String(e[attributes.source.header]);
}
if (Object.keys(eo.source).length === 0) {
delete eo.source;
}
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/error-serializer.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ export interface ErrorSourceAttribute<T> {
* @defaultValue `undefined`
*/
parameter: keyof T;

/**
* A string indicating the name of a single request header which caused
* the error.
*
* @defaultValue `undefined`
*/
header: keyof T;
}

export interface ErrorSerializerOptions<T extends Dictionary<any>> {
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/error.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export interface ErrorSource {
* A string indicating which URI query parameter caused the error.
*/
parameter?: string;

/**
* A string indicating the name of a single request header which caused
* the error.
*/
header?: string;
}

export interface ErrorOptions {
Expand Down
6 changes: 6 additions & 0 deletions src/models/error.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export default class JapiError {
* A string indicating which URI query parameter caused the error.
*/
parameter?: string;

/**
* A string indicating the name of a single request header which caused
* the error.
*/
header?: string;
};

/**
Expand Down
1 change: 0 additions & 1 deletion src/models/link.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { URL } from 'url';
import Meta from './meta.model';

export default class Link {
Expand Down
10 changes: 8 additions & 2 deletions test/error-serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CustomForbiddenError extends JapiError {
source: {
pointer: 'perhaps/in/response/body',
parameter: 'some-query-param',
header: 'some-header',
},
});
}
Expand All @@ -40,6 +41,7 @@ describe('Error Serializer Tests', () => {
source: {
pointer: 'name',
parameter: 'name',
header: 'name',
},
},
},
Expand All @@ -51,7 +53,7 @@ describe('Error Serializer Tests', () => {
code: 'Error',
detail: 'This is a test.',
id: 'Error',
source: { parameter: 'Error', pointer: 'Error' },
source: { header: 'Error', parameter: 'Error', pointer: 'Error' },
status: 'Error',
title: 'Error',
},
Expand Down Expand Up @@ -102,6 +104,7 @@ describe('Error Serializer Tests', () => {
source: {
parameter: 'some-query-param',
pointer: 'perhaps/in/response/body',
header: 'some-header',
},
},
],
Expand All @@ -110,15 +113,18 @@ describe('Error Serializer Tests', () => {
],
])('With Options %o', (options, ErrorType, expectedFrom) => {
let PrimitiveErrorSerializer: ErrorSerializer<any>;

it('should construct a ErrorSerializer', () => {
expect(() => (PrimitiveErrorSerializer = new ErrorSerializer(options))).not.toThrow();
});

it('tests a ErrorSerializer on User ID %s', () => {
// Get dummy data.
const error = new ErrorType('This is a test.');

// Testing methods
let document: ErrorDocument;
let document: ErrorDocument | undefined;

expect(() => (document = PrimitiveErrorSerializer.serialize(error))).not.toThrow();
expect(() => (document = PrimitiveErrorSerializer.serialize([error]))).not.toThrow();

Expand Down
Loading

0 comments on commit 769e3cf

Please sign in to comment.