users.service.ts
angular/examples/intermediate/users.service.ts
// @ts-nocheck
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, map, shareReplay } from 'rxjs';
export interface UserDto {
id: number;
name: string;
email: string;
role: 'viewer' | 'editor' | 'admin';
}
@Injectable({ providedIn: 'root' })
export class UsersService {
private http = inject(HttpClient);
getUsers(): Observable<UserDto[]> {
return this.http
.get<UserDto[]>('/api/users')
.pipe(shareReplay({ bufferSize: 1, refCount: true }));
}
saveUser(payload: Omit<UserDto, 'id'>): Observable<UserDto> {
return this.http
.post<UserDto>('/api/users', payload)
.pipe(map((user) => ({ ...user, role: user.role ?? 'viewer' })));
}
}
Artigos relacionados
server.js
server.js — javascript source code from the angular learning materials (angular/comprehensive-demo/server/server.js).
Ler artigo →app.component.ts
app.component.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.component.ts).
Ler artigo →app.config.ts
app.config.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.config.ts).
Ler artigo →app.routes.ts
app.routes.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.routes.ts).
Ler artigo →auth.guard.ts
auth.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/auth.guard.ts).
Ler artigo →role.guard.ts
role.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/role.guard.ts).
Ler artigo →