From 57275beb2ee3224994386ce616f86dbee292e2ee Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 18:41:46 +0300 Subject: chore: install cors, bluebird and logash w/ types --- app.ts | 2 ++ package-lock.json | 38 ++++++++++++++++++++++++++++++++++++++ package.json | 15 +++++++++++---- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app.ts b/app.ts index 0dce8b6..c4fb0e6 100644 --- a/app.ts +++ b/app.ts @@ -2,6 +2,7 @@ import feathers from '@feathersjs/feathers'; import express from '@feathersjs/express'; import socketio from '@feathersjs/socketio'; import '@feathersjs/transport-commons'; +import cors from 'cors'; import services from './services'; @@ -14,6 +15,7 @@ app.use(express.static(__dirname)); app.configure(express.rest()); app.configure(socketio()); app.use(express.errorHandler()); +app.use(cors()); app.configure(services); diff --git a/package-lock.json b/package-lock.json index ab94251..35cc880 100644 --- a/package-lock.json +++ b/package-lock.json @@ -76,6 +76,12 @@ "radix-router": "^3.0.1" } }, + "@types/bluebird": { + "version": "3.5.32", + "resolved": "https://registry.npmjs.org/@types/bluebird/-/bluebird-3.5.32.tgz", + "integrity": "sha512-dIOxFfI0C+jz89g6lQ+TqhGgPQ0MxSnh/E4xuC0blhFtyW269+mPG5QeLgbdwst/LvdP8o1y0o/Gz5EHXLec/g==", + "dev": true + }, "@types/body-parser": { "version": "1.19.0", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.0.tgz", @@ -89,6 +95,7 @@ "version": "4.0.2", "resolved": "https://registry.npmjs.org/@types/bson/-/bson-4.0.2.tgz", "integrity": "sha512-+uWmsejEHfmSjyyM/LkrP0orfE2m5Mx9Xel4tXNeqi1ldK5XMQcDsFkBmLDtuyKUbxj2jGDo0H240fbCRJZo7Q==", + "dev": true, "requires": { "@types/node": "*" } @@ -101,6 +108,15 @@ "@types/node": "*" } }, + "@types/cors": { + "version": "2.8.6", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.6.tgz", + "integrity": "sha512-invOmosX0DqbpA+cE2yoHGUlF/blyf7nB0OGYBBiH27crcVm5NmFaZkLP4Ta1hGaesckCi5lVLlydNJCxkTOSg==", + "dev": true, + "requires": { + "@types/express": "*" + } + }, "@types/engine.io": { "version": "3.1.4", "resolved": "https://registry.npmjs.org/@types/engine.io/-/engine.io-3.1.4.tgz", @@ -130,6 +146,12 @@ "@types/range-parser": "*" } }, + "@types/lodash": { + "version": "4.14.155", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.155.tgz", + "integrity": "sha512-vEcX7S7aPhsBCivxMwAANQburHBtfN9RdyXFk84IJmu2Z4Hkg1tOFgaslRiEqqvoLtbCBi6ika1EMspE+NZ9Lg==", + "dev": true + }, "@types/mime": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.2.tgz", @@ -139,6 +161,7 @@ "version": "3.5.20", "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.5.20.tgz", "integrity": "sha512-BN0wJn670DkivxiP7ZW0InX4qBtX01qITaucD+3A+sTgPQo4XUYay0Y+sGM4MJ9OyKDRlb3RQuVAlyeWzl/NoA==", + "dev": true, "requires": { "@types/bson": "*", "@types/node": "*" @@ -148,6 +171,7 @@ "version": "5.7.23", "resolved": "https://registry.npmjs.org/@types/mongoose/-/mongoose-5.7.23.tgz", "integrity": "sha512-Yh1vXr+Y+ro887sma0kihtvbazy3prC+gjT0FJZ/Kt6rOQoHnCRm8oiOvDWGncP3YVXAoCj4YeNLp7moIyBKIQ==", + "dev": true, "requires": { "@types/mongodb": "*", "@types/node": "*" @@ -347,6 +371,15 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, "debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", @@ -741,6 +774,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, "object-component": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz", diff --git a/package.json b/package.json index 6e1f213..24616e9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "@oneflow/which-api", + "name": "which-api", "version": "1.0.0", - "main": "index.js", + "main": "index.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -13,8 +13,9 @@ "@feathersjs/feathers": "^4.5.3", "@feathersjs/socketio": "^4.5.4", "@feathersjs/transport-commons": "^4.5.3", - "@types/mongoose": "^5.7.23", + "cors": "^2.8.5", "feathers-mongoose": "^8.3.0", + "lodash": "^4.17.15", "mongoose": "^5.9.18" }, "repository": { @@ -25,5 +26,11 @@ "url": "https://github.com/eug-vs/which-api/issues" }, "homepage": "https://github.com/eug-vs/which-api#readme", - "description": "" + "description": "", + "devDependencies": { + "@types/bluebird": "^3.5.32", + "@types/cors": "^2.8.6", + "@types/lodash": "^4.14.155", + "@types/mongoose": "^5.7.23" + } } -- cgit v1.2.3 From fd03f269c0e9498e58fe7a381794ab84991b054e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 18:42:33 +0300 Subject: refactor: rename interfaces --- models/polls/poll.model.ts | 4 ++-- models/polls/poll.schema.ts | 5 ++--- models/users/user.model.ts | 4 ++-- models/users/user.schema.ts | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/models/polls/poll.model.ts b/models/polls/poll.model.ts index 211d1e6..be1f604 100644 --- a/models/polls/poll.model.ts +++ b/models/polls/poll.model.ts @@ -1,5 +1,5 @@ import { Model, model } from "mongoose" -import { Poll, PollSchema } from './poll.schema'; +import { PollSchema, pollSchema } from './poll.schema'; -export default model>("Poll", PollSchema); +export default model>("Poll", pollSchema); diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index 2e7dccd..79036c3 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -1,12 +1,11 @@ import { Document, Schema, Types } from 'mongoose'; -import { User } from '../users/user.schema'; interface ImageData { url: string; votes: number; } -export interface Poll extends Document { +export interface PollSchema extends Document { authorId: string; contents: { left: ImageData; @@ -20,7 +19,7 @@ const imageDataSchema = { votes: Number } -export const PollSchema = new Schema({ +export const pollSchema = new Schema({ contents: { left: imageDataSchema, right: imageDataSchema diff --git a/models/users/user.model.ts b/models/users/user.model.ts index cec9298..d4c67df 100644 --- a/models/users/user.model.ts +++ b/models/users/user.model.ts @@ -1,5 +1,5 @@ import { Model, model } from "mongoose" -import { User, UserSchema } from './user.schema'; +import { UserSchema, userSchema } from './user.schema'; -export default model>("User", UserSchema); +export default model>("User", userSchema); diff --git a/models/users/user.schema.ts b/models/users/user.schema.ts index c846f81..5292add 100644 --- a/models/users/user.schema.ts +++ b/models/users/user.schema.ts @@ -1,12 +1,12 @@ import { Document, Schema } from "mongoose" -export interface User extends Document { +export interface UserSchema extends Document { name: string; avatarUrl?: string; age?: number; } -export const UserSchema = new Schema({ +export const userSchema = new Schema({ name: String, avatarUrl: { type: String, -- cgit v1.2.3 From 709416b125f7099b34686ea8c1d35cf7328a19e8 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 18:43:09 +0300 Subject: feat: add expandAuthor hook to polls --- services/polls/polls.hooks.ts | 38 ++++++++++++++++++++++++++++++++++++++ services/polls/polls.service.ts | 2 ++ 2 files changed, 40 insertions(+) create mode 100644 services/polls/polls.hooks.ts diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts new file mode 100644 index 0000000..ff6a83f --- /dev/null +++ b/services/polls/polls.hooks.ts @@ -0,0 +1,38 @@ +import { HookContext } from '@feathersjs/feathers'; +import bluebird from 'bluebird'; +import _ from 'lodash'; + +import { PollSchema } from '../../models/polls/poll.schema'; +import { UserSchema } from '../../models/users/user.schema'; +import UserModel from '../../models/users/user.model'; + + +interface Poll extends Omit { + author: UserSchema; +} + +const expandAuthor = async (poll: PollSchema): Promise => { + return UserModel.findById(poll.authorId).then((author: UserSchema | null): Poll | null => { + if (author) return _.merge(_.omit(poll, 'authorId'), { author }); + return null; + }); +}; + +const expandAuthorHook = async (context: HookContext): Promise => { + context.result = await expandAuthor(context.result); + return context; +}; + +const expandAuthorManyHook = async (context: HookContext): Promise => { + context.result = await bluebird.map(context.result, (poll: any) => expandAuthor(poll)); + console.log(context.result); + return context; +}; + + +export default { + after: { + get: [expandAuthorHook], + find: [expandAuthorManyHook] + } +} diff --git a/services/polls/polls.service.ts b/services/polls/polls.service.ts index ca75d5a..9635adf 100644 --- a/services/polls/polls.service.ts +++ b/services/polls/polls.service.ts @@ -1,10 +1,12 @@ import { Application } from '@feathersjs/express'; import Model from '../../models/polls/poll.model'; import service from 'feathers-mongoose'; +import hooks from './polls.hooks' const PollService = service({ Model }); export default (app: Application): void => { app.use('/polls', PollService); + app.service('polls').hooks(hooks); }; -- cgit v1.2.3 From b0daa9b051c9f44266fc4cbd1fbe8b704fe1fc81 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 19:44:42 +0300 Subject: refactor: reorder calls in app.ts --- app.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.ts b/app.ts index c4fb0e6..4b5ac3b 100644 --- a/app.ts +++ b/app.ts @@ -12,10 +12,11 @@ const app = express(feathers()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(express.static(__dirname)); -app.configure(express.rest()); -app.configure(socketio()); app.use(express.errorHandler()); app.use(cors()); + +app.configure(express.rest()); +app.configure(socketio()); app.configure(services); -- cgit v1.2.3 From 0915870f347eecbf5d1646fa732e2a73418960a6 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 19:45:13 +0300 Subject: feat: use bluebird promise globally --- index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index b358fdb..6db8929 100644 --- a/index.ts +++ b/index.ts @@ -1,8 +1,8 @@ import app from './app'; import mongoose from 'mongoose'; +import Promise from 'bluebird'; - -mongoose.Promise = global.Promise; +mongoose.Promise = Promise; mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true }); -- cgit v1.2.3 From d40f6e6cf8150f9e112d0b5d8d7075c2fc90264b Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 19:47:48 +0300 Subject: refactor: improve interfaces and expandAuthor --- models/polls/poll.schema.ts | 9 +++++++-- models/users/user.schema.ts | 4 +++- services/polls/polls.hooks.ts | 26 ++++++++++++++------------ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index 79036c3..ea91051 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -1,18 +1,23 @@ import { Document, Schema, Types } from 'mongoose'; +import { User } from '../users/user.schema' interface ImageData { url: string; votes: number; } -export interface PollSchema extends Document { - authorId: string; +export interface Poll { + author: User; contents: { left: ImageData; right: ImageData; }; } +export interface PollSchema extends Document, Omit { + authorId: string; +} + const imageDataSchema = { url: String, diff --git a/models/users/user.schema.ts b/models/users/user.schema.ts index 5292add..e39f667 100644 --- a/models/users/user.schema.ts +++ b/models/users/user.schema.ts @@ -1,11 +1,13 @@ import { Document, Schema } from "mongoose" -export interface UserSchema extends Document { +export interface User { name: string; avatarUrl?: string; age?: number; } +export interface UserSchema extends Document, User {} + export const userSchema = new Schema({ name: String, avatarUrl: { diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index ff6a83f..fc5fa45 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -2,20 +2,22 @@ import { HookContext } from '@feathersjs/feathers'; import bluebird from 'bluebird'; import _ from 'lodash'; -import { PollSchema } from '../../models/polls/poll.schema'; -import { UserSchema } from '../../models/users/user.schema'; +import { Poll, PollSchema } from '../../models/polls/poll.schema'; +import { User } from '../../models/users/user.schema'; import UserModel from '../../models/users/user.model'; -interface Poll extends Omit { - author: UserSchema; -} - const expandAuthor = async (poll: PollSchema): Promise => { - return UserModel.findById(poll.authorId).then((author: UserSchema | null): Poll | null => { - if (author) return _.merge(_.omit(poll, 'authorId'), { author }); - return null; - }); + return UserModel.findById(poll.authorId) + .lean() + .exec() + .then((author: User | null): Poll | null => { + return author && _.merge(_.omit(poll, 'authorId'), { author }); + }) + .catch(err => { + console.error(err); + return err; + }); }; const expandAuthorHook = async (context: HookContext): Promise => { @@ -24,8 +26,8 @@ const expandAuthorHook = async (context: HookContext): Promise => { }; const expandAuthorManyHook = async (context: HookContext): Promise => { - context.result = await bluebird.map(context.result, (poll: any) => expandAuthor(poll)); - console.log(context.result); + const polls = await bluebird.map(context.result, (poll: any) => expandAuthor(poll)); + context.result = _.compact(polls); return context; }; -- cgit v1.2.3