Skip to content

Commit

Permalink
feat(snippets): add readsnippet method
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 18, 2020
1 parent dcaf327 commit 398593a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class Fastly {
.upsertFn(this.createHttps, this.updateHttps, this.readHttps);

this.writeVCL = this.upsertFn(this.createVCL, this.updateVCL);
this.writeSnippet = this.upsertFn(this.createSnippet, this.updateSnippet);
this.writeSnippet = this.upsertFn(this.createSnippet, this.updateSnippet, this.readSnippet);
this.writeBackend = this.upsertFn(this.createBackend, this.updateBackend);

this.writeCondition = this.upsertFn(
Expand Down Expand Up @@ -916,6 +916,26 @@ class Fastly {
return this.request.get(`/service/${this.service_id}/version/${await this.getVersion(version, 'latest')}/condition/${name}`);
}

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

/**
* Create a new condition for a particular service and version.
*
Expand Down
53 changes: 53 additions & 0 deletions test/readSnippet.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

'use strict';

process.env.HELIX_FETCH_FORCE_HTTP1 = 'true';
/* 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/readSnippets.response');

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

nock(config.mainEntryPoint)
.get('/service/SU1Z0isxPaozGVKXdv0eY/version/182/snippet/my_snippet')
.reply(200, response.readSnippets[0]);

before(async () => {
res = await fastly.readSnippet('182', 'my_snippet');
});

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 array', () => {
expect(typeof res.data).toBe('object');
});

it('response body should contain all properties', () => {
['id', 'service_id', 'version', 'name', 'priority', 'dynamic', 'type', 'content', 'created_at', 'updated_at', 'deleted_at'].forEach((e) => {
expect(Object.keys(res.data)).toContain(e);
});
});
});

0 comments on commit 398593a

Please sign in to comment.