197 lines
5.5 KiB
TypeScript
197 lines
5.5 KiB
TypeScript
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<string> {
|
|
const resp = await this.http.get('/version');
|
|
return resp.data;
|
|
}
|
|
|
|
public async createAuthToken(creds: LoginSubmit)
|
|
: Promise<string> {
|
|
|
|
const resp = await this.http.post('/auth-token', creds);
|
|
this.setAuthToken(resp.data);
|
|
return resp.data;
|
|
}
|
|
|
|
public async getUser(): Promise<User> {
|
|
const resp = await this.http.get('/user');
|
|
return resp.data;
|
|
}
|
|
|
|
public async getAllUsers(): Promise<User[]> {
|
|
const resp = await this.http.get('/users');
|
|
return resp.data;
|
|
}
|
|
|
|
public async getUserById(reqUserId: string): Promise<User> {
|
|
const resp = await this.http.get(`/users/${reqUserId}`);
|
|
return resp.data;
|
|
}
|
|
|
|
public async createUser(newUser: User): Promise<User> {
|
|
const resp = await this.http.post('/users', newUser);
|
|
return resp.data;
|
|
}
|
|
|
|
public async deleteUser(toDeleteUserId: string)
|
|
: Promise<boolean> {
|
|
|
|
await this.http.delete(`/users/${toDeleteUserId}`);
|
|
return true;
|
|
}
|
|
|
|
public async changePwd(oldPassword: string, newPassword: string)
|
|
: Promise<boolean> {
|
|
|
|
await this.http.post('/change-pwd', { oldPassword, newPassword });
|
|
return true;
|
|
}
|
|
|
|
public async changePwdForUser(forUserId: string, newPassword: string)
|
|
: Promise<boolean> {
|
|
|
|
await this.http.post(`/change-pwd/${forUserId}`, { newPassword });
|
|
return true;
|
|
}
|
|
|
|
public async createApiToken(token: ApiToken)
|
|
: Promise<ApiToken[]> {
|
|
|
|
const resp = await this.http.post(`/api-tokens`, token);
|
|
return resp.data;
|
|
}
|
|
|
|
public async getAllApiTokens(): Promise<ApiToken[]> {
|
|
const resp = await this.http.get('/api-tokens');
|
|
return resp.data;
|
|
}
|
|
|
|
public async getApiToken(tokenId: string): Promise<ApiToken[]> {
|
|
const resp = await this.http.get(`/api-tokens/${tokenId}`);
|
|
return resp.data;
|
|
}
|
|
|
|
public async deleteApiToken(tokenId: string): Promise<boolean> {
|
|
const resp = await this.http.delete(`/api-tokens/${tokenId}`);
|
|
return true;
|
|
}
|
|
|
|
public async getAllMeasures(): Promise<Array<Measure<MeasureConfig>>> {
|
|
const resp = await this.http.get(`/measures`);
|
|
return resp.data;
|
|
}
|
|
|
|
public async createMeasure<T extends MeasureConfig>(measure: Measure<T>): Promise<Measure<T>> {
|
|
const resp = await this.http.post(`/measures`, measure);
|
|
return resp.data;
|
|
}
|
|
|
|
public async getMeasure(slug: string): Promise<Measure<MeasureConfig>> {
|
|
const resp = await this.http.get(`/measures/${slug}`);
|
|
return resp.data;
|
|
}
|
|
|
|
public async updateMeasure<T extends MeasureConfig>(measure: Measure<T>): Promise<Measure<T>> {
|
|
const resp = await this.http.post(`/measures/${measure.slug}`, measure);
|
|
return resp.data;
|
|
}
|
|
|
|
public async deleteMeasure(slug: string): Promise<boolean> {
|
|
const resp = await this.http.delete(`/measures/${slug}`);
|
|
return true;
|
|
}
|
|
|
|
public async getMeasurements(measureSlug: string)
|
|
: Promise<Array<Measurement<MeasurementMeta>>> {
|
|
|
|
const resp = await this.http.get(`/measurements/${measureSlug}`);
|
|
return resp.data.map(this.fromMeasurementDTO);
|
|
}
|
|
|
|
public async createMeasurement(
|
|
measureSlug: string,
|
|
measurement: Measurement<MeasurementMeta>)
|
|
: Promise<Measurement<MeasurementMeta>> {
|
|
|
|
const resp = await this.http.post(
|
|
`/measurements/${measureSlug}`,
|
|
this.toMeasurementDTO(measurement));
|
|
return this.fromMeasurementDTO(resp.data);
|
|
}
|
|
|
|
public async getMeasurement(
|
|
measureSlug: string,
|
|
measurementId: string)
|
|
: Promise<Measurement<MeasurementMeta>> {
|
|
|
|
const resp = await this.http
|
|
.get(`/measurements/${measureSlug}/${measurementId}`);
|
|
return this.fromMeasurementDTO(resp.data);
|
|
}
|
|
|
|
public async updateMeasurement(
|
|
measureSlug: string,
|
|
measurement: Measurement<MeasurementMeta>)
|
|
: Promise<Measurement<MeasurementMeta>> {
|
|
|
|
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<boolean> {
|
|
|
|
const resp = await this.http
|
|
.delete(`/measurements/${measureSlug}/${measurementId}`);
|
|
return true;
|
|
}
|
|
|
|
private fromMeasurementDTO(dto: any): Measurement<MeasurementMeta> {
|
|
return assign({}, dto, { timestamp: new Date(dto.timestamp) });
|
|
}
|
|
|
|
private toMeasurementDTO(measurement: Measurement<MeasurementMeta>): object {
|
|
return assign({}, measurement, { timestamp: measurement.timestamp.toISOString() });
|
|
}
|
|
|
|
}
|
|
|
|
export const api = new PmApiClient(process.env.VUE_APP_PM_API_BASE);
|
|
export default api;
|