Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Easily generate API client's SDK — organize and simplify API Requests — OOP-ish way.
Easily generate API client's SDK — organize and simplify API Requests. If you find yourself repeating HTTP requests code blocks, then this library is for you.
fetch("https://example.com/login",{
'method': 'POST',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
'data': 'user=test&password=test'
}).then((res) => (if(!res.ok){ throw new Error("error")})
.then((res) => res.json())
.catch((err) => console.warn)
.then(data)=> (console.log("Finally the response")));
api.login({
user: 'test',
password: 'test'
})
.then((data)=>(console.log(`Logged in!`)));
No dependencies, using Fetch API
Documentation Generator
Multiple API categories & subcategories
Elegant parameter handling
Universal support, small size (2.9KB)
For Browsers & Node.js
npm i wrape
You start by writing a JSON schema of all your API endpoints. You can split requests into categories and subcategories.
import Wrape from 'wrape';
const endpoints = {
user:{
login:{
path: '/user/login',
method: 'POST',
params:{
username:{
required: true,
},
password: {
required: true,
help: "The password must be at least 8 characters.",
validate: (password) => { return password.length >= 8;}
}
}
}
}
}
const api = Wrape(api_config,{ base: 'https://example.com'});
api.user.login({username: 'john', password:'short'}).catch((err) => {
console.log(err.message); //The password must be at least 8 characters.
});
api.user.login({username: 'john', password:'wrong_password'})
.catch((res) => {
console.log(res.json.message); //User password is invalid.
console.log(res.status); //401
});
Some projects using Wrape:
An API category is an object consisting of Endpoint Objects or subcategories. A category can also contain special keys:
$options
: Set options for this category, same object as Gloabl Options$help
: A description used for endpoint generationmethod
: The request method ,GET,POST etc.path
: The request path or full URL, which can also contain named parameters, check exmaple below.enctype
: The body encode type for *only for requests that have body parameters:
form
(multipart/form-data)
(default)url
(application/x-www-form-urlencoded)
json
(application/json)
text
(text/plain)
params
: An object consisting of Params Objects.response
: A hook function for modifying the response.request
: A hook function for modifying the request.help
: A description used for documantaion generationexample_response
: Example response used for documentation generationname
: By default the param name is the Param object key.required
: If this param is required or not.format
: A function to format the parameter value.validate
: Validate the param values, it can be:
in
: Array of valid allowed valueshelp
: An error message to throw if param is not valid, or required.default
: A default value for this param.location
: The location where this parameter will be in http request fields, it can be:
body
the param will be encoded in body as form data (default for POST request)query
the param will be URL encoded in URL query (default for GET request)headers
the param will be set in request headerspath
the param will be set in request path, you must declare the named parameters in path.example
: Example values used for documentation generationThis is the global options you set when you initalize Wrape, these options can be overridden by Category options.
Wrape(endpoints,global_options)
base
: The base Origin for all endpoints, will be prepended to each request path, default is empty.headers
: Append Headers to all requestsvalues
: An object of {param_key : param_value}
, useful for setting default values for all categories and endpoints.sender
: Use a custom function to send request -> custom_fetch(url, options, wrape_options, response_middleware?)
request_middleware
: Global hook pre-request functionresponse_middleware
: Global hook on response functionfetch_parse
: Parse Fetch Response (await JSON body\await body text) (default true)fetch_error_handler
: Function to handle fetch errorsfetch_agent
: You can use this option to set proxy if you're using node-fetch.You can generate a Markdown reference of the API, just like this:
api.$docs({
output: "API.md",
});
The response of request is parsed based when fetch_parse
option is true, returning an object like this:
{
status: 200,
statusText: 'OK',
headers: {...},
json: {'message': 'success'},
}
Depending on the content type, you can get the body as text
or json
.
In this example we declare a user category and set it's options with the special $options
key.
We also declare a information
endpoint which contains path
parameters.
import Wrape from 'wrape';
const endpoints = {
user:{
//This $options: A special key which overrides Global Options for this category and it's subcategories
$options:{
base: 'https://example.com/login'
},
profile:{
information:{
path: '/profile/:id',
method: 'GET',
params:{
id:{
required: true,
help: "The user ID is required.",
validate: "^[0-9]+$",
location: "path"
},
key:{
name: 'Authorization',
required: true,
help: "The user authorization token is required.",
validate: "^Bearer (.*?)$",
location: "headers"
}
}
},
edit: {...} //Other Endpoint/Subcategories for this category
}
}
}
const api = Wrape(api_config);
You can set enviroment variables for a category by initializing it with set
function.
const User = new api.user.set({
authorization: 'default_auth_token'
});
User.info({id: 1}).then((res) => {
console.log(res.json);
});
FAQs
Easily generate API client's SDK — organize and simplify API Requests — OOP-ish way.
The npm package wrape receives a total of 1 weekly downloads. As such, wrape popularity was classified as not popular.
We found that wrape demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.