@carecard/common-util 1.0.1 → 2.0.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.
@@ -0,0 +1,77 @@
1
+ /**
2
+ * The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource.
3
+ * It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full
4
+ * response if the content was not changed. Additionally, etags help to prevent simultaneous updates of
5
+ * a resource from overwriting each other ("mid-air collisions").
6
+ *
7
+ * If the resource at a given URL changes, a new Etag value must be generated. A comparison of them can
8
+ * determine whether two representations of a resource are the same. Etags are therefore similar to fingerprints, a
9
+ * nd might also be used for tracking purposes by some servers. They might also be set to persist indefinitely
10
+ * by a tracking server.
11
+ *
12
+ * For example, when editing a wiki, the current wiki content may be hashed and put into an Etag header
13
+ * in the response:
14
+ *
15
+ * ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
16
+ *
17
+ * When saving changes to a wiki page (posting data), the POST request will contain the If-Match header containing
18
+ * the ETag values to check freshness against.
19
+ *
20
+ * If-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"
21
+ *
22
+ * If the hashes don't match, it means that the document has been edited in-between and a 412 Precondition Failed
23
+ * error is thrown.
24
+ *
25
+ * @param res
26
+ * @param ETag
27
+ * @returns {*}
28
+ */
29
+ function setOk200( res, ETag ) {
30
+
31
+ res.set( { ETag: ETag } );
32
+ res.status( 200 );
33
+
34
+ return res;
35
+ }
36
+
37
+ /**
38
+ * The 201 (Created) status code indicates that the request has been
39
+ * fulfilled and has resulted in one or more new resources being
40
+ * created. The primary resource created by the request is identified
41
+ * by either a Location header field in the response or, if no Location
42
+ * field is received, by the effective request URI.
43
+ *
44
+ * The 201 response payload typically describes and links to the
45
+ * resource(s) created. See
46
+ * https://datatracker.ietf.org/doc/html/rfc7231#section-7.2
47
+ * for a discussion of the meaning and purpose of validator header
48
+ * fields, such as ETag and Last-Modified, in a 201 response.
49
+ *
50
+ * @param res
51
+ * @returns {*}
52
+ */
53
+ function setCreated201( res ) {
54
+ res.status( 201 );
55
+ return res;
56
+ }
57
+
58
+ /**
59
+ * The 400 (Bad Request) status code indicates that the server cannot or
60
+ * will not process the request due to something that is perceived to be
61
+ * a client error (e.g., malformed request syntax, invalid request
62
+ * message framing, or deceptive request routing).
63
+ *
64
+ * @param res
65
+ * @returns {*}
66
+ */
67
+ function setBadRequest400ClientError( res ) {
68
+ res.status( 400 );
69
+ return res;
70
+ }
71
+
72
+ module.exports = {
73
+ setOk200,
74
+ setCreated201,
75
+ setBadRequest400ClientError
76
+ };
77
+
@@ -0,0 +1,18 @@
1
+ // Create new object only with properties given in array.
2
+ function extractObjectWithProperties( obj, arrayOfProperties ) {
3
+ let returnObj = {};
4
+
5
+ arrayOfProperties.forEach( nameOfProperty => {
6
+ if ( obj?.[ nameOfProperty ] ) {
7
+ returnObj[ nameOfProperty ] = obj?.[ nameOfProperty ];
8
+ }
9
+ } );
10
+
11
+ return returnObj;
12
+ }
13
+
14
+
15
+
16
+ module.exports = {
17
+ extractObjectWithProperties
18
+ };
package/package.json CHANGED
@@ -1,55 +1,23 @@
1
1
  {
2
2
  "name": "@carecard/common-util",
3
- "version": "1.0.1",
4
- "private": false,
3
+ "version": "2.0.1",
4
+ "repository": "https://github.com/CareCard-ca/pkg-common-util",
5
5
  "description": "Common utility for MVC framework",
6
- "license": "ISC",
7
- "author": "PK Singh",
8
- "repository": {
9
- "type": "git",
10
- "url": "git+https:github.com/CareCard-ca/pkg-common-util.git"
11
- },
12
- "type": "module",
13
- "publishConfig": {
14
- "access": "public"
15
- },
16
- "main": "./dist/cjs/index.cjs",
17
- "module": "./dist/esm/index.js",
18
- "types": "./dist/esm/index.d.ts",
19
- "exports": {
20
- ".": {
21
- "import": "./dist/esm/index.js",
22
- "require": "./dist/cjs/index.cjs",
23
- "types": "./dist/esm/index.d.ts"
24
- }
25
- },
26
- "files": [
27
- "dist"
28
- ],
6
+ "main": "index.js",
29
7
  "scripts": {
30
- "build": "npm run build:esm && npm run build:cjs",
31
- "build:esm": "tsc -p tsconfig.esm.json",
32
- "build:cjs": "tsc -p tsconfig.cjs.json && node ./scripts/rename-cjs.js",
33
- "test": "NODE_NO_WARNINGS=1 jest --coverage",
34
- "format": "prettier --write .",
35
- "format:check": "prettier --check .",
36
- "lint": "eslint",
37
- "prepare": "husky"
8
+ "test": "export NODE_ENV=test && mocha --watch --recursive"
38
9
  },
10
+ "keywords": [
11
+ "validate",
12
+ "data"
13
+ ],
14
+ "author": "CareCard team",
15
+ "license": "ISC",
39
16
  "devDependencies": {
40
- "@types/express": "5.0.6",
41
- "@types/jest": "30.0.0",
42
- "@types/supertest": "^6.0.3",
43
- "eslint": "9.39.2",
44
- "husky": "9.1.7",
45
- "jest": "30.2.0",
46
- "prettier": "3.7.4",
47
- "supertest": "7.1.4",
48
- "ts-jest": "29.4.6",
49
- "typescript": "5.9.3",
50
- "typescript-eslint": "8.50.1"
17
+ "mocha": "11.7.5",
18
+ "supertest": "7.1.4"
51
19
  },
52
20
  "dependencies": {
53
- "express": "5.2.1"
21
+ "express": "5.1.0"
54
22
  }
55
23
  }
package/readme.md CHANGED
@@ -1,3 +1,7 @@
1
- # pkg-common-util
1
+ #Common Utility
2
+ ### Functions
2
3
 
3
- These are common utility functions for express application.
4
+ Extract specific attributes from an object
5
+ ```js
6
+ extractObjectWithProperties( obj, arrayOfProperties )
7
+ ```