@cfworker/web 5.0.3 → 5.0.5

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