@mx-space/api-client 0.3.4 → 0.4.0

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