@datatypes/repository
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "@datatypes/repository", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Class for repositories", | ||
@@ -10,3 +10,3 @@ "main": "source/index.js", | ||
"scripts": { | ||
"test": "node tests/instantiation.js", | ||
"test": "find ./tests -iname '*.js' -exec node {} \\;", | ||
"prepublish": "npm test" | ||
@@ -13,0 +13,0 @@ }, |
const nativeUrl = require('url') | ||
const providersMap = require('./providersMap') | ||
const providers = require('./providers') | ||
@@ -18,4 +18,23 @@ function parseRepoUrl (repoUrl) { | ||
function getNotSettableError (propertyName, value, dependencies) { | ||
const depsString = dependencies | ||
.map(dep => `".${dep}"`) | ||
.join(' and ') | ||
return new Error( | ||
`".${propertyName}" is a computed property | ||
and can not be set to "${value}" directly. | ||
Set ${depsString} instead.`.replace(/\s+/g, ' ') | ||
) | ||
} | ||
module.exports = class Repository { | ||
constructor (options = {}) { | ||
// Delete all fields which provider a getter but no setter | ||
delete options.slug | ||
delete options.fullName | ||
delete options.apiUrl | ||
delete options.object | ||
delete options.string | ||
Object.assign(this, options) | ||
@@ -55,5 +74,13 @@ } | ||
static addProvider (providerObject) { | ||
providers.add(providerObject) | ||
return this | ||
} | ||
get slug () { | ||
return '/' + this.owner + '/' + this.name | ||
} | ||
set slug (value) { | ||
throw getNotSettableError('slug', value, ['owner', 'name']) | ||
} | ||
@@ -63,5 +90,8 @@ get fullName () { | ||
} | ||
set fullName (value) { | ||
throw getNotSettableError('slug', value, ['owner', 'name']) | ||
} | ||
get url () { | ||
return providersMap[this.provider].url + this.slug | ||
return providers.get(this.provider).url + this.slug | ||
} | ||
@@ -73,13 +103,20 @@ set url (repoUrl) { | ||
get apiUrl () { | ||
return providersMap[this.provider].api + this.slug | ||
return providers.get(this.provider).apiUrl + this.slug | ||
} | ||
set apiUrl (apiUrl) { | ||
this._apiUrl = apiUrl | ||
set apiUrl (value) { | ||
throw getNotSettableError('apiUrl', value, ['provider', 'slug']) | ||
} | ||
get object () { | ||
const {name, owner, provider, url, apiUrl} = this | ||
return {name, owner, provider, url, apiUrl} | ||
const {name, owner, fullName, provider, url, apiUrl} = this | ||
return {name, owner, fullName, provider, url, apiUrl} | ||
} | ||
toJson () { | ||
set object (value) { | ||
throw getNotSettableError( | ||
'apiUrl', | ||
value, | ||
['name', 'owner', 'provider'] | ||
) | ||
} | ||
toJSON () { | ||
return this.object | ||
@@ -91,2 +128,9 @@ } | ||
} | ||
set string (value) { | ||
throw getNotSettableError( | ||
'apiUrl', | ||
value, | ||
['name', 'owner', 'provider'] | ||
) | ||
} | ||
toString () { | ||
@@ -93,0 +137,0 @@ return this.string |
7796
7
252