Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
api-formatter
Advanced tools
Readme
A utility to send formatted json responses as an express response
const express = require('express')
const Api = require('api-formatter').Api
let app = express()
// Register the middleware
app.use(Api.middleware({ name: 'My Fancy Api' }))
// Sending formatted data
app.get('/', (req, res) => {
res.api.sendData({ something: 'cool!' })
})
// Sending a formatted failure
app.get('/wip', (req, res) => {
res.api.sendFail('Not implemented yet', 418)
})
// Automatically catching errors
app.get('/bad', (req, res) => {
req.api.catch(api => {
throw new Error('Something went wrong :S')
})
})
{
"meta": {
"success": true,
"messages": [],
"name": "My Fancy Api",
"version": "0.1.2"
},
"data": {
"something": "cool!"
}
}
{
"meta": {
"success": false,
"messages": ["Something went wrong :S"],
"name": "My Fancy Api",
"version": "0.1.2"
},
"data": null
}
Name | Use |
---|---|
name | The name of your api, defaults to your package.json name |
version | The version of your api, defaults to your package.json version |
httpError | Whether a failed response should return a HTTP/400, defaults to true |
import express from 'express'
import { Api } from 'api-formatter'
let app = express()
app.use(Api.middleware({ name: 'dogs-api', version: 'v1' }))
app.get('/', (req, res) => {
res.api.sendData({ msg: 'Hey!' })
})
app.get('/error', (req, res) => {
res.api.sendFail(['Oops, something went wrong :S'])
})
app.listen(3000, () => console.log('Listening on :3000'))
The success response will be:
GET http://localhost:3000
→ http/200
{
"meta": {
"success": true,
"messages": [],
"status": 200,
"name": "dogs-api",
"version": "v1"
},
"data": { "msg": "Hey!" }
}
And the fail response will be:
GET http://localhost:3000/error
→ http/400
{
"meta": {
"success": false,
"messages": ["Oops, something went wrong :S"],
"status": 400,
"name": "dogs-api",
"version": "v1"
},
"data": null
}
FAQs
Format json responses in a meta-data structure
The npm package api-formatter receives a total of 9 weekly downloads. As such, api-formatter popularity was classified as not popular.
We found that api-formatter 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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.