Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
feat(client): Add Hostname Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nprail committed Jul 25, 2018
0 parents commit 4f14c9f
Show file tree
Hide file tree
Showing 10 changed files with 8,702 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
89 changes: 89 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

# Created by https://www.gitignore.io/api/node,code

### Code ###
# Visual Studio Code - https://code.visualstudio.com/
.settings/
.vscode/
tsconfig.json
jsconfig.json

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless


# End of https://www.gitignore.io/api/node,code

out
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js
node_js:
- "node"
cache:
directories:
- ~/.npm
script:
- npm test
notifications:
email: false
after_success:
- npm run travis-deploy-once "npm run semantic-release"
branches:
except:
- /^v\d+\.\d+\.\d+$/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Filiosoft, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<h1 align="center">Fly.io API Client</h1>

<p align="center">
<a href="https://github.com/Filiosoft/fly-api/blob/master/LICENSE"><img src="https://img.shields.io/github/license/Filiosoft/fly-api.svg" alt="GitHub license"></a>
<a href="https://www.npmjs.com/package/@filiosoft/fly"><img src="https://img.shields.io/npm/v/@filiosoft/fly.svg" alt="npm"></a>
<a href="https://travis-ci.com/Filiosoft/fly-api"><img src="https://travis-ci.com/Filiosoft/fly-api.svg?branch=master" alt="Build Status"></a>
<a href="http://commitizen.github.io/cz-cli/"><img src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg" alt="Commitizen friendly"></a>
<a href="https://standardjs.com"><img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="JavaScript Style Guide"></a>
<a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release"></a>

</p>
<p align="center"><b>A simple API client for [Fly.io](https://fly.io/docs/api/)</b></p>

## Usage

### Installation

```bash
npm install @filiosoft/fly
```

> This package uses async/await and requires Node.js 7.6
### Use

```javascript
const Fly = require("@filiosoft/fly");

const fly = new Fly("your-access-token");

const hostnames = await fly.getHostnames('name-of-site')
```

You can find the full docs [here](https://filiosoft.org/fly-api/Fly.html).

## License

Copyright © 2018, Filiosoft, LLC. Released under the [MIT License](LICENSE).
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Fly = require('./lib/flyApi')

module.exports = Fly
113 changes: 113 additions & 0 deletions lib/flyApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const axios = require('axios')

class Fly {
/**
* Create a Fly API Client
* @param {string} apiKey Fly.io access token
* @param {string} [resourceType=apps] Site type (`apps` or `sites`)
*/
constructor (apiKey, resourceType = 'apps') {
this.apiKey = apiKey
this.resourceType = resourceType

this.apiVersion = 'v1'
this.apiUrl = `https://fly.io/api/${this.apiVersion}/${this.resourceType}`

this.axios = axios.create({
baseURL: this.apiUrl,
headers: { Authorization: `Bearer ${this.apiKey}` }
})
}

/**
* Creates a new Error
* @private
* @param {Error} err Error object
*/
createError (err) {
const error = new Error(err)
if (err.response) {
if (err.response.status) {
error.status = err.response.status
}

if (err.response.data && err.response.data.errors) {
error.message = err.response.data.errors[0].title
}
}

throw error
}

/**
* List a site's hostnames
* @param {string} name Name of the site
*
* @returns {Promise} Promise object represents the API response
*/
async getHostnames (name) {
try {
const res = await this.axios.get(`/${name}/hostnames`)
return res.data.data
} catch (err) {
return this.createError(err)
}
}

/**
* Create a new site hostname
* @param {string} name Name of the site
* @param {string} hostname The new hostname
*
* @returns {Promise} Promise object represents the API response
*/
async postHostname (name, hostname) {
try {
const data = {
data: {
attributes: {
hostname: hostname
}
}
}
const res = await this.axios.post(`/${name}/hostnames`, data)
return res.data.data
} catch (err) {
return this.createError(err)
}
}

/**
* Get a hostname of a site
* @param {string} name Name of the site
* @param {string} hostname The requested hostname
*
* @returns {Promise} Promise object represents the API response
*/
async getHostname (name, hostname) {
try {
const res = await this.axios.get(`/${name}/hostnames/${hostname}`)
return res.data.data
} catch (err) {
return this.createError(err)
}
}

/**
* Delete a site hostname
* @param {string} name Name of the site
* @param {string} hostname Hostname to delete
*
* @returns {Promise} Promise object represents the API response
*/
async deleteHostname (name, hostname) {
try {
const res = await this.axios.delete(`/${name}/hostnames/${hostname}`)
return res.data.data
} catch (err) {
return this.createError(err)
}
}
}

module.exports = Fly
Loading

0 comments on commit 4f14c9f

Please sign in to comment.