express-ext 0.4.15 → 0.5.0

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 +48 -71
  2. package/package.json +1 -1
  3. package/src/index.ts +55 -72
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,10 +92,10 @@ 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)
@@ -122,20 +103,19 @@ var FollowController = (function () {
122
103
  }
123
104
  FollowController.prototype.follow = function (req, res) {
124
105
  var _this = this
125
- var id = req.params.id
126
- var target = req.params.target
106
+ var userId = res.locals[this.userId]
107
+ var id = req.params[this.id]
127
108
  if (!id || id.length === 0) {
128
- res.status(400).end("'" + this.id + "' cannot be empty")
129
- return
109
+ return res.status(400).end("'" + this.id + "' cannot be empty")
130
110
  }
131
- if (!target || target.length === 0) {
132
- res.status(400).end("'" + this.target + "' cannot be empty")
133
- return
111
+ if (!userId || userId.length === 0) {
112
+ return res.status(400).end("'" + this.userId + "' cannot be empty")
134
113
  }
135
114
  this.service
136
- .follow(id, target)
137
- .then(function (count) {
138
- return res.status(200).json(count).end()
115
+ .follow(userId, id)
116
+ .then(function (result) {
117
+ var status = result > 0 ? 200 : 409
118
+ res.status(status).json(result).end()
139
119
  })
140
120
  .catch(function (err) {
141
121
  return http_1.handleError(err, res, _this.log)
@@ -143,20 +123,19 @@ var FollowController = (function () {
143
123
  }
144
124
  FollowController.prototype.unfollow = function (req, res) {
145
125
  var _this = this
146
- var id = req.params.id
147
- var target = req.params.target
126
+ var userId = res.locals[this.userId]
127
+ var id = req.params[this.id]
148
128
  if (!id || id.length === 0) {
149
- res.status(400).end("'" + this.id + "' cannot be empty")
150
- return
129
+ return res.status(400).end("'" + this.id + "' cannot be empty")
151
130
  }
152
- if (!target || target.length === 0) {
153
- res.status(400).end("'" + this.target + "' cannot be empty")
154
- return
131
+ if (!userId || userId.length === 0) {
132
+ return res.status(400).end("'" + this.userId + "' cannot be empty")
155
133
  }
156
134
  this.service
157
- .unfollow(id, target)
158
- .then(function (count) {
159
- return res.status(200).json(count).end()
135
+ .unfollow(userId, id)
136
+ .then(function (result) {
137
+ var status = result > 0 ? 200 : 410
138
+ res.status(status).json(result).end()
160
139
  })
161
140
  .catch(function (err) {
162
141
  return http_1.handleError(err, res, _this.log)
@@ -164,18 +143,16 @@ var FollowController = (function () {
164
143
  }
165
144
  FollowController.prototype.checkFollow = function (req, res) {
166
145
  var _this = this
167
- var id = req.params.id
168
- var target = req.params.target
146
+ var userId = res.locals[this.userId]
147
+ var id = req.params[this.id]
169
148
  if (!id || id.length === 0) {
170
- res.status(400).end("'" + this.id + "' cannot be empty")
171
- return
149
+ return res.status(400).end("'" + this.id + "' cannot be empty")
172
150
  }
173
- if (!target || target.length === 0) {
174
- res.status(400).end("'" + this.target + "' cannot be empty")
175
- return
151
+ if (!userId || userId.length === 0) {
152
+ return res.status(400).end("'" + this.userId + "' cannot be empty")
176
153
  }
177
154
  this.service
178
- .checkFollow(id, target)
155
+ .checkFollow(userId, id)
179
156
  .then(function (count) {
180
157
  return res.status(200).json(count).end()
181
158
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.4.15",
3
+ "version": "0.5.0",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -42,132 +42,115 @@ 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
96
  checkFollow(id: string, target: string): Promise<number>
112
97
  }
113
98
  // tslint:disable-next-line:max-classes-per-file
114
99
  export class FollowController {
115
- constructor(public log: Log, public service: FollowService, public target: string, id: string) {
100
+ constructor(protected service: FollowService, protected log: Log, id?: string, userId?: string) {
101
+ this.userId = userId && userId.length > 0 ? userId : "userId"
116
102
  this.id = id && id.length > 0 ? id : "id"
117
103
  this.follow = this.follow.bind(this)
118
104
  this.unfollow = this.unfollow.bind(this)
119
105
  this.checkFollow = this.checkFollow.bind(this)
120
106
  }
121
- id: string
107
+ protected userId: string
108
+ protected id: string
122
109
  follow(req: Request, res: Response): void {
123
- const id = req.params.id
124
- const target = req.params.target
110
+ const userId: string = res.locals[this.userId]
111
+ const id = req.params[this.id]
125
112
  if (!id || id.length === 0) {
126
- res.status(400).end(`'${this.id}' cannot be empty`)
127
- return
113
+ return res.status(400).end(`'${this.id}' cannot be empty`)
128
114
  }
129
- if (!target || target.length === 0) {
130
- res.status(400).end(`'${this.target}' cannot be empty`)
131
- return
115
+ if (!userId || userId.length === 0) {
116
+ return res.status(400).end(`'${this.userId}' cannot be empty`)
132
117
  }
133
118
  this.service
134
- .follow(id, target)
135
- .then((count) => {
136
- return res.status(200).json(count).end()
119
+ .follow(userId, id)
120
+ .then((result) => {
121
+ const status = result > 0 ? 200 : 409
122
+ res.status(status).json(result).end()
137
123
  })
138
124
  .catch((err) => handleError(err, res, this.log))
139
125
  }
140
126
  unfollow(req: Request, res: Response): void {
141
- const id = req.params.id
142
- const target = req.params.target
127
+ const userId: string = res.locals[this.userId]
128
+ const id = req.params[this.id]
143
129
  if (!id || id.length === 0) {
144
- res.status(400).end(`'${this.id}' cannot be empty`)
145
- return
130
+ return res.status(400).end(`'${this.id}' cannot be empty`)
146
131
  }
147
- if (!target || target.length === 0) {
148
- res.status(400).end(`'${this.target}' cannot be empty`)
149
- return
132
+ if (!userId || userId.length === 0) {
133
+ return res.status(400).end(`'${this.userId}' cannot be empty`)
150
134
  }
151
135
  this.service
152
- .unfollow(id, target)
153
- .then((count) => {
154
- return res.status(200).json(count).end()
136
+ .unfollow(userId, id)
137
+ .then((result) => {
138
+ const status = result > 0 ? 200 : 410
139
+ res.status(status).json(result).end()
155
140
  })
156
141
  .catch((err) => handleError(err, res, this.log))
157
142
  }
158
143
  checkFollow(req: Request, res: Response): void {
159
- const id = req.params.id
160
- const target = req.params.target
144
+ const userId: string = res.locals[this.userId]
145
+ const id = req.params[this.id]
161
146
  if (!id || id.length === 0) {
162
- res.status(400).end(`'${this.id}' cannot be empty`)
163
- return
147
+ return res.status(400).end(`'${this.id}' cannot be empty`)
164
148
  }
165
- if (!target || target.length === 0) {
166
- res.status(400).end(`'${this.target}' cannot be empty`)
167
- return
149
+ if (!userId || userId.length === 0) {
150
+ return res.status(400).end(`'${this.userId}' cannot be empty`)
168
151
  }
169
152
  this.service
170
- .checkFollow(id, target)
153
+ .checkFollow(userId, id)
171
154
  .then((count) => {
172
155
  return res.status(200).json(count).end()
173
156
  })