@neogeek/tiny-api-tools@1.3.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
neogeek/tiny-api-toolsA set of functions for building tiny APIs in JavaScript.
This package works with Cloudflare Workers, Node.js, Deno, Bun
JSR Score
94%
Published
a week ago (1.3.0)
A set of functions for building tiny APIs in JavaScript.
Install
Deno
deno add jsr:@neogeek/tiny-api-tools
NPM
npx jsr add @neogeek/tiny-api-tools
Usage
Deno
import { httpStatusCodes, httpStatusMessages, } from 'jsr:@neogeek/tiny-api-tools/http-status-codes'; import { JsonResponse } from 'jsr:@neogeek/tiny-api-tools/http'; import { doesRequestMatchPattern, parsePathValuesFromRequest, } from 'jsr:@neogeek/tiny-api-tools/request'; Deno.serve({ port: 8080 }, (req) => { if (req.method === 'GET' && doesRequestMatchPattern(req, '/')) { return new JsonResponse({ version: '1.0.0' }); } else if ( req.method === 'GET' && doesRequestMatchPattern(req, '/hello/:name?') ) { const params = parsePathValuesFromRequest(req, '/hello/:name?'); return new JsonResponse({ message: `Hello, ${params.name || 'world'}!` }); } return new JsonResponse( { message: httpStatusMessages[httpStatusCodes.NotFound] }, { status: httpStatusCodes.NotFound, } ); });
Node.js
import { createServer } from 'node:http'; import { httpStatusCodes, httpStatusMessages, } from '@neogeek/tiny-api-tools/http-status-codes'; import { doesUrlMatchPattern, parsePathValuesFromUrl, } from '@neogeek/tiny-api-tools/url'; const PORT = process.env.PORT || 3000; const server = createServer((req, res) => { if (!req.url) { res.statusCode = httpStatusCodes.InternalServerError; res.end(JSON.stringify({ message: 'Internal Server Error' })); return; } const url = new URL(req.url, `http://localhost:${PORT}/`); if (req.method === 'GET' && doesUrlMatchPattern(url, '/')) { res.end(JSON.stringify({ version: '1.0.0' })); } else if ( req.method === 'GET' && doesUrlMatchPattern(url, '/hello/:name?') ) { const params = parsePathValuesFromUrl(url, '/hello/:name?'); res.end(JSON.stringify({ message: `Hello, ${params.name || 'world'}!` })); } else { res.statusCode = httpStatusCodes.NotFound; res.end( JSON.stringify({ message: httpStatusMessages[httpStatusCodes.NotFound] }) ); } }); server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });
Request Pattern
Request patterns are similar to how routes are defined in frameworks like express.
Each pattern consists of static names /version
and variable names /:username
. These can be used in any order you would like.
Variable name at the end of a pattern can be made optional by using a ?
character at the end of the pattern like this /:org/:repo/:branch?
.
Trailing slashes are not required and won't prevent matching if the request doesn't include it.
Examples
- Simple Server (Deno) - A simple server with two routes
/
and/hello/:name?
built in Deno. - Simple Server (Node.js) - A simple server with two routes
/
and/hello/:name?
built in Node.js.
License
Built and signed on
View transparency logGitHub Actions
Add Package
deno add jsr:@neogeek/tiny-api-tools
Import symbol
import * as tiny_api_tools from "@neogeek/tiny-api-tools";
---- OR ----
Import directly with a jsr specifier
import * as tiny_api_tools from "jsr:@neogeek/tiny-api-tools";
Add Package
npx jsr add @neogeek/tiny-api-tools
Import symbol
import * as tiny_api_tools from "@neogeek/tiny-api-tools";
Add Package
yarn dlx jsr add @neogeek/tiny-api-tools
Import symbol
import * as tiny_api_tools from "@neogeek/tiny-api-tools";
Add Package
pnpm dlx jsr add @neogeek/tiny-api-tools
Import symbol
import * as tiny_api_tools from "@neogeek/tiny-api-tools";
Add Package
bunx jsr add @neogeek/tiny-api-tools
Import symbol
import * as tiny_api_tools from "@neogeek/tiny-api-tools";