Skip to content

Commit

Permalink
Initial migration work
Browse files Browse the repository at this point in the history
Still blocked on a server-side compile step (for flow types) and a weird
package.json error (vercel/ncc#23)
  • Loading branch information
rosszurowski committed Nov 20, 2018
1 parent 725a7ea commit 71e61fa
Show file tree
Hide file tree
Showing 8 changed files with 1,380 additions and 515 deletions.
5 changes: 5 additions & 0 deletions api/.nowignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
dist/
build/
script/
backups/
12 changes: 0 additions & 12 deletions api/Dockerfile

This file was deleted.

7 changes: 3 additions & 4 deletions api/now.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"version": 2,
"name": "poketo-service",
"alias": "api.poketo.app",
"type": "docker",
"features": {
"cloud": "v2"
},
"public": true,
"builds": [{ "src": "src/index.js", "use": "@now/node" }],
"routes": [{ "src": "/(.*)", "dest": "/src/index.js" }],
"env": {
"MONGO_URL": "@poketo-mongo-url"
}
Expand Down
3 changes: 1 addition & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"name": "poketo-service",
"version": "0.0.2",
"private": true,
"main": "build/main.js",
"scripts": {
"start": "backpack dev | bunyan -o short",
"start": "BACKPACK=true backpack dev | bunyan -o short",
"build": "backpack build",
"lint": "xo && flow",
"test": "jest"
Expand Down
22 changes: 9 additions & 13 deletions api/src/db.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @flow

import { Database, Model } from 'mongorito';
import type { Series } from 'poketo';
import type { Bookmark } from '../../shared/types';
// import type { Series } from 'poketo';
// import type { Bookmark } from '../../shared/types';
import utils from './utils';

if (process.env.MONGO_URL === undefined && process.env.NOW !== 'true') {
Expand All @@ -15,24 +13,22 @@ export class Collection extends Model {}

const extendCollection = Collection => {
Collection.prototype.addBookmark = function(
series: Series,
linkToUrl: ?string = null,
lastReadChapterId: string | null = null,
series,
linkToUrl = null,
lastReadChapterId = null,
) {
const bookmarks = this.get('bookmarks');
const existingBookmark = bookmarks.find(
bookmark => bookmark.id === series.id,
);

if (existingBookmark) {
const err: any = new Error(
`A bookmark for ${series.url} already exists!`,
);
const err = new Error(`A bookmark for ${series.url} already exists!`);
err.status = 400;
throw err;
}

const bookmark: Bookmark = {
const bookmark = {
id: series.id,
url: series.url,
lastReadChapterId,
Expand All @@ -55,15 +51,15 @@ const extendCollection = Collection => {
);

if (bookmarkIndex === -1) {
const err: any = new Error(`Could not find bookmark with ID ${seriesId}`);
const err = new Error(`Could not find bookmark with ID ${seriesId}`);
err.status = 404;
throw err;
}

const newBookmarks = utils.deleteItemAtIndex(bookmarks, bookmarkIndex);

if (newBookmarks.length === 0) {
const err: any = new Error(
const err = new Error(
`Cannot delete last bookmark in a collection. Delete the collection instead.`,
);
err.status = 400;
Expand Down
28 changes: 15 additions & 13 deletions api/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import Koa from 'koa';
import route from 'koa-route';
import bodyparser from 'koa-bodyparser';
Expand All @@ -8,9 +6,9 @@ import cors from '@koa/cors';

import pmap from 'p-map';
import shortid from 'shortid';
import poketo from 'poketo';

import pkg from '../package';
import poketo from 'poketo';
import { Collection } from './db';
import utils from './utils';

Expand All @@ -20,7 +18,7 @@ app.use(cors());
app.use(bodyparser());
app.use(logger({ name: pkg.name }));

const getErrorStatus = (err): number => {
const getErrorStatus = err => {
switch (err.code) {
case 'INVALID_URL':
case 'UNSUPPORTED_SITE':
Expand All @@ -44,7 +42,7 @@ app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
const body: { message: string, code?: string } = {
const body = {
message: err.message,
};

Expand Down Expand Up @@ -302,13 +300,17 @@ app.use(route.get('/chapter', fetch));
* Server
*/

const PORT = process.env.PORT || '3001';
if (process.env.BACKPACK === 'true') {
const PORT = process.env.PORT || '3001';

app.listen(PORT, err => {
if (err) {
console.error(err);
return;
}
app.listen(PORT, err => {
if (err) {
console.error(err);
return;
}

console.log(`> Listening on http://localhost:${PORT}`);
});
console.log(`> Listening on http://localhost:${PORT}`);
});
}

export default app.callback();
20 changes: 9 additions & 11 deletions api/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// @flow

import { URL } from 'url';
import normalizeUrl from 'normalize-url';

export default {
isUrl: (input: string) => {
isUrl: input => {
try {
// eslint-disable-next-line no-new
new URL(normalizeUrl(input));
Expand All @@ -15,7 +13,7 @@ export default {
return true;
},

isPoketoId: (input: string) => {
isPoketoId: input => {
const components = input.split(':');
const isValidId = components.length > 1 && components.length < 4;
return isValidId;
Expand All @@ -26,22 +24,22 @@ export default {
/*
* Returns an Object keyed by the given function.
*/
keyArrayBy: (arr: Object[], getKey: (obj: Object) => string) =>
keyArrayBy: (arr, getKey) =>
arr.reduce((a, b) => ({ ...a, [getKey(b)]: b }), {}),

/*
* Returns a new Array with the given index replaced with the new item.
*/
replaceItemAtIndex: (
arr: Array<mixed>,
index: number,
item: mixed,
): Array<mixed> => [...arr.slice(0, index), item, ...arr.slice(index + 1)],
replaceItemAtIndex: (arr, index, item) => [
...arr.slice(0, index),
item,
...arr.slice(index + 1),
],

/*
* Returns a new Array with the given index removed.
*/
deleteItemAtIndex: (arr: Array<mixed>, index: number): Array<mixed> => [
deleteItemAtIndex: (arr, index) => [
...arr.slice(0, index),
...arr.slice(index + 1),
],
Expand Down
Loading

0 comments on commit 71e61fa

Please sign in to comment.