core-express 0.1.0 → 0.1.1

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/src/view.ts CHANGED
@@ -1,59 +1,73 @@
1
- import {Request} from 'express';
2
- import {Attribute, Attributes} from './metadata';
1
+ import { Request, Response } from "express"
2
+ import { Attribute, Attributes } from "./metadata"
3
3
 
4
- export function buildId<T>(req: Request, attrs?: Attribute[]): T {
5
- if (!attrs) {
6
- const id = req.params['id'];
4
+ export function buildAndCheckId<ID>(req: Request, res: Response, keys?: Attribute[]): ID | undefined {
5
+ const id = buildId<ID>(req, keys)
6
+ if (!id) {
7
+ res.status(400).end("invalid parameters")
8
+ return undefined
9
+ }
10
+ return id
11
+ }
12
+ export function buildId<T>(req: Request, attrs?: Attribute[]): T | undefined {
13
+ if (!attrs || attrs.length === 0) {
14
+ const id = req.params["id"] as string
7
15
  if (id && id.length > 0) {
8
- return id as any;
16
+ return id as any
9
17
  }
10
- return null;
18
+ return undefined
11
19
  }
12
20
  if (attrs && attrs.length === 1) {
13
- const key = (attrs[0].name ? attrs[0].name : 'id');
14
- const id = req.params[key];
21
+ let id = req.params["id"] as string
22
+ const n = attrs[0].name
23
+ if ((!id || id.length === 0) && n && n.length > 0) {
24
+ id = req.params[n] as string
25
+ }
15
26
  if (id && id.length > 0) {
16
- if (attrs[0].type === 'integer' || attrs[0].type === 'number') {
27
+ if (attrs[0].type === "integer" || attrs[0].type === "number") {
17
28
  if (isNaN(id as any)) {
18
- return null;
29
+ return undefined
19
30
  }
20
- const v = parseFloat(id);
21
- return v as any;
31
+ const v = parseFloat(id)
32
+ return v as any
22
33
  }
23
- return id as any;
34
+ return id as any
24
35
  }
25
36
  }
26
- const ids: any = {};
37
+ const ids: any = {}
27
38
  for (const attr of attrs) {
28
- const v = req.params[attr.name];
39
+ if (!attr.name) {
40
+ return undefined
41
+ }
42
+ const v = req.params[attr.name]
29
43
  if (!v) {
30
- return null;
44
+ return undefined
31
45
  }
32
- if (attr.type === 'integer' || attr.type === 'number') {
46
+ if (attr.type === "integer" || attr.type === "number") {
33
47
  if (isNaN(v as any)) {
34
- return null;
48
+ return undefined
35
49
  }
36
- ids[attr.name] = parseFloat(v);
50
+ ids[attr.name] = parseFloat(v as string)
37
51
  } else {
38
- ids[attr.name] = v;
52
+ ids[attr.name] = v
39
53
  }
40
- return ids;
54
+ return ids
41
55
  }
42
56
  }
43
- export function buildKeys(attrs: Attributes): Attribute[] {
57
+ export function buildKeys(attrs: Attributes): Attribute[] | undefined {
44
58
  if (!attrs) {
45
- return undefined;
59
+ return undefined
46
60
  }
47
- const keys: string[] = Object.keys(attrs);
48
- const ats: Attribute[] = [];
61
+ const keys: string[] = Object.keys(attrs)
62
+ const ats: Attribute[] = []
49
63
  for (const key of keys) {
50
- const attr: Attribute = attrs[key];
64
+ const attr: Attribute = attrs[key]
51
65
  if (attr) {
52
66
  if (attr.key === true) {
53
- const at: Attribute = {name: key, type: attr.type};
54
- ats.push(at);
67
+ const at: Attribute = { name: key, type: attr.type }
68
+ ats.push(at)
55
69
  }
56
70
  }
57
71
  }
58
- return ats;
72
+ return ats
59
73
  }
package/tsconfig.json CHANGED
@@ -6,6 +6,7 @@
6
6
  "outDir": "./lib",
7
7
  "module": "commonjs",
8
8
  "moduleResolution": "node",
9
+ "strict": true,
9
10
  "pretty": true,
10
11
  "sourceMap": false,
11
12
  "declaration": false,
package/lib/response.js DELETED
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- function handleError(err, res, log) {
4
- if (log) {
5
- log(err);
6
- res.status(500).end('Internal Server Error');
7
- }
8
- else {
9
- res.status(500).end(err);
10
- }
11
- }
12
- exports.handleError = handleError;
package/src/response.ts DELETED
@@ -1,10 +0,0 @@
1
- import {Response} from 'express';
2
-
3
- export function handleError(err: any, res: Response, log?: (msg: string, ctx?: any) => void) {
4
- if (log) {
5
- log(err as any);
6
- res.status(500).end('Internal Server Error');
7
- } else {
8
- res.status(500).end(err);
9
- }
10
- }