api-def 0.12.0-alpha.21 → 0.12.0-alpha.22

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/bin/index.js CHANGED
@@ -274958,7 +274958,7 @@ ${source}`;
274958
274958
 
274959
274959
  // cli/index.ts
274960
274960
  program.name("api-def");
274961
- var packageJson = JSON.parse(fs2.readFileSync("package.json", "utf-8"));
274961
+ var packageJson = JSON.parse(fs2.readFileSync(path.resolve(__dirname, "../package.json"), "utf-8"));
274962
274962
  program.version(packageJson.version);
274963
274963
  program.command("generate").argument("<inPath>", "Path to the OpenAPI spec").argument("<outPath>", "Path to the output file").description("Generate an api-def from an OpenAPI spec").action(async (inPath, outPath) => {
274964
274964
  const resolvedInPath = path.resolve(inPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-def",
3
- "version": "0.12.0-alpha.21",
3
+ "version": "0.12.0-alpha.22",
4
4
  "description": "Typed API definitions with middleware support",
5
5
  "main": "cjs/index.js",
6
6
  "types": "esm/index.d.ts",
@@ -1 +0,0 @@
1
- export {};
@@ -1,166 +0,0 @@
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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
- var qs = require("qs");
40
- var Api_1 = require("../Api");
41
- var ApiConstants_1 = require("../ApiConstants");
42
- var MockApi_1 = require("./mock/MockApi");
43
- var api = new Api_1.Api({
44
- baseUrl: "example.com",
45
- name: "Example API",
46
- mocking: {
47
- enabled: true,
48
- },
49
- middleware: [
50
- {
51
- beforeSend: function (context) { return __awaiter(void 0, void 0, void 0, function () {
52
- return __generator(this, function (_a) {
53
- context.updateQuery({
54
- test: "abc",
55
- });
56
- return [2 /*return*/];
57
- });
58
- }); },
59
- },
60
- ],
61
- });
62
- var queryReturnEndpoint = api
63
- .endpoint()
64
- .queryOf()
65
- .build({
66
- name: "Fetch Users",
67
- id: "fetchUsers",
68
- method: ApiConstants_1.RequestMethod.Get,
69
- path: "/users",
70
- mocking: {
71
- handler: function (req, res) {
72
- return res.status(200).send([{ name: "Test", query: req.query }]);
73
- },
74
- },
75
- });
76
- it("URL is properly constructed", function () { return __awaiter(void 0, void 0, void 0, function () {
77
- var res;
78
- return __generator(this, function (_a) {
79
- switch (_a.label) {
80
- case 0: return [4 /*yield*/, MockApi_1.postIdVerifStatus.submit({
81
- body: {
82
- forceReset: true,
83
- validationService: 1,
84
- },
85
- })];
86
- case 1:
87
- res = _a.sent();
88
- expect(res.data.url).toBe("https://example.com/id-verif/verif-status");
89
- return [2 /*return*/];
90
- }
91
- });
92
- }); });
93
- it("query as string", function () { return __awaiter(void 0, void 0, void 0, function () {
94
- var res;
95
- return __generator(this, function (_a) {
96
- switch (_a.label) {
97
- case 0: return [4 /*yield*/, queryReturnEndpoint.submit({
98
- query: "test=1&id=3",
99
- })];
100
- case 1:
101
- res = _a.sent();
102
- expect(res.data).toEqual([
103
- {
104
- name: "Test",
105
- query: {
106
- test: "abc",
107
- id: "3",
108
- },
109
- },
110
- ]);
111
- return [2 /*return*/];
112
- }
113
- });
114
- }); });
115
- it("qs", function () { return __awaiter(void 0, void 0, void 0, function () {
116
- var res;
117
- return __generator(this, function (_a) {
118
- switch (_a.label) {
119
- case 0: return [4 /*yield*/, queryReturnEndpoint.submit({
120
- query: "id[0]=1&id[1]=2",
121
- })];
122
- case 1:
123
- res = _a.sent();
124
- expect(res.data).toEqual([
125
- {
126
- name: "Test",
127
- query: {
128
- test: "abc",
129
- "id[0]": "1",
130
- "id[1]": "2",
131
- },
132
- },
133
- ]);
134
- return [4 /*yield*/, queryReturnEndpoint.submit({
135
- query: "id[0]=1&id[1]=2",
136
- queryHandling: qs,
137
- })];
138
- case 2:
139
- res = _a.sent();
140
- expect(res.data).toEqual([
141
- {
142
- name: "Test",
143
- query: {
144
- test: "abc",
145
- id: ["1", "2"],
146
- },
147
- },
148
- ]);
149
- return [2 /*return*/];
150
- }
151
- });
152
- }); });
153
- /*it("infer params in typescript", async (t) => {
154
- const endpoint = mockApi.endpoint()
155
- .build({
156
- method: "get",
157
- id: "test",
158
- path: "/test/:id",
159
- });
160
-
161
- endpoint.submit({
162
- params: {
163
- id: "test",
164
- },
165
- });
166
- });*/
@@ -1 +0,0 @@
1
- export {};
@@ -1,94 +0,0 @@
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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
- var axios_1 = require("axios");
40
- var Api_1 = require("../Api");
41
- var AxiosRequestBackend_1 = require("../backend/AxiosRequestBackend");
42
- var FetchRequestBackend_1 = require("../backend/FetchRequestBackend");
43
- var cleanResponse = function (response) {
44
- response.headers = undefined;
45
- response.__lowercaseHeaders = undefined;
46
- return response;
47
- };
48
- it("request backends output", function () { return __awaiter(void 0, void 0, void 0, function () {
49
- var api, fetchPage, fetchResult, axiosResult;
50
- return __generator(this, function (_a) {
51
- switch (_a.label) {
52
- case 0:
53
- api = new Api_1.Api({
54
- baseUrl: "httpstat.us",
55
- name: "Example API",
56
- requestBackend: new AxiosRequestBackend_1.default(axios_1.default),
57
- middleware: [
58
- {
59
- beforeSend: function (context) { return __awaiter(void 0, void 0, void 0, function () {
60
- return __generator(this, function (_a) {
61
- context.updateQuery({
62
- id: "abc",
63
- });
64
- return [2 /*return*/];
65
- });
66
- }); },
67
- },
68
- ],
69
- });
70
- fetchPage = api.endpoint().queryOf().build({
71
- name: "Get 200",
72
- id: "200",
73
- method: "get",
74
- path: "/200",
75
- });
76
- expect(fetchPage.name).toBe("Get 200");
77
- (0, Api_1.setRequestBackend)(new FetchRequestBackend_1.default());
78
- return [4 /*yield*/, fetchPage.submit({
79
- query: "test",
80
- })];
81
- case 1:
82
- fetchResult = _a.sent();
83
- (0, Api_1.setRequestBackend)(new AxiosRequestBackend_1.default(axios_1.default));
84
- return [4 /*yield*/, fetchPage.submit({
85
- query: "test",
86
- })];
87
- case 2:
88
- axiosResult = _a.sent();
89
- expect(cleanResponse(fetchResult)).toEqual(cleanResponse(axiosResult));
90
- expect(fetchResult.url).toBe("https://httpstat.us/200?test=true&id=abc");
91
- return [2 /*return*/];
92
- }
93
- });
94
- }); });
@@ -1 +0,0 @@
1
- export {};
@@ -1,92 +0,0 @@
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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
- var axios_1 = require("axios");
40
- var Api_1 = require("../Api");
41
- var AxiosRequestBackend_1 = require("../backend/AxiosRequestBackend");
42
- var FetchRequestBackend_1 = require("../backend/FetchRequestBackend");
43
- var cleanResponse = function (response) {
44
- response.headers = undefined;
45
- response.__lowercaseHeaders = undefined;
46
- return response;
47
- };
48
- it("request backends output", function () { return __awaiter(void 0, void 0, void 0, function () {
49
- var api, fetchPage, fetchResult, axiosResult;
50
- return __generator(this, function (_a) {
51
- switch (_a.label) {
52
- case 0:
53
- api = new Api_1.Api({
54
- baseUrl: "httpstat.us",
55
- name: "Example API",
56
- middleware: [
57
- {
58
- beforeSend: function (context) { return __awaiter(void 0, void 0, void 0, function () {
59
- return __generator(this, function (_a) {
60
- context.updateQuery({
61
- id: "abc",
62
- });
63
- return [2 /*return*/];
64
- });
65
- }); },
66
- },
67
- ],
68
- });
69
- fetchPage = api.endpoint().queryOf().build({
70
- name: "Get 200",
71
- id: "200",
72
- method: "get",
73
- path: "/200",
74
- });
75
- (0, Api_1.setRequestBackend)(new FetchRequestBackend_1.default());
76
- return [4 /*yield*/, fetchPage.submit({
77
- query: "test",
78
- })];
79
- case 1:
80
- fetchResult = _a.sent();
81
- (0, Api_1.setRequestBackend)(new AxiosRequestBackend_1.default(axios_1.default));
82
- return [4 /*yield*/, fetchPage.submit({
83
- query: "test",
84
- })];
85
- case 2:
86
- axiosResult = _a.sent();
87
- expect(cleanResponse(fetchResult)).toEqual(cleanResponse(axiosResult));
88
- expect(fetchResult.url).toBe("https://httpstat.us/200?test=true&id=abc");
89
- return [2 /*return*/];
90
- }
91
- });
92
- }); });
@@ -1 +0,0 @@
1
- export {};
@@ -1,58 +0,0 @@
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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
- var MockApi_1 = require("./mock/MockApi");
40
- it("correctly parses body for form url encoded request", function () { return __awaiter(void 0, void 0, void 0, function () {
41
- var res;
42
- return __generator(this, function (_a) {
43
- switch (_a.label) {
44
- case 0: return [4 /*yield*/, MockApi_1.postFormUrlEncoded.submit({
45
- body: {
46
- test: 123,
47
- b: "test",
48
- },
49
- })];
50
- case 1:
51
- res = _a.sent();
52
- expect(res.data).toEqual(["test=123&b=test"]);
53
- return [2 /*return*/];
54
- }
55
- });
56
- }); });
57
- // @ts-expect-error
58
- var a = { data: 123 };
@@ -1 +0,0 @@
1
- export {};
@@ -1,147 +0,0 @@
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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
- var Api_1 = require("../Api");
40
- var ApiConstants_1 = require("../ApiConstants");
41
- var api = new Api_1.Api({
42
- baseUrl: "example.com",
43
- name: "Example API",
44
- mocking: {
45
- enabled: true,
46
- },
47
- middleware: [
48
- {
49
- beforeSend: function (context) { return __awaiter(void 0, void 0, void 0, function () {
50
- return __generator(this, function (_a) {
51
- context.updateQuery({
52
- test: "abc",
53
- });
54
- return [2 /*return*/];
55
- });
56
- }); },
57
- },
58
- ],
59
- });
60
- var withColons = api
61
- .endpoint()
62
- .paramsOf()
63
- .build({
64
- name: "Fetch Users",
65
- id: "fetchUsers",
66
- method: ApiConstants_1.RequestMethod.Get,
67
- path: "/users/:id/:sub_id",
68
- mocking: {
69
- handler: function (req, res) {
70
- return res.status(200).send([{ name: "Test", data: req.url }]);
71
- },
72
- },
73
- });
74
- var withBrackets = api
75
- .endpoint()
76
- .paramsOf()
77
- .build({
78
- name: "Fetch Users",
79
- id: "fetchUsers",
80
- method: ApiConstants_1.RequestMethod.Get,
81
- path: "/users/{id}/{sub_id}",
82
- mocking: {
83
- handler: function (req, res) {
84
- return res.status(200).send([{ name: "Test", data: req.url }]);
85
- },
86
- },
87
- });
88
- it("should throw error if missing param", function () { return __awaiter(void 0, void 0, void 0, function () {
89
- var res, res2;
90
- return __generator(this, function (_a) {
91
- switch (_a.label) {
92
- case 0: return [4 /*yield*/, expect((function () { return __awaiter(void 0, void 0, void 0, function () {
93
- var res;
94
- return __generator(this, function (_a) {
95
- switch (_a.label) {
96
- case 0: return [4 /*yield*/, withColons.submit({
97
- params: {
98
- id: "123",
99
- },
100
- })];
101
- case 1:
102
- res = _a.sent();
103
- return [2 /*return*/, res];
104
- }
105
- });
106
- }); })()).rejects.toThrowError("[api-def] Not all path params have been resolved: '/users/123/:sub_id'")];
107
- case 1:
108
- _a.sent();
109
- return [4 /*yield*/, expect((function () { return __awaiter(void 0, void 0, void 0, function () {
110
- var res;
111
- return __generator(this, function (_a) {
112
- switch (_a.label) {
113
- case 0: return [4 /*yield*/, withBrackets.submit({
114
- params: {
115
- id: "123",
116
- },
117
- })];
118
- case 1:
119
- res = _a.sent();
120
- return [2 /*return*/, res];
121
- }
122
- });
123
- }); })()).rejects.toThrowError("[api-def] Not all path params have been resolved: '/users/123/{sub_id}'")];
124
- case 2:
125
- _a.sent();
126
- return [4 /*yield*/, withColons.submit({
127
- params: {
128
- id: "123",
129
- sub_id: "456",
130
- },
131
- })];
132
- case 3:
133
- res = _a.sent();
134
- expect(res.data).toEqual([{ name: "Test", data: "https://example.com/users/123/456" }]);
135
- return [4 /*yield*/, withBrackets.submit({
136
- params: {
137
- id: "123",
138
- sub_id: "456",
139
- },
140
- })];
141
- case 4:
142
- res2 = _a.sent();
143
- expect(res2.data).toEqual([{ name: "Test", data: "https://example.com/users/123/456" }]);
144
- return [2 /*return*/];
145
- }
146
- });
147
- }); });
@@ -1 +0,0 @@
1
- export {};