express-ext 0.4.15 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/lib/index.js +42 -85
  2. package/package.json +1 -1
  3. package/src/index.ts +49 -84
package/lib/index.js CHANGED
@@ -40,31 +40,29 @@ __export(require("./search"))
40
40
  __export(require("./SearchController"))
41
41
  __export(require("./view"))
42
42
  var SavedController = (function () {
43
- function SavedController(log, service, item, id) {
43
+ function SavedController(savedService, log, id, userId) {
44
+ this.savedService = savedService
44
45
  this.log = log
45
- this.service = service
46
- this.item = item
46
+ this.userId = userId && userId.length > 0 ? userId : "userId"
47
47
  this.id = id && id.length > 0 ? id : "id"
48
48
  this.save = this.save.bind(this)
49
49
  this.remove = this.remove.bind(this)
50
- this.load = this.load.bind(this)
51
50
  }
52
51
  SavedController.prototype.save = function (req, res) {
53
52
  var _this = this
53
+ var userId = res.locals[this.userId]
54
54
  var id = req.params[this.id]
55
- var itemId = req.params[this.item]
56
55
  if (!id || id.length === 0) {
57
- res.status(400).end("'" + this.id + "' cannot be empty")
58
- return
56
+ return res.status(400).end("'" + this.id + "' cannot be empty")
59
57
  }
60
- if (!itemId || itemId.length === 0) {
61
- res.status(400).end("'" + this.item + "' cannot be empty")
62
- return
58
+ if (!userId || userId.length === 0) {
59
+ return res.status(400).end("'" + this.userId + "' cannot be empty")
63
60
  }
64
- this.service
65
- .save(id, itemId)
66
- .then(function (data) {
67
- res.status(200).json(data).end()
61
+ this.savedService
62
+ .save(userId, id)
63
+ .then(function (result) {
64
+ var status = result > 0 ? 200 : result === 0 ? 409 : 422
65
+ res.status(status).json(result).end()
68
66
  })
69
67
  .catch(function (err) {
70
68
  return http_1.handleError(err, res, _this.log)
@@ -72,36 +70,19 @@ var SavedController = (function () {
72
70
  }
73
71
  SavedController.prototype.remove = function (req, res) {
74
72
  var _this = this
73
+ var userId = res.locals[this.userId]
75
74
  var id = req.params[this.id]
76
- var itemId = req.params[this.item]
77
75
  if (!id || id.length === 0) {
78
- res.status(400).end("'" + this.id + "' cannot be empty")
79
- return
76
+ return res.status(400).end("'" + this.id + "' cannot be empty")
80
77
  }
81
- if (!itemId || itemId.length === 0) {
82
- res.status(400).end("'" + this.item + "' cannot be empty")
83
- return
78
+ if (!userId || userId.length === 0) {
79
+ return res.status(400).end("'" + this.userId + "' cannot be empty")
84
80
  }
85
- this.service
86
- .remove(id, itemId)
87
- .then(function (data) {
88
- res.status(200).json(data).end()
89
- })
90
- .catch(function (err) {
91
- return http_1.handleError(err, res, _this.log)
92
- })
93
- }
94
- SavedController.prototype.load = function (req, res) {
95
- var _this = this
96
- var id = req.params[this.id]
97
- if (!id || id.length === 0) {
98
- res.status(400).end("'" + this.id + "' cannot be empty")
99
- return
100
- }
101
- this.service
102
- .load(id)
103
- .then(function (data) {
104
- res.status(200).json(data).end()
81
+ this.savedService
82
+ .remove(userId, id)
83
+ .then(function (result) {
84
+ var status = result > 0 ? 200 : 410
85
+ res.status(status).json(result).end()
105
86
  })
106
87
  .catch(function (err) {
107
88
  return http_1.handleError(err, res, _this.log)
@@ -111,31 +92,29 @@ var SavedController = (function () {
111
92
  })()
112
93
  exports.SavedController = SavedController
113
94
  var FollowController = (function () {
114
- function FollowController(log, service, target, id) {
115
- this.log = log
95
+ function FollowController(service, log, id, userId) {
116
96
  this.service = service
117
- this.target = target
97
+ this.log = log
98
+ this.userId = userId && userId.length > 0 ? userId : "userId"
118
99
  this.id = id && id.length > 0 ? id : "id"
119
100
  this.follow = this.follow.bind(this)
120
101
  this.unfollow = this.unfollow.bind(this)
121
- this.checkFollow = this.checkFollow.bind(this)
122
102
  }
123
103
  FollowController.prototype.follow = function (req, res) {
124
104
  var _this = this
125
- var id = req.params.id
126
- var target = req.params.target
105
+ var userId = res.locals[this.userId]
106
+ var id = req.params[this.id]
127
107
  if (!id || id.length === 0) {
128
- res.status(400).end("'" + this.id + "' cannot be empty")
129
- return
108
+ return res.status(400).end("'" + this.id + "' cannot be empty")
130
109
  }
131
- if (!target || target.length === 0) {
132
- res.status(400).end("'" + this.target + "' cannot be empty")
133
- return
110
+ if (!userId || userId.length === 0) {
111
+ return res.status(400).end("'" + this.userId + "' cannot be empty")
134
112
  }
135
113
  this.service
136
- .follow(id, target)
137
- .then(function (count) {
138
- return res.status(200).json(count).end()
114
+ .follow(userId, id)
115
+ .then(function (result) {
116
+ var status = result > 0 ? 200 : 409
117
+ res.status(status).json(result).end()
139
118
  })
140
119
  .catch(function (err) {
141
120
  return http_1.handleError(err, res, _this.log)
@@ -143,41 +122,19 @@ var FollowController = (function () {
143
122
  }
144
123
  FollowController.prototype.unfollow = function (req, res) {
145
124
  var _this = this
146
- var id = req.params.id
147
- var target = req.params.target
148
- if (!id || id.length === 0) {
149
- res.status(400).end("'" + this.id + "' cannot be empty")
150
- return
151
- }
152
- if (!target || target.length === 0) {
153
- res.status(400).end("'" + this.target + "' cannot be empty")
154
- return
155
- }
156
- this.service
157
- .unfollow(id, target)
158
- .then(function (count) {
159
- return res.status(200).json(count).end()
160
- })
161
- .catch(function (err) {
162
- return http_1.handleError(err, res, _this.log)
163
- })
164
- }
165
- FollowController.prototype.checkFollow = function (req, res) {
166
- var _this = this
167
- var id = req.params.id
168
- var target = req.params.target
125
+ var userId = res.locals[this.userId]
126
+ var id = req.params[this.id]
169
127
  if (!id || id.length === 0) {
170
- res.status(400).end("'" + this.id + "' cannot be empty")
171
- return
128
+ return res.status(400).end("'" + this.id + "' cannot be empty")
172
129
  }
173
- if (!target || target.length === 0) {
174
- res.status(400).end("'" + this.target + "' cannot be empty")
175
- return
130
+ if (!userId || userId.length === 0) {
131
+ return res.status(400).end("'" + this.userId + "' cannot be empty")
176
132
  }
177
133
  this.service
178
- .checkFollow(id, target)
179
- .then(function (count) {
180
- return res.status(200).json(count).end()
134
+ .unfollow(userId, id)
135
+ .then(function (result) {
136
+ var status = result > 0 ? 200 : 410
137
+ res.status(status).json(result).end()
181
138
  })
182
139
  .catch(function (err) {
183
140
  return http_1.handleError(err, res, _this.log)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.4.15",
3
+ "version": "0.5.1",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -42,134 +42,99 @@ export * from "./search"
42
42
  export * from "./SearchController"
43
43
  export * from "./view"
44
44
 
45
- export interface SavedService<T> {
46
- load(id: string): Promise<T[]>
47
- save(id: string, itemId: string): Promise<number>
48
- remove(id: string, itemId: string): Promise<number>
45
+ export interface SavedService {
46
+ save(userId: string, id: string): Promise<number>
47
+ remove(userId: string, id: string): Promise<number>
49
48
  }
50
- export class SavedController<T> {
51
- constructor(public log: (msg: string) => void, public service: SavedService<T>, public item: string, id?: string) {
49
+ export class SavedController {
50
+ constructor(protected savedService: SavedService, protected log: (msg: string) => void, id?: string, userId?: string) {
51
+ this.userId = userId && userId.length > 0 ? userId : "userId"
52
52
  this.id = id && id.length > 0 ? id : "id"
53
53
  this.save = this.save.bind(this)
54
54
  this.remove = this.remove.bind(this)
55
- this.load = this.load.bind(this)
56
55
  }
57
- id: string
56
+ protected userId: string
57
+ protected id: string
58
58
  save(req: Request, res: Response) {
59
+ const userId: string = res.locals[this.userId]
59
60
  const id = req.params[this.id]
60
- const itemId = req.params[this.item]
61
61
  if (!id || id.length === 0) {
62
- res.status(400).end(`'${this.id}' cannot be empty`)
63
- return
62
+ return res.status(400).end(`'${this.id}' cannot be empty`)
64
63
  }
65
- if (!itemId || itemId.length === 0) {
66
- res.status(400).end(`'${this.item}' cannot be empty`)
67
- return
64
+ if (!userId || userId.length === 0) {
65
+ return res.status(400).end(`'${this.userId}' cannot be empty`)
68
66
  }
69
- this.service
70
- .save(id, itemId)
71
- .then((data) => {
72
- res.status(200).json(data).end()
67
+ this.savedService
68
+ .save(userId, id)
69
+ .then((result) => {
70
+ const status = result > 0 ? 200 : result === 0 ? 409 : 422
71
+ res.status(status).json(result).end()
73
72
  })
74
73
  .catch((err) => handleError(err, res, this.log))
75
74
  }
76
75
  remove(req: Request, res: Response) {
76
+ const userId: string = res.locals[this.userId]
77
77
  const id = req.params[this.id]
78
- const itemId = req.params[this.item]
79
78
  if (!id || id.length === 0) {
80
- res.status(400).end(`'${this.id}' cannot be empty`)
81
- return
79
+ return res.status(400).end(`'${this.id}' cannot be empty`)
82
80
  }
83
- if (!itemId || itemId.length === 0) {
84
- res.status(400).end(`'${this.item}' cannot be empty`)
85
- return
81
+ if (!userId || userId.length === 0) {
82
+ return res.status(400).end(`'${this.userId}' cannot be empty`)
86
83
  }
87
- this.service
88
- .remove(id, itemId)
89
- .then((data) => {
90
- res.status(200).json(data).end()
91
- })
92
- .catch((err) => handleError(err, res, this.log))
93
- }
94
- load(req: Request, res: Response) {
95
- const id = req.params[this.id]
96
- if (!id || id.length === 0) {
97
- res.status(400).end(`'${this.id}' cannot be empty`)
98
- return
99
- }
100
- this.service
101
- .load(id)
102
- .then((data) => {
103
- res.status(200).json(data).end()
84
+ this.savedService
85
+ .remove(userId, id)
86
+ .then((result) => {
87
+ const status = result > 0 ? 200 : 410
88
+ res.status(status).json(result).end()
104
89
  })
105
90
  .catch((err) => handleError(err, res, this.log))
106
91
  }
107
92
  }
108
93
  export interface FollowService {
109
- follow(id: string, target: string): Promise<number | undefined>
94
+ follow(id: string, target: string): Promise<number>
110
95
  unfollow(id: string, target: string): Promise<number>
111
- checkFollow(id: string, target: string): Promise<number>
112
96
  }
113
97
  // tslint:disable-next-line:max-classes-per-file
114
98
  export class FollowController {
115
- constructor(public log: Log, public service: FollowService, public target: string, id: string) {
99
+ constructor(protected service: FollowService, protected log: Log, id?: string, userId?: string) {
100
+ this.userId = userId && userId.length > 0 ? userId : "userId"
116
101
  this.id = id && id.length > 0 ? id : "id"
117
102
  this.follow = this.follow.bind(this)
118
103
  this.unfollow = this.unfollow.bind(this)
119
- this.checkFollow = this.checkFollow.bind(this)
120
104
  }
121
- id: string
105
+ protected userId: string
106
+ protected id: string
122
107
  follow(req: Request, res: Response): void {
123
- const id = req.params.id
124
- const target = req.params.target
108
+ const userId: string = res.locals[this.userId]
109
+ const id = req.params[this.id]
125
110
  if (!id || id.length === 0) {
126
- res.status(400).end(`'${this.id}' cannot be empty`)
127
- return
111
+ return res.status(400).end(`'${this.id}' cannot be empty`)
128
112
  }
129
- if (!target || target.length === 0) {
130
- res.status(400).end(`'${this.target}' cannot be empty`)
131
- return
113
+ if (!userId || userId.length === 0) {
114
+ return res.status(400).end(`'${this.userId}' cannot be empty`)
132
115
  }
133
116
  this.service
134
- .follow(id, target)
135
- .then((count) => {
136
- return res.status(200).json(count).end()
117
+ .follow(userId, id)
118
+ .then((result) => {
119
+ const status = result > 0 ? 200 : 409
120
+ res.status(status).json(result).end()
137
121
  })
138
122
  .catch((err) => handleError(err, res, this.log))
139
123
  }
140
124
  unfollow(req: Request, res: Response): void {
141
- const id = req.params.id
142
- const target = req.params.target
143
- if (!id || id.length === 0) {
144
- res.status(400).end(`'${this.id}' cannot be empty`)
145
- return
146
- }
147
- if (!target || target.length === 0) {
148
- res.status(400).end(`'${this.target}' cannot be empty`)
149
- return
150
- }
151
- this.service
152
- .unfollow(id, target)
153
- .then((count) => {
154
- return res.status(200).json(count).end()
155
- })
156
- .catch((err) => handleError(err, res, this.log))
157
- }
158
- checkFollow(req: Request, res: Response): void {
159
- const id = req.params.id
160
- const target = req.params.target
125
+ const userId: string = res.locals[this.userId]
126
+ const id = req.params[this.id]
161
127
  if (!id || id.length === 0) {
162
- res.status(400).end(`'${this.id}' cannot be empty`)
163
- return
128
+ return res.status(400).end(`'${this.id}' cannot be empty`)
164
129
  }
165
- if (!target || target.length === 0) {
166
- res.status(400).end(`'${this.target}' cannot be empty`)
167
- return
130
+ if (!userId || userId.length === 0) {
131
+ return res.status(400).end(`'${this.userId}' cannot be empty`)
168
132
  }
169
133
  this.service
170
- .checkFollow(id, target)
171
- .then((count) => {
172
- return res.status(200).json(count).end()
134
+ .unfollow(userId, id)
135
+ .then((result) => {
136
+ const status = result > 0 ? 200 : 410
137
+ res.status(status).json(result).end()
173
138
  })
174
139
  .catch((err) => handleError(err, res, this.log))
175
140
  }