Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aexol/syncano-middleware


https://github.com/aexol/syncano-middleware

Last synced: about 1 month ago
JSON representation

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))
```