Skip to content

Commit

Permalink
added basic support for custom domains
Browse files Browse the repository at this point in the history
  • Loading branch information
noahbjohnson committed Oct 1, 2019
1 parent cf31a3f commit 701e60b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict'

function teamwork (api_key = '', domain = '') {
function teamwork (api_key = '', domain = '', custom_domain = false) {

api_key = !api_key ? process.env.TW_API : api_key
domain = !domain ? process.env.TW_SUB : domain

if (!api_key || !domain) {
throw new Error('api_key or domain not provided')
} if (custom_domain &&
!/^((\b(f|F)(t|T)(p|P)\b|\b(h|H)(t|T)(t|T)(p|P)\b|\b(h|H)(t|T)(t|T)(p|P)(s|S)\b)+(:\/\/[0-9a-zA-Z])|[0-9a-zA-Z])+\.+[0-9a-zA-Z]+(\.([0-9a-zA-z])+)+$/.test(domain)){
throw new Error('custom domains must be fully qualified and include protocol without a trailing slash')
}

const api = {
Expand Down Expand Up @@ -44,12 +47,12 @@ function teamwork (api_key = '', domain = '') {
clocking: require('./clocking')
}

const base = new api['teamwork'](api_key, domain)
const base = new api['teamwork'](api_key, domain, custom_domain)

return new Proxy(base, {
get: (target, name) => {
return api[name] ?
new api[name](api_key, domain) :
return api[name] ?
new api[name](api_key, domain, custom_domain) :
base[name]
}
})
Expand Down
7 changes: 4 additions & 3 deletions src/teamwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ var request = require('request-promise-native')

class Teamwork {

constructor(api_key, domain) {
constructor(api_key, domain, custom_domain) {
this.api_key = api_key
this.domain = domain
this.europe = domain.indexOf('.eu') === -1
this.customDomain = custom_domain
}

/**
Expand Down Expand Up @@ -39,7 +40,7 @@ class Teamwork {
authenticate() {
return this.query({
uri: '/authenticate.json',
baseUrl: 'https://' + (this.europe ? 'authenticate.teamwork.com' : 'authenticate.eu.teamwork.com')
baseUrl: this.customDomain ? this.domain : 'https://' + (this.europe ? 'authenticate.teamwork.com' : 'authenticate.eu.teamwork.com')
})
}

Expand Down Expand Up @@ -78,7 +79,7 @@ class Teamwork {

options = Object.assign({
method: 'GET',
baseUrl: `https://${this.domain}.teamwork.com`,
baseUrl: this.customDomain ? this.domain :`https://${this.domain}.teamwork.com`,
uri: '/',
json: true,
auth: {
Expand Down

0 comments on commit 701e60b

Please sign in to comment.