@edge/misc-utils
v1.0.7
Published
Provides commonly-used libraries across Edge products
Downloads
26
Readme
Edge Miscellaneous Utilities
Provides commonly-used libraries across Edge products
Overview
cycle
cycle provides a simple wrapper for background jobs.
import { cycle } from '@edge/misc-utils'
const hello: cycle.Job = {
name: 'hello',
interval: 2000,
async do() {
console.log('Hello')
}
}
cycle.run([hello]).catch(err => console.error(err))
http
http provides convenience methods for Express-based JSON APIs.
import { RequestHandler } from 'express'
import { http } from '@edge/misc-utils'
const handler: RequestHandler = (req, res, next) => {
http.notFound(res, next, { reason: 'this is just a README demonstration' })
}
query
query provides type-safe access to query string data.
import { RequestHandler } from 'express'
import { query } from '@edge/misc-utils'
const handler: RequestHandler = (req, res, next) => {
const page = query.integer(req.query.page, 1) || 1
res.send(`Page ${page}`)
next()
}
validate
validate provides a basic validation library, plus a powerful validate()
function that sanitises input. This is particularly useful for Express-based JSON APIs.
import { RequestHandler } from 'express'
import { validate } from '@edge/misc-utils'
const handler = (): RequestHandler => {
type Data = {
name: string
}
const readBody = validate.validate<Data>({
name: validate.seq(validate.str, validate.minLength(1), validate.maxLength(256))
})
return (req, res, next) => {
try {
const data = readBody(req.body)
res.json(data)
next()
}
catch (err) {
next(err)
}
}
}
Miscellaneous
identity()
provides a simple identity function, which can be useful for e.g. dereferencing arrays:
import { identity } from '@edge/misc-utils'
const a = [1, 2, 3]
const b = a.map(identity)
a.push(4)
console.log(a, b) // [ 1, 2, 3, 4 ] [ 1, 2, 3 ]
unique()
provides a simple way to isolate unique values.
import { unique } from '@edge/misc-utils'
const ids = ['abc', 'def', 'def', 'ghi'].filter(unique)
console.log(ids) // [ 'abc', 'def', 'ghi' ]
Contributing
Interested in contributing to the project? Amazing! Before you do, please have a quick look at our Contributor Guidelines where we've got a few tips to help you get started.
License
Edge is the infrastructure of Web3. A peer-to-peer network and blockchain providing high performance decentralised web services, powered by the spare capacity all around us.
Copyright notice (C) 2023 Edge Network Technologies Limited [email protected] All rights reserved
This product is part of Edge. Edge is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version ("the GPL").
If you wish to use Edge outside the scope of the GPL, please contact us at [email protected] for details of alternative license arrangements.
This product may be distributed alongside other components available under different licenses (which may not be GPL). See those components themselves, or the documentation accompanying them, to determine what licenses are applicable.
Edge is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
The GNU General Public License (GPL) is available at: https://www.gnu.org/licenses/gpl-3.0.en.html A copy can be found in the file GPL.md distributed with these files.
This copyright notice MUST APPEAR in all copies of the product!