Skip to content

Commit

Permalink
Refactored templates library (#169)
Browse files Browse the repository at this point in the history
* Updated templates lib and tests

* Updated templates doc and examples

* updated tests for payloads and cloneDeep

* template refactoring for standardized verbs

* Added optional options object to template update

* eslint fixes

* jsdoc updates

* fixed webhook imports
  • Loading branch information
aydrian authored Oct 25, 2016
1 parent 1a20aaa commit 70cf902
Show file tree
Hide file tree
Showing 20 changed files with 481 additions and 358 deletions.
51 changes: 18 additions & 33 deletions docs/resources/templates.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
# Templates

This library provides easy access to the [Templates](https://www.sparkpost.com/api#/reference/templates/) Resource.
This library provides easy access to the [Templates](https://developers.sparkpost.com/api/templates) Resource.

*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).*

## Methods
* **all(callback)**
* **list()**<br />
List a summary of all templates.
* `callback` - executed after task is completed. **required**
* standard `callback(err, data)`
* `err` - any error that occurred
* `data` - full response from request client
* **find(options, callback)**
Retrieve details about a specified template by its id
* **get(id[, options])**<br />
Get details about a specified template by its id
* `options.id` - the id of the template you want to look up **required**
* `options.draft` - specifies a draft or published template
* `callback` - see all function
* **create(options, callback)**
* **create(template)**<br />
Create a new template
* `options.template` - a template object **required**
* `callback` - see all function
* **delete(id, callback)**
* `template` - an object of [template attributes](https://developers.sparkpost.com/api/templates#header-template-attributes) **required**
* **update(id, template[, options])**<br />
Update an existing template
* `id` - the id of the template you want to update **required**
* `template` - an object of [template attributes](https://developers.sparkpost.com/api/templates#header-template-attributes) **required**
* `options.update_published` - If true, directly overwrite the existing published template. If false, create a new draft.
* **delete(id)**<br />
Delete an existing template
* `id` - the id of the template you want to delete **required**
* `callback` - see all function
* **preview(options, callback)**
* **preview(id[, options])**<br />
Preview the most recent version of an existing template by id
* `options.id` - the id of the template you want to look up **required**
* `options.data` - Object of substitution data
* `id` - the id of the template you want to look up **required**
* `options.substitution_data` - Object of substitution data
* `options.draft` - specifies a draft or published template
* `callback` - see all function

## Examples

```js
var SparkPost = require('sparkpost');
var client = new SparkPost('YOUR_API_KEY');

client.templates.all(function(err, data) {
if(err) {
console.log(err);
return;
}

console.log(data);
});

```

Check out all the examples provided [here](/examples/templates).
Visit our examples section to see all of [our template resource examples](/examples/templates).
36 changes: 36 additions & 0 deletions examples/templates/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, template = {
id: 'TEST_ID',
name: 'Test Template',
content: {
from: '[email protected]',
subject: 'Test email template!',
html: '<b>This is a test email template!</b>'
}
};

// Promise
client.templates.create(template)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Callback
client.templates.create(template, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});
25 changes: 0 additions & 25 deletions examples/templates/create_template.js

This file was deleted.

27 changes: 27 additions & 0 deletions examples/templates/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

// Promise
client.templates.delete('TEST_ID')
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Callback
client.templates.delete('TEST_ID', function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});
14 changes: 0 additions & 14 deletions examples/templates/delete_template.js

This file was deleted.

27 changes: 27 additions & 0 deletions examples/templates/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

// Promise
client.templates.get('TEST_ID')
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Callback
client.templates.get('TEST_ID', function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});
14 changes: 0 additions & 14 deletions examples/templates/get_all_templates.js

This file was deleted.

30 changes: 30 additions & 0 deletions examples/templates/get_draft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
draft: true
};

// Promise
client.templates.get('TEST_ID', options)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Callback
client.templates.get('TEST_ID', options, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});
18 changes: 0 additions & 18 deletions examples/templates/get_draft_template.js

This file was deleted.

17 changes: 0 additions & 17 deletions examples/templates/get_template.js

This file was deleted.

27 changes: 27 additions & 0 deletions examples/templates/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

// Promise
client.templates.list()
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Callback
client.templates.list(function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});
30 changes: 30 additions & 0 deletions examples/templates/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
id: 'TEST_ID',
substitution_data: {}
};

client.templates.preview(options)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.templates.preview(options, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});
18 changes: 0 additions & 18 deletions examples/templates/preview_template.js

This file was deleted.

34 changes: 34 additions & 0 deletions examples/templates/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, template = {
content: {
from: '[email protected]',
subject: 'Updated Test email template!',
html: '<b>This is a test email template! Updated!</b>'
}
};

// Promise
client.templates.update('TEST_ID', template)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Callback
client.templates.update('TEST_ID', template, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});
Loading

0 comments on commit 70cf902

Please sign in to comment.