4.4.0 • Published 4 years ago

rfunc v4.4.0

Weekly downloads
209
License
MIT
Repository
github
Last release
4 years ago

rfunc

Build Status npm Version JS Standard

Remote function call with async interface

Installation

$ npm install rfunc --save

Usage

Define functions on server side.

#!/usr/bin/env node

/**
 * This is an example to invoke rfunc server
 */
'use strict'

const rfunc = require('rfunc')

void async function () {
  // Setup server for remote call
  await  rfunc({
    // Define APIs
    'sign': {
      async signin (username, password) {
        /* ... */
        return {success: true}
      },
      async signout () {
        /* ... */
      }
    }
  }).listen(3000)
}().catch((err) => console.error(err))

rfunc client create the function dynamically on client side and you can just call it.

#!/usr/bin/env node

/**
 * This is an example of rfunc client
 */
'use strict'

const rclient = require('rfunc-clinet')

void async function () {
  const sign = await rclient().connect('sign') // Define a client

  // Call remote api and receive the result
  const {success} = await sign.signin('foo', 'bar1234')
  console.log('success:', success)
}().catch((err) => console.error(err))

Advanced Usage

To be more specific about api, provide $spec on server side.

#!/usr/bin/env node

/**
 * Advanced usage of the server
 */
'use strict'

const rfunc = require('rfunc')
const http = require('http')

void async function () {
  const server = http.createServer()
  rfunc({
    'sign': {
      async signin (username, password) {
        const {state} = this // Access state property of koa
        console.log(state)
        /* ... */
      },
      async signout () { /* ... */ },
      // Callback before a method invoked
      async $before (methodName, params) {
        const {state} = this
        if (state.somethingIsWrong) {
          throw new Error('Something wrong!') // Throw error to reject invoking
        }
        state.hey = 'Say hey from before' // Set state value to share something with methods
        /* ... */
      },
      // Callback after a method invoked
      async $after (methodName, params, returns) {
        let {state} = this
        /* ... */
      },
      // Describe api specification
      $spec: {
        name: 'sign-api',
        version: '1.0.0',
        desc: 'Remote API for user sign procedures',
        methods: {
          signin: {
            desc: 'Signin in to the application',
            params: [
              {name: 'username', desc: 'Name of user to signin'},
              {name: 'password', desc: 'User password'}
            ],
            returns: {
              type: 'object'
            }
          },
          signout: {/* ... */}
        }
      }
    },
    // Koa middlewares
    $middlewares: [
      async function customMW (ctx, next) {
        // Called before handling
        /* ... */
        await next()
      }
    ],
    $jsonLimit: '4mb',
    $endpoints: {
      '/api/foo/:id': { // Pass object to handle each HTTP verbs
        'POST': (ctx) => {
          const {id} = ctx.params
          ctx.body = `This is foo with id: "${id}"`
        }
      },
    },
    $static: '/opt/www/public'
  }).applyTo(server) // Apply to existing http server
  server.listen(3000)
}().catch((err) => console.error(err))

Then you can fetch the spec data via .describe() method on client side.

#!/usr/bin/env node

/**
 * This is an example of rfunc client
 */
'use strict'

const rclient = require('rfunc/clinet')

void async function () {
  let sign = await rclient().connect('sign')

  // Fetch the spec data
  let $spec = await sign.describe()
  /* ... */
}().catch((err) => console.error(err))

This cloud be useful to generate API documentations.

License

This software is released under the MIT License.

Links

4.4.0

4 years ago

4.3.3

4 years ago

4.3.1

4 years ago

4.3.0

4 years ago

4.2.0

4 years ago

4.1.1

5 years ago

4.0.14

5 years ago

4.0.13

5 years ago

4.0.11

5 years ago

4.0.10

5 years ago

4.0.9

5 years ago

4.0.8

5 years ago

4.0.7

5 years ago

4.0.6

5 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.2.39

8 years ago

1.2.38

8 years ago

1.2.37

8 years ago

1.2.36

8 years ago

1.2.35

8 years ago

1.2.34

8 years ago

1.2.33

8 years ago

1.2.32

8 years ago

1.2.31

8 years ago

1.2.30

8 years ago

1.2.29

8 years ago

1.2.28

8 years ago

1.2.27

8 years ago

1.2.26

8 years ago

1.2.25

8 years ago

1.2.24

8 years ago

1.2.23

8 years ago

1.2.22

8 years ago

1.2.21

8 years ago

1.2.20

8 years ago

1.2.19

8 years ago

1.2.18

8 years ago

1.2.17

8 years ago

1.2.16

8 years ago

1.2.15

8 years ago

1.2.14

8 years ago

1.2.13

8 years ago

1.2.12

8 years ago

1.2.11

8 years ago

1.2.10

8 years ago

1.2.9

8 years ago

1.2.8

8 years ago

1.2.7

8 years ago

1.2.6

8 years ago

1.2.5

8 years ago

1.2.4

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago