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 |
|
17 | The `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 |
|
19 | In it's basic form, the array of interceptors is processed in order, wrapping the parent client.
|
20 |
|
21 | ```javascript
|
22 | client: {
|
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 | },
|
33 | baseClient: { module: 'rest' },
|
34 | $plugins: [{ module: 'rest/wire' }]
|
35 | ```
|
36 |
|
37 | If 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
|
40 | client: {
|
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 |
|
51 | If a configuration element isn't needed, a string can be provided to represent the module
|
52 |
|
53 | ```javascript
|
54 | client: {
|
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 |
|
65 | An individual interceptors array entry can use any facility available within wire.js, including $ref.
|
66 |
|
67 | ```javascript
|
68 | client: {
|
69 | rest: [
|
70 | { $ref: 'mime', config: { mime: 'application/json' } },
|
71 | 'rest/interceptor/location',
|
72 | 'rest/interceptor/entity',
|
73 | { $ref: 'hateoas', config: { target: '' } }
|
74 | ]
|
75 | },
|
76 | mime: { module: 'rest/interceptor/mime' },
|
77 | hateoas: { module: 'rest/interceptor/hateoas' },
|
78 | $plugins: [{ module: 'rest/wire' }]
|
79 | ```
|