UNPKG

2.5 kBMarkdownView Raw
1# Form Builder JSON Web Token client (Node)
2
3Base client for making requests to Form Builder platform endpoints that require JSON Web Tokens for authenctication
4
5## Requirements
6
7Node
8
9## Installation
10
11`npm install @ministryofjustice/fb-jwt-client-node`
12
13## Usage
14
15### Loading and initialising basic client
16
17``` javascript
18// load client class
19const FBJWTClient = require('@ministryofjustice/fb-jwt-client-node')
20
21// initialise client
22const jwtClient = new FBJWTClient(serviceSecret, serviceToken, serviceSlug, microserviceUrl, [errorClass])
23```
24
25#### `serviceSecret`
26
27Constructor will throw an error if no service secret is passed
28
29#### `serviceToken`
30
31Constructor will throw an error if no service token is passed
32
33#### `serviceSlug`
34
35Constructor will throw an error if no service slug is passed
36
37#### `microserviceUrl`
38
39Constructor will throw an error if no service url is passed
40
41#### `errorClass`
42
43By default, uses FBJWTClientError
44
45### Extending
46
47``` javascript
48// extend base class
49class FBMyClient extends FBJWTClient {
50 constructor (serviceSecret, serviceToken, serviceSlug, microserviceUrl, myVar) {
51 super(serviceSecret, serviceToken, serviceSlug, microserviceUrl)
52 // do something with additional constructor argument
53 this.myVar = myVar
54 }
55}
56
57const myClient = new FBMyClient('service_secret', 'service_token', 'myservice', 'http://myservice', 'my var')
58```
59
60``` javascript
61// extend base class with custom error
62class FBAnotherClient extends FBJWTClient {
63 constructor (serviceSecret, serviceToken, serviceSlug, microserviceUrl) {
64 // create custom error class
65 class FBAnotherClientError extends FBJWTClient.prototype.ErrorClass {}
66 super(serviceSecret, serviceToken, serviceSlug, microserviceUrl, FBAnotherClientError)
67 }
68}
69```
70
71### Methods
72
73- generateAccessToken
74
75 Generate JWT access token
76
77- createEndpointUrl
78
79 Return user-specific endpoint
80
81- sendGet
82
83 Handle client get requests
84
85- sendPost
86
87 Handle client post requests
88
89- encrypt
90
91 Encrypt data with AES 256
92
93- decrypt
94
95 Decrypt data
96
97- encryptUserIdAndToken
98
99 Encrypt user ID and token using service secret
100
101- decryptUserIdAndToken
102
103 Decrypt user ID and token using service secret
104
105- handleRequestError
106
107 Handle client response errors
108
109- createRequestOptions
110
111 Create request options
112
113- throwRequestError
114
115 Convenience function for throwing errors
116
117## Further details
118
119See documentation in code for further details and `fb-user-datastore-client-node` and `fb-submitter-client-node` for examples.
\No newline at end of file