Interacting with external resources
Digital Writer includes a resource proxy which enables plugins to make HTTP requests to external resources, even when the Writer is hosted in an IP-locked environment.
In order to reach an external source the plugin should use the built in Router-instance which is exposed on the writer.router-property. This router-property contains functions for the normal HTTP methods, get() post(), del(), and put().
  • api.router.post(string path, object parameters)
  • api.router.put(string path, object parameters)
  • api.router.get(string path, object parameters)
  • api.router.del(string path, object parameters)

path and parameters specification

  • path is a string which should point to an internal Writer API resource. For the purposes
    of interacting with external sources, path should be '/api/resourceproxy'
  • parameters is an object which is converted to a querystring and is appended to the path parameter.
    • Use the url-property to define which external resource to fetch
    • The headers, and body-properties are forwarded to the external resource
    • Add a x-append-authorization-property to headers-property to forward any Authorization
      information to the external resource

Simple GET of external resource

1
import {api} from 'writer'
2
3
api.router.get('/api/resourceproxy', { url: 'https://my-external-url.com' })
4
.then(response => api.router.checkForOKStatus(response))
5
.then(response => api.router.toJson(response))
6
.then(json => {
7
// Handle json data
8
})
Copied!

Simple POST to external resource

1
import {api} from 'writer'
2
3
api.router.post('/api/resourceproxy', {
4
url: 'https://my-external-url.com',
5
body: JSON.stringify({
6
property: 'value'
7
}),
8
})
9
.then(response => api.router.checkForOKStatus(response))
10
.then(response => api.router.toJson(response))
11
.then(json => {
12
// Handle json data
13
})
Copied!

Simple request of external resource with added header

1
import {api} from 'writer'
2
3
api.router.get('/api/resourceproxy', {
4
url: 'https://my-external-url.com',
5
headers: {
6
'x-my-header': 'my-header-value'
7
}
8
})
9
.then(response => api.router.checkForOKStatus(response))
10
.then(response => api.router.toJson(response))
11
.then(json => {
12
// Handle json data
13
})
Copied!

POST with Authorization header using resourceproxy

1
import {api} from 'writer'
2
3
api.router.post('/api/resourceproxy', {
4
url: 'https://my-protected-external-url.com',
5
body: JSON.stringify({
6
property: 'value'
7
}),
8
headers: {
9
'x-append-authorization': 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
10
}
11
})
12
.then(response => api.router.checkForOKStatus(response))
13
.then(response => api.router.toJson(response))
14
.then(json => {
15
// Handle json data
16
})
Copied!

GET with Authorization header using resourceproxy

1
import {api} from 'writer'
2
3
api.router.get('/api/resourceproxy', {
4
url: 'https://my-protected-external-url.com',
5
headers: {
6
'x-append-authorization': 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
7
}
8
})
9
.then(response => api.router.checkForOKStatus(response))
10
.then(response => api.router.toJson(response))
11
.then(json => {
12
// Handle json data
13
})
Copied!