Skip to content

Commit

Permalink
feat(fastly): Add CRUD for headers
Browse files Browse the repository at this point in the history
Adds basic CRUD support for headers

fixes #35
  • Loading branch information
trieloff committed Mar 7, 2019
1 parent 8b23458 commit c00f867
Show file tree
Hide file tree
Showing 8 changed files with 590 additions and 0 deletions.
118 changes: 118 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ Helper class with high-level operations for condition-management
* [.createCondition(version, data)](#Fastly+createCondition) ⇒ <code>Promise</code>
* [.updateCondition(version, name, data)](#Fastly+updateCondition) ⇒ <code>Promise</code>
* [.deleteCondition(version, name)](#Fastly+deleteCondition) ⇒ <code>Promise</code>
* [.readHeaders(version)](#Fastly+readHeaders) ⇒ <code>Promise</code>
* [.readHeader(version, name)](#Fastly+readHeader) ⇒ <code>Promise</code>
* [.createHeader(version, data)](#Fastly+createHeader) ⇒ <code>Promise</code>
* [.updateHeader(version, name, data)](#Fastly+updateHeader) ⇒ <code>Promise</code>
* [.deleteHeader(version, name)](#Fastly+deleteHeader) ⇒ <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 @@ -1154,6 +1159,119 @@ instance.deleteCondition('34', 'extensions')
console.log(err.message);
});
```
<a name="Fastly+readHeaders"></a>

#### fastly.readHeaders(version) ⇒ <code>Promise</code>
List all headers 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#header_dd9da0592b2f1ff8ef0a4c1943f8abff

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

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

#### fastly.readHeader(version, name) ⇒ <code>Promise</code>
Get details of a single named header.

**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#header_86469e5eba4e5d6b1463e81f82a847e0

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

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

#### fastly.createHeader(version, data) ⇒ <code>Promise</code>
Create a new header 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#header_151df4ce647a8e222e730b260287cb39

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

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

#### fastly.updateHeader(version, name, data) ⇒ <code>Promise</code>
Update a header 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#header_c4257a0fd0eb017ea47b1fbb318fd61c

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

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

#### fastly.deleteHeader(version, name) ⇒ <code>Promise</code>
Delete a header 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#header_4bbb73fffda4d189bf5a19b474399a83

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

**Example**
```js
instance.deleteHeader('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
103 changes: 103 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ class Fastly {
this.readCondition,
);

this.writeHeader = this.upsertFn(
this.createHeader,
this.updateHeader,
this.readHeader,
);

this.conditions = new Conditions(this);
}
/**
Expand Down Expand Up @@ -1067,6 +1073,103 @@ class Fastly {
return this.request.delete(`/service/${this.service_id}/version/${await this.getVersion(version, 'current')}/condition/${encodeURIComponent(name)}`);
}

/* == HEADERS */

/**
* List all headers for a particular service and version.
*
* @see https://docs.fastly.com/api/config#header_dd9da0592b2f1ff8ef0a4c1943f8abff
* @example
* instance.readHeaders('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 readHeaders(version) {
return this.request.get(`/service/${this.service_id}/version/${await this.getVersion(version, 'latest')}/header`);
}

/**
* Get details of a single named header.
*
* @see https://docs.fastly.com/api/config#header_86469e5eba4e5d6b1463e81f82a847e0
* @example
* instance.readHeader('12', 'returning')
.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 header.
* @returns {Promise} The response object representing the completion or failure.
*/
async readHeader(version, name) {
return this.request.get(`/service/${this.service_id}/version/${await this.getVersion(version, 'latest')}/header/${name}`);
}

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

/**
* Update a header for a particular service and version.
*
* @see https://docs.fastly.com/api/config#header_c4257a0fd0eb017ea47b1fbb318fd61c
* @example
* instance.updateHeader('34', 'returning', { name: 'returning-visitor' })
.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 header.
* @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 updateHeader(version, name, data) {
return this.request.put(`/service/${this.service_id}/version/${await this.getVersion(version, 'current')}/header/${encodeURIComponent(name)}`, data);
}

/**
* Delete a header for a particular service and version.
*
* @see https://docs.fastly.com/api/config#header_4bbb73fffda4d189bf5a19b474399a83
* @example
* instance.deleteHeader('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 header.
* @returns {Promise} The response object representing the completion or failure.
* @fulfil {Response}
*/
async deleteHeader(version, name) {
return this.request.delete(`/service/${this.service_id}/version/${await this.getVersion(version, 'current')}/header/${encodeURIComponent(name)}`);
}

/**
* List all backends for a particular service and version.
*
Expand Down
61 changes: 61 additions & 0 deletions test/createHeader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'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/header.response');

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

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

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

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('testheader');
});

it('response body should contain all properties', () => {
[
'action',
'cache_condition',
'dst',
'ignore_if_set',
'name',
'priority',
'regex',
'request_condition',
'response_condition',
'service_id',
'src',
'substitution',
'type',
'version',
].forEach((e) => {
expect(Object.keys(res.data)).toContain(e);
});
});
});
46 changes: 46 additions & 0 deletions test/deleteHeader.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/header.response');

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

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

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

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 c00f867

Please sign in to comment.