cdnetworks
A simple Node.js wrapper for the CDNetworks Cache Flush Open API v2.4.0
Table of contents
- Installation
- Create an instance
- Defaults
- Return types
- API
- Example usage
- Service area
- Additional notes
Installation
npm install cdnetworks
Create an instance
var cdn=require('cdnetworks')();
will create an instance with default values
Either override the defaults on instantiation
var cdn=require('cdnetworks')({user:'myemail@email.com',pass:'secret'})
or later on, at any time with cdn.setValues(conf)
, which returns the cdn instance.
So a method can be chained
cdn
Defaults
user: null pass: null output: 'json' // xml not supported type:'item' // (doPurge): item || wildcard || all mailTo: '' // (status): array of emails to receive flush notifications servicearea: 'US' openStatusPage:false // (status): open flush status page in browser
Return types
setValues
returns the cdn instance.
The actual API methods return a promise.
You would normally follow the method call with a then/catch
cdn
API
padList
Lists all the PADs (domains) available to the user
Required arguments: [user, pass]
Optional: [output]
example
cdn
response
'image.foocdn.com' 'adv.foocdn.com'
doPurge
Sends cache flush requests.
Required arguments: [user, pass, type, pad],
Optional: [path, output]
Available types are ['item','wildcard','all']
Default is item.
all doesn't require path, because all items will be flushed.
example
This sends item flush for 2 files (assuming type has't been overridden)
cdn
This sends wildcard flush for n files in that directory
cdn
This sends wildcard flush, overriding type on the cdn instance, which will also apply to following calls
cdn
.then(function(res){
console.log(res)
})
response
'resultCode':200 'pid': 13459 'details': 'success (2 items)' 'paths': '/images/logo.gif' '/images/copyright.gif' 'notice': 'Maintenance scheduled at ...'
On each succesful purge the PID (purge id) is stored on the instance, and used by cdn.status()
if no PID is provided
status
Check Flush Completion Status
Required arguments: [user, pass, pid]
Optional: [ output, mailTo, openStatusPage]
example
this uses the last stored PID from latest flush
cdnstatus
this overddides some defaults and queries a PID that might not be from the latest purge
cdnstatus12345
response
"details": "Requested information returned" "percentComplete": 100 "resultCode": 200
That being said, in my tests status()
will almost invariably return a 400 if called right after a purge. That is because the PID takes about 10-15 seconds to be ready for queries. Optionally one can set openStatusPage:true
to have the status page open in a browser. One can then refresh the page to check the status.
Service area
CDNetworks offers different access domains for different areas served.
Available service areas are
US: 'https://openapi.us.cdnetworks.com/purge/rest/' KR: 'https://openapi.kr.cdnetworks.com/purge/rest/' JP: 'https://openapi.jp.cdnetworks.com/purge/rest/' CN: 'https://openapi.txnetworks.cn/purge/rest/'
The default is US/Global. I haven't noticed any difference using one or the other, but there you have them.
Again, you can override the default service area either on instantiation or later on with, for instance, setValues({servicearea:'JP'})
Example usage
var cdn= user:'my@email.com' pass:'secret' emailTo:'my@email.com'; var pidArr = ; cdn
Additional notes
Although the CDNetworks API offers both XML and JSON as output types, only JSON is supported here. I can't see why anyone would want to have to deal with XML rather than JSON when working with JavaScript. I would certainly avoid it when possible, so I did.