Skip to content

Commit

Permalink
using prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Angamanga committed Jun 2, 2020
1 parent 018c055 commit 58ffd2d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 76 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"build": "tsc -p tsconfig.release.json",
"build:watch": "tsc -w -p tsconfig.release.json",
"lint": "eslint . --ext .ts",
"prettier": "prettier --write 'src/**/*.ts'",
"test": "jest --coverage",
"test:watch": "jest --watch",
"prepare": "npm run build"
Expand Down
36 changes: 20 additions & 16 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import axios from 'axios';

export const getAuthToken = async (username:string, password:string, backendURL:string):Promise<{token: string}> => {
try {
const response = await axios.post(`${backendURL}/oauth/token`, {
grant_type: 'password',
client_secret: '35e7f0bca957836d05ca0492211b0ac707671261',
client_id: 'ushahidiui',
scope: 'posts country_codes media forms api tags savedsearches sets users stats layers config messages notifications webhooks contacts permissions csv',
password: password,
username: username
}
);
return {token: response.data.access_token};
} catch(err) {
return err;
}
}
export const getAuthToken = async (
username: string,
password: string,
backendURL: string,
): Promise<{ token: string }> => {
try {
const response = await axios.post(`${backendURL}/oauth/token`, {
grant_type: 'password',
client_secret: '35e7f0bca957836d05ca0492211b0ac707671261',
client_id: 'ushahidiui',
scope:
'posts country_codes media forms api tags savedsearches sets users stats layers config messages notifications webhooks contacts permissions csv',
password: password,
username: username,
});
return { token: response.data.access_token };
} catch (err) {
return err;
}
};
106 changes: 55 additions & 51 deletions src/surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,60 @@
import axios from 'axios';

export class Surveys {
backendUrl:string;
token:string;

constructor (backendUrl:string, token?:string) {
this.backendUrl = backendUrl;
this.token = token;
}

setToken(token?:string):object {
this.token = token;
return this;
}

async getSurveys(id?:string):Promise<unknown> {
try {
const url = id ? `${this.backendUrl}/api/v4/surveys/${id}` : `${this.backendUrl}/api/v4/surveys/`;
const config = this.token ? {
headers: {"Authorization": `Bearer ${this.token}`}
} : {}
const response = await axios.get(url, config);
return response.data.result || response.data.results;
}
catch(err) {
return err;
}
}


async saveSurvey(survey:{id?:string}):Promise<unknown> {
const method = survey.id ? 'put' : 'post';
const url = survey.id ? `${this.backendUrl}/api/v4/surveys/${survey.id}` : `${this.backendUrl}/api/v4/surveys/`;
const res = await axios({
method: method,
url: url,
headers:{
"Authorization": `Bearer ${this.token}`
},
data: survey
});
return res;
}

async deleteSurvey(id:string):Promise<unknown> {
const res = await axios({
method: 'delete',
url: `${this.backendUrl}/api/v4/surveys/${id}`,
headers:{
"Authorization": `Bearer ${this.token}`
}
});
return res;
backendUrl: string;
token: string;

constructor(backendUrl: string, token?: string) {
this.backendUrl = backendUrl;
this.token = token;
}

setToken(token?: string): object {
this.token = token;
return this;
}

async getSurveys(id?: string): Promise<unknown> {
try {
const url = id
? `${this.backendUrl}/api/v4/surveys/${id}`
: `${this.backendUrl}/api/v4/surveys/`;
const config = this.token
? {
headers: { Authorization: `Bearer ${this.token}` },
}
: {};
const response = await axios.get(url, config);
return response.data.result || response.data.results;
} catch (err) {
return err;
}
}

async saveSurvey(survey: { id?: string }): Promise<unknown> {
const method = survey.id ? 'put' : 'post';
const url = survey.id
? `${this.backendUrl}/api/v4/surveys/${survey.id}`
: `${this.backendUrl}/api/v4/surveys/`;
const res = await axios({
method: method,
url: url,
headers: {
Authorization: `Bearer ${this.token}`,
},
data: survey,
});
return res;
}

async deleteSurvey(id: string): Promise<unknown> {
const res = await axios({
method: 'delete',
url: `${this.backendUrl}/api/v4/surveys/${id}`,
headers: {
Authorization: `Bearer ${this.token}`,
},
});
return res;
}
}
17 changes: 8 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import axios from 'axios';

export const getLanguages = async (backendURL:string):Promise<unknown> => {
try {
const response = await axios.get(`${backendURL}/api/v4/languages`);
console.log(response)
return response.data;
} catch(err) {
return err;
}
}
export const getLanguages = async (backendURL: string): Promise<unknown> => {
try {
const response = await axios.get(`${backendURL}/api/v4/languages`);
return response.data;
} catch (err) {
return err;
}
};

0 comments on commit 58ffd2d

Please sign in to comment.