WIP Progress on skeleton of the Vue app.
This commit is contained in:
@ -1,9 +1,12 @@
|
||||
import { default as Axios, AxiosInstance } from 'axios';
|
||||
import { ApiToken, Measure, Measurement, User } from '@/models';
|
||||
import { ApiToken, LoginSubmit, Measure, Measurement, User } from '@/models';
|
||||
import { Logger, logService } from '@/services/logging';
|
||||
import merge from 'lodash.merge';
|
||||
|
||||
export class PmApiClient {
|
||||
private http: AxiosInstance;
|
||||
private authToken?: string;
|
||||
private log: Logger;
|
||||
|
||||
constructor(apiBase: string) {
|
||||
this.http = Axios.create({
|
||||
@ -16,161 +19,200 @@ export class PmApiClient {
|
||||
}
|
||||
*/
|
||||
});
|
||||
|
||||
this.log = logService.getLogger('services/pm-api-client');
|
||||
this.log.trace('Initialized PmApiClient');
|
||||
}
|
||||
|
||||
public version(): Promise<any> {
|
||||
return this.http
|
||||
.get('/version')
|
||||
.then((resp) => resp.data);
|
||||
public setAuthToken(t: string) { this.authToken = t; }
|
||||
|
||||
public async version(): Promise<string> {
|
||||
const resp = await this.http.get('/version');
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public createAuthToken(email: string, password: string): Promise<string> {
|
||||
return this.http
|
||||
.post('/auth-token', { email, password })
|
||||
.then((resp) => resp.data);
|
||||
public async createAuthToken(creds: LoginSubmit)
|
||||
: Promise<string> {
|
||||
|
||||
const resp = await this.http.post('/auth-token', creds);
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public getUser(authToken: string): Promise<User> {
|
||||
return this.http
|
||||
.get('/user', { headers: { Authorization: 'Bearer ' + authToken }})
|
||||
.then((resp) => merge(resp.data, { authToken }) );
|
||||
public async getUser(authToken: string): Promise<User> {
|
||||
|
||||
const resp = await this.http.get('/user',
|
||||
{ headers: { Authorization: 'Bearer ' + authToken }});
|
||||
|
||||
return merge(resp.data, { authToken }) ;
|
||||
}
|
||||
|
||||
public getAllUsers(user: User): Promise<User[]> {
|
||||
return this.http
|
||||
.get('/users', { headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
public async getAllUsers(): Promise<User[]> {
|
||||
const resp = await this.http.get('/users',
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public getUserById(curUser: User, reqUserId: string): Promise<User> {
|
||||
return this.http
|
||||
.get(`/users/${reqUserId}`, { headers: this.authHeader(curUser) })
|
||||
.then((resp) => resp.data);
|
||||
public async getUserById(reqUserId: string): Promise<User> {
|
||||
const resp = await this.http.get(`/users/${reqUserId}`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public createUser(curUser: User, newUser: User): Promise<User> {
|
||||
return this.http
|
||||
.post('/users', newUser, { headers: this.authHeader(curUser) })
|
||||
.then((resp) => resp.data);
|
||||
public async createUser(newUser: User): Promise<User> {
|
||||
const resp = await this.http.post('/users',
|
||||
newUser, { headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public deleteUser(curUser: User, toDeleteUserId: string): Promise<boolean> {
|
||||
return this.http
|
||||
.delete(`/users/${toDeleteUserId}`, { headers: this.authHeader(curUser) })
|
||||
.then((resp) => true);
|
||||
public async deleteUser(toDeleteUserId: string)
|
||||
: Promise<boolean> {
|
||||
|
||||
await this.http.delete(`/users/${toDeleteUserId}`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public changePwd(user: User, oldPassword: string, newPassword: string): Promise<boolean> {
|
||||
return this.http
|
||||
.post(
|
||||
'/change-pwd',
|
||||
{ oldPassword, newPassword },
|
||||
{ headers: this.authHeader(user) })
|
||||
.then((resp) => true);
|
||||
public async changePwd(oldPassword: string, newPassword: string)
|
||||
: Promise<boolean> {
|
||||
|
||||
await this.http.post('/change-pwd',
|
||||
{ oldPassword, newPassword }, { headers: this.authHeader() });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public changePwdForUser(user: User, forUserId: string, newPassword: string): Promise<boolean> {
|
||||
return this.http
|
||||
.post(
|
||||
`/change-pwd/${forUserId}`,
|
||||
{ newPassword },
|
||||
{ headers: this.authHeader(user) })
|
||||
.then((resp) => true);
|
||||
public async changePwdForUser(forUserId: string, newPassword: string)
|
||||
: Promise<boolean> {
|
||||
|
||||
await this.http.post(`/change-pwd/${forUserId}`,
|
||||
{ newPassword }, { headers: this.authHeader() });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public createApiToken(user: User, token: ApiToken): Promise<ApiToken[]> {
|
||||
return this.http
|
||||
.post(`/api-tokens`, token, { headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
public async createApiToken(token: ApiToken)
|
||||
: Promise<ApiToken[]> {
|
||||
|
||||
const resp = await this.http.post(`/api-tokens`,
|
||||
token, { headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public getAllApiTokens(user: User): Promise<ApiToken[]> {
|
||||
return this.http
|
||||
.get('/api-tokens', { headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
public async getAllApiTokens(): Promise<ApiToken[]> {
|
||||
const resp = await this.http.get('/api-tokens',
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public getApiToken(user: User, tokenId: string): Promise<ApiToken[]> {
|
||||
return this.http
|
||||
.get(`/api-tokens/${tokenId}`, { headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
public async getApiToken(tokenId: string): Promise<ApiToken[]> {
|
||||
const resp = await this.http.get(`/api-tokens/${tokenId}`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public deleteApiToken(user: User, tokenId: string): Promise<boolean> {
|
||||
return this.http
|
||||
.delete(`/api-tokens/${tokenId}`, { headers: this.authHeader(user) })
|
||||
.then((resp) => true);
|
||||
public async deleteApiToken(tokenId: string): Promise<boolean> {
|
||||
const resp = await this.http.delete(`/api-tokens/${tokenId}`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public getAllMeasures(user: User): Promise<Measure[]> {
|
||||
return this.http
|
||||
.get(`/measures`, { headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
public async getAllMeasures(): Promise<Measure[]> {
|
||||
const resp = await this.http.get(`/measures`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public createMeasure(user: User, measure: Measure): Promise<Measure> {
|
||||
return this.http
|
||||
.post(`/measures`, measure, { headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
public async createMeasure(measure: Measure): Promise<Measure> {
|
||||
const resp = await this.http.post(`/measures`,
|
||||
measure, { headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public getMeasure(user: User, slug: string): Promise<Measure> {
|
||||
return this.http
|
||||
.get(`/measures/${slug}`, { headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
public async getMeasure(slug: string): Promise<Measure> {
|
||||
const resp = await this.http.get(`/measures/${slug}`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public deleteMeasure(user: User, slug: string): Promise<boolean> {
|
||||
return this.http
|
||||
.delete(`/measures/${slug}`, { headers: this.authHeader(user) })
|
||||
.then((resp) => true);
|
||||
public async deleteMeasure(slug: string): Promise<boolean> {
|
||||
const resp = await this.http.delete(`/measures/${slug}`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public getMeasurements(user: User, measureSlug: string):
|
||||
Promise<Measurement[]> {
|
||||
public async getMeasurements(measureSlug: string)
|
||||
: Promise<Measurement[]> {
|
||||
|
||||
return this.http
|
||||
.get(`/measure/${measureSlug}`, { headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
const resp = await this.http.get(`/measure/${measureSlug}`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public createMeasurement(user: User, measureSlug: string,
|
||||
measurement: Measurement): Promise<Measurement> {
|
||||
return this.http
|
||||
.post(`/measure/${measureSlug}`,
|
||||
measurement,
|
||||
{ headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
public async createMeasurement(
|
||||
measureSlug: string,
|
||||
measurement: Measurement)
|
||||
: Promise<Measurement> {
|
||||
|
||||
const resp = await this.http.post(`/measure/${measureSlug}`,
|
||||
measurement, { headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public getMeasurement(user: User, measureSlug: string, measurementId: string):
|
||||
Promise<Measurement> {
|
||||
public async getMeasurement(
|
||||
measureSlug: string,
|
||||
measurementId: string)
|
||||
: Promise<Measurement> {
|
||||
|
||||
return this.http
|
||||
.get(`/measure/${measureSlug}/${measurementId}`,
|
||||
{ headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data);
|
||||
const resp = await this.http.get(`/measure/${measureSlug}/${measurementId}`,
|
||||
{ headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public updateMeasurement(user: User, measureSlug: string,
|
||||
measurement: Measurement): Promise<Measurement> {
|
||||
return this.http
|
||||
.put(`/measure/${measureSlug}/${measurement.id}`,
|
||||
measurement,
|
||||
{ headers: this.authHeader(user) })
|
||||
.then((resp) => resp.data) ;
|
||||
public async updateMeasurement(
|
||||
measureSlug: string,
|
||||
measurement: Measurement)
|
||||
: Promise<Measurement> {
|
||||
|
||||
const resp = await this.http.put(`/measure/${measureSlug}/${measurement.id}`,
|
||||
measurement, { headers: this.authHeader() });
|
||||
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
public deleteMeasurement(user: User, measureSlug: string, measurementId: string): Promise<boolean> {
|
||||
return this.http
|
||||
.delete(`/measure/${measureSlug}/${measurementId}`,
|
||||
{headers: this.authHeader(user) })
|
||||
.then((resp) => true);
|
||||
public async deleteMeasurement(
|
||||
measureSlug: string,
|
||||
measurementId: string)
|
||||
: Promise<boolean> {
|
||||
|
||||
const resp = await this.http.delete(`/measure/${measureSlug}/${measurementId}`,
|
||||
{headers: this.authHeader() });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private authHeader(user: User): { [key: string]: string } {
|
||||
return { Authorization: 'Bearer ' + user.authToken };
|
||||
private authHeader(): { [key: string]: string } {
|
||||
if (this.authToken) {
|
||||
return { Authorization: 'Bearer ' + this.authToken };
|
||||
} else {
|
||||
throw new Error('no authenticated user');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new PmApiClient(process.env.VUE_APP_PM_API_BASE);
|
||||
export const api = new PmApiClient(process.env.VUE_APP_PM_API_BASE);
|
||||
export default api;
|
||||
|
Reference in New Issue
Block a user