@flink-app/test-utils 0.4.6 → 0.4.7

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 ADDED
@@ -0,0 +1,18 @@
1
+ import { FlinkApp, FlinkResponse } from "@flink-app/flink";
2
+ export declare type HttpOpts = {
3
+ qs?: {
4
+ [x: string]: string | string[];
5
+ };
6
+ headers?: {
7
+ [x: string]: string;
8
+ };
9
+ };
10
+ /**
11
+ * Initializes test flink app.
12
+ * Should be invoked prior to using test HTTP methods.
13
+ */
14
+ export declare function init(_app: FlinkApp<any>, host?: string): void;
15
+ export declare function get<Res = any>(path: string, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
16
+ export declare function post<Req = any, Res = any>(path: string, body: Req, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
17
+ export declare function put<Req = any, Res = any>(path: string, body: Req, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
18
+ export declare function del<Res = any>(path: string, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
package/dist/http.js ADDED
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.del = exports.put = exports.post = exports.get = exports.init = void 0;
54
+ var got_1 = __importDefault(require("got"));
55
+ var qs_1 = __importDefault(require("qs"));
56
+ var app;
57
+ var baseUrl;
58
+ var gotOpts = {
59
+ throwHttpErrors: false,
60
+ json: true,
61
+ retry: 0,
62
+ };
63
+ /**
64
+ * Initializes test flink app.
65
+ * Should be invoked prior to using test HTTP methods.
66
+ */
67
+ function init(_app, host) {
68
+ if (host === void 0) { host = "localhost"; }
69
+ app = _app;
70
+ baseUrl = "http://" + host + ":" + app.port;
71
+ }
72
+ exports.init = init;
73
+ function get(path, opts) {
74
+ if (opts === void 0) { opts = {}; }
75
+ return __awaiter(this, void 0, void 0, function () {
76
+ var res;
77
+ return __generator(this, function (_a) {
78
+ switch (_a.label) {
79
+ case 0:
80
+ validateApp();
81
+ return [4 /*yield*/, got_1.default.get(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: opts.headers }))];
82
+ case 1:
83
+ res = _a.sent();
84
+ return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
85
+ }
86
+ });
87
+ });
88
+ }
89
+ exports.get = get;
90
+ function post(path, body, opts) {
91
+ if (opts === void 0) { opts = {}; }
92
+ return __awaiter(this, void 0, void 0, function () {
93
+ var res;
94
+ return __generator(this, function (_a) {
95
+ switch (_a.label) {
96
+ case 0:
97
+ validateApp();
98
+ return [4 /*yield*/, got_1.default.post(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: opts.headers }))];
99
+ case 1:
100
+ res = _a.sent();
101
+ return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
102
+ }
103
+ });
104
+ });
105
+ }
106
+ exports.post = post;
107
+ function put(path, body, opts) {
108
+ if (opts === void 0) { opts = {}; }
109
+ return __awaiter(this, void 0, void 0, function () {
110
+ var res;
111
+ return __generator(this, function (_a) {
112
+ switch (_a.label) {
113
+ case 0:
114
+ validateApp();
115
+ return [4 /*yield*/, got_1.default.put(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: opts.headers }))];
116
+ case 1:
117
+ res = _a.sent();
118
+ return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
119
+ }
120
+ });
121
+ });
122
+ }
123
+ exports.put = put;
124
+ function del(path, opts) {
125
+ if (opts === void 0) { opts = {}; }
126
+ return __awaiter(this, void 0, void 0, function () {
127
+ var res;
128
+ return __generator(this, function (_a) {
129
+ switch (_a.label) {
130
+ case 0:
131
+ validateApp();
132
+ return [4 /*yield*/, got_1.default.delete(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: opts.headers }))];
133
+ case 1:
134
+ res = _a.sent();
135
+ return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
136
+ }
137
+ });
138
+ });
139
+ }
140
+ exports.del = del;
141
+ function validateApp() {
142
+ if (!app) {
143
+ throw new Error("App not initialized, run `init(app)` prior to invoking get/post/put/del");
144
+ }
145
+ }
146
+ function getUrl(path, queryString) {
147
+ if (queryString) {
148
+ return baseUrl + path + "?" + qs_1.default.stringify(queryString);
149
+ }
150
+ return baseUrl + path;
151
+ }
package/dist/index.d.ts CHANGED
@@ -1,18 +1,2 @@
1
- import { FlinkApp, FlinkResponse } from "@flink-app/flink";
2
- export declare type HttpOpts = {
3
- qs?: {
4
- [x: string]: string | string[];
5
- };
6
- headers?: {
7
- [x: string]: string;
8
- };
9
- };
10
- /**
11
- * Initializes test flink app.
12
- * Should be invoked prior to using test methods.
13
- */
14
- export declare function init(_app: FlinkApp<any>, host?: string): void;
15
- export declare function get<Res = any>(path: string, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
16
- export declare function post<Req = any, Res = any>(path: string, body: Req, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
17
- export declare function put<Req = any, Res = any>(path: string, body: Req, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
18
- export declare function del<Res = any>(path: string, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
1
+ export * from "./http";
2
+ export * from "./mocks";
package/dist/index.js CHANGED
@@ -1,151 +1,14 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __importDefault = (this && this.__importDefault) || function (mod) {
50
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
51
11
  };
52
12
  Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.del = exports.put = exports.post = exports.get = exports.init = void 0;
54
- var got_1 = __importDefault(require("got"));
55
- var qs_1 = __importDefault(require("qs"));
56
- var app;
57
- var baseUrl;
58
- var gotOpts = {
59
- throwHttpErrors: false,
60
- json: true,
61
- retry: 0,
62
- };
63
- /**
64
- * Initializes test flink app.
65
- * Should be invoked prior to using test methods.
66
- */
67
- function init(_app, host) {
68
- if (host === void 0) { host = "localhost"; }
69
- app = _app;
70
- baseUrl = "http://" + host + ":" + app.port;
71
- }
72
- exports.init = init;
73
- function get(path, opts) {
74
- if (opts === void 0) { opts = {}; }
75
- return __awaiter(this, void 0, void 0, function () {
76
- var res;
77
- return __generator(this, function (_a) {
78
- switch (_a.label) {
79
- case 0:
80
- validateApp();
81
- return [4 /*yield*/, got_1.default.get(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: opts.headers }))];
82
- case 1:
83
- res = _a.sent();
84
- return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
85
- }
86
- });
87
- });
88
- }
89
- exports.get = get;
90
- function post(path, body, opts) {
91
- if (opts === void 0) { opts = {}; }
92
- return __awaiter(this, void 0, void 0, function () {
93
- var res;
94
- return __generator(this, function (_a) {
95
- switch (_a.label) {
96
- case 0:
97
- validateApp();
98
- return [4 /*yield*/, got_1.default.post(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: opts.headers }))];
99
- case 1:
100
- res = _a.sent();
101
- return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
102
- }
103
- });
104
- });
105
- }
106
- exports.post = post;
107
- function put(path, body, opts) {
108
- if (opts === void 0) { opts = {}; }
109
- return __awaiter(this, void 0, void 0, function () {
110
- var res;
111
- return __generator(this, function (_a) {
112
- switch (_a.label) {
113
- case 0:
114
- validateApp();
115
- return [4 /*yield*/, got_1.default.put(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: opts.headers }))];
116
- case 1:
117
- res = _a.sent();
118
- return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
119
- }
120
- });
121
- });
122
- }
123
- exports.put = put;
124
- function del(path, opts) {
125
- if (opts === void 0) { opts = {}; }
126
- return __awaiter(this, void 0, void 0, function () {
127
- var res;
128
- return __generator(this, function (_a) {
129
- switch (_a.label) {
130
- case 0:
131
- validateApp();
132
- return [4 /*yield*/, got_1.default.delete(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: opts.headers }))];
133
- case 1:
134
- res = _a.sent();
135
- return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
136
- }
137
- });
138
- });
139
- }
140
- exports.del = del;
141
- function validateApp() {
142
- if (!app) {
143
- throw new Error("App not initialized, run `init(app)` prior to invoking get/post/put/del");
144
- }
145
- }
146
- function getUrl(path, queryString) {
147
- if (queryString) {
148
- return baseUrl + path + "?" + qs_1.default.stringify(queryString);
149
- }
150
- return baseUrl + path;
151
- }
13
+ __exportStar(require("./http"), exports);
14
+ __exportStar(require("./mocks"), exports);
@@ -0,0 +1,20 @@
1
+ import { FlinkAuthPlugin, FlinkRequest } from "@flink-app/flink";
2
+ interface TestFlinkRequest<T, P, Q> extends Omit<Partial<FlinkRequest<T, P, Q>>, "body" | "params" | "query"> {
3
+ body?: T;
4
+ params?: P;
5
+ query?: Q;
6
+ }
7
+ /**
8
+ * Creates a mocked FlinkRequest object where only essential properties are required.
9
+ * Will convert req body to JSON to ensure the body is a plain object.
10
+ * @param req
11
+ * @returns
12
+ */
13
+ export declare function mockReq<T, P, Q>(req?: TestFlinkRequest<T, P, Q>): FlinkRequest<T, P, Q>;
14
+ /**
15
+ * Auth plugin used for testing handlers.
16
+ * Will allow all requests.
17
+ * @returns
18
+ */
19
+ export declare function noOpAuthPlugin(): FlinkAuthPlugin;
20
+ export {};
package/dist/mocks.js ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.noOpAuthPlugin = exports.mockReq = void 0;
40
+ /**
41
+ * Creates a mocked FlinkRequest object where only essential properties are required.
42
+ * Will convert req body to JSON to ensure the body is a plain object.
43
+ * @param req
44
+ * @returns
45
+ */
46
+ function mockReq(req) {
47
+ var aMockReq = {
48
+ get: function (headerName) { var _a; return (_a = req === null || req === void 0 ? void 0 : req.headers) === null || _a === void 0 ? void 0 : _a[headerName]; },
49
+ body: req === null || req === void 0 ? void 0 : req.body,
50
+ };
51
+ if (!req) {
52
+ return aMockReq;
53
+ }
54
+ req.body = req.body ? JSON.parse(JSON.stringify(req.body)) : undefined;
55
+ return aMockReq;
56
+ }
57
+ exports.mockReq = mockReq;
58
+ /**
59
+ * Auth plugin used for testing handlers.
60
+ * Will allow all requests.
61
+ * @returns
62
+ */
63
+ function noOpAuthPlugin() {
64
+ var _this = this;
65
+ return {
66
+ authenticateRequest: function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
67
+ return [2 /*return*/, true];
68
+ }); }); },
69
+ createToken: function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
70
+ return [2 /*return*/, "mock-token"];
71
+ }); }); },
72
+ };
73
+ }
74
+ exports.noOpAuthPlugin = noOpAuthPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/test-utils",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
4
4
  "description": "Test utils for Flink",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\"",
@@ -19,7 +19,7 @@
19
19
  "qs": "^6.7.0"
20
20
  },
21
21
  "devDependencies": {
22
- "@flink-app/flink": "^0.4.6",
22
+ "@flink-app/flink": "^0.4.7",
23
23
  "@types/express": "^4.17.12",
24
24
  "@types/got": "^9.6.12",
25
25
  "@types/node": "^15.6.2",
@@ -29,5 +29,5 @@
29
29
  "tsc-watch": "^4.2.9",
30
30
  "typescript": "^4.2.4"
31
31
  },
32
- "gitHead": "fc4e2f5febd2b8594d3ade42beb4e85eb7ada4ed"
32
+ "gitHead": "b565d5987e08ba3aba3653325e935e6c56cab24c"
33
33
  }
package/src/http.ts ADDED
@@ -0,0 +1,90 @@
1
+ import { FlinkApp, FlinkResponse } from "@flink-app/flink";
2
+ import got, { GotJSONOptions } from "got";
3
+ import qs from "qs";
4
+
5
+ let app: FlinkApp<any>;
6
+ let baseUrl: string;
7
+
8
+ const gotOpts: GotJSONOptions = {
9
+ throwHttpErrors: false,
10
+ json: true,
11
+ retry: 0,
12
+ };
13
+
14
+ export type HttpOpts = {
15
+ qs?: { [x: string]: string | string[] };
16
+ headers?: { [x: string]: string };
17
+ };
18
+
19
+ /**
20
+ * Initializes test flink app.
21
+ * Should be invoked prior to using test HTTP methods.
22
+ */
23
+ export function init(_app: FlinkApp<any>, host = "localhost") {
24
+ app = _app;
25
+ baseUrl = `http://${host}:${app.port}`;
26
+ }
27
+
28
+ export async function get<Res = any>(path: string, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
29
+ validateApp();
30
+ const res = await got.get(getUrl(path, opts.qs), {
31
+ ...gotOpts,
32
+ headers: opts.headers,
33
+ });
34
+ return {
35
+ status: res.statusCode,
36
+ ...res.body,
37
+ };
38
+ }
39
+
40
+ export async function post<Req = any, Res = any>(path: string, body: Req, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
41
+ validateApp();
42
+ const res = await got.post(getUrl(path, opts.qs), {
43
+ ...gotOpts,
44
+ body: body as any,
45
+ headers: opts.headers,
46
+ });
47
+
48
+ return {
49
+ status: res.statusCode,
50
+ ...res.body,
51
+ };
52
+ }
53
+
54
+ export async function put<Req = any, Res = any>(path: string, body: Req, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
55
+ validateApp();
56
+ const res = await got.put(getUrl(path, opts.qs), {
57
+ ...gotOpts,
58
+ body: body as any,
59
+ headers: opts.headers,
60
+ });
61
+ return {
62
+ status: res.statusCode,
63
+ ...res.body,
64
+ };
65
+ }
66
+
67
+ export async function del<Res = any>(path: string, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
68
+ validateApp();
69
+ const res = await got.delete(getUrl(path, opts.qs), {
70
+ ...gotOpts,
71
+ headers: opts.headers,
72
+ });
73
+ return {
74
+ status: res.statusCode,
75
+ ...res.body,
76
+ };
77
+ }
78
+
79
+ function validateApp() {
80
+ if (!app) {
81
+ throw new Error("App not initialized, run `init(app)` prior to invoking get/post/put/del");
82
+ }
83
+ }
84
+
85
+ function getUrl(path: string, queryString?: HttpOpts["qs"]) {
86
+ if (queryString) {
87
+ return baseUrl + path + "?" + qs.stringify(queryString);
88
+ }
89
+ return baseUrl + path;
90
+ }
package/src/index.ts CHANGED
@@ -1,90 +1,2 @@
1
- import { FlinkApp, FlinkResponse } from "@flink-app/flink";
2
- import got, { GotJSONOptions } from "got";
3
- import qs from "qs";
4
-
5
- let app: FlinkApp<any>;
6
- let baseUrl: string;
7
-
8
- const gotOpts: GotJSONOptions = {
9
- throwHttpErrors: false,
10
- json: true,
11
- retry: 0,
12
- };
13
-
14
- export type HttpOpts = {
15
- qs?: { [x: string]: string | string[] };
16
- headers?: { [x: string]: string };
17
- };
18
-
19
- /**
20
- * Initializes test flink app.
21
- * Should be invoked prior to using test methods.
22
- */
23
- export function init(_app: FlinkApp<any>, host = "localhost") {
24
- app = _app;
25
- baseUrl = `http://${host}:${app.port}`;
26
- }
27
-
28
- export async function get<Res = any>(path: string, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
29
- validateApp();
30
- const res = await got.get(getUrl(path, opts.qs), {
31
- ...gotOpts,
32
- headers: opts.headers,
33
- });
34
- return {
35
- status: res.statusCode,
36
- ...res.body,
37
- };
38
- }
39
-
40
- export async function post<Req = any, Res = any>(path: string, body: Req, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
41
- validateApp();
42
- const res = await got.post(getUrl(path, opts.qs), {
43
- ...gotOpts,
44
- body: body as any,
45
- headers: opts.headers,
46
- });
47
-
48
- return {
49
- status: res.statusCode,
50
- ...res.body,
51
- };
52
- }
53
-
54
- export async function put<Req = any, Res = any>(path: string, body: Req, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
55
- validateApp();
56
- const res = await got.put(getUrl(path, opts.qs), {
57
- ...gotOpts,
58
- body: body as any,
59
- headers: opts.headers,
60
- });
61
- return {
62
- status: res.statusCode,
63
- ...res.body,
64
- };
65
- }
66
-
67
- export async function del<Res = any>(path: string, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
68
- validateApp();
69
- const res = await got.delete(getUrl(path, opts.qs), {
70
- ...gotOpts,
71
- headers: opts.headers,
72
- });
73
- return {
74
- status: res.statusCode,
75
- ...res.body,
76
- };
77
- }
78
-
79
- function validateApp() {
80
- if (!app) {
81
- throw new Error("App not initialized, run `init(app)` prior to invoking get/post/put/del");
82
- }
83
- }
84
-
85
- function getUrl(path: string, queryString?: HttpOpts["qs"]) {
86
- if (queryString) {
87
- return baseUrl + path + "?" + qs.stringify(queryString);
88
- }
89
- return baseUrl + path;
90
- }
1
+ export * from "./http";
2
+ export * from "./mocks";
package/src/mocks.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { FlinkAuthPlugin, FlinkRequest } from "@flink-app/flink";
2
+
3
+ interface TestFlinkRequest<T, P, Q> extends Omit<Partial<FlinkRequest<T, P, Q>>, "body" | "params" | "query"> {
4
+ body?: T;
5
+ params?: P;
6
+ query?: Q;
7
+ }
8
+
9
+ /**
10
+ * Creates a mocked FlinkRequest object where only essential properties are required.
11
+ * Will convert req body to JSON to ensure the body is a plain object.
12
+ * @param req
13
+ * @returns
14
+ */
15
+ export function mockReq<T, P, Q>(req?: TestFlinkRequest<T, P, Q>): FlinkRequest<T, P, Q> {
16
+ const aMockReq = {
17
+ get: (headerName: string) => req?.headers?.[headerName],
18
+ body: req?.body,
19
+ };
20
+
21
+ if (!req) {
22
+ return aMockReq as any;
23
+ }
24
+
25
+ req.body = req.body ? JSON.parse(JSON.stringify(req.body)) : undefined;
26
+
27
+ return aMockReq as any;
28
+ }
29
+
30
+ /**
31
+ * Auth plugin used for testing handlers.
32
+ * Will allow all requests.
33
+ * @returns
34
+ */
35
+ export function noOpAuthPlugin(): FlinkAuthPlugin {
36
+ return {
37
+ authenticateRequest: async () => true,
38
+ createToken: async () => "mock-token",
39
+ };
40
+ }