diff --git a/lib/KnexCrudHandler.ts b/lib/KnexCrudHandler.ts index d40b1e8..c83aaf5 100644 --- a/lib/KnexCrudHandler.ts +++ b/lib/KnexCrudHandler.ts @@ -1,4 +1,6 @@ import { Request, Response } from 'express'; +import { http } from '@scode/consts'; +import { try_parse_json } from '@scode/utilities'; import { KnexCrudOptions } from './KnexCrudOptions'; import { CrudHandler } from './CrudHandler'; @@ -17,19 +19,44 @@ export class KnexCrudHandler implements CrudHandler { this.options = options; } - private call_auth (auth?: Function, req: Request, res: Response): boolean { + private call_auth ( + auth?: Function, + req: Request, + res: Response + ): Promise { if (typeof auth === 'undefined') return true; const promise = new Promise ((resolve) => { const result = auth (req, res, resolve); + if (typeof result !== 'undefined') + resolve (result); }); + + return promise; + } + + protected validate_body ( + req: Request, + res: Response + ): Promise | object { + if (typeof req.body === 'undefined') { + res.status (http.status_bad_request); + res.end (); + return null; + } + return try_parse_json (req.body); } public async create (req: Request, res: Response): Promise { - if (typeof req.body === 'undefined') - throw new Error ('request body is undefined. is body-parser running?'); - const data = JSON.parse (req.body); + if (!await this.call_auth (this.options.create_authentication, req, res)) + return; + if (!await this.call_auth (this.options.create_authorization, req, res)) + return; + + const body_data = await this.validate_body (req, res); + if (body_data === null) + return; } public async read (req: Request, res: Response): Promise { diff --git a/lib/KnexCrudOptions.ts b/lib/KnexCrudOptions.ts index 886aa61..50ef151 100644 --- a/lib/KnexCrudOptions.ts +++ b/lib/KnexCrudOptions.ts @@ -2,12 +2,12 @@ import { Request, Response } from 'express'; type Authentication = { (req: Request, res: Response, next: Function); - (req: Request, res: Response): boolean; + (req: Request, res: Response): Promise; } type Authorization = { (req: Request, res: Response, next: Function); - (req: Request, res: Response): boolean; + (req: Request, res: Response): Promise; } export class KnexCrudOptions { diff --git a/package.json b/package.json index 1a2b746..20605fe 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ }, "dependencies": { "@scode/consts": "^1.1.4", + "@scode/utilities": "^1.0.21", "@types/express": "^4.17.6", "express": "^4.17.1" } diff --git a/yarn.lock b/yarn.lock index a917cc3..883b1a2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -130,9 +130,9 @@ integrity sha512-KvrycsolDIkNKtTHnXFXk/Mnu9wXM+7ymZwRIOPhP8xtHDAQgNUyD94HkgCSG88DSNyCrgqE4JoWvdVrFz5h6g== "@scode/eslint-config-es6@^1.0.1": - version "1.0.18" - resolved "https://npm.scode.ovh/@scode%2feslint-config-es6/-/eslint-config-es6-1.0.18.tgz#b053581fb44b4a4afb12225ef50344d8583c2719" - integrity sha512-WIfMXOIoPBNUvIsV/xc1k900bXoN2tyITnzRxvyD0qaaZkFbv7rYPmcKv6XIYQNoN1030zDEn9dvvOdU67IkfQ== + version "1.0.20" + resolved "https://npm.scode.ovh/@scode%2feslint-config-es6/-/eslint-config-es6-1.0.20.tgz#b4a5deeab8168a73233b41149134d85bb075638f" + integrity sha512-h00GK6F5jycy7OmmXa9QHHIbv1x7DmMBuwLJSpl+lJD+pi7AUCCQv7oHmZ8autCZXXC9/8bPwJWrH9udIuiujg== dependencies: "@scode/eslint-config" "^2.0.1" eslint-plugin-import "^2.20.1" @@ -147,14 +147,19 @@ "@typescript-eslint/parser" "^2.26.0" "@scode/eslint-config@^2.0.1": - version "2.0.8" - resolved "https://npm.scode.ovh/@scode%2feslint-config/-/eslint-config-2.0.8.tgz#b96db031fe83ad353dc29e0ea95e4a63a0b1ed3f" - integrity sha512-rA/W4r2xyapet962Bz9XQEtZyL21KySmgsy7rN4qIg7yUFor7VMIHYv9mXTvv/KLSFUapmMrwvR1WpoiU57pEQ== + version "2.0.10" + resolved "https://npm.scode.ovh/@scode%2feslint-config/-/eslint-config-2.0.10.tgz#a154d30de47893046cda34925c191a88f400ddc9" + integrity sha512-6BEMBK8VNPX+9FWkBhXzCWQ6j0l0JTS0F0UTUcPnzefau48WYlFZyORtyIc8H0OvYaeeLUD1gUNjLVsKjwTPHg== dependencies: - eslint-plugin-jsdoc "22" + eslint-plugin-jsdoc "^23.0.0" eslint-plugin-node "^11.0.0" eslint-plugin-sort-requires-by-path "^1.0.2" +"@scode/utilities@^1.0.21": + version "1.0.21" + resolved "https://npm.scode.ovh/@scode%2futilities/-/utilities-1.0.21.tgz#adbc50720515df16fc05bc97b56cb9ba35499e0a" + integrity sha512-2F5mSTN7/2w5YQgamp2nan6q75Nln6bjAVBOvTrrQMbhWkZnieKzgt3g9M8GnsICVefmk5IPguiZhWPVYLmQxw== + "@stryker-mutator/api@^3.1.0": version "3.1.0" resolved "https://registry.yarnpkg.com/@stryker-mutator/api/-/api-3.1.0.tgz#7eb6f1e1f2af17ff0425c6aa0d4d244ca822e972" @@ -236,9 +241,9 @@ integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag== "@types/express-serve-static-core@*": - version "4.17.4" - resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.4.tgz#157c79c2d28b632d6418497c57c93185e392e444" - integrity sha512-dPs6CaRWxsfHbYDVU51VjEJaUJEcli4UI0fFMT4oWmgCvHj+j7oIxz5MLHVL0Rv++N004c21ylJNdWQvPkkb5w== + version "4.17.5" + resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.5.tgz#a00ac7dadd746ae82477443e4d480a6a93ea083c" + integrity sha512-578YH5Lt88AKoADy0b2jQGwJtrBxezXtVe/MBqWXKZpqx91SnC0pVkVCcxcytz3lWW+cHBYDi3Ysh0WXc+rAYw== dependencies: "@types/node" "*" "@types/range-parser" "*" @@ -287,39 +292,39 @@ "@types/mime" "*" "@typescript-eslint/eslint-plugin@^2.26.0": - version "2.27.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.27.0.tgz#e479cdc4c9cf46f96b4c287755733311b0d0ba4b" - integrity sha512-/my+vVHRN7zYgcp0n4z5A6HAK7bvKGBiswaM5zIlOQczsxj/aiD7RcgD+dvVFuwFaGh5+kM7XA6Q6PN0bvb1tw== + version "2.28.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.28.0.tgz#4431bc6d3af41903e5255770703d4e55a0ccbdec" + integrity sha512-w0Ugcq2iatloEabQP56BRWJowliXUP5Wv6f9fKzjJmDW81hOTBxRoJ4LoEOxRpz9gcY51Libytd2ba3yLmSOfg== dependencies: - "@typescript-eslint/experimental-utils" "2.27.0" + "@typescript-eslint/experimental-utils" "2.28.0" functional-red-black-tree "^1.0.1" regexpp "^3.0.0" tsutils "^3.17.1" -"@typescript-eslint/experimental-utils@2.27.0": - version "2.27.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.27.0.tgz#801a952c10b58e486c9a0b36cf21e2aab1e9e01a" - integrity sha512-vOsYzjwJlY6E0NJRXPTeCGqjv5OHgRU1kzxHKWJVPjDYGbPgLudBXjIlc+OD1hDBZ4l1DLbOc5VjofKahsu9Jw== +"@typescript-eslint/experimental-utils@2.28.0": + version "2.28.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.28.0.tgz#1fd0961cd8ef6522687b4c562647da6e71f8833d" + integrity sha512-4SL9OWjvFbHumM/Zh/ZeEjUFxrYKtdCi7At4GyKTbQlrj1HcphIDXlje4Uu4cY+qzszR5NdVin4CCm6AXCjd6w== dependencies: "@types/json-schema" "^7.0.3" - "@typescript-eslint/typescript-estree" "2.27.0" + "@typescript-eslint/typescript-estree" "2.28.0" eslint-scope "^5.0.0" eslint-utils "^2.0.0" "@typescript-eslint/parser@^2.26.0": - version "2.27.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.27.0.tgz#d91664335b2c46584294e42eb4ff35838c427287" - integrity sha512-HFUXZY+EdwrJXZo31DW4IS1ujQW3krzlRjBrFRrJcMDh0zCu107/nRfhk/uBasO8m0NVDbBF5WZKcIUMRO7vPg== + version "2.28.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.28.0.tgz#bb761286efd2b0714761cab9d0ee5847cf080385" + integrity sha512-RqPybRDquui9d+K86lL7iPqH6Dfp9461oyqvlXMNtap+PyqYbkY5dB7LawQjDzot99fqzvS0ZLZdfe+1Bt3Jgw== dependencies: "@types/eslint-visitor-keys" "^1.0.0" - "@typescript-eslint/experimental-utils" "2.27.0" - "@typescript-eslint/typescript-estree" "2.27.0" + "@typescript-eslint/experimental-utils" "2.28.0" + "@typescript-eslint/typescript-estree" "2.28.0" eslint-visitor-keys "^1.1.0" -"@typescript-eslint/typescript-estree@2.27.0": - version "2.27.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.27.0.tgz#a288e54605412da8b81f1660b56c8b2e42966ce8" - integrity sha512-t2miCCJIb/FU8yArjAvxllxbTiyNqaXJag7UOpB5DVoM3+xnjeOngtqlJkLRnMtzaRcJhe3CIR9RmL40omubhg== +"@typescript-eslint/typescript-estree@2.28.0": + version "2.28.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.28.0.tgz#d34949099ff81092c36dc275b6a1ea580729ba00" + integrity sha512-HDr8MP9wfwkiuqzRVkuM3BeDrOC4cKbO5a6BymZBHUt5y/2pL0BXD6I/C/ceq2IZoHWhcASk+5/zo+dwgu9V8Q== dependencies: debug "^4.1.1" eslint-visitor-keys "^1.1.0" @@ -492,9 +497,9 @@ cli-cursor@^3.1.0: restore-cursor "^3.1.0" cli-width@^2.0.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639" - integrity sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk= + version "2.2.1" + resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.1.tgz#b0433d0b4e9c847ef18868a4ef16fd5fc8271c48" + integrity sha512-GRMWDxpOB6Dgk2E5Uo+3eEBvtOOlimMmpbFiKuLFnQzYDavtLFY3K5ona41jgN/WdRZtG7utuVSVTL4HbZHGkw== color-convert@^1.9.0: version "1.9.3" @@ -739,10 +744,10 @@ eslint-plugin-import@^2.20.1: read-pkg-up "^2.0.0" resolve "^1.12.0" -eslint-plugin-jsdoc@22: - version "22.1.0" - resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-22.1.0.tgz#dadfa62653fc0d87f900d810307f5ed07ef6ecd5" - integrity sha512-54NdbICM7KrxsGUqQsev9aIMqPXyvyBx2218Qcm0TQ16P9CtBI+YY4hayJR6adrxlq4Ej0JLpgfUXWaQVFqmQg== +eslint-plugin-jsdoc@^23.0.0: + version "23.0.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-23.0.0.tgz#f80dca482fc9d93a9e30b3ead70b88dee261caa3" + integrity sha512-zj5ZephjKkFU/J9hEw3RcjwpuywChvwNMgHs2DTgOuKarpJ65SJU3JGgx/K4y9l8iFw0ysrk6NlAKDX88ZwZdw== dependencies: comment-parser "^0.7.2" debug "^4.1.1" @@ -854,11 +859,11 @@ esprima@^4.0.0: integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A== esquery@^1.0.1: - version "1.2.0" - resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.2.0.tgz#a010a519c0288f2530b3404124bfb5f02e9797fe" - integrity sha512-weltsSqdeWIX9G2qQZz7KlTRJdkkOCTPgLYJUz1Hacf48R4YOwGPHO3+ORfWedqJKbq5WQmsgK90n+pFLIKt/Q== + version "1.3.1" + resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.3.1.tgz#b78b5828aa8e214e29fb74c4d5b752e1c033da57" + integrity sha512-olpvt9QG0vniUBZspVRN6lwB7hOZoTRtT+jzR+tS4ffYx2mzbw+z0XCOk44aaLYKApNX5nMm+E+P6o25ip/DHQ== dependencies: - estraverse "^5.0.0" + estraverse "^5.1.0" esrecurse@^4.1.0: version "4.2.1" @@ -872,10 +877,10 @@ estraverse@^4.1.0, estraverse@^4.1.1: resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.3.0.tgz#398ad3f3c5a24948be7725e83d11a7de28cdbd1d" integrity sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw== -estraverse@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.0.0.tgz#ac81750b482c11cca26e4b07e83ed8f75fbcdc22" - integrity sha512-j3acdrMzqrxmJTNj5dbr1YbjacrYgAxVMeF0gK16E3j494mOe7xygM/ZLIguEQ0ETwAg2hlJCtHRGav+y0Ny5A== +estraverse@^5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.1.0.tgz#374309d39fd935ae500e7b92e8a6b4c720e59642" + integrity sha512-FyohXK+R0vE+y1nHLoBM7ZTyqRpqAlhdZHCWIWEviFLiGB8b04H6bQs8G+XTthacvT8VuwvteiP7RJSxMs8UEw== esutils@^2.0.2: version "2.0.3" @@ -1738,9 +1743,9 @@ resolve-from@^4.0.0: integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g== resolve@^1.10.0, resolve@^1.10.1, resolve@^1.12.0, resolve@^1.13.1: - version "1.15.1" - resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.15.1.tgz#27bdcdeffeaf2d6244b95bb0f9f4b4653451f3e8" - integrity sha512-84oo6ZTtoTUpjgNEr5SJyzQhzL72gaRodsSfyxC/AXRvwu0Yse9H8eF9IpGo7b8YetZhlI6v7ZQ6bKBFV/6S7w== + version "1.16.0" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.16.0.tgz#063dc704fa3413e13ac1d0d1756a7cbfe95dd1a7" + integrity sha512-LarL/PIKJvc09k1jaeT4kQb/8/7P+qV4qSnN2K80AES+OHdfZELAKVOBjxsvtToT/uLOfFbvYvKfZmV8cee7nA== dependencies: path-parse "^1.0.6" @@ -1939,9 +1944,9 @@ string-width@^4.1.0: strip-ansi "^6.0.0" string.prototype.trimend@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/string.prototype.trimend/-/string.prototype.trimend-1.0.0.tgz#ee497fd29768646d84be2c9b819e292439614373" - integrity sha512-EEJnGqa/xNfIg05SxiPSqRS7S9qwDhYts1TSLR1BQfYUfPe1stofgGKvwERK9+9yf+PpfBMlpBaCHucXGPQfUA== + version "1.0.1" + resolved "https://registry.yarnpkg.com/string.prototype.trimend/-/string.prototype.trimend-1.0.1.tgz#85812a6b847ac002270f5808146064c995fb6913" + integrity sha512-LRPxFUaTtpqYsTeNKaFOw3R4bxIzWOnbQ837QfBylo8jIxtcbK/A/sMV7Q+OAV/vWo+7s25pOE10KYSjaSO06g== dependencies: define-properties "^1.1.3" es-abstract "^1.17.5" @@ -1965,9 +1970,9 @@ string.prototype.trimright@^2.1.1: string.prototype.trimend "^1.0.0" string.prototype.trimstart@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/string.prototype.trimstart/-/string.prototype.trimstart-1.0.0.tgz#afe596a7ce9de905496919406c9734845f01a2f2" - integrity sha512-iCP8g01NFYiiBOnwG1Xc3WZLyoo+RuBymwIlWncShXDDJYWN6DbnM3odslBJdgCdRlq94B5s63NWAZlcn2CS4w== + version "1.0.1" + resolved "https://registry.yarnpkg.com/string.prototype.trimstart/-/string.prototype.trimstart-1.0.1.tgz#14af6d9f34b053f7cfc89b72f8f2ee14b9039a54" + integrity sha512-XxZn+QpvrBI1FOcg6dIpxUPgWCPuNXvMD72aaRaUQv1eD4e/Qy8i/hFTe0BUmD60p/QA6bh1avmuPTfNjqVWRw== dependencies: define-properties "^1.1.3" es-abstract "^1.17.5"