Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { provideStore } from '@ngxs/store';
import { withNgxsReduxDevtoolsPlugin } from '@ngxs/devtools-plugin';
import { providePrimeNG } from 'primeng/config';
import Aura from '@primeng/themes/aura';
import { provideHttpClient } from '@angular/common/http';

export const appConfig: ApplicationConfig = {
providers: [
Expand All @@ -16,5 +17,6 @@ export const appConfig: ApplicationConfig = {
preset: Aura,
},
}),
provideHttpClient(),
],
};
8 changes: 8 additions & 0 deletions src/app/core/services/json-api/json-api.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface JsonApiResponse<T> {
data: ApiData<T> | ApiData<T>[];
}

export interface ApiData<T> {
id: string | number;
attributes: T;
}
30 changes: 30 additions & 0 deletions src/app/core/services/json-api/json-api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, Observable } from 'rxjs';
import {
ApiData,
JsonApiResponse,
} from '@core/services/json-api/json-api.entity';

@Injectable({
providedIn: 'root',
})
export class JsonApiService {
constructor(private http: HttpClient) {}

get<T>(url: string): Observable<T> {
return this.http
.get<JsonApiResponse<T>>(url)
.pipe(map((response) => (response.data as ApiData<T>).attributes));
}

getArray<T>(url: string): Observable<T[]> {
return this.http
.get<JsonApiResponse<T>>(url)
.pipe(
map((response) =>
(response.data as ApiData<T>[]).map((item) => item.attributes),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface UserUS {
full_name: string;
given_name: string;
}
9 changes: 9 additions & 0 deletions src/app/core/services/mappers/users/users.mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { User } from '@core/services/user/user.entity';
import { UserUS } from '@core/services/json-api/underscore-entites/user/user-us.entity';

export function mapUserUStoUser(user: UserUS): User {
return {
fullName: user.full_name,
givenName: user.given_name,
};
}
4 changes: 4 additions & 0 deletions src/app/core/services/user/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface User {
fullName: string;
givenName: string;
}
19 changes: 19 additions & 0 deletions src/app/core/services/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { inject, Injectable } from '@angular/core';
import { map, Observable } from 'rxjs';
import { JsonApiService } from '@core/services/json-api/json-api.service';
import { User } from '@core/services/user/user.entity';
import { UserUS } from '@core/services/json-api/underscore-entites/user/user-us.entity';
import { mapUserUStoUser } from '@core/services/mappers/users/users.mapper';

@Injectable({
providedIn: 'root',
})
export class UserService {
jsonApiService = inject(JsonApiService);

getMe(): Observable<User> {
return this.jsonApiService
.get<UserUS>('https://api.test.osf.io/v2/users/me')
.pipe(map((user) => mapUserUStoUser(user)));
}
}