@sera4/essentia 1.1.40 → 1.1.42

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 @@
1
+ export * from "./test-server-wrapper.js";
@@ -0,0 +1,170 @@
1
+ import uuidv4 from 'uuid/v4.js';
2
+ import fs from "fs";
3
+
4
+ /**
5
+ * A small wrapper around an express js server
6
+ * meant to be used in unit tests
7
+ */
8
+ export class TestServerWrapper {
9
+
10
+ constructor(apiPrefix, expressServer, defaultHeaders, oldAuthToken, routePrinter, chai) {
11
+ this.server = expressServer;
12
+ this.defaultHeaders = defaultHeaders;
13
+ this.apiPrefix = apiPrefix;
14
+ this.routePrinter = routePrinter;
15
+ this.chai = chai;
16
+
17
+ // Support for older/deprecated apis
18
+ const oldAuthHeaders = {field: "Authorization", value: `Token organization_token=${oldAuthToken}`};
19
+ this.v1 = {
20
+ get: (url) => this.getWithPrefix("/v1", url, oldAuthHeaders),
21
+ post: (url) => this.postWithPrefix("/v1", url, oldAuthHeaders),
22
+ put: (url) => this.putWithPrefix("/v1", url, oldAuthHeaders),
23
+ delete: (url) => this.deleteWithPrefix("/v1", url, oldAuthHeaders)
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Adds a leading slash to the url if missing
29
+ * @param {String} url the url to sanitize
30
+ * @returns sanitized url
31
+ */
32
+ sanitizeUrl (url) {
33
+ // add leading slash if missing
34
+ if (!url.startsWith("/")) {
35
+ return "/" + url;
36
+ } else {
37
+ return url;
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Apply given headers to the request object
43
+ * @param {Object} request
44
+ * @param {Object} headers
45
+ */
46
+ applyHeaders (request, headers) {
47
+ Object.keys(headers).map(k => {
48
+ request.set(k, headers[k]);
49
+ });
50
+ }
51
+
52
+ get (url, options) {
53
+ return this.getWithPrefix(this.apiPrefix, url, options);
54
+ }
55
+
56
+ getWithPrefix (prefix, url, options) {
57
+ const sanitizedUrl = this.sanitizeUrl(url);
58
+ const request = this.chai.request(this.server).get(`${prefix}${sanitizedUrl}`);
59
+ this.applyHeaders(request, this.defaultHeaders);
60
+
61
+ if (options?.field) {
62
+ this.applyHeaders(request, {[options.field] : options.value});
63
+ }
64
+
65
+ if (options?.body) {
66
+ request.send(options.body);
67
+ }
68
+ return request;
69
+ }
70
+
71
+ post (url, options) {
72
+ return this.postWithPrefix(this.apiPrefix, url, options);
73
+ }
74
+
75
+ postWithPrefix (prefix, url, options) {
76
+ const sanitizedUrl = this.sanitizeUrl(url);
77
+ const request = this.chai.request(this.server).post(`${prefix}${sanitizedUrl}`);
78
+ this.applyHeaders(request, this.defaultHeaders);
79
+
80
+ if (options?.field) {
81
+ this.applyHeaders(request, {[options.field] : options.value});
82
+ }
83
+ return request;
84
+ }
85
+
86
+ put (url, options) {
87
+ return this.putWithPrefix(this.apiPrefix, url, options);
88
+ }
89
+
90
+ putWithPrefix (prefix, url, options) {
91
+ const sanitizedUrl = this.sanitizeUrl(url);
92
+ const request = this.chai.request(this.server).put(`${prefix}${sanitizedUrl}`);
93
+ this.applyHeaders(request, this.defaultHeaders);
94
+
95
+ if (options?.field) {
96
+ this.applyHeaders(request, {[options.field] : options.value});
97
+ }
98
+ return request;
99
+ }
100
+
101
+ delete (url, options) {
102
+ return this.deleteWithPrefix(this.apiPrefix, url, options);
103
+ }
104
+
105
+ deleteWithPrefix (prefix, url, options) {
106
+ const sanitizedUrl = this.sanitizeUrl(url);
107
+ const request = this.chai.request(this.server).delete(`${prefix}${sanitizedUrl}`);
108
+ this.applyHeaders(request, this.defaultHeaders);
109
+
110
+ if (options?.field) {
111
+ this.applyHeaders(request, {[options.field] : options.value});
112
+ }
113
+ return request;
114
+ }
115
+
116
+ upload ( url, fields, file, options) {
117
+ return this.uploadWithPrefix(this.apiPrefix, url, fields, file, options);
118
+ }
119
+
120
+ uploadWithPrefix (prefix, url, fields, file, options) {
121
+ const sanitizedUrl = this.sanitizeUrl(url);
122
+ const request = this.chai.request(this.server).post(`${prefix}${sanitizedUrl}`);
123
+ this.applyHeaders(request, this.defaultHeaders);
124
+
125
+ if (options?.field) {
126
+ this.applyHeaders(request, {[options.field] : options.value});
127
+ }
128
+
129
+ request.attach("data", fs.readFileSync(file), {filename: "data"});
130
+ const keys = Object.keys(fields);
131
+ keys.forEach(k => {
132
+ request.field(k, fields[k]);
133
+ });
134
+ return request;
135
+ }
136
+
137
+ stop () {
138
+ this.server.close();
139
+ }
140
+
141
+ allRoutes () {
142
+ return this.routePrinter.getAll();
143
+ }
144
+
145
+ withHeader (h) {
146
+ return {
147
+ get: (url, options) => this.get(url, {...h, ...options}),
148
+ post: (url, options) => this.post(url, {...h, ...options}),
149
+ put: (url, options) => this.put(url, {...h, ...options}),
150
+ delete: (url, options) => this.delete(url, {...h, ...options}),
151
+ upload: (url, fields, file, options) => this.upload(url, fields, file, {...h, ...options}),
152
+ stop: this.stop,
153
+ allRoutes: this.allRoutes,
154
+ v1: this.v1
155
+ }
156
+ }
157
+
158
+ withMembership (m) {
159
+ const h = { field: "tws-membership", value: JSON.stringify(m) }
160
+ return this.withHeader(h);
161
+ }
162
+
163
+ withS4 () {
164
+ return this.withMembership({s4_role: 1, id: uuidv4(), account_id: uuidv4(), tenant_id: uuidv4()});
165
+ }
166
+
167
+ withTap() {
168
+ return this.withMembership({s4_role: 2, id: uuidv4(), account_id: uuidv4(), tenant_id: uuidv4()});
169
+ }
170
+ }
package/index.js CHANGED
@@ -12,3 +12,4 @@ export { default as paperTrail } from "./paper-trail/index.js";
12
12
  export { default as serializer } from "./serializer/index.js";
13
13
  export * from "./utils/index.js";
14
14
  export * from "./safe_proxy/index.js";
15
+ export * from "./helpers/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.1.40",
3
+ "version": "1.1.42",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/package.tar.gz CHANGED
Binary file