Skip to content

Commit fa0b960

Browse files
committed
Updates for new standard
1 parent 9a6de8b commit fa0b960

11 files changed

Lines changed: 208 additions & 146 deletions

File tree

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ updates:
1010
- dependency-name: "@apst/biome"
1111
- dependency-name: "@apst/tsconfig"
1212
- dependency-name: "@biomejs/biome"
13-
- dependency-name: "tsup"
13+
- dependency-name: "tsdown"
1414
- dependency-name: "typedoc"
1515
- dependency-name: "typedoc-plugin-markdown"
1616
- dependency-name: "typescript"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A response builder for JavaScript / TypeScript.
44

5-
This package includes different response builders based on the JSON response structure specified in [JSON Data Error Response (JDER)](https://github.com/jder-std/spec). With the builders, various kinds of responses can be created easily instead of sending plain text responses.
5+
This package includes different response builders based on the JSON response structure specified in [JSON Data Errors Response (JDER)](https://github.com/jder-std/spec). With the builders, various kinds of responses can be created easily instead of sending plain text responses.
66

77
## Installation
88

docs/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ import { createJsonResponse } from "@jderjs/core";
7979

8080
const route = (): Response => {
8181
return createJsonResponse({
82-
error: {
83-
code: "server",
84-
},
82+
errors: [
83+
{
84+
code: "server",
85+
},
86+
],
8587
});
8688
}
8789
```
@@ -91,9 +93,11 @@ And the response will be shown as below:
9193
```json
9294
{
9395
"success": false,
94-
"error": {
95-
"code": "server"
96-
}
96+
"errors": [
97+
{
98+
"code": "server"
99+
}
100+
]
97101
}
98102
```
99103

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"@apst/biome": "~0.3.0",
55
"@apst/tsconfig": "~0.1.3",
66
"@biomejs/biome": "^2.1.4",
7-
"tsdown": "~0.13.0",
7+
"tsdown": "~0.14.1",
88
"typedoc": "~0.28.10",
99
"typedoc-plugin-markdown": "^4.8.1",
1010
"typescript": "^5.9.2"

package/CHANGELOG.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
## 0.3.0
2+
3+
### Breaking Changes
4+
5+
- Support new standard
6+
7+
### Migrating from 0.2.0 to 0.3.0
8+
9+
```diff
10+
import {
11+
createJsonResponse,
12+
} from "@jderjs/core";
13+
14+
const route = (): Response => {
15+
return createJsonResponse({
16+
- error: {
17+
- code: "parse",
18+
- field: "title",
19+
- message: "Invalid title",
20+
- },
21+
+ errors: [
22+
+ {
23+
+ code: "parse",
24+
+ path: ["json", "title"],
25+
+ message: "Invalid title",
26+
+ }
27+
+ ],
28+
});
29+
}
30+
```
31+
132
## 0.2.0 (2025-08-06)
233

334
### What's Changed

package/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A standard response builder.
44

5-
This package includes different response builders based on the JSON response structure specified in [JSON Data Error Response (JDER)](https://github.com/jder-std/spec). With the builders, various kinds of responses can be created easily instead of sending plain text responses.
5+
This package includes different response builders based on the JSON response structure specified in [JSON Data Errors Response (JDER)](https://github.com/jder-std/spec). With the builders, various kinds of responses can be created easily instead of sending plain text responses.
66

77
## Quick Start
88

package/src/@types/response.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ type HeaderTuple = [
66

77
/** JSON response error. */
88
type JsonResponseError = {
9-
/** Error code. */
9+
/** Code representing the error. */
1010
code: string;
11-
/** Field of the error. */
12-
field?: string;
13-
/** Message of the error. */
11+
/** Indicates where the error occurred. */
12+
path?: string[];
13+
/** Detail of the error. */
1414
message?: string;
1515
};
1616

1717
/** JSON response. */
1818
type JsonResponse<D = unknown> = {
19-
/** Whether the response is successful. */
19+
/** Indicates whether the response is successful or not. */
2020
success: boolean;
21-
/** Data for the response when `success` is `true`. */
21+
/** Requested information for the response when `success` is `true`. */
2222
data?: D;
23-
/** Error for the response when `success` is `false`. */
24-
error?: JsonResponseError;
23+
/** A list of errors for the response when `success` is `false`. */
24+
errors?: JsonResponseError[];
2525
};
2626

2727
export type { HeaderTuple, JsonResponseError, JsonResponse };

package/src/response/json/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ type CreateJsonResponseOptions<D = unknown> =
4040
*
4141
* const route = (): Response => {
4242
* return createJsonResponse({
43-
* error: {
44-
* code: "server",
45-
* message: "Internal server error",
46-
* },
43+
* status: 500,
44+
* errors: [
45+
* {
46+
* code: "server",
47+
* message: "Internal server error",
48+
* },
49+
* ],
4750
* });
4851
* };
4952
* ```

package/src/response/json/struct.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ type CreateJsonSuccessResponseStructOptions<D = unknown> = {
2828
*/
2929
data?: D;
3030
/**
31-
* Error for the response when `success` is `false`.
31+
* A list of errors for the response when `success` is `false`.
3232
*/
33-
error?: never;
33+
errors?: never;
3434
};
3535

3636
type CreateJsonFailureResponseStructOptions = {
@@ -53,9 +53,9 @@ type CreateJsonFailureResponseStructOptions = {
5353
*/
5454
data?: never;
5555
/**
56-
* Error for the response when `success` is `false`.
56+
* A list of errors for the response when `success` is `false`.
5757
*/
58-
error: JsonResponseError;
58+
errors: JsonResponseError[];
5959
};
6060

6161
/** Options of `createJsonResponseStruct` function. */
@@ -79,8 +79,8 @@ const isResponseSuccess = <D>(
7979
): boolean => {
8080
// blank options
8181
if (!options) return true;
82-
// error specified
83-
if ("error" in options && typeof options.error === "object") return false;
82+
// errors specified
83+
if ("errors" in options && Array.isArray(options.errors)) return false;
8484
// default
8585
return true;
8686
};
@@ -111,7 +111,8 @@ const createJsonResponseStruct = <D = unknown>(
111111
}
112112
: {
113113
success: false,
114-
error: (options as CreateJsonFailureResponseStructOptions).error,
114+
errors: (options as CreateJsonFailureResponseStructOptions)
115+
.errors,
115116
};
116117

117118
return {

0 commit comments

Comments
 (0)