@mx-space/api-client 1.0.0-beta.0 → 1.0.0-beta.1
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/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/index.min.cjs +1 -1
- package/dist/index.min.cjs.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.umd.js +1 -0
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/esm/models/link.d.ts +2 -1
- package/esm/models/link.js +1 -0
- package/esm/models/link.js.map +1 -1
- package/lib/models/link.d.ts +2 -1
- package/lib/models/link.js +1 -0
- package/lib/models/link.js.map +1 -1
- package/models/link.ts +1 -0
- package/package.json +1 -1
- package/types/models/link.d.ts +2 -1
- package/build/index.cjs.js +0 -980
- package/build/index.cjs.js.map +0 -1
- package/build/index.cjs.min.js +0 -2
- package/build/index.cjs.min.js.map +0 -1
- package/build/index.esm.min.mjs +0 -2
- package/build/index.esm.min.mjs.map +0 -1
- package/build/index.esm.mjs +0 -958
- package/build/index.esm.mjs.map +0 -1
- package/build/index.umd.js +0 -986
- package/build/index.umd.js.map +0 -1
- package/build/index.umd.min.js +0 -2
- package/build/index.umd.min.js.map +0 -1
package/build/index.umd.js
DELETED
|
@@ -1,986 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@mx-space/api-client"] = {}));
|
|
5
|
-
})(this, (function (exports) { 'use strict';
|
|
6
|
-
|
|
7
|
-
const isPlainObject = (obj) => isObject(obj) &&
|
|
8
|
-
Object.prototype.toString.call(obj) === '[object Object]' &&
|
|
9
|
-
Object.getPrototypeOf(obj) === Object.prototype;
|
|
10
|
-
const sortOrderToNumber = (order) => {
|
|
11
|
-
return ({
|
|
12
|
-
asc: 1,
|
|
13
|
-
desc: -1,
|
|
14
|
-
}[order] || 1);
|
|
15
|
-
};
|
|
16
|
-
const isObject = (obj) => obj && typeof obj === 'object';
|
|
17
|
-
const destructureData = (payload) => {
|
|
18
|
-
if (typeof payload !== 'object') {
|
|
19
|
-
return payload;
|
|
20
|
-
}
|
|
21
|
-
if (payload === null) {
|
|
22
|
-
return payload;
|
|
23
|
-
}
|
|
24
|
-
const data = payload.data;
|
|
25
|
-
const dataIsPlainObject = isPlainObject(data);
|
|
26
|
-
if (dataIsPlainObject && Object.keys(payload).length === 1) {
|
|
27
|
-
const d = Object.assign({}, data);
|
|
28
|
-
// attach raw onto new data
|
|
29
|
-
attachRawFromOneToAnthor(payload, d);
|
|
30
|
-
return d;
|
|
31
|
-
}
|
|
32
|
-
return payload;
|
|
33
|
-
};
|
|
34
|
-
const attachRawFromOneToAnthor = (from, to) => {
|
|
35
|
-
if (!from || !isObject(to)) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
from.$raw &&
|
|
39
|
-
Object.defineProperty(to, '$raw', {
|
|
40
|
-
value: { ...from.$raw },
|
|
41
|
-
enumerable: false,
|
|
42
|
-
});
|
|
43
|
-
from.$request &&
|
|
44
|
-
Object.defineProperty(to, '$request', {
|
|
45
|
-
value: { ...from.$request },
|
|
46
|
-
enumerable: false,
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
// @ts-nocheck
|
|
51
|
-
// @copy: https://github.com/sindresorhus/auto-bind/blob/main/index.js
|
|
52
|
-
// Gets all non-builtin properties up the prototype chain.
|
|
53
|
-
const getAllProperties = (object) => {
|
|
54
|
-
const properties = new Set();
|
|
55
|
-
do {
|
|
56
|
-
for (const key of Reflect.ownKeys(object)) {
|
|
57
|
-
properties.add([object, key]);
|
|
58
|
-
}
|
|
59
|
-
} while ((object = Reflect.getPrototypeOf(object)) &&
|
|
60
|
-
object !== Object.prototype);
|
|
61
|
-
return properties;
|
|
62
|
-
};
|
|
63
|
-
function autoBind(self, { include, exclude } = {}) {
|
|
64
|
-
const filter = (key) => {
|
|
65
|
-
const match = (pattern) => typeof pattern === 'string' ? key === pattern : pattern.test(key);
|
|
66
|
-
if (include) {
|
|
67
|
-
return include.some(match);
|
|
68
|
-
}
|
|
69
|
-
if (exclude) {
|
|
70
|
-
return !exclude.some(match);
|
|
71
|
-
}
|
|
72
|
-
return true;
|
|
73
|
-
};
|
|
74
|
-
for (const [object, key] of getAllProperties(self.constructor.prototype)) {
|
|
75
|
-
if (key === 'constructor' || !filter(key)) {
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
|
|
79
|
-
if (descriptor && typeof descriptor.value === 'function') {
|
|
80
|
-
self[key] = self[key].bind(self);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return self;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
class AggregateController {
|
|
87
|
-
constructor(client) {
|
|
88
|
-
this.client = client;
|
|
89
|
-
this.base = 'aggregate';
|
|
90
|
-
this.name = 'aggregate';
|
|
91
|
-
autoBind(this);
|
|
92
|
-
}
|
|
93
|
-
get proxy() {
|
|
94
|
-
return this.client.proxy(this.base);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* 获取聚合数据
|
|
98
|
-
*/
|
|
99
|
-
getAggregateData() {
|
|
100
|
-
return this.proxy.get();
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* 获取最新发布的内容
|
|
104
|
-
*/
|
|
105
|
-
getTop(size = 5) {
|
|
106
|
-
return this.proxy.top.get({ params: { size } });
|
|
107
|
-
}
|
|
108
|
-
getTimeline(options) {
|
|
109
|
-
const { sort, type, year } = options || {};
|
|
110
|
-
return this.proxy.timeline.get({
|
|
111
|
-
params: {
|
|
112
|
-
sort: sort && sortOrderToNumber(sort),
|
|
113
|
-
type,
|
|
114
|
-
year,
|
|
115
|
-
},
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* 获取聚合数据统计
|
|
120
|
-
*/
|
|
121
|
-
getStat() {
|
|
122
|
-
return this.proxy.stat.get();
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
class RequestError extends Error {
|
|
127
|
-
constructor(message, status, path, raw) {
|
|
128
|
-
super(message);
|
|
129
|
-
this.status = status;
|
|
130
|
-
this.path = path;
|
|
131
|
-
this.raw = raw;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
exports.CategoryType = void 0;
|
|
136
|
-
(function (CategoryType) {
|
|
137
|
-
CategoryType[CategoryType["Category"] = 0] = "Category";
|
|
138
|
-
CategoryType[CategoryType["Tag"] = 1] = "Tag";
|
|
139
|
-
})(exports.CategoryType || (exports.CategoryType = {}));
|
|
140
|
-
|
|
141
|
-
class CategoryController {
|
|
142
|
-
constructor(client) {
|
|
143
|
-
this.client = client;
|
|
144
|
-
this.name = 'category';
|
|
145
|
-
this.base = 'categories';
|
|
146
|
-
autoBind(this);
|
|
147
|
-
}
|
|
148
|
-
get proxy() {
|
|
149
|
-
return this.client.proxy(this.base);
|
|
150
|
-
}
|
|
151
|
-
getAllCategories() {
|
|
152
|
-
return this.proxy.get({
|
|
153
|
-
params: {
|
|
154
|
-
type: exports.CategoryType.Category,
|
|
155
|
-
},
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
getAllTags() {
|
|
159
|
-
return this.proxy.get({
|
|
160
|
-
params: {
|
|
161
|
-
type: exports.CategoryType.Tag,
|
|
162
|
-
},
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
async getCategoryDetail(ids) {
|
|
166
|
-
if (typeof ids === 'string') {
|
|
167
|
-
const data = await this.proxy.get({
|
|
168
|
-
params: {
|
|
169
|
-
ids,
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
const result = Object.values(data.entries)[0];
|
|
173
|
-
attachRawFromOneToAnthor(data, result);
|
|
174
|
-
return result;
|
|
175
|
-
}
|
|
176
|
-
else if (Array.isArray(ids)) {
|
|
177
|
-
const data = await this.proxy.get({
|
|
178
|
-
params: {
|
|
179
|
-
ids: ids.join(','),
|
|
180
|
-
},
|
|
181
|
-
});
|
|
182
|
-
const entries = data?.entries;
|
|
183
|
-
if (!entries) {
|
|
184
|
-
throw new RequestError('data structure error', 500, data.$request.path, data);
|
|
185
|
-
}
|
|
186
|
-
const map = new Map(Object.entries(entries).map(([id, value]) => [id.toLowerCase(), value]));
|
|
187
|
-
attachRawFromOneToAnthor(data, map);
|
|
188
|
-
return map;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
async getCategoryByIdOrSlug(idOrSlug) {
|
|
192
|
-
const res = await this.proxy(idOrSlug).get();
|
|
193
|
-
return destructureData(res);
|
|
194
|
-
}
|
|
195
|
-
async getTagByName(name) {
|
|
196
|
-
const res = await this.proxy(name).get({
|
|
197
|
-
params: {
|
|
198
|
-
tag: 1,
|
|
199
|
-
},
|
|
200
|
-
});
|
|
201
|
-
return res;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
class CommentController {
|
|
206
|
-
constructor(client) {
|
|
207
|
-
this.client = client;
|
|
208
|
-
this.base = 'comments';
|
|
209
|
-
this.name = 'comment';
|
|
210
|
-
autoBind(this);
|
|
211
|
-
}
|
|
212
|
-
get proxy() {
|
|
213
|
-
return this.client.proxy(this.base);
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* 根据 comment id 获取评论, 包括子评论
|
|
217
|
-
*/
|
|
218
|
-
getById(id) {
|
|
219
|
-
return this.proxy(id).get();
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* 获取文章的评论列表
|
|
223
|
-
* @param refId 文章 Id
|
|
224
|
-
*/
|
|
225
|
-
getByRefId(refId, pagination = {}) {
|
|
226
|
-
const { page, size } = pagination;
|
|
227
|
-
return this.proxy
|
|
228
|
-
.ref(refId)
|
|
229
|
-
.get({
|
|
230
|
-
params: { page: page || 1, size: size || 10 },
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* 评论
|
|
235
|
-
*/
|
|
236
|
-
comment(refId, data) {
|
|
237
|
-
return this.proxy(refId).post({
|
|
238
|
-
data,
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* 回复评论
|
|
243
|
-
*/
|
|
244
|
-
reply(commentId, data) {
|
|
245
|
-
return this.proxy.reply(commentId).post({
|
|
246
|
-
data,
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
class BaseCrudController {
|
|
252
|
-
constructor(client) {
|
|
253
|
-
this.client = client;
|
|
254
|
-
autoBind(this);
|
|
255
|
-
}
|
|
256
|
-
get proxy() {
|
|
257
|
-
return this.client.proxy(this.base);
|
|
258
|
-
}
|
|
259
|
-
getById(id) {
|
|
260
|
-
return this.proxy(id).get();
|
|
261
|
-
}
|
|
262
|
-
getAll() {
|
|
263
|
-
return this.proxy.all.get();
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* 带分页的查询
|
|
267
|
-
* @param page
|
|
268
|
-
* @param perPage
|
|
269
|
-
*/
|
|
270
|
-
getAllPaginated(page, perPage, sortOption) {
|
|
271
|
-
return this.proxy.get({
|
|
272
|
-
params: { page, size: perPage, ...sortOption },
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
class LinkController extends BaseCrudController {
|
|
278
|
-
constructor(client) {
|
|
279
|
-
super(client);
|
|
280
|
-
this.client = client;
|
|
281
|
-
this.name = ['link', 'friend'];
|
|
282
|
-
this.base = 'links';
|
|
283
|
-
autoBind(this);
|
|
284
|
-
}
|
|
285
|
-
// 是否可以申请友链
|
|
286
|
-
async canApplyLink() {
|
|
287
|
-
const { can } = await this.proxy.audit.get();
|
|
288
|
-
return can;
|
|
289
|
-
}
|
|
290
|
-
// 申请友链
|
|
291
|
-
async applyLink(data) {
|
|
292
|
-
return await this.proxy.audit.post({ data });
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
class NoteController {
|
|
297
|
-
constructor(client) {
|
|
298
|
-
this.client = client;
|
|
299
|
-
this.base = 'notes';
|
|
300
|
-
this.name = 'note';
|
|
301
|
-
autoBind(this);
|
|
302
|
-
}
|
|
303
|
-
get proxy() {
|
|
304
|
-
return this.client.proxy(this.base);
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* 最新日记
|
|
308
|
-
*/
|
|
309
|
-
getLatest() {
|
|
310
|
-
return this.proxy.latest.get();
|
|
311
|
-
}
|
|
312
|
-
getNoteById(...rest) {
|
|
313
|
-
const [id, password = undefined, singleResult = false] = rest;
|
|
314
|
-
if (typeof id === 'number') {
|
|
315
|
-
return this.proxy.nid(id.toString()).get({
|
|
316
|
-
params: { password, single: singleResult ? '1' : undefined },
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
return this.proxy(id).get();
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* 日记列表分页
|
|
325
|
-
*/
|
|
326
|
-
getList(page = 1, perPage = 10, options = {}) {
|
|
327
|
-
const { select, sortBy, sortOrder, year } = options;
|
|
328
|
-
return this.proxy.get({
|
|
329
|
-
params: {
|
|
330
|
-
page,
|
|
331
|
-
size: perPage,
|
|
332
|
-
select: select?.join(' '),
|
|
333
|
-
sortBy,
|
|
334
|
-
sortOrder,
|
|
335
|
-
year,
|
|
336
|
-
},
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* 获取当前日记的上下各 n / 2 篇日记
|
|
341
|
-
*/
|
|
342
|
-
getMiddleList(id, size = 5) {
|
|
343
|
-
return this.proxy.list(id).get({
|
|
344
|
-
params: { size },
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* 喜欢这篇日记
|
|
349
|
-
*/
|
|
350
|
-
likeIt(id) {
|
|
351
|
-
return this.proxy.like(id).get();
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* 获取专栏内的所有日记
|
|
355
|
-
*/
|
|
356
|
-
getNoteByTopicId(topicId, page = 1, size = 10, sortOptions = {}) {
|
|
357
|
-
return this.proxy.topics(topicId).get({
|
|
358
|
-
params: { page, size, ...sortOptions },
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
class PageController {
|
|
364
|
-
constructor(client) {
|
|
365
|
-
this.client = client;
|
|
366
|
-
this.base = 'pages';
|
|
367
|
-
this.name = 'page';
|
|
368
|
-
autoBind(this);
|
|
369
|
-
}
|
|
370
|
-
get proxy() {
|
|
371
|
-
return this.client.proxy(this.base);
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* 页面列表
|
|
375
|
-
*/
|
|
376
|
-
getList(page = 1, perPage = 10, options = {}) {
|
|
377
|
-
const { select, sortBy, sortOrder } = options;
|
|
378
|
-
return this.proxy.get({
|
|
379
|
-
params: {
|
|
380
|
-
page,
|
|
381
|
-
size: perPage,
|
|
382
|
-
select: select?.join(' '),
|
|
383
|
-
sortBy,
|
|
384
|
-
sortOrder,
|
|
385
|
-
},
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* 页面详情
|
|
390
|
-
*/
|
|
391
|
-
getById(id) {
|
|
392
|
-
return this.proxy(id).get();
|
|
393
|
-
}
|
|
394
|
-
/**
|
|
395
|
-
* 根据路径获取页面
|
|
396
|
-
* @param slug 路径
|
|
397
|
-
* @returns
|
|
398
|
-
*/
|
|
399
|
-
getBySlug(slug) {
|
|
400
|
-
return this.proxy.slug(slug).get({});
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
class PostController {
|
|
405
|
-
constructor(client) {
|
|
406
|
-
this.client = client;
|
|
407
|
-
this.base = 'posts';
|
|
408
|
-
this.name = 'post';
|
|
409
|
-
autoBind(this);
|
|
410
|
-
}
|
|
411
|
-
get proxy() {
|
|
412
|
-
return this.client.proxy(this.base);
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* 获取文章列表分页
|
|
416
|
-
* @param page
|
|
417
|
-
* @param perPage
|
|
418
|
-
* @returns
|
|
419
|
-
*/
|
|
420
|
-
getList(page = 1, perPage = 10, options = {}) {
|
|
421
|
-
const { select, sortBy, sortOrder, year } = options;
|
|
422
|
-
return this.proxy.get({
|
|
423
|
-
params: {
|
|
424
|
-
page,
|
|
425
|
-
size: perPage,
|
|
426
|
-
select: select?.join(' '),
|
|
427
|
-
sortBy,
|
|
428
|
-
sortOrder,
|
|
429
|
-
year,
|
|
430
|
-
},
|
|
431
|
-
});
|
|
432
|
-
}
|
|
433
|
-
getPost(idOrCategoryName, slug) {
|
|
434
|
-
if (arguments.length == 1) {
|
|
435
|
-
return this.proxy(idOrCategoryName).get();
|
|
436
|
-
}
|
|
437
|
-
else {
|
|
438
|
-
return this.proxy(idOrCategoryName)(slug).get();
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* 获取最新的文章
|
|
443
|
-
*/
|
|
444
|
-
getLatest() {
|
|
445
|
-
return this.proxy.latest.get();
|
|
446
|
-
}
|
|
447
|
-
/**
|
|
448
|
-
* 点赞
|
|
449
|
-
*/
|
|
450
|
-
thumbsUp(id) {
|
|
451
|
-
return this.proxy('_thumbs-up').get({ params: { id } });
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
class ProjectController extends BaseCrudController {
|
|
456
|
-
constructor(client) {
|
|
457
|
-
super(client);
|
|
458
|
-
this.client = client;
|
|
459
|
-
this.base = 'projects';
|
|
460
|
-
this.name = 'project';
|
|
461
|
-
autoBind(this);
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
class RecentlyController {
|
|
466
|
-
constructor(client) {
|
|
467
|
-
this.client = client;
|
|
468
|
-
this.base = 'recently';
|
|
469
|
-
this.name = ['recently', 'shorthand'];
|
|
470
|
-
autoBind(this);
|
|
471
|
-
}
|
|
472
|
-
get proxy() {
|
|
473
|
-
return this.client.proxy(this.base);
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* 获取最新一条
|
|
477
|
-
*/
|
|
478
|
-
getLatestOne() {
|
|
479
|
-
return this.proxy.latest.get();
|
|
480
|
-
}
|
|
481
|
-
getAll() {
|
|
482
|
-
return this.proxy.all.get();
|
|
483
|
-
}
|
|
484
|
-
getList(before, after, size) {
|
|
485
|
-
return this.proxy.get({
|
|
486
|
-
params: {
|
|
487
|
-
before,
|
|
488
|
-
after,
|
|
489
|
-
size,
|
|
490
|
-
},
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
class SayController extends BaseCrudController {
|
|
496
|
-
constructor(client) {
|
|
497
|
-
super(client);
|
|
498
|
-
this.client = client;
|
|
499
|
-
this.base = 'says';
|
|
500
|
-
this.name = 'say';
|
|
501
|
-
autoBind(this);
|
|
502
|
-
}
|
|
503
|
-
get proxy() {
|
|
504
|
-
return this.client.proxy(this.base);
|
|
505
|
-
}
|
|
506
|
-
/**
|
|
507
|
-
* 获取随机一条
|
|
508
|
-
*/
|
|
509
|
-
getRandom() {
|
|
510
|
-
return this.proxy.random.get();
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
class SearchController {
|
|
515
|
-
constructor(client) {
|
|
516
|
-
this.client = client;
|
|
517
|
-
this.base = 'search';
|
|
518
|
-
this.name = 'search';
|
|
519
|
-
autoBind(this);
|
|
520
|
-
}
|
|
521
|
-
get proxy() {
|
|
522
|
-
return this.client.proxy(this.base);
|
|
523
|
-
}
|
|
524
|
-
search(type, keyword, options = {}) {
|
|
525
|
-
return this.proxy(type).get({
|
|
526
|
-
params: { keyword, ...options },
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
/**
|
|
530
|
-
* 从 algolya 搜索
|
|
531
|
-
* https://www.algolia.com/doc/api-reference/api-methods/search/
|
|
532
|
-
* @param keyword
|
|
533
|
-
* @param options
|
|
534
|
-
* @returns
|
|
535
|
-
*/
|
|
536
|
-
searchByAlgolia(keyword, options) {
|
|
537
|
-
return this.proxy('algolia').get({ params: { keyword, ...options } });
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
class ServerlessController {
|
|
542
|
-
constructor(client) {
|
|
543
|
-
this.client = client;
|
|
544
|
-
this.base = 'serverless';
|
|
545
|
-
this.name = 'serverless';
|
|
546
|
-
autoBind(this);
|
|
547
|
-
}
|
|
548
|
-
get proxy() {
|
|
549
|
-
return this.client.proxy(this.base);
|
|
550
|
-
}
|
|
551
|
-
getByReferenceAndName(reference, name) {
|
|
552
|
-
return this.proxy(reference)(name).get();
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
class SnippetController {
|
|
557
|
-
constructor(client) {
|
|
558
|
-
this.client = client;
|
|
559
|
-
this.base = 'snippets';
|
|
560
|
-
this.name = 'snippet';
|
|
561
|
-
autoBind(this);
|
|
562
|
-
}
|
|
563
|
-
get proxy() {
|
|
564
|
-
return this.client.proxy(this.base);
|
|
565
|
-
}
|
|
566
|
-
// getById(id: string) {
|
|
567
|
-
// return this.proxy(id).get<Omit<SnippetModel, 'data'>>()
|
|
568
|
-
// }
|
|
569
|
-
getByReferenceAndName(reference, name) {
|
|
570
|
-
return this.proxy(reference)(name).get();
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
class TopicController extends BaseCrudController {
|
|
575
|
-
constructor(client) {
|
|
576
|
-
super(client);
|
|
577
|
-
this.client = client;
|
|
578
|
-
this.base = 'topics';
|
|
579
|
-
this.name = 'topic';
|
|
580
|
-
autoBind(this);
|
|
581
|
-
}
|
|
582
|
-
get proxy() {
|
|
583
|
-
return this.client.proxy(this.base);
|
|
584
|
-
}
|
|
585
|
-
getTopicBySlug(slug) {
|
|
586
|
-
return this.proxy.slug(slug).get();
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
class UserController {
|
|
591
|
-
constructor(client) {
|
|
592
|
-
this.client = client;
|
|
593
|
-
this.base = 'master';
|
|
594
|
-
this.name = ['user', 'master'];
|
|
595
|
-
autoBind(this);
|
|
596
|
-
}
|
|
597
|
-
get proxy() {
|
|
598
|
-
return this.client.proxy(this.base);
|
|
599
|
-
}
|
|
600
|
-
getMasterInfo() {
|
|
601
|
-
return this.proxy.get();
|
|
602
|
-
}
|
|
603
|
-
login(username, password) {
|
|
604
|
-
return this.proxy.login.post({
|
|
605
|
-
data: {
|
|
606
|
-
username,
|
|
607
|
-
password,
|
|
608
|
-
},
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
loginWithToken(token) {
|
|
612
|
-
return this.proxy.login.put({
|
|
613
|
-
params: token
|
|
614
|
-
? {
|
|
615
|
-
token: `bearer ${token.replace(/^Bearer\s/i, '')}`,
|
|
616
|
-
}
|
|
617
|
-
: undefined,
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
checkTokenValid(token) {
|
|
621
|
-
return this.proxy.check_logged.get({
|
|
622
|
-
params: {
|
|
623
|
-
token: `bearer ${token.replace(/^Bearer\s/i, '')}`,
|
|
624
|
-
},
|
|
625
|
-
});
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
const allControllers = [
|
|
630
|
-
AggregateController,
|
|
631
|
-
CategoryController,
|
|
632
|
-
CommentController,
|
|
633
|
-
LinkController,
|
|
634
|
-
NoteController,
|
|
635
|
-
PageController,
|
|
636
|
-
PostController,
|
|
637
|
-
ProjectController,
|
|
638
|
-
RecentlyController,
|
|
639
|
-
TopicController,
|
|
640
|
-
SayController,
|
|
641
|
-
SearchController,
|
|
642
|
-
SnippetController,
|
|
643
|
-
ServerlessController,
|
|
644
|
-
UserController,
|
|
645
|
-
];
|
|
646
|
-
const allContollerNames = [
|
|
647
|
-
'aggregate',
|
|
648
|
-
'category',
|
|
649
|
-
'comment',
|
|
650
|
-
'link',
|
|
651
|
-
'note',
|
|
652
|
-
'page',
|
|
653
|
-
'post',
|
|
654
|
-
'project',
|
|
655
|
-
'topic',
|
|
656
|
-
'recently',
|
|
657
|
-
'say',
|
|
658
|
-
'search',
|
|
659
|
-
'snippet',
|
|
660
|
-
'serverless',
|
|
661
|
-
'user',
|
|
662
|
-
// alias,
|
|
663
|
-
'friend',
|
|
664
|
-
'master',
|
|
665
|
-
'shorthand',
|
|
666
|
-
];
|
|
667
|
-
|
|
668
|
-
const camelcaseKeys = (obj) => {
|
|
669
|
-
if (Array.isArray(obj)) {
|
|
670
|
-
return obj.map((x) => camelcaseKeys(x));
|
|
671
|
-
}
|
|
672
|
-
if (isPlainObject(obj)) {
|
|
673
|
-
return Object.keys(obj).reduce((result, key) => {
|
|
674
|
-
result[camelcase(key)] = camelcaseKeys(obj[key]);
|
|
675
|
-
return result;
|
|
676
|
-
}, {});
|
|
677
|
-
}
|
|
678
|
-
return obj;
|
|
679
|
-
};
|
|
680
|
-
function camelcase(str) {
|
|
681
|
-
return str.replace(/([-_][a-z])/gi, ($1) => {
|
|
682
|
-
return $1.toUpperCase().replace('-', '').replace('_', '');
|
|
683
|
-
});
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
const resolveFullPath = (endpoint, path) => {
|
|
687
|
-
if (!path.startsWith('/')) {
|
|
688
|
-
path = `/${path}`;
|
|
689
|
-
}
|
|
690
|
-
return `${endpoint}${path}`;
|
|
691
|
-
};
|
|
692
|
-
|
|
693
|
-
function attachRequestMethod(target) {
|
|
694
|
-
Object.defineProperty(target, '$$get', {
|
|
695
|
-
value(url, options) {
|
|
696
|
-
// HINT: method get only accept search params;
|
|
697
|
-
const { params = {}, ...rest } = options;
|
|
698
|
-
const qs = handleSearchParams(params);
|
|
699
|
-
return target.instance.get(`${url}${qs ? `${`?${qs}`}` : ''}`, rest);
|
|
700
|
-
},
|
|
701
|
-
});
|
|
702
|
-
['put', 'post', 'patch', 'delete'].forEach((method) => {
|
|
703
|
-
Object.defineProperty(target, `$$${method}`, {
|
|
704
|
-
value(path, options) {
|
|
705
|
-
return target.instance[method](path, options);
|
|
706
|
-
},
|
|
707
|
-
});
|
|
708
|
-
});
|
|
709
|
-
}
|
|
710
|
-
// FIXME: only support string value
|
|
711
|
-
function handleSearchParams(obj) {
|
|
712
|
-
if (!obj && typeof obj !== 'object') {
|
|
713
|
-
throw new TypeError('params must be object.');
|
|
714
|
-
}
|
|
715
|
-
if (obj instanceof URLSearchParams) {
|
|
716
|
-
return obj.toString();
|
|
717
|
-
}
|
|
718
|
-
const search = new URLSearchParams();
|
|
719
|
-
Object.entries(obj).forEach(([k, v]) => {
|
|
720
|
-
if (typeof v === 'undefined' ||
|
|
721
|
-
Object.prototype.toString.call(v) === '[object Null]') {
|
|
722
|
-
return;
|
|
723
|
-
}
|
|
724
|
-
search.set(k, v);
|
|
725
|
-
});
|
|
726
|
-
return search.toString();
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
const methodPrefix = '_$';
|
|
730
|
-
class HTTPClient {
|
|
731
|
-
constructor(_endpoint, _adaptor, options = {}) {
|
|
732
|
-
this._endpoint = _endpoint;
|
|
733
|
-
this._adaptor = _adaptor;
|
|
734
|
-
this.options = options;
|
|
735
|
-
this._endpoint = _endpoint
|
|
736
|
-
.replace(/\/*$/, '')
|
|
737
|
-
.replace('localhost', '127.0.0.1');
|
|
738
|
-
this._proxy = this.buildRoute(this)();
|
|
739
|
-
options.transformResponse =
|
|
740
|
-
options.transformResponse || ((data) => camelcaseKeys(data));
|
|
741
|
-
this.initGetClient();
|
|
742
|
-
attachRequestMethod(this);
|
|
743
|
-
}
|
|
744
|
-
initGetClient() {
|
|
745
|
-
for (const name of allContollerNames) {
|
|
746
|
-
Object.defineProperty(this, name, {
|
|
747
|
-
get() {
|
|
748
|
-
const client = Reflect.get(this, `${methodPrefix}${name}`);
|
|
749
|
-
if (!client) {
|
|
750
|
-
throw new ReferenceError(`${name.charAt(0).toUpperCase() + name.slice(1)} Client not inject yet, please inject with client.injectClients(...)`);
|
|
751
|
-
}
|
|
752
|
-
return client;
|
|
753
|
-
},
|
|
754
|
-
configurable: false,
|
|
755
|
-
enumerable: false,
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
injectControllers(Controller, ...rest) {
|
|
760
|
-
Controller = Array.isArray(Controller) ? Controller : [Controller, ...rest];
|
|
761
|
-
for (const Client of Controller) {
|
|
762
|
-
const cl = new Client(this);
|
|
763
|
-
if (Array.isArray(cl.name)) {
|
|
764
|
-
for (const name of cl.name) {
|
|
765
|
-
attach.call(this, name, cl);
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
else {
|
|
769
|
-
attach.call(this, cl.name, cl);
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
function attach(name, cl) {
|
|
773
|
-
Object.defineProperty(this, `${methodPrefix}${name.toLowerCase()}`, {
|
|
774
|
-
get() {
|
|
775
|
-
return cl;
|
|
776
|
-
},
|
|
777
|
-
enumerable: false,
|
|
778
|
-
configurable: false,
|
|
779
|
-
});
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
get endpoint() {
|
|
783
|
-
return this._endpoint;
|
|
784
|
-
}
|
|
785
|
-
get instance() {
|
|
786
|
-
return this._adaptor;
|
|
787
|
-
}
|
|
788
|
-
request(options) {
|
|
789
|
-
return this[`$$${String(options.method || 'get').toLowerCase()}`](options.url, options);
|
|
790
|
-
}
|
|
791
|
-
get proxy() {
|
|
792
|
-
return this._proxy;
|
|
793
|
-
}
|
|
794
|
-
buildRoute(manager) {
|
|
795
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
796
|
-
const noop = () => { };
|
|
797
|
-
const methods = ['get', 'post', 'delete', 'patch', 'put'];
|
|
798
|
-
const reflectors = [
|
|
799
|
-
'toString',
|
|
800
|
-
'valueOf',
|
|
801
|
-
'inspect',
|
|
802
|
-
'constructor',
|
|
803
|
-
Symbol.toPrimitive,
|
|
804
|
-
];
|
|
805
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
806
|
-
const that = this;
|
|
807
|
-
return () => {
|
|
808
|
-
const route = [''];
|
|
809
|
-
const handler = {
|
|
810
|
-
get(target, name) {
|
|
811
|
-
if (reflectors.includes(name))
|
|
812
|
-
return (withBase) => {
|
|
813
|
-
if (withBase) {
|
|
814
|
-
const path = resolveFullPath(that.endpoint, route.join('/'));
|
|
815
|
-
route.length = 0;
|
|
816
|
-
return path;
|
|
817
|
-
}
|
|
818
|
-
else {
|
|
819
|
-
const path = route.join('/');
|
|
820
|
-
route.length = 0;
|
|
821
|
-
return path.startsWith('/') ? path : `/${path}`;
|
|
822
|
-
}
|
|
823
|
-
};
|
|
824
|
-
if (methods.includes(name)) {
|
|
825
|
-
return async (options) => {
|
|
826
|
-
const url = resolveFullPath(that.endpoint, route.join('/'));
|
|
827
|
-
route.length = 0;
|
|
828
|
-
let res;
|
|
829
|
-
try {
|
|
830
|
-
res = await manager.request({
|
|
831
|
-
method: name,
|
|
832
|
-
...options,
|
|
833
|
-
url,
|
|
834
|
-
});
|
|
835
|
-
}
|
|
836
|
-
catch (e) {
|
|
837
|
-
let message = e.message;
|
|
838
|
-
let code = e.code ||
|
|
839
|
-
e.status ||
|
|
840
|
-
e.statusCode ||
|
|
841
|
-
e.response?.status ||
|
|
842
|
-
e.response?.statusCode ||
|
|
843
|
-
e.response?.code ||
|
|
844
|
-
500;
|
|
845
|
-
if (that.options.getCodeMessageFromException) {
|
|
846
|
-
const errorInfo = that.options.getCodeMessageFromException(e);
|
|
847
|
-
message = errorInfo.message || message;
|
|
848
|
-
code = errorInfo.code || code;
|
|
849
|
-
}
|
|
850
|
-
throw that.options.customThrowResponseError
|
|
851
|
-
? that.options.customThrowResponseError(e)
|
|
852
|
-
: new RequestError(message, code, url, e);
|
|
853
|
-
}
|
|
854
|
-
const data = res.data;
|
|
855
|
-
if (!data) {
|
|
856
|
-
return null;
|
|
857
|
-
}
|
|
858
|
-
const transform = (Array.isArray(data) || isPlainObject(data)) &&
|
|
859
|
-
that.options.transformResponse
|
|
860
|
-
? that.options.transformResponse(data)
|
|
861
|
-
: data;
|
|
862
|
-
if (transform && typeof transform === 'object') {
|
|
863
|
-
Object.defineProperty(transform, '$raw', {
|
|
864
|
-
get() {
|
|
865
|
-
return res;
|
|
866
|
-
},
|
|
867
|
-
enumerable: false,
|
|
868
|
-
configurable: false,
|
|
869
|
-
});
|
|
870
|
-
// attach request config onto response
|
|
871
|
-
Object.defineProperty(transform, '$request', {
|
|
872
|
-
get() {
|
|
873
|
-
return {
|
|
874
|
-
url,
|
|
875
|
-
method: name,
|
|
876
|
-
options,
|
|
877
|
-
};
|
|
878
|
-
},
|
|
879
|
-
enumerable: false,
|
|
880
|
-
});
|
|
881
|
-
}
|
|
882
|
-
return transform;
|
|
883
|
-
};
|
|
884
|
-
}
|
|
885
|
-
route.push(name);
|
|
886
|
-
return new Proxy(noop, handler);
|
|
887
|
-
},
|
|
888
|
-
// @ts-ignore
|
|
889
|
-
apply(target, _, args) {
|
|
890
|
-
route.push(...args.filter((x) => x !== null));
|
|
891
|
-
return new Proxy(noop, handler);
|
|
892
|
-
},
|
|
893
|
-
};
|
|
894
|
-
return new Proxy(noop, handler);
|
|
895
|
-
};
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
function createClient(adapter) {
|
|
899
|
-
return (endpoint, options) => {
|
|
900
|
-
const client = new HTTPClient(endpoint, adapter, options);
|
|
901
|
-
const { controllers } = options || {};
|
|
902
|
-
if (controllers) {
|
|
903
|
-
client.injectControllers(controllers);
|
|
904
|
-
}
|
|
905
|
-
return client;
|
|
906
|
-
};
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
exports.TimelineType = void 0;
|
|
910
|
-
(function (TimelineType) {
|
|
911
|
-
TimelineType[TimelineType["Post"] = 0] = "Post";
|
|
912
|
-
TimelineType[TimelineType["Note"] = 1] = "Note";
|
|
913
|
-
})(exports.TimelineType || (exports.TimelineType = {}));
|
|
914
|
-
|
|
915
|
-
exports.RefType = void 0;
|
|
916
|
-
(function (RefType) {
|
|
917
|
-
RefType["Page"] = "Page";
|
|
918
|
-
RefType["Post"] = "Post";
|
|
919
|
-
RefType["Note"] = "Note";
|
|
920
|
-
})(exports.RefType || (exports.RefType = {}));
|
|
921
|
-
exports.CommentState = void 0;
|
|
922
|
-
(function (CommentState) {
|
|
923
|
-
CommentState[CommentState["Unread"] = 0] = "Unread";
|
|
924
|
-
CommentState[CommentState["Read"] = 1] = "Read";
|
|
925
|
-
CommentState[CommentState["Junk"] = 2] = "Junk";
|
|
926
|
-
})(exports.CommentState || (exports.CommentState = {}));
|
|
927
|
-
|
|
928
|
-
exports.LinkType = void 0;
|
|
929
|
-
(function (LinkType) {
|
|
930
|
-
LinkType[LinkType["Friend"] = 0] = "Friend";
|
|
931
|
-
LinkType[LinkType["Collection"] = 1] = "Collection";
|
|
932
|
-
})(exports.LinkType || (exports.LinkType = {}));
|
|
933
|
-
exports.LinkState = void 0;
|
|
934
|
-
(function (LinkState) {
|
|
935
|
-
LinkState[LinkState["Pass"] = 0] = "Pass";
|
|
936
|
-
LinkState[LinkState["Audit"] = 1] = "Audit";
|
|
937
|
-
LinkState[LinkState["Outdate"] = 2] = "Outdate";
|
|
938
|
-
LinkState[LinkState["Banned"] = 3] = "Banned";
|
|
939
|
-
})(exports.LinkState || (exports.LinkState = {}));
|
|
940
|
-
|
|
941
|
-
exports.EnumPageType = void 0;
|
|
942
|
-
(function (EnumPageType) {
|
|
943
|
-
EnumPageType["md"] = "md";
|
|
944
|
-
EnumPageType["html"] = "html";
|
|
945
|
-
EnumPageType["frame"] = "frame";
|
|
946
|
-
})(exports.EnumPageType || (exports.EnumPageType = {}));
|
|
947
|
-
|
|
948
|
-
exports.RecentlyRefTypes = void 0;
|
|
949
|
-
(function (RecentlyRefTypes) {
|
|
950
|
-
RecentlyRefTypes["Post"] = "Post";
|
|
951
|
-
RecentlyRefTypes["Note"] = "Note";
|
|
952
|
-
RecentlyRefTypes["Page"] = "Page";
|
|
953
|
-
})(exports.RecentlyRefTypes || (exports.RecentlyRefTypes = {}));
|
|
954
|
-
|
|
955
|
-
exports.SnippetType = void 0;
|
|
956
|
-
(function (SnippetType) {
|
|
957
|
-
SnippetType["JSON"] = "json";
|
|
958
|
-
SnippetType["Function"] = "function";
|
|
959
|
-
SnippetType["Text"] = "text";
|
|
960
|
-
SnippetType["YAML"] = "yaml";
|
|
961
|
-
})(exports.SnippetType || (exports.SnippetType = {}));
|
|
962
|
-
|
|
963
|
-
exports.AggregateController = AggregateController;
|
|
964
|
-
exports.CategoryController = CategoryController;
|
|
965
|
-
exports.CommentController = CommentController;
|
|
966
|
-
exports.LinkController = LinkController;
|
|
967
|
-
exports.NoteController = NoteController;
|
|
968
|
-
exports.PageController = PageController;
|
|
969
|
-
exports.PostController = PostController;
|
|
970
|
-
exports.ProjectController = ProjectController;
|
|
971
|
-
exports.RecentlyController = RecentlyController;
|
|
972
|
-
exports.RequestError = RequestError;
|
|
973
|
-
exports.SayController = SayController;
|
|
974
|
-
exports.SearchController = SearchController;
|
|
975
|
-
exports.ServerlessController = ServerlessController;
|
|
976
|
-
exports.SnippetController = SnippetController;
|
|
977
|
-
exports.TopicController = TopicController;
|
|
978
|
-
exports.UserController = UserController;
|
|
979
|
-
exports.allContollerNames = allContollerNames;
|
|
980
|
-
exports.allControllers = allControllers;
|
|
981
|
-
exports.createClient = createClient;
|
|
982
|
-
|
|
983
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
984
|
-
|
|
985
|
-
}));
|
|
986
|
-
//# sourceMappingURL=index.umd.js.map
|