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