@spytecgps/lambda-utils 2.0.6 → 2.1.0-rc1
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/errors/BadRequestError.d.ts +6 -6
- package/dist/errors/BadRequestError.js +28 -0
- package/dist/errors/BaseError.d.ts +4 -4
- package/dist/errors/BaseError.js +24 -0
- package/dist/errors/ConflictError.d.ts +6 -6
- package/dist/errors/ForbiddenError.d.ts +6 -6
- package/dist/errors/HttpError.d.ts +8 -8
- package/dist/errors/HttpError.js +32 -0
- package/dist/errors/NotFoundError.d.ts +6 -6
- package/dist/errors/NotFoundError.js +28 -0
- package/dist/errors/UnauthorizedError.d.ts +6 -6
- package/dist/errors/UnauthorizedError.js +28 -0
- package/dist/errors/index.d.ts +8 -8
- package/dist/errors/index.js +12 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.js +1 -1
- package/dist/logger/index.d.ts +4 -4
- package/dist/logger/logger.d.ts +3 -0
- package/dist/logger/logger.js +57 -0
- package/dist/middleware/contextualLogger.d.ts +15 -15
- package/dist/middleware/contextualLogger.js +87 -0
- package/dist/middleware/index.d.ts +12 -12
- package/dist/middleware/index.js +62 -0
- package/dist/middleware/ioLogger.d.ts +1 -1
- package/dist/middleware/ioLogger.js +14 -0
- package/dist/middleware/middleware.test.d.ts +1 -1
- package/dist/middleware/middleware.test.js +167 -0
- package/dist/middleware/responseWrapper.d.ts +3 -3
- package/dist/middleware/responseWrapper.js +12 -0
- package/dist/middleware/types.d.ts +7 -7
- package/dist/middleware/types.js +2 -0
- package/dist/middleware/validation.d.ts +9 -9
- package/dist/middleware/validation.js +27 -0
- package/dist/types.d.ts +73 -72
- package/dist/types.js +2 -0
- package/dist/utils/cache.d.ts +23 -23
- package/dist/utils/cacheWrapper.d.ts +2 -2
- package/dist/utils/index.d.ts +4 -4
- package/dist/utils/timeOut.d.ts +1 -1
- package/dist/validation/custom.d.ts +18 -18
- package/dist/validation/custom.js +122 -0
- package/dist/validation/index.d.ts +6 -6
- package/dist/validation/index.js +20 -0
- package/dist/validation/requestContext.d.ts +14 -14
- package/dist/validation/requestContext.js +54 -0
- package/dist/validation/validateEvent.d.ts +3 -3
- package/dist/validation/validateEvent.js +25 -0
- package/dist/wrappers/apiGatewayEventWrapper.d.ts +4 -4
- package/dist/wrappers/apiGatewayEventWrapper.js +82 -0
- package/dist/wrappers/index.d.ts +5 -5
- package/dist/wrappers/index.js +21 -0
- package/dist/wrappers/response.d.ts +8 -8
- package/dist/wrappers/response.js +35 -0
- package/dist/wrappers/sqsEventWrapper.d.ts +3 -3
- package/dist/wrappers/sqsEventWrapper.js +116 -0
- package/dist/wrappers/sqsEventWrapperWithReturn.d.ts +3 -3
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var index_1 = require("./index");
|
|
43
|
+
var joi_1 = __importDefault(require("joi"));
|
|
44
|
+
var validation_1 = require("./validation");
|
|
45
|
+
var errors_1 = require("../errors");
|
|
46
|
+
var responseWrapper_1 = require("./responseWrapper");
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
48
|
+
var webEvent = require('./mocks/webEvent.json');
|
|
49
|
+
var context = {};
|
|
50
|
+
var callback = jest.fn();
|
|
51
|
+
var clone = function (obj) { return JSON.parse(JSON.stringify(obj)); };
|
|
52
|
+
describe('Middleware', function () {
|
|
53
|
+
var schema = joi_1.default.object({
|
|
54
|
+
queryStringParameters: joi_1.default.object({
|
|
55
|
+
startDate: joi_1.default.date().required(),
|
|
56
|
+
endDate: joi_1.default.date()
|
|
57
|
+
.default(function () { return new Date(); })
|
|
58
|
+
.greater(joi_1.default.ref('startDate')),
|
|
59
|
+
}),
|
|
60
|
+
});
|
|
61
|
+
describe('APIGatewayProxyHandler', function () {
|
|
62
|
+
var handler = function (event) { return __awaiter(void 0, void 0, void 0, function () {
|
|
63
|
+
return __generator(this, function (_a) {
|
|
64
|
+
console.log('ok done');
|
|
65
|
+
return [2 /*return*/, {
|
|
66
|
+
statusCode: 200,
|
|
67
|
+
body: {
|
|
68
|
+
year: event.queryStringParameters.startDate.getFullYear(),
|
|
69
|
+
yes: ['a', 'b', 'c'],
|
|
70
|
+
},
|
|
71
|
+
}];
|
|
72
|
+
});
|
|
73
|
+
}); };
|
|
74
|
+
var handlerWithError = function (_event) { return __awaiter(void 0, void 0, void 0, function () {
|
|
75
|
+
return __generator(this, function (_a) {
|
|
76
|
+
console.log('here i am');
|
|
77
|
+
throw new errors_1.UnauthorizedError('oh no');
|
|
78
|
+
});
|
|
79
|
+
}); };
|
|
80
|
+
var middifiedHandler = index_1.apiGatewayMiddy(handler).use(validation_1.validatorMiddleware({ schema: schema }));
|
|
81
|
+
var middifiedHandlerWithError = index_1.apiGatewayMiddy(handlerWithError).use(validation_1.validatorMiddleware({ schema: schema }));
|
|
82
|
+
it('should work', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
83
|
+
var result;
|
|
84
|
+
return __generator(this, function (_a) {
|
|
85
|
+
switch (_a.label) {
|
|
86
|
+
case 0: return [4 /*yield*/, middifiedHandler(webEvent, context, callback)];
|
|
87
|
+
case 1:
|
|
88
|
+
result = _a.sent();
|
|
89
|
+
console.log(result);
|
|
90
|
+
return [2 /*return*/];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}); });
|
|
94
|
+
it('should throw', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
95
|
+
var invalidEvent, result;
|
|
96
|
+
return __generator(this, function (_a) {
|
|
97
|
+
switch (_a.label) {
|
|
98
|
+
case 0:
|
|
99
|
+
invalidEvent = clone(webEvent);
|
|
100
|
+
invalidEvent.queryStringParameters.startDate = 'asdf';
|
|
101
|
+
return [4 /*yield*/, middifiedHandler(invalidEvent, context, callback)];
|
|
102
|
+
case 1:
|
|
103
|
+
result = _a.sent();
|
|
104
|
+
console.log(result);
|
|
105
|
+
return [2 /*return*/];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}); });
|
|
109
|
+
it('should error', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
110
|
+
var result;
|
|
111
|
+
return __generator(this, function (_a) {
|
|
112
|
+
switch (_a.label) {
|
|
113
|
+
case 0: return [4 /*yield*/, middifiedHandlerWithError(webEvent, context, callback)];
|
|
114
|
+
case 1:
|
|
115
|
+
result = _a.sent();
|
|
116
|
+
console.log(result);
|
|
117
|
+
return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}); });
|
|
121
|
+
});
|
|
122
|
+
describe('responseWrapperMiddleware', function () {
|
|
123
|
+
var handler = function (_event) { return __awaiter(void 0, void 0, void 0, function () {
|
|
124
|
+
return __generator(this, function (_a) {
|
|
125
|
+
console.log('ok done');
|
|
126
|
+
return [2 /*return*/, {
|
|
127
|
+
data: {
|
|
128
|
+
abc: 123,
|
|
129
|
+
},
|
|
130
|
+
}];
|
|
131
|
+
});
|
|
132
|
+
}); };
|
|
133
|
+
var middifiedHandler = index_1.apiGatewayMiddy(handler).use(responseWrapper_1.responseWrapperMiddleware());
|
|
134
|
+
it('should work', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
135
|
+
var result;
|
|
136
|
+
return __generator(this, function (_a) {
|
|
137
|
+
switch (_a.label) {
|
|
138
|
+
case 0: return [4 /*yield*/, middifiedHandler(webEvent, context, callback)];
|
|
139
|
+
case 1:
|
|
140
|
+
result = _a.sent();
|
|
141
|
+
console.log(result);
|
|
142
|
+
return [2 /*return*/];
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}); });
|
|
146
|
+
});
|
|
147
|
+
describe('responseWrapperMiddleware error', function () {
|
|
148
|
+
var handler = function (_event) { return __awaiter(void 0, void 0, void 0, function () {
|
|
149
|
+
return __generator(this, function (_a) {
|
|
150
|
+
throw new errors_1.NotFoundError();
|
|
151
|
+
});
|
|
152
|
+
}); };
|
|
153
|
+
var middifiedHandler = index_1.apiGatewayMiddy(handler).use(responseWrapper_1.responseWrapperMiddleware());
|
|
154
|
+
it('should work', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
155
|
+
var result;
|
|
156
|
+
return __generator(this, function (_a) {
|
|
157
|
+
switch (_a.label) {
|
|
158
|
+
case 0: return [4 /*yield*/, middifiedHandler(webEvent, context, callback)];
|
|
159
|
+
case 1:
|
|
160
|
+
result = _a.sent();
|
|
161
|
+
console.log(result);
|
|
162
|
+
return [2 /*return*/];
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}); });
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const responseWrapperMiddleware: () => {
|
|
2
|
-
after: (req: any) => void;
|
|
3
|
-
};
|
|
1
|
+
export declare const responseWrapperMiddleware: () => {
|
|
2
|
+
after: (req: any) => void;
|
|
3
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.responseWrapperMiddleware = void 0;
|
|
4
|
+
var wrappers_1 = require("../wrappers");
|
|
5
|
+
exports.responseWrapperMiddleware = function () {
|
|
6
|
+
var responseWrapperMiddlewareAfter = function (req) {
|
|
7
|
+
req.response = wrappers_1.buildProxyResult(req.response);
|
|
8
|
+
};
|
|
9
|
+
return {
|
|
10
|
+
after: responseWrapperMiddlewareAfter,
|
|
11
|
+
};
|
|
12
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { APIGatewayProxyResult, Context as LambdaContext } from 'aws-lambda';
|
|
2
|
-
import { Callback as LambdaCallback } from 'aws-lambda/handler';
|
|
3
|
-
export type MiddyInputPromiseHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext) => Promise<TResult>;
|
|
4
|
-
export interface APIResult<T = any> extends Omit<APIGatewayProxyResult, 'body'> {
|
|
5
|
-
body: T;
|
|
6
|
-
}
|
|
7
|
-
export type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => Promise<TResult>;
|
|
1
|
+
import { APIGatewayProxyResult, Context as LambdaContext } from 'aws-lambda';
|
|
2
|
+
import { Callback as LambdaCallback } from 'aws-lambda/handler';
|
|
3
|
+
export type MiddyInputPromiseHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext) => Promise<TResult>;
|
|
4
|
+
export interface APIResult<T = any> extends Omit<APIGatewayProxyResult, 'body'> {
|
|
5
|
+
body: T;
|
|
6
|
+
}
|
|
7
|
+
export type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => Promise<TResult>;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import Joi from 'joi';
|
|
2
|
-
interface ValidatorOpts {
|
|
3
|
-
schema: Joi.Schema;
|
|
4
|
-
allowUnknown?: boolean;
|
|
5
|
-
}
|
|
6
|
-
export declare const validatorMiddleware: ({ schema, allowUnknown }: ValidatorOpts) => {
|
|
7
|
-
before: (request: any) => void;
|
|
8
|
-
};
|
|
9
|
-
export {};
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
interface ValidatorOpts {
|
|
3
|
+
schema: Joi.Schema;
|
|
4
|
+
allowUnknown?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare const validatorMiddleware: ({ schema, allowUnknown }: ValidatorOpts) => {
|
|
7
|
+
before: (request: any) => void;
|
|
8
|
+
};
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validatorMiddleware = void 0;
|
|
4
|
+
var errors_1 = require("../errors");
|
|
5
|
+
var logger_1 = require("../logger/logger");
|
|
6
|
+
exports.validatorMiddleware = function (_a) {
|
|
7
|
+
var schema = _a.schema, _b = _a.allowUnknown, allowUnknown = _b === void 0 ? true : _b;
|
|
8
|
+
var validatorMiddlewareBefore = function (request) {
|
|
9
|
+
var _a = schema.validate(request.event, {
|
|
10
|
+
allowUnknown: allowUnknown,
|
|
11
|
+
errors: {
|
|
12
|
+
label: 'key',
|
|
13
|
+
wrap: {
|
|
14
|
+
label: false,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
}), error = _a.error, value = _a.value;
|
|
18
|
+
if (error) {
|
|
19
|
+
logger_1.logger.error('Validation error', { error: error });
|
|
20
|
+
throw error.isJoi ? new errors_1.BadRequestError(error.message) : error;
|
|
21
|
+
}
|
|
22
|
+
request.event = value;
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
before: validatorMiddlewareBefore,
|
|
26
|
+
};
|
|
27
|
+
};
|
package/dist/types.d.ts
CHANGED
|
@@ -1,72 +1,73 @@
|
|
|
1
|
-
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyWithCognitoAuthorizerEvent, APIGatewayProxyWithLambdaAuthorizerEvent, Context, SQSEvent, SQSRecord } from 'aws-lambda';
|
|
2
|
-
import { Callback } from 'aws-lambda/handler';
|
|
3
|
-
import { APIGatewayProxyEventBase } from 'aws-lambda/trigger/api-gateway-proxy';
|
|
4
|
-
import { ObjectSchema } from 'joi';
|
|
5
|
-
import * as Joi from 'joi';
|
|
6
|
-
export interface SpytecAuthorizedResources {
|
|
7
|
-
devices: string[];
|
|
8
|
-
boundaries: number[];
|
|
9
|
-
}
|
|
10
|
-
export type AuthClass = 'user' | 'client' | 'system';
|
|
11
|
-
export type DevicesAccess = 'full' | 'limited';
|
|
12
|
-
export type BoundariesAccess = 'full' | 'limited';
|
|
13
|
-
export type TasksAccess = 'full' | 'can_complete' | 'read_only' | 'no_access';
|
|
14
|
-
export type BillingMethod = 'credit-card-hubspot' | 'ach-hubspot' | 'credit-card-braintree' | 'ach-braintree' | 'ach-wire' | 'check' | 'dib-sap';
|
|
15
|
-
export type CustomerSegment = 'whale' | 'bass' | 'guppy' | 'guppy-self-serve';
|
|
16
|
-
export interface SpytecAuthContext {
|
|
17
|
-
type: AuthClass;
|
|
18
|
-
userId?: string;
|
|
19
|
-
clientId: number;
|
|
20
|
-
devicesAccess: DevicesAccess;
|
|
21
|
-
boundariesAccess: BoundariesAccess;
|
|
22
|
-
minAccessDate?: string;
|
|
23
|
-
resources: SpytecAuthorizedResources;
|
|
24
|
-
principalId: string;
|
|
25
|
-
scope?: string;
|
|
26
|
-
enterprise?: boolean;
|
|
27
|
-
maintenanceModule?: boolean;
|
|
28
|
-
tasksAccess?: TasksAccess;
|
|
29
|
-
billingMethod?: BillingMethod;
|
|
30
|
-
customerSegment?: CustomerSegment;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
export type
|
|
41
|
-
export type
|
|
42
|
-
export
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
export
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
1
|
+
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyWithCognitoAuthorizerEvent, APIGatewayProxyWithLambdaAuthorizerEvent, Context, SQSEvent, SQSRecord } from 'aws-lambda';
|
|
2
|
+
import { Callback } from 'aws-lambda/handler';
|
|
3
|
+
import { APIGatewayProxyEventBase } from 'aws-lambda/trigger/api-gateway-proxy';
|
|
4
|
+
import { ObjectSchema } from 'joi';
|
|
5
|
+
import * as Joi from 'joi';
|
|
6
|
+
export interface SpytecAuthorizedResources {
|
|
7
|
+
devices: string[];
|
|
8
|
+
boundaries: number[];
|
|
9
|
+
}
|
|
10
|
+
export type AuthClass = 'user' | 'client' | 'system';
|
|
11
|
+
export type DevicesAccess = 'full' | 'limited';
|
|
12
|
+
export type BoundariesAccess = 'full' | 'limited';
|
|
13
|
+
export type TasksAccess = 'full' | 'can_complete' | 'read_only' | 'no_access';
|
|
14
|
+
export type BillingMethod = 'credit-card-hubspot' | 'ach-hubspot' | 'credit-card-braintree' | 'ach-braintree' | 'ach-wire' | 'check' | 'dib-sap';
|
|
15
|
+
export type CustomerSegment = 'whale' | 'bass' | 'guppy' | 'guppy-self-serve';
|
|
16
|
+
export interface SpytecAuthContext {
|
|
17
|
+
type: AuthClass;
|
|
18
|
+
userId?: string;
|
|
19
|
+
clientId: number;
|
|
20
|
+
devicesAccess: DevicesAccess;
|
|
21
|
+
boundariesAccess: BoundariesAccess;
|
|
22
|
+
minAccessDate?: string;
|
|
23
|
+
resources: SpytecAuthorizedResources;
|
|
24
|
+
principalId: string;
|
|
25
|
+
scope?: string;
|
|
26
|
+
enterprise?: boolean;
|
|
27
|
+
maintenanceModule?: boolean;
|
|
28
|
+
tasksAccess?: TasksAccess;
|
|
29
|
+
billingMethod?: BillingMethod;
|
|
30
|
+
customerSegment?: CustomerSegment;
|
|
31
|
+
securityGroupTagId?: number;
|
|
32
|
+
}
|
|
33
|
+
export type BaseAPIGatewayEvent = Omit<APIGatewayProxyEventBase<SpytecAuthContext>, 'pathParameters' | 'queryStringParameters' | 'body'>;
|
|
34
|
+
export interface WrapperArgs<E, R> {
|
|
35
|
+
event: APIGatewayProxyEvent;
|
|
36
|
+
context?: APIGatewayEventRequestContext;
|
|
37
|
+
schema?: ObjectSchema<E>;
|
|
38
|
+
handler: Handler<E, R>;
|
|
39
|
+
}
|
|
40
|
+
export type UserAPIHandler<TEvent = APIGatewayProxyWithLambdaAuthorizerEvent<SpytecAuthContext>, TResult = any> = (event: TEvent, context: APIGatewayEventRequestContext, callback: Callback<TResult>) => void | Promise<TResult>;
|
|
41
|
+
export type ServerAPIHandler<TResult = any> = (event: APIGatewayProxyWithCognitoAuthorizerEvent, context: APIGatewayEventRequestContext, callback: Callback<TResult>) => void | Promise<TResult>;
|
|
42
|
+
export type Handler<E, R> = (event: E) => Promise<HandlerResponse<R>>;
|
|
43
|
+
export interface HandlerResponse<R> {
|
|
44
|
+
statusCode?: number;
|
|
45
|
+
headers?: {
|
|
46
|
+
[header: string]: boolean | number | string;
|
|
47
|
+
};
|
|
48
|
+
isBase64Encoded?: boolean;
|
|
49
|
+
message?: string;
|
|
50
|
+
data?: R;
|
|
51
|
+
rawResult?: boolean;
|
|
52
|
+
stringifyBody?: boolean;
|
|
53
|
+
}
|
|
54
|
+
export type BaseRecord = Omit<SQSRecord, 'body' | 'messageAttributes'>;
|
|
55
|
+
export interface MessageEvent<RecordType extends BaseRecord> {
|
|
56
|
+
Records: RecordType[];
|
|
57
|
+
}
|
|
58
|
+
export type SQSWrapperArgs<RecordType extends BaseRecord> = {
|
|
59
|
+
event: SQSEvent;
|
|
60
|
+
context?: Context;
|
|
61
|
+
schema?: Joi.ObjectSchema<MessageEvent<RecordType>>;
|
|
62
|
+
handler?: (e: MessageEvent<RecordType>) => void | Promise<void>;
|
|
63
|
+
singleHandler?: (record: RecordType) => void | Promise<void>;
|
|
64
|
+
mode?: 'serial' | 'parallel';
|
|
65
|
+
};
|
|
66
|
+
export type SQSWrapperArgsWithReturn<RecordType extends BaseRecord, T> = {
|
|
67
|
+
event: SQSEvent;
|
|
68
|
+
context?: Context;
|
|
69
|
+
schema?: Joi.ObjectSchema<MessageEvent<RecordType>>;
|
|
70
|
+
handler?: (e: MessageEvent<RecordType>) => T | Promise<T>;
|
|
71
|
+
singleHandler?: (record: RecordType) => T | Promise<T>;
|
|
72
|
+
mode?: 'serial' | 'parallel';
|
|
73
|
+
};
|
package/dist/types.js
ADDED
package/dist/utils/cache.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
export declare class LambdaCache {
|
|
2
|
-
private collectionName;
|
|
3
|
-
/**
|
|
4
|
-
* @param {String} collectionName (not required) - The collection key used to store the cache values.
|
|
5
|
-
* If not provide default collection name uses :
|
|
6
|
-
* ${process.env.AWS_LAMBDA_FUNCTION_NAME}-${process.env.AWS_LAMBDA_FUNCTION_VERSION}
|
|
7
|
-
* */
|
|
8
|
-
constructor(collectionName?: string);
|
|
9
|
-
/**
|
|
10
|
-
* @param {String} key (required) - cache key
|
|
11
|
-
* @param {Object} value (required) - cache value
|
|
12
|
-
* @param {Number} expire (required) - cache expiration time (seconds)
|
|
13
|
-
* */
|
|
14
|
-
set(key: string, value: unknown, ttl: number): void;
|
|
15
|
-
/**
|
|
16
|
-
* @param {String} key (required) - cache key to get
|
|
17
|
-
* */
|
|
18
|
-
get(key: string): unknown;
|
|
19
|
-
/**
|
|
20
|
-
* @param {String} key (required) - cache key to remove
|
|
21
|
-
* */
|
|
22
|
-
remove(key: any): void;
|
|
23
|
-
}
|
|
1
|
+
export declare class LambdaCache {
|
|
2
|
+
private collectionName;
|
|
3
|
+
/**
|
|
4
|
+
* @param {String} collectionName (not required) - The collection key used to store the cache values.
|
|
5
|
+
* If not provide default collection name uses :
|
|
6
|
+
* ${process.env.AWS_LAMBDA_FUNCTION_NAME}-${process.env.AWS_LAMBDA_FUNCTION_VERSION}
|
|
7
|
+
* */
|
|
8
|
+
constructor(collectionName?: string);
|
|
9
|
+
/**
|
|
10
|
+
* @param {String} key (required) - cache key
|
|
11
|
+
* @param {Object} value (required) - cache value
|
|
12
|
+
* @param {Number} expire (required) - cache expiration time (seconds)
|
|
13
|
+
* */
|
|
14
|
+
set(key: string, value: unknown, ttl: number): void;
|
|
15
|
+
/**
|
|
16
|
+
* @param {String} key (required) - cache key to get
|
|
17
|
+
* */
|
|
18
|
+
get(key: string): unknown;
|
|
19
|
+
/**
|
|
20
|
+
* @param {String} key (required) - cache key to remove
|
|
21
|
+
* */
|
|
22
|
+
remove(key: any): void;
|
|
23
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { LambdaCache } from './cache';
|
|
2
|
-
export declare const promiseWithCache: <T>(promise: () => Promise<T>, cacheInstance: LambdaCache, cacheKey: string, ttl: number) => Promise<T>;
|
|
1
|
+
import { LambdaCache } from './cache';
|
|
2
|
+
export declare const promiseWithCache: <T>(promise: () => Promise<T>, cacheInstance: LambdaCache, cacheKey: string, ttl: number) => Promise<T>;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LambdaCache } from './cache';
|
|
2
|
-
export * from './cacheWrapper';
|
|
3
|
-
export * from './timeOut';
|
|
4
|
-
export { LambdaCache };
|
|
1
|
+
import { LambdaCache } from './cache';
|
|
2
|
+
export * from './cacheWrapper';
|
|
3
|
+
export * from './timeOut';
|
|
4
|
+
export { LambdaCache };
|
package/dist/utils/timeOut.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const promiseWithTimeout: <T>(promise: Promise<T>, ms: number, timeoutError?: Error) => Promise<T>;
|
|
1
|
+
export declare const promiseWithTimeout: <T>(promise: Promise<T>, ms: number, timeoutError?: Error) => Promise<T>;
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import * as Joi from 'joi';
|
|
2
|
-
import { ArraySchema, ObjectSchema, SchemaMap, StringSchema } from 'joi';
|
|
3
|
-
export declare const json: any;
|
|
4
|
-
export declare const urlEncoded: any;
|
|
5
|
-
export declare const imeiSchema: Joi.StringSchema<string>;
|
|
6
|
-
export declare const iccidSchema: Joi.StringSchema<string>;
|
|
7
|
-
interface SpytecJoi extends Joi.Root {
|
|
8
|
-
urlEncodedObject<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
|
|
9
|
-
jsonObject<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
|
|
10
|
-
imei(): StringSchema;
|
|
11
|
-
iccid(): StringSchema;
|
|
12
|
-
delimitedArray(): ArraySchema;
|
|
13
|
-
dateString(): StringSchema;
|
|
14
|
-
jsonArray<TSchema = any, T = TSchema>(schema?: Joi.SchemaMap<T>): ArraySchema<TSchema>;
|
|
15
|
-
base64ThenUriEncodedObject<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
|
|
16
|
-
}
|
|
17
|
-
export declare const SpytecJoi: SpytecJoi;
|
|
18
|
-
export {};
|
|
1
|
+
import * as Joi from 'joi';
|
|
2
|
+
import { ArraySchema, ObjectSchema, SchemaMap, StringSchema } from 'joi';
|
|
3
|
+
export declare const json: any;
|
|
4
|
+
export declare const urlEncoded: any;
|
|
5
|
+
export declare const imeiSchema: Joi.StringSchema<string>;
|
|
6
|
+
export declare const iccidSchema: Joi.StringSchema<string>;
|
|
7
|
+
interface SpytecJoi extends Joi.Root {
|
|
8
|
+
urlEncodedObject<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
|
|
9
|
+
jsonObject<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
|
|
10
|
+
imei(): StringSchema;
|
|
11
|
+
iccid(): StringSchema;
|
|
12
|
+
delimitedArray(): ArraySchema;
|
|
13
|
+
dateString(): StringSchema;
|
|
14
|
+
jsonArray<TSchema = any, T = TSchema>(schema?: Joi.SchemaMap<T>): ArraySchema<TSchema>;
|
|
15
|
+
base64ThenUriEncodedObject<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
|
|
16
|
+
}
|
|
17
|
+
export declare const SpytecJoi: SpytecJoi;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.SpytecJoi = exports.iccidSchema = exports.imeiSchema = exports.urlEncoded = exports.json = void 0;
|
|
23
|
+
var Joi = __importStar(require("joi"));
|
|
24
|
+
var qs = __importStar(require("qs"));
|
|
25
|
+
var moment = __importStar(require("moment-timezone"));
|
|
26
|
+
exports.json = Joi.extend(function (joi) {
|
|
27
|
+
return {
|
|
28
|
+
type: 'object',
|
|
29
|
+
base: joi.object(),
|
|
30
|
+
messages: {
|
|
31
|
+
'json.valid': 'must be valid JSON',
|
|
32
|
+
},
|
|
33
|
+
coerce: function (value) {
|
|
34
|
+
try {
|
|
35
|
+
return { value: JSON.parse(value) };
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
validate: function (value, helpers) {
|
|
42
|
+
if (!value) {
|
|
43
|
+
return { value: value, errors: helpers.error('json.valid') };
|
|
44
|
+
}
|
|
45
|
+
return { value: value };
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
exports.urlEncoded = Joi.extend(function (joi) {
|
|
50
|
+
return {
|
|
51
|
+
type: 'object',
|
|
52
|
+
base: joi.object(),
|
|
53
|
+
coerce: function (value) {
|
|
54
|
+
return { value: qs.parse(value) };
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
exports.imeiSchema = Joi.string()
|
|
59
|
+
.regex(/^\d{15,16}$/)
|
|
60
|
+
.error(new Error('Invalid IMEI'));
|
|
61
|
+
exports.iccidSchema = Joi.string()
|
|
62
|
+
.regex(/^[0-9A-Za-z]{18,22}$/)
|
|
63
|
+
.error(new Error('Invalid ICCID'));
|
|
64
|
+
exports.SpytecJoi = Joi.extend(function (joi) { return ({
|
|
65
|
+
type: 'imei',
|
|
66
|
+
base: joi
|
|
67
|
+
.string()
|
|
68
|
+
.regex(/^\d{15,16}$/)
|
|
69
|
+
.error(new Error('Invalid IMEI')),
|
|
70
|
+
}); }, function (joi) { return ({
|
|
71
|
+
type: 'iccid',
|
|
72
|
+
base: joi
|
|
73
|
+
.string()
|
|
74
|
+
.regex(/^[0-9A-Za-z]{18,22}$/)
|
|
75
|
+
.error(new Error('Invalid ICCID')),
|
|
76
|
+
}); }, function (joi) { return ({
|
|
77
|
+
type: 'urlEncodedObject',
|
|
78
|
+
base: joi.object(),
|
|
79
|
+
coerce: function (value) {
|
|
80
|
+
return { value: qs.parse(value) };
|
|
81
|
+
},
|
|
82
|
+
}); }, function (joi) { return ({
|
|
83
|
+
type: 'jsonObject',
|
|
84
|
+
base: joi.object(),
|
|
85
|
+
messages: {
|
|
86
|
+
'json.valid': 'must be valid JSON',
|
|
87
|
+
},
|
|
88
|
+
coerce: function (value) {
|
|
89
|
+
try {
|
|
90
|
+
return { value: JSON.parse(value) };
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
validate: function (value, helpers) {
|
|
97
|
+
if (!value) {
|
|
98
|
+
return { value: value, errors: helpers.error('json.valid') };
|
|
99
|
+
}
|
|
100
|
+
return { value: value };
|
|
101
|
+
},
|
|
102
|
+
}); }, function (joi) { return ({
|
|
103
|
+
type: 'delimitedArray',
|
|
104
|
+
base: joi.array().default([]),
|
|
105
|
+
coerce: function (value) { return ({
|
|
106
|
+
value: value.split ? value.split(',') : value,
|
|
107
|
+
}); },
|
|
108
|
+
}); }, function (joi) { return ({
|
|
109
|
+
type: 'queryStringParameters',
|
|
110
|
+
base: joi
|
|
111
|
+
.object()
|
|
112
|
+
.required()
|
|
113
|
+
.error(new Error('Missing query parameters')),
|
|
114
|
+
}); }, function (joi) { return ({
|
|
115
|
+
type: 'date',
|
|
116
|
+
base: joi.date(),
|
|
117
|
+
prepare: function (value) {
|
|
118
|
+
return {
|
|
119
|
+
value: moment.tz(value, 'UTC').toDate(),
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
}); });
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BaseAPIGatewayEvent } from '../types';
|
|
2
|
-
import validateEvent from './validateEvent';
|
|
3
|
-
export { validateEvent };
|
|
4
|
-
export * from './custom';
|
|
5
|
-
export * from './requestContext';
|
|
6
|
-
export declare const defaultApiSchema: import("joi").ObjectSchema<BaseAPIGatewayEvent>;
|
|
1
|
+
import { BaseAPIGatewayEvent } from '../types';
|
|
2
|
+
import validateEvent from './validateEvent';
|
|
3
|
+
export { validateEvent };
|
|
4
|
+
export * from './custom';
|
|
5
|
+
export * from './requestContext';
|
|
6
|
+
export declare const defaultApiSchema: import("joi").ObjectSchema<BaseAPIGatewayEvent>;
|