aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.ts5
-rw-r--r--index.ts4
-rw-r--r--models/polls/poll.model.ts4
-rw-r--r--models/polls/poll.schema.ts12
-rw-r--r--models/users/user.model.ts4
-rw-r--r--models/users/user.schema.ts6
-rw-r--r--package-lock.json38
-rw-r--r--package.json15
-rw-r--r--services/polls/polls.hooks.ts40
-rw-r--r--services/polls/polls.service.ts2
10 files changed, 113 insertions, 17 deletions
diff --git a/app.ts b/app.ts
index 0dce8b6..4b5ac3b 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';
@@ -11,9 +12,11 @@ const app = express(feathers());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname));
+app.use(express.errorHandler());
+app.use(cors());
+
app.configure(express.rest());
app.configure(socketio());
-app.use(express.errorHandler());
app.configure(services);
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 });
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, Model<Poll>>("Poll", PollSchema);
+export default model<PollSchema, Model<PollSchema>>("Poll", pollSchema);
diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts
index 2e7dccd..ea91051 100644
--- a/models/polls/poll.schema.ts
+++ b/models/polls/poll.schema.ts
@@ -1,26 +1,30 @@
import { Document, Schema, Types } from 'mongoose';
-import { User } from '../users/user.schema';
+import { User } from '../users/user.schema'
interface ImageData {
url: string;
votes: number;
}
-export interface Poll extends Document {
- authorId: string;
+export interface Poll {
+ author: User;
contents: {
left: ImageData;
right: ImageData;
};
}
+export interface PollSchema extends Document, Omit<Poll, 'author'> {
+ authorId: string;
+}
+
const imageDataSchema = {
url: String,
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, Model<User>>("User", UserSchema);
+export default model<UserSchema, Model<UserSchema>>("User", userSchema);
diff --git a/models/users/user.schema.ts b/models/users/user.schema.ts
index c846f81..e39f667 100644
--- a/models/users/user.schema.ts
+++ b/models/users/user.schema.ts
@@ -1,12 +1,14 @@
import { Document, Schema } from "mongoose"
-export interface User extends Document {
+export interface User {
name: string;
avatarUrl?: string;
age?: number;
}
-export const UserSchema = new Schema({
+export interface UserSchema extends Document, User {}
+
+export const userSchema = new Schema({
name: String,
avatarUrl: {
type: String,
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"
+ }
}
diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts
new file mode 100644
index 0000000..fc5fa45
--- /dev/null
+++ b/services/polls/polls.hooks.ts
@@ -0,0 +1,40 @@
+import { HookContext } from '@feathersjs/feathers';
+import bluebird from 'bluebird';
+import _ from 'lodash';
+
+import { Poll, PollSchema } from '../../models/polls/poll.schema';
+import { User } from '../../models/users/user.schema';
+import UserModel from '../../models/users/user.model';
+
+
+const expandAuthor = async (poll: PollSchema): Promise<Poll | null> => {
+ return UserModel.findById(poll.authorId)
+ .lean<User>()
+ .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<HookContext> => {
+ context.result = await expandAuthor(context.result);
+ return context;
+};
+
+const expandAuthorManyHook = async (context: HookContext): Promise<HookContext> => {
+ const polls = await bluebird.map(context.result, (poll: any) => expandAuthor(poll));
+ context.result = _.compact(polls);
+ 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);
};