From 58ffd2dbe4f4b5f65353b0e0130b431da2d9dccc Mon Sep 17 00:00:00 2001 From: Anna Date: Tue, 2 Jun 2020 13:05:06 +0200 Subject: [PATCH] using prettier --- package.json | 1 + src/auth.ts | 36 +++++++++-------- src/surveys.ts | 106 +++++++++++++++++++++++++------------------------ src/utils.ts | 17 ++++---- 4 files changed, 84 insertions(+), 76 deletions(-) diff --git a/package.json b/package.json index 15c2747..dd1102a 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/auth.ts b/src/auth.ts index f52d8c9..73c73bc 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -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; + } +}; diff --git a/src/surveys.ts b/src/surveys.ts index 77394f7..ebd4b64 100644 --- a/src/surveys.ts +++ b/src/surveys.ts @@ -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 { - 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 { - 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 { - 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 { + 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 { + 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 { + const res = await axios({ + method: 'delete', + url: `${this.backendUrl}/api/v4/surveys/${id}`, + headers: { + Authorization: `Bearer ${this.token}`, + }, + }); + return res; + } } diff --git a/src/utils.ts b/src/utils.ts index 86b5d15..7d3d6f0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,10 @@ import axios from 'axios'; -export const getLanguages = async (backendURL:string):Promise => { - try { - const response = await axios.get(`${backendURL}/api/v4/languages`); - console.log(response) - return response.data; - } catch(err) { - return err; - } -} \ No newline at end of file +export const getLanguages = async (backendURL: string): Promise => { + try { + const response = await axios.get(`${backendURL}/api/v4/languages`); + return response.data; + } catch (err) { + return err; + } +};