@mx-space/api-client 0.3.4 → 0.4.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.
Files changed (205) hide show
  1. package/build/index.cjs.js +911 -860
  2. package/build/index.cjs.js.map +1 -1
  3. package/build/index.cjs.min.js +1 -1
  4. package/build/index.cjs.min.js.map +1 -1
  5. package/build/index.d.ts +84 -86
  6. package/build/index.esm.js +911 -860
  7. package/build/index.esm.js.map +1 -1
  8. package/build/index.esm.min.js +1 -1
  9. package/build/index.esm.min.js.map +1 -1
  10. package/build/index.umd.js +1179 -1128
  11. package/build/index.umd.js.map +1 -1
  12. package/build/index.umd.min.js +1 -1
  13. package/build/index.umd.min.js.map +1 -1
  14. package/esm/adaptors/axios.d.ts +3 -0
  15. package/esm/adaptors/axios.js +46 -0
  16. package/esm/adaptors/axios.js.map +1 -0
  17. package/esm/adaptors/umi-request.d.ts +3 -0
  18. package/esm/adaptors/umi-request.js +27 -0
  19. package/esm/adaptors/umi-request.js.map +1 -0
  20. package/esm/{core/controllers → controllers}/aggregate.d.ts +5 -5
  21. package/esm/{core/controllers → controllers}/aggregate.js +3 -1
  22. package/esm/controllers/aggregate.js.map +1 -0
  23. package/{lib/core → esm}/controllers/base.d.ts +3 -3
  24. package/esm/{core/controllers → controllers}/base.js +2 -0
  25. package/esm/controllers/base.js.map +1 -0
  26. package/{lib/core → esm}/controllers/category.d.ts +5 -5
  27. package/esm/{core/controllers → controllers}/category.js +5 -3
  28. package/esm/controllers/category.js.map +1 -0
  29. package/esm/controllers/comment.d.ts +40 -0
  30. package/esm/{core/controllers → controllers}/comment.js +2 -0
  31. package/esm/controllers/comment.js.map +1 -0
  32. package/esm/{core/controllers/all.d.ts → controllers/index.d.ts} +0 -0
  33. package/esm/{core/controllers/all.js → controllers/index.js} +1 -1
  34. package/esm/controllers/index.js.map +1 -0
  35. package/{lib/core → esm}/controllers/link.d.ts +2 -2
  36. package/esm/{core/controllers → controllers}/link.js +2 -0
  37. package/esm/controllers/link.js.map +1 -0
  38. package/esm/{core/controllers → controllers}/note.d.ts +11 -14
  39. package/esm/{core/controllers → controllers}/note.js +2 -0
  40. package/esm/controllers/note.js.map +1 -0
  41. package/{lib/core → esm}/controllers/page.d.ts +9 -10
  42. package/esm/{core/controllers → controllers}/page.js +2 -0
  43. package/esm/controllers/page.js.map +1 -0
  44. package/esm/{core/controllers → controllers}/post.d.ts +6 -6
  45. package/esm/{core/controllers → controllers}/post.js +2 -0
  46. package/esm/controllers/post.js.map +1 -0
  47. package/{lib/core → esm}/controllers/project.d.ts +2 -2
  48. package/esm/{core/controllers → controllers}/project.js +2 -0
  49. package/esm/controllers/project.js.map +1 -0
  50. package/{lib/core → esm}/controllers/recently.d.ts +7 -8
  51. package/esm/{core/controllers → controllers}/recently.js +2 -0
  52. package/esm/controllers/recently.js.map +1 -0
  53. package/{lib/core → esm}/controllers/say.d.ts +5 -6
  54. package/esm/{core/controllers → controllers}/say.js +2 -0
  55. package/esm/controllers/say.js.map +1 -0
  56. package/esm/{core/controllers → controllers}/search.d.ts +6 -6
  57. package/esm/{core/controllers → controllers}/search.js +2 -0
  58. package/esm/controllers/search.js.map +1 -0
  59. package/esm/controllers/snippet.d.ts +18 -0
  60. package/esm/{core/controllers → controllers}/snippet.js +2 -0
  61. package/esm/controllers/snippet.js.map +1 -0
  62. package/esm/controllers/user.d.ts +28 -0
  63. package/esm/{core/controllers → controllers}/user.js +2 -0
  64. package/esm/controllers/user.js.map +1 -0
  65. package/esm/core/attachRequest.js +6 -4
  66. package/esm/core/attachRequest.js.map +1 -1
  67. package/esm/core/client.d.ts +5 -5
  68. package/esm/core/client.js +4 -5
  69. package/esm/core/client.js.map +1 -1
  70. package/esm/core/define.d.ts +1 -1
  71. package/esm/core/index.d.ts +0 -1
  72. package/esm/core/index.js +0 -1
  73. package/esm/core/index.js.map +1 -1
  74. package/esm/{core/dtos → dtos}/comment.d.ts +0 -0
  75. package/esm/{core/dtos → dtos}/comment.js +0 -0
  76. package/esm/dtos/comment.js.map +1 -0
  77. package/esm/index.d.ts +2 -2
  78. package/esm/index.js +2 -2
  79. package/esm/index.js.map +1 -1
  80. package/esm/interfaces/instance.d.ts +12 -7
  81. package/esm/utils/auto-bind.d.ts +4 -0
  82. package/esm/utils/auto-bind.js +36 -0
  83. package/esm/utils/auto-bind.js.map +1 -0
  84. package/lib/adaptors/axios.d.ts +3 -0
  85. package/lib/adaptors/axios.js +52 -0
  86. package/lib/adaptors/axios.js.map +1 -0
  87. package/lib/adaptors/umi-request.d.ts +3 -0
  88. package/lib/adaptors/umi-request.js +30 -0
  89. package/lib/adaptors/umi-request.js.map +1 -0
  90. package/lib/{core/controllers → controllers}/aggregate.d.ts +5 -5
  91. package/lib/{core/controllers → controllers}/aggregate.js +3 -1
  92. package/lib/controllers/aggregate.js.map +1 -0
  93. package/{esm/core → lib}/controllers/base.d.ts +3 -3
  94. package/lib/{core/controllers → controllers}/base.js +2 -0
  95. package/lib/controllers/base.js.map +1 -0
  96. package/{esm/core → lib}/controllers/category.d.ts +5 -5
  97. package/lib/{core/controllers → controllers}/category.js +5 -3
  98. package/lib/controllers/category.js.map +1 -0
  99. package/lib/controllers/comment.d.ts +40 -0
  100. package/lib/{core/controllers → controllers}/comment.js +2 -0
  101. package/lib/controllers/comment.js.map +1 -0
  102. package/lib/{core/controllers/all.d.ts → controllers/index.d.ts} +0 -0
  103. package/lib/{core/controllers/all.js → controllers/index.js} +1 -1
  104. package/lib/controllers/index.js.map +1 -0
  105. package/{esm/core → lib}/controllers/link.d.ts +2 -2
  106. package/lib/{core/controllers → controllers}/link.js +2 -0
  107. package/lib/controllers/link.js.map +1 -0
  108. package/lib/{core/controllers → controllers}/note.d.ts +11 -14
  109. package/lib/{core/controllers → controllers}/note.js +2 -0
  110. package/lib/controllers/note.js.map +1 -0
  111. package/{esm/core → lib}/controllers/page.d.ts +9 -10
  112. package/lib/{core/controllers → controllers}/page.js +2 -0
  113. package/lib/controllers/page.js.map +1 -0
  114. package/lib/{core/controllers → controllers}/post.d.ts +6 -6
  115. package/lib/{core/controllers → controllers}/post.js +2 -0
  116. package/lib/controllers/post.js.map +1 -0
  117. package/{esm/core → lib}/controllers/project.d.ts +2 -2
  118. package/lib/{core/controllers → controllers}/project.js +2 -0
  119. package/lib/controllers/project.js.map +1 -0
  120. package/{esm/core → lib}/controllers/recently.d.ts +7 -8
  121. package/lib/{core/controllers → controllers}/recently.js +2 -0
  122. package/lib/controllers/recently.js.map +1 -0
  123. package/{esm/core → lib}/controllers/say.d.ts +5 -6
  124. package/lib/{core/controllers → controllers}/say.js +2 -0
  125. package/lib/controllers/say.js.map +1 -0
  126. package/lib/{core/controllers → controllers}/search.d.ts +6 -6
  127. package/lib/{core/controllers → controllers}/search.js +2 -0
  128. package/lib/controllers/search.js.map +1 -0
  129. package/lib/controllers/snippet.d.ts +18 -0
  130. package/lib/{core/controllers → controllers}/snippet.js +2 -0
  131. package/lib/controllers/snippet.js.map +1 -0
  132. package/lib/controllers/user.d.ts +28 -0
  133. package/lib/{core/controllers → controllers}/user.js +2 -0
  134. package/lib/controllers/user.js.map +1 -0
  135. package/lib/core/attachRequest.js +6 -4
  136. package/lib/core/attachRequest.js.map +1 -1
  137. package/lib/core/client.d.ts +5 -5
  138. package/lib/core/client.js +4 -5
  139. package/lib/core/client.js.map +1 -1
  140. package/lib/core/define.d.ts +1 -1
  141. package/lib/core/index.d.ts +0 -1
  142. package/lib/core/index.js +0 -1
  143. package/lib/core/index.js.map +1 -1
  144. package/lib/{core/dtos → dtos}/comment.d.ts +0 -0
  145. package/lib/{core/dtos → dtos}/comment.js +0 -0
  146. package/lib/dtos/comment.js.map +1 -0
  147. package/lib/index.d.ts +2 -2
  148. package/lib/index.js +2 -4
  149. package/lib/index.js.map +1 -1
  150. package/lib/interfaces/instance.d.ts +12 -7
  151. package/lib/utils/auto-bind.d.ts +4 -0
  152. package/lib/utils/auto-bind.js +40 -0
  153. package/lib/utils/auto-bind.js.map +1 -0
  154. package/package.json +18 -14
  155. package/readme.md +77 -57
  156. package/esm/core/controllers/aggregate.js.map +0 -1
  157. package/esm/core/controllers/all.js.map +0 -1
  158. package/esm/core/controllers/base.js.map +0 -1
  159. package/esm/core/controllers/category.js.map +0 -1
  160. package/esm/core/controllers/comment.d.ts +0 -43
  161. package/esm/core/controllers/comment.js.map +0 -1
  162. package/esm/core/controllers/index.d.ts +0 -1
  163. package/esm/core/controllers/index.js +0 -2
  164. package/esm/core/controllers/index.js.map +0 -1
  165. package/esm/core/controllers/link.js.map +0 -1
  166. package/esm/core/controllers/note.js.map +0 -1
  167. package/esm/core/controllers/page.js.map +0 -1
  168. package/esm/core/controllers/post.js.map +0 -1
  169. package/esm/core/controllers/project.js.map +0 -1
  170. package/esm/core/controllers/recently.js.map +0 -1
  171. package/esm/core/controllers/say.js.map +0 -1
  172. package/esm/core/controllers/search.js.map +0 -1
  173. package/esm/core/controllers/snippet.d.ts +0 -19
  174. package/esm/core/controllers/snippet.js.map +0 -1
  175. package/esm/core/controllers/user.d.ts +0 -29
  176. package/esm/core/controllers/user.js.map +0 -1
  177. package/esm/core/dtos/comment.js.map +0 -1
  178. package/esm/types/helper.d.ts +0 -2
  179. package/esm/types/helper.js +0 -2
  180. package/esm/types/helper.js.map +0 -1
  181. package/lib/core/controllers/aggregate.js.map +0 -1
  182. package/lib/core/controllers/all.js.map +0 -1
  183. package/lib/core/controllers/base.js.map +0 -1
  184. package/lib/core/controllers/category.js.map +0 -1
  185. package/lib/core/controllers/comment.d.ts +0 -43
  186. package/lib/core/controllers/comment.js.map +0 -1
  187. package/lib/core/controllers/index.d.ts +0 -1
  188. package/lib/core/controllers/index.js +0 -14
  189. package/lib/core/controllers/index.js.map +0 -1
  190. package/lib/core/controllers/link.js.map +0 -1
  191. package/lib/core/controllers/note.js.map +0 -1
  192. package/lib/core/controllers/page.js.map +0 -1
  193. package/lib/core/controllers/post.js.map +0 -1
  194. package/lib/core/controllers/project.js.map +0 -1
  195. package/lib/core/controllers/recently.js.map +0 -1
  196. package/lib/core/controllers/say.js.map +0 -1
  197. package/lib/core/controllers/search.js.map +0 -1
  198. package/lib/core/controllers/snippet.d.ts +0 -19
  199. package/lib/core/controllers/snippet.js.map +0 -1
  200. package/lib/core/controllers/user.d.ts +0 -29
  201. package/lib/core/controllers/user.js.map +0 -1
  202. package/lib/core/dtos/comment.js.map +0 -1
  203. package/lib/types/helper.d.ts +0 -2
  204. package/lib/types/helper.js +0 -3
  205. package/lib/types/helper.js.map +0 -1
@@ -1,946 +1,998 @@
1
- var mapObj$1 = {exports: {}};
2
-
3
- const isObject$1 = value => typeof value === 'object' && value !== null;
4
- const mapObjectSkip = Symbol('skip');
5
-
6
- // Customized for this use-case
7
- const isObjectCustom = value =>
8
- isObject$1(value) &&
9
- !(value instanceof RegExp) &&
10
- !(value instanceof Error) &&
11
- !(value instanceof Date);
12
-
13
- const mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
14
- options = {
15
- deep: false,
16
- target: {},
17
- ...options
18
- };
19
-
20
- if (isSeen.has(object)) {
21
- return isSeen.get(object);
22
- }
23
-
24
- isSeen.set(object, options.target);
25
-
26
- const {target} = options;
27
- delete options.target;
28
-
29
- const mapArray = array => array.map(element => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element);
30
- if (Array.isArray(object)) {
31
- return mapArray(object);
32
- }
33
-
34
- for (const [key, value] of Object.entries(object)) {
35
- const mapResult = mapper(key, value, object);
36
-
37
- if (mapResult === mapObjectSkip) {
38
- continue;
39
- }
40
-
41
- let [newKey, newValue, {shouldRecurse = true} = {}] = mapResult;
42
-
43
- // Drop `__proto__` keys.
44
- if (newKey === '__proto__') {
45
- continue;
46
- }
47
-
48
- if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
49
- newValue = Array.isArray(newValue) ?
50
- mapArray(newValue) :
51
- mapObject(newValue, mapper, options, isSeen);
52
- }
53
-
54
- target[newKey] = newValue;
55
- }
56
-
57
- return target;
58
- };
59
-
60
- mapObj$1.exports = (object, mapper, options) => {
61
- if (!isObject$1(object)) {
62
- throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
63
- }
64
-
65
- return mapObject(object, mapper, options);
1
+ const isPlainObject = (obj) => typeof obj === 'object' &&
2
+ Object.prototype.toString.call(obj) === '[object Object]';
3
+ const sortOrderToNumber = (order) => {
4
+ return ({
5
+ asc: 1,
6
+ desc: -1,
7
+ }[order] || 1);
66
8
  };
67
-
68
- mapObj$1.exports.mapObjectSkip = mapObjectSkip;
69
-
70
- var camelcase = {exports: {}};
71
-
72
- const preserveCamelCase = (string, locale) => {
73
- let isLastCharLower = false;
74
- let isLastCharUpper = false;
75
- let isLastLastCharUpper = false;
76
-
77
- for (let i = 0; i < string.length; i++) {
78
- const character = string[i];
79
-
80
- if (isLastCharLower && /[\p{Lu}]/u.test(character)) {
81
- string = string.slice(0, i) + '-' + string.slice(i);
82
- isLastCharLower = false;
83
- isLastLastCharUpper = isLastCharUpper;
84
- isLastCharUpper = true;
85
- i++;
86
- } else if (isLastCharUpper && isLastLastCharUpper && /[\p{Ll}]/u.test(character)) {
87
- string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
88
- isLastLastCharUpper = isLastCharUpper;
89
- isLastCharUpper = false;
90
- isLastCharLower = true;
91
- } else {
92
- isLastCharLower = character.toLocaleLowerCase(locale) === character && character.toLocaleUpperCase(locale) !== character;
93
- isLastLastCharUpper = isLastCharUpper;
94
- isLastCharUpper = character.toLocaleUpperCase(locale) === character && character.toLocaleLowerCase(locale) !== character;
95
- }
96
- }
97
-
98
- return string;
9
+ const destructureData = (payload) => {
10
+ if (typeof payload !== 'object') {
11
+ return payload;
12
+ }
13
+ if (payload === null) {
14
+ return payload;
15
+ }
16
+ const data = payload.data;
17
+ const dataIsPlainObject = Object.prototype.toString.call(data) === '[object Object]';
18
+ if (dataIsPlainObject && Object.keys(payload).length === 1) {
19
+ const d = Object.assign({}, data);
20
+ // attach raw onto new data
21
+ attachRawFromOneToAnthor(payload, d);
22
+ return d;
23
+ }
24
+ return payload;
99
25
  };
100
-
101
- const preserveConsecutiveUppercase = input => {
102
- return input.replace(/^[\p{Lu}](?![\p{Lu}])/gu, m1 => m1.toLowerCase());
26
+ const attachRawFromOneToAnthor = (from, to) => {
27
+ if (!from) {
28
+ return;
29
+ }
30
+ from.$raw &&
31
+ Object.defineProperty(to, '$raw', {
32
+ value: Object.assign({}, from.$raw),
33
+ enumerable: false,
34
+ });
35
+ from.$request &&
36
+ Object.defineProperty(to, '$request', {
37
+ value: Object.assign({}, from.$request),
38
+ enumerable: false,
39
+ });
103
40
  };
104
41
 
105
- const postProcess = (input, options) => {
106
- return input.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase(options.locale))
107
- .replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase(options.locale));
42
+ // @ts-nocheck
43
+ // @copy: https://github.com/sindresorhus/auto-bind/blob/main/index.js
44
+ // Gets all non-builtin properties up the prototype chain.
45
+ const getAllProperties = (object) => {
46
+ const properties = new Set();
47
+ do {
48
+ for (const key of Reflect.ownKeys(object)) {
49
+ properties.add([object, key]);
50
+ }
51
+ } while ((object = Reflect.getPrototypeOf(object)) &&
52
+ object !== Object.prototype);
53
+ return properties;
108
54
  };
55
+ function autoBind(self, { include, exclude } = {}) {
56
+ const filter = (key) => {
57
+ const match = (pattern) => typeof pattern === 'string' ? key === pattern : pattern.test(key);
58
+ if (include) {
59
+ return include.some(match);
60
+ }
61
+ if (exclude) {
62
+ return !exclude.some(match);
63
+ }
64
+ return true;
65
+ };
66
+ for (const [object, key] of getAllProperties(self.constructor.prototype)) {
67
+ if (key === 'constructor' || !filter(key)) {
68
+ continue;
69
+ }
70
+ const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
71
+ if (descriptor && typeof descriptor.value === 'function') {
72
+ self[key] = self[key].bind(self);
73
+ }
74
+ }
75
+ return self;
76
+ }
109
77
 
110
- const camelCase$1 = (input, options) => {
111
- if (!(typeof input === 'string' || Array.isArray(input))) {
112
- throw new TypeError('Expected the input to be `string | string[]`');
113
- }
114
-
115
- options = {
116
- pascalCase: false,
117
- preserveConsecutiveUppercase: false,
118
- ...options
119
- };
120
-
121
- if (Array.isArray(input)) {
122
- input = input.map(x => x.trim())
123
- .filter(x => x.length)
124
- .join('-');
125
- } else {
126
- input = input.trim();
127
- }
128
-
129
- if (input.length === 0) {
130
- return '';
131
- }
132
-
133
- if (input.length === 1) {
134
- return options.pascalCase ? input.toLocaleUpperCase(options.locale) : input.toLocaleLowerCase(options.locale);
135
- }
136
-
137
- const hasUpperCase = input !== input.toLocaleLowerCase(options.locale);
138
-
139
- if (hasUpperCase) {
140
- input = preserveCamelCase(input, options.locale);
141
- }
142
-
143
- input = input.replace(/^[_.\- ]+/, '');
144
-
145
- if (options.preserveConsecutiveUppercase) {
146
- input = preserveConsecutiveUppercase(input);
147
- } else {
148
- input = input.toLocaleLowerCase();
149
- }
150
-
151
- if (options.pascalCase) {
152
- input = input.charAt(0).toLocaleUpperCase(options.locale) + input.slice(1);
153
- }
154
-
155
- return postProcess(input, options);
156
- };
78
+ class AggregateController {
79
+ constructor(client) {
80
+ this.client = client;
81
+ this.base = 'aggregate';
82
+ this.name = 'aggregate';
83
+ autoBind(this);
84
+ }
85
+ get proxy() {
86
+ return this.client.proxy(this.base);
87
+ }
88
+ /**
89
+ * 获取聚合数据
90
+ */
91
+ getAggregateData() {
92
+ return this.proxy.get();
93
+ }
94
+ /**
95
+ * 获取最新发布的内容
96
+ */
97
+ getTop(size = 5) {
98
+ return this.proxy.top.get({ params: { size } });
99
+ }
100
+ getTimeline(options) {
101
+ const { sort, type, year } = options || {};
102
+ return this.proxy.timeline.get({
103
+ params: {
104
+ sort: sort && sortOrderToNumber(sort),
105
+ type,
106
+ year,
107
+ },
108
+ });
109
+ }
110
+ /**
111
+ * 获取聚合数据统计
112
+ */
113
+ getStat() {
114
+ return this.proxy.stat.get();
115
+ }
116
+ }
157
117
 
158
- camelcase.exports = camelCase$1;
159
- // TODO: Remove this for the next major release
160
- camelcase.exports.default = camelCase$1;
118
+ class RequestError extends Error {
119
+ constructor(message, status, path, raw) {
120
+ super(message);
121
+ this.status = status;
122
+ this.path = path;
123
+ this.raw = raw;
124
+ }
125
+ }
161
126
 
162
- class QuickLRU {
163
- constructor(options = {}) {
164
- if (!(options.maxSize && options.maxSize > 0)) {
165
- throw new TypeError('`maxSize` must be a number greater than 0');
166
- }
127
+ var CategoryType;
128
+ (function (CategoryType) {
129
+ CategoryType[CategoryType["Category"] = 0] = "Category";
130
+ CategoryType[CategoryType["Tag"] = 1] = "Tag";
131
+ })(CategoryType || (CategoryType = {}));
167
132
 
168
- this.maxSize = options.maxSize;
169
- this.onEviction = options.onEviction;
170
- this.cache = new Map();
171
- this.oldCache = new Map();
172
- this._size = 0;
173
- }
133
+ class CategoryController {
134
+ constructor(client) {
135
+ this.client = client;
136
+ this.name = 'category';
137
+ this.base = 'categories';
138
+ autoBind(this);
139
+ }
140
+ get proxy() {
141
+ return this.client.proxy(this.base);
142
+ }
143
+ getAllCategories() {
144
+ return this.proxy.get({
145
+ params: {
146
+ type: CategoryType.Category,
147
+ },
148
+ });
149
+ }
150
+ getAllTags() {
151
+ return this.proxy.get({
152
+ params: {
153
+ type: CategoryType.Tag,
154
+ },
155
+ });
156
+ }
157
+ async getCategoryDetail(ids) {
158
+ if (typeof ids === 'string') {
159
+ const data = await this.proxy.get({
160
+ params: {
161
+ ids: ids,
162
+ },
163
+ });
164
+ const result = Object.values(data.entries)[0];
165
+ attachRawFromOneToAnthor(data, result);
166
+ return result;
167
+ }
168
+ else if (Array.isArray(ids)) {
169
+ const data = await this.proxy.get({
170
+ params: {
171
+ ids: ids.join(','),
172
+ },
173
+ });
174
+ const entries = data === null || data === void 0 ? void 0 : data.entries;
175
+ if (!entries) {
176
+ throw new RequestError('data structure error', 500, data.$request.path, data);
177
+ }
178
+ const map = new Map(Object.entries(entries).map(([id, value]) => [id.toLowerCase(), value]));
179
+ attachRawFromOneToAnthor(data, map);
180
+ return map;
181
+ }
182
+ }
183
+ async getCategoryByIdOrSlug(idOrSlug) {
184
+ const res = await this.proxy(idOrSlug).get();
185
+ return destructureData(res);
186
+ }
187
+ async getTagByName(name) {
188
+ const res = await this.proxy(name).get({
189
+ params: {
190
+ tag: 1,
191
+ },
192
+ });
193
+ return res;
194
+ }
195
+ }
174
196
 
175
- _set(key, value) {
176
- this.cache.set(key, value);
177
- this._size++;
197
+ class CommentController {
198
+ constructor(client) {
199
+ this.client = client;
200
+ this.base = 'comments';
201
+ this.name = 'comment';
202
+ autoBind(this);
203
+ }
204
+ get proxy() {
205
+ return this.client.proxy(this.base);
206
+ }
207
+ /**
208
+ * 根据 comment id 获取评论, 包括子评论
209
+ */
210
+ getById(id) {
211
+ return this.proxy(id).get();
212
+ }
213
+ /**
214
+ * 获取文章的评论列表
215
+ * @param refId 文章 Id
216
+ */
217
+ getByRefId(refId, pagination = {}) {
218
+ const { page, size } = pagination;
219
+ return this.proxy.ref(refId).get({
220
+ params: { page: page || 1, size: size || 10 },
221
+ });
222
+ }
223
+ /**
224
+ * 评论
225
+ */
226
+ comment(refId, data) {
227
+ return this.proxy(refId).post({
228
+ data,
229
+ });
230
+ }
231
+ /**
232
+ * 回复评论
233
+ */
234
+ reply(commentId, data) {
235
+ return this.proxy.reply(commentId).post({
236
+ data,
237
+ });
238
+ }
239
+ }
240
+
241
+ class BaseCrudController {
242
+ constructor(client) {
243
+ this.client = client;
244
+ autoBind(this);
245
+ }
246
+ get proxy() {
247
+ return this.client.proxy(this.base);
248
+ }
249
+ getById(id) {
250
+ return this.proxy(id).get();
251
+ }
252
+ getAll() {
253
+ return this.proxy.all.get();
254
+ }
255
+ /**
256
+ * 带分页的查询
257
+ * @param page
258
+ * @param perPage
259
+ */
260
+ getAllPaginated(page, perPage) {
261
+ return this.proxy.get({
262
+ params: { page, size: perPage },
263
+ });
264
+ }
265
+ }
266
+
267
+ class LinkController extends BaseCrudController {
268
+ constructor(client) {
269
+ super(client);
270
+ this.client = client;
271
+ this.name = 'link';
272
+ this.base = 'links';
273
+ autoBind(this);
274
+ }
275
+ }
276
+
277
+ class NoteController {
278
+ constructor(client) {
279
+ this.client = client;
280
+ this.base = 'notes';
281
+ this.name = 'note';
282
+ autoBind(this);
283
+ }
284
+ get proxy() {
285
+ return this.client.proxy(this.base);
286
+ }
287
+ /**
288
+ * 最新日记
289
+ */
290
+ getLatest() {
291
+ return this.proxy.latest.get();
292
+ }
293
+ /**
294
+ * 获取一篇日记
295
+ * @param id id | nid
296
+ * @param password 访问密码
297
+ */
298
+ getNoteById(id, password) {
299
+ if (typeof id === 'number') {
300
+ return this.proxy.nid(id.toString()).get({
301
+ params: { password },
302
+ });
303
+ }
304
+ else {
305
+ return this.proxy(id).get({ params: { password } });
306
+ }
307
+ }
308
+ /**
309
+ * 日记列表分页
310
+ */
311
+ getList(page = 1, perPage = 10, options = {}) {
312
+ const { select, sortBy, sortOrder, year } = options;
313
+ return this.proxy.get({
314
+ params: {
315
+ page,
316
+ size: perPage,
317
+ select: select === null || select === void 0 ? void 0 : select.join(' '),
318
+ sortBy,
319
+ sortOrder,
320
+ year,
321
+ },
322
+ });
323
+ }
324
+ /**
325
+ * 获取当前日记的上下各 n / 2 篇日记
326
+ */
327
+ getMiddleList(id, size = 5) {
328
+ return this.proxy.list(id).get({
329
+ params: { size },
330
+ });
331
+ }
332
+ /**
333
+ * 喜欢这篇日记
334
+ */
335
+ likeIt(id) {
336
+ return this.proxy.like(id).get();
337
+ }
338
+ }
339
+
340
+ class PageController {
341
+ constructor(client) {
342
+ this.client = client;
343
+ this.base = 'pages';
344
+ this.name = 'page';
345
+ autoBind(this);
346
+ }
347
+ get proxy() {
348
+ return this.client.proxy(this.base);
349
+ }
350
+ /**
351
+ * 页面列表
352
+ */
353
+ getList(page = 1, perPage = 10, options = {}) {
354
+ const { select, sortBy, sortOrder } = options;
355
+ return this.proxy.get({
356
+ params: {
357
+ page,
358
+ size: perPage,
359
+ select: select === null || select === void 0 ? void 0 : select.join(' '),
360
+ sortBy,
361
+ sortOrder,
362
+ },
363
+ });
364
+ }
365
+ /**
366
+ * 页面详情
367
+ */
368
+ getById(id) {
369
+ return this.proxy(id).get();
370
+ }
371
+ /**
372
+ * 根据路径获取页面
373
+ * @param slug 路径
374
+ * @returns
375
+ */
376
+ getBySlug(slug) {
377
+ return this.proxy.slug(slug).get({});
378
+ }
379
+ }
380
+
381
+ class PostController {
382
+ constructor(client) {
383
+ this.client = client;
384
+ this.base = 'posts';
385
+ this.name = 'post';
386
+ autoBind(this);
387
+ }
388
+ get proxy() {
389
+ return this.client.proxy(this.base);
390
+ }
391
+ /**
392
+ * 获取文章列表分页
393
+ * @param page
394
+ * @param perPage
395
+ * @returns
396
+ */
397
+ getList(page = 1, perPage = 10, options = {}) {
398
+ const { select, sortBy, sortOrder, year } = options;
399
+ return this.proxy.get({
400
+ params: {
401
+ page,
402
+ size: perPage,
403
+ select: select === null || select === void 0 ? void 0 : select.join(' '),
404
+ sortBy,
405
+ sortOrder,
406
+ year,
407
+ },
408
+ });
409
+ }
410
+ getPost(idOrCategoryName, slug) {
411
+ if (arguments.length == 1) {
412
+ return this.proxy(idOrCategoryName).get();
413
+ }
414
+ else {
415
+ return this.proxy(idOrCategoryName)(slug).get();
416
+ }
417
+ }
418
+ /**
419
+ * 获取最新的文章
420
+ */
421
+ getLatest() {
422
+ return this.proxy.latest.get();
423
+ }
424
+ /**
425
+ * 点赞
426
+ */
427
+ thumbsUp(id) {
428
+ return this.proxy('_thumbs-up').get({ params: { id } });
429
+ }
430
+ }
431
+
432
+ class ProjectController extends BaseCrudController {
433
+ constructor(client) {
434
+ super(client);
435
+ this.client = client;
436
+ this.base = 'projects';
437
+ this.name = 'project';
438
+ autoBind(this);
439
+ }
440
+ }
441
+
442
+ class RecentlyController {
443
+ constructor(client) {
444
+ this.client = client;
445
+ this.base = 'recently';
446
+ this.name = 'recently';
447
+ autoBind(this);
448
+ }
449
+ get proxy() {
450
+ return this.client.proxy(this.base);
451
+ }
452
+ /**
453
+ * 获取最新一条
454
+ */
455
+ getLatestOne() {
456
+ return this.proxy.latest.get();
457
+ }
458
+ getAll() {
459
+ return this.proxy.all.get();
460
+ }
461
+ getList(before, after, size) {
462
+ if (!before && !after) {
463
+ throw new Error('you can only choose `before` or `after`');
464
+ }
465
+ return this.proxy.get({
466
+ params: {
467
+ before,
468
+ after,
469
+ size,
470
+ },
471
+ });
472
+ }
473
+ }
474
+
475
+ class SayController extends BaseCrudController {
476
+ constructor(client) {
477
+ super(client);
478
+ this.client = client;
479
+ this.base = 'says';
480
+ this.name = 'say';
481
+ autoBind(this);
482
+ }
483
+ get proxy() {
484
+ return this.client.proxy(this.base);
485
+ }
486
+ /**
487
+ * 获取随机一条
488
+ */
489
+ getRandom() {
490
+ return this.proxy.random.get();
491
+ }
492
+ }
493
+
494
+ class SearchController {
495
+ constructor(client) {
496
+ this.client = client;
497
+ this.base = 'search';
498
+ this.name = 'search';
499
+ autoBind(this);
500
+ }
501
+ get proxy() {
502
+ return this.client.proxy(this.base);
503
+ }
504
+ search(type, keyword, options = {}) {
505
+ return this.proxy(type).get({
506
+ params: Object.assign({ keyword }, options),
507
+ });
508
+ }
509
+ /**
510
+ * 从 algolya 搜索
511
+ * https://www.algolia.com/doc/api-reference/api-methods/search/
512
+ * @param keyword
513
+ * @param options
514
+ * @returns
515
+ */
516
+ searchByAlgolia(keyword, options) {
517
+ return this.proxy('algolia').get({ params: Object.assign({ keyword }, options) });
518
+ }
519
+ }
520
+
521
+ class SnippetController {
522
+ constructor(client) {
523
+ this.client = client;
524
+ this.base = 'snippets';
525
+ this.name = 'snippet';
526
+ autoBind(this);
527
+ }
528
+ get proxy() {
529
+ return this.client.proxy(this.base);
530
+ }
531
+ getById(id) {
532
+ return this.proxy(id).get();
533
+ }
534
+ getByReferenceAndName(reference, name) {
535
+ return this.proxy(reference)(name).get();
536
+ }
537
+ }
538
+
539
+ class UserController {
540
+ constructor(client) {
541
+ this.client = client;
542
+ this.base = 'master';
543
+ this.name = 'user';
544
+ autoBind(this);
545
+ }
546
+ get proxy() {
547
+ return this.client.proxy(this.base);
548
+ }
549
+ getMasterInfo() {
550
+ return this.proxy.get();
551
+ }
552
+ login(username, password) {
553
+ return this.proxy.login.post({
554
+ data: {
555
+ username,
556
+ password,
557
+ },
558
+ });
559
+ }
560
+ checkTokenValid(token) {
561
+ return this.proxy.check_logged.get({
562
+ params: {
563
+ token,
564
+ },
565
+ });
566
+ }
567
+ }
568
+
569
+ const allControllers = [
570
+ AggregateController,
571
+ CategoryController,
572
+ CommentController,
573
+ LinkController,
574
+ NoteController,
575
+ PageController,
576
+ PostController,
577
+ ProjectController,
578
+ RecentlyController,
579
+ SayController,
580
+ SearchController,
581
+ SnippetController,
582
+ UserController,
583
+ ];
584
+ const allContollerNames = [
585
+ 'aggregate',
586
+ 'category',
587
+ 'comment',
588
+ 'link',
589
+ 'note',
590
+ 'page',
591
+ 'post',
592
+ 'project',
593
+ 'recently',
594
+ 'say',
595
+ 'search',
596
+ 'snippet',
597
+ 'user',
598
+ ];
178
599
 
179
- if (this._size >= this.maxSize) {
180
- this._size = 0;
600
+ var mapObj$1 = {exports: {}};
181
601
 
182
- if (typeof this.onEviction === 'function') {
183
- for (const [key, value] of this.oldCache.entries()) {
184
- this.onEviction(key, value);
185
- }
186
- }
602
+ const isObject$1 = value => typeof value === 'object' && value !== null;
603
+ const mapObjectSkip = Symbol('skip');
187
604
 
188
- this.oldCache = this.cache;
189
- this.cache = new Map();
190
- }
191
- }
605
+ // Customized for this use-case
606
+ const isObjectCustom = value =>
607
+ isObject$1(value) &&
608
+ !(value instanceof RegExp) &&
609
+ !(value instanceof Error) &&
610
+ !(value instanceof Date);
192
611
 
193
- get(key) {
194
- if (this.cache.has(key)) {
195
- return this.cache.get(key);
196
- }
612
+ const mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
613
+ options = {
614
+ deep: false,
615
+ target: {},
616
+ ...options
617
+ };
197
618
 
198
- if (this.oldCache.has(key)) {
199
- const value = this.oldCache.get(key);
200
- this.oldCache.delete(key);
201
- this._set(key, value);
202
- return value;
203
- }
619
+ if (isSeen.has(object)) {
620
+ return isSeen.get(object);
204
621
  }
205
622
 
206
- set(key, value) {
207
- if (this.cache.has(key)) {
208
- this.cache.set(key, value);
209
- } else {
210
- this._set(key, value);
211
- }
623
+ isSeen.set(object, options.target);
212
624
 
213
- return this;
214
- }
625
+ const {target} = options;
626
+ delete options.target;
215
627
 
216
- has(key) {
217
- return this.cache.has(key) || this.oldCache.has(key);
628
+ const mapArray = array => array.map(element => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element);
629
+ if (Array.isArray(object)) {
630
+ return mapArray(object);
218
631
  }
219
632
 
220
- peek(key) {
221
- if (this.cache.has(key)) {
222
- return this.cache.get(key);
223
- }
224
-
225
- if (this.oldCache.has(key)) {
226
- return this.oldCache.get(key);
227
- }
228
- }
633
+ for (const [key, value] of Object.entries(object)) {
634
+ const mapResult = mapper(key, value, object);
229
635
 
230
- delete(key) {
231
- const deleted = this.cache.delete(key);
232
- if (deleted) {
233
- this._size--;
636
+ if (mapResult === mapObjectSkip) {
637
+ continue;
234
638
  }
235
639
 
236
- return this.oldCache.delete(key) || deleted;
237
- }
238
-
239
- clear() {
240
- this.cache.clear();
241
- this.oldCache.clear();
242
- this._size = 0;
243
- }
640
+ let [newKey, newValue, {shouldRecurse = true} = {}] = mapResult;
244
641
 
245
- * keys() {
246
- for (const [key] of this) {
247
- yield key;
642
+ // Drop `__proto__` keys.
643
+ if (newKey === '__proto__') {
644
+ continue;
248
645
  }
249
- }
250
646
 
251
- * values() {
252
- for (const [, value] of this) {
253
- yield value;
647
+ if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
648
+ newValue = Array.isArray(newValue) ?
649
+ mapArray(newValue) :
650
+ mapObject(newValue, mapper, options, isSeen);
254
651
  }
652
+
653
+ target[newKey] = newValue;
255
654
  }
256
655
 
257
- * [Symbol.iterator]() {
258
- for (const item of this.cache) {
259
- yield item;
260
- }
656
+ return target;
657
+ };
261
658
 
262
- for (const item of this.oldCache) {
263
- const [key] = item;
264
- if (!this.cache.has(key)) {
265
- yield item;
266
- }
267
- }
659
+ mapObj$1.exports = (object, mapper, options) => {
660
+ if (!isObject$1(object)) {
661
+ throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
268
662
  }
269
663
 
270
- get size() {
271
- let oldCacheSize = 0;
272
- for (const key of this.oldCache.keys()) {
273
- if (!this.cache.has(key)) {
274
- oldCacheSize++;
275
- }
276
- }
664
+ return mapObject(object, mapper, options);
665
+ };
277
666
 
278
- return Math.min(this._size + oldCacheSize, this.maxSize);
279
- }
280
- }
667
+ mapObj$1.exports.mapObjectSkip = mapObjectSkip;
281
668
 
282
- var quickLru = QuickLRU;
669
+ var camelcase = {exports: {}};
283
670
 
284
- const mapObj = mapObj$1.exports;
285
- const camelCase = camelcase.exports;
286
- const QuickLru = quickLru;
671
+ const preserveCamelCase = (string, locale) => {
672
+ let isLastCharLower = false;
673
+ let isLastCharUpper = false;
674
+ let isLastLastCharUpper = false;
287
675
 
288
- const has = (array, key) => array.some(x => {
289
- if (typeof x === 'string') {
290
- return x === key;
676
+ for (let i = 0; i < string.length; i++) {
677
+ const character = string[i];
678
+
679
+ if (isLastCharLower && /[\p{Lu}]/u.test(character)) {
680
+ string = string.slice(0, i) + '-' + string.slice(i);
681
+ isLastCharLower = false;
682
+ isLastLastCharUpper = isLastCharUpper;
683
+ isLastCharUpper = true;
684
+ i++;
685
+ } else if (isLastCharUpper && isLastLastCharUpper && /[\p{Ll}]/u.test(character)) {
686
+ string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
687
+ isLastLastCharUpper = isLastCharUpper;
688
+ isLastCharUpper = false;
689
+ isLastCharLower = true;
690
+ } else {
691
+ isLastCharLower = character.toLocaleLowerCase(locale) === character && character.toLocaleUpperCase(locale) !== character;
692
+ isLastLastCharUpper = isLastCharUpper;
693
+ isLastCharUpper = character.toLocaleUpperCase(locale) === character && character.toLocaleLowerCase(locale) !== character;
694
+ }
291
695
  }
292
696
 
293
- x.lastIndex = 0;
294
- return x.test(key);
295
- });
697
+ return string;
698
+ };
296
699
 
297
- const cache = new QuickLru({maxSize: 100000});
700
+ const preserveConsecutiveUppercase = input => {
701
+ return input.replace(/^[\p{Lu}](?![\p{Lu}])/gu, m1 => m1.toLowerCase());
702
+ };
298
703
 
299
- // Reproduces behavior from `map-obj`
300
- const isObject = value =>
301
- typeof value === 'object' &&
302
- value !== null &&
303
- !(value instanceof RegExp) &&
304
- !(value instanceof Error) &&
305
- !(value instanceof Date);
704
+ const postProcess = (input, options) => {
705
+ return input.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase(options.locale))
706
+ .replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase(options.locale));
707
+ };
306
708
 
307
- const camelCaseConvert = (input, options) => {
308
- if (!isObject(input)) {
309
- return input;
709
+ const camelCase$1 = (input, options) => {
710
+ if (!(typeof input === 'string' || Array.isArray(input))) {
711
+ throw new TypeError('Expected the input to be `string | string[]`');
310
712
  }
311
713
 
312
714
  options = {
313
- deep: false,
314
715
  pascalCase: false,
716
+ preserveConsecutiveUppercase: false,
315
717
  ...options
316
718
  };
317
719
 
318
- const {exclude, pascalCase, stopPaths, deep} = options;
720
+ if (Array.isArray(input)) {
721
+ input = input.map(x => x.trim())
722
+ .filter(x => x.length)
723
+ .join('-');
724
+ } else {
725
+ input = input.trim();
726
+ }
319
727
 
320
- const stopPathsSet = new Set(stopPaths);
728
+ if (input.length === 0) {
729
+ return '';
730
+ }
321
731
 
322
- const makeMapper = parentPath => (key, value) => {
323
- if (deep && isObject(value)) {
324
- const path = parentPath === undefined ? key : `${parentPath}.${key}`;
732
+ if (input.length === 1) {
733
+ return options.pascalCase ? input.toLocaleUpperCase(options.locale) : input.toLocaleLowerCase(options.locale);
734
+ }
325
735
 
326
- if (!stopPathsSet.has(path)) {
327
- value = mapObj(value, makeMapper(path));
328
- }
736
+ const hasUpperCase = input !== input.toLocaleLowerCase(options.locale);
737
+
738
+ if (hasUpperCase) {
739
+ input = preserveCamelCase(input, options.locale);
740
+ }
741
+
742
+ input = input.replace(/^[_.\- ]+/, '');
743
+
744
+ if (options.preserveConsecutiveUppercase) {
745
+ input = preserveConsecutiveUppercase(input);
746
+ } else {
747
+ input = input.toLocaleLowerCase();
748
+ }
749
+
750
+ if (options.pascalCase) {
751
+ input = input.charAt(0).toLocaleUpperCase(options.locale) + input.slice(1);
752
+ }
753
+
754
+ return postProcess(input, options);
755
+ };
756
+
757
+ camelcase.exports = camelCase$1;
758
+ // TODO: Remove this for the next major release
759
+ camelcase.exports.default = camelCase$1;
760
+
761
+ class QuickLRU {
762
+ constructor(options = {}) {
763
+ if (!(options.maxSize && options.maxSize > 0)) {
764
+ throw new TypeError('`maxSize` must be a number greater than 0');
329
765
  }
330
766
 
331
- if (!(exclude && has(exclude, key))) {
332
- const cacheKey = pascalCase ? `${key}_` : key;
767
+ this.maxSize = options.maxSize;
768
+ this.onEviction = options.onEviction;
769
+ this.cache = new Map();
770
+ this.oldCache = new Map();
771
+ this._size = 0;
772
+ }
773
+
774
+ _set(key, value) {
775
+ this.cache.set(key, value);
776
+ this._size++;
333
777
 
334
- if (cache.has(cacheKey)) {
335
- key = cache.get(cacheKey);
336
- } else {
337
- const returnValue = camelCase(key, {pascalCase});
778
+ if (this._size >= this.maxSize) {
779
+ this._size = 0;
338
780
 
339
- if (key.length < 100) { // Prevent abuse
340
- cache.set(cacheKey, returnValue);
781
+ if (typeof this.onEviction === 'function') {
782
+ for (const [key, value] of this.oldCache.entries()) {
783
+ this.onEviction(key, value);
341
784
  }
342
-
343
- key = returnValue;
344
785
  }
345
- }
346
786
 
347
- return [key, value];
348
- };
787
+ this.oldCache = this.cache;
788
+ this.cache = new Map();
789
+ }
790
+ }
349
791
 
350
- return mapObj(input, makeMapper(undefined));
351
- };
792
+ get(key) {
793
+ if (this.cache.has(key)) {
794
+ return this.cache.get(key);
795
+ }
352
796
 
353
- var camelcaseKeys = (input, options) => {
354
- if (Array.isArray(input)) {
355
- return Object.keys(input).map(key => camelCaseConvert(input[key], options));
797
+ if (this.oldCache.has(key)) {
798
+ const value = this.oldCache.get(key);
799
+ this.oldCache.delete(key);
800
+ this._set(key, value);
801
+ return value;
802
+ }
356
803
  }
357
804
 
358
- return camelCaseConvert(input, options);
359
- };
360
-
361
- const isPlainObject = (obj) => typeof obj === 'object' &&
362
- Object.prototype.toString.call(obj) === '[object Object]';
363
- const sortOrderToNumber = (order) => {
364
- return ({
365
- asc: 1,
366
- desc: -1,
367
- }[order] || 1);
368
- };
369
- const destructureData = (payload) => {
370
- if (typeof payload !== 'object') {
371
- return payload;
372
- }
373
- if (payload === null) {
374
- return payload;
375
- }
376
- const data = payload.data;
377
- const dataIsPlainObject = Object.prototype.toString.call(data) === '[object Object]';
378
- if (dataIsPlainObject && Object.keys(payload).length === 1) {
379
- const d = Object.assign({}, data);
380
- // attach raw onto new data
381
- attachRawFromOneToAnthor(payload, d);
382
- return d;
383
- }
384
- return payload;
385
- };
386
- const attachRawFromOneToAnthor = (from, to) => {
387
- if (!from) {
388
- return;
389
- }
390
- from.$raw &&
391
- Object.defineProperty(to, '$raw', {
392
- value: Object.assign({}, from.$raw),
393
- enumerable: false,
394
- });
395
- from.$request &&
396
- Object.defineProperty(to, '$request', {
397
- value: Object.assign({}, from.$request),
398
- enumerable: false,
399
- });
400
- };
805
+ set(key, value) {
806
+ if (this.cache.has(key)) {
807
+ this.cache.set(key, value);
808
+ } else {
809
+ this._set(key, value);
810
+ }
401
811
 
402
- function attachRequestMethod(target) {
403
- Object.defineProperty(target, '$$get', {
404
- value: function (url, options) {
405
- // HINT: method get only accept search params;
406
- const { params = {} } = options;
407
- const qs = handleSearchParams(params);
408
- return target.instance.get(url + `${qs ? `${'?' + qs}` : ''}`);
409
- },
410
- });
411
- Object.defineProperty(target, '$$post', {
412
- value: function (path, options) {
413
- return target.instance.post(path, options);
414
- },
415
- });
416
- }
417
- // FIXME: only support string value
418
- function handleSearchParams(obj) {
419
- if (!obj && typeof obj !== 'object') {
420
- throw new TypeError('params must be object.');
421
- }
422
- if (obj instanceof URLSearchParams) {
423
- return obj.toString();
424
- }
425
- const search = new URLSearchParams();
426
- Object.entries(obj).forEach(([k, v]) => {
427
- if (typeof v === 'undefined' ||
428
- Object.prototype.toString.call(v) === '[object Null]') {
429
- return;
430
- }
431
- search.set(k, v);
432
- });
433
- return search.toString();
434
- }
812
+ return this;
813
+ }
435
814
 
436
- class AggregateController {
437
- constructor(client) {
438
- this.client = client;
439
- this.base = 'aggregate';
440
- this.name = 'aggregate';
441
- }
442
- get proxy() {
443
- return this.client.proxy(this.base);
444
- }
445
- /**
446
- * 获取聚合数据
447
- */
448
- getAggregateData() {
449
- return this.proxy.get();
450
- }
451
- /**
452
- * 获取最新发布的内容
453
- */
454
- getTop(size = 5) {
455
- return this.proxy.top.get({ params: { size } });
456
- }
457
- getTimeline(options) {
458
- const { sort, type, year } = options || {};
459
- return this.proxy.timeline.get({
460
- params: {
461
- sort: sort && sortOrderToNumber(sort),
462
- type,
463
- year,
464
- },
465
- });
466
- }
467
- /**
468
- * 获取聚合数据统计
469
- */
470
- getStat() {
471
- return this.proxy.stat.get();
472
- }
473
- }
815
+ has(key) {
816
+ return this.cache.has(key) || this.oldCache.has(key);
817
+ }
474
818
 
475
- var CategoryType;
476
- (function (CategoryType) {
477
- CategoryType[CategoryType["Category"] = 0] = "Category";
478
- CategoryType[CategoryType["Tag"] = 1] = "Tag";
479
- })(CategoryType || (CategoryType = {}));
819
+ peek(key) {
820
+ if (this.cache.has(key)) {
821
+ return this.cache.get(key);
822
+ }
480
823
 
481
- class RequestError extends Error {
482
- constructor(message, status, path, raw) {
483
- super(message);
484
- this.status = status;
485
- this.path = path;
486
- this.raw = raw;
487
- }
488
- }
824
+ if (this.oldCache.has(key)) {
825
+ return this.oldCache.get(key);
826
+ }
827
+ }
489
828
 
490
- class CategoryController {
491
- constructor(client) {
492
- this.client = client;
493
- this.name = 'category';
494
- this.base = 'categories';
495
- }
496
- get proxy() {
497
- return this.client.proxy(this.base);
498
- }
499
- getAllCategories() {
500
- return this.proxy.get({
501
- params: {
502
- type: CategoryType.Category,
503
- },
504
- });
505
- }
506
- getAllTags() {
507
- return this.proxy.get({
508
- params: {
509
- type: CategoryType.Tag,
510
- },
511
- });
512
- }
513
- async getCategoryDetail(ids) {
514
- if (typeof ids === 'string') {
515
- const data = await this.proxy.get({
516
- params: {
517
- ids: ids,
518
- },
519
- });
520
- const result = Object.values(data.entries)[0];
521
- attachRawFromOneToAnthor(data, result);
522
- return result;
523
- }
524
- else if (Array.isArray(ids)) {
525
- const data = await this.proxy.get({
526
- params: {
527
- ids: ids.join(','),
528
- },
529
- });
530
- const entries = data === null || data === void 0 ? void 0 : data.entries;
531
- if (!entries) {
532
- throw new RequestError('data structure error', 500, data.$request.path, data);
533
- }
534
- const map = new Map(Object.entries(entries).map(([id, value]) => [id.toLowerCase(), value]));
535
- attachRawFromOneToAnthor(data, map);
536
- return map;
537
- }
538
- }
539
- async getCategoryByIdOrSlug(idOrSlug) {
540
- const res = await this.proxy(idOrSlug).get();
541
- return destructureData(res);
542
- }
543
- async getTagByName(name) {
544
- const res = await this.proxy(name).get({
545
- params: {
546
- tag: 1,
547
- },
548
- });
549
- return res;
550
- }
551
- }
829
+ delete(key) {
830
+ const deleted = this.cache.delete(key);
831
+ if (deleted) {
832
+ this._size--;
833
+ }
552
834
 
553
- class CommentController {
554
- constructor(client) {
555
- this.client = client;
556
- this.base = 'comments';
557
- this.name = 'comment';
558
- }
559
- get proxy() {
560
- return this.client.proxy(this.base);
561
- }
562
- /**
563
- * 根据 comment id 获取评论, 包括子评论
564
- */
565
- getById(id) {
566
- return this.proxy(id).get();
567
- }
568
- /**
569
- * 获取文章的评论列表
570
- * @param refId 文章 Id
571
- */
572
- getByRefId(refId, pagination = {}) {
573
- const { page, size } = pagination;
574
- return this.proxy.ref(refId).get({
575
- params: { page: page || 1, size: size || 10 },
576
- });
577
- }
578
- /**
579
- * 评论
580
- */
581
- comment(refId, data) {
582
- return this.proxy(refId).post({
583
- data,
584
- });
585
- }
586
- /**
587
- * 回复评论
588
- */
589
- reply(commentId, data) {
590
- return this.proxy.reply(commentId).post({
591
- data,
592
- });
593
- }
594
- }
835
+ return this.oldCache.delete(key) || deleted;
836
+ }
595
837
 
596
- class BaseCrudController {
597
- constructor(client) {
598
- this.client = client;
599
- }
600
- get proxy() {
601
- return this.client.proxy(this.base);
602
- }
603
- getById(id) {
604
- return this.proxy(id).get();
605
- }
606
- getAll() {
607
- return this.proxy.all.get();
608
- }
609
- /**
610
- * 带分页的查询
611
- * @param page
612
- * @param perPage
613
- */
614
- getAllPaginated(page, perPage) {
615
- return this.proxy.get({
616
- params: { page, size: perPage },
617
- });
618
- }
619
- }
838
+ clear() {
839
+ this.cache.clear();
840
+ this.oldCache.clear();
841
+ this._size = 0;
842
+ }
620
843
 
621
- class LinkController extends BaseCrudController {
622
- constructor(client) {
623
- super(client);
624
- this.client = client;
625
- this.name = 'link';
626
- this.base = 'links';
627
- }
628
- }
844
+ * keys() {
845
+ for (const [key] of this) {
846
+ yield key;
847
+ }
848
+ }
629
849
 
630
- class NoteController {
631
- constructor(client) {
632
- this.client = client;
633
- this.base = 'notes';
634
- this.name = 'note';
635
- }
636
- get proxy() {
637
- return this.client.proxy(this.base);
638
- }
639
- /**
640
- * 最新日记
641
- */
642
- getLatest() {
643
- return this.proxy.latest.get();
644
- }
645
- /**
646
- * 获取一篇日记
647
- * @param id id | nid
648
- * @param password 访问密码
649
- */
650
- getNoteById(id, password) {
651
- if (typeof id === 'number') {
652
- return this.proxy.nid(id.toString()).get({
653
- params: { password },
654
- });
655
- }
656
- else {
657
- return this.proxy(id).get({ params: { password } });
658
- }
659
- }
660
- /**
661
- * 日记列表分页
662
- */
663
- getList(page = 1, perPage = 10, options = {}) {
664
- const { select, sortBy, sortOrder, year } = options;
665
- return this.proxy.get({
666
- params: {
667
- page,
668
- size: perPage,
669
- select: select === null || select === void 0 ? void 0 : select.join(' '),
670
- sortBy,
671
- sortOrder,
672
- year,
673
- },
674
- });
675
- }
676
- /**
677
- * 获取当前日记的上下各 n / 2 篇日记
678
- */
679
- getMiddleList(id, size = 5) {
680
- return this.proxy.list(id).get({
681
- params: { size },
682
- });
683
- }
684
- /**
685
- * 喜欢这篇日记
686
- */
687
- likeIt(id) {
688
- return this.proxy.like(id).get();
689
- }
690
- }
850
+ * values() {
851
+ for (const [, value] of this) {
852
+ yield value;
853
+ }
854
+ }
691
855
 
692
- class PageController {
693
- constructor(client) {
694
- this.client = client;
695
- this.base = 'pages';
696
- this.name = 'page';
697
- }
698
- get proxy() {
699
- return this.client.proxy(this.base);
700
- }
701
- /**
702
- * 页面列表
703
- */
704
- getList(page = 1, perPage = 10, options = {}) {
705
- const { select, sortBy, sortOrder } = options;
706
- return this.proxy.get({
707
- params: {
708
- page,
709
- size: perPage,
710
- select: select === null || select === void 0 ? void 0 : select.join(' '),
711
- sortBy,
712
- sortOrder,
713
- },
714
- });
715
- }
716
- /**
717
- * 页面详情
718
- */
719
- getById(id) {
720
- return this.proxy(id).get();
721
- }
722
- /**
723
- * 根据路径获取页面
724
- * @param slug 路径
725
- * @returns
726
- */
727
- getBySlug(slug) {
728
- return this.proxy.slug(slug).get({});
729
- }
730
- }
856
+ * [Symbol.iterator]() {
857
+ for (const item of this.cache) {
858
+ yield item;
859
+ }
731
860
 
732
- class PostController {
733
- constructor(client) {
734
- this.client = client;
735
- this.base = 'posts';
736
- this.name = 'post';
737
- }
738
- get proxy() {
739
- return this.client.proxy(this.base);
740
- }
741
- /**
742
- * 获取文章列表分页
743
- * @param page
744
- * @param perPage
745
- * @returns
746
- */
747
- getList(page = 1, perPage = 10, options = {}) {
748
- const { select, sortBy, sortOrder, year } = options;
749
- return this.proxy.get({
750
- params: {
751
- page,
752
- size: perPage,
753
- select: select === null || select === void 0 ? void 0 : select.join(' '),
754
- sortBy,
755
- sortOrder,
756
- year,
757
- },
758
- });
759
- }
760
- getPost(idOrCategoryName, slug) {
761
- if (arguments.length == 1) {
762
- return this.proxy(idOrCategoryName).get();
763
- }
764
- else {
765
- return this.proxy(idOrCategoryName)(slug).get();
766
- }
767
- }
768
- /**
769
- * 获取最新的文章
770
- */
771
- getLatest() {
772
- return this.proxy.latest.get();
773
- }
774
- /**
775
- * 点赞
776
- */
777
- thumbsUp(id) {
778
- return this.proxy('_thumbs-up').get({ params: { id } });
779
- }
780
- }
861
+ for (const item of this.oldCache) {
862
+ const [key] = item;
863
+ if (!this.cache.has(key)) {
864
+ yield item;
865
+ }
866
+ }
867
+ }
868
+
869
+ get size() {
870
+ let oldCacheSize = 0;
871
+ for (const key of this.oldCache.keys()) {
872
+ if (!this.cache.has(key)) {
873
+ oldCacheSize++;
874
+ }
875
+ }
781
876
 
782
- class ProjectController extends BaseCrudController {
783
- constructor(client) {
784
- super(client);
785
- this.client = client;
786
- this.base = 'projects';
787
- this.name = 'project';
788
- }
877
+ return Math.min(this._size + oldCacheSize, this.maxSize);
878
+ }
789
879
  }
790
880
 
791
- class RecentlyController {
792
- constructor(client) {
793
- this.client = client;
794
- this.base = 'recently';
795
- this.name = 'recently';
796
- }
797
- get proxy() {
798
- return this.client.proxy(this.base);
799
- }
800
- /**
801
- * 获取最新一条
802
- */
803
- getLatestOne() {
804
- return this.proxy.latest.get();
805
- }
806
- getAll() {
807
- return this.proxy.all.get();
808
- }
809
- getList(before, after, size) {
810
- if (!before && !after) {
811
- throw new Error('you can only choose `before` or `after`');
812
- }
813
- return this.proxy.get({
814
- params: {
815
- before,
816
- after,
817
- size,
818
- },
819
- });
820
- }
821
- }
881
+ var quickLru = QuickLRU;
822
882
 
823
- class SayController extends BaseCrudController {
824
- constructor(client) {
825
- super(client);
826
- this.client = client;
827
- this.base = 'says';
828
- this.name = 'say';
829
- }
830
- get proxy() {
831
- return this.client.proxy(this.base);
832
- }
833
- /**
834
- * 获取随机一条
835
- */
836
- getRandom() {
837
- return this.proxy.random.get();
838
- }
839
- }
883
+ const mapObj = mapObj$1.exports;
884
+ const camelCase = camelcase.exports;
885
+ const QuickLru = quickLru;
840
886
 
841
- class SearchController {
842
- constructor(client) {
843
- this.client = client;
844
- this.base = 'search';
845
- this.name = 'search';
846
- }
847
- get proxy() {
848
- return this.client.proxy(this.base);
849
- }
850
- search(type, keyword, options = {}) {
851
- return this.proxy(type).get({
852
- params: Object.assign({ keyword }, options),
853
- });
854
- }
855
- /**
856
- * 从 algolya 搜索
857
- * https://www.algolia.com/doc/api-reference/api-methods/search/
858
- * @param keyword
859
- * @param options
860
- * @returns
861
- */
862
- searchByAlgolia(keyword, options) {
863
- return this.proxy('algolia').get({ params: Object.assign({ keyword }, options) });
864
- }
865
- }
887
+ const has = (array, key) => array.some(x => {
888
+ if (typeof x === 'string') {
889
+ return x === key;
890
+ }
866
891
 
867
- class SnippetController {
868
- constructor(client) {
869
- this.client = client;
870
- this.base = 'snippets';
871
- this.name = 'snippet';
872
- }
873
- get proxy() {
874
- return this.client.proxy(this.base);
875
- }
876
- getById(id) {
877
- return this.proxy(id).get();
878
- }
879
- getByReferenceAndName(reference, name) {
880
- return this.proxy(reference)(name).get();
881
- }
882
- }
892
+ x.lastIndex = 0;
893
+ return x.test(key);
894
+ });
883
895
 
884
- class UserController {
885
- constructor(client) {
886
- this.client = client;
887
- this.base = 'master';
888
- this.name = 'user';
889
- }
890
- get proxy() {
891
- return this.client.proxy(this.base);
892
- }
893
- getMasterInfo() {
894
- return this.proxy.get();
895
- }
896
- login(username, password) {
897
- return this.proxy.login.post({
898
- data: {
899
- username,
900
- password,
896
+ const cache = new QuickLru({maxSize: 100000});
897
+
898
+ // Reproduces behavior from `map-obj`
899
+ const isObject = value =>
900
+ typeof value === 'object' &&
901
+ value !== null &&
902
+ !(value instanceof RegExp) &&
903
+ !(value instanceof Error) &&
904
+ !(value instanceof Date);
905
+
906
+ const camelCaseConvert = (input, options) => {
907
+ if (!isObject(input)) {
908
+ return input;
909
+ }
910
+
911
+ options = {
912
+ deep: false,
913
+ pascalCase: false,
914
+ ...options
915
+ };
916
+
917
+ const {exclude, pascalCase, stopPaths, deep} = options;
918
+
919
+ const stopPathsSet = new Set(stopPaths);
920
+
921
+ const makeMapper = parentPath => (key, value) => {
922
+ if (deep && isObject(value)) {
923
+ const path = parentPath === undefined ? key : `${parentPath}.${key}`;
924
+
925
+ if (!stopPathsSet.has(path)) {
926
+ value = mapObj(value, makeMapper(path));
927
+ }
928
+ }
929
+
930
+ if (!(exclude && has(exclude, key))) {
931
+ const cacheKey = pascalCase ? `${key}_` : key;
932
+
933
+ if (cache.has(cacheKey)) {
934
+ key = cache.get(cacheKey);
935
+ } else {
936
+ const returnValue = camelCase(key, {pascalCase});
937
+
938
+ if (key.length < 100) { // Prevent abuse
939
+ cache.set(cacheKey, returnValue);
940
+ }
941
+
942
+ key = returnValue;
943
+ }
944
+ }
945
+
946
+ return [key, value];
947
+ };
948
+
949
+ return mapObj(input, makeMapper(undefined));
950
+ };
951
+
952
+ var camelcaseKeys = (input, options) => {
953
+ if (Array.isArray(input)) {
954
+ return Object.keys(input).map(key => camelCaseConvert(input[key], options));
955
+ }
956
+
957
+ return camelCaseConvert(input, options);
958
+ };
959
+
960
+ function attachRequestMethod(target) {
961
+ Object.defineProperty(target, '$$get', {
962
+ value: function (url, options) {
963
+ // HINT: method get only accept search params;
964
+ const { params = {} } = options;
965
+ const qs = handleSearchParams(params);
966
+ return target.instance.get(url + `${qs ? `${'?' + qs}` : ''}`);
967
+ },
968
+ });
969
+ ['put', 'post', 'patch', 'delete'].forEach((method) => {
970
+ Object.defineProperty(target, '$$' + method, {
971
+ value: function (path, options) {
972
+ return target.instance[method](path, options);
901
973
  },
902
974
  });
975
+ });
976
+ }
977
+ // FIXME: only support string value
978
+ function handleSearchParams(obj) {
979
+ if (!obj && typeof obj !== 'object') {
980
+ throw new TypeError('params must be object.');
903
981
  }
904
- checkTokenValid(token) {
905
- return this.proxy.check_logged.get({
906
- params: {
907
- token,
908
- },
909
- });
982
+ if (obj instanceof URLSearchParams) {
983
+ return obj.toString();
910
984
  }
985
+ const search = new URLSearchParams();
986
+ Object.entries(obj).forEach(([k, v]) => {
987
+ if (typeof v === 'undefined' ||
988
+ Object.prototype.toString.call(v) === '[object Null]') {
989
+ return;
990
+ }
991
+ search.set(k, v);
992
+ });
993
+ return search.toString();
911
994
  }
912
995
 
913
- const allControllers = [
914
- AggregateController,
915
- CategoryController,
916
- CommentController,
917
- LinkController,
918
- NoteController,
919
- PageController,
920
- PostController,
921
- ProjectController,
922
- RecentlyController,
923
- SayController,
924
- SearchController,
925
- SnippetController,
926
- UserController,
927
- ];
928
- const allContollerNames = [
929
- 'aggregate',
930
- 'category',
931
- 'comment',
932
- 'link',
933
- 'note',
934
- 'page',
935
- 'post',
936
- 'project',
937
- 'recently',
938
- 'say',
939
- 'search',
940
- 'snippet',
941
- 'user',
942
- ];
943
-
944
996
  class HTTPControllerDefine {
945
997
  get master() {
946
998
  return this.user;
@@ -954,11 +1006,10 @@ class HTTPControllerDefine {
954
1006
  }
955
1007
 
956
1008
  class HTTPClient extends HTTPControllerDefine {
957
- // define all clients
958
- constructor(_endpoint, _instance) {
1009
+ constructor(_endpoint, _adaptor) {
959
1010
  super();
960
1011
  this._endpoint = _endpoint;
961
- this._instance = _instance;
1012
+ this._adaptor = _adaptor;
962
1013
  this._endpoint = _endpoint
963
1014
  .replace(/\/*$/, '')
964
1015
  .replace('localhost', '127.0.0.1');
@@ -999,7 +1050,7 @@ class HTTPClient extends HTTPControllerDefine {
999
1050
  return this._endpoint;
1000
1051
  }
1001
1052
  get instance() {
1002
- return this._instance;
1053
+ return this._adaptor;
1003
1054
  }
1004
1055
  request(options) {
1005
1056
  return this['$$' + String(options.method || 'get').toLowerCase()](options.url, options);