@flink-app/test-utils 0.12.1-alpha.2 → 0.12.1-alpha.4

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/dist/http.d.ts CHANGED
@@ -1,15 +1,30 @@
1
1
  import { FlinkApp, FlinkResponse } from "@flink-app/flink";
2
2
  export type HttpOpts = {
3
+ /***
4
+ * Optional query string
5
+ */
3
6
  qs?: {
4
7
  [x: string]: string | string[];
5
8
  };
9
+ /**
10
+ * Optional headers
11
+ */
6
12
  headers?: {
7
13
  [x: string]: string;
8
14
  };
15
+ /**
16
+ * Optional user object to be used for authentication.
17
+ *
18
+ * An auth plugin must be set in the app for this to work. Most likely the auth plugin
19
+ * is a mock auth plugin in test scenarios.
20
+ */
21
+ user?: any & {
22
+ roles: string[];
23
+ };
9
24
  };
10
25
  /**
11
26
  * Initializes test flink app.
12
- * Should be invoked prior to using test HTTP methods.
27
+ * Must be invoked prior to using test HTTP methods.
13
28
  */
14
29
  export declare function init(_app: FlinkApp<any>, host?: string): void;
15
30
  export declare function get<Res = any>(path: string, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
package/dist/http.js CHANGED
@@ -62,7 +62,7 @@ var gotOpts = {
62
62
  };
63
63
  /**
64
64
  * Initializes test flink app.
65
- * Should be invoked prior to using test HTTP methods.
65
+ * Must be invoked prior to using test HTTP methods.
66
66
  */
67
67
  function init(_app, host) {
68
68
  if (host === void 0) { host = "localhost"; }
@@ -72,14 +72,17 @@ function init(_app, host) {
72
72
  exports.init = init;
73
73
  function get(path_1) {
74
74
  return __awaiter(this, arguments, void 0, function (path, opts) {
75
- var res;
75
+ var headers, res;
76
76
  if (opts === void 0) { opts = {}; }
77
77
  return __generator(this, function (_a) {
78
78
  switch (_a.label) {
79
79
  case 0:
80
80
  validateApp();
81
- return [4 /*yield*/, got_1.default.get(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: opts.headers }))];
81
+ return [4 /*yield*/, setAuthHeader(opts.user, opts.headers)];
82
82
  case 1:
83
+ headers = _a.sent();
84
+ return [4 /*yield*/, got_1.default.get(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: headers }))];
85
+ case 2:
83
86
  res = _a.sent();
84
87
  return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
85
88
  }
@@ -89,16 +92,27 @@ function get(path_1) {
89
92
  exports.get = get;
90
93
  function post(path_1, body_1) {
91
94
  return __awaiter(this, arguments, void 0, function (path, body, opts) {
92
- var res;
95
+ var headers, res, err_1;
93
96
  if (opts === void 0) { opts = {}; }
94
97
  return __generator(this, function (_a) {
95
98
  switch (_a.label) {
96
99
  case 0:
97
100
  validateApp();
98
- return [4 /*yield*/, got_1.default.post(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: opts.headers }))];
101
+ return [4 /*yield*/, setAuthHeader(opts.user, opts.headers)];
99
102
  case 1:
103
+ headers = _a.sent();
104
+ _a.label = 2;
105
+ case 2:
106
+ _a.trys.push([2, 4, , 5]);
107
+ return [4 /*yield*/, got_1.default.post(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: headers }))];
108
+ case 3:
100
109
  res = _a.sent();
101
110
  return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
111
+ case 4:
112
+ err_1 = _a.sent();
113
+ console.error(err_1);
114
+ throw err_1;
115
+ case 5: return [2 /*return*/];
102
116
  }
103
117
  });
104
118
  });
@@ -106,14 +120,17 @@ function post(path_1, body_1) {
106
120
  exports.post = post;
107
121
  function put(path_1, body_1) {
108
122
  return __awaiter(this, arguments, void 0, function (path, body, opts) {
109
- var res;
123
+ var headers, res;
110
124
  if (opts === void 0) { opts = {}; }
111
125
  return __generator(this, function (_a) {
112
126
  switch (_a.label) {
113
127
  case 0:
114
128
  validateApp();
115
- return [4 /*yield*/, got_1.default.put(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: opts.headers }))];
129
+ return [4 /*yield*/, setAuthHeader(opts.user, opts.headers)];
116
130
  case 1:
131
+ headers = _a.sent();
132
+ return [4 /*yield*/, got_1.default.put(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: headers }))];
133
+ case 2:
117
134
  res = _a.sent();
118
135
  return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
119
136
  }
@@ -123,14 +140,17 @@ function put(path_1, body_1) {
123
140
  exports.put = put;
124
141
  function del(path_1) {
125
142
  return __awaiter(this, arguments, void 0, function (path, opts) {
126
- var res;
143
+ var headers, res;
127
144
  if (opts === void 0) { opts = {}; }
128
145
  return __generator(this, function (_a) {
129
146
  switch (_a.label) {
130
147
  case 0:
131
148
  validateApp();
132
- return [4 /*yield*/, got_1.default.delete(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: opts.headers }))];
149
+ return [4 /*yield*/, setAuthHeader(opts.user, opts.headers)];
133
150
  case 1:
151
+ headers = _a.sent();
152
+ return [4 /*yield*/, got_1.default.delete(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: headers }))];
153
+ case 2:
134
154
  res = _a.sent();
135
155
  return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
136
156
  }
@@ -149,3 +169,24 @@ function getUrl(path, queryString) {
149
169
  }
150
170
  return baseUrl + path;
151
171
  }
172
+ function setAuthHeader(user, headers) {
173
+ return __awaiter(this, void 0, void 0, function () {
174
+ var token;
175
+ return __generator(this, function (_a) {
176
+ switch (_a.label) {
177
+ case 0:
178
+ if (!user)
179
+ return [2 /*return*/, headers];
180
+ headers = headers || {};
181
+ if (!app.auth) {
182
+ throw new Error("Auth plugin not set, cannot use user option");
183
+ }
184
+ return [4 /*yield*/, app.auth.createToken(user, user.roles || [])];
185
+ case 1:
186
+ token = _a.sent();
187
+ headers["Authorization"] = "Bearer ".concat(token);
188
+ return [2 /*return*/, headers];
189
+ }
190
+ });
191
+ });
192
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/test-utils",
3
- "version": "0.12.1-alpha.2",
3
+ "version": "0.12.1-alpha.4",
4
4
  "description": "Test utils for Flink",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\"",
@@ -20,7 +20,7 @@
20
20
  "qs": "^6.7.0"
21
21
  },
22
22
  "devDependencies": {
23
- "@flink-app/flink": "^0.12.1-alpha.2",
23
+ "@flink-app/flink": "^0.12.1-alpha.4",
24
24
  "@types/express": "^4.17.12",
25
25
  "@types/got": "^9.6.12",
26
26
  "@types/node": "22.13.10",
@@ -30,5 +30,5 @@
30
30
  "tsc-watch": "^4.2.9",
31
31
  "typescript": "5.4.5"
32
32
  },
33
- "gitHead": "b5be77c75e10067c81e0ecab8716b30f99833b83"
33
+ "gitHead": "8f06a4ab98e7179322d36546756d4305fd196f5a"
34
34
  }
package/src/http.ts CHANGED
@@ -12,13 +12,26 @@ const gotOpts: GotJSONOptions = {
12
12
  };
13
13
 
14
14
  export type HttpOpts = {
15
+ /***
16
+ * Optional query string
17
+ */
15
18
  qs?: { [x: string]: string | string[] };
19
+ /**
20
+ * Optional headers
21
+ */
16
22
  headers?: { [x: string]: string };
23
+ /**
24
+ * Optional user object to be used for authentication.
25
+ *
26
+ * An auth plugin must be set in the app for this to work. Most likely the auth plugin
27
+ * is a mock auth plugin in test scenarios.
28
+ */
29
+ user?: any & { roles: string[] };
17
30
  };
18
31
 
19
32
  /**
20
33
  * Initializes test flink app.
21
- * Should be invoked prior to using test HTTP methods.
34
+ * Must be invoked prior to using test HTTP methods.
22
35
  */
23
36
  export function init(_app: FlinkApp<any>, host = "localhost") {
24
37
  app = _app;
@@ -27,9 +40,12 @@ export function init(_app: FlinkApp<any>, host = "localhost") {
27
40
 
28
41
  export async function get<Res = any>(path: string, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
29
42
  validateApp();
43
+
44
+ const headers = await setAuthHeader(opts.user, opts.headers);
45
+
30
46
  const res = await got.get(getUrl(path, opts.qs), {
31
47
  ...gotOpts,
32
- headers: opts.headers,
48
+ headers,
33
49
  });
34
50
  return {
35
51
  status: res.statusCode,
@@ -39,24 +55,35 @@ export async function get<Res = any>(path: string, opts: HttpOpts = {}): Promise
39
55
 
40
56
  export async function post<Req = any, Res = any>(path: string, body: Req, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
41
57
  validateApp();
42
- const res = await got.post(getUrl(path, opts.qs), {
43
- ...gotOpts,
44
- body: body as any,
45
- headers: opts.headers,
46
- });
47
58
 
48
- return {
49
- status: res.statusCode,
50
- ...res.body,
51
- };
59
+ const headers = await setAuthHeader(opts.user, opts.headers);
60
+
61
+ try {
62
+ const res = await got.post(getUrl(path, opts.qs), {
63
+ ...gotOpts,
64
+ body: body as any,
65
+ headers,
66
+ });
67
+
68
+ return {
69
+ status: res.statusCode,
70
+ ...res.body,
71
+ };
72
+ } catch (err) {
73
+ console.error(err);
74
+ throw err;
75
+ }
52
76
  }
53
77
 
54
78
  export async function put<Req = any, Res = any>(path: string, body: Req, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
55
79
  validateApp();
80
+
81
+ const headers = await setAuthHeader(opts.user, opts.headers);
82
+
56
83
  const res = await got.put(getUrl(path, opts.qs), {
57
84
  ...gotOpts,
58
85
  body: body as any,
59
- headers: opts.headers,
86
+ headers,
60
87
  });
61
88
  return {
62
89
  status: res.statusCode,
@@ -66,9 +93,12 @@ export async function put<Req = any, Res = any>(path: string, body: Req, opts: H
66
93
 
67
94
  export async function del<Res = any>(path: string, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
68
95
  validateApp();
96
+
97
+ const headers = await setAuthHeader(opts.user, opts.headers);
98
+
69
99
  const res = await got.delete(getUrl(path, opts.qs), {
70
100
  ...gotOpts,
71
- headers: opts.headers,
101
+ headers,
72
102
  });
73
103
  return {
74
104
  status: res.statusCode,
@@ -88,3 +118,16 @@ function getUrl(path: string, queryString?: HttpOpts["qs"]) {
88
118
  }
89
119
  return baseUrl + path;
90
120
  }
121
+
122
+ async function setAuthHeader(user?: any, headers?: HttpOpts["headers"]) {
123
+ if (!user) return headers;
124
+
125
+ headers = headers || {};
126
+
127
+ if (!app.auth) {
128
+ throw new Error("Auth plugin not set, cannot use user option");
129
+ }
130
+ const token = await app.auth.createToken(user, user.roles || []);
131
+ headers["Authorization"] = `Bearer ${token}`;
132
+ return headers;
133
+ }