rest-in-model
model based REST consumer library.
Installing
npm install rest-in-model
Usage
Imports
// full import;// or you can import by destructuring;
Settings
Endpoint configuration
// Add single endpointsettings;// Add multiple endpoint. And in this case can be only one default endpoint, otherwise throw errorsettings;// set default endpoint uses that cases: if either not given default endpoint or desired to change default endpointsettings;
Api Paths configurations
// Add single api pathsettings;settings;// Add multiple api path. And in this case can be only one default api path, otherwise throw error// truesettings;// falsesettings;// set default api path uses that cases: if either not given default api path or desired to change default api pathsettings;
Set constant header configuration for each requests
// set common headers for all models with setHeader method of settingssettings;
You can add additional model-specific headers to each model in
getConfig
method while defining model config. SeeUser
model definition below:
Model Definition
User model:
;{return// you can add additional headersheaders:'Content-Type': 'multipart/form-data'fields:id: primary: truename: {}username: /*map: 'user_name'*/email: {}company: {}phone: /*default: null*/website: {}address: {}// Normally you don't need to do this. But sometimes back-end doesn't/can't give what you want...// You can get any child from response as result list to convert if you need.responseresultcontentspaths:default: 'users'posts: 'users/{userId}/posts'//endpointName: '',//apiPathName: '',;}moduleexports = User;
Post model:
;{returnfields:id: primary: truetitle: {}body: {}userId: {}paths:default: 'posts'userPosts: 'users/{userId}/posts'//endpointName: '',//apiPathName: '',}moduleexports = Post;
Methods
; const userInstance = ; // all returns PromiseuserInstance;User; userInstance;User; User;Useralloptions;
Detailed Explanation
userInstance.save(options);
options: { path, patch }
Property | Description | Type | Default Value |
---|---|---|---|
path(optional) | one of the path attribute name in paths object defined in model | string |
default |
patch(optional) | array of model fields that need to be updated with patch request | string[] |
- |
userInstance; // userInstance.id === undefined// XHR finished loading: POST "https://jsonplaceholder.typicode.com/users" userInstance; // userInstance.id !== undefined// XHR finished loading: PUT "https://jsonplaceholder.typicode.com/users/(:userId)" userInstance; // userInstance.id !== undefined// XHR finished loading: PATCH "https://jsonplaceholder.typicode.com/users/(:userId)"
User.save(options);
options: { model, path, patch }
Property | Description | Type | Default Value |
---|---|---|---|
model(required) | instance of Model extended from RestBaseModel | instance of RestBaseModel |
- |
path(optional) | one of the path attribute name in paths object defined in model | string |
default |
patch(optional) | array of model fields that need to be updated with patch request | string[] |
- |
const userInstance =name: 'engin üstün'username: 'enginustun'email: 'enginustun@outlook.com'company: '-'phone: '-'website: '-'address: '-';User;// XHR finished loading: POST "https://jsonplaceholder.typicode.com/users"
userInstance.delete(options);
options: { path }
Property | Description | Type | Default Value |
---|---|---|---|
path(optional) | one of the path attribute name in paths object defined in model | string |
default |
userInstance; // userInstance.id !== undefined// XHR finished loading: DELETE "https://jsonplaceholder.typicode.com/users/(:userId)"userInstance; // userInstance.id doesn't matter// XHR finished loading: DELETE "https://jsonplaceholder.typicode.com/users/4"
User.delete(options);
options: { id, path }
Property | Description | Type | Default Value |
---|---|---|---|
id(required) | required id parameter of model will be deleted. if it is not provided, there will be an error thrown | number\|string |
- |
path(optional) | one of the path attribute name in paths object defined in model | string |
default |
User; // throws an errorUser;// XHR finished loading: DELETE "https://jsonplaceholder.typicode.com/users/4"
User.get(options);
options: { id, path, pathData, queryParams }
Property | Description | Type | Default Value |
---|---|---|---|
id(required) | required id parameter of model will be requested from server. if it is not provided, there will be an error thrown | number\|string |
- |
resultField(optional) | Better to define as config in model class for related field(s) for string: if the response is an object, result will be converted based on this name from the response. if this property is not provided or 'response[resultField]' is a falsy value, the response will be assumed as model itself and result will be converted from the response directly. for function: you can return any of child/sub-child of given response object. |
string\|(response) => response.what.you.need |
- |
path(optional) | one of the path attribute name in paths object defined in model | string |
default |
pathData(optional) | object that contains values of variables in path specified | object |
- |
queryParams(optional) | object that contains keys and values of query parameters | object |
- |
User; // throws an errorUser;// XHR finished loading: GET "https://jsonplaceholder.typicode.com/users/2"// see User.all() function for usage of pathData property.
User.all(options);
options: { resultList, resultListField, resultListItemType, path, pathData, queryParams }
Property | Description | Type | Default Value |
---|---|---|---|
resultList(optional) | array object that will be filled models into it | [] reference |
- |
resultListField(optional) | Better to define as config in model class for related field(s) for string: if the response is an object, result list will be converted based on this name from the response. if this property is not provided or 'response[resultListField]' is not an array, the response will be assumed as model list itself and result will be converted from the response directly. for function: you can return any of child/sub-child of given response object. |
string\|(response) => response.what.you.need |
- |
resultListItemType(optional) | it needs to be provided if the result type is different from class type itself. | a model class that is inherited from 'RestBaseModel' | itself |
path(optional) | one of the path attribute name in paths object defined in model | string |
default |
pathData(optional) | object that contains values of variables in path specified | object |
- |
queryParams(optional) | object that contains keys and values of query parameters | object |
- |
Userall;// XHR finished loading: GET "https://jsonplaceholder.typicode.com/users"// recommended usage is abovevar list = ;Userall resultList: list;// XHR finished loading: GET "https://jsonplaceholder.typicode.com/users"// path, pathData and resultListItemType usageUserall path: 'posts' pathData: userId: 2 resultListItemType: Post;// XHR finished loading: GET "https://jsonplaceholder.typicode.com/users/2/posts"// it works properly but this way is not recommended.// best practice of this usage is belowPostall path: 'userPosts' pathData: userId: 2;// sends same request as above// XHR finished loading: GET "https://jsonplaceholder.typicode.com/users/2/posts"