@cfworker/web 5.0.6 → 5.1.0

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.
@@ -2,6 +2,7 @@ export declare class HttpError extends Error {
2
2
  readonly status: number;
3
3
  readonly statusText: string;
4
4
  readonly body: string | object | null;
5
- constructor(status: number, body?: string | object | null);
5
+ readonly headers: Headers;
6
+ constructor(status: number, body?: string | object | null, headers?: HeadersInit);
6
7
  toResponse(): Response;
7
8
  }
@@ -9,7 +9,8 @@ class HttpError extends Error {
9
9
  status;
10
10
  statusText;
11
11
  body;
12
- constructor(status, body = null) {
12
+ headers;
13
+ constructor(status, body = null, headers = {}) {
13
14
  const statusText = statuses_1.default.message[status];
14
15
  super(statusText);
15
16
  this.name = this.constructor.name;
@@ -19,17 +20,22 @@ class HttpError extends Error {
19
20
  this.status = status;
20
21
  this.statusText = statusText;
21
22
  this.body = statuses_1.default.empty[this.status] ? null : (body ?? statusText);
23
+ this.headers = headers instanceof Headers ? headers : new Headers(headers);
22
24
  }
23
25
  toResponse() {
24
26
  let body = this.body;
25
- const headers = {};
27
+ const headers = this.headers;
26
28
  if (body === null) {
27
29
  }
28
30
  else if (typeof body === 'string') {
29
- headers['content-type'] = 'text/plain';
31
+ if (!headers.has('content-type')) {
32
+ headers.set('content-type', 'text/plain');
33
+ }
30
34
  }
31
35
  else {
32
- headers['content-type'] = 'application/json';
36
+ if (!headers.has('content-type')) {
37
+ headers.set('content-type', 'application/json');
38
+ }
33
39
  body = JSON.stringify(body);
34
40
  }
35
41
  const { status, statusText } = this;
@@ -2,6 +2,7 @@ export declare class HttpError extends Error {
2
2
  readonly status: number;
3
3
  readonly statusText: string;
4
4
  readonly body: string | object | null;
5
- constructor(status: number, body?: string | object | null);
5
+ readonly headers: Headers;
6
+ constructor(status: number, body?: string | object | null, headers?: HeadersInit);
6
7
  toResponse(): Response;
7
8
  }
@@ -3,7 +3,8 @@ export class HttpError extends Error {
3
3
  status;
4
4
  statusText;
5
5
  body;
6
- constructor(status, body = null) {
6
+ headers;
7
+ constructor(status, body = null, headers = {}) {
7
8
  const statusText = statuses.message[status];
8
9
  super(statusText);
9
10
  this.name = this.constructor.name;
@@ -13,17 +14,22 @@ export class HttpError extends Error {
13
14
  this.status = status;
14
15
  this.statusText = statusText;
15
16
  this.body = statuses.empty[this.status] ? null : (body ?? statusText);
17
+ this.headers = headers instanceof Headers ? headers : new Headers(headers);
16
18
  }
17
19
  toResponse() {
18
20
  let body = this.body;
19
- const headers = {};
21
+ const headers = this.headers;
20
22
  if (body === null) {
21
23
  }
22
24
  else if (typeof body === 'string') {
23
- headers['content-type'] = 'text/plain';
25
+ if (!headers.has('content-type')) {
26
+ headers.set('content-type', 'text/plain');
27
+ }
24
28
  }
25
29
  else {
26
- headers['content-type'] = 'application/json';
30
+ if (!headers.has('content-type')) {
31
+ headers.set('content-type', 'application/json');
32
+ }
27
33
  body = JSON.stringify(body);
28
34
  }
29
35
  const { status, statusText } = this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfworker/web",
3
- "version": "5.0.6",
3
+ "version": "5.1.0",
4
4
  "description": "Web framework for Cloudflare Workers and service workers, inspired by Koa and fastify",
5
5
  "keywords": [
6
6
  "middleware",