Skip to content

Commit bd36aeb

Browse files
authored
Merge pull request #2 from eug-vs/refactor
Structurize feathers app
2 parents abe7c32 + e9d0438 commit bd36aeb

File tree

8 files changed

+67
-47
lines changed

8 files changed

+67
-47
lines changed

UserService.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

app.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import feathers from '@feathersjs/feathers';
2-
import '@feathersjs/transport-commons';
32
import express from '@feathersjs/express';
43
import socketio from '@feathersjs/socketio';
4+
import '@feathersjs/transport-commons';
5+
6+
import services from './services';
57

6-
import { PollService } from './PollService';
7-
import {UserService} from "./UserService";
88

99
const app = express(feathers());
1010

@@ -14,24 +14,10 @@ app.use(express.static(__dirname));
1414
app.configure(express.rest());
1515
app.configure(socketio());
1616
app.use(express.errorHandler());
17-
18-
app.use('/polls', new PollService());
19-
app.use('/users', new UserService());
20-
21-
// Add any new real-time connection to the `everybody` channel
22-
app.on('connection', connection =>
23-
app.channel('everybody').join(connection)
24-
);
25-
// Publish all events to the `everybody` channel
26-
app.publish(data => app.channel('everybody'));
17+
app.configure(services);
2718

2819

29-
app.listen(3030).on('listening', () =>
30-
console.log('Feathers server listening on localhost:3030')
31-
);
32-
33-
// For good measure let's create a message
34-
// So our API doesn't look so empty
20+
// Mock data
3521
app.service('polls').create({
3622
contents: {
3723
left: {
@@ -46,10 +32,10 @@ app.service('polls').create({
4632
});
4733

4834
app.service('users').create({
49-
info: {
5035
name: 'John Doe',
5136
age: 20,
52-
nationality: 'Belarus',
53-
sex: 'male'
54-
}
37+
avatarUrl: 'https://github.com/ilyayudovin.png'
5538
});
39+
40+
export default app;
41+

index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import app from './app';
2+
3+
// Add any new real-time connection to the `everybody` channel
4+
app.on('connection', connection =>
5+
app.channel('everybody').join(connection)
6+
);
7+
// Publish all events to the `everybody` channel
8+
app.publish(data => app.channel('everybody'));
9+
10+
11+
app.listen(3030).on('listening', () =>
12+
console.log('Feathers server listening on localhost:3030')
13+
);
14+

services/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Users from './users/users.service';
2+
import Polls from './polls/polls.service';
3+
4+
export default (app: Application): void => {
5+
app.configure(Users);
6+
app.configure(Polls);
7+
};
8+

PollService.ts renamed to services/polls/polls.class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface User {
88
avatarUrl: string;
99
}
1010

11-
export interface Poll {
11+
interface Poll {
1212
author: User;
1313
contents: {
1414
left: ImageData;
@@ -22,7 +22,7 @@ const defaultUser: User = {
2222
};
2323

2424

25-
export class PollService {
25+
export default class Polls {
2626
polls: Poll[] = [];
2727

2828
async find () {

services/polls/polls.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Application } from '@feathersjs/express';
2+
import Polls from './polls.class';
3+
4+
export default (app: Application): void => {
5+
app.use('/polls', new Polls());
6+
};
7+

services/users/users.class.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface User {
2+
name: string;
3+
avatarUrl?: string;
4+
age?: number;
5+
}
6+
7+
export default class Users {
8+
users: User[] = [];
9+
10+
async find (){
11+
return this.users;
12+
}
13+
14+
async create(data: Pick<User, 'name' | 'avatarUrl' | 'age'>){
15+
const user: User = { ...data };
16+
this.users.push(user);
17+
return user;
18+
}
19+
}
20+

services/users/users.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Application } from '@feathersjs/express';
2+
import Users from './users.class';
3+
4+
export default (app: Application): void => {
5+
app.use('/users', new Users());
6+
};
7+

0 commit comments

Comments
 (0)