UNPKG

3.13 kBMarkdownView Raw
1# wire.js
2
3[wire.js](https://github.com/cujojs/wire/) is an Inversion of Control container that allows applications to be composed together at runtime based on a declarative configuration. A rest.js plugin is provided for wire.js that enables declarative configuration of rest.js clients, including chaning interceptors with their configuration.
4
5
6<a name="module-rest/wire"></a>
7## Wire Plugin
8
9`rest/wire` ([src](../wire.js))
10
11**TIP:** In each of these examples, `{ module: 'rest/wire' }` is loaded as it provides the 'rest' factory to the wire.js spec. Without this module being loaded into the spec, the facilities below will silently fail.
12
13
14<a name="wire-rest-factory"></a>
15### 'rest' Factory
16
17The `rest` factory provides a declarative way to define a client with an interceptor chain that is nearly identical in capability to imperative JavaScript. The factory access two main config properties, a parent client, and an array of interceptors. Each entry in the interceptor array contains a reference to the interceptor module, and the configuration for that interceptor. The array of interceptors is chained off the client in order returning the resulting client as the wire.js component.
18
19In it's basic form, the array of interceptors is processed in order, wrapping the parent client.
20
21```javascript
22client: {
23 rest: {
24 parent: { $ref: 'baseClient' },
25 interceptors: [
26 { module: 'rest/interceptor/mime', config: { mime: 'application/json' } },
27 { module: 'rest/interceptor/location' },
28 { module: 'rest/interceptor/entity' },
29 { module: 'rest/interceptor/hateoas', config: { target: '' } }
30 ]
31 }
32},
33baseClient: { module: 'rest' },
34$plugins: [{ module: 'rest/wire' }]
35```
36
37If parent is not defined, or is not a function, the default client is used as the parent. In that case, the interceptors array can replace the whole factory object
38
39```javascript
40client: {
41 rest: [
42 { module: 'rest/interceptor/mime', config: { mime: 'application/json' } },
43 { module: 'rest/interceptor/location' },
44 { module: 'rest/interceptor/entity' },
45 { module: 'rest/interceptor/hateoas', config: { target: '' } }
46 ]
47},
48$plugins: [{ module: 'rest/wire' }]
49```
50
51If a configuration element isn't needed, a string can be provided to represent the module
52
53```javascript
54client: {
55 rest: [
56 { module: 'rest/interceptor/mime', config: { mime: 'application/json' } },
57 'rest/interceptor/location',
58 'rest/interceptor/entity',
59 { module: 'rest/interceptor/hateoas', config: { target: '' } }
60 ]
61},
62$plugins: [{ module: 'rest/wire' }]
63```
64
65An individual interceptors array entry can use any facility available within wire.js, including $ref.
66
67```javascript
68client: {
69 rest: [
70 { $ref: 'mime', config: { mime: 'application/json' } },
71 'rest/interceptor/location',
72 'rest/interceptor/entity',
73 { $ref: 'hateoas', config: { target: '' } }
74 ]
75},
76mime: { module: 'rest/interceptor/mime' },
77hateoas: { module: 'rest/interceptor/hateoas' },
78$plugins: [{ module: 'rest/wire' }]
79```