@mx-space/api-client 0.3.7 → 0.4.3

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