api-def 0.12.0-alpha.39 → 0.12.0-alpha.40

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.
Files changed (82) hide show
  1. package/cjs/ApiTypes.d.ts +2 -1
  2. package/cjs/backend/AxiosRequestBackend.js +36 -1
  3. package/cjs/backend/FetchRequestBackend.js +2 -1
  4. package/cjs/cache/ClientCacheBackend.d.ts +6 -0
  5. package/cjs/cache/ClientCaching.d.ts +10 -0
  6. package/cjs/cache/{Caching.js → ClientCaching.js} +17 -17
  7. package/cjs/cache/LocalForageClientCacheBackend.d.ts +9 -0
  8. package/cjs/{tests/state.test.js → cache/LocalForageClientCacheBackend.js} +52 -52
  9. package/cjs/cache/LocalStorageClientCacheBackend.d.ts +7 -0
  10. package/cjs/cache/{LocalStorageCacheBackend.js → LocalStorageClientCacheBackend.js} +14 -14
  11. package/cjs/index.d.ts +4 -4
  12. package/cjs/index.js +10 -10
  13. package/cjs/middleware/ClientCacheMiddleware.d.ts +7 -0
  14. package/cjs/middleware/{CacheMiddleware.js → ClientCacheMiddleware.js} +16 -14
  15. package/esm/ApiTypes.d.ts +2 -1
  16. package/esm/backend/AxiosRequestBackend.js +25 -1
  17. package/esm/backend/FetchRequestBackend.js +2 -1
  18. package/esm/cache/ClientCacheBackend.d.ts +6 -0
  19. package/esm/cache/ClientCaching.d.ts +10 -0
  20. package/esm/cache/{Caching.js → ClientCaching.js} +12 -12
  21. package/esm/cache/LocalForageClientCacheBackend.d.ts +9 -0
  22. package/esm/cache/LocalForageClientCacheBackend.js +36 -0
  23. package/esm/cache/LocalStorageClientCacheBackend.d.ts +7 -0
  24. package/esm/cache/{LocalStorageCacheBackend.js → LocalStorageClientCacheBackend.js} +8 -8
  25. package/esm/index.d.ts +4 -4
  26. package/esm/index.js +4 -4
  27. package/esm/middleware/ClientCacheMiddleware.d.ts +7 -0
  28. package/esm/middleware/{CacheMiddleware.js → ClientCacheMiddleware.js} +14 -12
  29. package/package.json +3 -3
  30. package/cjs/cache/CacheBackend.d.ts +0 -6
  31. package/cjs/cache/Caching.d.ts +0 -10
  32. package/cjs/cache/LocalForageCacheBackend.d.ts +0 -9
  33. package/cjs/cache/LocalForageCacheBackend.js +0 -23
  34. package/cjs/cache/LocalStorageCacheBackend.d.ts +0 -7
  35. package/cjs/middleware/CacheMiddleware.d.ts +0 -7
  36. package/cjs/tests/api.test.js +0 -166
  37. package/cjs/tests/axios.test.d.ts +0 -1
  38. package/cjs/tests/axios.test.js +0 -94
  39. package/cjs/tests/backend.test.d.ts +0 -1
  40. package/cjs/tests/backend.test.js +0 -92
  41. package/cjs/tests/bodyTypes.test.d.ts +0 -1
  42. package/cjs/tests/bodyTypes.test.js +0 -58
  43. package/cjs/tests/mock/MockApi.d.ts +0 -16
  44. package/cjs/tests/mock/MockApi.js +0 -67
  45. package/cjs/tests/params.test.d.ts +0 -1
  46. package/cjs/tests/params.test.js +0 -147
  47. package/cjs/tests/retry.test.d.ts +0 -1
  48. package/cjs/tests/retry.test.js +0 -134
  49. package/cjs/tests/state.test.d.ts +0 -1
  50. package/cjs/tests/streaming.test.d.ts +0 -1
  51. package/cjs/tests/streaming.test.js +0 -207
  52. package/cjs/tests/validation.test.d.ts +0 -1
  53. package/cjs/tests/validation.test.js +0 -124
  54. package/esm/cache/CacheBackend.d.ts +0 -6
  55. package/esm/cache/CacheBackend.js +0 -1
  56. package/esm/cache/Caching.d.ts +0 -10
  57. package/esm/cache/LocalForageCacheBackend.d.ts +0 -9
  58. package/esm/cache/LocalForageCacheBackend.js +0 -19
  59. package/esm/cache/LocalStorageCacheBackend.d.ts +0 -7
  60. package/esm/middleware/CacheMiddleware.d.ts +0 -7
  61. package/esm/tests/api.test.d.ts +0 -1
  62. package/esm/tests/api.test.js +0 -108
  63. package/esm/tests/axios.test.d.ts +0 -1
  64. package/esm/tests/axios.test.js +0 -51
  65. package/esm/tests/backend.test.d.ts +0 -1
  66. package/esm/tests/backend.test.js +0 -49
  67. package/esm/tests/bodyTypes.test.d.ts +0 -1
  68. package/esm/tests/bodyTypes.test.js +0 -21
  69. package/esm/tests/mock/MockApi.d.ts +0 -16
  70. package/esm/tests/mock/MockApi.js +0 -64
  71. package/esm/tests/params.test.d.ts +0 -1
  72. package/esm/tests/params.test.js +0 -88
  73. package/esm/tests/retry.test.d.ts +0 -1
  74. package/esm/tests/retry.test.js +0 -77
  75. package/esm/tests/state.test.d.ts +0 -1
  76. package/esm/tests/state.test.js +0 -52
  77. package/esm/tests/streaming.test.d.ts +0 -1
  78. package/esm/tests/streaming.test.js +0 -123
  79. package/esm/tests/validation.test.d.ts +0 -1
  80. package/esm/tests/validation.test.js +0 -81
  81. /package/cjs/cache/{CacheBackend.js → ClientCacheBackend.js} +0 -0
  82. /package/{cjs/tests/api.test.d.ts → esm/cache/ClientCacheBackend.js} +0 -0
@@ -0,0 +1,36 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ export default class LocalForageClientCacheBackend {
11
+ constructor(localforage) {
12
+ this.store = localforage.createInstance({
13
+ name: "requestClientCache",
14
+ });
15
+ }
16
+ getItem(key) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ return this.store.getItem(key);
19
+ });
20
+ }
21
+ setItem(key, value) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ yield this.store.setItem(key, value);
24
+ });
25
+ }
26
+ removeItem(key) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ yield this.store.removeItem(key);
29
+ });
30
+ }
31
+ clear() {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ yield this.store.clear();
34
+ });
35
+ }
36
+ }
@@ -0,0 +1,7 @@
1
+ import type { ClientCacheBackend } from "./ClientCacheBackend";
2
+ export default class LocalStorageClientCacheBackend implements ClientCacheBackend {
3
+ getItem(key: string): Promise<any>;
4
+ setItem(key: string, value: any): Promise<void>;
5
+ removeItem(key: string): Promise<void>;
6
+ clear(): Promise<void>;
7
+ }
@@ -7,16 +7,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- export default class LocalStorageCacheBackend {
11
- clear() {
10
+ export default class LocalStorageClientCacheBackend {
11
+ getItem(key) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
- localStorage.clear();
13
+ const item = localStorage.getItem(key);
14
+ return item ? JSON.parse(item) : null;
14
15
  });
15
16
  }
16
- getItem(key) {
17
+ setItem(key, value) {
17
18
  return __awaiter(this, void 0, void 0, function* () {
18
- const value = localStorage.getItem(key);
19
- return value ? JSON.parse(value) : null;
19
+ localStorage.setItem(key, JSON.stringify(value));
20
20
  });
21
21
  }
22
22
  removeItem(key) {
@@ -24,9 +24,9 @@ export default class LocalStorageCacheBackend {
24
24
  localStorage.removeItem(key);
25
25
  });
26
26
  }
27
- setItem(key, value) {
27
+ clear() {
28
28
  return __awaiter(this, void 0, void 0, function* () {
29
- localStorage.setItem(key, JSON.stringify(value));
29
+ localStorage.clear();
30
30
  });
31
31
  }
32
32
  }
package/esm/index.d.ts CHANGED
@@ -6,10 +6,10 @@ export { default as retry } from "./util/retry";
6
6
  export type { RetryOptions } from "./util/retry/interfaces";
7
7
  export { isRequestError, type RequestError } from "./RequestError";
8
8
  export { default as Endpoint, type EndpointConfig } from "./Endpoint";
9
- export { clearCache, setCacheBackend } from "./cache/Caching";
10
- export { default as LocalStorageCacheBackend } from "./cache/LocalStorageCacheBackend";
11
- export { default as LocaleForageCacheBackend } from "./cache/LocalForageCacheBackend";
9
+ export { clearClientCache, setClientCacheBackend } from "./cache/ClientCaching";
10
+ export { default as LocalStorageClientCacheBackend } from "./cache/LocalStorageClientCacheBackend";
11
+ export { default as LocalForageClientCacheBackend } from "./cache/LocalForageClientCacheBackend";
12
12
  export { default as AxiosRequestBackend } from "./backend/AxiosRequestBackend";
13
13
  export { default as FetchRequestBackend } from "./backend/FetchRequestBackend";
14
- export { default as CacheMiddleware } from "./middleware/CacheMiddleware";
14
+ export { default as ClientCacheMiddleware } from "./middleware/ClientCacheMiddleware";
15
15
  export { default as LoggingMiddleware } from "./middleware/LoggingMiddleware";
package/esm/index.js CHANGED
@@ -6,10 +6,10 @@ export * from "./ApiConstants";
6
6
  export { default as retry } from "./util/retry";
7
7
  export { isRequestError } from "./RequestError";
8
8
  export { default as Endpoint } from "./Endpoint";
9
- export { clearCache, setCacheBackend } from "./cache/Caching";
10
- export { default as LocalStorageCacheBackend } from "./cache/LocalStorageCacheBackend";
11
- export { default as LocaleForageCacheBackend } from "./cache/LocalForageCacheBackend";
9
+ export { clearClientCache, setClientCacheBackend } from "./cache/ClientCaching";
10
+ export { default as LocalStorageClientCacheBackend } from "./cache/LocalStorageClientCacheBackend";
11
+ export { default as LocalForageClientCacheBackend } from "./cache/LocalForageClientCacheBackend";
12
12
  export { default as AxiosRequestBackend } from "./backend/AxiosRequestBackend";
13
13
  export { default as FetchRequestBackend } from "./backend/FetchRequestBackend";
14
- export { default as CacheMiddleware } from "./middleware/CacheMiddleware";
14
+ export { default as ClientCacheMiddleware } from "./middleware/ClientCacheMiddleware";
15
15
  export { default as LoggingMiddleware } from "./middleware/LoggingMiddleware";
@@ -0,0 +1,7 @@
1
+ import type { RequestMiddleware } from "../ApiTypes";
2
+ export interface ClientCacheMiddlewareOptions {
3
+ defaultExpiry?: number;
4
+ predicate?: () => boolean;
5
+ }
6
+ declare const ClientCacheMiddleware: (options?: ClientCacheMiddlewareOptions) => RequestMiddleware;
7
+ export default ClientCacheMiddleware;
@@ -8,27 +8,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { CacheSource, EventResultType, RequestEvent, RequestMethod } from "../ApiConstants";
11
- import * as Caching from "../cache/Caching";
12
- const CacheMiddleware = (options = {}) => {
11
+ import * as ClientCaching from "../cache/ClientCaching";
12
+ const ClientCacheMiddleware = (options = {}) => {
13
13
  return {
14
14
  [RequestEvent.SUCCESS]: (context) => __awaiter(void 0, void 0, void 0, function* () {
15
15
  if (context.method !== RequestMethod.GET)
16
16
  return;
17
- const { cache } = context.requestConfig || {};
17
+ const { clientCache } = context.requestConfig || {};
18
18
  const shouldCache = !options.predicate || options.predicate();
19
- if (cache && shouldCache) {
20
- const expiry = typeof cache === "number" ? cache : options.defaultExpiry || Caching.DEFAULT_CACHE_EXPIRY;
21
- yield Caching.setCachedItem(context.key, context.response, expiry);
19
+ if (clientCache && shouldCache) {
20
+ const expiry = typeof clientCache === "number"
21
+ ? clientCache
22
+ : options.defaultExpiry || ClientCaching.DEFAULT_CLIENT_CACHE_EXPIRY;
23
+ yield ClientCaching.setClientCachedItem(context.key, context.response, expiry);
22
24
  }
23
25
  }),
24
26
  [RequestEvent.BEFORE_SEND]: (context) => __awaiter(void 0, void 0, void 0, function* () {
25
27
  if (context.method !== RequestMethod.GET)
26
28
  return;
27
- const { cache } = context.requestConfig || {};
29
+ const { clientCache } = context.requestConfig || {};
28
30
  const shouldCache = !options.predicate || options.predicate();
29
- if (cache && shouldCache) {
30
- if (cache) {
31
- const cachedValue = yield Caching.getCachedItem(context.key);
31
+ if (clientCache && shouldCache) {
32
+ if (clientCache) {
33
+ const cachedValue = yield ClientCaching.getClientCachedItem(context.key);
32
34
  if (cachedValue) {
33
35
  context.stats.cached = {
34
36
  is: true,
@@ -43,7 +45,7 @@ const CacheMiddleware = (options = {}) => {
43
45
  }
44
46
  }
45
47
  }
46
- if (cache === false) {
48
+ if (clientCache === false) {
47
49
  context.updateQuery({
48
50
  _bust: Math.floor(Math.random() * 9000) + 1000,
49
51
  });
@@ -55,4 +57,4 @@ const CacheMiddleware = (options = {}) => {
55
57
  }),
56
58
  };
57
59
  };
58
- export default CacheMiddleware;
60
+ export default ClientCacheMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-def",
3
- "version": "0.12.0-alpha.39",
3
+ "version": "0.12.0-alpha.40",
4
4
  "description": "Typed API definitions with middleware support",
5
5
  "main": "cjs/index.js",
6
6
  "types": "esm/index.d.ts",
@@ -18,8 +18,8 @@
18
18
  "check": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome check",
19
19
  "cleanup": "rimraf esm && rimraf cjs",
20
20
  "build": "npm run cleanup && npm run build:esm && npm run build:cjs && npm run build:cli",
21
- "build:esm": "tsc --module es2015 --target es2016 --outDir esm --preserveWatchOutput",
22
- "build:cjs": "tsc --module commonjs --target es5 --outDir cjs --preserveWatchOutput",
21
+ "build:esm": "tsc --module es2015 --target es2016 --outDir esm --preserveWatchOutput && rm -rf esm/tests",
22
+ "build:cjs": "tsc --module commonjs --target es5 --outDir cjs --preserveWatchOutput && rm -rf cjs/tests",
23
23
  "build:cli": "esbuild cli/index.ts --bundle --platform=node --outfile=bin/index.js --minify --legal-comments=none",
24
24
  "website:dev": "cd website && npm run start",
25
25
  "website:deploy": "cd website && npm run deploy"
@@ -1,6 +0,0 @@
1
- export interface CacheBackend {
2
- removeItem(key: string): Promise<void>;
3
- setItem<T>(key: string, value: T): Promise<void>;
4
- getItem<T>(key: string): Promise<T | null>;
5
- clear(): Promise<void>;
6
- }
@@ -1,10 +0,0 @@
1
- import type { CacheBackend } from "./CacheBackend";
2
- export declare const setCacheBackend: (backend: CacheBackend) => void;
3
- export declare const DEFAULT_CACHE_EXPIRY: number;
4
- export declare const clearCache: () => Promise<void>;
5
- export interface CacheEntry {
6
- data: any;
7
- expiry: number | null;
8
- }
9
- export declare const setCachedItem: <T>(key: string, value: T, expiry?: number) => Promise<T>;
10
- export declare const getCachedItem: <T = any>(key: string) => Promise<T | undefined>;
@@ -1,9 +0,0 @@
1
- import type { CacheBackend } from "./CacheBackend";
2
- export default class LocalForageCacheBackend implements CacheBackend {
3
- private readonly store;
4
- constructor(localforage: any);
5
- clear(): Promise<void>;
6
- getItem<T>(key: string): Promise<T | null>;
7
- removeItem(key: string): Promise<void>;
8
- setItem<T>(key: string, value: T): Promise<void>;
9
- }
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var LocalForageCacheBackend = /** @class */ (function () {
4
- function LocalForageCacheBackend(localforage) {
5
- this.store = localforage.createInstance({
6
- name: "requestCache",
7
- });
8
- }
9
- LocalForageCacheBackend.prototype.clear = function () {
10
- return this.store.clear();
11
- };
12
- LocalForageCacheBackend.prototype.getItem = function (key) {
13
- return this.store.getItem(key);
14
- };
15
- LocalForageCacheBackend.prototype.removeItem = function (key) {
16
- return this.store.removeItem(key);
17
- };
18
- LocalForageCacheBackend.prototype.setItem = function (key, value) {
19
- return this.store.setItem(key, value);
20
- };
21
- return LocalForageCacheBackend;
22
- }());
23
- exports.default = LocalForageCacheBackend;
@@ -1,7 +0,0 @@
1
- import type { CacheBackend } from "./CacheBackend";
2
- export default class LocalStorageCacheBackend implements CacheBackend {
3
- clear(): Promise<void>;
4
- getItem<T>(key: string): Promise<T | null>;
5
- removeItem(key: string): Promise<void>;
6
- setItem<T>(key: string, value: T): Promise<void>;
7
- }
@@ -1,7 +0,0 @@
1
- import type { RequestMiddleware } from "../ApiTypes";
2
- export interface CacheMiddlewareOptions {
3
- defaultExpiry?: number;
4
- predicate?: () => boolean;
5
- }
6
- declare const CacheMiddleware: (options?: CacheMiddlewareOptions) => RequestMiddleware;
7
- export default CacheMiddleware;
@@ -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 {};