7.1.6 • Published 2 years ago

@lanetix/microservice v7.1.6

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

lanetix-microservice

Wrapper for writing JWT-authenticated microservices for internal use within Lanetix.

Installation

npm install --save @lanetix/microservice

Usage

Optimally, the creation of the express app should be separated out from the creation of the HTTP server for ease of testing.

// server.js - build up your express app

import express from 'express'
import { server } from 'lanetix-microservice'
import fooRouter from './routers/foo'
import bonkRouter from './routers/bonk'

const app = express()
export default server(app, {
  postAuthentication: [
    // middleware goes here
  ],

  routers: {
    '/foo': fooRouter,
    '/bonk': bonkRouter
  }
})

// web.js - bind to a port and launch your application

import { createServer } from 'http'
import server from './server'

const port = 9999
const httpServer = createServer(server)
httpServer.listen(port, () => {
  console.log(`listening on port ${port}`)
})

Logging

Logging convenience methods are appended to the request object that automatically append the x-request-id, if available.

Available logging methods

  • error
  • info
  • log
  • warn

Usage

module.exports = (req, res, next) {
  req.error('oh no!')
  next()
  // output will be `> 12345 oh no!` where 12345 is the request id
}

Testing

The test client allows you to make requests to an in-memory version of your application. GET, POST, PATCH, PUT, and DELETE are supported HTTP verbs.

A user can be impersonated by providing either:

  • A user_id property directly on the context
  • Or a user property on the context that has an id
import { test } from 'lanetix-microservice'
import server from '../server'

describe('Microservice tests', () => {
  it('should return 200 OK for a GET to /foo', () =>
    test.serve(server, { user_id: 1337 })
      .get('/foo')
      .expect(200)
  )

  it('should return 201 Created for a POST to /bonk', () =>
    test.serve(server, { user_id: 1337 })
      .post('/bonk')
      .send({ data: 'hello' })
      .expect(201)
  )
})

Authorization

In order to avoid a 401 unauthorized response, an authorization token should be present on each request.

access_token (query string)

You'll need this for native get requests that aren't capable of putting headers on the request. These will be traditional anchor tags:

<a href ="http://localhost:5015/download/csv?access_token=accesstoken">

bearer token (request header)

The bearer token approach will be the most common. API calls will employ this strategy:

var request = {
  method: 'POST',
  url: 'http://localhost:5015/api/call',
  headers: {
    'Authorization': 'Bearer accesstoken' // this is the important part
  }
};

// send request
7.1.6

2 years ago

7.1.6-beta

2 years ago

7.1.5

3 years ago

7.1.5-beta

3 years ago

7.1.4

3 years ago

7.1.3

5 years ago

7.1.2

5 years ago

7.1.1

5 years ago

7.1.0

5 years ago

7.0.2

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.0.0

5 years ago

5.1.0

5 years ago

5.0.4

6 years ago

5.0.3

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.1-next.1

6 years ago

5.0.1-next.0

6 years ago

5.0.0

6 years ago

5.0.0-next.21

6 years ago

5.0.0-next.20

6 years ago

5.0.0-next.19

6 years ago

5.0.0-next.18

6 years ago

5.0.0-next.17

6 years ago

5.0.0-next.16

6 years ago

5.0.0-next.15

6 years ago

5.0.0-next.14

6 years ago

5.0.0-next.13

6 years ago

5.0.0-next.12

6 years ago

5.0.0-next.11

6 years ago

5.0.0-next.10

6 years ago

5.0.0-next.9

6 years ago

5.0.0-next.8

6 years ago

5.0.0-next.7

6 years ago

5.0.0-next.6

6 years ago

5.0.0-next.5

6 years ago

5.0.0-next.4

6 years ago

5.0.0-next.3

6 years ago

5.0.0-next.2

6 years ago

5.0.0-next.1

6 years ago

5.0.0-next.0

6 years ago

4.2.0-next.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.1.0

7 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.0

8 years ago

2.2.0

8 years ago

2.1.0

9 years ago

2.0.5

9 years ago

2.0.4

9 years ago

1.7.0

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago