Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aexol/syncano-middleware-common
https://github.com/aexol/syncano-middleware-common
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/aexol/syncano-middleware-common
- Owner: aexol
- Created: 2018-02-23T09:55:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-05T14:46:53.000Z (almost 7 years ago)
- Last Synced: 2024-11-18T10:55:40.560Z (about 2 months ago)
- Language: TypeScript
- Size: 159 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Syncano Middleware Common
## About
Collection of common utilities for sockets in syncano that can be used in conjunction with @aexol/syncano-middleware lib.## Installation
npm install @aexol/syncano-middleware-common## Usage
## API Reference
## Modules
- @aexol/syncano-middleware-common
-
Common middlewares for syncano.
## Functions
-
allowedMethods(fn, allowed) ⇒function
-
Checks if request is allowed based on request method.
-
loggedIn(fn, opts) ⇒Object
-
Checks if user is logged in, returning response with 403 and message if not.
-
parseGETFields(fn) ⇒function
-
Parses args in GET request as json if possible. If not, leaves them unchanged.
-
replaceBuffers(fn, opts) ⇒Object
-
Replace all buffers in socket args.
-
rootAccount(fn, opts) ⇒Object
-
Root account check middleware.
- toBool(fn, fields)
-
Attempts to cast certain fields in request to bool.
Can be useful to handling both GET and POST input on endpoint
as GET endpoints will always have a string.Fields that are either
true
or'true'
will evaluate to true.
Everything else will be considered false. - toNumber(fn, fields)
-
Attempts to cast certain fields in request to number.
Can be useful to handling both GET and POST input on endpoint
as GET endpoints will always have a string.
## @aexol/syncano-middleware-common
Common middlewares for syncano.
## allowedMethods(fn, allowed) ⇒ function
Checks if request is allowed based on request method.
**Kind**: global function
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| fn | function
\| Object
| Either next function in chain or object with `key: value` pairs of method and handler function for method type. |
| allowed | Array
| List of allowed methods in case of fn being function. Optional. |
**Example**
```javascript
import serve, {response} from '@aexol/syncano-middleware'
import {allowedMethods} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.user.username}`)
}
export default ctx => serve(ctx, allowedMethods(hello, ['GET']))
```
**Example**
```javascript
import serve, {response} from '@aexol/syncano-middleware'
import {allowedMethods} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.user.username}`)
}
export default ctx => serve(ctx, allowedMethods({
GET: hello,
POST: hello
}))
```
## loggedIn(fn, opts) ⇒ Object
Checks if user is logged in, returning response with 403 and message if not.
**Kind**: global function
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| fn | function
| Next function in request chain |
| opts | Object
| Additional options. Optional |
| opts.message | String
| Alternative message if user is not logged in. |
**Example**
```javascript
import serve, {response} from '@aexol/syncano-middleware'
import {loggedIn} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.user.username}`)
}
export default ctx => serve(ctx, loggedIn(hello))
```
## parseGETFields(fn) ⇒ function
Parses args in GET request as json if possible. If not, leaves them unchanged.
**Kind**: global function
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| fn | function
| Next function in request chain |
**Example**
```javascript
import serve, {response} from '@aexol/syncano-middleware'
import {parseGETFields} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.user.username}`)
}
export default ctx => serve(ctx, parseGETFields(hello))
```
## replaceBuffers(fn, opts) ⇒ Object
Replace all buffers in socket args.
**Kind**: global function
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| fn | function
| Handler function |
| opts | Object
| Aditional opts. Optional |
| opts.replaceFn | function
| If set, will be used to replace buffer contents instead of default behaviour. |
| opts.exclude | Array
| List of args to skip from replacing. |
| opts.encoding | String
| Input encoding of buffer in args. |
| opts.inputEncoding | String
| Output encoding of buffer in args. Unless, replaceFn was set, this middleware replaces all buffers with it's string content in place. Modifies ctx.args. |
**Example**
```javascript
import serve, {response} from '@aexol/syncano-middleware'
import {replaceBuffers} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: 'ok'})
}
export default ctx => serve(ctx, replaceBuffers(hello))
```
## rootAccount(fn, opts) ⇒ Object
Root account check middleware.
**Kind**: global function
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| fn | function
| Next function in chain. |
| opts | Object
| Additional options. Optional. |
| opts.message | String
| Custom error message if not root account. |
| opts.condFn | function
| Check for root only if function evaluates to true. |
**Example**
```javascript
import serve, {response} from '@aexol/syncano-middleware'
import {rootAccount} from '@aexol/syncano-middleware-common'
async function hello(ctx, syncano) {
return response.success({message: `Hello, ${ctx.meta.admin.email}`)
}
export default ctx => serve(ctx, rootAccount(hello))
```
## toBool(fn, fields)
Attempts to cast certain fields in request to bool.
Can be useful to handling both GET and POST input on endpoint
as GET endpoints will always have a string.
Fields that are either `true` or `'true'` will evaluate to true.
Everything else will be considered false.
**Kind**: global function
| Param | Type | Description |
| --- | --- | --- |
| fn | function
| Next function in chain |
| fields | Array
| fields to cast to bool |
## toNumber(fn, fields)
Attempts to cast certain fields in request to number.
Can be useful to handling both GET and POST input on endpoint
as GET endpoints will always have a string.
**Kind**: global function
| Param | Type | Description |
| --- | --- | --- |
| fn | function
| Next function in chain |
| fields | Array
| fields to cast to number |