Skip to content

Commit 3266ce0

Browse files
committed
Add type for API repsonse value
1 parent b513483 commit 3266ce0

File tree

3 files changed

+41
-40
lines changed

3 files changed

+41
-40
lines changed

projects/angular-token/src/lib/angular-token.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ export interface ResetPasswordData {
3434

3535
// API Response Format
3636

37-
export interface ApiResponse<T> {
37+
export interface ApiResponse {
3838
status?: string;
3939
success?: boolean;
4040
statusText?: string;
41-
data?: T;
41+
data?: UserData;
4242
errors?: any;
4343
}
4444

projects/angular-token/src/lib/angular-token.service.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class AngularTokenService implements CanActivate {
170170
*/
171171

172172
// Register request
173-
registerAccount(registerData: RegisterData, additionalData?: any): Observable<any> {
173+
registerAccount(registerData: RegisterData, additionalData?: any): Observable<ApiResponse> {
174174

175175
registerData = Object.assign({}, registerData);
176176

@@ -199,16 +199,18 @@ export class AngularTokenService implements CanActivate {
199199

200200
registerData.confirm_success_url = this.options.registerAccountCallback;
201201

202-
return this.http.post(this.getServerPath() + this.options.registerAccountPath, registerData);
202+
return this.http.post<ApiResponse>(
203+
this.getServerPath() + this.options.registerAccountPath, registerData
204+
);
203205
}
204206

205207
// Delete Account
206-
deleteAccount(): Observable<any> {
207-
return this.http.delete(this.getServerPath() + this.options.deleteAccountPath);
208+
deleteAccount(): Observable<ApiResponse> {
209+
return this.http.delete<ApiResponse>(this.getServerPath() + this.options.deleteAccountPath);
208210
}
209211

210212
// Sign in request and set storage
211-
signIn(signInData: SignInData, additionalData?: any): Observable<any> {
213+
signIn(signInData: SignInData, additionalData?: any): Observable<ApiResponse> {
212214
this.userType = (signInData.userType == null) ? null : this.getUserTypeByName(signInData.userType);
213215

214216
const body = {
@@ -220,11 +222,11 @@ export class AngularTokenService implements CanActivate {
220222
body.additionalData = additionalData;
221223
}
222224

223-
const observ = this.http.post<ApiResponse<UserData>>(
224-
this.getServerPath() + this.options.signInPath, body, { observe: 'response' }
225+
const observ = this.http.post<ApiResponse>(
226+
this.getServerPath() + this.options.signInPath, body
225227
).pipe(share());
226228

227-
observ.subscribe(res => this.userData = res.body.data);
229+
observ.subscribe(res => this.userData = res.data);
228230

229231
return observ;
230232
}
@@ -267,30 +269,30 @@ export class AngularTokenService implements CanActivate {
267269
}
268270

269271
// Sign out request and delete storage
270-
signOut(): Observable<any> {
271-
const observ = this.http.delete<any>(this.getServerPath() + this.options.signOutPath)
272-
// Only remove the localStorage and clear the data after the call
273-
.pipe(
274-
finalize(() => {
275-
this.localStorage.removeItem('accessToken');
276-
this.localStorage.removeItem('client');
277-
this.localStorage.removeItem('expiry');
278-
this.localStorage.removeItem('tokenType');
279-
this.localStorage.removeItem('uid');
280-
281-
this.authData = null;
282-
this.userType = null;
283-
this.userData = null;
284-
}
285-
)
286-
);
287-
288-
return observ;
272+
signOut(): Observable<ApiResponse> {
273+
return this.http.delete<ApiResponse>(this.getServerPath() + this.options.signOutPath)
274+
// Only remove the localStorage and clear the data after the call
275+
.pipe(
276+
finalize(() => {
277+
this.localStorage.removeItem('accessToken');
278+
this.localStorage.removeItem('client');
279+
this.localStorage.removeItem('expiry');
280+
this.localStorage.removeItem('tokenType');
281+
this.localStorage.removeItem('uid');
282+
283+
this.authData = null;
284+
this.userType = null;
285+
this.userData = null;
286+
}
287+
)
288+
);
289289
}
290290

291291
// Validate token request
292-
validateToken(): Observable<any> {
293-
const observ = this.http.get<ApiResponse<UserData>>(this.getServerPath() + this.options.validateTokenPath).pipe(share());
292+
validateToken(): Observable<ApiResponse> {
293+
const observ = this.http.get<ApiResponse>(
294+
this.getServerPath() + this.options.validateTokenPath
295+
).pipe(share());
294296

295297
observ.subscribe(
296298
(res) => this.userData = res.data,
@@ -304,7 +306,7 @@ export class AngularTokenService implements CanActivate {
304306
}
305307

306308
// Update password request
307-
updatePassword(updatePasswordData: UpdatePasswordData): Observable<any> {
309+
updatePassword(updatePasswordData: UpdatePasswordData): Observable<ApiResponse> {
308310

309311
if (updatePasswordData.userType != null) {
310312
this.userType = this.getUserTypeByName(updatePasswordData.userType);
@@ -330,11 +332,11 @@ export class AngularTokenService implements CanActivate {
330332
}
331333

332334
const body = args;
333-
return this.http.put(this.getServerPath() + this.options.updatePasswordPath, body);
335+
return this.http.put<ApiResponse>(this.getServerPath() + this.options.updatePasswordPath, body);
334336
}
335337

336338
// Reset password request
337-
resetPassword(resetPasswordData: ResetPasswordData): Observable<any> {
339+
resetPassword(resetPasswordData: ResetPasswordData): Observable<ApiResponse> {
338340

339341
this.userType = (resetPasswordData.userType == null) ? null : this.getUserTypeByName(resetPasswordData.userType);
340342

@@ -343,7 +345,7 @@ export class AngularTokenService implements CanActivate {
343345
redirect_url: this.options.resetPasswordCallback
344346
};
345347

346-
return this.http.post(this.getServerPath() + this.options.resetPasswordPath, body);
348+
return this.http.post<ApiResponse>(this.getServerPath() + this.options.resetPasswordPath, body);
347349
}
348350

349351

src/app/example/output/output.component.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, Input } from '@angular/core';
2-
3-
import { AuthResponse } from './auth.model';
2+
import { ApiResponse} from '../../../../projects/angular-token/src/public_api';
43

54
@Component({
65
selector: 'app-output',
@@ -9,12 +8,12 @@ import { AuthResponse } from './auth.model';
98
})
109
export class OutputComponent {
1110

12-
output: AuthResponse = <AuthResponse>{};
11+
output: ApiResponse = <ApiResponse>{};
1312

1413
@Input()
15-
set data(res: AuthResponse) {
14+
set data(res: ApiResponse) {
1615

17-
this.output = <AuthResponse>{};
16+
this.output = <ApiResponse>{};
1817

1918
if (res != null) {
2019
this.output.status = res.statusText + ' (' + res.status + ')';

0 commit comments

Comments
 (0)