Skip to content

Commit

Permalink
feat(createSnippet): Add createSnippet class method
Browse files Browse the repository at this point in the history
Add createSnippet class method to create a snippet for a particular service and version
  • Loading branch information
philippschulte committed Mar 11, 2018
1 parent 563a0b8 commit f59570f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ Each `fastly-promises` API method returns the following response object:
- [Backend](#backend)
- [.readBackends(version)](#readBackends)
- [.updateBackend(version, name, data)](#updateBackend)
- [VCL Snippets](#vcl-snippets)
- [.createSnippet(version, data)](#createSnippet)

<a name="constructor"></a>

Expand Down Expand Up @@ -652,6 +654,39 @@ instance.updateBackend('34', 'slow-server', { name: 'fast-server' })
**Param**: data {object} The data to be sent as the request body.
**Return**: schema {promise} The response object representing the completion or failure.

---

### VCL Snippets

<a name="createSnippet"></a>

### [.createSnippet(version, data)](https://docs.fastly.com/api/config#snippet_41e0e11c662d4d56adada215e707f30d)

*Create a snippet for a particular service and version.*

**Example**:

```javascript
instance.createSnippet('36', {
name: 'your_snippet',
priority: 10,
dynamic: 1,
content: 'table referer_blacklist {}',
type: 'init'
})
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
```

**Kind**: method
**Param**: version {string} The current version of a service.
**Param**: data {object} The data to be sent as the request body.
**Return**: schema {promise} The response object representing the completion or failure.

## Tests

To run the test suite, first install the dependencies, then run the [`npm test` command](https://docs.npmjs.com/cli/test):
Expand Down
16 changes: 13 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ class Fastly {
return this.request.put(`/service/${this.service_id}/version/${version}/activate`);
}

/**
* Checks the status of all domains for a particular service and version.
* @param version {String} The current version of a service.
* @return {Promise} The response object representing the completion or failure.
*/
domainCheckAll(version = '') {
return this.request.get(`/service/${this.service_id}/version/${version}/domain/check_all`);
}

/**
* List all the domains for a particular service and version.
* @param version {String} The current version of a service.
Expand Down Expand Up @@ -160,12 +169,13 @@ class Fastly {
}

/**
* Checks the status of all domains for a particular service and version.
* Create a snippet for a particular service and version.
* @param version {String} The current version of a service.
* @param data {Object} The data to be sent as the request body.
* @return {Promise} The response object representing the completion or failure.
*/
domainCheckAll(version = '') {
return this.request.get(`/service/${this.service_id}/version/${version}/domain/check_all`);
createSnippet(version = '', data = {}) {
return this.request.post(`/service/${this.service_id}/version/${version}/snippet`, data);
}
}

Expand Down
55 changes: 55 additions & 0 deletions test/createSnippet.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const nock = require('nock');
const expect = require('expect');
const config = require('../src/config');
const fastlyPromises = require('../src/index');
const response = require('./response/createSnippet.response');

describe('#createSnippet', () => {
const fastly = fastlyPromises('923b6bd5266a7f932e41962755bd4254', 'SU1Z0isxPaozGVKXdv0eY');
let res;

nock(config.mainEntryPoint)
.post('/service/SU1Z0isxPaozGVKXdv0eY/version/1/snippet')
.reply(200, response.createSnippet);

before(async () => {
res = await fastly.createSnippet('1', { name: 'my_snippet', priority: '10', dynamic: '1', type: 'fetch' });
});

it('response should be a status 200', () => {
expect(res.status).toBe(200);
});

it('response body should exist', () => {
expect(res.data).toExist();
});

it('response body should be an object', () => {
expect(res.data).toBeA('object');
});

it('response body properties should be created', () => {
expect(res.data.name).toBe('my_snippet');
expect(res.data.priority).toBe('10');
expect(res.data.dynamic).toBe('1');
expect(res.data.type).toBe('fetch');
});

it('response body should contain all properties', () => {
expect(res.data).toIncludeKeys([
'name',
'priority',
'dynamic',
'content',
'type',
'service_id',
'version',
'deleted_at',
'created_at',
'updated_at',
'id'
]);
});
});
15 changes: 15 additions & 0 deletions test/response/createSnippet.response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports.createSnippet = {
'name': 'my_snippet',
'priority': '10',
'dynamic': '1',
'content': null,
'type': 'fetch',
'service_id': 'SU1Z0isxPaozGVKXdv0eY',
'version': '1',
'deleted_at': null,
'created_at': '2018-03-11T00:46:24Z',
'updated_at': '2018-03-11T00:46:24Z',
'id': '62Yd1WfiCBPENLloXfXmlO'
};

0 comments on commit f59570f

Please sign in to comment.