Skip to content

Commit

Permalink
feat(fastly): Add CRUD support for edge dictionaries
Browse files Browse the repository at this point in the history
Adds the ability to list (`readDictionaries`), get (`readDictionary`), create (`createDictionary`),
update (`updateDictionary`), safely create (`writeDictionary`), and delete (`deleteDictionary`) edge
dictionaries.

#6
  • Loading branch information
trieloff committed Jan 18, 2019
1 parent 930d374 commit 8458107
Show file tree
Hide file tree
Showing 9 changed files with 538 additions and 0 deletions.
118 changes: 118 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ HTTP status code can be retrieved. Known error status codes include:</p>
* [.activateVersion(version)](#Fastly+activateVersion) ⇒ <code>Promise</code>
* [.domainCheckAll(version)](#Fastly+domainCheckAll) ⇒ <code>Promise</code>
* [.readDomains(version)](#Fastly+readDomains) ⇒ <code>Promise</code>
* [.readDictionaries(version)](#Fastly+readDictionaries) ⇒ <code>Promise</code>
* [.readDictionary(version, name)](#Fastly+readDictionary) ⇒ <code>Promise</code>
* [.createDictionary(version, data)](#Fastly+createDictionary) ⇒ <code>Promise</code>
* [.updateDictionary(version, name, data)](#Fastly+updateDictionary) ⇒ <code>Promise</code>
* [.deleteDictionary(version, name)](#Fastly+deleteDictionary) ⇒ <code>Promise</code>
* [.readBackends(version)](#Fastly+readBackends) ⇒ <code>Promise</code>
* [.updateBackend(version, name, data)](#Fastly+updateBackend) ⇒ <code>Promise</code>
* [.createBackend(version, data)](#Fastly+createBackend) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -701,6 +706,119 @@ instance.readDomains('182')
console.log(err.message);
});
```
<a name="Fastly+readDictionaries"></a>

#### fastly.readDictionaries(version) ⇒ <code>Promise</code>
List all dictionaries for a particular service and version.

**Kind**: instance method of [<code>Fastly</code>](#Fastly)
**Returns**: <code>Promise</code> - The response object representing the completion or failure.
**See**: https://docs.fastly.com/api/config#dictionary_6d2cc293b994eb8c16d93e92e91f3915

| Param | Type | Description |
| --- | --- | --- |
| version | <code>string</code> | The current version of a service. |

**Example**
```js
instance.readDictionaries('12')
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
```
<a name="Fastly+readDictionary"></a>

#### fastly.readDictionary(version, name) ⇒ <code>Promise</code>
Get details of a single dictionary.

**Kind**: instance method of [<code>Fastly</code>](#Fastly)
**Returns**: <code>Promise</code> - The response object representing the completion or failure.
**See**: https://docs.fastly.com/api/config#dictionary_0e16df083830ed3b6c30b73dcef64014

| Param | Type | Description |
| --- | --- | --- |
| version | <code>string</code> | The current version of a service. |
| name | <code>string</code> | Name of the dictionary. |

**Example**
```js
instance.readDictionary('12', 'extensions')
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
```
<a name="Fastly+createDictionary"></a>

#### fastly.createDictionary(version, data) ⇒ <code>Promise</code>
Create a new dictionary for a particular service and version.

**Kind**: instance method of [<code>Fastly</code>](#Fastly)
**Returns**: <code>Promise</code> - The reponse object.
**Fulfil**: [<code>Response</code>](#Response)
**See**: https://docs.fastly.com/api/config#dictionary_7d48b87bf82433162a3b209292722125

| Param | Type | Description |
| --- | --- | --- |
| version | <code>number</code> | The version number (current if omitted). |
| data | <code>Object</code> | The dictionary definition. |

<a name="Fastly+updateDictionary"></a>

#### fastly.updateDictionary(version, name, data) ⇒ <code>Promise</code>
Update a dictionary for a particular service and version.

**Kind**: instance method of [<code>Fastly</code>](#Fastly)
**Returns**: <code>Promise</code> - The response object representing the completion or failure.
**Fulfil**: [<code>Response</code>](#Response)
**See**: https://docs.fastly.com/api/config#dictionary_8c9da370b1591d99e5389143a5589a32

| Param | Type | Description |
| --- | --- | --- |
| version | <code>string</code> | The current version of a service. |
| name | <code>string</code> | The name of the dictionary. |
| data | <code>Object</code> | The data to be sent as the request body. |

**Example**
```js
instance.updateDictionary('34', 'old-name', { name: 'new-name' })
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
```
<a name="Fastly+deleteDictionary"></a>

#### fastly.deleteDictionary(version, name) ⇒ <code>Promise</code>
Delete a dictionary for a particular service and version.

**Kind**: instance method of [<code>Fastly</code>](#Fastly)
**Returns**: <code>Promise</code> - The response object representing the completion or failure.
**Fulfil**: [<code>Response</code>](#Response)
**See**: https://docs.fastly.com/api/config#dictionary_8c9da370b1591d99e5389143a5589a32

| Param | Type | Description |
| --- | --- | --- |
| version | <code>string</code> | The current version of a service. |
| name | <code>string</code> | The name of the dictionary. |

**Example**
```js
instance.deleteDictionary('34', 'extensions')
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
```
<a name="Fastly+readBackends"></a>

#### fastly.readBackends(version) ⇒ <code>Promise</code>
Expand Down
1 change: 1 addition & 0 deletions src/httpclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function create({ baseURL, timeout, headers }) {
post: makereq('post'),
get: makereq('get'),
put: makereq('put'),
delete: makereq('delete'),
};
}

Expand Down
100 changes: 100 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ class Fastly {
this.writeVCL = this.upsertFn(this.createVCL, this.updateVCL);
this.writeSnippet = this.upsertFn(this.createSnippet, this.updateSnippet);
this.writeBackend = this.upsertFn(this.createBackend, this.updateBackend);
this.writeDictionary = this.upsertFn(
this.createDictionary,
this.updateDictionary,
this.readDictionary,
);
}
/**
* @typedef {Object} FastlyError
Expand Down Expand Up @@ -647,6 +652,101 @@ class Fastly {
return this.request.get(`/service/${this.service_id}/version/${await this.getVersion(version, 'latest')}/domain`);
}

/**
* List all dictionaries for a particular service and version.
*
* @see https://docs.fastly.com/api/config#dictionary_6d2cc293b994eb8c16d93e92e91f3915
* @example
* instance.readDictionaries('12')
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
* @param {string} version - The current version of a service.
* @returns {Promise} The response object representing the completion or failure.
*/
async readDictionaries(version) {
return this.request.get(`/service/${this.service_id}/version/${await this.getVersion(version, 'latest')}/dictionary`);
}

/**
* Get details of a single dictionary.
*
* @see https://docs.fastly.com/api/config#dictionary_0e16df083830ed3b6c30b73dcef64014
* @example
* instance.readDictionary('12', 'extensions')
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
* @param {string} version - The current version of a service.
* @param {string} name - Name of the dictionary.
* @returns {Promise} The response object representing the completion or failure.
*/
async readDictionary(version, name) {
return this.request.get(`/service/${this.service_id}/version/${await this.getVersion(version, 'latest')}/dictionary/${name}`);
}

/**
* Create a new dictionary for a particular service and version.
*
* @see https://docs.fastly.com/api/config#dictionary_7d48b87bf82433162a3b209292722125
* @param {number} version - The version number (current if omitted).
* @param {Object} data - The dictionary definition.
* @returns {Promise} The reponse object.
* @fulfil {Response}
*/
async createDictionary(version, data) {
return this.request.post(`/service/${this.service_id}/version/${await this.getVersion(version, 'current')}/dictionary`, data);
}

/**
* Update a dictionary for a particular service and version.
*
* @see https://docs.fastly.com/api/config#dictionary_8c9da370b1591d99e5389143a5589a32
* @example
* instance.updateDictionary('34', 'old-name', { name: 'new-name' })
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
* @param {string} version - The current version of a service.
* @param {string} name - The name of the dictionary.
* @param {Object} data - The data to be sent as the request body.
* @returns {Promise} The response object representing the completion or failure.
* @fulfil {Response}
*/
async updateDictionary(version, name, data) {
return this.request.put(`/service/${this.service_id}/version/${await this.getVersion(version, 'current')}/dictionary/${encodeURIComponent(name)}`, data);
}

/**
* Delete a dictionary for a particular service and version.
*
* @see https://docs.fastly.com/api/config#dictionary_8c9da370b1591d99e5389143a5589a32
* @example
* instance.deleteDictionary('34', 'extensions')
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
* @param {string} version - The current version of a service.
* @param {string} name - The name of the dictionary.
* @returns {Promise} The response object representing the completion or failure.
* @fulfil {Response}
*/
async deleteDictionary(version, name) {
return this.request.delete(`/service/${this.service_id}/version/${await this.getVersion(version, 'current')}/dictionary/${encodeURIComponent(name)}`);
}

/**
* List all backends for a particular service and version.
*
Expand Down
57 changes: 57 additions & 0 deletions test/createDictionary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

/* eslint-env mocha */

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

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

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

before(async () => {
res = await fastly.createDictionary(1, {
name: 'my_dictionary',
});
});

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

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

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

it('response body properties should be created', () => {
expect(res.data.name).toBe('my_dictionary');
expect(res.data.deleted_at).toBe(null);
expect(res.data.write_only).toBe(false);
});

it('response body should contain all properties', () => {
[
'created_at',
'deleted_at',
'id',
'name',
'service_id',
'updated_at',
'version',
'write_only',
].forEach((e) => {
expect(Object.keys(res.data)).toContain(e);
});
});
});
46 changes: 46 additions & 0 deletions test/deleteDictionary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

/* eslint-env mocha */

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

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

nock(config.mainEntryPoint)
.delete('/service/SU1Z0isxPaozGVKXdv0eY/version/1/dictionary/my_dictionary')
.reply(200, response.delete);

before(async () => {
res = await fastly.deleteDictionary(1, 'my_dictionary');
});

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

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

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

it('response body properties should be created', () => {
expect(res.data.status).toBe('ok');
});

it('response body should contain all properties', () => {
[
'status',
].forEach((e) => {
expect(Object.keys(res.data)).toContain(e);
});
});
});
Loading

0 comments on commit 8458107

Please sign in to comment.