Skip to content

Commit

Permalink
feat: add checkToken
Browse files Browse the repository at this point in the history
  • Loading branch information
tao1991123 committed Jul 9, 2019
1 parent 59ba665 commit 2858e0d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alifd/fusion-site-sdk",
"version": "0.0.9",
"version": "0.1.0",
"description": "fusion site open api node sdk",
"main": "build/index.js",
"files": [
Expand Down
13 changes: 9 additions & 4 deletions src/lib/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ export default function createInstance(token: string, env?: Env) {
return Promise.reject(error);
});
instance.interceptors.response.use(function(response) {
if (response.status === 200 && !response.data.success) {
debug("%o", response);
const err = new ResponseFailError();
err.response = response;
const err = new ResponseFailError();
err.response = response;

if (response.status !== 200) {
// 非200 报错
return Promise.reject(err);
} else if (response.request.method !== "HEAD" && !response.data.success) {
// 非HEAD方法 但是返回体里面没有内容 报错
return Promise.reject(err);
}

return response;
}, function(error) {
debug("%o", error);
Expand Down
13 changes: 12 additions & 1 deletion src/lib/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import Debug from "debug";
const debug = Debug("fusion-sdk:user");
import { AxiosInstance } from "axios";
import {IFusionSite} from "../type";
import BaseHttpClient from "./base";

Expand All @@ -12,6 +11,18 @@ class User extends BaseHttpClient {
debug("%o", res.data.data);
return res.data.data as IFusionSite[];
}
public async checkToken(): Promise<boolean> {
const url = "/api/v1/istokenvalid";
try {
const res = await this.client.head(url);
if (res.status !== 200) {
return false;
}
return true;
} catch (error) {
return false;
}
}
}

export default User;
6 changes: 6 additions & 0 deletions test/lib/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ test("get user sites", async () => {
assert(res.length > 0);
assert(res[0].id > 0);
});

test.only("check user token", async () => {
const user = new User(instance);
const res = await user.checkToken();
assert(res);
});

0 comments on commit 2858e0d

Please sign in to comment.