Skip to content

Commit

Permalink
Add typescript interfaces for the endpoints and a basic ApiService
Browse files Browse the repository at this point in the history
  • Loading branch information
Macavity committed Aug 3, 2023
1 parent 564848c commit 7a97127
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 12 deletions.
44 changes: 44 additions & 0 deletions apps/client/src/app/services/api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import {
Transaction,
User,
PagedCollection,
} from '@money-sprouts/shared/domain';

@Injectable({
providedIn: 'root',
})
export class ApiService {
/**
* TODO replace with .env variable
*/
private baseUrl = 'http://localhost:8081';

constructor(private http: HttpClient) {}

getUsers(): Observable<PagedCollection<User>> {
return this.http.get<PagedCollection<User>>(`${this.baseUrl}/api/users/`);
}

getUserById(id: string): Observable<User> {
return this.http.get<User>(`${this.baseUrl}/api/users/${id}`);
}

getTransactions(): Observable<PagedCollection<Transaction>> {
return this.http.get<PagedCollection<Transaction>>(
`${this.baseUrl}/api/transactions/`
);
}

getTransactionById(id: string): Observable<Transaction> {
return this.http.get<Transaction>(`${this.baseUrl}/api/transactions/${id}`);
}

getTransactionsByUserId(userId: string): Observable<Transaction> {
return this.http.get<Transaction>(
`${this.baseUrl}/api/users/${userId}/transactions`
);
}
}
5 changes: 0 additions & 5 deletions apps/client/src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable, map, of, tap } from 'rxjs';

export interface User {
username: string;
avatar: string;
}

@Injectable({
providedIn: 'root',
})
Expand Down
7 changes: 5 additions & 2 deletions libs/shared/domain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './lib/models/user';
export * from './lib/models/transaction';
export * from './interfaces/user';
export * from './interfaces/transaction';
export * from './interfaces/types';
export * from './interfaces/ApiResource';
export * from './interfaces/Collection';
21 changes: 21 additions & 0 deletions libs/shared/domain/src/interfaces/Collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ApiResource } from './types';

export interface Pagination {
'hydra:first'?: string;
'hydra:previous'?: string;
'hydra:next'?: string;
'hydra:last'?: string;
}

export interface PagedCollection<T> extends ApiResource {
'@context'?: string;
'@type'?: string;
'hydra:firstPage'?: string;
'hydra:itemsPerPage'?: number;
'hydra:lastPage'?: string;
'hydra:member'?: T[];
'hydra:nextPage'?: string;
'hydra:search'?: object;
'hydra:totalItems'?: number;
'hydra:view'?: Pagination;
}
13 changes: 13 additions & 0 deletions libs/shared/domain/src/interfaces/dataAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const normalizeLinks = (
value: string | string[] | undefined
): string[] => {
if (!value) {
return [];
}

if (typeof value === "string") {
return value.split(",");
}

return value;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Transaction {
"@id"?: string;
'@id'?: string;
title?: string;
type?: number;
value?: number;
Expand Down
22 changes: 22 additions & 0 deletions libs/shared/domain/src/interfaces/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type TError = SubmissionError | Error | null;

export interface ApiResource {
"@id": string;
}

export interface SubmissionErrors {
[p: string]: string;
}

export class SubmissionError extends Error {
private readonly _errors: SubmissionErrors;

constructor(message: string, errors: SubmissionErrors) {
super(message);
this._errors = errors;
}

public get errors(): SubmissionErrors {
return this._errors;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface User {
"@id"?: string;
'@id'?: string;
email?: string;
name?: string;
roles?: any;
roles?: string[];
password?: string;
allowance?: number;
nextPayday?: Date;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start-client": "nx serve client",
"start-server": "nx serve server",
"build:dev": "nx run client:build --configuration=development",
"generate:interfaces": "npm init @api-platform/client http://localhost:8101/api/ libs/shared/domain/src/lib/ -- --generator typescript"
"generate:interfaces": "npm init @api-platform/client http://localhost:8101/api/ libs/shared/domain/src/ -- --generator typescript"
},
"private": true,
"dependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/Factory/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ protected function getDefaults(): array
'email' => self::faker()->email,
'name' => self::faker()->firstName,
'password' => self::faker()->password,
'avatar' => self::faker()->randomElement(['/build/assets/images/avatar_female.png', '/build/assets/images/avatar_male.png']),
'avatar' => self::faker()->randomElement([
'/build/assets/images/avatar_female.png',
'/build/assets/images/avatar_male.png'
]),
'roles' => [UserRole::USER],
];
}
Expand Down

0 comments on commit 7a97127

Please sign in to comment.