@mx-space/api-client 0.0.4 → 0.3.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 (250) hide show
  1. package/build/index.cjs.js +526 -33
  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 +542 -34
  6. package/build/index.esm.js +523 -30
  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 +526 -33
  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/core/attachRequest.js +29 -6
  15. package/esm/core/attachRequest.js.map +1 -1
  16. package/esm/core/client.d.ts +12 -15
  17. package/esm/core/client.js +21 -8
  18. package/esm/core/client.js.map +1 -1
  19. package/esm/core/controllers/aggregate.d.ts +44 -0
  20. package/esm/core/controllers/aggregate.js +40 -0
  21. package/esm/core/controllers/aggregate.js.map +1 -0
  22. package/esm/core/controllers/all.d.ts +14 -0
  23. package/esm/core/controllers/all.js +39 -0
  24. package/esm/core/controllers/all.js.map +1 -0
  25. package/esm/core/controllers/base.d.ts +29 -0
  26. package/esm/core/controllers/base.js +25 -0
  27. package/esm/core/controllers/base.js.map +1 -0
  28. package/esm/core/controllers/category.d.ts +44 -0
  29. package/esm/core/controllers/category.js +66 -0
  30. package/esm/core/controllers/category.js.map +1 -0
  31. package/esm/core/controllers/comment.d.ts +42 -0
  32. package/esm/core/controllers/comment.js +43 -0
  33. package/esm/core/controllers/comment.js.map +1 -0
  34. package/esm/core/controllers/index.d.ts +1 -0
  35. package/esm/core/controllers/index.js +2 -0
  36. package/esm/core/controllers/index.js.map +1 -0
  37. package/esm/core/controllers/link.d.ts +9 -0
  38. package/esm/core/controllers/link.js +10 -0
  39. package/esm/core/controllers/link.js.map +1 -0
  40. package/esm/core/controllers/note.d.ts +54 -0
  41. package/esm/core/controllers/note.js +54 -0
  42. package/esm/core/controllers/note.js.map +1 -0
  43. package/esm/core/controllers/page.d.ts +34 -0
  44. package/esm/core/controllers/page.js +33 -0
  45. package/esm/core/controllers/page.js.map +1 -0
  46. package/esm/core/controllers/post.d.ts +47 -0
  47. package/esm/core/controllers/post.js +42 -0
  48. package/esm/core/controllers/post.js.map +1 -0
  49. package/esm/core/controllers/recently.d.ts +33 -0
  50. package/esm/core/controllers/recently.js +32 -0
  51. package/esm/core/controllers/recently.js.map +1 -0
  52. package/esm/core/controllers/say.d.ts +22 -0
  53. package/esm/core/controllers/say.js +19 -0
  54. package/esm/core/controllers/say.js.map +1 -0
  55. package/esm/core/controllers/search.d.ts +58 -0
  56. package/esm/core/controllers/search.js +26 -0
  57. package/esm/core/controllers/search.js.map +1 -0
  58. package/esm/core/controllers/user.d.ts +28 -0
  59. package/esm/core/controllers/user.js +29 -0
  60. package/esm/core/controllers/user.js.map +1 -0
  61. package/esm/core/define.d.ts +17 -0
  62. package/esm/core/define.js +12 -0
  63. package/esm/core/define.js.map +1 -0
  64. package/esm/core/dtos/comment.d.ts +6 -0
  65. package/esm/core/dtos/comment.js +3 -0
  66. package/esm/core/dtos/comment.js.map +1 -0
  67. package/esm/core/index.d.ts +1 -1
  68. package/esm/core/index.js +1 -1
  69. package/esm/core/index.js.map +1 -1
  70. package/esm/index.d.ts +1 -1
  71. package/esm/index.js +1 -1
  72. package/esm/index.js.map +1 -1
  73. package/esm/interfaces/{client.d.ts → controller.d.ts} +1 -1
  74. package/esm/interfaces/controller.js +2 -0
  75. package/esm/interfaces/controller.js.map +1 -0
  76. package/esm/interfaces/instance.d.ts +3 -2
  77. package/esm/interfaces/options.d.ts +1 -0
  78. package/esm/interfaces/options.js +2 -0
  79. package/esm/interfaces/options.js.map +1 -0
  80. package/esm/interfaces/params.d.ts +4 -0
  81. package/esm/interfaces/params.js +2 -0
  82. package/esm/interfaces/params.js.map +1 -0
  83. package/esm/interfaces/request.d.ts +19 -9
  84. package/esm/models/aggregate.d.ts +47 -0
  85. package/esm/models/aggregate.js +6 -0
  86. package/esm/models/aggregate.js.map +1 -0
  87. package/esm/models/base.d.ts +1 -1
  88. package/esm/models/category.d.ts +24 -0
  89. package/esm/models/category.js +6 -0
  90. package/esm/models/category.js.map +1 -0
  91. package/esm/models/comment.d.ts +28 -0
  92. package/esm/models/comment.js +7 -0
  93. package/esm/models/comment.js.map +1 -0
  94. package/esm/models/link.d.ts +17 -0
  95. package/esm/models/link.js +11 -0
  96. package/esm/models/link.js.map +1 -0
  97. package/esm/models/note.d.ts +34 -0
  98. package/esm/models/note.js +2 -0
  99. package/esm/models/note.js.map +1 -0
  100. package/esm/models/page.d.ts +24 -0
  101. package/esm/models/page.js +7 -0
  102. package/esm/models/page.js.map +1 -0
  103. package/esm/models/post.d.ts +2 -7
  104. package/esm/models/recently.d.ts +5 -0
  105. package/esm/models/recently.js +2 -0
  106. package/esm/models/recently.js.map +1 -0
  107. package/esm/models/say.d.ts +6 -0
  108. package/esm/models/say.js +2 -0
  109. package/esm/models/say.js.map +1 -0
  110. package/esm/models/setting.d.ts +66 -0
  111. package/esm/models/setting.js +2 -0
  112. package/esm/models/setting.js.map +1 -0
  113. package/esm/models/user.d.ts +29 -0
  114. package/esm/models/user.js +2 -0
  115. package/esm/models/user.js.map +1 -0
  116. package/esm/utils/index.d.ts +4 -0
  117. package/esm/utils/index.js +38 -0
  118. package/esm/utils/index.js.map +1 -1
  119. package/lib/core/attachRequest.js +29 -6
  120. package/lib/core/attachRequest.js.map +1 -1
  121. package/lib/core/client.d.ts +12 -15
  122. package/lib/core/client.js +21 -8
  123. package/lib/core/client.js.map +1 -1
  124. package/lib/core/controllers/aggregate.d.ts +44 -0
  125. package/lib/core/controllers/aggregate.js +44 -0
  126. package/lib/core/controllers/aggregate.js.map +1 -0
  127. package/lib/core/controllers/all.d.ts +14 -0
  128. package/lib/core/controllers/all.js +52 -0
  129. package/lib/core/controllers/all.js.map +1 -0
  130. package/lib/core/controllers/base.d.ts +29 -0
  131. package/lib/core/controllers/base.js +29 -0
  132. package/lib/core/controllers/base.js.map +1 -0
  133. package/lib/core/controllers/category.d.ts +44 -0
  134. package/lib/core/controllers/category.js +70 -0
  135. package/lib/core/controllers/category.js.map +1 -0
  136. package/lib/core/controllers/comment.d.ts +42 -0
  137. package/lib/core/controllers/comment.js +47 -0
  138. package/lib/core/controllers/comment.js.map +1 -0
  139. package/lib/core/controllers/index.d.ts +1 -0
  140. package/lib/core/{clients → controllers}/index.js +0 -3
  141. package/lib/core/controllers/index.js.map +1 -0
  142. package/lib/core/controllers/link.d.ts +9 -0
  143. package/lib/core/controllers/link.js +14 -0
  144. package/lib/core/controllers/link.js.map +1 -0
  145. package/lib/core/controllers/note.d.ts +54 -0
  146. package/lib/core/controllers/note.js +58 -0
  147. package/lib/core/controllers/note.js.map +1 -0
  148. package/lib/core/controllers/page.d.ts +34 -0
  149. package/lib/core/controllers/page.js +37 -0
  150. package/lib/core/controllers/page.js.map +1 -0
  151. package/lib/core/controllers/post.d.ts +47 -0
  152. package/lib/core/controllers/post.js +46 -0
  153. package/lib/core/controllers/post.js.map +1 -0
  154. package/lib/core/controllers/recently.d.ts +33 -0
  155. package/lib/core/controllers/recently.js +36 -0
  156. package/lib/core/controllers/recently.js.map +1 -0
  157. package/lib/core/controllers/say.d.ts +22 -0
  158. package/lib/core/controllers/say.js +23 -0
  159. package/lib/core/controllers/say.js.map +1 -0
  160. package/lib/core/controllers/search.d.ts +58 -0
  161. package/lib/core/controllers/search.js +30 -0
  162. package/lib/core/controllers/search.js.map +1 -0
  163. package/lib/core/controllers/user.d.ts +28 -0
  164. package/lib/core/controllers/user.js +33 -0
  165. package/lib/core/controllers/user.js.map +1 -0
  166. package/lib/core/define.d.ts +17 -0
  167. package/lib/core/define.js +16 -0
  168. package/lib/core/define.js.map +1 -0
  169. package/lib/core/dtos/comment.d.ts +6 -0
  170. package/lib/core/dtos/comment.js +7 -0
  171. package/lib/core/dtos/comment.js.map +1 -0
  172. package/lib/core/index.d.ts +1 -1
  173. package/lib/core/index.js +1 -1
  174. package/lib/core/index.js.map +1 -1
  175. package/lib/index.d.ts +1 -1
  176. package/lib/index.js +4 -4
  177. package/lib/index.js.map +1 -1
  178. package/lib/interfaces/{client.d.ts → controller.d.ts} +1 -1
  179. package/lib/interfaces/controller.js +3 -0
  180. package/lib/interfaces/controller.js.map +1 -0
  181. package/lib/interfaces/instance.d.ts +3 -2
  182. package/lib/interfaces/options.d.ts +1 -0
  183. package/lib/interfaces/options.js +3 -0
  184. package/lib/interfaces/options.js.map +1 -0
  185. package/lib/interfaces/params.d.ts +4 -0
  186. package/lib/interfaces/{client.js → params.js} +1 -1
  187. package/lib/interfaces/params.js.map +1 -0
  188. package/lib/interfaces/request.d.ts +19 -9
  189. package/lib/models/aggregate.d.ts +47 -0
  190. package/lib/models/aggregate.js +9 -0
  191. package/lib/models/aggregate.js.map +1 -0
  192. package/lib/models/base.d.ts +1 -1
  193. package/lib/models/category.d.ts +24 -0
  194. package/lib/models/category.js +9 -0
  195. package/lib/models/category.js.map +1 -0
  196. package/lib/models/comment.d.ts +28 -0
  197. package/lib/models/comment.js +10 -0
  198. package/lib/models/comment.js.map +1 -0
  199. package/lib/models/link.d.ts +17 -0
  200. package/lib/models/link.js +14 -0
  201. package/lib/models/link.js.map +1 -0
  202. package/lib/models/note.d.ts +34 -0
  203. package/lib/models/note.js +3 -0
  204. package/lib/models/note.js.map +1 -0
  205. package/lib/models/page.d.ts +24 -0
  206. package/lib/models/page.js +10 -0
  207. package/lib/models/page.js.map +1 -0
  208. package/lib/models/post.d.ts +2 -7
  209. package/lib/models/recently.d.ts +5 -0
  210. package/lib/models/recently.js +3 -0
  211. package/lib/models/recently.js.map +1 -0
  212. package/lib/models/say.d.ts +6 -0
  213. package/lib/models/say.js +3 -0
  214. package/lib/models/say.js.map +1 -0
  215. package/lib/models/setting.d.ts +66 -0
  216. package/lib/models/setting.js +3 -0
  217. package/lib/models/setting.js.map +1 -0
  218. package/lib/models/user.d.ts +29 -0
  219. package/lib/models/user.js +3 -0
  220. package/lib/models/user.js.map +1 -0
  221. package/lib/utils/index.d.ts +4 -0
  222. package/lib/utils/index.js +42 -1
  223. package/lib/utils/index.js.map +1 -1
  224. package/package.json +12 -11
  225. package/esm/core/clients/all.d.ts +0 -3
  226. package/esm/core/clients/all.js +0 -9
  227. package/esm/core/clients/all.js.map +0 -1
  228. package/esm/core/clients/index.d.ts +0 -3
  229. package/esm/core/clients/index.js +0 -5
  230. package/esm/core/clients/index.js.map +0 -1
  231. package/esm/core/clients/note.d.ts +0 -5
  232. package/esm/core/clients/note.js +0 -7
  233. package/esm/core/clients/note.js.map +0 -1
  234. package/esm/core/clients/post.d.ts +0 -16
  235. package/esm/core/clients/post.js +0 -23
  236. package/esm/core/clients/post.js.map +0 -1
  237. package/esm/interfaces/client.js +0 -2
  238. package/esm/interfaces/client.js.map +0 -1
  239. package/lib/core/clients/all.d.ts +0 -3
  240. package/lib/core/clients/all.js +0 -12
  241. package/lib/core/clients/all.js.map +0 -1
  242. package/lib/core/clients/index.d.ts +0 -3
  243. package/lib/core/clients/index.js.map +0 -1
  244. package/lib/core/clients/note.d.ts +0 -5
  245. package/lib/core/clients/note.js +0 -11
  246. package/lib/core/clients/note.js.map +0 -1
  247. package/lib/core/clients/post.d.ts +0 -16
  248. package/lib/core/clients/post.js +0 -27
  249. package/lib/core/clients/post.js.map +0 -1
  250. package/lib/interfaces/client.js.map +0 -1
@@ -366,55 +366,536 @@
366
366
 
367
367
  const isPlainObject = (obj) => typeof obj === 'object' &&
368
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
+ };
369
407
 
370
408
  function attachRequestMethod(target) {
371
- const resolveFullPath = target.resolveFullPath;
372
- Object.defineProperty(target, 'get', {
373
- value: function (path, params) {
374
- return target.instance.get(resolveFullPath(path), {
375
- params,
376
- });
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}` : ''}`);
377
415
  },
378
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 CategoryController {
488
+ constructor(client) {
489
+ this.client = client;
490
+ this.name = 'category';
491
+ this.base = 'categories';
492
+ }
493
+ get proxy() {
494
+ return this.client.proxy(this.base);
495
+ }
496
+ getAllCategories() {
497
+ return this.proxy.get({
498
+ params: {
499
+ type: CategoryType.Category,
500
+ },
501
+ });
502
+ }
503
+ getAllTags() {
504
+ return this.proxy.get({
505
+ params: {
506
+ type: CategoryType.Tag,
507
+ },
508
+ });
509
+ }
510
+ async getCategoryDetail(ids) {
511
+ if (typeof ids === 'string') {
512
+ const data = await this.proxy.get({
513
+ params: {
514
+ ids: ids,
515
+ },
516
+ });
517
+ const result = Object.values(data.entries)[0];
518
+ attachRawFromOneToAnthor(data, result);
519
+ return result;
520
+ }
521
+ else if (Array.isArray(ids)) {
522
+ const data = await this.proxy.get({
523
+ params: {
524
+ ids: ids.join(','),
525
+ },
526
+ });
527
+ const entries = data === null || data === void 0 ? void 0 : data.entries;
528
+ if (!entries) {
529
+ throw new RequestError('data structure error', 500, data.request.path, data);
530
+ }
531
+ const map = new Map(Object.entries(entries).map(([id, value]) => [id.toLowerCase(), value]));
532
+ attachRawFromOneToAnthor(data, map);
533
+ return map;
534
+ }
535
+ }
536
+ async getCategoryByIdOrSlug(idOrSlug) {
537
+ const res = await this.proxy(idOrSlug).get();
538
+ return destructureData(res);
539
+ }
540
+ async getTagByName(name) {
541
+ const res = await this.proxy(name).get({
542
+ params: {
543
+ tag: 1,
544
+ },
545
+ });
546
+ return res;
547
+ }
548
+ }
549
+
550
+ class CommentController {
551
+ constructor(client) {
552
+ this.client = client;
553
+ this.base = 'comments';
554
+ this.name = 'comment';
555
+ }
556
+ get proxy() {
557
+ return this.client.proxy(this.base);
558
+ }
559
+ /**
560
+ * 根据 comment id 获取评论, 包括子评论
561
+ */
562
+ getById(id) {
563
+ return this.proxy(id).get();
564
+ }
565
+ /**
566
+ * 获取文章的评论列表
567
+ * @param refId 文章 Id
568
+ */
569
+ getByRefId(refId, pagination = {}) {
570
+ const { page, size } = pagination;
571
+ return this.proxy.ref(refId).get({
572
+ params: { page: page || 1, size: size || 10 },
573
+ });
574
+ }
575
+ /**
576
+ * 评论
577
+ */
578
+ comment(refId, data) {
579
+ return this.proxy(refId).post({
580
+ data,
581
+ });
582
+ }
583
+ /**
584
+ * 回复评论
585
+ */
586
+ reply(commentId, data) {
587
+ return this.proxy.reply(commentId).post({
588
+ data,
589
+ });
590
+ }
591
+ }
592
+
593
+ class BaseCrudController {
594
+ constructor(client) {
595
+ this.client = client;
596
+ }
597
+ get proxy() {
598
+ return this.client.proxy(this.base);
599
+ }
600
+ getById(id) {
601
+ return this.proxy(id).get();
602
+ }
603
+ getAll() {
604
+ return this.proxy.all.get();
605
+ }
606
+ /**
607
+ * 带分页的查询
608
+ * @param page
609
+ * @param perPage
610
+ */
611
+ getAllPaginated(page, perPage) {
612
+ return this.proxy.get({
613
+ params: { page, size: perPage },
614
+ });
615
+ }
616
+ }
617
+
618
+ class LinkController extends BaseCrudController {
619
+ constructor(client) {
620
+ super(client);
621
+ this.client = client;
622
+ this.name = 'link';
623
+ this.base = 'links';
624
+ }
379
625
  }
380
626
 
381
- class NoteClient {
382
- constructor() {
627
+ class NoteController {
628
+ constructor(client) {
629
+ this.client = client;
383
630
  this.base = 'notes';
384
631
  this.name = 'note';
385
632
  }
633
+ get proxy() {
634
+ return this.client.proxy(this.base);
635
+ }
636
+ /**
637
+ * 最新日记
638
+ */
639
+ getLatest() {
640
+ return this.proxy.latest.get();
641
+ }
642
+ /**
643
+ * 获取一篇日记
644
+ * @param id id | nid
645
+ * @param password 访问密码
646
+ */
647
+ getNoteById(id, password) {
648
+ if (typeof id === 'number') {
649
+ return this.proxy.nid(id.toString()).get({
650
+ params: { password },
651
+ });
652
+ }
653
+ else {
654
+ return this.proxy(id).get({ params: { password } });
655
+ }
656
+ }
657
+ /**
658
+ * 日记列表分页
659
+ */
660
+ getList(page = 1, perPage = 10) {
661
+ return this.proxy.get({
662
+ params: { page, size: perPage },
663
+ });
664
+ }
665
+ /**
666
+ * 获取当前日记的上下各 n / 2 篇日记
667
+ */
668
+ getMiddleList(id, size = 5) {
669
+ return this.proxy.list(id).get({
670
+ params: { size },
671
+ });
672
+ }
673
+ /**
674
+ * 喜欢这篇日记
675
+ */
676
+ likeIt(id) {
677
+ return this.proxy.like(id).get();
678
+ }
386
679
  }
387
680
 
388
- class PostClient {
681
+ class PageController {
682
+ constructor(client) {
683
+ this.client = client;
684
+ this.base = 'pages';
685
+ this.name = 'page';
686
+ }
687
+ get proxy() {
688
+ return this.client.proxy(this.base);
689
+ }
690
+ /**
691
+ * 页面列表
692
+ */
693
+ getList(page = 1, perPage = 10) {
694
+ return this.proxy.get({
695
+ params: { page, size: perPage },
696
+ });
697
+ }
698
+ /**
699
+ * 页面详情
700
+ */
701
+ getById(id) {
702
+ return this.proxy(id).get();
703
+ }
704
+ /**
705
+ * 根据路径获取页面
706
+ * @param slug 路径
707
+ * @returns
708
+ */
709
+ getBySlug(slug) {
710
+ return this.proxy.slug(slug).get({});
711
+ }
712
+ }
713
+
714
+ class PostController {
389
715
  constructor(client) {
390
716
  this.client = client;
391
717
  this.base = 'posts';
392
718
  this.name = 'post';
393
719
  }
394
- getPosts(page = 1, perPage = 10) {
395
- return this.client.proxy(this.base).get({
720
+ get proxy() {
721
+ return this.client.proxy(this.base);
722
+ }
723
+ /**
724
+ * 获取文章列表分页
725
+ * @param page
726
+ * @param perPage
727
+ * @returns
728
+ */
729
+ getList(page = 1, perPage = 10) {
730
+ return this.proxy.get({
396
731
  params: { page, size: perPage },
397
732
  });
398
733
  }
399
734
  getPost(idOrCategoryName, slug) {
400
735
  if (arguments.length == 1) {
401
- return this.client.proxy(this.base)(idOrCategoryName).get();
736
+ return this.proxy(idOrCategoryName).get();
402
737
  }
403
738
  else {
404
- return this.client
405
- .proxy(this.base)(idOrCategoryName)(slug)
406
- .get();
739
+ return this.proxy(idOrCategoryName)(slug).get();
740
+ }
741
+ }
742
+ /**
743
+ * 获取最新的文章
744
+ */
745
+ getLatest() {
746
+ return this.proxy.latest.get();
747
+ }
748
+ /**
749
+ * 点赞
750
+ */
751
+ thumbsUp(id) {
752
+ return this.proxy('_thumbs-up').get({ params: { id } });
753
+ }
754
+ }
755
+
756
+ class RecentlyController {
757
+ constructor(client) {
758
+ this.client = client;
759
+ this.base = 'recently';
760
+ this.name = 'recently';
761
+ }
762
+ get proxy() {
763
+ return this.client.proxy(this.base);
764
+ }
765
+ /**
766
+ * 获取最新一条
767
+ */
768
+ getLatestOne() {
769
+ return this.proxy.latest.get();
770
+ }
771
+ getAll() {
772
+ return this.proxy.all.get();
773
+ }
774
+ getList(before, after, size) {
775
+ if (!before && !after) {
776
+ throw new Error('you can only choose `before` or `after`');
407
777
  }
778
+ return this.proxy.get({
779
+ params: {
780
+ before,
781
+ after,
782
+ size,
783
+ },
784
+ });
408
785
  }
409
786
  }
410
787
 
411
- const allClients = [NoteClient, PostClient];
412
- // export const allClientName = allClients.map((c) =>
413
- // c.name.replace('Client', '').toLowerCase(),
414
- // )
415
- // Why not code like before
416
- // In order to tree shake unnecessary client
417
- const allClientName = ['note', 'post'];
788
+ class SayController extends BaseCrudController {
789
+ constructor(client) {
790
+ super(client);
791
+ this.client = client;
792
+ this.base = 'says';
793
+ this.name = 'say';
794
+ }
795
+ get proxy() {
796
+ return this.client.proxy(this.base);
797
+ }
798
+ /**
799
+ * 获取随机一条
800
+ */
801
+ getRandom() {
802
+ return this.proxy.random.get();
803
+ }
804
+ }
805
+
806
+ class SearchController {
807
+ constructor(client) {
808
+ this.client = client;
809
+ this.base = 'search';
810
+ this.name = 'search';
811
+ }
812
+ get proxy() {
813
+ return this.client.proxy(this.base);
814
+ }
815
+ search(type, keyword, options = {}) {
816
+ return this.proxy(type).get({
817
+ params: Object.assign({ keyword }, options),
818
+ });
819
+ }
820
+ /**
821
+ * 从 algolya 搜索
822
+ * https://www.algolia.com/doc/api-reference/api-methods/search/
823
+ * @param keyword
824
+ * @param options
825
+ * @returns
826
+ */
827
+ searchByAlgolia(keyword, options) {
828
+ return this.proxy('algolia').get({ params: Object.assign({ keyword }, options) });
829
+ }
830
+ }
831
+
832
+ class UserController {
833
+ constructor(client) {
834
+ this.client = client;
835
+ this.base = 'master';
836
+ this.name = 'user';
837
+ }
838
+ get proxy() {
839
+ return this.client.proxy(this.base);
840
+ }
841
+ getMasterInfo() {
842
+ return this.proxy.get();
843
+ }
844
+ login(username, password) {
845
+ return this.proxy.login.post({
846
+ data: {
847
+ username,
848
+ password,
849
+ },
850
+ });
851
+ }
852
+ checkTokenValid(token) {
853
+ return this.proxy.check_logged.get({
854
+ params: {
855
+ token,
856
+ },
857
+ });
858
+ }
859
+ }
860
+
861
+ const allControllers = [
862
+ AggregateController,
863
+ CategoryController,
864
+ CommentController,
865
+ LinkController,
866
+ NoteController,
867
+ PageController,
868
+ PostController,
869
+ RecentlyController,
870
+ SayController,
871
+ SearchController,
872
+ UserController,
873
+ ];
874
+ const allContollerNames = [
875
+ 'aggregate',
876
+ 'category',
877
+ 'comment',
878
+ 'link',
879
+ 'note',
880
+ 'page',
881
+ 'post',
882
+ 'recently',
883
+ 'say',
884
+ 'search',
885
+ 'user',
886
+ ];
887
+
888
+ class HTTPControllerDefine {
889
+ get master() {
890
+ return this.user;
891
+ }
892
+ get friend() {
893
+ return this.link;
894
+ }
895
+ get shorthand() {
896
+ return this.recently;
897
+ }
898
+ }
418
899
 
419
900
  class RequestError extends Error {
420
901
  constructor(message, status, path, raw) {
@@ -425,8 +906,10 @@
425
906
  }
426
907
  }
427
908
 
428
- class HTTPClient {
909
+ class HTTPClient extends HTTPControllerDefine {
910
+ // define all clients
429
911
  constructor(_endpoint, _instance) {
912
+ super();
430
913
  this._endpoint = _endpoint;
431
914
  this._instance = _instance;
432
915
  this._endpoint = _endpoint
@@ -437,7 +920,7 @@
437
920
  attachRequestMethod(this);
438
921
  }
439
922
  initGetClient() {
440
- const clientsName = allClientName;
923
+ const clientsName = allContollerNames;
441
924
  for (const name of clientsName) {
442
925
  Object.defineProperty(this, name, {
443
926
  get() {
@@ -452,9 +935,9 @@
452
935
  });
453
936
  }
454
937
  }
455
- injectClients(Clients, ...rest) {
456
- Clients = Array.isArray(Clients) ? Clients : [Clients, ...rest];
457
- for (const Client of Clients) {
938
+ injectControllers(Controller, ...rest) {
939
+ Controller = Array.isArray(Controller) ? Controller : [Controller, ...rest];
940
+ for (const Client of Controller) {
458
941
  const cl = new Client(this);
459
942
  Object.defineProperty(this, `_${cl.name.toLowerCase()}`, {
460
943
  get() {
@@ -472,7 +955,7 @@
472
955
  return this._instance;
473
956
  }
474
957
  request(options) {
475
- return this._instance[String(options.method || 'get').toLowerCase()](options.url, options);
958
+ return this['$$' + String(options.method || 'get').toLowerCase()](options.url, options);
476
959
  }
477
960
  get proxy() {
478
961
  return this._proxy;
@@ -535,6 +1018,17 @@
535
1018
  enumerable: false,
536
1019
  configurable: false,
537
1020
  });
1021
+ // attach request config onto response
1022
+ Object.defineProperty(transform, 'request', {
1023
+ get() {
1024
+ return {
1025
+ url,
1026
+ method: name,
1027
+ options,
1028
+ };
1029
+ },
1030
+ enumerable: false,
1031
+ });
538
1032
  return transform;
539
1033
  };
540
1034
  }
@@ -552,18 +1046,17 @@
552
1046
  }
553
1047
  }
554
1048
  function createClient(adapter) {
555
- // TODO: add support other network lib
556
1049
  return (endpoint) => {
557
1050
  return new HTTPClient(endpoint, adapter);
558
1051
  };
559
1052
  }
560
1053
 
561
1054
  exports.HTTPClient = HTTPClient;
562
- exports.NoteClient = NoteClient;
563
- exports.PostClient = PostClient;
1055
+ exports.NoteClient = NoteController;
1056
+ exports.PostClient = PostController;
564
1057
  exports.RequestError = RequestError;
565
- exports.allClientName = allClientName;
566
- exports.allClients = allClients;
1058
+ exports.allClientName = allContollerNames;
1059
+ exports.allClients = allControllers;
567
1060
  exports.createClient = createClient;
568
1061
 
569
1062
  Object.defineProperty(exports, '__esModule', { value: true });