@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.
- package/build/index.cjs.js +526 -33
- package/build/index.cjs.js.map +1 -1
- package/build/index.cjs.min.js +1 -1
- package/build/index.cjs.min.js.map +1 -1
- package/build/index.d.ts +542 -34
- package/build/index.esm.js +523 -30
- package/build/index.esm.js.map +1 -1
- package/build/index.esm.min.js +1 -1
- package/build/index.esm.min.js.map +1 -1
- package/build/index.umd.js +526 -33
- package/build/index.umd.js.map +1 -1
- package/build/index.umd.min.js +1 -1
- package/build/index.umd.min.js.map +1 -1
- package/esm/core/attachRequest.js +29 -6
- package/esm/core/attachRequest.js.map +1 -1
- package/esm/core/client.d.ts +12 -15
- package/esm/core/client.js +21 -8
- package/esm/core/client.js.map +1 -1
- package/esm/core/controllers/aggregate.d.ts +44 -0
- package/esm/core/controllers/aggregate.js +40 -0
- package/esm/core/controllers/aggregate.js.map +1 -0
- package/esm/core/controllers/all.d.ts +14 -0
- package/esm/core/controllers/all.js +39 -0
- package/esm/core/controllers/all.js.map +1 -0
- package/esm/core/controllers/base.d.ts +29 -0
- package/esm/core/controllers/base.js +25 -0
- package/esm/core/controllers/base.js.map +1 -0
- package/esm/core/controllers/category.d.ts +44 -0
- package/esm/core/controllers/category.js +66 -0
- package/esm/core/controllers/category.js.map +1 -0
- package/esm/core/controllers/comment.d.ts +42 -0
- package/esm/core/controllers/comment.js +43 -0
- package/esm/core/controllers/comment.js.map +1 -0
- package/esm/core/controllers/index.d.ts +1 -0
- package/esm/core/controllers/index.js +2 -0
- package/esm/core/controllers/index.js.map +1 -0
- package/esm/core/controllers/link.d.ts +9 -0
- package/esm/core/controllers/link.js +10 -0
- package/esm/core/controllers/link.js.map +1 -0
- package/esm/core/controllers/note.d.ts +54 -0
- package/esm/core/controllers/note.js +54 -0
- package/esm/core/controllers/note.js.map +1 -0
- package/esm/core/controllers/page.d.ts +34 -0
- package/esm/core/controllers/page.js +33 -0
- package/esm/core/controllers/page.js.map +1 -0
- package/esm/core/controllers/post.d.ts +47 -0
- package/esm/core/controllers/post.js +42 -0
- package/esm/core/controllers/post.js.map +1 -0
- package/esm/core/controllers/recently.d.ts +33 -0
- package/esm/core/controllers/recently.js +32 -0
- package/esm/core/controllers/recently.js.map +1 -0
- package/esm/core/controllers/say.d.ts +22 -0
- package/esm/core/controllers/say.js +19 -0
- package/esm/core/controllers/say.js.map +1 -0
- package/esm/core/controllers/search.d.ts +58 -0
- package/esm/core/controllers/search.js +26 -0
- package/esm/core/controllers/search.js.map +1 -0
- package/esm/core/controllers/user.d.ts +28 -0
- package/esm/core/controllers/user.js +29 -0
- package/esm/core/controllers/user.js.map +1 -0
- package/esm/core/define.d.ts +17 -0
- package/esm/core/define.js +12 -0
- package/esm/core/define.js.map +1 -0
- package/esm/core/dtos/comment.d.ts +6 -0
- package/esm/core/dtos/comment.js +3 -0
- package/esm/core/dtos/comment.js.map +1 -0
- package/esm/core/index.d.ts +1 -1
- package/esm/core/index.js +1 -1
- package/esm/core/index.js.map +1 -1
- package/esm/index.d.ts +1 -1
- package/esm/index.js +1 -1
- package/esm/index.js.map +1 -1
- package/esm/interfaces/{client.d.ts → controller.d.ts} +1 -1
- package/esm/interfaces/controller.js +2 -0
- package/esm/interfaces/controller.js.map +1 -0
- package/esm/interfaces/instance.d.ts +3 -2
- package/esm/interfaces/options.d.ts +1 -0
- package/esm/interfaces/options.js +2 -0
- package/esm/interfaces/options.js.map +1 -0
- package/esm/interfaces/params.d.ts +4 -0
- package/esm/interfaces/params.js +2 -0
- package/esm/interfaces/params.js.map +1 -0
- package/esm/interfaces/request.d.ts +19 -9
- package/esm/models/aggregate.d.ts +47 -0
- package/esm/models/aggregate.js +6 -0
- package/esm/models/aggregate.js.map +1 -0
- package/esm/models/base.d.ts +1 -1
- package/esm/models/category.d.ts +24 -0
- package/esm/models/category.js +6 -0
- package/esm/models/category.js.map +1 -0
- package/esm/models/comment.d.ts +28 -0
- package/esm/models/comment.js +7 -0
- package/esm/models/comment.js.map +1 -0
- package/esm/models/link.d.ts +17 -0
- package/esm/models/link.js +11 -0
- package/esm/models/link.js.map +1 -0
- package/esm/models/note.d.ts +34 -0
- package/esm/models/note.js +2 -0
- package/esm/models/note.js.map +1 -0
- package/esm/models/page.d.ts +24 -0
- package/esm/models/page.js +7 -0
- package/esm/models/page.js.map +1 -0
- package/esm/models/post.d.ts +2 -7
- package/esm/models/recently.d.ts +5 -0
- package/esm/models/recently.js +2 -0
- package/esm/models/recently.js.map +1 -0
- package/esm/models/say.d.ts +6 -0
- package/esm/models/say.js +2 -0
- package/esm/models/say.js.map +1 -0
- package/esm/models/setting.d.ts +66 -0
- package/esm/models/setting.js +2 -0
- package/esm/models/setting.js.map +1 -0
- package/esm/models/user.d.ts +29 -0
- package/esm/models/user.js +2 -0
- package/esm/models/user.js.map +1 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +38 -0
- package/esm/utils/index.js.map +1 -1
- package/lib/core/attachRequest.js +29 -6
- package/lib/core/attachRequest.js.map +1 -1
- package/lib/core/client.d.ts +12 -15
- package/lib/core/client.js +21 -8
- package/lib/core/client.js.map +1 -1
- package/lib/core/controllers/aggregate.d.ts +44 -0
- package/lib/core/controllers/aggregate.js +44 -0
- package/lib/core/controllers/aggregate.js.map +1 -0
- package/lib/core/controllers/all.d.ts +14 -0
- package/lib/core/controllers/all.js +52 -0
- package/lib/core/controllers/all.js.map +1 -0
- package/lib/core/controllers/base.d.ts +29 -0
- package/lib/core/controllers/base.js +29 -0
- package/lib/core/controllers/base.js.map +1 -0
- package/lib/core/controllers/category.d.ts +44 -0
- package/lib/core/controllers/category.js +70 -0
- package/lib/core/controllers/category.js.map +1 -0
- package/lib/core/controllers/comment.d.ts +42 -0
- package/lib/core/controllers/comment.js +47 -0
- package/lib/core/controllers/comment.js.map +1 -0
- package/lib/core/controllers/index.d.ts +1 -0
- package/lib/core/{clients → controllers}/index.js +0 -3
- package/lib/core/controllers/index.js.map +1 -0
- package/lib/core/controllers/link.d.ts +9 -0
- package/lib/core/controllers/link.js +14 -0
- package/lib/core/controllers/link.js.map +1 -0
- package/lib/core/controllers/note.d.ts +54 -0
- package/lib/core/controllers/note.js +58 -0
- package/lib/core/controllers/note.js.map +1 -0
- package/lib/core/controllers/page.d.ts +34 -0
- package/lib/core/controllers/page.js +37 -0
- package/lib/core/controllers/page.js.map +1 -0
- package/lib/core/controllers/post.d.ts +47 -0
- package/lib/core/controllers/post.js +46 -0
- package/lib/core/controllers/post.js.map +1 -0
- package/lib/core/controllers/recently.d.ts +33 -0
- package/lib/core/controllers/recently.js +36 -0
- package/lib/core/controllers/recently.js.map +1 -0
- package/lib/core/controllers/say.d.ts +22 -0
- package/lib/core/controllers/say.js +23 -0
- package/lib/core/controllers/say.js.map +1 -0
- package/lib/core/controllers/search.d.ts +58 -0
- package/lib/core/controllers/search.js +30 -0
- package/lib/core/controllers/search.js.map +1 -0
- package/lib/core/controllers/user.d.ts +28 -0
- package/lib/core/controllers/user.js +33 -0
- package/lib/core/controllers/user.js.map +1 -0
- package/lib/core/define.d.ts +17 -0
- package/lib/core/define.js +16 -0
- package/lib/core/define.js.map +1 -0
- package/lib/core/dtos/comment.d.ts +6 -0
- package/lib/core/dtos/comment.js +7 -0
- package/lib/core/dtos/comment.js.map +1 -0
- package/lib/core/index.d.ts +1 -1
- package/lib/core/index.js +1 -1
- package/lib/core/index.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +4 -4
- package/lib/index.js.map +1 -1
- package/lib/interfaces/{client.d.ts → controller.d.ts} +1 -1
- package/lib/interfaces/controller.js +3 -0
- package/lib/interfaces/controller.js.map +1 -0
- package/lib/interfaces/instance.d.ts +3 -2
- package/lib/interfaces/options.d.ts +1 -0
- package/lib/interfaces/options.js +3 -0
- package/lib/interfaces/options.js.map +1 -0
- package/lib/interfaces/params.d.ts +4 -0
- package/lib/interfaces/{client.js → params.js} +1 -1
- package/lib/interfaces/params.js.map +1 -0
- package/lib/interfaces/request.d.ts +19 -9
- package/lib/models/aggregate.d.ts +47 -0
- package/lib/models/aggregate.js +9 -0
- package/lib/models/aggregate.js.map +1 -0
- package/lib/models/base.d.ts +1 -1
- package/lib/models/category.d.ts +24 -0
- package/lib/models/category.js +9 -0
- package/lib/models/category.js.map +1 -0
- package/lib/models/comment.d.ts +28 -0
- package/lib/models/comment.js +10 -0
- package/lib/models/comment.js.map +1 -0
- package/lib/models/link.d.ts +17 -0
- package/lib/models/link.js +14 -0
- package/lib/models/link.js.map +1 -0
- package/lib/models/note.d.ts +34 -0
- package/lib/models/note.js +3 -0
- package/lib/models/note.js.map +1 -0
- package/lib/models/page.d.ts +24 -0
- package/lib/models/page.js +10 -0
- package/lib/models/page.js.map +1 -0
- package/lib/models/post.d.ts +2 -7
- package/lib/models/recently.d.ts +5 -0
- package/lib/models/recently.js +3 -0
- package/lib/models/recently.js.map +1 -0
- package/lib/models/say.d.ts +6 -0
- package/lib/models/say.js +3 -0
- package/lib/models/say.js.map +1 -0
- package/lib/models/setting.d.ts +66 -0
- package/lib/models/setting.js +3 -0
- package/lib/models/setting.js.map +1 -0
- package/lib/models/user.d.ts +29 -0
- package/lib/models/user.js +3 -0
- package/lib/models/user.js.map +1 -0
- package/lib/utils/index.d.ts +4 -0
- package/lib/utils/index.js +42 -1
- package/lib/utils/index.js.map +1 -1
- package/package.json +12 -11
- package/esm/core/clients/all.d.ts +0 -3
- package/esm/core/clients/all.js +0 -9
- package/esm/core/clients/all.js.map +0 -1
- package/esm/core/clients/index.d.ts +0 -3
- package/esm/core/clients/index.js +0 -5
- package/esm/core/clients/index.js.map +0 -1
- package/esm/core/clients/note.d.ts +0 -5
- package/esm/core/clients/note.js +0 -7
- package/esm/core/clients/note.js.map +0 -1
- package/esm/core/clients/post.d.ts +0 -16
- package/esm/core/clients/post.js +0 -23
- package/esm/core/clients/post.js.map +0 -1
- package/esm/interfaces/client.js +0 -2
- package/esm/interfaces/client.js.map +0 -1
- package/lib/core/clients/all.d.ts +0 -3
- package/lib/core/clients/all.js +0 -12
- package/lib/core/clients/all.js.map +0 -1
- package/lib/core/clients/index.d.ts +0 -3
- package/lib/core/clients/index.js.map +0 -1
- package/lib/core/clients/note.d.ts +0 -5
- package/lib/core/clients/note.js +0 -11
- package/lib/core/clients/note.js.map +0 -1
- package/lib/core/clients/post.d.ts +0 -16
- package/lib/core/clients/post.js +0 -27
- package/lib/core/clients/post.js.map +0 -1
- package/lib/interfaces/client.js.map +0 -1
package/build/index.cjs.js
CHANGED
|
@@ -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
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
393
|
-
return this.client.proxy(this.base)
|
|
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.
|
|
734
|
+
return this.proxy(idOrCategoryName).get();
|
|
400
735
|
}
|
|
401
736
|
else {
|
|
402
|
-
return this.
|
|
403
|
-
|
|
404
|
-
|
|
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
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
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 =
|
|
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
|
-
|
|
454
|
-
|
|
455
|
-
for (const Client of
|
|
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
|
|
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 =
|
|
561
|
-
exports.PostClient =
|
|
1053
|
+
exports.NoteClient = NoteController;
|
|
1054
|
+
exports.PostClient = PostController;
|
|
562
1055
|
exports.RequestError = RequestError;
|
|
563
|
-
exports.allClientName =
|
|
564
|
-
exports.allClients =
|
|
1056
|
+
exports.allClientName = allContollerNames;
|
|
1057
|
+
exports.allClients = allControllers;
|
|
565
1058
|
exports.createClient = createClient;
|
|
566
1059
|
//# sourceMappingURL=index.cjs.js.map
|