Gathering detailed insights and metrics for rests
Gathering detailed insights and metrics for rests
Gathering detailed insights and metrics for rests
Gathering detailed insights and metrics for rests
Easily generate API client's SDK — organize and simplify API Requests — OOP-ish way.
npm install rests
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
7 Stars
25 Commits
1 Watching
2 Branches
1 Contributors
Updated on 12 May 2023
TypeScript (73.94%)
JavaScript (26.06%)
Cumulative downloads
Total Downloads
Last day
-11.8%
802
Compared to previous day
Last week
-10.3%
4,469
Compared to previous week
Last month
18%
22,988
Compared to previous month
Last year
136.2%
158,718
Compared to previous year
5
Work with requests in a modern and beautiful way.
Easily generate API client's SDK — organize and simplify HTTP Requests.
1api.login({ 2 user: 'test', 3 password: 'test' 4}) 5.then(({json})=>( 6 console.log(`Logged in!`) 7));
1fetch("https://example.com/login",{ 2 'method': 'POST', 3 'headers': { 4 'Content-Type': 'application/x-www-form-urlencoded' 5 }, 6 'data': `user=${user}&password=${password}` 7}).then((res) => (if(!res.ok){ return Promise.reject("error"))}) 8.then((res) => res.json()) 9.catch((err) => console.warn) 10.then(data)=> (console.log("Finally the response")));
& more
npm i rests
You should also install it globally in order to easily run the cli.
npm i rests -g
Recommended
1import Rests from 'rests';
Import it like this to get the Types & Intellisense suggestions on CommonJS
1const Rests = require("rests").default;
1import Rests from 'rests'; 2 3const API = Rests({ 4 $options: { 5 base: 'https://example.com' 6 }, 7 user:{ 8 login:{ 9 path: '/user/login', 10 method: 'POST', 11 params:{ 12 username:{ 13 required: true, 14 type: "string", 15 help: "A valid username is required", 16 validate: /\w+/ 17 }, 18 password: { 19 required: true, 20 help: "A valid password is required", 21 type: "string", 22 23 format: (password) => { 24 if(password.length < 8){ 25 throw new Error("The password must be at least 8 characters."); 26 } 27 return password; 28 } 29 30 } 31 } 32 }, 33 profile: { 34 $options:{ 35 params:{ 36 //Set authentication parameters for all requests in this category 37 authorization: {...} 38 } 39 } 40 info: { 41 ... 42 }, 43 update: { 44 ... 45 } 46 } 47 } 48}); 49 50export default API; 51
1import API from './API.js'; 2 3API.user.login({ 4 username: 'nice', 5 password: 'mypassword' 6}) 7.then((res)=>{ 8 console.log(res.json); 9 //Successful Response, body automatically parsed. 10}) 11.catch((res)=>{ 12 console.log(res.json || res.message); 13 //Error Response 14})
1import API from './API.js'; 2 3API.user.login({ 4 username: 'john', 5 password: 'short' 6}).catch((err) => { 7 console.log(err.field, err.message); 8 //Prints: password The password must be at least 8 characters. 9}); 10
A category is like a class instance, you can intialize it with new values/options.
You can set parameter values for all requests in a category scope
1const User = new api.user({ 2 authorization: 'user_auth_token' 3}); 4
You can also update the options for a category by using the special $options
key
1const User = new api.user({ 2 $options: { 3 on_error: (error)=>{ 4 if(error?.statusCode == 401){ 5 alert("Session has expired"); 6 } 7 } 8 } 9});
Rests comes with a simple CLI for generating types and API markdown reference.
Generate the types file ./api.d.ts
automatically and watch for changes
1> rests ./api.js --types --watch
Generate the markdown API refrence
1> rests ./api.js --docs
TikAPI is using Rests:
cookie
as parameter locationAPI.user(rawBodyBytes)
)An API category is an object consisting of Endpoint Objects or subcategories. A category can also contain these special keys:
$options
: Options for this category and it's subcategories, overriding other options. See Optionshelp
: A description of the category.path
: The request path or full URL, which can also contain named parameters.method
: The request method, GET,POST etc. (default: GET)enctype
: The body encode type for *only for requests that have body parameters:
json
(application/json)
(default)form
(multipart/form-data)
urlencode
(application/x-www-form-urlencoded)
params
: An object consisting of Params Objects.help
: A description of this endpointexample_response
: Example response used for documentation generationon_success
: See Optionson_error
: See Optionson_request
: See Options$other
: Any other custom option you may need to includename
: The parameter HTTP name, this defaults to the object key name.
required
: boolean
(default: false).
help
: A helpful message to throw if the parameter is invalid.
type
: Supported types:
"string"
"number"
"array"
"object"
"boolean"
"any"
(default)format
: A function to format the parameter value, or throw an error if it's invalid.
validate
: Regex validation.
default
: A default value.
location
: The location where this parameter will be in the HTTP request fields:
body
the param will be included in request body (default for POST request)query
the param will be URL encoded in request URL query (default for GET request)headers
the param will be included in request headerspath
the param will be included in request path
/get/{key}
.example
: Example values used for documentation generation
in
: Array of allowed values
max
: Maximum allowed value for number types
min
: Minimum allowed value for number types
The options object can be defined in every category using the special $options
key, all the subcatgories will inherit them.
Rested(endpoints, options?)
base
: This will be prepended before each requests path. (e.g https://example.com
)
sandboxBase
: For sandbox requests.
headers
: Key-value object of headers to include in all requests
params
: Params to include in all requests
values
: Key-value object store to set default values for all parameters
on_request
: A global hook function that is called before each request. Accepts an object of {url, options, params, key, instance, self}
where self is the current method function.
To modify the request:
1return {url, options}
To prevent the request from sending:
1return false
on_success
: A hook function that is called on successful response, you can also modify and return a different response. Accepts (response, request)
.
on_error
: A hook function that is called on errors. Accepts (error_response, request)
. You can also return a new error like this:
1return Promise.reject(CustomErrorResponse)
fetch_agent
: You can use this option to configure proxy if you're using node-fetch.
proxies
: Requests proxies dict, for python version only.
$other
: Any other custom option you may need to include
No vulnerabilities found.
No security vulnerabilities found.