From 4d925364b95484256f77de4323c310fd48c0ecba Mon Sep 17 00:00:00 2001 From: a632079 Date: Thu, 27 Feb 2020 18:11:18 +0800 Subject: [PATCH] feat: v1.0.2 --- .npmignore | 2 ++ README.md | 6 +++++- dist/core.d.ts | 1 - dist/core.js | 1 - dist/src/core/v1/auth.d.ts | 1 - dist/src/core/v1/auth.js | 1 - dist/src/core/v1/core.d.ts | 5 ++--- dist/src/core/v1/core.js | 14 ++++++-------- dist/src/core/v1/decorator.d.ts | 1 - dist/src/core/v1/decorator.js | 1 - dist/src/core/v1/like.d.ts | 1 - dist/src/core/v1/like.js | 1 - dist/src/core/v1/request.d.ts | 1 - dist/src/core/v1/request.js | 1 - dist/src/core/v1/sentence.d.ts | 1 - dist/src/core/v1/sentence.js | 1 - dist/src/core/v1/user.d.ts | 1 - dist/src/core/v1/user.js | 1 - dist/src/sentence/v1/core.d.ts | 1 - dist/src/sentence/v1/core.js | 1 - dist/src/test/sentence/v1/get.test.d.ts | 1 - dist/src/test/sentence/v1/get.test.js | 1 - package.json | 2 +- src/core/v1/core.ts | 17 ++++++++--------- 24 files changed, 24 insertions(+), 40 deletions(-) diff --git a/.npmignore b/.npmignore index dde991c..de8e4df 100644 --- a/.npmignore +++ b/.npmignore @@ -2,3 +2,5 @@ node_modules .yarn .vscode src +.pnp.js +.yarnrc diff --git a/README.md b/README.md index 96091d6..3c42b5b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 在终端中输入: ```shell -$ yarn add @hitokoto/node # 如果你使用 npm 的话: npm install @hitokoto/node +$ yarn add @hitokoto/node-sdk # 如果你使用 npm 的话: npm install @hitokoto/node-sdk ``` ## 使用 @@ -34,6 +34,8 @@ try { .then(userData) => { console.log(userData) }) +} catch (err) { + console.error(err) } // 使用语句接口 @@ -70,6 +72,8 @@ try { .then(userData) => { console.log(userData) }) +} catch(err) { + console.error(err) } // 使用语句接口 diff --git a/dist/core.d.ts b/dist/core.d.ts index 2715551..032a1e8 100644 --- a/dist/core.d.ts +++ b/dist/core.d.ts @@ -6,4 +6,3 @@ export interface SentenceApiV1 { } export declare class SentenceApiV1 { } -//# sourceMappingURL=core.d.ts.map \ No newline at end of file diff --git a/dist/core.js b/dist/core.js index 10f0930..207db66 100644 --- a/dist/core.js +++ b/dist/core.js @@ -8,4 +8,3 @@ class SentenceApiV1 { } exports.SentenceApiV1 = SentenceApiV1; SentenceApiV1.prototype.getSentence = core_1.getSentence; -//# sourceMappingURL=core.js.map \ No newline at end of file diff --git a/dist/src/core/v1/auth.d.ts b/dist/src/core/v1/auth.d.ts index cae2d61..4452632 100644 --- a/dist/src/core/v1/auth.d.ts +++ b/dist/src/core/v1/auth.d.ts @@ -35,4 +35,3 @@ export declare class AuthApi { */ set Token(token: string); } -//# sourceMappingURL=auth.d.ts.map \ No newline at end of file diff --git a/dist/src/core/v1/auth.js b/dist/src/core/v1/auth.js index 276009c..a1fe809 100644 --- a/dist/src/core/v1/auth.js +++ b/dist/src/core/v1/auth.js @@ -47,4 +47,3 @@ class AuthApi { } } exports.AuthApi = AuthApi; -//# sourceMappingURL=auth.js.map \ No newline at end of file diff --git a/dist/src/core/v1/core.d.ts b/dist/src/core/v1/core.d.ts index 56a6558..8954adb 100644 --- a/dist/src/core/v1/core.d.ts +++ b/dist/src/core/v1/core.d.ts @@ -13,9 +13,9 @@ export declare class CoreApi { constructor(token?: string); /** * 检验 Token 是否有效,如果有效才能进行其他的接口请求 - * @returns {Promise} + * @returns {Promise} */ - verifyToken(): Promise; + verifyToken(): Promise; /** * 获得令牌 * @returns {string} 令牌 @@ -29,4 +29,3 @@ export declare class CoreApi { } export interface CoreApi extends AuthApi, UserApi, LikeApi, SentenceApi { } -//# sourceMappingURL=core.d.ts.map \ No newline at end of file diff --git a/dist/src/core/v1/core.js b/dist/src/core/v1/core.js index 60f1832..f12eedb 100644 --- a/dist/src/core/v1/core.js +++ b/dist/src/core/v1/core.js @@ -24,20 +24,19 @@ class CoreApi { } /** * 检验 Token 是否有效,如果有效才能进行其他的接口请求 - * @returns {Promise} + * @returns {Promise} */ async verifyToken() { if (!this.request.token) { - return false; + throw new Error('令牌无效'); } else if (this.request.token.length !== 40) { - return false; + throw new Error('令牌长度不符合'); } const data = await this.request.get('/user'); - if (data.status === 200) { - this.request.isValid = true; - } - return data.status === 200; + request_1.checkStatusCode(data); + this.request.isValid = true; + return this; } /** * 获得令牌 @@ -78,4 +77,3 @@ function applyMixins(derivedCtor, baseCtors) { }); }); } -//# sourceMappingURL=core.js.map \ No newline at end of file diff --git a/dist/src/core/v1/decorator.d.ts b/dist/src/core/v1/decorator.d.ts index 5e1704d..b0d120a 100644 --- a/dist/src/core/v1/decorator.d.ts +++ b/dist/src/core/v1/decorator.d.ts @@ -1,2 +1 @@ export declare function checkValid(): (target: any, name: string, descriptor: PropertyDescriptor) => void; -//# sourceMappingURL=decorator.d.ts.map \ No newline at end of file diff --git a/dist/src/core/v1/decorator.js b/dist/src/core/v1/decorator.js index 7019694..6dcb650 100644 --- a/dist/src/core/v1/decorator.js +++ b/dist/src/core/v1/decorator.js @@ -15,4 +15,3 @@ function checkValid() { }; } exports.checkValid = checkValid; -//# sourceMappingURL=decorator.js.map \ No newline at end of file diff --git a/dist/src/core/v1/like.d.ts b/dist/src/core/v1/like.d.ts index 7b2f042..32a73b7 100644 --- a/dist/src/core/v1/like.d.ts +++ b/dist/src/core/v1/like.d.ts @@ -16,4 +16,3 @@ export declare class LikeApi { cancalSentenceLike(sentenceUuid: string): Promise; } export {}; -//# sourceMappingURL=like.d.ts.map \ No newline at end of file diff --git a/dist/src/core/v1/like.js b/dist/src/core/v1/like.js index b18dd57..03601e9 100644 --- a/dist/src/core/v1/like.js +++ b/dist/src/core/v1/like.js @@ -43,4 +43,3 @@ __decorate([ __metadata("design:returntype", Promise) ], LikeApi.prototype, "cancalSentenceLike", null); exports.LikeApi = LikeApi; -//# sourceMappingURL=like.js.map \ No newline at end of file diff --git a/dist/src/core/v1/request.d.ts b/dist/src/core/v1/request.d.ts index 538c68f..7e2a581 100644 --- a/dist/src/core/v1/request.d.ts +++ b/dist/src/core/v1/request.d.ts @@ -56,4 +56,3 @@ export declare class ApiRequest { set isValid(isValid: boolean); } export declare function checkStatusCode(responseData: ResponseStruct): void; -//# sourceMappingURL=request.d.ts.map \ No newline at end of file diff --git a/dist/src/core/v1/request.js b/dist/src/core/v1/request.js index 143037f..deb0afa 100644 --- a/dist/src/core/v1/request.js +++ b/dist/src/core/v1/request.js @@ -156,4 +156,3 @@ function checkStatusCode(responseData) { } } exports.checkStatusCode = checkStatusCode; -//# sourceMappingURL=request.js.map \ No newline at end of file diff --git a/dist/src/core/v1/sentence.d.ts b/dist/src/core/v1/sentence.d.ts index 68fe036..451d0bc 100644 --- a/dist/src/core/v1/sentence.d.ts +++ b/dist/src/core/v1/sentence.d.ts @@ -78,4 +78,3 @@ export declare class SentenceApi { reportSentence(sentenceUuid: string, comment: string): Promise; } export {}; -//# sourceMappingURL=sentence.d.ts.map \ No newline at end of file diff --git a/dist/src/core/v1/sentence.js b/dist/src/core/v1/sentence.js index 2e0525c..7f30d94 100644 --- a/dist/src/core/v1/sentence.js +++ b/dist/src/core/v1/sentence.js @@ -77,4 +77,3 @@ __decorate([ __metadata("design:returntype", Promise) ], SentenceApi.prototype, "reportSentence", null); exports.SentenceApi = SentenceApi; -//# sourceMappingURL=sentence.js.map \ No newline at end of file diff --git a/dist/src/core/v1/user.d.ts b/dist/src/core/v1/user.d.ts index 923cf3d..8b66932 100644 --- a/dist/src/core/v1/user.d.ts +++ b/dist/src/core/v1/user.d.ts @@ -88,4 +88,3 @@ export declare class UserApi { getUserHitokotoHistoryAccept(offset: 0, limit: 20): Promise; } export {}; -//# sourceMappingURL=user.d.ts.map \ No newline at end of file diff --git a/dist/src/core/v1/user.js b/dist/src/core/v1/user.js index c3014df..1ca7c6c 100644 --- a/dist/src/core/v1/user.js +++ b/dist/src/core/v1/user.js @@ -175,4 +175,3 @@ __decorate([ __metadata("design:returntype", Promise) ], UserApi.prototype, "getUserHitokotoHistoryAccept", null); exports.UserApi = UserApi; -//# sourceMappingURL=user.js.map \ No newline at end of file diff --git a/dist/src/sentence/v1/core.d.ts b/dist/src/sentence/v1/core.d.ts index a898f78..8d7275d 100644 --- a/dist/src/sentence/v1/core.d.ts +++ b/dist/src/sentence/v1/core.d.ts @@ -11,4 +11,3 @@ export interface sentenceApiResponse { created_at: number; } export declare function getSentence(categroy?: string[] | string): Promise; -//# sourceMappingURL=core.d.ts.map \ No newline at end of file diff --git a/dist/src/sentence/v1/core.js b/dist/src/sentence/v1/core.js index 20c71c9..7bd23d2 100644 --- a/dist/src/sentence/v1/core.js +++ b/dist/src/sentence/v1/core.js @@ -30,4 +30,3 @@ async function getSentence(categroy) { } } exports.getSentence = getSentence; -//# sourceMappingURL=core.js.map \ No newline at end of file diff --git a/dist/src/test/sentence/v1/get.test.d.ts b/dist/src/test/sentence/v1/get.test.d.ts index 4e08af3..cb0ff5c 100644 --- a/dist/src/test/sentence/v1/get.test.d.ts +++ b/dist/src/test/sentence/v1/get.test.d.ts @@ -1,2 +1 @@ export {}; -//# sourceMappingURL=get.test.d.ts.map \ No newline at end of file diff --git a/dist/src/test/sentence/v1/get.test.js b/dist/src/test/sentence/v1/get.test.js index 7b3e8f1..58f112a 100644 --- a/dist/src/test/sentence/v1/get.test.js +++ b/dist/src/test/sentence/v1/get.test.js @@ -5,4 +5,3 @@ test('测试是否能正常获得一言语句', async () => { const sentence = await core_1.getSentence(); expect(sentence.uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i); }); -//# sourceMappingURL=get.test.js.map \ No newline at end of file diff --git a/package.json b/package.json index d8bf964..1f8a6ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hitokoto/node-sdk", - "version": "1.0.1", + "version": "1.0.2", "description": "hitokoto.cn official node.js sdk.", "main": "dist/core.js", "scripts": { diff --git a/src/core/v1/core.ts b/src/core/v1/core.ts index 8f56ed9..253fba9 100644 --- a/src/core/v1/core.ts +++ b/src/core/v1/core.ts @@ -1,6 +1,6 @@ /* eslint-disable camelcase */ /* eslint-disable no-unused-vars */ -import { ApiRequest, ResponseStruct } from './request' +import { ApiRequest, ResponseStruct, checkStatusCode } from './request' import { AuthApi } from './auth' import { SentenceApi } from './sentence' import { LikeApi } from './like' @@ -25,19 +25,18 @@ export class CoreApi { /** * 检验 Token 是否有效,如果有效才能进行其他的接口请求 - * @returns {Promise} + * @returns {Promise} */ - async verifyToken () { + async verifyToken (): Promise { if (!this.request.token) { - return false + throw new Error('令牌无效') } else if (this.request.token.length !== 40) { - return false + throw new Error('令牌长度不符合') } const data: ResponseStruct = await this.request.get('/user') - if (data.status === 200) { - this.request.isValid = true - } - return data.status === 200 + checkStatusCode(data) + this.request.isValid = true + return this } /**