diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-06-09 14:26:22 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-09 14:26:22 +0300 |
commit | bd36aeb53d04a13167925ecd4d5d73ed2ad09914 (patch) | |
tree | 5bc0a1e78cf72c6e1ae48f5ee3954511748c81b1 | |
parent | abe7c3220c40031b9adecf9b27ab7141b7d3f736 (diff) | |
parent | e9d0438f6c02664c652a593c686564361fa0de6b (diff) | |
download | which-api-bd36aeb53d04a13167925ecd4d5d73ed2ad09914.tar.gz |
Merge pull request #2 from eug-vs/refactor
Structurize feathers app
-rw-r--r-- | UserService.ts | 22 | ||||
-rw-r--r-- | app.ts | 32 | ||||
-rw-r--r-- | index.ts | 14 | ||||
-rw-r--r-- | services/index.ts | 8 | ||||
-rw-r--r-- | services/polls/polls.class.ts (renamed from PollService.ts) | 4 | ||||
-rw-r--r-- | services/polls/polls.service.ts | 7 | ||||
-rw-r--r-- | services/users/users.class.ts | 20 | ||||
-rw-r--r-- | services/users/users.service.ts | 7 |
8 files changed, 67 insertions, 47 deletions
diff --git a/UserService.ts b/UserService.ts deleted file mode 100644 index 0cbb649..0000000 --- a/UserService.ts +++ /dev/null @@ -1,22 +0,0 @@ -interface User { - info : { - name: string; - age: number; - nationality: string; - sex: string; - } -} - -export class UserService { - users: User[] = []; - - async find (){ - return this.users; - } - - async create(data: Pick<User, 'info'>){ - const user: User = {...data}; - this.users.push(user); - return user; - } -}
\ No newline at end of file @@ -1,10 +1,10 @@ import feathers from '@feathersjs/feathers'; -import '@feathersjs/transport-commons'; import express from '@feathersjs/express'; import socketio from '@feathersjs/socketio'; +import '@feathersjs/transport-commons'; + +import services from './services'; -import { PollService } from './PollService'; -import {UserService} from "./UserService"; const app = express(feathers()); @@ -14,24 +14,10 @@ app.use(express.static(__dirname)); app.configure(express.rest()); app.configure(socketio()); app.use(express.errorHandler()); - -app.use('/polls', new PollService()); -app.use('/users', new UserService()); - -// Add any new real-time connection to the `everybody` channel -app.on('connection', connection => - app.channel('everybody').join(connection) -); -// Publish all events to the `everybody` channel -app.publish(data => app.channel('everybody')); +app.configure(services); -app.listen(3030).on('listening', () => - console.log('Feathers server listening on localhost:3030') -); - -// For good measure let's create a message -// So our API doesn't look so empty +// Mock data app.service('polls').create({ contents: { left: { @@ -46,10 +32,10 @@ app.service('polls').create({ }); app.service('users').create({ - info: { name: 'John Doe', age: 20, - nationality: 'Belarus', - sex: 'male' - } + avatarUrl: 'https://github.com/ilyayudovin.png' }); + +export default app; + diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..35d1411 --- /dev/null +++ b/index.ts @@ -0,0 +1,14 @@ +import app from './app'; + +// Add any new real-time connection to the `everybody` channel +app.on('connection', connection => + app.channel('everybody').join(connection) +); +// Publish all events to the `everybody` channel +app.publish(data => app.channel('everybody')); + + +app.listen(3030).on('listening', () => + console.log('Feathers server listening on localhost:3030') +); + diff --git a/services/index.ts b/services/index.ts new file mode 100644 index 0000000..eeaf485 --- /dev/null +++ b/services/index.ts @@ -0,0 +1,8 @@ +import Users from './users/users.service'; +import Polls from './polls/polls.service'; + +export default (app: Application): void => { + app.configure(Users); + app.configure(Polls); +}; + diff --git a/PollService.ts b/services/polls/polls.class.ts index 2944af3..82ef155 100644 --- a/PollService.ts +++ b/services/polls/polls.class.ts @@ -8,7 +8,7 @@ interface User { avatarUrl: string; } -export interface Poll { +interface Poll { author: User; contents: { left: ImageData; @@ -22,7 +22,7 @@ const defaultUser: User = { }; -export class PollService { +export default class Polls { polls: Poll[] = []; async find () { diff --git a/services/polls/polls.service.ts b/services/polls/polls.service.ts new file mode 100644 index 0000000..a4bd816 --- /dev/null +++ b/services/polls/polls.service.ts @@ -0,0 +1,7 @@ +import { Application } from '@feathersjs/express'; +import Polls from './polls.class'; + +export default (app: Application): void => { + app.use('/polls', new Polls()); +}; + diff --git a/services/users/users.class.ts b/services/users/users.class.ts new file mode 100644 index 0000000..db6b5a9 --- /dev/null +++ b/services/users/users.class.ts @@ -0,0 +1,20 @@ +interface User { + name: string; + avatarUrl?: string; + age?: number; +} + +export default class Users { + users: User[] = []; + + async find (){ + return this.users; + } + + async create(data: Pick<User, 'name' | 'avatarUrl' | 'age'>){ + const user: User = { ...data }; + this.users.push(user); + return user; + } +} + diff --git a/services/users/users.service.ts b/services/users/users.service.ts new file mode 100644 index 0000000..bf608fe --- /dev/null +++ b/services/users/users.service.ts @@ -0,0 +1,7 @@ +import { Application } from '@feathersjs/express'; +import Users from './users.class'; + +export default (app: Application): void => { + app.use('/users', new Users()); +}; + |