-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
283 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
HASH_MIN_LENGTH=6 | ||
HASH_SALT= | ||
HASH_MAX_LENGTH=100 | ||
DOMAIN=foo.bar | ||
MONGODB_URI=mongodb://localhost/myURL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const mongoose = require('mongoose') | ||
|
||
mongoose.connect(process.env.MONGODB_URI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}) | ||
mongoose.connection.on('error', err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) | ||
|
||
module.exports = mongoose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,40 @@ | ||
const { URL } = require('url') | ||
const normalizeUrl = require('normalize-url') | ||
const HashIds = require('hashids/cjs') | ||
const hash = new HashIds(process.env.HASH_SALT, process.env.HASH_MIN_LENGTH - 0) | ||
const hash = new HashIds(process.env.DOMAIN, process.env.HASH_MIN_LENGTH - 0) | ||
|
||
class Link { | ||
constructor(link) { | ||
this.url = normalizeUrl(link, { stripProtocol: true }) | ||
} | ||
const mongoose = require('../lib/mongoose') | ||
const AutoIncrement = require('mongoose-sequence')(mongoose) | ||
|
||
async exists() { | ||
// TODO: check if this link exists | ||
} | ||
const schema = new mongoose.Schema( | ||
{ | ||
_id: { | ||
type: Number, | ||
get: id => hash.encode(id) | ||
}, | ||
url: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
minlength: process.env.HASH_MIN_LENGTH, | ||
maxlength: process.env.HASH_MAX_LENGTH, | ||
validate: [ | ||
{ | ||
validator: url => new URL(url).host !== process.env.DOMAIN, | ||
msg: `Cannot shorten ${process.env.DOMAIN} URLs` | ||
} | ||
], | ||
set: url => normalizeUrl(url, { forceHttps: true }), | ||
unique: true | ||
} | ||
}, | ||
{ _id: false } | ||
) | ||
|
||
async save() { | ||
// TODO: fetch next ID | ||
hash.encode(1) | ||
} | ||
schema.plugin(require('mongoose-unique-validator')) | ||
schema.plugin(AutoIncrement) | ||
schema.statics.findByHashId = function(hashId) { | ||
return this.findById(hash.decode(hashId)[0]) | ||
} | ||
|
||
module.exports = Link | ||
module.exports = mongoose.model('Link', schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require('dotenv').config() | ||
const Link = require('../models/link') | ||
|
||
describe('Link model', () => { | ||
const url = 'www.nodejs.org' | ||
const normalizedURL = 'https://nodejs.org' | ||
let id | ||
|
||
beforeAll(async () => { | ||
return Link.deleteMany({}) | ||
}) | ||
|
||
test('shorten URL', async () => { | ||
const link = new Link({ url }) | ||
const doc = await link.save() | ||
id = doc.id | ||
|
||
expect(doc.id).toBeDefined() | ||
expect(typeof doc.id).toBe('string') | ||
expect(doc.id.length).toBeGreaterThanOrEqual(6) | ||
}) | ||
|
||
test('fetch URL', async () => { | ||
const doc = await Link.findByHashId(id) | ||
|
||
expect(doc.url).toBe(normalizedURL) | ||
}) | ||
}) |
Oops, something went wrong.