nest-utilities-client
v3.3.5
Published
Provides a generic HTTP service which interfaces with the nest-utilities package.
Downloads
80
Maintainers
Readme
Basic usage
The package revolves around the CrudService
. Extending this service will grant you methods to call upon the endpoints generated by nest-utilities as well as the available query parameters.
import { CrudService } from "nest-utilities-client";
import { IUser } from "../interfaces"
class UserService extends CrudService<IUser> {
constructor() {
super("http://localhost:3000/users");
}
}
HTTP service
Out of the box the CrudService
will supplement your extended class with a basic HTTP service to execute your calls. In a lot of cases you'll want to create generic error handlers and define headers to send along with your requests.
You can customize the HTTP service by creating a new class which extends the HTTP service offered by the package. In this new class you will be prompted to implement abstract methods which help you tailor the service with ease.
import { HttpService } from "nest-utilities-client";
import { messageService } from "../services";
class CustomHttpService extends HttpService {
getHeaders(url: string, init: RequestInit) {
return {
"Authorization": localStorage.session
};
}
onRequestError(error: Error) {
messageService.notify(error.message);
}
}
import { CrudService } from "nest-utilities-client";
import { CustomHttpService } from "../services";
import { IUser } from "../interfaces"
class UserService extends CrudService<IUser> {
constructor() {
super("http://localhost:3000/users", new CustomHttpService());
}
}