Skip to content

Commit

Permalink
Added template endpoints (#258)
Browse files Browse the repository at this point in the history
* added template endpoints

* updated NPM version

Co-authored-by: Julie Lin <[email protected]>
  • Loading branch information
Shariq Torres and Julie Lin authored Jan 4, 2022
1 parent a010609 commit f32413a
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 6.5.3 (2022-01-04)

##### Features
* **templates** add support for HTML templates endpoint ([PR_258](https://github.com/lob/lob-node/pull/258))

### 6.5.2 (2021-10-18)

##### Bug Fix
Expand Down
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,9 @@ node create_postcard_template.js
```
node create_self_mailer.js
```

### Create an HTML template and use that template to create a postcard

```
node create_template.js
```
40 changes: 40 additions & 0 deletions examples/create_template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

/*
* This script shows how to save an HTML template for later use
* as a postcard.
*/

const fs = require('fs');
const lobFactory = require('../lib/index');
const Lob = new lobFactory('<YOUR_API_KEY>');
const file = fs.readFileSync(`${__dirname}/html/card.html`).toString();
const ADDRESS = {
name: 'Robin Joseph',
email: '[email protected]',
phone: '123456789',
address_line1: '123 Test Street',
address_line2: 'Unit 199',
address_city: 'Chicago',
address_state: 'IL',
address_zip: '60012',
address_country: 'US'
}
Lob.templates.create({html: file, description: 'TestTemplate'})
.then((template) => {
Lob.addresses.create(ADDRESS).then((address) => {
Lob.postcards.create({
description: 'Template-backed Postcard',
to: address.id,
front: template.id,
back: template.id,
merge_variables: {
name: 'Robin'
}
}).then((postcard) => {
console.log("The LOB API responded with the following postcard object", postcard)
});
});
}).catch((err) => {
console.log(err)
});
3 changes: 2 additions & 1 deletion lib/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = {
usAutocompletions: require('./usAutocompletions'),
usReverseGeocodeLookups: require('./usReverseGeocodeLookups'),
usVerifications: require('./usVerifications'),
usZipLookups: require('./usZipLookups')
usZipLookups: require('./usZipLookups'),
templates: require('./templates')
};
32 changes: 32 additions & 0 deletions lib/resources/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const ResourceBase = require('./resourceBase');

class Template extends ResourceBase {
constructor (config) {
super('templates', config);
}

list (options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}

return this._transmit('GET', null, options, null, callback);
}

create (options, callback) {
return this._transmit('POST', null, null, options, callback);
}

retrieve (id, callback) {
return this._transmit('GET', id, null, null, callback);
}

delete (id, callback) {
return this._transmit('DELETE', id, null, null, callback);
}
}

module.exports = Template;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"Lob.com",
"printing"
],
"version": "6.5.2",
"version": "6.5.3",
"homepage": "https://github.com/lob/lob-node",
"author": "Lob <[email protected]> (https://lob.com/)",
"dependencies": {
Expand Down
68 changes: 68 additions & 0 deletions test/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const { expect } = require("chai");

const EXAMPLE_TEMPLATE = {
html: "<html><body><h1>Hello World</h1></body></html>",
description: "My New Template"
}
describe('templates', () => {

describe('list', () => {
it('returns a list of saved templates', (done) => {
Lob.templates.list((err, res) => {
expect(res.object).to.eql('list');
expect(res.data).to.be.instanceof(Array);
expect(res.data.length).to.be.at.most(10);
expect(res.count).to.be.at.most(10);
return done();
});
});

it('filters templates', (done) => {
Lob.templates.list({limit: 1}, (err, res) => {
expect(res.data).to.be.instanceof(Array);
expect(res.data.length).to.eql(1);
expect(res.count).to.eql(1);
return done();
});
});
});

describe('create', () => {
it('creates a new template', (done) => {
Lob.templates.create(EXAMPLE_TEMPLATE, (err, res) => {
expect(res.object).to.eql('template');
expect(res.description).to.eql(EXAMPLE_TEMPLATE.description);
return done();
});
});
});

describe('retrieve', () => {
it('retrieves a template', (done) => {
Lob.templates.create(EXAMPLE_TEMPLATE, (err, res) => {
Lob.templates.retrieve(res.id, (err, res2) => {
expect(res2.description).to.eql(EXAMPLE_TEMPLATE.description);
expect(res2.id).to.eql(res.id);
expect(res2.object).to.eql('template');
expect(res2.published_version).to.be.instanceof(Object);
return done();
});
});
});
});

describe('delete', () => {
it('deletes a template', (done) => {
Lob.templates.create(EXAMPLE_TEMPLATE, (err, res) => {
Lob.templates.delete(res.id, (err, res2) => {
expect(res2.deleted).to.eql(true);
expect(res2.id).to.eql(res.id);
return done();
});
});
});
});

});

0 comments on commit f32413a

Please sign in to comment.