@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
@@ -364,55 +364,536 @@ var camelcaseKeys = (input, options) => {
364
364
 
365
365
  const isPlainObject = (obj) => typeof obj === 'object' &&
366
366
  Object.prototype.toString.call(obj) === '[object Object]';
367
+ const sortOrderToNumber = (order) => {
368
+ return ({
369
+ asc: 1,
370
+ desc: -1,
371
+ }[order] || 1);
372
+ };
373
+ const destructureData = (payload) => {
374
+ if (typeof payload !== 'object') {
375
+ return payload;
376
+ }
377
+ if (payload === null) {
378
+ return payload;
379
+ }
380
+ const data = payload.data;
381
+ const dataIsPlainObject = Object.prototype.toString.call(data) === '[object Object]';
382
+ if (dataIsPlainObject && Object.keys(payload).length === 1) {
383
+ const d = Object.assign({}, data);
384
+ // attach raw onto new data
385
+ attachRawFromOneToAnthor(payload, d);
386
+ return d;
387
+ }
388
+ return payload;
389
+ };
390
+ const attachRawFromOneToAnthor = (from, to) => {
391
+ if (!from) {
392
+ return;
393
+ }
394
+ from.raw &&
395
+ Object.defineProperty(to, 'raw', {
396
+ value: Object.assign({}, from.raw),
397
+ enumerable: false,
398
+ });
399
+ from.request &&
400
+ Object.defineProperty(to, 'request', {
401
+ value: Object.assign({}, from.request),
402
+ enumerable: false,
403
+ });
404
+ };
367
405
 
368
406
  function attachRequestMethod(target) {
369
- const resolveFullPath = target.resolveFullPath;
370
- Object.defineProperty(target, 'get', {
371
- value: function (path, params) {
372
- return target.instance.get(resolveFullPath(path), {
373
- params,
374
- });
407
+ Object.defineProperty(target, '$$get', {
408
+ value: function (url, options) {
409
+ // HINT: method get only accept search params;
410
+ const { params = {} } = options;
411
+ const qs = handleSearchParams(params);
412
+ return target.instance.get(url + `${qs ? `${'?' + qs}` : ''}`);
375
413
  },
376
414
  });
415
+ Object.defineProperty(target, '$$post', {
416
+ value: function (path, options) {
417
+ return target.instance.post(path, options);
418
+ },
419
+ });
420
+ }
421
+ // FIXME: only support string value
422
+ function handleSearchParams(obj) {
423
+ if (!obj && typeof obj !== 'object') {
424
+ throw new TypeError('params must be object.');
425
+ }
426
+ if (obj instanceof URLSearchParams) {
427
+ return obj.toString();
428
+ }
429
+ const search = new URLSearchParams();
430
+ Object.entries(obj).forEach(([k, v]) => {
431
+ if (typeof v === 'undefined' ||
432
+ Object.prototype.toString.call(v) === '[object Null]') {
433
+ return;
434
+ }
435
+ search.set(k, v);
436
+ });
437
+ return search.toString();
438
+ }
439
+
440
+ class AggregateController {
441
+ constructor(client) {
442
+ this.client = client;
443
+ this.base = 'aggregate';
444
+ this.name = 'aggregate';
445
+ }
446
+ get proxy() {
447
+ return this.client.proxy(this.base);
448
+ }
449
+ /**
450
+ * 获取聚合数据
451
+ */
452
+ getAggregateData() {
453
+ return this.proxy.get();
454
+ }
455
+ /**
456
+ * 获取最新发布的内容
457
+ */
458
+ getTop(size = 5) {
459
+ return this.proxy.top.get({ params: { size } });
460
+ }
461
+ getTimeline(options) {
462
+ const { sort, type, year } = options || {};
463
+ return this.proxy.timeline.get({
464
+ params: {
465
+ sort: sort && sortOrderToNumber(sort),
466
+ type,
467
+ year,
468
+ },
469
+ });
470
+ }
471
+ /**
472
+ * 获取聚合数据统计
473
+ */
474
+ getStat() {
475
+ return this.proxy.stat.get();
476
+ }
477
+ }
478
+
479
+ var CategoryType;
480
+ (function (CategoryType) {
481
+ CategoryType[CategoryType["Category"] = 0] = "Category";
482
+ CategoryType[CategoryType["Tag"] = 1] = "Tag";
483
+ })(CategoryType || (CategoryType = {}));
484
+
485
+ class CategoryController {
486
+ constructor(client) {
487
+ this.client = client;
488
+ this.name = 'category';
489
+ this.base = 'categories';
490
+ }
491
+ get proxy() {
492
+ return this.client.proxy(this.base);
493
+ }
494
+ getAllCategories() {
495
+ return this.proxy.get({
496
+ params: {
497
+ type: CategoryType.Category,
498
+ },
499
+ });
500
+ }
501
+ getAllTags() {
502
+ return this.proxy.get({
503
+ params: {
504
+ type: CategoryType.Tag,
505
+ },
506
+ });
507
+ }
508
+ async getCategoryDetail(ids) {
509
+ if (typeof ids === 'string') {
510
+ const data = await this.proxy.get({
511
+ params: {
512
+ ids: ids,
513
+ },
514
+ });
515
+ const result = Object.values(data.entries)[0];
516
+ attachRawFromOneToAnthor(data, result);
517
+ return result;
518
+ }
519
+ else if (Array.isArray(ids)) {
520
+ const data = await this.proxy.get({
521
+ params: {
522
+ ids: ids.join(','),
523
+ },
524
+ });
525
+ const entries = data === null || data === void 0 ? void 0 : data.entries;
526
+ if (!entries) {
527
+ throw new RequestError('data structure error', 500, data.request.path, data);
528
+ }
529
+ const map = new Map(Object.entries(entries).map(([id, value]) => [id.toLowerCase(), value]));
530
+ attachRawFromOneToAnthor(data, map);
531
+ return map;
532
+ }
533
+ }
534
+ async getCategoryByIdOrSlug(idOrSlug) {
535
+ const res = await this.proxy(idOrSlug).get();
536
+ return destructureData(res);
537
+ }
538
+ async getTagByName(name) {
539
+ const res = await this.proxy(name).get({
540
+ params: {
541
+ tag: 1,
542
+ },
543
+ });
544
+ return res;
545
+ }
546
+ }
547
+
548
+ class CommentController {
549
+ constructor(client) {
550
+ this.client = client;
551
+ this.base = 'comments';
552
+ this.name = 'comment';
553
+ }
554
+ get proxy() {
555
+ return this.client.proxy(this.base);
556
+ }
557
+ /**
558
+ * 根据 comment id 获取评论, 包括子评论
559
+ */
560
+ getById(id) {
561
+ return this.proxy(id).get();
562
+ }
563
+ /**
564
+ * 获取文章的评论列表
565
+ * @param refId 文章 Id
566
+ */
567
+ getByRefId(refId, pagination = {}) {
568
+ const { page, size } = pagination;
569
+ return this.proxy.ref(refId).get({
570
+ params: { page: page || 1, size: size || 10 },
571
+ });
572
+ }
573
+ /**
574
+ * 评论
575
+ */
576
+ comment(refId, data) {
577
+ return this.proxy(refId).post({
578
+ data,
579
+ });
580
+ }
581
+ /**
582
+ * 回复评论
583
+ */
584
+ reply(commentId, data) {
585
+ return this.proxy.reply(commentId).post({
586
+ data,
587
+ });
588
+ }
589
+ }
590
+
591
+ class BaseCrudController {
592
+ constructor(client) {
593
+ this.client = client;
594
+ }
595
+ get proxy() {
596
+ return this.client.proxy(this.base);
597
+ }
598
+ getById(id) {
599
+ return this.proxy(id).get();
600
+ }
601
+ getAll() {
602
+ return this.proxy.all.get();
603
+ }
604
+ /**
605
+ * 带分页的查询
606
+ * @param page
607
+ * @param perPage
608
+ */
609
+ getAllPaginated(page, perPage) {
610
+ return this.proxy.get({
611
+ params: { page, size: perPage },
612
+ });
613
+ }
614
+ }
615
+
616
+ class LinkController extends BaseCrudController {
617
+ constructor(client) {
618
+ super(client);
619
+ this.client = client;
620
+ this.name = 'link';
621
+ this.base = 'links';
622
+ }
377
623
  }
378
624
 
379
- class NoteClient {
380
- constructor() {
625
+ class NoteController {
626
+ constructor(client) {
627
+ this.client = client;
381
628
  this.base = 'notes';
382
629
  this.name = 'note';
383
630
  }
631
+ get proxy() {
632
+ return this.client.proxy(this.base);
633
+ }
634
+ /**
635
+ * 最新日记
636
+ */
637
+ getLatest() {
638
+ return this.proxy.latest.get();
639
+ }
640
+ /**
641
+ * 获取一篇日记
642
+ * @param id id | nid
643
+ * @param password 访问密码
644
+ */
645
+ getNoteById(id, password) {
646
+ if (typeof id === 'number') {
647
+ return this.proxy.nid(id.toString()).get({
648
+ params: { password },
649
+ });
650
+ }
651
+ else {
652
+ return this.proxy(id).get({ params: { password } });
653
+ }
654
+ }
655
+ /**
656
+ * 日记列表分页
657
+ */
658
+ getList(page = 1, perPage = 10) {
659
+ return this.proxy.get({
660
+ params: { page, size: perPage },
661
+ });
662
+ }
663
+ /**
664
+ * 获取当前日记的上下各 n / 2 篇日记
665
+ */
666
+ getMiddleList(id, size = 5) {
667
+ return this.proxy.list(id).get({
668
+ params: { size },
669
+ });
670
+ }
671
+ /**
672
+ * 喜欢这篇日记
673
+ */
674
+ likeIt(id) {
675
+ return this.proxy.like(id).get();
676
+ }
384
677
  }
385
678
 
386
- class PostClient {
679
+ class PageController {
680
+ constructor(client) {
681
+ this.client = client;
682
+ this.base = 'pages';
683
+ this.name = 'page';
684
+ }
685
+ get proxy() {
686
+ return this.client.proxy(this.base);
687
+ }
688
+ /**
689
+ * 页面列表
690
+ */
691
+ getList(page = 1, perPage = 10) {
692
+ return this.proxy.get({
693
+ params: { page, size: perPage },
694
+ });
695
+ }
696
+ /**
697
+ * 页面详情
698
+ */
699
+ getById(id) {
700
+ return this.proxy(id).get();
701
+ }
702
+ /**
703
+ * 根据路径获取页面
704
+ * @param slug 路径
705
+ * @returns
706
+ */
707
+ getBySlug(slug) {
708
+ return this.proxy.slug(slug).get({});
709
+ }
710
+ }
711
+
712
+ class PostController {
387
713
  constructor(client) {
388
714
  this.client = client;
389
715
  this.base = 'posts';
390
716
  this.name = 'post';
391
717
  }
392
- getPosts(page = 1, perPage = 10) {
393
- return this.client.proxy(this.base).get({
718
+ get proxy() {
719
+ return this.client.proxy(this.base);
720
+ }
721
+ /**
722
+ * 获取文章列表分页
723
+ * @param page
724
+ * @param perPage
725
+ * @returns
726
+ */
727
+ getList(page = 1, perPage = 10) {
728
+ return this.proxy.get({
394
729
  params: { page, size: perPage },
395
730
  });
396
731
  }
397
732
  getPost(idOrCategoryName, slug) {
398
733
  if (arguments.length == 1) {
399
- return this.client.proxy(this.base)(idOrCategoryName).get();
734
+ return this.proxy(idOrCategoryName).get();
400
735
  }
401
736
  else {
402
- return this.client
403
- .proxy(this.base)(idOrCategoryName)(slug)
404
- .get();
737
+ return this.proxy(idOrCategoryName)(slug).get();
738
+ }
739
+ }
740
+ /**
741
+ * 获取最新的文章
742
+ */
743
+ getLatest() {
744
+ return this.proxy.latest.get();
745
+ }
746
+ /**
747
+ * 点赞
748
+ */
749
+ thumbsUp(id) {
750
+ return this.proxy('_thumbs-up').get({ params: { id } });
751
+ }
752
+ }
753
+
754
+ class RecentlyController {
755
+ constructor(client) {
756
+ this.client = client;
757
+ this.base = 'recently';
758
+ this.name = 'recently';
759
+ }
760
+ get proxy() {
761
+ return this.client.proxy(this.base);
762
+ }
763
+ /**
764
+ * 获取最新一条
765
+ */
766
+ getLatestOne() {
767
+ return this.proxy.latest.get();
768
+ }
769
+ getAll() {
770
+ return this.proxy.all.get();
771
+ }
772
+ getList(before, after, size) {
773
+ if (!before && !after) {
774
+ throw new Error('you can only choose `before` or `after`');
405
775
  }
776
+ return this.proxy.get({
777
+ params: {
778
+ before,
779
+ after,
780
+ size,
781
+ },
782
+ });
406
783
  }
407
784
  }
408
785
 
409
- const allClients = [NoteClient, PostClient];
410
- // export const allClientName = allClients.map((c) =>
411
- // c.name.replace('Client', '').toLowerCase(),
412
- // )
413
- // Why not code like before
414
- // In order to tree shake unnecessary client
415
- const allClientName = ['note', 'post'];
786
+ class SayController extends BaseCrudController {
787
+ constructor(client) {
788
+ super(client);
789
+ this.client = client;
790
+ this.base = 'says';
791
+ this.name = 'say';
792
+ }
793
+ get proxy() {
794
+ return this.client.proxy(this.base);
795
+ }
796
+ /**
797
+ * 获取随机一条
798
+ */
799
+ getRandom() {
800
+ return this.proxy.random.get();
801
+ }
802
+ }
803
+
804
+ class SearchController {
805
+ constructor(client) {
806
+ this.client = client;
807
+ this.base = 'search';
808
+ this.name = 'search';
809
+ }
810
+ get proxy() {
811
+ return this.client.proxy(this.base);
812
+ }
813
+ search(type, keyword, options = {}) {
814
+ return this.proxy(type).get({
815
+ params: Object.assign({ keyword }, options),
816
+ });
817
+ }
818
+ /**
819
+ * 从 algolya 搜索
820
+ * https://www.algolia.com/doc/api-reference/api-methods/search/
821
+ * @param keyword
822
+ * @param options
823
+ * @returns
824
+ */
825
+ searchByAlgolia(keyword, options) {
826
+ return this.proxy('algolia').get({ params: Object.assign({ keyword }, options) });
827
+ }
828
+ }
829
+
830
+ class UserController {
831
+ constructor(client) {
832
+ this.client = client;
833
+ this.base = 'master';
834
+ this.name = 'user';
835
+ }
836
+ get proxy() {
837
+ return this.client.proxy(this.base);
838
+ }
839
+ getMasterInfo() {
840
+ return this.proxy.get();
841
+ }
842
+ login(username, password) {
843
+ return this.proxy.login.post({
844
+ data: {
845
+ username,
846
+ password,
847
+ },
848
+ });
849
+ }
850
+ checkTokenValid(token) {
851
+ return this.proxy.check_logged.get({
852
+ params: {
853
+ token,
854
+ },
855
+ });
856
+ }
857
+ }
858
+
859
+ const allControllers = [
860
+ AggregateController,
861
+ CategoryController,
862
+ CommentController,
863
+ LinkController,
864
+ NoteController,
865
+ PageController,
866
+ PostController,
867
+ RecentlyController,
868
+ SayController,
869
+ SearchController,
870
+ UserController,
871
+ ];
872
+ const allContollerNames = [
873
+ 'aggregate',
874
+ 'category',
875
+ 'comment',
876
+ 'link',
877
+ 'note',
878
+ 'page',
879
+ 'post',
880
+ 'recently',
881
+ 'say',
882
+ 'search',
883
+ 'user',
884
+ ];
885
+
886
+ class HTTPControllerDefine {
887
+ get master() {
888
+ return this.user;
889
+ }
890
+ get friend() {
891
+ return this.link;
892
+ }
893
+ get shorthand() {
894
+ return this.recently;
895
+ }
896
+ }
416
897
 
417
898
  class RequestError extends Error {
418
899
  constructor(message, status, path, raw) {
@@ -423,8 +904,10 @@ class RequestError extends Error {
423
904
  }
424
905
  }
425
906
 
426
- class HTTPClient {
907
+ class HTTPClient extends HTTPControllerDefine {
908
+ // define all clients
427
909
  constructor(_endpoint, _instance) {
910
+ super();
428
911
  this._endpoint = _endpoint;
429
912
  this._instance = _instance;
430
913
  this._endpoint = _endpoint
@@ -435,7 +918,7 @@ class HTTPClient {
435
918
  attachRequestMethod(this);
436
919
  }
437
920
  initGetClient() {
438
- const clientsName = allClientName;
921
+ const clientsName = allContollerNames;
439
922
  for (const name of clientsName) {
440
923
  Object.defineProperty(this, name, {
441
924
  get() {
@@ -450,9 +933,9 @@ class HTTPClient {
450
933
  });
451
934
  }
452
935
  }
453
- injectClients(Clients, ...rest) {
454
- Clients = Array.isArray(Clients) ? Clients : [Clients, ...rest];
455
- for (const Client of Clients) {
936
+ injectControllers(Controller, ...rest) {
937
+ Controller = Array.isArray(Controller) ? Controller : [Controller, ...rest];
938
+ for (const Client of Controller) {
456
939
  const cl = new Client(this);
457
940
  Object.defineProperty(this, `_${cl.name.toLowerCase()}`, {
458
941
  get() {
@@ -470,7 +953,7 @@ class HTTPClient {
470
953
  return this._instance;
471
954
  }
472
955
  request(options) {
473
- return this._instance[String(options.method || 'get').toLowerCase()](options.url, options);
956
+ return this['$$' + String(options.method || 'get').toLowerCase()](options.url, options);
474
957
  }
475
958
  get proxy() {
476
959
  return this._proxy;
@@ -533,6 +1016,17 @@ class HTTPClient {
533
1016
  enumerable: false,
534
1017
  configurable: false,
535
1018
  });
1019
+ // attach request config onto response
1020
+ Object.defineProperty(transform, 'request', {
1021
+ get() {
1022
+ return {
1023
+ url,
1024
+ method: name,
1025
+ options,
1026
+ };
1027
+ },
1028
+ enumerable: false,
1029
+ });
536
1030
  return transform;
537
1031
  };
538
1032
  }
@@ -550,17 +1044,16 @@ class HTTPClient {
550
1044
  }
551
1045
  }
552
1046
  function createClient(adapter) {
553
- // TODO: add support other network lib
554
1047
  return (endpoint) => {
555
1048
  return new HTTPClient(endpoint, adapter);
556
1049
  };
557
1050
  }
558
1051
 
559
1052
  exports.HTTPClient = HTTPClient;
560
- exports.NoteClient = NoteClient;
561
- exports.PostClient = PostClient;
1053
+ exports.NoteClient = NoteController;
1054
+ exports.PostClient = PostController;
562
1055
  exports.RequestError = RequestError;
563
- exports.allClientName = allClientName;
564
- exports.allClients = allClients;
1056
+ exports.allClientName = allContollerNames;
1057
+ exports.allClients = allControllers;
565
1058
  exports.createClient = createClient;
566
1059
  //# sourceMappingURL=index.cjs.js.map