0.1.1 • Published 3 years ago
ngx-grs v0.1.1
Ngx-generic-rest-service
A lightweight, strongly typed & very easy-to-use Angular library that handles HTTP requests to decrease boilerplate when you write HTTP services.
Installation
npm install ngx-grs
or
yarn add ngx-grs
Usage
Create your Angular service (e.g., tasksService):
ng generate service tasks
Extend your Angular service from
NgxGenericRestService
:@Injectable({ providedIn: "root" }) export class TasksService extends NgxGenericRestService {}
- Call the constructor of the
NgxGenericRestService
class and pass-in thebaseUrl
andresourceName
:
That's it! You can now use your tasks service to perform all kind of HTTP requests@Injectable({ providedIn: "root" }) export class TasksService extends NgxGenericRestService { constructor() { super({ baseUrl: "https://example.com/api", // environment.apiUrl resourceName: "tasks", // API controller }); // endpoint: https://example.com/api/tasks } }
Perform HTTP request
Let's assume you have a
tasks-list
smart component:
The first thing you need to do is to inject your TasksService
:
@Component({
templateUrl: "./tasks-list.page.html",
styleUrls: ["./tasks-list.scss"],
})
export class TasksListPage implements OnInit {
constructor(private tasksService: TasksService) {}
ngOnInit(): void {}
}
You can now perform HTTP requests inside this component:
// Model
export interface Task {
id: number;
name: string;
done: boolean;
}
Get a list of tasks
tasks: Task[]; fetchTasks(): void { this.tasksService.list<Task[]>().subscribe(tasks => this.tasks = tasks); }
Get a single task
task: Task; fetchTask(taskId: number): void { this.tasksService.single<Task>(taskId).subscribe(task => this.task = task); }
Create a task
createTask(task: Task): void { this.tasksService.add<Task>(task).subscribe(); }
- Update a task with a PUT request
updateTask(taskId: number, task: Partial<Task>): void { this.tasksService.update<Task>(taskId, task).subscribe(); }
- Update a task with a PATCH request
updateTask(taskId: number, task: Partial<Task>): void { this.tasksService .update<Task>(taskId, task, { method: 'PATCH' }) .subscribe(); }
- Delete a task
deleteTasks(taskId: number): void { this.tasksService.delete<Task>(taskId).subscribe(); }
Default HttpClient request options
Option | Description | Used by |
---|---|---|
headers | Headers to be attached to a Request | List, Single, Add, Update, Delete |
params | Query parameters to be included in a Request. | List, Single, Add, Update, Delete |
observe | Determines the return type, according to what you are interested in observing. | List, Single, Add, Update, Delete |
reportProgress | Whether this request should be made in a way that exposes progress events. | List, Single, Add, Update, Delete |
responseType | The expected response type of the server. | List, Single, Add, Update, Delete |
withCredentials | Whether this request should be sent with outgoing credentials (cookies). | List, Single, Add, Update, Delete |
Custom HTTP options
Option | Description | Used by |
---|---|---|
urlRewrite | Rewrites the entire request API URL | List, Single, Add, Update, Delete |
urlPostfix | Adds a postfix to the API URL (useful to specify sub-resources) | List, Single, Add, Update, Delete |
method | Helps the service to understand if it is a PUT or a PATCH request (PUT by default) | Update |
mapFn | Transforms the API response to the desired output | List, Single, Add, Update, Delete |