Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt OOP design #12

Open
neocotic opened this issue Mar 24, 2017 · 2 comments
Open

Adopt OOP design #12

neocotic opened this issue Mar 24, 2017 · 2 comments

Comments

@neocotic
Copy link
Owner

Currently, the API is a bit confusing with the whole connect/disconnect aspect that means you can only use it for one endpoint at a time. I think it's time to adopt an object-orientated programming (OOP) design that would make the API more open, configurable/extensible, easier to maintain/understand, and support the ability to connect to multiple YOURLS endpoints at any given time.

@neocotic
Copy link
Owner Author

Since OOP will also make unit testing much easier, I've moved #5 to v3 as well.

@neocotic
Copy link
Owner Author

neocotic commented Mar 24, 2017

Here's a proposal of what the new API could look like:

var yourls = new YOURLS('https://example.com/yourls-api.php')
// OR:
var yourls = new YOURLS({
  url: 'https://example.com/yourls-api.php',
  format: 'json',
  method: 'post',
  credentials: {
    username: 'admin',
    password: 'qwerty'
  }
})

yourls.shorten('https://github.com/neocotic/yourls-api', function(result, response) {
  console.log(result.shorturl)
  //=> "https://example.com/abc123"
  console.log(result.title)
  //=> "https://github.com/neocotic/yourls-api"
  console.log(result.url.keyword)
  //=> "abc123"
})
// OR:
yourls.shorten({
  url: 'https://github.com/neocotic/yourls-api',
  keyword: 'yourls',
  title: 'YOURLS API'
}, function(result, response) {
  console.log(result.shorturl)
  //=> "https://example.com/yourls"
  console.log(result.title)
  //=> "YOURLS API"
  console.log(result.url.keyword)
  //=> "yourls"
})

yourls.stats(function(result, response) {
  console.log(result.stats.total_clicks)
  //=> "98765"
  console.log(result.stats.total_links)
  //=> "123"
})
// OR:
yourls.stats({
  filter: 'last',
  limit: 5,
  start: 5
}, function(result, response) {
  console.log(result.links.length)
  //=> 5
  console.log(result.links[0].shorturl)
  //=> "https://example.com/abc123"
  console.log(result.stats.total_links)
  //=> "123"
})

yourls.db.stats(function(result, response) {
  console.log(result.total_clicks)
  //=> "98765"
  console.log(result.total_links)
  //=> "123"
})

yourls.version(function(result, response) {
  console.log(result.version)
  //=> "1.7"
})
// OR:
yourls.version({
  db: true
}, function(result, response) {
  console.log(result.version)
  //=> "1.7"
  console.log(result.db_version)
  //=> "482"
})

var myUrl = yourls.url('yourls')
// myUrl instanceof YOURLS.URL

myUrl.expand(function(result, response) {
  console.log(result.keyword)
  //=> "yourls"
  console.log(result.longurl)
  //=> "https://github.com/neocotic/yourls-api"
  console.log(result.shorturl)
  //=> "https://example.com/yourls"
})

myUrl.stats(function(result, response) {
  console.log(result.clicks)
  //=> "123"
  console.log(result.title)
  //=> "neocotic/yourls-api: JavaScript bindings for the YOURLS API"
  console.log(result.url)
  //=> "https://github.com/neocotic/yourls-api"
})

console.log(YOURLS.VERSION)
//=> "3.0.0"

This is just a draft, but you get the idea.

@neocotic neocotic mentioned this issue Mar 24, 2017
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant