sweet-request
v1.0.5259
Published
sweet request developed with solid architecture
Downloads
5
Readme
sweet-request
Total abstraction of http request with standalone architecture if using (S.O.L.I.D) principles.
Helps you bring all your api requests and models in one place.
Finally Adding Readme documentations for this project!
Here are basic usage
First
install via npm
npm install sweet-request
or clone git repo.
create a remote folder with the following folder structure
---remote
---model
---all models files here
---controller
---all controller files here
---api
---all api files here
create your models eg. User.tsx
User.tsx
export class UserModel{
public email:string;
public role:string;
constructor(email:string,role:string){
this.email=email;
this.role=role;
}
public static fromJSON(json){
return new UserModel(json.email,json.role);
}
}
inside the api folder
UserApi.tsx
export class UserLoginApi extends RequestManager<DataResources<ResponseMessage>,requestprop>{
constructor(form:FormData){
super(RequestMethod.POST,"api/login",{data:form})
}
//executes after your request is complete
public data(data:any):DataResources<ResponseMessage>{
return new DataSuccess(ResponseMessage.fromJson(data));
}
}
export class UserRegisterApi extends RequestManager<DataResources<ResponseMessage>,requestprop>{
constructor(form:FormData){
super(RequestMethod.POST,"api/register",{data:form})
}
//executes after your request is complete
public data(data:any):DataResources<ResponseMessage>{
return new DataSuccess(ResponseMessage.fromJson(data));
}
}
inside the controller folder
UserController.tsx
export class UserController{
public static function login(form:FormData){
return new UserLoginApi(form).sendRequest();
}
public static function register(form:FormData){
return new UserLoginApi(form).sendRequest();
}
}
inside the main folder
App.tsx
const = App()=>{
const submit=(ev)=>{
ev.preventDefault();
//send login request here
UserController.login(form)
.then(data=>console.log(data.message))
.catch(err=>console.log(err.message));
}
return <>{/*Login Form Here....*/}</>
}
RUN
npm run dev for vite
npm start for create-react-app
Buy me coffee Next time
for more colloborators feel free