@spytecgps/lambda-utils 0.0.2
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/README.md +5 -0
- package/dist/errors/BadRequestError.d.ts +3 -0
- package/dist/errors/NotFoundError.d.ts +4 -0
- package/dist/errors/index.d.ts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/response.d.ts +12 -0
- package/dist/types.d.ts +18 -0
- package/dist/wrapper.d.ts +4 -0
- package/package.json +56 -0
package/README.md
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports["jarvis-events-sdk"]=r():e["jarvis-events-sdk"]=r()}(global,(function(){return function(e){var r={};function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var o in e)t.d(n,o,function(r){return e[r]}.bind(null,o));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(e,r,t){"use strict";t.r(r),t.d(r,"wrapper",(function(){return a})),t.d(r,"errors",(function(){return n}));var n={};t.r(n),t.d(n,"BadRequestError",(function(){return o})),t.d(n,"NotFoundError",(function(){return s}));class o extends Error{constructor(){super(...arguments),this.code=400}}class s extends Error{constructor(){super(...arguments),this.code=404,this.message="Not found"}}const u={"Content-Type":"application/json"},c=(e,r,t)=>{const n=((e,r,t)=>({success:e<400,message:r,result:t}))(e,r,t);return{headers:Object.assign({},u),statusCode:e,body:n&&JSON.stringify(n)}};var a=async({event:e,schema:r,handler:t})=>{console.log("wrapper - got event",e);try{const{error:n,value:s}=((e,r)=>e.validate(r,{allowUnknown:!0,errors:{label:"key",wrap:{label:!1}}}))(r,e);if(n)throw new o(n.message);const u=await t(s);return c(200,"ok",u)}catch(e){return console.error("wrapper - caught error",e),c(e.code||500,e.message||"Error")}}}])}));
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["webpack://jarvis-events-sdk/webpack/universalModuleDefinition","webpack://jarvis-events-sdk/webpack/bootstrap","webpack://jarvis-events-sdk/./src/errors/BadRequestError.ts","webpack://jarvis-events-sdk/./src/errors/NotFoundError.ts","webpack://jarvis-events-sdk/./src/response.ts","webpack://jarvis-events-sdk/./src/wrapper.ts"],"names":["root","factory","exports","module","define","amd","global","installedModules","__webpack_require__","moduleId","i","l","modules","call","m","c","d","name","getter","o","Object","defineProperty","enumerable","get","r","Symbol","toStringTag","value","t","mode","__esModule","ns","create","key","bind","n","object","property","prototype","hasOwnProperty","p","s","BadRequestError","Error","code","NotFoundError","message","baseHeaders","buildResponse","data","resp","success","result","apiResponse","headers","statusCode","body","JSON","stringify","async","event","schema","handler","console","log","error","validatedEvent","validate","allowUnknown","errors","label","wrap","doValidation","err"],"mappings":"CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,IACQ,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,GACe,iBAAZC,QACdA,QAAQ,qBAAuBD,IAE/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SAC/C,WAA8B,OAAOA,GAEtC,OADAK,EAAoBQ,EAAEE,EAAQ,IAAKA,GAC5BA,GAIRV,EAAoBW,EAAI,SAASiB,EAAQC,GAAY,OAAOjB,OAAOkB,UAAUC,eAAe1B,KAAKuB,EAAQC,IAGzG7B,EAAoBgC,EAAI,GAIjBhC,EAAoBA,EAAoBiC,EAAI,G,mOClFtC,MAAMC,UAAwBC,MAA7C,c,oBACE,KAAAC,KAAO,KCDM,MAAMC,UAAsBF,MAA3C,c,oBACE,KAAAC,KAAO,IACP,KAAAE,QAAU,aCFL,MAQDC,EAAc,CAClB,eAAgB,oBAGLC,EAAgB,CAACJ,EAAcE,EAAiBG,KAC3D,MAAMC,EAbmB,EAAIN,EAAcE,EAAiBG,KACrD,CACLE,QAASP,EAAO,IAChBE,UACAM,OAAQH,IASGI,CAAYT,EAAME,EAASG,GACxC,MAAO,CACLK,QAAS,OAAF,UAAOP,GACdQ,WAAYX,EACZY,KAAMN,GAAQO,KAAKC,UAAUR,KCelB,MAfCS,OAAqBC,QAAOC,SAAQC,cAClDC,QAAQC,IAAI,sBAAuBJ,GACnC,IACE,MAAM,MAAEK,EAAOtC,MAAOuC,GAdL,EAAIL,EAAyBD,IAChDC,EAAOM,SAASP,EAAO,CACrBQ,cAAc,EACdC,OAAQ,CACNC,MAAO,MACPC,KAAM,CACJD,OAAO,MAQ8BE,CAAgBX,EAAQD,GACjE,GAAIK,EACF,MAAM,IAAIvB,EAAgBuB,EAAMnB,SAElC,MAAMM,QAAeU,EAAQI,GAC7B,OAAOlB,EAAc,IAAK,KAAMI,GAChC,MAAOqB,GAEP,OADAV,QAAQE,MAAM,yBAA0BQ,GACjCzB,EAAeyB,EAAI7B,MAAmB,IAAK6B,EAAI3B,SAAW","file":"index.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"jarvis-events-sdk\"] = factory();\n\telse\n\t\troot[\"jarvis-events-sdk\"] = factory();\n})(global, function() {\nreturn "," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n","export default class BadRequestError extends Error {\r\n code = 400;\r\n}\r\n","export default class NotFoundError extends Error {\r\n code = 404;\r\n message = 'Not found';\r\n}\r\n","export const apiResponse = <T>(code: number, message: string, data?: T) => {\r\n return {\r\n success: code < 400,\r\n message,\r\n result: data,\r\n };\r\n};\r\n\r\nconst baseHeaders = {\r\n 'Content-Type': 'application/json',\r\n};\r\n\r\nexport const buildResponse = (code: number, message: string, data?: any) => {\r\n const resp = apiResponse(code, message, data);\r\n return {\r\n headers: { ...baseHeaders },\r\n statusCode: code,\r\n body: resp && JSON.stringify(resp),\r\n };\r\n};\r\n","import { APIGatewayProxyResult } from 'aws-lambda';\r\nimport { ObjectSchema } from 'joi';\r\nimport { BadRequestError } from './errors';\r\nimport { buildResponse } from './response';\r\nimport { FullValidationResult, WrapperArgs } from './types';\r\n\r\nconst doValidation = <T>(schema: ObjectSchema<T>, event): FullValidationResult<T> =>\r\n schema.validate(event, {\r\n allowUnknown: true,\r\n errors: {\r\n label: 'key',\r\n wrap: {\r\n label: false,\r\n },\r\n },\r\n }) as FullValidationResult<T>;\r\n\r\nconst wrapper = async <T, R = any>({ event, schema, handler }: WrapperArgs<T, R>): Promise<APIGatewayProxyResult> => {\r\n console.log(`wrapper - got event`, event);\r\n try {\r\n const { error, value: validatedEvent } = doValidation<T>(schema, event);\r\n if (error) {\r\n throw new BadRequestError(error.message);\r\n }\r\n const result = await handler(validatedEvent);\r\n return buildResponse(200, 'ok', result);\r\n } catch (err) {\r\n console.error(`wrapper - caught error`, err);\r\n return buildResponse((err.code as number) || 500, err.message || 'Error');\r\n }\r\n};\r\n\r\nexport default wrapper;\r\n"],"sourceRoot":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const apiResponse: <T>(code: number, message: string, data?: T) => {
|
|
2
|
+
success: boolean;
|
|
3
|
+
message: string;
|
|
4
|
+
result: T;
|
|
5
|
+
};
|
|
6
|
+
export declare const buildResponse: (code: number, message: string, data?: any) => {
|
|
7
|
+
headers: {
|
|
8
|
+
'Content-Type': string;
|
|
9
|
+
};
|
|
10
|
+
statusCode: number;
|
|
11
|
+
body: string;
|
|
12
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { APIGatewayProxyWithLambdaAuthorizerEvent } from 'aws-lambda';
|
|
2
|
+
import { ObjectSchema, ValidationResult } from 'joi';
|
|
3
|
+
export interface SpytecAuthContext {
|
|
4
|
+
type: 'user' | 'client';
|
|
5
|
+
userId?: string;
|
|
6
|
+
clientId: number;
|
|
7
|
+
resources: {
|
|
8
|
+
devices: string[];
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface WrapperArgs<T, R> {
|
|
12
|
+
event: APIGatewayProxyWithLambdaAuthorizerEvent<SpytecAuthContext>;
|
|
13
|
+
schema: ObjectSchema<T>;
|
|
14
|
+
handler: (event: T) => Promise<R>;
|
|
15
|
+
}
|
|
16
|
+
export interface FullValidationResult<T> extends Omit<ValidationResult, 'value'> {
|
|
17
|
+
value: T;
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spytecgps/lambda-utils",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Lambda Utils",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"version": "npm run format && git add -A src",
|
|
9
|
+
"postversion": "git push && git push --tags",
|
|
10
|
+
"prepublishOnly": "npm test && npm run lint",
|
|
11
|
+
"prepare": "npm run build",
|
|
12
|
+
"build-dev": "webpack --mode=development",
|
|
13
|
+
"build": "webpack --mode=production",
|
|
14
|
+
"test": "jest",
|
|
15
|
+
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
16
|
+
"lint": "eslint . --ext .ts --fix && eslint . --ext .ts",
|
|
17
|
+
"version-publish": "npm version patch && npm publish"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/spytecgps/lambda-utils"
|
|
22
|
+
},
|
|
23
|
+
"author": "@eliyahud",
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/spytecgps/lambda-utils/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/spytecgps/lambda-utils#readme",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"joi": "^17.4.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/aws-lambda": "^8.10.76",
|
|
34
|
+
"@types/jest": "^25.1.4",
|
|
35
|
+
"@types/joi": "^17.2.3",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^4.20.0",
|
|
37
|
+
"@typescript-eslint/parser": "^4.20.0",
|
|
38
|
+
"aws-sdk": "^2.919.0",
|
|
39
|
+
"eslint": "^7.23.0",
|
|
40
|
+
"eslint-config-airbnb-base": "^14.2.1",
|
|
41
|
+
"eslint-config-prettier": "^6.10.0",
|
|
42
|
+
"eslint-plugin-import": "^2.22.1",
|
|
43
|
+
"eslint-plugin-prettier": "^3.1.2",
|
|
44
|
+
"eslint-plugin-simple-import-sort": "^5.0.1",
|
|
45
|
+
"jest": "^25.1.0",
|
|
46
|
+
"prettier": "^1.19.1",
|
|
47
|
+
"ts-jest": "^25.2.1",
|
|
48
|
+
"ts-loader": "^6.2.1",
|
|
49
|
+
"typescript": "^3.8.3",
|
|
50
|
+
"webpack": "^4.46.0",
|
|
51
|
+
"webpack-cli": "^3.3.11"
|
|
52
|
+
},
|
|
53
|
+
"files": [
|
|
54
|
+
"dist/**/*"
|
|
55
|
+
]
|
|
56
|
+
}
|