Skip to content

Commit

Permalink
format code style, use switch instead of if...else
Browse files Browse the repository at this point in the history
  • Loading branch information
moesoha committed Aug 17, 2017
1 parent c306490 commit f722238
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 75 deletions.
4 changes: 2 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Soha King
http://soha.moe
=============================*/

module.exports={
module.exports = {
request: {
header: {
header: {
'Accept': 'application/json',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
Expand Down
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ Soha King
http://soha.moe
=============================*/

var config=require('./config');
var axios=require('axios');
var http=require('http');
var https=require('https');
var config = require('./config');
var axios = require('axios');
var http = require('http');
var https = require('https');

var api=function (key,secret,interfaceUrl){
config.oiinhand.auth.key=key;
config.oiinhand.auth.secret=secret;
if(interfaceUrl){
config.oiinhand.interfaceUrl=interfaceUrl;
var OIHSDK = function (key, secret, interfaceUrl) {
config.oiinhand.auth.key = key;
config.oiinhand.auth.secret = secret;
if (interfaceUrl) {
config.oiinhand.interfaceUrl = interfaceUrl;
}
var axiosInstance=axios.create({
var axiosInstance = axios.create({
baseURL: config.oiinhand.interfaceUrl,
headers: config.request.header,
params:{
params: {
apikey: config.oiinhand.auth.key,
apisecret: config.oiinhand.auth.secret
},
Expand All @@ -36,9 +36,9 @@ var api=function (key,secret,interfaceUrl){
keepAlive: true
})
});
api.prototype.problem=new (require('./lib/problem'))(axiosInstance,config);
api.prototype.search=new (require('./lib/search'))(axiosInstance,config);
api.prototype.token=new (require('./lib/token'))(axiosInstance,config);
OIHSDK.prototype.problem = new (require('./lib/problem'))(axiosInstance, config);
OIHSDK.prototype.search = new (require('./lib/search'))(axiosInstance, config);
OIHSDK.prototype.token = new (require('./lib/token'))(axiosInstance, config);
}

module.exports=api;
module.exports = OIHSDK;
28 changes: 14 additions & 14 deletions lib/problem.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@ Soha King
var axiosInstance;
var _config;

var subapi=function (axios,i_config){
axiosInstance=axios;
_config=i_config;
var Problem = function (axios, i_config) {
axiosInstance = axios;
_config = i_config;
};

subapi.prototype.show=function (id){
return(axiosInstance.request({
url: 'problem/show/'+id,
Problem.prototype.show = function (id) {
return (axiosInstance.request({
url: 'problem/show/' + id,
method: 'get',
}));
}

subapi.prototype.list=function (page,limit,parameter){
if(typeof(parameter)=='object'){
parameter.page=page;
parameter.limit=limit;
}else{
parameter={
Problem.prototype.list = function (page, limit, parameter) {
if (typeof (parameter) == 'object') {
parameter.page = page;
parameter.limit = limit;
} else {
parameter = {
page: page,
limit: limit
}
}
return(axiosInstance.request({
return (axiosInstance.request({
url: 'problem/list',
method: 'get',
params: parameter
}));
}

module.exports=subapi;
module.exports = Problem;
44 changes: 22 additions & 22 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,45 @@ Soha King
var axiosInstance;
var _config;

var subapi=function (axios,i_config){
axiosInstance=axios;
_config=i_config;
var Search = function (axios, i_config) {
axiosInstance = axios;
_config = i_config;
};

subapi.prototype.simple=function (q,parameter){
if(typeof(parameter)=='object'){
if(typeof(parameter.oj)=="object"){
parameter.oj=parameter.oj.join(',');
Search.prototype.simple = function (q, parameter) {
if (typeof (parameter) == 'object') {
if (typeof (parameter.oj) == "object") {
parameter.oj = parameter.oj.join(',');
}
parameter.query=q;
}else{
parameter={
parameter.query = q;
} else {
parameter = {
query: q
}
}
return(axiosInstance.request({
return (axiosInstance.request({
url: 'problem/search/AwesomeSoha',
params: parameter
}));
}

subapi.prototype.wtag=function (q,tags,parameter){
if(typeof(parameter)=='object'){
if(typeof(parameter.oj)=="object"){
parameter.oj=parameter.oj.join(',');
Search.prototype.wtag = function (q, tags, parameter) {
if (typeof (parameter) == 'object') {
if (typeof (parameter.oj) == "object") {
parameter.oj = parameter.oj.join(',');
}
parameter.query=q;
parameter.tag=(typeof(tags)=="object") ? tags.join('|') : tags;
}else{
parameter={
parameter.query = q;
parameter.tag = (typeof (tags) == "object") ? tags.join('|') : tags;
} else {
parameter = {
query: q,
tag: ((typeof(tags)=="object") ? tags.join('|') : tags)
tag: ((typeof (tags) == "object") ? tags.join('|') : tags)
}
}
return(axiosInstance.request({
return (axiosInstance.request({
url: 'problem/searchAdvanced/withTag',
params: parameter
}));
}

module.exports=subapi;
module.exports = Search;
45 changes: 24 additions & 21 deletions lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,42 @@ Soha King
var axiosInstance;
var _config;

var subapi=function (axios,i_config){
axiosInstance=axios;
_config=i_config;
var Token = function (axios, i_config) {
axiosInstance = axios;
_config = i_config;
};

subapi.prototype.newClient=function (){
return(axiosInstance.request({
Token.prototype.newClient = function () {
return (axiosInstance.request({
url: 'token/newClient',
method: 'get',
responseType: 'text'
}));
}
};

subapi.prototype.validate=async function (token){
var response,code;
try{
response=await axiosInstance.request({
url: 'token/validate/'+token,
Token.prototype.validate = async function (token) {
var response, code;
try {
response = await axiosInstance.request({
url: 'token/validate/' + token,
method: 'get',
responseType: 'text'
});
code=(response.response)?response.response.status:response.status;
}catch(response){
code=(response.response)?response.response.status:null;
code = (response.response) ? response.response.status : response.status;
} catch (response) {
code = (response.response) ? response.response.status : null;
}

if(code==418){
return(false);
}else if(code==204){
return(true);
}else{
throw response;
switch (code) {
case 204:
return (true);
break;
case 418:
return (false);
break;
default:
throw new Error("Unknown status code when validating token.");
}
}

module.exports=subapi;
module.exports = Token;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oiinhand-sdk-axios",
"version": "1.0.7",
"version": "1.0.8",
"description": "The Node.js SDK for OI in Hand API",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f722238

Please sign in to comment.