@furystack/rest-service 8.0.0 → 9.0.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.
- package/esm/actions/login.d.ts +0 -1
- package/esm/actions/login.d.ts.map +1 -1
- package/esm/actions/login.js +0 -1
- package/esm/actions/login.js.map +1 -1
- package/esm/add-cors-header.d.ts +14 -0
- package/esm/add-cors-header.d.ts.map +1 -0
- package/esm/add-cors-header.js +23 -0
- package/esm/add-cors-header.js.map +1 -0
- package/esm/add-cors-header.spec.js +7 -8
- package/esm/add-cors-header.spec.js.map +1 -1
- package/esm/api-manager.d.ts.map +1 -1
- package/esm/api-manager.js +4 -5
- package/esm/api-manager.js.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.d.ts +0 -1
- package/esm/endpoint-generators/create-delete-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.js +0 -1
- package/esm/endpoint-generators/create-delete-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.spec.js +4 -2
- package/esm/endpoint-generators/create-delete-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.d.ts +0 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.js +0 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.js +16 -10
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.d.ts +0 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.js +0 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.js +10 -6
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.d.ts +0 -1
- package/esm/endpoint-generators/create-patch-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.js +2 -2
- package/esm/endpoint-generators/create-patch-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.spec.js +4 -2
- package/esm/endpoint-generators/create-patch-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.d.ts +0 -1
- package/esm/endpoint-generators/create-post-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.js +2 -2
- package/esm/endpoint-generators/create-post-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.spec.js +4 -2
- package/esm/endpoint-generators/create-post-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/utils.d.ts.map +1 -1
- package/esm/endpoint-generators/utils.js +0 -2
- package/esm/endpoint-generators/utils.js.map +1 -1
- package/esm/http-user-context.d.ts.map +1 -1
- package/esm/http-user-context.js +0 -3
- package/esm/http-user-context.js.map +1 -1
- package/esm/http-user-context.spec.js +1 -2
- package/esm/http-user-context.spec.js.map +1 -1
- package/esm/incoming-message-extensions.d.ts +0 -2
- package/esm/incoming-message-extensions.d.ts.map +1 -1
- package/esm/incoming-message-extensions.js +1 -10
- package/esm/incoming-message-extensions.js.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +2 -2
- package/esm/index.js.map +1 -1
- package/esm/models/default-session.d.ts.map +1 -1
- package/esm/models/default-session.js +0 -8
- package/esm/models/default-session.js.map +1 -1
- package/esm/read-post-body.d.ts +12 -0
- package/esm/read-post-body.d.ts.map +1 -0
- package/esm/read-post-body.js +33 -0
- package/esm/read-post-body.js.map +1 -0
- package/esm/rest-service.integration.spec.js +23 -30
- package/esm/rest-service.integration.spec.js.map +1 -1
- package/esm/rest.integration.test.js +1 -1
- package/esm/static-server-manager.d.ts.map +1 -1
- package/esm/static-server-manager.js +0 -1
- package/esm/static-server-manager.js.map +1 -1
- package/esm/static-server-manager.spec.js +2 -15
- package/esm/static-server-manager.spec.js.map +1 -1
- package/esm/validate.integration.spec.js +2 -2
- package/esm/validate.integration.spec.js.map +1 -1
- package/package.json +10 -10
- package/src/actions/login.ts +0 -1
- package/src/add-cors-header.spec.ts +7 -8
- package/src/add-cors-header.ts +32 -0
- package/src/api-manager.ts +5 -5
- package/src/endpoint-generators/create-delete-endpoint.spec.ts +4 -2
- package/src/endpoint-generators/create-delete-endpoint.ts +0 -1
- package/src/endpoint-generators/create-get-collection-endpoint.spec.ts +16 -10
- package/src/endpoint-generators/create-get-collection-endpoint.ts +0 -1
- package/src/endpoint-generators/create-get-entity-endpoint.spec.ts +10 -6
- package/src/endpoint-generators/create-get-entity-endpoint.ts +0 -1
- package/src/endpoint-generators/create-patch-endpoint.spec.ts +4 -2
- package/src/endpoint-generators/create-patch-endpoint.ts +2 -2
- package/src/endpoint-generators/create-post-endpoint.spec.ts +4 -2
- package/src/endpoint-generators/create-post-endpoint.ts +2 -2
- package/src/endpoint-generators/utils.ts +2 -2
- package/src/http-user-context.spec.ts +1 -2
- package/src/http-user-context.ts +3 -3
- package/src/incoming-message-extensions.ts +0 -15
- package/src/index.ts +2 -2
- package/src/models/default-session.ts +2 -2
- package/src/read-post-body.ts +36 -0
- package/src/rest-service.integration.spec.ts +24 -34
- package/src/rest.integration.test.ts +1 -1
- package/src/static-server-manager.spec.ts +2 -18
- package/src/static-server-manager.ts +1 -1
- package/src/validate.integration.spec.ts +2 -2
- package/esm/incoming-message-extensions.spec.d.ts +0 -2
- package/esm/incoming-message-extensions.spec.d.ts.map +0 -1
- package/esm/incoming-message-extensions.spec.js +0 -38
- package/esm/incoming-message-extensions.spec.js.map +0 -1
- package/esm/utils.d.ts +0 -25
- package/esm/utils.d.ts.map +0 -1
- package/esm/utils.js +0 -70
- package/esm/utils.js.map +0 -1
- package/src/incoming-message-extensions.spec.ts +0 -42
- package/src/utils.ts +0 -68
|
@@ -14,7 +14,7 @@ import { getPort } from '@furystack/core/port-generator'
|
|
|
14
14
|
const createValidateApi = async () => {
|
|
15
15
|
const injector = new Injector()
|
|
16
16
|
const port = getPort()
|
|
17
|
-
useRestService<ValidationApi>({
|
|
17
|
+
await useRestService<ValidationApi>({
|
|
18
18
|
injector,
|
|
19
19
|
api: {
|
|
20
20
|
GET: {
|
|
@@ -54,7 +54,7 @@ const createValidateApi = async () => {
|
|
|
54
54
|
root: '/api',
|
|
55
55
|
})
|
|
56
56
|
const client = createClient<ValidationApi>({
|
|
57
|
-
endpointUrl: `http://
|
|
57
|
+
endpointUrl: `http://127.0.0.1:${port}/api`,
|
|
58
58
|
})
|
|
59
59
|
return {
|
|
60
60
|
dispose: injector.dispose.bind(injector),
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"incoming-message-extensions.spec.d.ts","sourceRoot":"","sources":["../src/incoming-message-extensions.spec.ts"],"names":[],"mappings":"AACA,OAAO,+BAA+B,CAAA"}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage } from 'http';
|
|
2
|
-
import './incoming-message-extensions';
|
|
3
|
-
import { Socket } from 'net';
|
|
4
|
-
import { describe, it, expect } from 'vitest';
|
|
5
|
-
describe('IncomingMessage extensions', () => {
|
|
6
|
-
describe('readPostBody', () => {
|
|
7
|
-
it('Should be extended', () => {
|
|
8
|
-
const socket = new Socket();
|
|
9
|
-
const msg = new IncomingMessage(socket);
|
|
10
|
-
expect(typeof msg.readPostBody).toBe('function');
|
|
11
|
-
});
|
|
12
|
-
it('Should read the raw post body', async () => {
|
|
13
|
-
const exampleValue = { value: Math.random().toString() };
|
|
14
|
-
const socket = new Socket();
|
|
15
|
-
const msg = new IncomingMessage(socket);
|
|
16
|
-
setTimeout(() => {
|
|
17
|
-
msg.read = () => JSON.stringify(exampleValue);
|
|
18
|
-
msg.emit('readable');
|
|
19
|
-
msg.emit('end');
|
|
20
|
-
}, 10);
|
|
21
|
-
const result = await msg.readPostBodyRaw();
|
|
22
|
-
expect(result).toEqual(JSON.stringify(exampleValue));
|
|
23
|
-
});
|
|
24
|
-
it('Should read the post body', async () => {
|
|
25
|
-
const exampleValue = { value: Math.random().toString() };
|
|
26
|
-
const socket = new Socket();
|
|
27
|
-
const msg = new IncomingMessage(socket);
|
|
28
|
-
setTimeout(() => {
|
|
29
|
-
msg.read = () => JSON.stringify(exampleValue);
|
|
30
|
-
msg.emit('readable');
|
|
31
|
-
msg.emit('end');
|
|
32
|
-
}, 10);
|
|
33
|
-
const result = await msg.readPostBody();
|
|
34
|
-
expect(result).toEqual(exampleValue);
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
//# sourceMappingURL=incoming-message-extensions.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"incoming-message-extensions.spec.js","sourceRoot":"","sources":["../src/incoming-message-extensions.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAA;AACtC,OAAO,+BAA+B,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,CAAA;AAC5B,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE7C,QAAQ,CAAC,4BAA4B,EAAE,GAAG,EAAE;IAC1C,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;QAC5B,EAAE,CAAC,oBAAoB,EAAE,GAAG,EAAE;YAC5B,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAA;YAC3B,MAAM,GAAG,GAAG,IAAI,eAAe,CAAC,MAAM,CAAC,CAAA;YACvC,MAAM,CAAC,OAAO,GAAG,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QAClD,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,+BAA+B,EAAE,KAAK,IAAI,EAAE;YAC7C,MAAM,YAAY,GAAG,EAAE,KAAK,EAAE,IA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C,CAAA;QACtC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
package/esm/utils.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/// <reference path="server-response-extensions.d.ts" />
|
|
2
|
-
/// <reference path="incoming-message-extensions.d.ts" />
|
|
3
|
-
/// <reference types="node/http.js" />
|
|
4
|
-
import type { IncomingMessage, ServerResponse } from 'http';
|
|
5
|
-
import type { CorsOptions } from './models/cors-options.js';
|
|
6
|
-
/**
|
|
7
|
-
* A collection of various HTTP API related utilities
|
|
8
|
-
*/
|
|
9
|
-
export declare class Utils {
|
|
10
|
-
readPostBodyRaw(incomingMessage: IncomingMessage): Promise<string>;
|
|
11
|
-
/**
|
|
12
|
-
* Reads the post's body and returns a promise with a parsed value
|
|
13
|
-
* @param incomingMessage The incoming message instance
|
|
14
|
-
* @returns the parsed object from the post body
|
|
15
|
-
*/
|
|
16
|
-
readPostBody<T>(incomingMessage: IncomingMessage): Promise<T>;
|
|
17
|
-
/**
|
|
18
|
-
* Adds the specified CORS headers to the response
|
|
19
|
-
* @param options The CORS Options object
|
|
20
|
-
* @param incomingMessage The incoming message instance
|
|
21
|
-
* @param serverResponse The outgoing response instance
|
|
22
|
-
*/
|
|
23
|
-
addCorsHeaders(options: CorsOptions, incomingMessage: IncomingMessage, serverResponse: ServerResponse): void;
|
|
24
|
-
}
|
|
25
|
-
//# sourceMappingURL=utils.d.ts.map
|
package/esm/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAE3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAE3D;;GAEG;AACH,qBACa,KAAK;IACH,eAAe,CAAC,eAAe,EAAE,eAAe;IAuB7D;;;;OAIG;IACU,YAAY,CAAC,CAAC,EAAE,eAAe,EAAE,eAAe,GAAG,OAAO,CAAC,CAAC,CAAC;IAM1E;;;;;OAKG;IACI,cAAc,CAAC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,eAAe,EAAE,cAAc,EAAE,cAAc;CAkB7G"}
|
package/esm/utils.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
import { Injectable } from '@furystack/inject';
|
|
8
|
-
/**
|
|
9
|
-
* A collection of various HTTP API related utilities
|
|
10
|
-
*/
|
|
11
|
-
let Utils = class Utils {
|
|
12
|
-
async readPostBodyRaw(incomingMessage) {
|
|
13
|
-
if (!incomingMessage.readable) {
|
|
14
|
-
throw Error('Incoming message is not readable');
|
|
15
|
-
}
|
|
16
|
-
let body = '';
|
|
17
|
-
await new Promise((resolve, reject) => {
|
|
18
|
-
incomingMessage.on('readable', () => {
|
|
19
|
-
const data = incomingMessage.read();
|
|
20
|
-
if (data) {
|
|
21
|
-
body += data;
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
incomingMessage.on('end', () => {
|
|
25
|
-
resolve();
|
|
26
|
-
});
|
|
27
|
-
incomingMessage.on('error', (err) => {
|
|
28
|
-
reject(err);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
return body;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Reads the post's body and returns a promise with a parsed value
|
|
35
|
-
* @param incomingMessage The incoming message instance
|
|
36
|
-
* @returns the parsed object from the post body
|
|
37
|
-
*/
|
|
38
|
-
async readPostBody(incomingMessage) {
|
|
39
|
-
const body = incomingMessage.postBody || JSON.parse(await this.readPostBodyRaw(incomingMessage));
|
|
40
|
-
incomingMessage.postBody = body;
|
|
41
|
-
return body;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Adds the specified CORS headers to the response
|
|
45
|
-
* @param options The CORS Options object
|
|
46
|
-
* @param incomingMessage The incoming message instance
|
|
47
|
-
* @param serverResponse The outgoing response instance
|
|
48
|
-
*/
|
|
49
|
-
addCorsHeaders(options, incomingMessage, serverResponse) {
|
|
50
|
-
if (incomingMessage.headers &&
|
|
51
|
-
incomingMessage.headers.origin !== incomingMessage.headers.host &&
|
|
52
|
-
options.origins.some((origin) => origin === incomingMessage.headers.origin)) {
|
|
53
|
-
serverResponse.setHeader('Access-Control-Allow-Origin', incomingMessage.headers.origin);
|
|
54
|
-
if (options.credentials) {
|
|
55
|
-
serverResponse.setHeader('Access-Control-Allow-Credentials', 'true');
|
|
56
|
-
}
|
|
57
|
-
if (options.headers && options.headers.length) {
|
|
58
|
-
serverResponse.setHeader('Access-Control-Allow-Headers', options.headers.join(', '));
|
|
59
|
-
}
|
|
60
|
-
if (options.methods && options.methods.length) {
|
|
61
|
-
serverResponse.setHeader('Access-Control-Allow-Methods', options.methods.join(', '));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
Utils = __decorate([
|
|
67
|
-
Injectable({ lifetime: 'transient' })
|
|
68
|
-
], Utils);
|
|
69
|
-
export { Utils };
|
|
70
|
-
//# sourceMappingURL=utils.js.map
|
package/esm/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EA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OAAO,CAAC,IAAI;YAC/D,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,EAC3E,CAAC;YACD,cAAc,CAAC,SAAS,CAAC,6BAA6B,EAAE,eAAe,CAAC,OAAO,CAAC,MAAgB,CAAC,CAAA;YACjG,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;gBACxB,cAAc,CAAC,SAAS,CAAC,kCAAkC,EAAE,MAAM,CAAC,CAAA;YACtE,CAAC;YACD,IAAI,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;gBAC9C,cAAc,CAAC,SAAS,CAAC,8BAA8B,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;YACtF,CAAC;YACD,IAAI,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;gBAC9C,cAAc,CAAC,SAAS,CAAC,8BAA8B,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;YACtF,CAAC;QACH,CAAC;IACH,CAAC;CACF,CAAA;AA3DY,KAAK;IADjB,UAAU,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;GACzB,KAAK,CA2DjB"}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage } from 'http'
|
|
2
|
-
import './incoming-message-extensions'
|
|
3
|
-
import { Socket } from 'net'
|
|
4
|
-
import { describe, it, expect } from 'vitest'
|
|
5
|
-
|
|
6
|
-
describe('IncomingMessage extensions', () => {
|
|
7
|
-
describe('readPostBody', () => {
|
|
8
|
-
it('Should be extended', () => {
|
|
9
|
-
const socket = new Socket()
|
|
10
|
-
const msg = new IncomingMessage(socket)
|
|
11
|
-
expect(typeof msg.readPostBody).toBe('function')
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
it('Should read the raw post body', async () => {
|
|
15
|
-
const exampleValue = { value: Math.random().toString() }
|
|
16
|
-
const socket = new Socket()
|
|
17
|
-
const msg = new IncomingMessage(socket)
|
|
18
|
-
setTimeout(() => {
|
|
19
|
-
msg.read = () => JSON.stringify(exampleValue)
|
|
20
|
-
msg.emit('readable')
|
|
21
|
-
msg.emit('end')
|
|
22
|
-
}, 10)
|
|
23
|
-
|
|
24
|
-
const result = await msg.readPostBodyRaw()
|
|
25
|
-
expect(result).toEqual(JSON.stringify(exampleValue))
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('Should read the post body', async () => {
|
|
29
|
-
const exampleValue = { value: Math.random().toString() }
|
|
30
|
-
const socket = new Socket()
|
|
31
|
-
const msg = new IncomingMessage(socket)
|
|
32
|
-
setTimeout(() => {
|
|
33
|
-
msg.read = () => JSON.stringify(exampleValue)
|
|
34
|
-
msg.emit('readable')
|
|
35
|
-
msg.emit('end')
|
|
36
|
-
}, 10)
|
|
37
|
-
|
|
38
|
-
const result = await msg.readPostBody()
|
|
39
|
-
expect(result).toEqual(exampleValue)
|
|
40
|
-
})
|
|
41
|
-
})
|
|
42
|
-
})
|
package/src/utils.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from 'http'
|
|
2
|
-
import { Injectable } from '@furystack/inject'
|
|
3
|
-
import type { CorsOptions } from './models/cors-options.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A collection of various HTTP API related utilities
|
|
7
|
-
*/
|
|
8
|
-
@Injectable({ lifetime: 'transient' })
|
|
9
|
-
export class Utils {
|
|
10
|
-
public async readPostBodyRaw(incomingMessage: IncomingMessage) {
|
|
11
|
-
if (!incomingMessage.readable) {
|
|
12
|
-
throw Error('Incoming message is not readable')
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
let body = ''
|
|
16
|
-
await new Promise<void>((resolve, reject) => {
|
|
17
|
-
incomingMessage.on('readable', () => {
|
|
18
|
-
const data = incomingMessage.read()
|
|
19
|
-
if (data) {
|
|
20
|
-
body += data
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
-
incomingMessage.on('end', () => {
|
|
24
|
-
resolve()
|
|
25
|
-
})
|
|
26
|
-
incomingMessage.on('error', (err) => {
|
|
27
|
-
reject(err)
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
return body
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Reads the post's body and returns a promise with a parsed value
|
|
35
|
-
* @param incomingMessage The incoming message instance
|
|
36
|
-
* @returns the parsed object from the post body
|
|
37
|
-
*/
|
|
38
|
-
public async readPostBody<T>(incomingMessage: IncomingMessage): Promise<T> {
|
|
39
|
-
const body = incomingMessage.postBody || JSON.parse(await this.readPostBodyRaw(incomingMessage))
|
|
40
|
-
incomingMessage.postBody = body
|
|
41
|
-
return body
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Adds the specified CORS headers to the response
|
|
46
|
-
* @param options The CORS Options object
|
|
47
|
-
* @param incomingMessage The incoming message instance
|
|
48
|
-
* @param serverResponse The outgoing response instance
|
|
49
|
-
*/
|
|
50
|
-
public addCorsHeaders(options: CorsOptions, incomingMessage: IncomingMessage, serverResponse: ServerResponse) {
|
|
51
|
-
if (
|
|
52
|
-
incomingMessage.headers &&
|
|
53
|
-
incomingMessage.headers.origin !== incomingMessage.headers.host &&
|
|
54
|
-
options.origins.some((origin) => origin === incomingMessage.headers.origin)
|
|
55
|
-
) {
|
|
56
|
-
serverResponse.setHeader('Access-Control-Allow-Origin', incomingMessage.headers.origin as string)
|
|
57
|
-
if (options.credentials) {
|
|
58
|
-
serverResponse.setHeader('Access-Control-Allow-Credentials', 'true')
|
|
59
|
-
}
|
|
60
|
-
if (options.headers && options.headers.length) {
|
|
61
|
-
serverResponse.setHeader('Access-Control-Allow-Headers', options.headers.join(', '))
|
|
62
|
-
}
|
|
63
|
-
if (options.methods && options.methods.length) {
|
|
64
|
-
serverResponse.setHeader('Access-Control-Allow-Methods', options.methods.join(', '))
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|