-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.txt
272 lines (239 loc) · 7.42 KB
/
notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const http = require('http');
http.createServer(function(req,res) {
res.writeHead(200,
{
'Content-Type': 'text/plain; charset=utf-8'
}
);
res.end('hey, my server is up and running!');
}).listen(3000);
console.log('The server has been started at localhost:3000. press Ctrl+C to stop the server');
function hello(person) {
return "Hello, " + person;
}
let user = "John User";
document.body.innerHTML = hello(user);
Araújo, Biharck Muniz. Hands-On RESTful Web Services with TypeScript 3: Design and develop scalable RESTful APIs for your applications . Packt Publishing. Kindle Edition.
"test": "mocha --require ts-node/register test/**/*.spec.ts"
{
"name": "order-api",
"version": "1.0.0",
"description": "This is the example from the Book Hands on RESTful Web Services with TypeScript 3",
"main": "./dist/server.js",
"scripts": {
"build": "npm run clean && tsc",
"clean": "rimraf dist && rimraf reports",
"lint": "tslint ./src/**/*.ts ./test/**/*.spec.ts",
"lint:fix": "tslint --fix ./src/**/*.ts ./test/**/*.spec.ts -t verbose",
"pretest": "cross-env NODE_ENV=test npm run build && npm run lint",
"test": "cross-env NODE_ENV=test mocha --reporter spec --compilers ts:ts-node/register test/**/*.spec.ts ",
"test:mutation": "stryker run",
"stryker:init": "stryker init",
"dev": "cross-env PORT=3000 NODE_ENV=dev ts-node ./src/server.ts",
"prod": "PORT=3000 npm run build && npm run start",
"tsc": "tsc"
},
"engines": {
"node": ">=8.0.0"
},
"keywords": [
"order POC",
"Hands on RESTful Web Services with TypeScript 3",
"TypeScript 3",
"Packt Books"
],
"author": "Biharck Muniz Araújo",
"license": "MIT",
"devDependencies": {
"@types/body-parser": "^1.17.0",
"@types/chai": "^4.1.7",
"@types/chai-http": "^3.0.5",
"@types/express": "^4.16.0",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.12",
"chai": "^4.2.0",
"cross-env": "^5.2.0",
"mocha": "^5.2.0",
"rimraf": "^2.6.2",
"stryker": "^0.33.1",
"stryker-api": "^0.22.0",
"stryker-html-reporter": "^0.16.9",
"stryker-mocha-framework": "^0.13.2",
"stryker-mocha-runner": "^0.15.2",
"stryker-typescript": "^0.16.1",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.17.0",
"typescript": "^3.2.1"
},
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"chai-http": "^4.2.1"
}
}
As we already know, this file is responsible for maintaining the package's dependencies for all of the libraries that this project is going to use. Up until now, all of the dependencies have been from the previous chapters. The next step is to add a file called tsconfig.json, which is responsible for keeping all TypeScript configuration together:
{
"compilerOptions": {
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": ["es6", "dom"],
"target": "es6", //default is es5
"module": "commonjs", //CommonJs style module in output
"outDir": "dist", //change the output directory
"resolveJsonModule": true //to import out json database
},
"include": [
"src/**/*.ts" //which kind of files to compile
],
"exclude": [
"node_modules" //which files or directories to ignore
]
}
Now that we have the application package dependencies and TypeScript configuration in place, it is time to add two more files. One is responsible for defining the rules for the Linters. It is called tslint.json and includes the following data:
{
"extends": ["tslint:recommended", "tslint-config-prettier"],
"rules": {
"array-type": [true, "generic"],
"no-string-literal": false,
"object-literal-shorthand": [true, "never"],
"only-arrow-functions": true,
"interface-name": false,
"max-classes-per-file": false,
"no-var-requires": false,
"ban-types": false
}
}
The second file is called stryker.conf.js and includes the configuration details for Stryker:
module.exports = function(config) {
config.set({
testRunner: 'mocha',
mochaOptions: {
files: ['test/**/*.spec.ts'],
opts: './test/mocha.opts',
ui: 'bdd',
timeout: 35000,
require: ['ts-node/register', 'source-map-support/register'],
asyncOnly: false,
},
mutator: 'typescript',
transpilers: [],
reporters: ['html', 'progress', 'dashboard'],
packageManager: 'npm',
testFramework: 'mocha',
coverageAnalysis: 'off',
tsconfigFile: 'tsconfig.json',
mutate: ['src/**/*.ts'],
})
}
{
"username": "jose",
"firstName": "test",
"lastName": "test",
"email": "test",
"password": "test",
"phone": "2343432"
}
{
"username": "Mary",
"firstName": "Mary",
"lastName": "Jane",
"email": "[email protected]",
"password": "maryjane",
"phone": "34343434"
}
{
"userId": 77,
"quantity": 5,
"shipDate": "2018-12-15T01:52:19.458Z"
}
/store/orders:
get:
tags:
- store
summary: Returns orders
description: Returns the orders
operationId: getOrder
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
additionalProperties:
type: integer
format: int32
'401':
$ref: '#/components/responses/UnauthorizedError'
security:
- bearerAuth: []
it('should return all orders so far', async () => {
return chai
.request(app)
.get(`/store/orders`)
.then(res => {
expect(res.status).to.be.equal(200)
expect(res.body.length).to.be.equal(1)
})
})
it('should not return orders because offset is higher than the size of the orders array', async () => {
return chai
.request(app)
.get(`/store/orders?offset=2&limit=2`)
.then(res => {
expect(res.status).to.be.equal(200)
expect(res.body.length).to.be.equal(0)
})
})
return formatOutput(res, { title: 'Order API' }, 200, ApplicationType.JSON)
$ docker run --name my-mongo -p 27017:27017 -v /data/mongo:/data/db -d mongo:latest
before(async () => {
expect(UserModel.modelName).to.be.equal('User')
UserModel.collection.drop()
})
it('should create a new user for Order tests and retrieve it back', async () => {
const user = {
username: 'OrderUser',
firstName: 'Order',
lastName: 'User',
email: '[email protected]',
password: 'password',
phone: '5555555',
userStatus: 1,
}
return chai
.request(app)
.post('/users')
.send(user)
.then(res => {
expect(res.status).to.be.equal(201)
expect(res.body.username).to.be.equal(user.username)
order.userId = res.body._id
})
})
openssl req -newkey rsa:2048 -nodes -keyout keytemp.pem -x509 -days 365 -out cert.pem
openssl rsa -in keytemp.pem -out key.pem
import * as passport from 'passport'
import { Strategy } from 'passport-jwt'
import { ExtractJwt } from 'passport-jwt'
export class PassportConfiguration {
constructor() {
passport.use(
new Strategy(
{
secretOrKey: 'top_secret',
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
},
async (token, done) => {
try {
return done(null, token.user)
} catch (error) {
done(error)
}
}
)
)
}
}