@asterflow/response 1.0.4 → 1.0.6
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.
- package/README.md +2 -6
- package/dist/cjs/index.cjs +62 -53
- package/dist/mjs/index.js +48 -39
- package/dist/types/controllers/Response.d.ts +35 -34
- package/dist/types/index.d.ts +1 -0
- package/dist/types/types/response.d.ts +11 -8
- package/dist/types/types/utils.d.ts +16 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,12 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|

|
|
6
6
|

|
|
7
|
+

|
|
7
8
|
|
|
8
|
-

|
|
9
|
-

|
|
10
|
-

|
|
11
|
-
|
|
12
|
-

|
|
13
9
|

|
|
14
10
|
|
|
15
11
|
</div>
|
|
@@ -204,7 +200,7 @@ const response = new Response()
|
|
|
204
200
|
|
|
205
201
|
## 🔗 Related Packages
|
|
206
202
|
|
|
207
|
-
- [
|
|
203
|
+
- [asterflow](https://www.npmjs.com/package/asterflow) - Core framework
|
|
208
204
|
- [@asterflow/router](https://www.npmjs.com/package/@asterflow/router) - Type-safe routing system
|
|
209
205
|
- [@asterflow/adapter](https://www.npmjs.com/package/@asterflow/adapter) - HTTP adapters for different runtimes
|
|
210
206
|
- [@asterflow/request](https://www.npmjs.com/package/@asterflow/request) - Unified HTTP request system
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,98 +1,104 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
2
|
+
var d = Object.defineProperty;
|
|
3
|
+
var i = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var h = Object.getOwnPropertyNames;
|
|
5
5
|
var c = Object.prototype.hasOwnProperty;
|
|
6
|
-
var
|
|
6
|
+
var y = (o, e) => {
|
|
7
7
|
for (var t in e)
|
|
8
|
-
|
|
9
|
-
},
|
|
8
|
+
d(o, t, { get: e[t], enumerable: !0 });
|
|
9
|
+
}, p = (o, e, t, n) => {
|
|
10
10
|
if (e && typeof e == "object" || typeof e == "function")
|
|
11
|
-
for (let
|
|
12
|
-
!c.call(
|
|
13
|
-
return
|
|
11
|
+
for (let s of h(e))
|
|
12
|
+
!c.call(o, s) && s !== t && d(o, s, { get: () => e[s], enumerable: !(n = i(e, s)) || n.enumerable });
|
|
13
|
+
return o;
|
|
14
14
|
};
|
|
15
|
-
var
|
|
15
|
+
var S = (o) => p(d({}, "__esModule", { value: !0 }), o);
|
|
16
16
|
// packages/response/src/index.ts
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
var u = {};
|
|
18
|
+
y(u, {
|
|
19
|
+
AsterResponse: () => r
|
|
20
20
|
});
|
|
21
|
-
module.exports =
|
|
21
|
+
module.exports = S(u);
|
|
22
22
|
// packages/response/src/controllers/Response.ts
|
|
23
|
-
var
|
|
23
|
+
var r = class o {
|
|
24
24
|
_status;
|
|
25
25
|
body;
|
|
26
|
-
|
|
27
|
-
cookies;
|
|
26
|
+
context;
|
|
28
27
|
constructor(e) {
|
|
29
|
-
this._status = e?.code ?? 200, this.body = e?.data, this.
|
|
30
|
-
}
|
|
31
|
-
clone(e) {
|
|
32
|
-
return new n({
|
|
33
|
-
code: e.code !== void 0 ? e.code : this._status,
|
|
34
|
-
data: e.data !== void 0 ? e.data : this.body,
|
|
35
|
-
header: e.header !== void 0 ? e.header : this.header,
|
|
36
|
-
cookies: e.cookies !== void 0 ? e.cookies : this.cookies
|
|
37
|
-
});
|
|
28
|
+
this._status = e?.code ?? 200, this.body = e?.data, this.context = e?.context ?? { header: {}, cookies: {} };
|
|
38
29
|
}
|
|
39
30
|
status(e) {
|
|
40
|
-
return this.
|
|
41
|
-
}
|
|
42
|
-
getStatus() {
|
|
43
|
-
return this._status;
|
|
31
|
+
return this._status = e, this;
|
|
44
32
|
}
|
|
45
33
|
code(e) {
|
|
46
34
|
return this.status(e);
|
|
47
35
|
}
|
|
36
|
+
getStatus() {
|
|
37
|
+
return this._status;
|
|
38
|
+
}
|
|
48
39
|
send(e) {
|
|
49
|
-
return this.
|
|
40
|
+
return this.body = e, this;
|
|
50
41
|
}
|
|
51
42
|
json(e) {
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
return this.send(e), this.context = {
|
|
44
|
+
...this.context,
|
|
45
|
+
header: {
|
|
46
|
+
...this.context.header,
|
|
47
|
+
"Content-Type": "application/json"
|
|
48
|
+
}
|
|
49
|
+
}, this;
|
|
54
50
|
}
|
|
55
51
|
success(e) {
|
|
56
|
-
return this.
|
|
52
|
+
return this.status(200), this.send(e), this;
|
|
57
53
|
}
|
|
58
54
|
created(e) {
|
|
59
|
-
return this.
|
|
55
|
+
return this.status(201), this.send(e), this;
|
|
60
56
|
}
|
|
61
57
|
noContent(e) {
|
|
62
|
-
return this.
|
|
58
|
+
return this.status(204), this.send(e), this;
|
|
63
59
|
}
|
|
64
60
|
badRequest(e) {
|
|
65
|
-
return this.
|
|
61
|
+
return this.status(400), this.send(e), this;
|
|
66
62
|
}
|
|
67
63
|
unauthorized(e) {
|
|
68
|
-
return this.
|
|
64
|
+
return this.status(401), this.send(e), this;
|
|
69
65
|
}
|
|
70
66
|
forbidden(e) {
|
|
71
|
-
return this.
|
|
67
|
+
return this.status(403), this.send(e), this;
|
|
72
68
|
}
|
|
73
69
|
notFound(e) {
|
|
74
|
-
return this.
|
|
70
|
+
return this.status(404), this.send(e), this;
|
|
75
71
|
}
|
|
76
72
|
validationError(e) {
|
|
77
|
-
return this.
|
|
73
|
+
return this.status(422), this.send(e), this;
|
|
78
74
|
}
|
|
79
75
|
internalServerError(e) {
|
|
80
|
-
return this.
|
|
76
|
+
return this.status(500), this.send(e), this;
|
|
81
77
|
}
|
|
82
78
|
setHeader(e, t) {
|
|
83
|
-
|
|
84
|
-
|
|
79
|
+
return this.context = {
|
|
80
|
+
...this.context,
|
|
81
|
+
header: {
|
|
82
|
+
...this.context.header,
|
|
83
|
+
[e]: t
|
|
84
|
+
}
|
|
85
|
+
}, this;
|
|
85
86
|
}
|
|
86
87
|
setCookie(e, t) {
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
return this.context = {
|
|
89
|
+
...this.context,
|
|
90
|
+
cookies: {
|
|
91
|
+
...this.context.cookies,
|
|
92
|
+
[e]: t
|
|
93
|
+
}
|
|
94
|
+
}, this;
|
|
89
95
|
}
|
|
90
96
|
toResponse() {
|
|
91
97
|
let e = new Headers();
|
|
92
98
|
e.set("Content-Type", "text/plain");
|
|
93
|
-
for (let [
|
|
99
|
+
for (let [n, s] of Object.entries(this.context.header)) e.set(n, s);
|
|
94
100
|
(typeof this.body == "object" || Array.isArray(this.body)) && e.set("Content-Type", "application/json");
|
|
95
|
-
for (let [
|
|
101
|
+
for (let [n, s] of Object.entries(this.context.cookies)) e.append("Set-Cookie", `${n}=${s}`);
|
|
96
102
|
let t = e.get("Content-Type") === "application/json" ? JSON.stringify(this.body) : String(this.body);
|
|
97
103
|
return new globalThis.Response(t, {
|
|
98
104
|
status: this._status,
|
|
@@ -102,13 +108,16 @@ var i = class n {
|
|
|
102
108
|
toServerResponse(e) {
|
|
103
109
|
let t = new Headers();
|
|
104
110
|
t.set("Content-Type", "text/plain");
|
|
105
|
-
for (let [
|
|
111
|
+
for (let [s, a] of Object.entries(this.context.header)) t.set(s, a);
|
|
106
112
|
(typeof this.body == "object" || Array.isArray(this.body)) && t.set("Content-Type", "application/json");
|
|
107
|
-
for (let [
|
|
108
|
-
let
|
|
109
|
-
e.writeHead(this._status, Object.fromEntries(t)), e.end(
|
|
113
|
+
for (let [s, a] of Object.entries(this.context.cookies)) t.append("Set-Cookie", `${s}=${a}`);
|
|
114
|
+
let n = t.get("Content-Type") === "application/json" ? JSON.stringify(this.body) : String(this.body);
|
|
115
|
+
e.writeHead(this._status, Object.fromEntries(t)), e.end(n);
|
|
116
|
+
}
|
|
117
|
+
static create() {
|
|
118
|
+
return new o();
|
|
110
119
|
}
|
|
111
120
|
};
|
|
112
121
|
0 && (module.exports = {
|
|
113
|
-
|
|
122
|
+
AsterResponse
|
|
114
123
|
});
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,77 +1,83 @@
|
|
|
1
1
|
// packages/response/src/controllers/Response.ts
|
|
2
|
-
var
|
|
2
|
+
var a = class d {
|
|
3
3
|
_status;
|
|
4
4
|
body;
|
|
5
|
-
|
|
6
|
-
cookies;
|
|
5
|
+
context;
|
|
7
6
|
constructor(e) {
|
|
8
|
-
this._status = e?.code ?? 200, this.body = e?.data, this.
|
|
9
|
-
}
|
|
10
|
-
clone(e) {
|
|
11
|
-
return new a({
|
|
12
|
-
code: e.code !== void 0 ? e.code : this._status,
|
|
13
|
-
data: e.data !== void 0 ? e.data : this.body,
|
|
14
|
-
header: e.header !== void 0 ? e.header : this.header,
|
|
15
|
-
cookies: e.cookies !== void 0 ? e.cookies : this.cookies
|
|
16
|
-
});
|
|
7
|
+
this._status = e?.code ?? 200, this.body = e?.data, this.context = e?.context ?? { header: {}, cookies: {} };
|
|
17
8
|
}
|
|
18
9
|
status(e) {
|
|
19
|
-
return this.
|
|
20
|
-
}
|
|
21
|
-
getStatus() {
|
|
22
|
-
return this._status;
|
|
10
|
+
return this._status = e, this;
|
|
23
11
|
}
|
|
24
12
|
code(e) {
|
|
25
13
|
return this.status(e);
|
|
26
14
|
}
|
|
15
|
+
getStatus() {
|
|
16
|
+
return this._status;
|
|
17
|
+
}
|
|
27
18
|
send(e) {
|
|
28
|
-
return this.
|
|
19
|
+
return this.body = e, this;
|
|
29
20
|
}
|
|
30
21
|
json(e) {
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
return this.send(e), this.context = {
|
|
23
|
+
...this.context,
|
|
24
|
+
header: {
|
|
25
|
+
...this.context.header,
|
|
26
|
+
"Content-Type": "application/json"
|
|
27
|
+
}
|
|
28
|
+
}, this;
|
|
33
29
|
}
|
|
34
30
|
success(e) {
|
|
35
|
-
return this.
|
|
31
|
+
return this.status(200), this.send(e), this;
|
|
36
32
|
}
|
|
37
33
|
created(e) {
|
|
38
|
-
return this.
|
|
34
|
+
return this.status(201), this.send(e), this;
|
|
39
35
|
}
|
|
40
36
|
noContent(e) {
|
|
41
|
-
return this.
|
|
37
|
+
return this.status(204), this.send(e), this;
|
|
42
38
|
}
|
|
43
39
|
badRequest(e) {
|
|
44
|
-
return this.
|
|
40
|
+
return this.status(400), this.send(e), this;
|
|
45
41
|
}
|
|
46
42
|
unauthorized(e) {
|
|
47
|
-
return this.
|
|
43
|
+
return this.status(401), this.send(e), this;
|
|
48
44
|
}
|
|
49
45
|
forbidden(e) {
|
|
50
|
-
return this.
|
|
46
|
+
return this.status(403), this.send(e), this;
|
|
51
47
|
}
|
|
52
48
|
notFound(e) {
|
|
53
|
-
return this.
|
|
49
|
+
return this.status(404), this.send(e), this;
|
|
54
50
|
}
|
|
55
51
|
validationError(e) {
|
|
56
|
-
return this.
|
|
52
|
+
return this.status(422), this.send(e), this;
|
|
57
53
|
}
|
|
58
54
|
internalServerError(e) {
|
|
59
|
-
return this.
|
|
55
|
+
return this.status(500), this.send(e), this;
|
|
60
56
|
}
|
|
61
57
|
setHeader(e, t) {
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
return this.context = {
|
|
59
|
+
...this.context,
|
|
60
|
+
header: {
|
|
61
|
+
...this.context.header,
|
|
62
|
+
[e]: t
|
|
63
|
+
}
|
|
64
|
+
}, this;
|
|
64
65
|
}
|
|
65
66
|
setCookie(e, t) {
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
return this.context = {
|
|
68
|
+
...this.context,
|
|
69
|
+
cookies: {
|
|
70
|
+
...this.context.cookies,
|
|
71
|
+
[e]: t
|
|
72
|
+
}
|
|
73
|
+
}, this;
|
|
68
74
|
}
|
|
69
75
|
toResponse() {
|
|
70
76
|
let e = new Headers();
|
|
71
77
|
e.set("Content-Type", "text/plain");
|
|
72
|
-
for (let [
|
|
78
|
+
for (let [o, s] of Object.entries(this.context.header)) e.set(o, s);
|
|
73
79
|
(typeof this.body == "object" || Array.isArray(this.body)) && e.set("Content-Type", "application/json");
|
|
74
|
-
for (let [
|
|
80
|
+
for (let [o, s] of Object.entries(this.context.cookies)) e.append("Set-Cookie", `${o}=${s}`);
|
|
75
81
|
let t = e.get("Content-Type") === "application/json" ? JSON.stringify(this.body) : String(this.body);
|
|
76
82
|
return new globalThis.Response(t, {
|
|
77
83
|
status: this._status,
|
|
@@ -81,13 +87,16 @@ var r = class a {
|
|
|
81
87
|
toServerResponse(e) {
|
|
82
88
|
let t = new Headers();
|
|
83
89
|
t.set("Content-Type", "text/plain");
|
|
84
|
-
for (let [
|
|
90
|
+
for (let [s, n] of Object.entries(this.context.header)) t.set(s, n);
|
|
85
91
|
(typeof this.body == "object" || Array.isArray(this.body)) && t.set("Content-Type", "application/json");
|
|
86
|
-
for (let [
|
|
87
|
-
let
|
|
88
|
-
e.writeHead(this._status, Object.fromEntries(t)), e.end(
|
|
92
|
+
for (let [s, n] of Object.entries(this.context.cookies)) t.append("Set-Cookie", `${s}=${n}`);
|
|
93
|
+
let o = t.get("Content-Type") === "application/json" ? JSON.stringify(this.body) : String(this.body);
|
|
94
|
+
e.writeHead(this._status, Object.fromEntries(t)), e.end(o);
|
|
95
|
+
}
|
|
96
|
+
static create() {
|
|
97
|
+
return new d();
|
|
89
98
|
}
|
|
90
99
|
};
|
|
91
100
|
export {
|
|
92
|
-
|
|
101
|
+
a as AsterResponse
|
|
93
102
|
};
|
|
@@ -1,38 +1,39 @@
|
|
|
1
|
-
import type { BodyMap, Responders, ResponseOptions } from '../types/response';
|
|
2
1
|
import { ServerResponse } from 'http';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
cookies
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
2
|
+
import type { BaseContext, BodyMap, Responders, ResponseOptions } from '../types/response';
|
|
3
|
+
import type { HttpHeader, Prettify } from '../types/utils';
|
|
4
|
+
export declare class AsterResponse<RawResponder extends Responders = Responders, BodySchema extends BodyMap<RawResponder> = BodyMap<RawResponder>, StatusCode extends keyof BodySchema = keyof BodySchema, Context extends BaseContext = BaseContext> {
|
|
5
|
+
protected _status: StatusCode;
|
|
6
|
+
body?: BodySchema[StatusCode];
|
|
7
|
+
context: Context;
|
|
8
|
+
constructor(options?: ResponseOptions<RawResponder, BodySchema, StatusCode, Context>);
|
|
9
|
+
status<NS extends keyof BodySchema>(code: NS): AsterResponse<RawResponder, BodySchema, NS, Context>;
|
|
10
|
+
code<NS extends keyof BodySchema>(code: NS): AsterResponse<RawResponder, BodySchema, NS, Context>;
|
|
11
|
+
getStatus(): StatusCode;
|
|
12
|
+
send(data: BodySchema[StatusCode]): AsterResponse<RawResponder, BodySchema, StatusCode, Context>;
|
|
13
|
+
json(data: BodySchema[StatusCode]): AsterResponse<RawResponder, BodySchema, StatusCode, {
|
|
14
|
+
header: Prettify<Context["header"] & {
|
|
15
|
+
"Content-Type": "application/json";
|
|
16
|
+
}>;
|
|
17
|
+
cookies: Context["cookies"];
|
|
18
|
+
}>;
|
|
19
|
+
success(data: BodySchema[200]): AsterResponse<RawResponder, BodySchema, 200, Context>;
|
|
20
|
+
created(data: BodySchema[201]): AsterResponse<RawResponder, BodySchema, 201, Context>;
|
|
21
|
+
noContent(data: BodySchema[204]): AsterResponse<RawResponder, BodySchema, 204, Context>;
|
|
22
|
+
badRequest(data: BodySchema[400]): AsterResponse<RawResponder, BodySchema, 400, Context>;
|
|
23
|
+
unauthorized(data: BodySchema[401]): AsterResponse<RawResponder, BodySchema, 401, Context>;
|
|
24
|
+
forbidden(data: BodySchema[403]): AsterResponse<RawResponder, BodySchema, 403, Context>;
|
|
25
|
+
notFound(data: BodySchema[404]): AsterResponse<RawResponder, BodySchema, 404, Context>;
|
|
26
|
+
validationError(data: BodySchema[422]): AsterResponse<RawResponder, BodySchema, 422, Context>;
|
|
27
|
+
internalServerError(data: BodySchema[500]): AsterResponse<RawResponder, BodySchema, 500, Context>;
|
|
28
|
+
setHeader<Name extends HttpHeader, Value extends string>(name: Name, value: Value): AsterResponse<RawResponder, BodySchema, StatusCode, {
|
|
29
|
+
header: Prettify<Context["header"] & Record<Name, Value>>;
|
|
30
|
+
cookies: Context["cookies"];
|
|
31
|
+
}>;
|
|
32
|
+
setCookie<Name extends string, Value extends string>(name: Name, value: Value): AsterResponse<RawResponder, BodySchema, StatusCode, {
|
|
33
|
+
header: Context["header"];
|
|
34
|
+
cookies: Prettify<Context["cookies"] & Record<Name, Value>>;
|
|
35
|
+
}>;
|
|
36
36
|
toResponse(): globalThis.Response;
|
|
37
37
|
toServerResponse(output: ServerResponse): void;
|
|
38
|
+
static create<RawResponder extends Responders>(): AsterResponse<RawResponder, BodyMap<RawResponder>, keyof RawResponder, BaseContext>;
|
|
38
39
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
export
|
|
2
|
-
[
|
|
3
|
-
}
|
|
1
|
+
export interface Responders {
|
|
2
|
+
[code: number]: unknown;
|
|
3
|
+
}
|
|
4
4
|
export type BodyMap<Responder extends Responders> = {
|
|
5
5
|
[S in keyof Responder]: Responder[S];
|
|
6
6
|
};
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
export type BaseContext = {
|
|
8
|
+
header: Record<string, string>;
|
|
9
|
+
cookies: Record<string, string>;
|
|
10
|
+
};
|
|
11
|
+
export type ResponseOptions<RawResponder extends Responders, BodySchema extends BodyMap<RawResponder>, StatusCode extends keyof BodySchema, Context extends BaseContext> = {
|
|
12
|
+
data?: BodySchema[StatusCode];
|
|
13
|
+
code?: StatusCode;
|
|
14
|
+
context?: Context;
|
|
12
15
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { OutgoingHttpHeaders } from 'http';
|
|
2
|
+
type OmitIndexSignature<T> = {
|
|
3
|
+
[K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* HTTP header strings
|
|
7
|
+
* Use this type only for input values, not for output values.
|
|
8
|
+
*/
|
|
9
|
+
export type HttpHeader = keyof OmitIndexSignature<OutgoingHttpHeaders> | (string & Record<never, never>);
|
|
10
|
+
/**
|
|
11
|
+
* Forces TypeScript to evaluate and “flatten” the structure of an object type.
|
|
12
|
+
*/
|
|
13
|
+
export type Prettify<T> = {
|
|
14
|
+
[K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
|
|
15
|
+
} & {};
|
|
16
|
+
export {};
|