api-helper-lib
v1.0.1
Published
Full-stack API helpers: query, headers, body, response builders, and HTTP status utilities
Downloads
179
Maintainers
Readme
api-helper-lib
Full-stack API helpers: query parsing, headers, body parsing, response builders, and HTTP status utilities. Use in Node (Express, Fastify, etc.) or with fetch/Workers.
Features
- Zero dependencies
- Query — Parse and stringify URL query strings
- Headers — Read Authorization/Bearer, Content-Type, CORS helpers
- Body — Parse JSON or form body from Request / IncomingMessage
- Response — Consistent JSON success/error and paginated responses
- Status — HTTP status constants and helpers
Installation
npm install api-helper-libQuick Start
const {
parseQuery,
getQueryParam,
getBearerToken,
parseBody,
successResponse,
errorResponse,
STATUS,
corsHeaders,
} = require('api-helper-lib');
// Query (e.g. req.url or location.search)
const params = parseQuery('?page=1&sort=name');
const page = getQueryParam(req.url, 'page');
// Headers
const token = getBearerToken(req.headers);
const cors = corsHeaders('*', { credentials: true });
// Body (async)
const data = await parseBody(req, req.headers);
// Response shapes (use with your framework)
const res = successResponse({ user: { id: 1 } });
const err = errorResponse('Invalid input', STATUS.BAD_REQUEST);
const notFound = notFoundResponse();
const validation = validationErrorResponse([{ field: 'email', message: 'Invalid' }]);Subpath Imports
const query = require('api-helper-lib/query');
const headers = require('api-helper-lib/headers');
const body = require('api-helper-lib/body');
const response = require('api-helper-lib/response');
const status = require('api-helper-lib/status');API
query — parseQuery, getQueryParam, stringifyQuery, mergeQuery
headers — getHeader, getBearerToken, getContentType, isJsonRequest, corsHeaders, jsonHeaders
body — readBody, parseBody, parseJsonSafe
response — jsonResponse, successResponse, errorResponse, notFoundResponse, unauthorizedResponse, validationErrorResponse, paginatedResponse
status — STATUS, STATUS_TEXT, getStatusText, isSuccess, isClientError, isServerError
License
MIT
