@spytecgps/lambda-utils 0.9.23 → 1.0.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 -5
- package/dist/errors/BadRequestError.js +28 -27
- package/dist/errors/BaseError.d.ts +3 -3
- package/dist/errors/BaseError.js +24 -24
- package/dist/errors/HttpError.d.ts +8 -0
- package/dist/errors/HttpError.js +32 -0
- package/dist/errors/NotFoundError.d.ts +6 -5
- package/dist/errors/NotFoundError.js +28 -27
- package/dist/errors/UnauthorizedError.d.ts +6 -5
- package/dist/errors/UnauthorizedError.js +28 -27
- package/dist/errors/index.d.ts +4 -4
- package/dist/errors/index.js +12 -9
- package/dist/index.d.ts +4 -4
- package/dist/index.js +16 -16
- package/dist/middleware/contextualLogger.d.ts +12 -0
- package/dist/middleware/contextualLogger.js +55 -0
- package/dist/middleware/index.d.ts +10 -0
- package/dist/middleware/index.js +60 -0
- package/dist/middleware/ioLogger.d.ts +1 -0
- package/dist/middleware/ioLogger.js +14 -0
- package/dist/middleware/middleware.test.d.ts +1 -0
- package/dist/middleware/middleware.test.js +121 -0
- package/dist/middleware/types.d.ts +7 -0
- package/dist/middleware/types.js +15 -0
- package/dist/middleware/validation.d.ts +9 -0
- package/dist/middleware/validation.js +27 -0
- package/dist/types.d.ts +49 -59
- package/dist/types.js +2 -2
- package/dist/validation/custom.d.ts +16 -16
- package/dist/validation/custom.js +122 -103
- package/dist/validation/index.d.ts +4 -4
- package/dist/validation/index.js +20 -17
- package/dist/validation/requestContext.d.ts +14 -14
- package/dist/validation/requestContext.js +54 -35
- package/dist/validation/validateEvent.d.ts +3 -3
- package/dist/validation/validateEvent.js +25 -25
- package/dist/wrappers/apiGatewayEventWrapper.d.ts +4 -4
- package/dist/wrappers/apiGatewayEventWrapper.js +82 -120
- package/dist/wrappers/index.d.ts +4 -4
- package/dist/wrappers/index.js +21 -18
- package/dist/wrappers/response.d.ts +8 -8
- package/dist/wrappers/response.js +35 -35
- package/dist/wrappers/sqsEventWrapper.d.ts +3 -3
- package/dist/wrappers/sqsEventWrapper.js +116 -113
- package/package.json +6 -2
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
47
|
+
var webEvent = require('./mocks/webEvent.json');
|
|
48
|
+
var context = {};
|
|
49
|
+
var callback = jest.fn();
|
|
50
|
+
var clone = function (obj) { return JSON.parse(JSON.stringify(obj)); };
|
|
51
|
+
describe('Middleware', function () {
|
|
52
|
+
var schema = joi_1.default.object({
|
|
53
|
+
queryStringParameters: joi_1.default.object({
|
|
54
|
+
startDate: joi_1.default.date().required(),
|
|
55
|
+
endDate: joi_1.default.date()
|
|
56
|
+
.default(function () { return new Date(); })
|
|
57
|
+
.greater(joi_1.default.ref('startDate')),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
describe('APIGatewayProxyHandler', function () {
|
|
61
|
+
var handler = function (event) { return __awaiter(void 0, void 0, void 0, function () {
|
|
62
|
+
return __generator(this, function (_a) {
|
|
63
|
+
console.log('ok done');
|
|
64
|
+
return [2 /*return*/, {
|
|
65
|
+
statusCode: 200,
|
|
66
|
+
body: {
|
|
67
|
+
year: event.queryStringParameters.startDate.getFullYear(),
|
|
68
|
+
yes: ['a', 'b', 'c'],
|
|
69
|
+
},
|
|
70
|
+
}];
|
|
71
|
+
});
|
|
72
|
+
}); };
|
|
73
|
+
var handlerWithError = function (_event) { return __awaiter(void 0, void 0, void 0, function () {
|
|
74
|
+
return __generator(this, function (_a) {
|
|
75
|
+
console.log('here i am');
|
|
76
|
+
throw new errors_1.UnauthorizedError('oh no');
|
|
77
|
+
});
|
|
78
|
+
}); };
|
|
79
|
+
var middifiedHandler = index_1.apiGatewayMiddy(handler).use(validation_1.validatorMiddleware({ schema: schema }));
|
|
80
|
+
var middifiedHandlerWithError = index_1.apiGatewayMiddy(handlerWithError).use(validation_1.validatorMiddleware({ schema: schema }));
|
|
81
|
+
it('should work', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
82
|
+
var result;
|
|
83
|
+
return __generator(this, function (_a) {
|
|
84
|
+
switch (_a.label) {
|
|
85
|
+
case 0: return [4 /*yield*/, middifiedHandler(webEvent, context, callback)];
|
|
86
|
+
case 1:
|
|
87
|
+
result = _a.sent();
|
|
88
|
+
console.log(result);
|
|
89
|
+
return [2 /*return*/];
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}); });
|
|
93
|
+
it('should throw', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
94
|
+
var invalidEvent, result;
|
|
95
|
+
return __generator(this, function (_a) {
|
|
96
|
+
switch (_a.label) {
|
|
97
|
+
case 0:
|
|
98
|
+
invalidEvent = clone(webEvent);
|
|
99
|
+
invalidEvent.queryStringParameters.startDate = 'asdf';
|
|
100
|
+
return [4 /*yield*/, middifiedHandler(invalidEvent, context, callback)];
|
|
101
|
+
case 1:
|
|
102
|
+
result = _a.sent();
|
|
103
|
+
console.log(result);
|
|
104
|
+
return [2 /*return*/];
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}); });
|
|
108
|
+
it('should error', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
109
|
+
var result;
|
|
110
|
+
return __generator(this, function (_a) {
|
|
111
|
+
switch (_a.label) {
|
|
112
|
+
case 0: return [4 /*yield*/, middifiedHandlerWithError(webEvent, context, callback)];
|
|
113
|
+
case 1:
|
|
114
|
+
result = _a.sent();
|
|
115
|
+
console.log(result);
|
|
116
|
+
return [2 /*return*/];
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}); });
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { APIGatewayProxyResult, Context as LambdaContext } from 'aws-lambda';
|
|
2
|
+
import { Callback as LambdaCallback } from 'aws-lambda/handler';
|
|
3
|
+
export declare 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 declare type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => Promise<TResult>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
//
|
|
4
|
+
// export type MiddlewareHandler<
|
|
5
|
+
// THandler extends LambdaHandler<any, any>,
|
|
6
|
+
// TContext extends LambdaContext = LambdaContext
|
|
7
|
+
// > = THandler extends LambdaHandler<infer TEvent, infer TResult> // always true
|
|
8
|
+
// ? MiddyInputHandler<TEvent, TResult, TContext>
|
|
9
|
+
// : never;
|
|
10
|
+
//
|
|
11
|
+
// export type MiddlewareHandler<TContext extends LambdaContext = LambdaContext> = MiddyInputHandler<
|
|
12
|
+
// TEvent,
|
|
13
|
+
// TResult,
|
|
14
|
+
// TContext
|
|
15
|
+
// >;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validatorMiddleware = void 0;
|
|
4
|
+
var sdk_1 = require("@spytecgps/sdk");
|
|
5
|
+
var errors_1 = require("../errors");
|
|
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
|
+
sdk_1.logger.error({ error: error }, 'Validation 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,59 +1,49 @@
|
|
|
1
|
-
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyWithLambdaAuthorizerEvent, SQSEvent, SQSRecord, Context, APIGatewayProxyWithCognitoAuthorizerEvent } from 'aws-lambda';
|
|
2
|
-
import { ObjectSchema } from 'joi';
|
|
3
|
-
import { APIGatewayProxyEventBase } from 'aws-lambda/trigger/api-gateway-proxy';
|
|
4
|
-
import * as Joi from 'joi';
|
|
5
|
-
import { Callback } from 'aws-lambda/handler';
|
|
6
|
-
export interface SpytecAuthorizedResources {
|
|
7
|
-
devices: string[];
|
|
8
|
-
}
|
|
9
|
-
export declare type AuthClass = 'user' | 'client' | 'system';
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
export
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
Records: RecordType[];
|
|
51
|
-
}
|
|
52
|
-
export declare type SQSWrapperArgs<RecordType extends BaseRecord> = {
|
|
53
|
-
event: SQSEvent;
|
|
54
|
-
context?: Context;
|
|
55
|
-
schema?: Joi.ObjectSchema<MessageEvent<RecordType>>;
|
|
56
|
-
handler?: (e: MessageEvent<RecordType>) => void | Promise<void>;
|
|
57
|
-
singleHandler?: (record: RecordType) => void | Promise<void>;
|
|
58
|
-
mode?: 'serial' | 'parallel';
|
|
59
|
-
};
|
|
1
|
+
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyWithLambdaAuthorizerEvent, SQSEvent, SQSRecord, Context, APIGatewayProxyWithCognitoAuthorizerEvent } from 'aws-lambda';
|
|
2
|
+
import { ObjectSchema } from 'joi';
|
|
3
|
+
import { APIGatewayProxyEventBase } from 'aws-lambda/trigger/api-gateway-proxy';
|
|
4
|
+
import * as Joi from 'joi';
|
|
5
|
+
import { Callback } from 'aws-lambda/handler';
|
|
6
|
+
export interface SpytecAuthorizedResources {
|
|
7
|
+
devices: string[];
|
|
8
|
+
}
|
|
9
|
+
export declare type AuthClass = 'user' | 'client' | 'system';
|
|
10
|
+
export interface SpytecAuthContext {
|
|
11
|
+
type: AuthClass;
|
|
12
|
+
userId?: string;
|
|
13
|
+
clientId: number;
|
|
14
|
+
resources: SpytecAuthorizedResources;
|
|
15
|
+
principalId: string;
|
|
16
|
+
scope?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare type BaseAPIGatewayEvent = Omit<APIGatewayProxyEventBase<SpytecAuthContext>, 'pathParameters' | 'queryStringParameters' | 'body'>;
|
|
19
|
+
export interface WrapperArgs<E, R> {
|
|
20
|
+
event: APIGatewayProxyEvent;
|
|
21
|
+
context?: APIGatewayEventRequestContext;
|
|
22
|
+
schema?: ObjectSchema<E>;
|
|
23
|
+
handler: Handler<E, R>;
|
|
24
|
+
}
|
|
25
|
+
export declare type UserAPIHandler<TEvent = APIGatewayProxyWithLambdaAuthorizerEvent<SpytecAuthContext>, TResult = any> = (event: TEvent, context: APIGatewayEventRequestContext, callback: Callback<TResult>) => void | Promise<TResult>;
|
|
26
|
+
export declare type ServerAPIHandler<TResult = any> = (event: APIGatewayProxyWithCognitoAuthorizerEvent, context: APIGatewayEventRequestContext, callback: Callback<TResult>) => void | Promise<TResult>;
|
|
27
|
+
export declare type Handler<E, R> = (event: E) => Promise<HandlerResponse<R>>;
|
|
28
|
+
export interface HandlerResponse<R> {
|
|
29
|
+
statusCode?: number;
|
|
30
|
+
headers?: {
|
|
31
|
+
[header: string]: boolean | number | string;
|
|
32
|
+
};
|
|
33
|
+
isBase64Encoded?: boolean;
|
|
34
|
+
message?: string;
|
|
35
|
+
data?: R;
|
|
36
|
+
rawResult?: boolean;
|
|
37
|
+
}
|
|
38
|
+
export declare type BaseRecord = Omit<SQSRecord, 'body' | 'messageAttributes'>;
|
|
39
|
+
export interface MessageEvent<RecordType extends BaseRecord> {
|
|
40
|
+
Records: RecordType[];
|
|
41
|
+
}
|
|
42
|
+
export declare type SQSWrapperArgs<RecordType extends BaseRecord> = {
|
|
43
|
+
event: SQSEvent;
|
|
44
|
+
context?: Context;
|
|
45
|
+
schema?: Joi.ObjectSchema<MessageEvent<RecordType>>;
|
|
46
|
+
handler?: (e: MessageEvent<RecordType>) => void | Promise<void>;
|
|
47
|
+
singleHandler?: (record: RecordType) => void | Promise<void>;
|
|
48
|
+
mode?: 'serial' | 'parallel';
|
|
49
|
+
};
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,16 +1,16 @@
|
|
|
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;
|
|
6
|
-
export declare const iccidSchema: Joi.StringSchema;
|
|
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
|
-
}
|
|
15
|
-
export declare const SpytecJoi: SpytecJoi;
|
|
16
|
-
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;
|
|
6
|
+
export declare const iccidSchema: Joi.StringSchema;
|
|
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
|
+
}
|
|
15
|
+
export declare const SpytecJoi: SpytecJoi;
|
|
16
|
+
export {};
|
|
@@ -1,103 +1,122 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import validateEvent from './validateEvent';
|
|
2
|
-
export { validateEvent };
|
|
3
|
-
export * from './custom';
|
|
4
|
-
export * from './requestContext';
|
|
1
|
+
import validateEvent from './validateEvent';
|
|
2
|
+
export { validateEvent };
|
|
3
|
+
export * from './custom';
|
|
4
|
+
export * from './requestContext';
|
package/dist/validation/index.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
exports
|
|
16
|
-
|
|
17
|
-
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.validateEvent = void 0;
|
|
17
|
+
var validateEvent_1 = __importDefault(require("./validateEvent"));
|
|
18
|
+
exports.validateEvent = validateEvent_1.default;
|
|
19
|
+
__exportStar(require("./custom"), exports);
|
|
20
|
+
__exportStar(require("./requestContext"), exports);
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import * as Joi from 'joi';
|
|
2
|
-
import { AuthClass, SpytecAuthContext } from '../types';
|
|
3
|
-
import { APIGatewayEventRequestContextWithAuthorizer } from 'aws-lambda/common/api-gateway';
|
|
4
|
-
interface GetAuthorizerValidatorParams {
|
|
5
|
-
scope?: string;
|
|
6
|
-
type?: AuthClass;
|
|
7
|
-
}
|
|
8
|
-
export declare const getAuthorizerValidator: ({ scope, type }?: GetAuthorizerValidatorParams) => Joi.ObjectSchema<SpytecAuthContext>;
|
|
9
|
-
/**
|
|
10
|
-
* @deprecated
|
|
11
|
-
*/
|
|
12
|
-
export declare const requestContextValidator: Joi.ObjectSchema<any>;
|
|
13
|
-
export declare const getRequestContextValidator: (params?: GetAuthorizerValidatorParams) => Joi.ObjectSchema<APIGatewayEventRequestContextWithAuthorizer<SpytecAuthContext>>;
|
|
14
|
-
export {};
|
|
1
|
+
import * as Joi from 'joi';
|
|
2
|
+
import { AuthClass, SpytecAuthContext } from '../types';
|
|
3
|
+
import { APIGatewayEventRequestContextWithAuthorizer } from 'aws-lambda/common/api-gateway';
|
|
4
|
+
interface GetAuthorizerValidatorParams {
|
|
5
|
+
scope?: string;
|
|
6
|
+
type?: AuthClass;
|
|
7
|
+
}
|
|
8
|
+
export declare const getAuthorizerValidator: ({ scope, type }?: GetAuthorizerValidatorParams) => Joi.ObjectSchema<SpytecAuthContext>;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated
|
|
11
|
+
*/
|
|
12
|
+
export declare const requestContextValidator: Joi.ObjectSchema<any>;
|
|
13
|
+
export declare const getRequestContextValidator: (params?: GetAuthorizerValidatorParams) => Joi.ObjectSchema<APIGatewayEventRequestContextWithAuthorizer<SpytecAuthContext>>;
|
|
14
|
+
export {};
|