Skip to content

Commit 705dfae

Browse files
authored
Merge pull request #193 from StatelessStudio/v3.3.0
[3.3.0] Nov-14-2019
2 parents 711808c + e46b77d commit 705dfae

54 files changed

Lines changed: 985 additions & 525 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- "10.15.3"
3+
- "12.13.0"
44
before_script:
55
- psql -c 'create database testdb;' -U postgres
66
- cp local.config.travis.json local.config.json

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# PointyApi Changelog
22

3-
## [3.3.0] Nov-13-2019
3+
## [3.3.0] Nov-14-2019
44

55
### Additions
6+
- Add custom options object to HttpClient methods
7+
- Add npm keywords
8+
- [Issue #192] HttpClient must allow for custom headers
69

710
### Fixes
8-
- Lint
11+
- Node 12 Upgrade
12+
- Test classes cannot use function `this`
13+
- npm update (includes audit & outdated)
14+
- Lint (on master)
915

1016
## [3.2.0] Aug-19-2019
1117

package-lock.json

Lines changed: 368 additions & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,38 @@
2424
"dependencies": {
2525
"@types/atob": "^2.1.2",
2626
"@types/btoa": "^1.2.3",
27-
"@types/express": "^4.17.1",
28-
"@types/jsonwebtoken": "^7.2.8",
29-
"@types/node": "^10.14.15",
30-
"@types/request": "^2.48.2",
27+
"@types/express": "^4.17.2",
28+
"@types/jsonwebtoken": "^8.3.5",
29+
"@types/node": "^12.12.7",
30+
"@types/request": "^2.48.3",
3131
"atob": "^2.1.2",
3232
"bcryptjs": "^2.4.3",
3333
"btoa": "^1.2.1",
34-
"class-validator": "^0.9.1",
34+
"class-validator": "^0.11.0",
3535
"express": "^4.17.1",
3636
"jsonwebtoken": "^8.5.1",
37-
"mock-req-res": "^1.1.1",
37+
"mock-req-res": "^1.1.2",
3838
"pg": "^7.12.1",
3939
"pg-connection-string": "^2.1.0",
4040
"reflect-metadata": "^0.1.13",
4141
"request": "^2.88.0",
42-
"sinon": "^7.4.1",
43-
"typeorm": "^0.2.18",
44-
"validator": "^10.11.0"
42+
"sinon": "^7.5.0",
43+
"typeorm": "^0.2.20",
44+
"validator": "^12.0.0"
4545
},
4646
"main": "./index.js",
4747
"types": "./index.d.ts",
4848
"devDependencies": {
4949
"@istanbuljs/nyc-config-typescript": "^0.1.3",
50-
"@types/jasmine": "^2.8.16",
51-
"coveralls": "^3.0.6",
52-
"jasmine": "^3.4.0",
50+
"@types/jasmine": "^3.4.6",
51+
"coveralls": "^3.0.7",
52+
"jasmine": "^3.5.0",
5353
"nyc": "^14.1.1",
54-
"source-map-support": "^0.5.13",
55-
"ts-node": "^7.0.1",
56-
"tslint": "^5.18.0",
54+
"source-map-support": "^0.5.16",
55+
"ts-node": "^8.5.0",
56+
"tslint": "^5.20.1",
5757
"tslint-eslint-rules": "^5.4.0",
58-
"typescript": "^3.5.3"
58+
"typescript": "^3.7.2"
5959
},
6060
"description": "*\"Stop writing endpoints\"*",
6161
"directories": {
@@ -69,5 +69,27 @@
6969
"bugs": {
7070
"url": "https://github.com/statelessstudio/pointyapi/issues"
7171
},
72-
"homepage": "https://github.com/statelessstudio/pointyapi#readme"
72+
"homepage": "https://github.com/statelessstudio/pointyapi#readme",
73+
"keywords": [
74+
"api",
75+
"server",
76+
"framework",
77+
"typescript",
78+
"database",
79+
"orm",
80+
"endpoint",
81+
"filter",
82+
"guard",
83+
"handler",
84+
"http",
85+
"middleware",
86+
"model",
87+
"query",
88+
"request",
89+
"response",
90+
"util",
91+
"jwt",
92+
"authentication",
93+
"authorization"
94+
]
7395
}

src/http/http-client.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,26 @@ export class HttpClient {
2424
// Server PORT
2525
public port: number | string = process.env.PORT;
2626

27+
// Global headers
28+
public headers?: Object;
29+
2730
/**
2831
* Send a POST http request to the server
2932
* @param path Path to send to (e.g. /users)
3033
* @param data Data to send as query
3134
* @param bearer Bearer token to send
3235
* @param expect Array of status codes to expect
36+
* @param customOptions Additional request options
3337
* @return Returns a promise of HttpClientResponse
3438
*/
3539
public post(
3640
path: string,
3741
data: object,
3842
bearer: boolean | string = false,
39-
expect: number[] = [ 200, 201, 202, 204 ]
43+
expect: number[] = [ 200, 201, 202, 204 ],
44+
customOptions?: object
4045
): Promise<HttpClientResponse> {
41-
const options = {
46+
let options = {
4247
method: 'POST',
4348
url: `${this.url}:${this.port}${path}`,
4449
body: data,
@@ -48,6 +53,14 @@ export class HttpClient {
4853
if (bearer) {
4954
options['auth'] = { bearer: `${bearer}` };
5055
}
56+
57+
if (this.headers) {
58+
options['headers'] = this.headers;
59+
}
60+
61+
if (customOptions) {
62+
options = Object.assign(options, customOptions);
63+
}
5164

5265
return new Promise<HttpClientResponse>((accept, reject) => {
5366
this.request(options, (error, response, body) => {
@@ -75,16 +88,18 @@ export class HttpClient {
7588
* @param data Data to send as query
7689
* @param bearer Bearer token to send
7790
* @param expect Array of status codes to expect
91+
* @param customOptions Additional request options
7892
* @return Returns a promise of HttpClientResponse
7993
*/
8094
public get(
8195
path: string,
8296
data: boolean | Object = false,
8397
bearer: boolean | string = false,
84-
expect: number[] = [ 200, 202 ]
98+
expect: number[] = [ 200, 202 ],
99+
customOptions?: object
85100
): Promise<HttpClientResponse> {
86101
return new Promise<HttpClientResponse>((accept, reject) => {
87-
const options = {
102+
let options = {
88103
method: 'GET',
89104
url: `${this.url}:${this.port}${path}`,
90105
json: true,
@@ -94,6 +109,14 @@ export class HttpClient {
94109
if (bearer) {
95110
options['auth'] = { bearer: `${bearer}` };
96111
}
112+
113+
if (this.headers) {
114+
options['headers'] = this.headers;
115+
}
116+
117+
if (customOptions) {
118+
options = Object.assign(options, customOptions);
119+
}
97120

98121
this.request(options, (error, response, body) => {
99122
if (error) {
@@ -120,15 +143,17 @@ export class HttpClient {
120143
* @param data Data to send as query
121144
* @param bearer Bearer token to send
122145
* @param expect Array of status codes to expect
146+
* @param customOptions Additional request options
123147
* @return Returns a promise of HttpClientResponse
124148
*/
125149
public patch(
126150
path: string,
127151
data: object,
128152
bearer: boolean | string = false,
129-
expect: number[] = [ 200, 201, 202, 204 ]
153+
expect: number[] = [ 200, 201, 202, 204 ],
154+
customOptions?: object
130155
): Promise<HttpClientResponse> {
131-
const options = {
156+
let options = {
132157
method: 'PATCH',
133158
url: `${this.url}:${this.port}${path}`,
134159
body: data,
@@ -138,6 +163,14 @@ export class HttpClient {
138163
if (bearer) {
139164
options['auth'] = { bearer: `${bearer}` };
140165
}
166+
167+
if (this.headers) {
168+
options['headers'] = this.headers;
169+
}
170+
171+
if (customOptions) {
172+
options = Object.assign(options, customOptions);
173+
}
141174

142175
return new Promise<HttpClientResponse>((accept, reject) => {
143176
this.request(options, (error, response, body) => {
@@ -165,22 +198,32 @@ export class HttpClient {
165198
* @param data Data to send as query
166199
* @param bearer Bearer token to send
167200
* @param expect Array of status codes to expect
201+
* @param customOptions Additional request options
168202
* @return Returns a promise of HttpClientResponse
169203
*/
170204
public delete(
171205
path: string,
172206
bearer: boolean | string = false,
173-
expect: number[] = [ 200, 202, 204 ]
207+
expect: number[] = [ 200, 202, 204 ],
208+
customOptions?: object
174209
): Promise<HttpClientResponse> {
175210
return new Promise<HttpClientResponse>((accept, reject) => {
176-
const options = {
211+
let options = {
177212
method: 'DELETE',
178213
url: `${this.url}:${this.port}${path}`
179214
};
180215

181216
if (bearer) {
182217
options['auth'] = { bearer: `${bearer}` };
183218
}
219+
220+
if (this.headers) {
221+
options['headers'] = this.headers;
222+
}
223+
224+
if (customOptions) {
225+
options = Object.assign(options, customOptions);
226+
}
184227

185228
this.request(options, (error, response, body) => {
186229
if (error) {

test/spec/api/bodyguard/compare-id-to-user.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@ import { getBodyguardKeys, compareIdToUser } from '../../../../src/bodyguard';
66
* pointyapi/bodyguard
77
*/
88
describe('[Bodyguard] compareIdToUser', () => {
9+
let user;
10+
let guardKeys;
11+
912
beforeAll(() => {
1013
// Create user
11-
this.user = new ExampleUser();
12-
this.user.id = 2;
14+
user = new ExampleUser();
15+
user.id = 2;
1316

1417
// Get bodyguard keys
15-
this.guardKeys = getBodyguardKeys(new ExampleUser());
18+
guardKeys = getBodyguardKeys(new ExampleUser());
1619
});
1720

1821
it('returns true if the user matches', () => {
19-
// Check if this.user has an id of 2
20-
const result = compareIdToUser('id', 2, this.user, this.guardKeys);
22+
// Check if user has an id of 2
23+
const result = compareIdToUser('id', 2, user, guardKeys);
2124

2225
expect(result).toBe(true);
2326
});
2427

2528
it('returns false if the user does not match', () => {
2629
// This user should not have an id of 3
27-
const result = compareIdToUser('id', 3, this.user, this.guardKeys);
30+
const result = compareIdToUser('id', 3, user, guardKeys);
2831

2932
expect(result).toBe(false);
3033
});

test/spec/api/database/base-db.spec.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,30 @@ import { BaseDb } from '../../../../src/database';
55
* pointyapi/database
66
*/
77
describe('[BaseDb]', () => {
8+
let baseDb;
9+
let clog;
10+
let cerr;
11+
812
beforeAll(() => {
9-
this.baseDb = new BaseDb();
13+
baseDb = new BaseDb();
1014
});
1115

1216
beforeEach(() => {
13-
this.clog = console.log;
14-
this.cerr = console.error;
17+
clog = console.log;
18+
cerr = console.error;
1519
});
1620

1721
afterEach(() => {
18-
console.log = this.clog;
19-
console.error = this.cerr;
22+
console.log = clog;
23+
console.error = cerr;
2024
});
2125

2226
it('contains setEntities() & allows chaining', () => {
23-
expect(this.baseDb.setEntities([])).toEqual(this.baseDb);
27+
expect(baseDb.setEntities([])).toEqual(baseDb);
2428
});
2529

2630
it('contains connect() & returns a promise', () => {
27-
expect(this.baseDb.connect({})).toEqual(jasmine.any(Promise));
31+
expect(baseDb.connect({})).toEqual(jasmine.any(Promise));
2832
});
2933

3034
it('can log a message', () => {
@@ -34,7 +38,7 @@ describe('[BaseDb]', () => {
3438
result = true;
3539
};
3640

37-
this.baseDb.logger();
41+
baseDb.logger();
3842
expect(result).toBe(true);
3943
});
4044
});

test/spec/api/database/postgres.spec.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,31 @@ const ROOT_PATH = require('app-root-path').toString();
88
* pointyapi/database
99
*/
1010
describe('[Database: Postgres]', async () => {
11+
let db;
12+
let clog;
13+
1114
beforeAll(() => {
12-
this.db = new PointyPostgres();
13-
this.db.connectionName = 'testconn';
14-
this.db.errorHandler = (error) => fail(error);
15-
this.db.logger = () => {};
15+
db = new PointyPostgres();
16+
db.connectionName = 'testconn';
17+
db.errorHandler = (error) => fail(error);
18+
db.logger = () => {};
1619
});
1720

1821
beforeEach(() => {
19-
this.clog = console.log;
22+
clog = console.log;
2023
});
2124

2225
afterEach(() => {
23-
console.log = this.clog;
26+
console.log = clog;
2427
});
2528

2629
it('can set entities', () => {
27-
this.db.setEntities([ ExampleUser ]);
30+
db.setEntities([ ExampleUser ]);
2831
});
2932

3033
it('can connect', async () => {
3134
// Database
32-
await this.db.connect(ROOT_PATH).catch((error) => fail(error));
35+
await db.connect(ROOT_PATH).catch((error) => fail(error));
3336
});
3437

3538
it('can connect with json options', async () => {

0 commit comments

Comments
 (0)