jj.js 0.8.7 → 0.9.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.
package/lib/cookie.js CHANGED
@@ -1,41 +1,83 @@
1
- const {cookie: cfg_cookie} = require('./config');
2
- const Context = require('./context');
3
-
4
- class Cookie extends Context
5
- {
6
- set(key, value, options) {
7
- this.ctx.cookies.set(key, value, {...cfg_cookie, ...options});
8
- }
9
-
10
- get(key) {
11
- if(key === undefined) {
12
- return this.all();
13
- }
14
- return this.ctx.cookies.get(key);
15
- }
16
-
17
- delete(key) {
18
- if(key === undefined) {
19
- return this.clear();
20
- } else {
21
- this.ctx.cookies.set(key, '', {maxAge: 0});
22
- }
23
- }
24
-
25
- all() {
26
- const cookies = {};
27
- this.keys().forEach(key => cookies[key] = this.get(key));
28
- return cookies;
29
- }
30
-
31
- clear() {
32
- this.keys().forEach(key => this.delete(key));
33
- }
34
-
35
- keys() {
36
- const cookie_header = this.ctx.request.headers.cookie || '';
37
- return cookie_header ? cookie_header.split(';').map(value => value.split('=')[0].trim()) : [];
38
- }
39
- }
40
-
1
+ const {cookie: cfg_cookie} = require('./config');
2
+ const Context = require('./context');
3
+
4
+ /**
5
+ * @extends Context
6
+ */
7
+ class Cookie extends Context
8
+ {
9
+ /**
10
+ * 设置cookie
11
+ * @public
12
+ * @param {string} key
13
+ * @param {(string|null)} [value]
14
+ * @param {import('cookies').SetOption} [options]
15
+ * @returns {this}
16
+ */
17
+ set(key, value, options) {
18
+ this.ctx.cookies.set(key, value, {...cfg_cookie, ...options});
19
+ return this;
20
+ }
21
+
22
+ /**
23
+ * 获取cookie
24
+ * @public
25
+ * @param {string} [key] - 为空,则获取所有cookie
26
+ * @param {import('cookies').GetOption} [options]
27
+ * @returns {string}
28
+ */
29
+ get(key, options) {
30
+ if(key === undefined) {
31
+ return this.all();
32
+ }
33
+ return this.ctx.cookies.get(key, options);
34
+ }
35
+
36
+ /**
37
+ * 删除或清理cookie
38
+ * @public
39
+ * @param {*} [key] - 为空则清理所有cookie
40
+ * @returns {this}
41
+ */
42
+ delete(key) {
43
+ if(key === undefined) {
44
+ this.clear();
45
+ } else {
46
+ this.ctx.cookies.set(key, '', {maxAge: 0});
47
+ }
48
+ return this;
49
+ }
50
+
51
+ /**
52
+ * 获取所有cookie
53
+ * @public
54
+ * @returns {object}
55
+ */
56
+ all() {
57
+ const cookies = {};
58
+ this.keys().forEach(key => cookies[key] = this.get(key));
59
+ return cookies;
60
+ }
61
+
62
+ /**
63
+ * 清理所有cookie
64
+ * @public
65
+ * @returns {this}
66
+ */
67
+ clear() {
68
+ this.keys().forEach(key => this.delete(key));
69
+ return this;
70
+ }
71
+
72
+ /**
73
+ * 获取所有cookie key
74
+ * @public
75
+ * @returns {array}
76
+ */
77
+ keys() {
78
+ const cookie_header = this.ctx.request.headers.cookie || '';
79
+ return cookie_header ? cookie_header.split(';').map(value => value.split('=')[0].trim()) : [];
80
+ }
81
+ }
82
+
41
83
  module.exports = Cookie;
package/lib/ctx.js CHANGED
@@ -1,5 +1,14 @@
1
+ // @ts-nocheck
1
2
  const loader = require('./loader');
2
3
 
4
+ /**
5
+ * @typedef {typeof import('./types')} Ctx
6
+ */
7
+
8
+ /**
9
+ * @class Ctx
10
+ * @type {Ctx}
11
+ */
3
12
  const Ctx = new Proxy(class {}, {
4
13
  construct() {
5
14
  return new Proxy({__proto__: arguments[2].prototype}, {