-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typescript interfaces for the endpoints and a basic ApiService
- Loading branch information
Showing
10 changed files
with
113 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
2 changes: 1 addition & 1 deletion
2
.../domain/src/lib/interfaces/transaction.ts → ...ared/domain/src/interfaces/transaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
.../shared/domain/src/lib/interfaces/user.ts → libs/shared/domain/src/interfaces/user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters