Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aexol/syncano-middleware
https://github.com/aexol/syncano-middleware
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/aexol/syncano-middleware
- Owner: aexol
- Created: 2018-02-08T17:18:26.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-01T15:29:26.000Z (almost 7 years ago)
- Last Synced: 2024-11-17T15:55:08.986Z (about 2 months ago)
- Language: TypeScript
- Size: 381 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Syncano middleware
## About
Simple library easing writing of reusable middlewares for socket.
## Installation
```sh
$ npm install @aexol/syncano-middleware
```## Usage
socket.yml
```yaml
name: example
description: Description of example
version: 0.0.1
runtime: nodejs_v8
endpoints:
file: example.js
response:
success:
mimetype: application/json
exit_code: 200
```src/example.js
```javascript
import serve, {response} from '@aexol/syncano-middleware';async function run(ctx, syncano) {
return response.success({message: 'Hello world!'})
}export default ctx => serve(ctx, run)
```### Writing new middleware
socket.yml
```yaml
name: example
description: Description of example
version: 0.0.1
runtime: nodejs_v8
endpoints:
file: example.js
response:
success:
mimetype: application/json
exit_code: 200
forbidden:
mimetype: application/json
exit_code: 403
```src/utils.js
```javascript
import {response} from '@aexol/syncano-middleware';export function loggedIn(fn) {
return async (ctx, syncano) => {
if(!ctx.meta.user) {
return response.forbidden({message: 'You must be logged in to perform this action.'})
}
return fn(ctx, syncano)
}
}
```src/example.js
```javascript
import serve, {response} from '@aexol/syncano-middleware';
import {loggedIn} from './utils'async function run(ctx, syncano) {
return response.success({message: 'Hello world!'})
}export default ctx => serve(ctx, loggedIn(run))
```### cleanExit middleware
This library comes with built-in cleanExit middleware which handles exceptions raised by the socket script.
#### Example
socket.yml
```yaml
name: example
description: Description of example
version: 0.0.1
runtime: nodejs_v8
endpoints:
file: example.js
response:
success:
mimetype: application/json
exit_code: 200
serverError:
mimetype: application/json
exit_code: 500
```src/example.js
```javascript
import serve, {response, cleanExit} from '@aexol/syncano-middleware';async function run(ctx, syncano) {
return response.success({message: 'Hello world!'})
}export default ctx => serve(ctx, cleanExit(run))
```