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

APIs #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

APIs #33

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/middlewares/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function(req, res, next) {
const LIMIT = 20
const PAGE = 1
if (!req.query.page || req.query.page <= 0) {
req.query.page = PAGE
}
if (!req.query.limit || req.query.limit <= 0) {
req.query.limit = LIMIT
}
res.locals.pagination = {
page: req.query.page,
limit: req.query.limit,
}
next()
}
3 changes: 3 additions & 0 deletions src/routes/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Router } from 'express'
import { route as urlApis } from './urls'

export const route = Router()

route.use('/urls', urlApis)
139 changes: 139 additions & 0 deletions src/routes/api/urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { ensureLoggedIn } from 'connect-ensure-login'
import { Router } from 'express'
import querystring from 'querystring'
import Raven from 'raven'
import { findGroupById, findGroupByPrefix } from '../../controllers/groups'
import {
createUrl,
findUrlByCodeInt,
findUrlByShortcode,
getAllUrlsForUser,
PageOptions,
updateUrl,
URLOptions,
} from '../../controllers/urls'
import paginationMiddleware from '../../middlewares/pagination'
import { optsFromGroupedShortcode } from '../../utils/shortener'

export const route = Router()

// APIs only for logged in peeps !
route.use(ensureLoggedIn('/login'))

// Pagination Middleware
route.use(paginationMiddleware)

route.get('/', async (req, res) => {
const page: PageOptions = {
offset: res.locals.pagination.limit * (res.locals.pagination.page - 1),
limit: res.locals.pagination.limit,
}
let getAll: boolean = false
if (req.query.all && req.query.all === 'true') {
req.query.all = true
getAll = true
}
const urlsAndPagination = await getAllUrlsForUser(req.user, page, getAll)
if (urlsAndPagination.pagination.hasNext) {
urlsAndPagination.pagination.nextUrl =
'?' +
querystring.stringify({
...req.query,
page: urlsAndPagination.pagination.page + 1,
})
}
if (urlsAndPagination.pagination.hasPrev) {
urlsAndPagination.pagination.prevUrl =
'?' +
querystring.stringify({
...req.query,
page: urlsAndPagination.pagination.page - 1,
})
}

res.json(urlsAndPagination)
})

route.post('/', async (req, res) => {
try {
const url = await createUrl(
{
longUrl: req.body.longUrl,
shortCode: req.body.shortCode,
private: req.body.private,
},
req.user,
)
if (!url) {
throw new Error('Error creating shortlink. Try again')
}

res.json(url)
} catch (e) {
Raven.captureException(e)
res.send(e)
}
})

route.get('/:url', async (req, res) => {
try {
const url = await findUrlByShortcode(req.params.url)

res.json(url)
} catch (e) {
Raven.captureException(e)
res.send(e)
}
})

route.post('/:url', async (req, res) => {
try {
const newUrl: URLOptions = {
longUrl: req.body.longUrl,
private: req.body.private,
}
const urlOpts = await updateUrl(req.params.url, newUrl, req.user)
res.json(urlOpts)
} catch (e) {
Raven.captureException(e)
res.send(e)
}
})

route.get('/:group/:url', async (req, res) => {
try {
const group = await findGroupByPrefix(req.params.group)
if (!group) {
throw new Error('Group prefix does not exist')
}
const opts = optsFromGroupedShortcode(group, req.params.url)
const url = await findUrlByCodeInt(opts.codeInt)
if (!url) {
throw new Error('Shortcode does not exist')
}

res.json(url)
} catch (e) {
Raven.captureException(e)
res.send(e)
}
})

route.post('/:group/:url', async (req, res) => {
try {
const newUrl: URLOptions = {
longUrl: req.body.longUrl,
private: req.body.private,
}
const group = await findGroupByPrefix(req.params.group)
if (!group) {
throw new Error('Group prefic does not exist')
}
const urlOpts = await updateUrl(req.params.url, newUrl, req.user, group)

res.json(urlOpts)
} catch (e) {
Raven.captureException(e)
res.send(e)
}
})
13 changes: 2 additions & 11 deletions src/routes/pages/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
updateUrl,
URLOptions,
} from '../../controllers/urls'
import paginationMiddleware from '../../middlewares/pagination'
import { optsFromGroupedShortcode } from '../../utils/shortener'

export const route = Router()
Expand All @@ -22,17 +23,7 @@ export const route = Router()
route.use(ensureLoggedIn('/login'))

// Pagination middleware
route.use((req, res, next) => {
const LIMIT = 20
if (!req.query.page || req.query.page < 0) {
req.query.page = 1
}
res.locals.pagination = {
page: req.query.page,
limit: LIMIT,
}
next()
})
route.use(paginationMiddleware)

route.get('/', async (req, res) => {
const page: PageOptions = {
Expand Down