Observed since 20 Oct, 20:15 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
REST Client built on top of axios and zod packages
4rest (Forest) is a promise based, HTTP REST Client built on top of axios and zod packages suggesting easy to use and extensively customizable, extendable and configurable services with built in CRUD methods and type safe request functions to API.
Using npm
npm install 4rest
Using yarn
yarn add 4rest
The package was created to help developers to get their requests functions from client to API up and runing quickly and comfortably, as well as making configuration for requests all centeralized in one spot at the code base of the app. Using the package, you can divide your requests functions to API to different services based on the models (data structures) of the API. When you have initiallized your service, you can make request to API at every place in your app with type safety based on the service configuration.
Note: The package was meant to be used with rest API only and it does not support graphql.
💎 Service with built in CRUD Methods:
🎨 Extended Services with option to add additional methods extending out CRUD methods that comes built in with the base service you create
🧱 Services Built on fully configurable axios Instance
⚙️ Convenient Configuration with custom routes, request configuration, and payload data (body property of request) key custom define. all of that configuration is per service or can be set globally on fetching instance as well
🛡️ Data Type Validation - API fetching requests, payloads and responses data validation with zod schemas
🧪 Test Proof - 4rest has 100% test coverage
import forest from "4rest";
const instance = forest.create({ baseURL: "http://localhost:5000" });
import { instance } from "./forestInstance";
import { UserWithId, User } from "./types";
const userService = instance.createService<UserWithId, User>("user");
// GET http://localhost:5000/user
async function getUsers() {
const users: User[] = await (await userService.getAll()).data;
}
// GET http://localhost:5000/user/:id
async function getUserById(id: string) {
const user: User = await (await userService.getById(id)).data;
}
// DELETE http://localhost:5000/user
async function deleteAllUsers() {
const usersDeleted: User[] = await (await userService.deleteAll()).data;
}
// DELETE http://localhost:5000/user/:id
async function deleteUserById(id: ObjectId) {
const userDeleted: User = await (await userService.deleteById(id)).data;
}
// POST http://localhost:5000/user
async function createUser(newUser: User) {
const userCreated: User = await (await userService.post(newUser)).data;
}
// PATCH http://localhost:5000/user
async function updateUser(partialUser: Partial<User>) {
const updatedUser: User = await (await userService.patch(partialUser)).data;
}
// PATCH http://localhost:5000/user/:id
async function updateUserById(id: ObjectId, partialUser: Partial<User>) {
const updatedUser: User = await (await userService.patchById(id, partialUser)).data;
}
// PUT http://localhost:5000/user
async function updateUser(partialUser: Partial<User>) {
const updatedUser: User = await (await userService.put(partialUser)).data;
}
// PUT http://localhost:5000/user/:id
async function updateUserById(id: ObjectId, partialUser: Partial<User>) {
const updatedUser: User = await (await userService.putById(id, partialUser)).data;
}
import { ForestService } from "4rest";
import { instance } from "./forestInstance";
export class UserService extends ForestService<UserWithId, User> {
constructor() {
super("user", instance, {
/* service config will go here */
});
/* prefix for request url will be "user" */
}
public getByName = (name: string) => this.methodsCreator.getByParam<UserWithId, string>({ suffix: "name" })(name);
public getByNameWithQuery = (name: string) =>
this.methodsCreator.get<UserWithId>({ route: "name", config: { params: { name } } })();
public isEmailTaken = (email: string) =>
this.methodsCreator.getByParam<boolean, string>({ route: ["email", "taken"] })(email);
}
Notes:
You must include constructor in the structure that is shown above in your extended service in order for it to work properly
Extended Service will include all the base service methods as well as the additional ones you have added
Read about Methods Creator to add new methods to extended service easily
const userService = new UserService();
async function getUserName(name: string) {
const user: User = await (await userService.getByName(name)).data;
}
async function getUserNameByQuery(name: string) {
const user: User = await (await userService.getByNameWithQuery(name)).data;
}
async function isEmailTaken(email: string) {
const isEmailTaken: boolean = await (await userService.isEmailTaken(email)).data;
}
axios Instance with forest.create() Function
import forest from "4rest";
/* Customised Forest Instance can be based on
AxiosInstance, AxiosRequestConfig */
const forestInstance = forest.create({
axiosSettings: /* Here goes instance or config*/,
globalServiceConfig: /* Here goes service configuration that will be applied by default to
all created service from these ForestInstance*/
});
See What Service Config includes down below.
Note: if a created service will have a config of it's own, it's config properties will be overriding the global service config properties, which means the more specific property is the one that will be in use eventually
Options to configure forest.create()
interface InstanceConfig {
axiosSettings?: AxiosSettings;
globalServiceConfig?: ServiceConfig;
}
Note: Configuration is completly optional, and if axiosSettings are empty the forest instance will be based on the base AxiosInstance
You can access the totally regular AxiosInstance that the ForestInstance is based on which contains all of axios methods on it:
Access it using the axiosInstance property on created ForestInstance
import { forestInstance } from "./instance";
const response = forestInstance.axiosInstance.get("localhost:5000/users" /* Here goes axios config*/);
createService() Method:You may want to change few of the built in service method route to extend the prefix based on the API you are working with.
Do it easily by configuring an extended route for each method you want.
Note: method with no configured extended route will send request to basic route: baseUrl/prefix or baseUrl/prefix/param
Example:
import { instance } from "./forestInstance";
const userService = instance.createService<User>("user", {
/* All Service built in CRUD methods route control ( string | string[] ) */
routes: {
getAll: ["get", "all"], // GET http://localhost:5000/user/get/all
deleteAll: "all", // DELETE http://localhost:5000/user/all
deleteById: "id", // DELETE http://localhost:5000/user/id/:id
...,
post: "create", // POST http://localhost:5000/user/create
patch: "update" // PATCH http://localhost:5000/user/update
patchById: "update" // PATCH http://localhost:5000/user/update/:id
}
});
You can set a requestConfig of type AxiosRequestConfig for attaching metadata to a request (like headers, params, etc.)
requestConfig can be set for each method seperatly or make one general config for all methods
Note: if a method has its own specific requestConfig, it will be used over the general one
Example:
import { instance } from "./forestInstance";
const userService = instance.createService<UserWithId, User, number>("user", {
requestConfigByMethod: {
/* Request Config Per Method */
getAll: { params: { page: 1, size: 10 } },
getById: { maxRedirects: 3 },
},
requestConfig: {
/* Request Config For All Methods */
headers: {
Authentication: "Bearer Header",
},
},
});
For HTTP methods with payload (Post, Patch, Put) you can set a payloadKey for setting the payload data on the key you want inside the body of the request
// By Default
request: {
body: data,
...
}
// After Setting payloadKey
request: {
body: {
[payloadKey]: data
},
...
}
payloadKey can be set for each HTTP payload method seperatly or set one general config for all methods
Note: if a method has its own specific payloadKey, it will be used over the general one
Example:
import { instance } from "./forestInstance";
const userService = instance.createService<UserWithId, User, number>("user", {
payloadKey: "update",
payloadKeyByMethod: { post: "data" },
});
You can configure in advance how to handle each request when it is completes successfully or failing and throws error.
Set up onSuccess function which parameters will be the response from successful request and optionally the metadata of the request.
Set up onError function which parameters will be the error that was thrown from a failed request and optionally the metadata of the request.
Without metadata parameter used:
const userService = forestInstance.createService<UserWithId, User, number>("user", {
onSuccess: (response) => response.data,
onError: (error) => {
return { error, msg: "error" };
},
});
With metadata parameter used:
const userService = forestInstance.createService<UserWithId, User, number>("user", {
onSuccess: (response, metadata) => {
console.log(metadata.serviceConfig.validation);
return response.data
},
onError: (error, metadata) => {
console.log(metadata.serviceConfig.routes);
return { error, msg: "error" };
},
});
You can also set different onSuccess and onError functions for each service method in the following way:
const userService = forestInstance.createService<UserWithId, User, number>("user", {
onSuccessByMethod: {
getAll: (response) => response.data,
getById: (response) => response.status,
...,
post: (response, metadata) => {
console.log(metadata.serviceFunction);
return response.data;
},
},
onErrorByMethod: {
getById: (error) => {
return { error, msg: "errorByMethod" };
},
...,
post: (error, metadata) => {
console.log('error at method:', metadata.serviceFunction);
throw error;
},
},
});
If you want to make sure that the data you send to the API or recieved back from it, matches your data model schemas you can set validation config for service with zod schemas.
First of all, decalre your validation schemas using zod:
export const UserSchema = z.object({
name: z.string(),
email: z.string().optional(),
});
export const UserWithIdSchema = UserSchema.extend({
_id: z.number(),
});
then apply zod schemas to the fitting property of service configuration object:
import { UserWithIdSchema, UserSchema } from "../../types/user";
const userService = forestInstance.createService<UserWithId, User, number>("user", {
validation: {
types: { resoponseData: UserWithIdSchema },
}
}
});
Examples for data recieved back from API:
// Valid Data, no error will be thrown
[{ _id: 1, name: "John Smith" }];
// Invalid data, will throw error
[{ name: "John Smith" }];
import { UserWithIdSchema, UserSchema } from "../../types/user";
const userService = forestInstance.createService<UserWithId, User, number>("user", {
validation: {
onMethods: {
post: { types: { requestPayload: UserSchema, resoponseData: UserWithIdSchema } },
getById: { types: { resoponseData: UserSchema.strict() } },
},
},
});
Examples for payload sent to API:
// Valid Data, no error will be thrown
{ name: "John Smith", email: "john.smith@gmail.com" };
// Invalid data, will throw error
{ email: "john.smith@gmail.com" };
import { UserWithIdSchema, UserSchema } from "../../types/user";
const userService = forestInstance.createService<UserWithId, User, number>("user", {
validation: {
onMethods: {
post: { types: { requestPayload: UserSchema, resoponseData: UserWithIdSchema } },
getById: { types: { resoponseData: UserSchema.strict() } },
},
types: { resoponseData: UserWithIdSchema },
},
});
Note: if a method has its own specific validation, it will be used over the global one
To help you construct new service methods, ForestService class comes included with property named methodsCreator that you can utilize to create new methods easily.
methodsCreator property includes the following helper methods:
Examples:
public getByName = (name: string) => this.methodsCreator.getByParam<UserWithId, string>({ suffix: "name" })(name);
public getByNameWithQuery = (name: string) => this.methodsCreator.get<UserWithId>({ route: "name", config: { params: { name } } })();
public isEmailTaken = (email: string) => this.methodsCreator.getByParam<boolean, string>({ route: ["email", "taken"] })(email);
interface BaseConfig {
route?: Route; // string or string[]
config?: AxiosRequestConfig;
serviceFunction?: ServiceFunction;
validation?: MethodValidationConfig; // configuartion object based on Zod Schemas;
onSuccess?: OnSuccessFunction;
onError?: OnErrorFunction;
}
interface PayloadConfig {
route?: Route;
config?: AxiosRequestConfig;
validation?: MethodValidationConfig;
onSuccess?: OnSuccessFunction;
onError?: OnErrorFunction;
key?: Key; // string
}
interface ByParamConfig {
route?: Route;
suffix?: Route; // string or string[] added after the param in request url
config?: AxiosRequestConfig;
validation?: MethodValidationConfig;
onSuccess?: OnSuccessFunction;
onError?: OnErrorFunction;
}
interface PayloadByParamConfig {
route?: Route;
suffix?: Route;
config?: AxiosRequestConfig;
validation?: MethodValidationConfig;
onSuccess?: OnSuccessFunction;
onError?: OnErrorFunction;
key?: Key;
}
Service has generic types to control the following types of the service methods
class ForestService<ResponseData = any, PayloadData = Response, IdType = string>
By doing so, Typescript will force you to give it the parameters with matching types when calling the service methods or will recognize alone the response data type for more comfortable auto-completion in the future.
You pass this generic types when creating new service with createService() function of a forestInstance
Example:
import { instance } from "./forestInstance";
import { UserWithId, User } from "./types";
const userService = instance.createService<UserWithId, User, string>("user");
// ResponseData - UserWithId
// PayloadData - User
// IdType - string
By default the service takes the types you passed to it and transform them to each service method in the following way:
ResponseData[]ResponseDataIdTypeResponseData[]ResponseDataIdTypeResponseDataPayloadDataResponseDataPartial<PayloadData>ResponseDataPartial<PayloadData>IdTypeResponseDataPartial<PayloadData>ResponseDataPartial<PayloadData>IdTypeif you would like to change one or more of this method types, you can do it when calling the method by passing to it generic types that will be relevant to the this method only, at the place you are calling it.
Example:
Lets say you would like to change the type of the response data that comes back from calling to the post method from ResponseData to boolean because the API you working with is returns only with data that indicates whether or not an User has been created successfully
You can do that in the following way:
const data: boolean = await(await userService.post<boolean>(/* newUserData */)).data;
Each request function (method) in 4rest includes metadata about the current request.
Method metadata includes the following properties:
interface Metadata {
serviceConfig?: ServiceConfig; // current service config
serviceFunction?: ServiceFunction; // name of one of the built in base service functions
}
Function For Handling Successful Requests:
type OnSuccessFunction = <T>(value: AxiosResponse<T>, metadata?: Metadata) => any;
Function For Handling Failed Requests:
type OnSuccessFunction = <T>(value: AxiosResponse<T>, metadata?: Metadata) => any;
ValidationTypes - validation can be set on either request payload or response data
interface ValidationTypes<T = ZodSchema> {
requestPayload?: T;
resoponseData?: T;
}
ServiceValidationConfig - lets you set validation on each method seperatly via onMethods property or globaly for all methods via types property
interface MethodsValidation {
types: ValidationTypes;
}
interface ServiceValidationConfig {
types: ValidationTypes;
onMethods?: Partial<Record<ServiceFunction, MethodsValidation>>;
}
MethodValidationConfig - you can set validation specificlly on custom method you create for your extended service
type MethodValidationConfig = ValidationTypes;
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 2.0.0Registry latest | 7 May 2022 | 23 | |
| 1.2.1 | 20 Apr 2022 | 22 | |
| 1.2.0 | 20 Apr 2022 | 22 | |
| 1.1.1 | 7 Mar 2022 | 22 | |
| 1.1.0 | 7 Mar 2022 | 22 | |
| 1.0.10 | 1 Mar 2022 | 23 | |
| 1.0.9 | 1 Mar 2022 | 23 | |
| 1.0.8 | 1 Mar 2022 | 23 | |
| 1.0.7 | 22 Feb 2022 | 23 | |
| 1.0.5 | 21 Feb 2022 | 23 | |
| 1.0.4 | 21 Feb 2022 | 23 | |
| 1.0.3 | 21 Feb 2022 | 23 | |
| 1.0.2 | 21 Feb 2022 | 23 | |
| 1.0.1 | 21 Feb 2022 | 23 | |
| 1.0.0 | 21 Feb 2022 | 23 |
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 20:15 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:2b119abea3855a9fd3aacbf0a5633bee7ec03925e1ba62955a536db8d3f537ea