
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
simple-protocol-http
Advanced tools
This module normalizes http responses using Simple Protocol. HTTP response bodies are parsed as JSON by default but gracefully fall back to text if the response body is not valid JSON.
This module normalizes http responses using Simple Protocol. HTTP response bodies are parsed as JSON by default but gracefully fall back to text if the response body is not valid JSON.
Simple protocol is simple:
{
success: true,
payload: {
// the result of the operation, i.e. an http response body
}
}
{
success: false,
error: {
// error details or object
}
}
That's it! Both success and error cases are handled the same way and can follow the same code path.
npm i --save simple-protocol-http
const { get, post, put, remove} = require('simple-protocol-http')
let result = await get(url)
let result = await post(url, payload)
let result = await put(url, payload)
let result = await remove(url)
If you want to send custom fetch options (i.e. send custom headers, etc):
const { get, post, put, remove} = require('simple-protocol-http').options
let result = await get(fetchOptions, url)
let result = await post(fetchOptions, url, payload)
let result = await put(fetchOptions, url, payload)
let result = await remove(fetchOptions, url)
The whole enchilada, i.e. full referential transparency:
const { get, post, put, remove} = require('simple-protocol-http').full
let result = await get(fetch, fetchOptions, url)
let result = await post(fetch, fetchOptions, url, payload)
let result = await put(fetch, fetchOptions, url, payload)
let result = await remove(fetch, fetchOptions, url)
const { get } = require('simple-protocol-http')
let result = await get('http://www.example.com/api')
If the server returns this with a 200 status code:
{
value: 'foo'
}
The value of result is:
{
success: true,
payload: {
value: 'foo'
},
meta: {
status: 200,
statusText: 'OK',
headers: {...}
}
}
const { post } = require('simple-protocol-http')
let result = await post('http://www.example.com/api', {...})
If the server returns this with a 400 status code:
{
type: 'ValidationError',
message: 'Some field is invalid'
}
The value of result is:
{
success: false,
error: {
type: 'ValidationError',
message: 'Some field is invalid'
},
meta: {
status: 400,
statusText: 'Bad Request',
headers: {...}
}
}
const { post } = require('simple-protocol-http')
let result = await post('http://www.example.com/api', {...})
If the server returns this:
{
success: true,
payload: {
foo: 'bar'
}
}
This module will return this:
{
success: true,
payload: {
foo: 'bar'
},
meta: {
status: 200,
statusText: 'OK',
headers: {...}
}
}
const { post } = require('simple-protocol-http')
let result = await post('http://www.example.com/api', {...})
If the server returns this:
{
success: false,
error: {
message: 'You posted something invalid'
}
}
This module will return this:
{
success: false,
error: {
message: 'You posted something invalid'
},
meta: {
status: 200,
statusText: 'OK',
headers: {...}
}
}
FAQs
This module normalizes http responses using Simple Protocol. HTTP response bodies are parsed as JSON by default but gracefully fall back to text if the response body is not valid JSON.
The npm package simple-protocol-http receives a total of 2 weekly downloads. As such, simple-protocol-http popularity was classified as not popular.
We found that simple-protocol-http 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.