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