@unito/integration-sdk 1.0.27 → 1.0.28
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/dist/src/helpers.d.ts +2 -2
- package/dist/src/helpers.js +2 -2
- package/dist/src/index.cjs +208 -208
- package/dist/src/integration.js +8 -8
- package/dist/src/middlewares/correlationId.d.ts +2 -2
- package/dist/src/middlewares/correlationId.js +3 -3
- package/dist/src/middlewares/credentials.d.ts +2 -2
- package/dist/src/middlewares/credentials.js +3 -3
- package/dist/src/middlewares/errors.d.ts +2 -2
- package/dist/src/middlewares/errors.js +3 -3
- package/dist/src/middlewares/filters.d.ts +2 -2
- package/dist/src/middlewares/filters.js +4 -4
- package/dist/src/middlewares/finish.d.ts +2 -2
- package/dist/src/middlewares/finish.js +3 -3
- package/dist/src/middlewares/logger.d.ts +2 -2
- package/dist/src/middlewares/logger.js +3 -3
- package/dist/src/middlewares/notFound.d.ts +2 -2
- package/dist/src/middlewares/notFound.js +3 -3
- package/dist/src/middlewares/secrets.d.ts +2 -2
- package/dist/src/middlewares/secrets.js +3 -3
- package/dist/src/middlewares/selects.d.ts +2 -2
- package/dist/src/middlewares/selects.js +3 -3
- package/dist/src/middlewares/signal.d.ts +2 -2
- package/dist/src/middlewares/signal.js +3 -3
- package/dist/src/middlewares/{requestStartTime.d.ts → start.d.ts} +2 -2
- package/dist/src/middlewares/{requestStartTime.js → start.js} +3 -3
- package/dist/test/middlewares/correlationId.test.js +3 -3
- package/dist/test/middlewares/credentials.test.js +4 -4
- package/dist/test/middlewares/errors.test.js +4 -4
- package/dist/test/middlewares/filters.test.js +20 -12
- package/dist/test/middlewares/finish.test.js +4 -4
- package/dist/test/middlewares/logger.test.js +5 -5
- package/dist/test/middlewares/notFound.test.js +2 -2
- package/dist/test/middlewares/secrets.test.js +3 -3
- package/dist/test/middlewares/selects.test.js +3 -3
- package/dist/test/middlewares/signal.test.js +3 -3
- package/dist/test/middlewares/start.test.d.ts +1 -0
- package/dist/test/middlewares/start.test.js +11 -0
- package/package.json +1 -1
- package/src/helpers.ts +2 -2
- package/src/integration.ts +8 -8
- package/src/middlewares/correlationId.ts +3 -3
- package/src/middlewares/credentials.ts +3 -3
- package/src/middlewares/errors.ts +3 -3
- package/src/middlewares/filters.ts +4 -4
- package/src/middlewares/finish.ts +3 -3
- package/src/middlewares/logger.ts +3 -3
- package/src/middlewares/notFound.ts +3 -3
- package/src/middlewares/secrets.ts +3 -3
- package/src/middlewares/selects.ts +3 -3
- package/src/middlewares/signal.ts +3 -3
- package/src/middlewares/{requestStartTime.ts → start.ts} +3 -3
- package/test/middlewares/correlationId.test.ts +3 -3
- package/test/middlewares/credentials.test.ts +4 -4
- package/test/middlewares/errors.test.ts +4 -4
- package/test/middlewares/filters.test.ts +31 -12
- package/test/middlewares/finish.test.ts +4 -4
- package/test/middlewares/logger.test.ts +5 -5
- package/test/middlewares/notFound.test.ts +2 -2
- package/test/middlewares/secrets.test.ts +3 -3
- package/test/middlewares/selects.test.ts +3 -3
- package/test/middlewares/signal.test.ts +3 -3
- package/test/middlewares/start.test.ts +14 -0
package/dist/src/helpers.d.ts
CHANGED
|
@@ -10,6 +10,6 @@ import { Filter } from './index.js';
|
|
|
10
10
|
* @param fields The schema of the item against which the filters are applied
|
|
11
11
|
* @returns The validated filters
|
|
12
12
|
*/
|
|
13
|
-
export declare
|
|
13
|
+
export declare function getApplicableFilters(context: {
|
|
14
14
|
filters: Filter[];
|
|
15
|
-
}, fields: FieldSchema[])
|
|
15
|
+
}, fields: FieldSchema[]): Filter[];
|
package/dist/src/helpers.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* @param fields The schema of the item against which the filters are applied
|
|
9
9
|
* @returns The validated filters
|
|
10
10
|
*/
|
|
11
|
-
export
|
|
11
|
+
export function getApplicableFilters(context, fields) {
|
|
12
12
|
const applicableFilters = [];
|
|
13
13
|
for (const filter of context.filters) {
|
|
14
14
|
let field = undefined;
|
|
@@ -25,4 +25,4 @@ export const getApplicableFilters = (context, fields) => {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
return applicableFilters;
|
|
28
|
-
}
|
|
28
|
+
}
|
package/dist/src/index.cjs
CHANGED
|
@@ -421,201 +421,6 @@ function buildHttpError(responseStatus, message) {
|
|
|
421
421
|
return httpError;
|
|
422
422
|
}
|
|
423
423
|
|
|
424
|
-
const middleware$a = (req, res, next) => {
|
|
425
|
-
res.locals.correlationId = req.header('X-Unito-Correlation-Id') ?? crypto.randomUUID();
|
|
426
|
-
next();
|
|
427
|
-
};
|
|
428
|
-
|
|
429
|
-
const ADDITIONAL_CONTEXT_HEADER = 'X-Unito-Additional-Logging-Context';
|
|
430
|
-
const middleware$9 = (req, res, next) => {
|
|
431
|
-
const logger = new Logger({ correlation_id: res.locals.correlationId });
|
|
432
|
-
res.locals.logger = logger;
|
|
433
|
-
const rawAdditionalContext = req.header(ADDITIONAL_CONTEXT_HEADER);
|
|
434
|
-
if (typeof rawAdditionalContext === 'string') {
|
|
435
|
-
try {
|
|
436
|
-
const additionalContext = JSON.parse(rawAdditionalContext);
|
|
437
|
-
logger.decorate(additionalContext);
|
|
438
|
-
}
|
|
439
|
-
catch (error) {
|
|
440
|
-
logger.warn(`Failed parsing header ${ADDITIONAL_CONTEXT_HEADER}: ${rawAdditionalContext}`);
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
next();
|
|
444
|
-
};
|
|
445
|
-
|
|
446
|
-
const CREDENTIALS_HEADER = 'X-Unito-Credentials';
|
|
447
|
-
const middleware$8 = (req, res, next) => {
|
|
448
|
-
const credentialsHeader = req.header(CREDENTIALS_HEADER);
|
|
449
|
-
if (credentialsHeader) {
|
|
450
|
-
let credentials;
|
|
451
|
-
try {
|
|
452
|
-
credentials = JSON.parse(Buffer.from(credentialsHeader, 'base64').toString('utf8'));
|
|
453
|
-
}
|
|
454
|
-
catch {
|
|
455
|
-
throw new BadRequestError(`Malformed HTTP header ${CREDENTIALS_HEADER}`);
|
|
456
|
-
}
|
|
457
|
-
res.locals.credentials = credentials;
|
|
458
|
-
}
|
|
459
|
-
next();
|
|
460
|
-
};
|
|
461
|
-
|
|
462
|
-
const OPERATION_DEADLINE_HEADER = 'X-Unito-Operation-Deadline';
|
|
463
|
-
const middleware$7 = (req, res, next) => {
|
|
464
|
-
const operationDeadlineHeader = Number(req.header(OPERATION_DEADLINE_HEADER));
|
|
465
|
-
if (operationDeadlineHeader) {
|
|
466
|
-
// `operationDeadlineHeader` represents a timestamp in the future, in seconds.
|
|
467
|
-
// We need to convert it to a number of milliseconds.
|
|
468
|
-
const deadline = operationDeadlineHeader * 1000 - Date.now();
|
|
469
|
-
if (deadline > 0) {
|
|
470
|
-
res.locals.signal = AbortSignal.timeout(deadline);
|
|
471
|
-
}
|
|
472
|
-
else {
|
|
473
|
-
throw new TimeoutError('Request already timed out upon reception');
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
else {
|
|
477
|
-
// Default to 20s, which is the maximum time frame allowed for an operation by Unito.
|
|
478
|
-
res.locals.signal = AbortSignal.timeout(20000);
|
|
479
|
-
}
|
|
480
|
-
next();
|
|
481
|
-
};
|
|
482
|
-
|
|
483
|
-
const SECRETS_HEADER = 'X-Unito-Secrets';
|
|
484
|
-
const middleware$6 = (req, res, next) => {
|
|
485
|
-
const secretsHeader = req.header(SECRETS_HEADER);
|
|
486
|
-
if (secretsHeader) {
|
|
487
|
-
let secrets;
|
|
488
|
-
try {
|
|
489
|
-
secrets = JSON.parse(Buffer.from(secretsHeader, 'base64').toString('utf8'));
|
|
490
|
-
}
|
|
491
|
-
catch {
|
|
492
|
-
throw new BadRequestError(`Malformed HTTP header ${SECRETS_HEADER}`);
|
|
493
|
-
}
|
|
494
|
-
res.locals.secrets = secrets;
|
|
495
|
-
}
|
|
496
|
-
next();
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
// The operators are ordered by their symbol length, in descending order.
|
|
500
|
-
// This is necessary because the symbol of an operator can be
|
|
501
|
-
// a subset of the symbol of another operator.
|
|
502
|
-
//
|
|
503
|
-
// For example, the symbol "=" (EQUAL) is a subset of the symbol "!=" (NOT_EQUAL).
|
|
504
|
-
const ORDERED_OPERATORS = Object.values(integrationApi.OperatorType).sort((o1, o2) => o1.length - o2.length);
|
|
505
|
-
const middleware$5 = (req, res, next) => {
|
|
506
|
-
const rawFilters = req.query.filter;
|
|
507
|
-
res.locals.filters = [];
|
|
508
|
-
if (typeof rawFilters === 'string') {
|
|
509
|
-
for (const rawFilter of rawFilters.split(',')) {
|
|
510
|
-
for (const operator of ORDERED_OPERATORS) {
|
|
511
|
-
if (rawFilter.includes(operator)) {
|
|
512
|
-
const [field, valuesRaw] = rawFilter.split(operator, 2);
|
|
513
|
-
const values = valuesRaw ? valuesRaw.split('|').map(decodeURIComponent) : [];
|
|
514
|
-
res.locals.filters.push({ field: field, operator, values });
|
|
515
|
-
break;
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
next();
|
|
521
|
-
};
|
|
522
|
-
|
|
523
|
-
const middleware$4 = (req, res, next) => {
|
|
524
|
-
const rawSelect = req.query.select;
|
|
525
|
-
if (typeof rawSelect === 'string') {
|
|
526
|
-
res.locals.selects = rawSelect.split(',');
|
|
527
|
-
}
|
|
528
|
-
else {
|
|
529
|
-
res.locals.selects = [];
|
|
530
|
-
}
|
|
531
|
-
next();
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
const middleware$3 = (err, _req, res, next) => {
|
|
535
|
-
if (res.headersSent) {
|
|
536
|
-
return next(err);
|
|
537
|
-
}
|
|
538
|
-
let error;
|
|
539
|
-
if (err instanceof HttpError) {
|
|
540
|
-
error = {
|
|
541
|
-
code: err.status.toString(),
|
|
542
|
-
message: err.message,
|
|
543
|
-
details: {
|
|
544
|
-
stack: err.stack,
|
|
545
|
-
},
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
else {
|
|
549
|
-
error = {
|
|
550
|
-
code: '500',
|
|
551
|
-
message: 'Oops! Something went wrong',
|
|
552
|
-
originalError: {
|
|
553
|
-
code: err.name,
|
|
554
|
-
message: err.message,
|
|
555
|
-
details: {
|
|
556
|
-
stack: err.stack,
|
|
557
|
-
},
|
|
558
|
-
},
|
|
559
|
-
};
|
|
560
|
-
}
|
|
561
|
-
res.locals.error = structuredClone(error);
|
|
562
|
-
// Keep the stack details in development for the Debugger
|
|
563
|
-
if (process.env.NODE_ENV !== 'development') {
|
|
564
|
-
delete error.details;
|
|
565
|
-
delete error.originalError?.details;
|
|
566
|
-
}
|
|
567
|
-
res.status(Number(error.code)).json(error);
|
|
568
|
-
};
|
|
569
|
-
|
|
570
|
-
const middleware$2 = (req, res, next) => {
|
|
571
|
-
if (req.originalUrl !== '/health') {
|
|
572
|
-
res.on('finish', function () {
|
|
573
|
-
const error = res.locals.error;
|
|
574
|
-
const durationInNs = Number(process.hrtime.bigint() - res.locals.requestStartTime);
|
|
575
|
-
const durationInMs = (durationInNs / 1_000_000) | 0;
|
|
576
|
-
const message = `${req.method} ${req.originalUrl} ${res.statusCode} - ${durationInMs} ms`;
|
|
577
|
-
const metadata = {
|
|
578
|
-
duration: durationInNs,
|
|
579
|
-
// Use reserved and standard attributes of Datadog
|
|
580
|
-
// https://app.datadoghq.com/logs/pipelines/standard-attributes
|
|
581
|
-
http: { method: req.method, status_code: res.statusCode, url_details: { path: req.originalUrl } },
|
|
582
|
-
...(error
|
|
583
|
-
? {
|
|
584
|
-
error: {
|
|
585
|
-
kind: error.message,
|
|
586
|
-
stack: (error.originalError?.details?.stack ?? error.details?.stack),
|
|
587
|
-
message: error.originalError?.message ?? error.message,
|
|
588
|
-
},
|
|
589
|
-
}
|
|
590
|
-
: {}),
|
|
591
|
-
};
|
|
592
|
-
if ([404, 429].includes(res.statusCode)) {
|
|
593
|
-
res.locals.logger.warn(message, metadata);
|
|
594
|
-
}
|
|
595
|
-
else if (res.statusCode >= 400) {
|
|
596
|
-
res.locals.logger.error(message, metadata);
|
|
597
|
-
}
|
|
598
|
-
else {
|
|
599
|
-
res.locals.logger.info(message, metadata);
|
|
600
|
-
}
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
next();
|
|
604
|
-
};
|
|
605
|
-
|
|
606
|
-
const middleware$1 = (req, res, _next) => {
|
|
607
|
-
const error = {
|
|
608
|
-
code: '404',
|
|
609
|
-
message: `Path ${req.path} not found.`,
|
|
610
|
-
};
|
|
611
|
-
res.status(404).json(error);
|
|
612
|
-
};
|
|
613
|
-
|
|
614
|
-
const middleware = (_, res, next) => {
|
|
615
|
-
res.locals.requestStartTime = process.hrtime.bigint();
|
|
616
|
-
next();
|
|
617
|
-
};
|
|
618
|
-
|
|
619
424
|
function assertValidPath(path) {
|
|
620
425
|
if (!path.startsWith('/')) {
|
|
621
426
|
throw new InvalidHandler(`The provided path '${path}' is invalid. All paths must start with a '/'.`);
|
|
@@ -948,6 +753,201 @@ class Handler {
|
|
|
948
753
|
}
|
|
949
754
|
}
|
|
950
755
|
|
|
756
|
+
function extractCorrelationId(req, res, next) {
|
|
757
|
+
res.locals.correlationId = req.header('X-Unito-Correlation-Id') ?? crypto.randomUUID();
|
|
758
|
+
next();
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
const CREDENTIALS_HEADER = 'X-Unito-Credentials';
|
|
762
|
+
function extractCredentials(req, res, next) {
|
|
763
|
+
const credentialsHeader = req.header(CREDENTIALS_HEADER);
|
|
764
|
+
if (credentialsHeader) {
|
|
765
|
+
let credentials;
|
|
766
|
+
try {
|
|
767
|
+
credentials = JSON.parse(Buffer.from(credentialsHeader, 'base64').toString('utf8'));
|
|
768
|
+
}
|
|
769
|
+
catch {
|
|
770
|
+
throw new BadRequestError(`Malformed HTTP header ${CREDENTIALS_HEADER}`);
|
|
771
|
+
}
|
|
772
|
+
res.locals.credentials = credentials;
|
|
773
|
+
}
|
|
774
|
+
next();
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function onError(err, _req, res, next) {
|
|
778
|
+
if (res.headersSent) {
|
|
779
|
+
return next(err);
|
|
780
|
+
}
|
|
781
|
+
let error;
|
|
782
|
+
if (err instanceof HttpError) {
|
|
783
|
+
error = {
|
|
784
|
+
code: err.status.toString(),
|
|
785
|
+
message: err.message,
|
|
786
|
+
details: {
|
|
787
|
+
stack: err.stack,
|
|
788
|
+
},
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
error = {
|
|
793
|
+
code: '500',
|
|
794
|
+
message: 'Oops! Something went wrong',
|
|
795
|
+
originalError: {
|
|
796
|
+
code: err.name,
|
|
797
|
+
message: err.message,
|
|
798
|
+
details: {
|
|
799
|
+
stack: err.stack,
|
|
800
|
+
},
|
|
801
|
+
},
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
res.locals.error = structuredClone(error);
|
|
805
|
+
// Keep the stack details in development for the Debugger
|
|
806
|
+
if (process.env.NODE_ENV !== 'development') {
|
|
807
|
+
delete error.details;
|
|
808
|
+
delete error.originalError?.details;
|
|
809
|
+
}
|
|
810
|
+
res.status(Number(error.code)).json(error);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
// The operators are ordered by their symbol length, in descending order.
|
|
814
|
+
// This is necessary because the symbol of an operator can be
|
|
815
|
+
// a subset of the symbol of another operator.
|
|
816
|
+
//
|
|
817
|
+
// For example, the symbol "=" (EQUAL) is a subset of the symbol "!=" (NOT_EQUAL).
|
|
818
|
+
const ORDERED_OPERATORS = Object.values(integrationApi.OperatorType).sort((o1, o2) => o2.length - o1.length);
|
|
819
|
+
function extractFilters(req, res, next) {
|
|
820
|
+
const rawFilters = req.query.filter;
|
|
821
|
+
res.locals.filters = [];
|
|
822
|
+
if (typeof rawFilters === 'string') {
|
|
823
|
+
for (const rawFilter of rawFilters.split(',')) {
|
|
824
|
+
for (const operator of ORDERED_OPERATORS) {
|
|
825
|
+
if (rawFilter.includes(operator)) {
|
|
826
|
+
const [field, valuesRaw] = rawFilter.split(operator, 2);
|
|
827
|
+
const values = valuesRaw ? valuesRaw.split('|').map(decodeURIComponent) : [];
|
|
828
|
+
res.locals.filters.push({ field: field, operator, values });
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
next();
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
function onFinish(req, res, next) {
|
|
838
|
+
if (req.originalUrl !== '/health') {
|
|
839
|
+
res.on('finish', function () {
|
|
840
|
+
const error = res.locals.error;
|
|
841
|
+
const durationInNs = Number(process.hrtime.bigint() - res.locals.requestStartTime);
|
|
842
|
+
const durationInMs = (durationInNs / 1_000_000) | 0;
|
|
843
|
+
const message = `${req.method} ${req.originalUrl} ${res.statusCode} - ${durationInMs} ms`;
|
|
844
|
+
const metadata = {
|
|
845
|
+
duration: durationInNs,
|
|
846
|
+
// Use reserved and standard attributes of Datadog
|
|
847
|
+
// https://app.datadoghq.com/logs/pipelines/standard-attributes
|
|
848
|
+
http: { method: req.method, status_code: res.statusCode, url_details: { path: req.originalUrl } },
|
|
849
|
+
...(error
|
|
850
|
+
? {
|
|
851
|
+
error: {
|
|
852
|
+
kind: error.message,
|
|
853
|
+
stack: (error.originalError?.details?.stack ?? error.details?.stack),
|
|
854
|
+
message: error.originalError?.message ?? error.message,
|
|
855
|
+
},
|
|
856
|
+
}
|
|
857
|
+
: {}),
|
|
858
|
+
};
|
|
859
|
+
if ([404, 429].includes(res.statusCode)) {
|
|
860
|
+
res.locals.logger.warn(message, metadata);
|
|
861
|
+
}
|
|
862
|
+
else if (res.statusCode >= 400) {
|
|
863
|
+
res.locals.logger.error(message, metadata);
|
|
864
|
+
}
|
|
865
|
+
else {
|
|
866
|
+
res.locals.logger.info(message, metadata);
|
|
867
|
+
}
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
next();
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
function notFound(req, res, _next) {
|
|
874
|
+
const error = {
|
|
875
|
+
code: '404',
|
|
876
|
+
message: `Path ${req.path} not found.`,
|
|
877
|
+
};
|
|
878
|
+
res.status(404).json(error);
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
const ADDITIONAL_CONTEXT_HEADER = 'X-Unito-Additional-Logging-Context';
|
|
882
|
+
function injectLogger(req, res, next) {
|
|
883
|
+
const logger = new Logger({ correlation_id: res.locals.correlationId });
|
|
884
|
+
res.locals.logger = logger;
|
|
885
|
+
const rawAdditionalContext = req.header(ADDITIONAL_CONTEXT_HEADER);
|
|
886
|
+
if (typeof rawAdditionalContext === 'string') {
|
|
887
|
+
try {
|
|
888
|
+
const additionalContext = JSON.parse(rawAdditionalContext);
|
|
889
|
+
logger.decorate(additionalContext);
|
|
890
|
+
}
|
|
891
|
+
catch (error) {
|
|
892
|
+
logger.warn(`Failed parsing header ${ADDITIONAL_CONTEXT_HEADER}: ${rawAdditionalContext}`);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
next();
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
function start(_, res, next) {
|
|
899
|
+
res.locals.requestStartTime = process.hrtime.bigint();
|
|
900
|
+
next();
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const SECRETS_HEADER = 'X-Unito-Secrets';
|
|
904
|
+
function extractSecrets(req, res, next) {
|
|
905
|
+
const secretsHeader = req.header(SECRETS_HEADER);
|
|
906
|
+
if (secretsHeader) {
|
|
907
|
+
let secrets;
|
|
908
|
+
try {
|
|
909
|
+
secrets = JSON.parse(Buffer.from(secretsHeader, 'base64').toString('utf8'));
|
|
910
|
+
}
|
|
911
|
+
catch {
|
|
912
|
+
throw new BadRequestError(`Malformed HTTP header ${SECRETS_HEADER}`);
|
|
913
|
+
}
|
|
914
|
+
res.locals.secrets = secrets;
|
|
915
|
+
}
|
|
916
|
+
next();
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
function extractSelects(req, res, next) {
|
|
920
|
+
const rawSelect = req.query.select;
|
|
921
|
+
if (typeof rawSelect === 'string') {
|
|
922
|
+
res.locals.selects = rawSelect.split(',');
|
|
923
|
+
}
|
|
924
|
+
else {
|
|
925
|
+
res.locals.selects = [];
|
|
926
|
+
}
|
|
927
|
+
next();
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
const OPERATION_DEADLINE_HEADER = 'X-Unito-Operation-Deadline';
|
|
931
|
+
function extractOperationDeadline(req, res, next) {
|
|
932
|
+
const operationDeadlineHeader = Number(req.header(OPERATION_DEADLINE_HEADER));
|
|
933
|
+
if (operationDeadlineHeader) {
|
|
934
|
+
// `operationDeadlineHeader` represents a timestamp in the future, in seconds.
|
|
935
|
+
// We need to convert it to a number of milliseconds.
|
|
936
|
+
const deadline = operationDeadlineHeader * 1000 - Date.now();
|
|
937
|
+
if (deadline > 0) {
|
|
938
|
+
res.locals.signal = AbortSignal.timeout(deadline);
|
|
939
|
+
}
|
|
940
|
+
else {
|
|
941
|
+
throw new TimeoutError('Request already timed out upon reception');
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
else {
|
|
945
|
+
// Default to 20s, which is the maximum time frame allowed for an operation by Unito.
|
|
946
|
+
res.locals.signal = AbortSignal.timeout(20000);
|
|
947
|
+
}
|
|
948
|
+
next();
|
|
949
|
+
}
|
|
950
|
+
|
|
951
951
|
function printErrorMessage(message) {
|
|
952
952
|
console.error();
|
|
953
953
|
console.error(`\x1b[31m Oops! Something went wrong! \x1b[0m`);
|
|
@@ -1049,11 +1049,11 @@ class Integration {
|
|
|
1049
1049
|
app.set('query parser', 'extended');
|
|
1050
1050
|
app.use(express.json());
|
|
1051
1051
|
// Must be one of the first handlers (to catch all the errors).
|
|
1052
|
-
app.use(
|
|
1052
|
+
app.use(onFinish);
|
|
1053
1053
|
// Instantiate internal middlewares.
|
|
1054
|
-
app.use(
|
|
1055
|
-
app.use(
|
|
1056
|
-
app.use(
|
|
1054
|
+
app.use(start);
|
|
1055
|
+
app.use(extractCorrelationId);
|
|
1056
|
+
app.use(injectLogger);
|
|
1057
1057
|
// Making sure we log all incoming requests (except to '/health'), prior any processing.
|
|
1058
1058
|
app.use((req, res, next) => {
|
|
1059
1059
|
if (req.originalUrl !== '/health') {
|
|
@@ -1063,11 +1063,11 @@ class Integration {
|
|
|
1063
1063
|
});
|
|
1064
1064
|
// Instantiate application middlewares. These can throw, so they have an implicit dependency on the internal
|
|
1065
1065
|
// middlewares such as the logger, the correlationId, and the error handling.
|
|
1066
|
-
app.use(
|
|
1067
|
-
app.use(
|
|
1068
|
-
app.use(
|
|
1069
|
-
app.use(
|
|
1070
|
-
app.use(
|
|
1066
|
+
app.use(extractCredentials);
|
|
1067
|
+
app.use(extractSecrets);
|
|
1068
|
+
app.use(extractFilters);
|
|
1069
|
+
app.use(extractSelects);
|
|
1070
|
+
app.use(extractOperationDeadline);
|
|
1071
1071
|
// Load handlers as needed.
|
|
1072
1072
|
if (this.handlers.length) {
|
|
1073
1073
|
for (const handler of this.handlers) {
|
|
@@ -1083,9 +1083,9 @@ class Integration {
|
|
|
1083
1083
|
process.exit(1);
|
|
1084
1084
|
}
|
|
1085
1085
|
// Must be the (last - 1) handler.
|
|
1086
|
-
app.use(
|
|
1086
|
+
app.use(onError);
|
|
1087
1087
|
// Must be the last handler.
|
|
1088
|
-
app.use(
|
|
1088
|
+
app.use(notFound);
|
|
1089
1089
|
// Start the server.
|
|
1090
1090
|
this.instance = app.listen(this.port, () => console.info(`Server started on port ${this.port}.`));
|
|
1091
1091
|
}
|
|
@@ -1410,7 +1410,7 @@ class Provider {
|
|
|
1410
1410
|
* @param fields The schema of the item against which the filters are applied
|
|
1411
1411
|
* @returns The validated filters
|
|
1412
1412
|
*/
|
|
1413
|
-
|
|
1413
|
+
function getApplicableFilters(context, fields) {
|
|
1414
1414
|
const applicableFilters = [];
|
|
1415
1415
|
for (const filter of context.filters) {
|
|
1416
1416
|
let field = undefined;
|
|
@@ -1427,7 +1427,7 @@ const getApplicableFilters = (context, fields) => {
|
|
|
1427
1427
|
}
|
|
1428
1428
|
}
|
|
1429
1429
|
return applicableFilters;
|
|
1430
|
-
}
|
|
1430
|
+
}
|
|
1431
1431
|
|
|
1432
1432
|
exports.Api = integrationApi__namespace;
|
|
1433
1433
|
exports.Cache = Cache;
|
package/dist/src/integration.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
2
|
import { InvalidHandler } from './errors.js';
|
|
3
|
+
import { Handler } from './handler.js';
|
|
3
4
|
import correlationIdMiddleware from './middlewares/correlationId.js';
|
|
4
|
-
import loggerMiddleware from './middlewares/logger.js';
|
|
5
5
|
import credentialsMiddleware from './middlewares/credentials.js';
|
|
6
|
-
import signalMiddleware from './middlewares/signal.js';
|
|
7
|
-
import secretsMiddleware from './middlewares/secrets.js';
|
|
8
|
-
import filtersMiddleware from './middlewares/filters.js';
|
|
9
|
-
import selectsMiddleware from './middlewares/selects.js';
|
|
10
6
|
import errorsMiddleware from './middlewares/errors.js';
|
|
7
|
+
import filtersMiddleware from './middlewares/filters.js';
|
|
11
8
|
import finishMiddleware from './middlewares/finish.js';
|
|
12
9
|
import notFoundMiddleware from './middlewares/notFound.js';
|
|
13
|
-
import
|
|
14
|
-
import
|
|
10
|
+
import loggerMiddleware from './middlewares/logger.js';
|
|
11
|
+
import startMiddleware from './middlewares/start.js';
|
|
12
|
+
import secretsMiddleware from './middlewares/secrets.js';
|
|
13
|
+
import selectsMiddleware from './middlewares/selects.js';
|
|
14
|
+
import signalMiddleware from './middlewares/signal.js';
|
|
15
15
|
function printErrorMessage(message) {
|
|
16
16
|
console.error();
|
|
17
17
|
console.error(`\x1b[31m Oops! Something went wrong! \x1b[0m`);
|
|
@@ -115,7 +115,7 @@ export default class Integration {
|
|
|
115
115
|
// Must be one of the first handlers (to catch all the errors).
|
|
116
116
|
app.use(finishMiddleware);
|
|
117
117
|
// Instantiate internal middlewares.
|
|
118
|
-
app.use(
|
|
118
|
+
app.use(startMiddleware);
|
|
119
119
|
app.use(correlationIdMiddleware);
|
|
120
120
|
app.use(loggerMiddleware);
|
|
121
121
|
// Making sure we log all incoming requests (except to '/health'), prior any processing.
|
|
@@ -6,5 +6,5 @@ declare global {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
-
declare
|
|
10
|
-
export default
|
|
9
|
+
declare function extractCorrelationId(req: Request, res: Response, next: NextFunction): void;
|
|
10
|
+
export default extractCorrelationId;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
|
-
|
|
2
|
+
function extractCorrelationId(req, res, next) {
|
|
3
3
|
res.locals.correlationId = req.header('X-Unito-Correlation-Id') ?? crypto.randomUUID();
|
|
4
4
|
next();
|
|
5
|
-
}
|
|
6
|
-
export default
|
|
5
|
+
}
|
|
6
|
+
export default extractCorrelationId;
|
|
@@ -10,5 +10,5 @@ export type Credentials = {
|
|
|
10
10
|
accessToken?: string;
|
|
11
11
|
[keys: string]: unknown;
|
|
12
12
|
};
|
|
13
|
-
declare
|
|
14
|
-
export default
|
|
13
|
+
declare function extractCredentials(req: Request, res: Response, next: NextFunction): void;
|
|
14
|
+
export default extractCredentials;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BadRequestError } from '../httpErrors.js';
|
|
2
2
|
const CREDENTIALS_HEADER = 'X-Unito-Credentials';
|
|
3
|
-
|
|
3
|
+
function extractCredentials(req, res, next) {
|
|
4
4
|
const credentialsHeader = req.header(CREDENTIALS_HEADER);
|
|
5
5
|
if (credentialsHeader) {
|
|
6
6
|
let credentials;
|
|
@@ -13,5 +13,5 @@ const middleware = (req, res, next) => {
|
|
|
13
13
|
res.locals.credentials = credentials;
|
|
14
14
|
}
|
|
15
15
|
next();
|
|
16
|
-
}
|
|
17
|
-
export default
|
|
16
|
+
}
|
|
17
|
+
export default extractCredentials;
|
|
@@ -7,5 +7,5 @@ declare global {
|
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
declare
|
|
11
|
-
export default
|
|
10
|
+
declare function onError(err: Error, _req: Request, res: Response, next: NextFunction): void;
|
|
11
|
+
export default onError;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HttpError } from '../httpErrors.js';
|
|
2
|
-
|
|
2
|
+
function onError(err, _req, res, next) {
|
|
3
3
|
if (res.headersSent) {
|
|
4
4
|
return next(err);
|
|
5
5
|
}
|
|
@@ -33,5 +33,5 @@ const middleware = (err, _req, res, next) => {
|
|
|
33
33
|
delete error.originalError?.details;
|
|
34
34
|
}
|
|
35
35
|
res.status(Number(error.code)).json(error);
|
|
36
|
-
}
|
|
37
|
-
export default
|
|
36
|
+
}
|
|
37
|
+
export default onError;
|
|
@@ -21,5 +21,5 @@ export type Filter = {
|
|
|
21
21
|
operator: OperatorType;
|
|
22
22
|
values: string[] | undefined;
|
|
23
23
|
};
|
|
24
|
-
declare
|
|
25
|
-
export default
|
|
24
|
+
declare function extractFilters(req: Request, res: Response, next: NextFunction): void;
|
|
25
|
+
export default extractFilters;
|
|
@@ -4,8 +4,8 @@ import { OperatorType } from '@unito/integration-api';
|
|
|
4
4
|
// a subset of the symbol of another operator.
|
|
5
5
|
//
|
|
6
6
|
// For example, the symbol "=" (EQUAL) is a subset of the symbol "!=" (NOT_EQUAL).
|
|
7
|
-
const ORDERED_OPERATORS = Object.values(OperatorType).sort((o1, o2) =>
|
|
8
|
-
|
|
7
|
+
const ORDERED_OPERATORS = Object.values(OperatorType).sort((o1, o2) => o2.length - o1.length);
|
|
8
|
+
function extractFilters(req, res, next) {
|
|
9
9
|
const rawFilters = req.query.filter;
|
|
10
10
|
res.locals.filters = [];
|
|
11
11
|
if (typeof rawFilters === 'string') {
|
|
@@ -21,5 +21,5 @@ const middleware = (req, res, next) => {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
next();
|
|
24
|
-
}
|
|
25
|
-
export default
|
|
24
|
+
}
|
|
25
|
+
export default extractFilters;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
function onFinish(req, res, next) {
|
|
2
2
|
if (req.originalUrl !== '/health') {
|
|
3
3
|
res.on('finish', function () {
|
|
4
4
|
const error = res.locals.error;
|
|
@@ -32,5 +32,5 @@ const middleware = (req, res, next) => {
|
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
next();
|
|
35
|
-
}
|
|
36
|
-
export default
|
|
35
|
+
}
|
|
36
|
+
export default onFinish;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Logger from '../resources/logger.js';
|
|
2
2
|
const ADDITIONAL_CONTEXT_HEADER = 'X-Unito-Additional-Logging-Context';
|
|
3
|
-
|
|
3
|
+
function injectLogger(req, res, next) {
|
|
4
4
|
const logger = new Logger({ correlation_id: res.locals.correlationId });
|
|
5
5
|
res.locals.logger = logger;
|
|
6
6
|
const rawAdditionalContext = req.header(ADDITIONAL_CONTEXT_HEADER);
|
|
@@ -14,5 +14,5 @@ const middleware = (req, res, next) => {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
next();
|
|
17
|
-
}
|
|
18
|
-
export default
|
|
17
|
+
}
|
|
18
|
+
export default injectLogger;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
declare
|
|
3
|
-
export default
|
|
2
|
+
declare function notFound(req: Request, res: Response, _next: NextFunction): void;
|
|
3
|
+
export default notFound;
|