import { default as Axios, AxiosInstance } from 'axios'; import assign from 'lodash.assign'; import { ApiToken, LoginSubmit, Measure, MeasureConfig, Measurement, MeasurementMeta, User } from '@/models'; import { Logger, logService } from '@/services/logging'; const logger = logService.getLogger('services/pm-api-client'); export class PmApiClient { private http: AxiosInstance; constructor(apiBase: string) { this.http = Axios.create({ baseURL: apiBase /* headers: { post: { 'Content-Type': 'application/json' } } */ }); logger.trace('Initialized PmApiClient'); } public setAuthToken(authToken: string) { /*tslint:disable:no-string-literal*/ this.http.defaults.headers.common['Authorization'] = `Bearer ${authToken}`; } public clearAuthToken() { this.http.defaults.headers.common['Authorization'] = ''; } public async version(): Promise { const resp = await this.http.get('/version'); return resp.data; } public async createAuthToken(creds: LoginSubmit) : Promise { const resp = await this.http.post('/auth-token', creds); this.setAuthToken(resp.data); return resp.data; } public async getUser(): Promise { const resp = await this.http.get('/user'); return resp.data; } public async getAllUsers(): Promise { const resp = await this.http.get('/users'); return resp.data; } public async getUserById(reqUserId: string): Promise { const resp = await this.http.get(`/users/${reqUserId}`); return resp.data; } public async createUser(newUser: User): Promise { const resp = await this.http.post('/users', newUser); return resp.data; } public async deleteUser(toDeleteUserId: string) : Promise { await this.http.delete(`/users/${toDeleteUserId}`); return true; } public async changePwd(oldPassword: string, newPassword: string) : Promise { await this.http.post('/change-pwd', { oldPassword, newPassword }); return true; } public async changePwdForUser(forUserId: string, newPassword: string) : Promise { await this.http.post(`/change-pwd/${forUserId}`, { newPassword }); return true; } public async createApiToken(token: ApiToken) : Promise { const resp = await this.http.post(`/api-tokens`, token); return resp.data; } public async getAllApiTokens(): Promise { const resp = await this.http.get('/api-tokens'); return resp.data; } public async getApiToken(tokenId: string): Promise { const resp = await this.http.get(`/api-tokens/${tokenId}`); return resp.data; } public async deleteApiToken(tokenId: string): Promise { const resp = await this.http.delete(`/api-tokens/${tokenId}`); return true; } public async getAllMeasures(): Promise>> { const resp = await this.http.get(`/measures`); return resp.data; } public async createMeasure(measure: Measure): Promise> { const resp = await this.http.post(`/measures`, measure); return resp.data; } public async getMeasure(slug: string): Promise> { const resp = await this.http.get(`/measures/${slug}`); return resp.data; } public async updateMeasure(measure: Measure): Promise> { const resp = await this.http.post(`/measures/${measure.slug}`, measure); return resp.data; } public async deleteMeasure(slug: string): Promise { const resp = await this.http.delete(`/measures/${slug}`); return true; } public async getMeasurements(measureSlug: string) : Promise>> { const resp = await this.http.get(`/measurements/${measureSlug}`); return resp.data.map(this.fromMeasurementDTO); } public async createMeasurement( measureSlug: string, measurement: Measurement) : Promise> { const resp = await this.http.post( `/measurements/${measureSlug}`, this.toMeasurementDTO(measurement)); return this.fromMeasurementDTO(resp.data); } public async getMeasurement( measureSlug: string, measurementId: string) : Promise> { const resp = await this.http .get(`/measurements/${measureSlug}/${measurementId}`); return this.fromMeasurementDTO(resp.data); } public async updateMeasurement( measureSlug: string, measurement: Measurement) : Promise> { const resp = await this.http.put( `/measurements/${measureSlug}/${measurement.id}`, this.toMeasurementDTO(measurement)); return this.fromMeasurementDTO(resp.data); } public async deleteMeasurement( measureSlug: string, measurementId: string) : Promise { const resp = await this.http .delete(`/measurements/${measureSlug}/${measurementId}`); return true; } private fromMeasurementDTO(dto: any): Measurement { return assign({}, dto, { timestamp: new Date(dto.timestamp) }); } private toMeasurementDTO(measurement: Measurement): object { return assign({}, measurement, { timestamp: measurement.timestamp.toISOString() }); } } export const api = new PmApiClient(process.env.VUE_APP_PM_API_BASE); export default api;