3.5.2 • Published 4 years ago

cubic-api v3.5.2

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

cubic-api

npm build dependencies

Usage

const Cubic = require('cubic')
const Api = require('cubic-api')
const cubic = new Cubic()

cubic.use(new Api(options))

This will open a web server on localhost:3003 which serves data from connected cubic-core nodes. No further setup needed - the core nodes are where our application logic goes.

How does it work?

At its core, cubic-api is a load balancer for connected cubic-core nodes. What makes it special is that it allows the use of custom connection adapters that create a common req and res object from any connection type. (HTTP & WebSockets by default)

This way our middleware functions and routed endpoints will work for all connection types, with no need to adjust them individually.

For further understanding, here's a simple model showing the way a request will go until we get a response:

model

This is only one half of the way a request goes. To see what happens once the request is sent to a connected core node, check out cubic-core.

Writing custom middleware

If you need to access the req, res objects before they're sent to the core node, you can simply add your custom function to the async middleware stack. It behaves much like express middleware, but takes advantage of ES7 async.

Example

cubic.nodes.api.use('/ferret', async (req, res) => {

  // Return image of angry ferret if the user isn't tobi.
  if (req.user.uid !== 'tobi') {
    let image = await getSomeAngryFerretPictures()

    // we MUST return a truthy value to stop the middleware chain from executing
    return res.send(image)
  }

  // If nothing is returned, we'll assume the user is tobi and proceed with the
  // next middleware function
})

We recommend reading through the full docs at the async-middleware-stack repo if you need further information.

Native Middleware

If necessary, you can still add native connection middleware which runs before our own.

cubic.nodes.api.server.http.app.use((req, res, next) => {}) // Native Express Middleware
cubic.nodes.api.server.ws.app.use((socket, next) => {}) // Native Primus Middleware

Making requests as a client

We heavily recommend using cubic-client since it takes care of authorization, rate limits and potential downtimes automatically. This package is also used to connect core nodes to API nodes, so we most likely won't be slacking with its maintenance.

Scope authentication

API endpoints can be protected by the this.schema.scope option in the endpoint constructor. If a client without the required scope tries to connect, it will return 401 Unauthorized with a fitting error reason.

Options

cubic.use(new Api(options))
OptionDefaultDescription
port3003Port to listen on for requests.
redisUrl'redis://localhost'Base URL for redis connection.
cacheDb1Redis database used to store cache data.
cacheExp10Time in seconds until cached data expires when no explicit duration is specified.
requestTimeout1000Time to wait in ms when sending request to core node before assuming timeout.
authCookie'cubic-auth-cookie'Cookie name to use for access/refresh tokens.
authCookieExpire30Expire time for authCookie, in days.
authUrl'http://localhost:3030'Auth node to connect to when provided access tokens need to be refreshed.
userKeynoneUser key to authenticate with. These are registered and assigned automatically in dev mode. In production, you need to register them yourself. (see cubic-auth for reference)
userSecretnoneUser secret to authenticate with. Handled the same way as above.
endpointPath${process.cwd()}/endpointsPath(s) to get endpoints from. Can be String or Array.

License

MIT

3.5.2

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.2

4 years ago

3.4.1

4 years ago

3.4.0

4 years ago

3.3.5

4 years ago

3.3.4

4 years ago

3.3.3

4 years ago

3.3.2

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.3

5 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.0

6 years ago