@utiliread/http 1.16.1 → 1.17.1

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 (142) hide show
  1. package/dist/index.d.ts +227 -0
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +534 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +507 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/dist/{esm/json.d.ts → json.d.ts} +15 -17
  8. package/dist/json.d.ts.map +1 -0
  9. package/dist/json.js +100 -0
  10. package/dist/json.js.map +1 -0
  11. package/dist/json.mjs +100 -0
  12. package/dist/json.mjs.map +1 -0
  13. package/dist/{esm/jsonpatch.d.ts → jsonpatch.d.ts} +10 -9
  14. package/dist/jsonpatch.d.ts.map +1 -0
  15. package/dist/jsonpatch.js +18 -0
  16. package/dist/jsonpatch.js.map +1 -0
  17. package/dist/jsonpatch.mjs +18 -0
  18. package/dist/jsonpatch.mjs.map +1 -0
  19. package/dist/{esm/msgpack.d.ts → msgpack.d.ts} +9 -10
  20. package/dist/msgpack.d.ts.map +1 -0
  21. package/dist/msgpack.js +62 -0
  22. package/dist/msgpack.js.map +1 -0
  23. package/dist/msgpack.mjs +62 -0
  24. package/dist/msgpack.mjs.map +1 -0
  25. package/json.d.ts +1 -1
  26. package/jsonpatch.d.ts +1 -1
  27. package/karma.config.js +27 -29
  28. package/msgpack.d.ts +1 -1
  29. package/package.json +28 -20
  30. package/plugins/json/package-lock.json +145 -0
  31. package/plugins/json/package.json +11 -0
  32. package/{src/json.ts → plugins/json/src/index.ts} +124 -117
  33. package/plugins/json/tsconfig.json +3 -0
  34. package/plugins/jsonpatch/package-lock.json +124 -0
  35. package/plugins/jsonpatch/package.json +13 -0
  36. package/{src/jsonpatch.ts → plugins/jsonpatch/src/index.ts} +31 -30
  37. package/plugins/jsonpatch/tsconfig.json +3 -0
  38. package/plugins/msgpack/package-lock.json +134 -0
  39. package/plugins/msgpack/package.json +12 -0
  40. package/{src/msgpack.ts → plugins/msgpack/src/index.ts} +61 -64
  41. package/plugins/msgpack/tsconfig.json +3 -0
  42. package/src/http-builder.ts +1 -1
  43. package/src/http.spec.ts +0 -27
  44. package/src/http.ts +132 -116
  45. package/src/index.ts +8 -6
  46. package/src/mapping.ts +1 -0
  47. package/tsconfig.json +8 -17
  48. package/tsconfig.test.json +10 -0
  49. package/dist/cjs/event-aggregator.js +0 -94
  50. package/dist/cjs/event-aggregator.js.map +0 -1
  51. package/dist/cjs/events.js +0 -23
  52. package/dist/cjs/events.js.map +0 -1
  53. package/dist/cjs/helpers.js +0 -16
  54. package/dist/cjs/helpers.js.map +0 -1
  55. package/dist/cjs/http-builder.js +0 -284
  56. package/dist/cjs/http-builder.js.map +0 -1
  57. package/dist/cjs/http-error.js +0 -53
  58. package/dist/cjs/http-error.js.map +0 -1
  59. package/dist/cjs/http-response.js +0 -102
  60. package/dist/cjs/http-response.js.map +0 -1
  61. package/dist/cjs/http.js +0 -97
  62. package/dist/cjs/http.js.map +0 -1
  63. package/dist/cjs/http.spec.js +0 -144
  64. package/dist/cjs/http.spec.js.map +0 -1
  65. package/dist/cjs/index.js +0 -37
  66. package/dist/cjs/index.js.map +0 -1
  67. package/dist/cjs/json.js +0 -71
  68. package/dist/cjs/json.js.map +0 -1
  69. package/dist/cjs/jsonpatch.js +0 -15
  70. package/dist/cjs/jsonpatch.js.map +0 -1
  71. package/dist/cjs/mapping.js +0 -37
  72. package/dist/cjs/mapping.js.map +0 -1
  73. package/dist/cjs/msgpack.js +0 -173
  74. package/dist/cjs/msgpack.js.map +0 -1
  75. package/dist/cjs/pagination.js +0 -3
  76. package/dist/cjs/pagination.js.map +0 -1
  77. package/dist/cjs/problem-details.js +0 -3
  78. package/dist/cjs/problem-details.js.map +0 -1
  79. package/dist/cjs/query-string.js +0 -69
  80. package/dist/cjs/query-string.js.map +0 -1
  81. package/dist/cjs/query-string.spec.js +0 -52
  82. package/dist/cjs/query-string.spec.js.map +0 -1
  83. package/dist/cjs/status-codes.js +0 -71
  84. package/dist/cjs/status-codes.js.map +0 -1
  85. package/dist/cjs/timeout-error.js +0 -32
  86. package/dist/cjs/timeout-error.js.map +0 -1
  87. package/dist/esm/event-aggregator.d.ts +0 -11
  88. package/dist/esm/event-aggregator.js +0 -91
  89. package/dist/esm/event-aggregator.js.map +0 -1
  90. package/dist/esm/events.d.ts +0 -11
  91. package/dist/esm/events.js +0 -19
  92. package/dist/esm/events.js.map +0 -1
  93. package/dist/esm/helpers.d.ts +0 -5
  94. package/dist/esm/helpers.js +0 -10
  95. package/dist/esm/helpers.js.map +0 -1
  96. package/dist/esm/http-builder.d.ts +0 -60
  97. package/dist/esm/http-builder.js +0 -281
  98. package/dist/esm/http-builder.js.map +0 -1
  99. package/dist/esm/http-error.d.ts +0 -10
  100. package/dist/esm/http-error.js +0 -50
  101. package/dist/esm/http-error.js.map +0 -1
  102. package/dist/esm/http-response.d.ts +0 -18
  103. package/dist/esm/http-response.js +0 -99
  104. package/dist/esm/http-response.js.map +0 -1
  105. package/dist/esm/http.d.ts +0 -32
  106. package/dist/esm/http.js +0 -94
  107. package/dist/esm/http.js.map +0 -1
  108. package/dist/esm/http.spec.d.ts +0 -1
  109. package/dist/esm/http.spec.js +0 -142
  110. package/dist/esm/http.spec.js.map +0 -1
  111. package/dist/esm/index.d.ts +0 -13
  112. package/dist/esm/index.js +0 -10
  113. package/dist/esm/index.js.map +0 -1
  114. package/dist/esm/json.js +0 -69
  115. package/dist/esm/json.js.map +0 -1
  116. package/dist/esm/jsonpatch.js +0 -13
  117. package/dist/esm/jsonpatch.js.map +0 -1
  118. package/dist/esm/mapping.d.ts +0 -8
  119. package/dist/esm/mapping.js +0 -32
  120. package/dist/esm/mapping.js.map +0 -1
  121. package/dist/esm/msgpack.js +0 -171
  122. package/dist/esm/msgpack.js.map +0 -1
  123. package/dist/esm/pagination.d.ts +0 -23
  124. package/dist/esm/pagination.js +0 -2
  125. package/dist/esm/pagination.js.map +0 -1
  126. package/dist/esm/problem-details.d.ts +0 -7
  127. package/dist/esm/problem-details.js +0 -2
  128. package/dist/esm/problem-details.js.map +0 -1
  129. package/dist/esm/query-string.d.ts +0 -6
  130. package/dist/esm/query-string.js +0 -66
  131. package/dist/esm/query-string.js.map +0 -1
  132. package/dist/esm/query-string.spec.d.ts +0 -1
  133. package/dist/esm/query-string.spec.js +0 -50
  134. package/dist/esm/query-string.spec.js.map +0 -1
  135. package/dist/esm/status-codes.d.ts +0 -67
  136. package/dist/esm/status-codes.js +0 -68
  137. package/dist/esm/status-codes.js.map +0 -1
  138. package/dist/esm/timeout-error.d.ts +0 -3
  139. package/dist/esm/timeout-error.js +0 -29
  140. package/dist/esm/timeout-error.js.map +0 -1
  141. package/index.d.ts +0 -1
  142. package/tsconfig.cjs.json +0 -9
@@ -1,30 +1,31 @@
1
- import { HttpBuilder, HttpBuilderOfT } from "./http-builder";
2
- import { serialize } from "@utiliread/json";
3
- import type { Operation } from "@utiliread/jsonpatch";
4
-
5
- declare module "./http-builder" {
6
- interface HttpBuilder {
7
- withJsonPatch(operations: Operation[]): this;
8
- }
9
- interface HttpBuilderOfT<T> {
10
- withJsonPatch(operations: Operation[]): this;
11
- }
12
- }
13
-
14
- HttpBuilder.prototype.withJsonPatch = function (
15
- this: HttpBuilder,
16
- operations: Operation[]
17
- ) {
18
- this.message.content = serialize(operations);
19
- this.message.contentType = "application/json-patch+json";
20
- return this;
21
- };
22
-
23
- HttpBuilderOfT.prototype.withJsonPatch = function <T>(
24
- this: HttpBuilderOfT<T>,
25
- operations: Operation[]
26
- ) {
27
- this.message.content = serialize(operations);
28
- this.message.contentType = "application/json-patch+json";
29
- return this;
30
- };
1
+ import { HttpBuilder, HttpBuilderOfT } from "@utiliread/http";
2
+ import { serialize } from "@utiliread/json";
3
+ import type { Operation } from "@utiliread/jsonpatch";
4
+
5
+ // https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
6
+ declare module "@utiliread/http" {
7
+ interface HttpBuilder {
8
+ withJsonPatch(operations: Operation[]): this;
9
+ }
10
+ interface HttpBuilderOfT<T> {
11
+ withJsonPatch(operations: Operation[]): this;
12
+ }
13
+ }
14
+
15
+ HttpBuilder.prototype.withJsonPatch = function (
16
+ this: HttpBuilder,
17
+ operations: Operation[]
18
+ ) {
19
+ this.message.content = serialize(operations);
20
+ this.message.contentType = "application/json-patch+json";
21
+ return this;
22
+ };
23
+
24
+ HttpBuilderOfT.prototype.withJsonPatch = function <T>(
25
+ this: HttpBuilderOfT<T>,
26
+ operations: Operation[]
27
+ ) {
28
+ this.message.content = serialize(operations);
29
+ this.message.contentType = "application/json-patch+json";
30
+ return this;
31
+ };
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.json"
3
+ }
@@ -0,0 +1,134 @@
1
+ {
2
+ "name": "@utiliread/http/msgpack",
3
+ "lockfileVersion": 2,
4
+ "requires": true,
5
+ "packages": {
6
+ "": {
7
+ "name": "@utiliread/http/msgpack",
8
+ "dependencies": {
9
+ "@msgpack/msgpack": "^2.7.2",
10
+ "@utiliread/http": "../../",
11
+ "@utiliread/msgpack": "^1.0.1"
12
+ }
13
+ },
14
+ "../..": {
15
+ "name": "@utiliread/http",
16
+ "version": "1.16.2",
17
+ "devDependencies": {
18
+ "@parcel/packager-ts": "^2.5.0",
19
+ "@parcel/transformer-typescript-types": "^2.5.0",
20
+ "@types/chai": "^4.2.16",
21
+ "@types/luxon": "^2.0.5",
22
+ "@types/mocha": "^9.0.0",
23
+ "@utiliread/json": "^1.0.2",
24
+ "@utiliread/jsonpatch": "^1.0.0",
25
+ "@utiliread/msgpack": "^1.0.1",
26
+ "chai": "^4.3.4",
27
+ "karma": "^6.3.8",
28
+ "karma-chai": "^0.1.0",
29
+ "karma-chrome-launcher": "^3.1.0",
30
+ "karma-mocha": "^2.0.1",
31
+ "karma-typescript": "^5.5.1",
32
+ "luxon": "^2.1.0",
33
+ "mocha": "^9.1.3",
34
+ "parcel": "^2.5.0",
35
+ "rimraf": "^3.0.2",
36
+ "typescript": "^4.2.4"
37
+ },
38
+ "peerDependencies": {
39
+ "@utiliread/json": "^1.0.2",
40
+ "@utiliread/jsonpatch": "^1.0.0",
41
+ "@utiliread/msgpack": "^1.0.1",
42
+ "luxon": "^2.1.0"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "@utiliread/json": {
46
+ "optional": true
47
+ },
48
+ "@utiliread/jsonpatch": {
49
+ "optional": true
50
+ },
51
+ "@utiliread/msgpack": {
52
+ "optional": true
53
+ }
54
+ }
55
+ },
56
+ "node_modules/@msgpack/msgpack": {
57
+ "version": "2.7.2",
58
+ "resolved": "https://registry.npmjs.org/@msgpack/msgpack/-/msgpack-2.7.2.tgz",
59
+ "integrity": "sha512-rYEi46+gIzufyYUAoHDnRzkWGxajpD9vVXFQ3g1vbjrBm6P7MBmm+s/fqPa46sxa+8FOUdEuRQKaugo5a4JWpw==",
60
+ "engines": {
61
+ "node": ">= 10"
62
+ }
63
+ },
64
+ "node_modules/@utiliread/http": {
65
+ "resolved": "../..",
66
+ "link": true
67
+ },
68
+ "node_modules/@utiliread/msgpack": {
69
+ "version": "1.0.1",
70
+ "resolved": "https://registry.npmjs.org/@utiliread/msgpack/-/msgpack-1.0.1.tgz",
71
+ "integrity": "sha512-M5bG7drhwrp4YLZhL1Al+K2SkFw3rVhsCVJvi+yV2n5M8nD/qnLuMRu5ZvjYT5iNYKfo1MJWUXfSbq30a0GqLA==",
72
+ "dependencies": {
73
+ "@msgpack/msgpack": "^2.4.0"
74
+ },
75
+ "peerDependencies": {
76
+ "luxon": "^2.1.0"
77
+ }
78
+ },
79
+ "node_modules/luxon": {
80
+ "version": "2.4.0",
81
+ "resolved": "https://registry.npmjs.org/luxon/-/luxon-2.4.0.tgz",
82
+ "integrity": "sha512-w+NAwWOUL5hO0SgwOHsMBAmZ15SoknmQXhSO0hIbJCAmPKSsGeK8MlmhYh2w6Iib38IxN2M+/ooXWLbeis7GuA==",
83
+ "peer": true,
84
+ "engines": {
85
+ "node": ">=12"
86
+ }
87
+ }
88
+ },
89
+ "dependencies": {
90
+ "@msgpack/msgpack": {
91
+ "version": "2.7.2",
92
+ "resolved": "https://registry.npmjs.org/@msgpack/msgpack/-/msgpack-2.7.2.tgz",
93
+ "integrity": "sha512-rYEi46+gIzufyYUAoHDnRzkWGxajpD9vVXFQ3g1vbjrBm6P7MBmm+s/fqPa46sxa+8FOUdEuRQKaugo5a4JWpw=="
94
+ },
95
+ "@utiliread/http": {
96
+ "version": "file:../..",
97
+ "requires": {
98
+ "@parcel/packager-ts": "^2.5.0",
99
+ "@parcel/transformer-typescript-types": "^2.5.0",
100
+ "@types/chai": "^4.2.16",
101
+ "@types/luxon": "^2.0.5",
102
+ "@types/mocha": "^9.0.0",
103
+ "@utiliread/json": "^1.0.2",
104
+ "@utiliread/jsonpatch": "^1.0.0",
105
+ "@utiliread/msgpack": "^1.0.1",
106
+ "chai": "^4.3.4",
107
+ "karma": "^6.3.8",
108
+ "karma-chai": "^0.1.0",
109
+ "karma-chrome-launcher": "^3.1.0",
110
+ "karma-mocha": "^2.0.1",
111
+ "karma-typescript": "^5.5.1",
112
+ "luxon": "^2.1.0",
113
+ "mocha": "^9.1.3",
114
+ "parcel": "^2.5.0",
115
+ "rimraf": "^3.0.2",
116
+ "typescript": "^4.2.4"
117
+ }
118
+ },
119
+ "@utiliread/msgpack": {
120
+ "version": "1.0.1",
121
+ "resolved": "https://registry.npmjs.org/@utiliread/msgpack/-/msgpack-1.0.1.tgz",
122
+ "integrity": "sha512-M5bG7drhwrp4YLZhL1Al+K2SkFw3rVhsCVJvi+yV2n5M8nD/qnLuMRu5ZvjYT5iNYKfo1MJWUXfSbq30a0GqLA==",
123
+ "requires": {
124
+ "@msgpack/msgpack": "^2.4.0"
125
+ }
126
+ },
127
+ "luxon": {
128
+ "version": "2.4.0",
129
+ "resolved": "https://registry.npmjs.org/luxon/-/luxon-2.4.0.tgz",
130
+ "integrity": "sha512-w+NAwWOUL5hO0SgwOHsMBAmZ15SoknmQXhSO0hIbJCAmPKSsGeK8MlmhYh2w6Iib38IxN2M+/ooXWLbeis7GuA==",
131
+ "peer": true
132
+ }
133
+ }
134
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@utiliread/http/msgpack",
3
+ "source": "./src/index.ts",
4
+ "main": "../../dist/msgpack.js",
5
+ "module": "../../dist/msgpack.mjs",
6
+ "types": "../../dist/msgpack.d.ts",
7
+ "dependencies": {
8
+ "@msgpack/msgpack": "^2.7.2",
9
+ "@utiliread/http": "../../",
10
+ "@utiliread/msgpack": "^1.0.1"
11
+ }
12
+ }
@@ -1,64 +1,61 @@
1
- import { deserialize } from "@utiliread/msgpack";
2
- import { HttpBuilder } from "./http-builder";
3
- import { decodeArrayStream, decodeAsync } from "@msgpack/msgpack";
4
- import { getMapper, Mapper, Type } from "./mapping";
5
- import { HttpResponse } from "./http-response";
6
-
7
- type TypeOrMapper<T> = Type<T> | Mapper<T>;
8
-
9
- declare module "./http-builder" {
10
- interface HttpBuilder {
11
- expectMessagePack<T>(
12
- typeOrMapper?: TypeOrMapper<T>
13
- ): HttpBuilderOfT<T>;
14
- expectMessagePackArray<T>(
15
- typeOrMapper?: TypeOrMapper<T>
16
- ): HttpBuilderOfT<T[]>;
17
- streamMessagePackArray<T>(
18
- typeOrMapper?: TypeOrMapper<T>
19
- ): HttpBuilderOfT<AsyncGenerator<T, void, unknown>>;
20
- }
21
- }
22
-
23
- HttpBuilder.prototype.expectMessagePack = function <T>(
24
- this: HttpBuilder,
25
- typeOrMapper?: TypeOrMapper<T>
26
- ) {
27
- this.message.headers.set("Accept", "application/x-msgpack");
28
- return this.useHandler(async (response) => {
29
- const itemFactory = getMapper(deserialize, typeOrMapper);
30
- const decoded = await decodeAsync(response.rawResponse.body!);
31
- return itemFactory(decoded);
32
- });
33
- };
34
-
35
- HttpBuilder.prototype.expectMessagePackArray = function <T>(
36
- this: HttpBuilder,
37
- typeOrMapper?: TypeOrMapper<T>
38
- ) {
39
- this.message.headers.set("Accept", "application/x-msgpack");
40
- return this.useHandler(async (response) => {
41
- const items: T[] = [];
42
- const itemFactory = getMapper(deserialize, typeOrMapper);
43
- for await (const item of decodeArrayStream(response.rawResponse.body!)) {
44
- items.push(itemFactory(item));
45
- }
46
- return items;
47
- });
48
- };
49
-
50
- HttpBuilder.prototype.streamMessagePackArray = function <T>(
51
- this: HttpBuilder,
52
- typeOrMapper?: TypeOrMapper<T>
53
- ) {
54
- this.message.headers.set("Accept", "application/x-msgpack");
55
-
56
- async function* handler(response: HttpResponse) {
57
- const itemFactory = getMapper(deserialize, typeOrMapper);
58
- for await (const item of decodeArrayStream(response.rawResponse.body!)) {
59
- yield itemFactory(item);
60
- }
61
- }
62
-
63
- return this.useHandler((response) => Promise.resolve(handler(response)));
64
- };
1
+ import { deserialize } from "@utiliread/msgpack";
2
+ import { HttpBuilder, HttpBuilderOfT, HttpResponse } from "@utiliread/http";
3
+ import { decodeArrayStream, decodeAsync } from "@msgpack/msgpack";
4
+ import type { TypeOrMapper } from "../../../src/mapping";
5
+ import { getMapper } from "../../../src/mapping";
6
+
7
+ // https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
8
+ declare module "@utiliread/http" {
9
+ interface HttpBuilder {
10
+ expectMessagePack<T>(typeOrMapper?: TypeOrMapper<T>): HttpBuilderOfT<T>;
11
+ expectMessagePackArray<T>(
12
+ typeOrMapper?: TypeOrMapper<T>
13
+ ): HttpBuilderOfT<T[]>;
14
+ streamMessagePackArray<T>(
15
+ typeOrMapper?: TypeOrMapper<T>
16
+ ): HttpBuilderOfT<AsyncGenerator<T, void, unknown>>;
17
+ }
18
+ }
19
+
20
+ HttpBuilder.prototype.expectMessagePack = function <T>(
21
+ this: HttpBuilder,
22
+ typeOrMapper?: TypeOrMapper<T>
23
+ ) {
24
+ this.message.headers.set("Accept", "application/x-msgpack");
25
+ return this.useHandler(async (response) => {
26
+ const itemFactory = getMapper(deserialize, typeOrMapper);
27
+ const decoded = await decodeAsync(response.rawResponse.body!);
28
+ return itemFactory(decoded);
29
+ });
30
+ };
31
+
32
+ HttpBuilder.prototype.expectMessagePackArray = function <T>(
33
+ this: HttpBuilder,
34
+ typeOrMapper?: TypeOrMapper<T>
35
+ ) {
36
+ this.message.headers.set("Accept", "application/x-msgpack");
37
+ return this.useHandler(async (response) => {
38
+ const items: T[] = [];
39
+ const itemFactory = getMapper(deserialize, typeOrMapper);
40
+ for await (const item of decodeArrayStream(response.rawResponse.body!)) {
41
+ items.push(itemFactory(item));
42
+ }
43
+ return items;
44
+ });
45
+ };
46
+
47
+ HttpBuilder.prototype.streamMessagePackArray = function <T>(
48
+ this: HttpBuilder,
49
+ typeOrMapper?: TypeOrMapper<T>
50
+ ) {
51
+ this.message.headers.set("Accept", "application/x-msgpack");
52
+
53
+ async function* handler(response: HttpResponse) {
54
+ const itemFactory = getMapper(deserialize, typeOrMapper);
55
+ for await (const item of decodeArrayStream(response.rawResponse.body!)) {
56
+ yield itemFactory(item);
57
+ }
58
+ }
59
+
60
+ return this.useHandler((response) => Promise.resolve(handler(response)));
61
+ };
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.json"
3
+ }
@@ -2,7 +2,7 @@ import { Fetch } from './http';
2
2
  import { HttpResponse, HttpResponseOfT } from './http-response';
3
3
  import { TimeoutError } from './timeout-error';
4
4
  import { EventAggregator } from './event-aggregator';
5
- import { Http } from '.';
5
+ import { Http } from './http';
6
6
 
7
7
  export class HttpBuilder {
8
8
  private _ensureSuccessStatusCode = true;
package/src/http.spec.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { expect } from "chai";
2
2
  import { Http } from "./http";
3
- import "./json";
4
3
 
5
4
  describe("static http", () => {
6
5
  it("can get", () => {
@@ -71,32 +70,6 @@ describe("instance http", () => {
71
70
  const builder = http.get("/hello");
72
71
  expect(builder.getUrl()).to.equal("base/hello");
73
72
  });
74
-
75
- it("can post json, with then expect", async () => {
76
- const user = await new Http()
77
- .post("https://reqres.in/api/users")
78
- .withJson({
79
- name: "morpheus",
80
- job: "leader",
81
- })
82
- .expectJson<{ name: string; job: string }>()
83
- .transfer();
84
-
85
- expect(user.name).to.equal("morpheus");
86
- });
87
-
88
- it("can post json, expect then with", async () => {
89
- const user = await new Http()
90
- .post("https://reqres.in/api/users")
91
- .expectJson<{ name: string; job: string }>()
92
- .withJson({
93
- name: "morpheus",
94
- job: "leader",
95
- })
96
- .transfer();
97
-
98
- expect(user.name).to.equal("morpheus");
99
- }).timeout(5000);
100
73
  });
101
74
 
102
75
  function fakeFetch(input: RequestInfo, init?: RequestInit): Promise<Response> {
package/src/http.ts CHANGED
@@ -1,122 +1,138 @@
1
- import { EventAggregator } from './event-aggregator';
2
- import { HttpBuilder, HttpMethod, Message } from './http-builder';
3
- import { HttpResponse, HttpResponseOfT } from './http-response';
4
- import { QueryString } from './query-string';
1
+ import { EventAggregator } from "./event-aggregator";
2
+ import type { Subscription } from "./event-aggregator";
3
+ import { HttpBuilder, HttpMethod, Message } from "./http-builder";
4
+ import { HttpResponse, HttpResponseOfT } from "./http-response";
5
+ import { QueryString } from "./query-string";
5
6
 
6
- export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>;
7
+ export type Fetch = (
8
+ input: RequestInfo,
9
+ init?: RequestInit
10
+ ) => Promise<Response>;
7
11
 
8
12
  export class Http {
9
- static defaults: HttpOptions = {
10
- fetch: self.fetch ? self.fetch.bind(self) : undefined,
11
- }
12
- private static instance?: Http;
13
- options: HttpOptions;
14
- /** @internal */
15
- _onSend = new EventAggregator<[Message]>();
16
- /** @internal */
17
- _onSent = new EventAggregator<[HttpResponse, Message]>();
18
- /** @internal */
19
- _onReceived = new EventAggregator<[HttpResponseOfT<any>, Message, any]>();
20
-
21
- constructor(options?: Partial<HttpOptions>) {
22
- this.options = Object.assign({}, Http.defaults, options); // Later sources' properties overwrite earlier ones.
23
- }
24
-
25
- static request(method: HttpMethod, url: string, params?: any) {
26
- return this.getInstance().request(method, url, params);
27
- }
28
-
29
- static head(url: string, params?: any) {
30
- return this.getInstance().head(url, params);
31
- }
32
-
33
- static post(url: string, params?: any) {
34
- return this.getInstance().post(url, params);
35
- }
36
-
37
- static get(url: string, params?: any) {
38
- return this.getInstance().get(url, params);
39
- }
40
-
41
- static put(url: string, params?: any) {
42
- return this.getInstance().put(url, params);
43
- }
44
-
45
- static patch(url: string, params?: any) {
46
- return this.getInstance().patch(url, params);
47
- }
48
-
49
- static delete(url: string, params?: any) {
50
- return this.getInstance().delete(url, params);
51
- }
52
-
53
- private static getInstance() {
54
- if (!this.instance) {
55
- this.instance = new Http();
56
- // The singleton instance should always use the static options
57
- this.instance.options = Http.defaults;
58
- }
59
- return this.instance;
60
- }
61
-
62
- request(method: HttpMethod, url: string, params?: any) {
63
- const message = {
64
- method,
65
- url: url + QueryString.serialize(params),
66
- headers: new Headers(),
67
- properties: {},
68
- };
69
- const fetch = this.options.fetch;
70
- if (!fetch) {
71
- throw Error('fetch() is not properly configured');
72
- }
73
- const builder = new HttpBuilder(message, {
74
- fetch,
75
- timeout: this.options.timeout,
76
- baseUrl: this.options.baseUrl,
77
- }, this);
78
- return builder;
79
- }
80
-
81
- head(url: string, params?: any) {
82
- return this.request('HEAD', url, params);
83
- }
84
-
85
- post(url: string, params?: any) {
86
- return this.request('POST', url, params);
87
- }
88
-
89
- get(url: string, params?: any) {
90
- return this.request('GET', url, params);
91
- }
92
-
93
- put(url: string, params?: any) {
94
- return this.request('PUT', url, params);
95
- }
96
-
97
- patch(url: string, params?: any) {
98
- return this.request('PATCH', url, params);
99
- }
100
-
101
- delete(url: string, params?: any) {
102
- return this.request('DELETE', url, params);
103
- }
104
-
105
- onSend(callback: (request: Message) => void | Promise<void>) {
106
- return this._onSend.subscribe(callback);
107
- }
108
-
109
- onSent(callback: (response: HttpResponse, request: Message) => void | Promise<void>) {
110
- return this._onSent.subscribe(callback);
111
- }
112
-
113
- onReceived(callback: (response: HttpResponseOfT<any>, request: Message, value: any) => void | Promise<void>) {
114
- return this._onReceived.subscribe(callback);
115
- }
13
+ static defaults: HttpOptions = {
14
+ fetch: self.fetch ? self.fetch.bind(self) : undefined,
15
+ };
16
+ private static instance?: Http;
17
+ options: HttpOptions;
18
+ /** @internal */
19
+ _onSend = new EventAggregator<[Message]>();
20
+ /** @internal */
21
+ _onSent = new EventAggregator<[HttpResponse, Message]>();
22
+ /** @internal */
23
+ _onReceived = new EventAggregator<[HttpResponseOfT<any>, Message, any]>();
24
+
25
+ constructor(options?: Partial<HttpOptions>) {
26
+ this.options = Object.assign({}, Http.defaults, options); // Later sources' properties overwrite earlier ones.
27
+ }
28
+
29
+ static request(method: HttpMethod, url: string, params?: any) {
30
+ return this.getInstance().request(method, url, params);
31
+ }
32
+
33
+ static head(url: string, params?: any) {
34
+ return this.getInstance().head(url, params);
35
+ }
36
+
37
+ static post(url: string, params?: any) {
38
+ return this.getInstance().post(url, params);
39
+ }
40
+
41
+ static get(url: string, params?: any) {
42
+ return this.getInstance().get(url, params);
43
+ }
44
+
45
+ static put(url: string, params?: any) {
46
+ return this.getInstance().put(url, params);
47
+ }
48
+
49
+ static patch(url: string, params?: any) {
50
+ return this.getInstance().patch(url, params);
51
+ }
52
+
53
+ static delete(url: string, params?: any) {
54
+ return this.getInstance().delete(url, params);
55
+ }
56
+
57
+ private static getInstance() {
58
+ if (!this.instance) {
59
+ this.instance = new Http();
60
+ // The singleton instance should always use the static options
61
+ this.instance.options = Http.defaults;
62
+ }
63
+ return this.instance;
64
+ }
65
+
66
+ request(method: HttpMethod, url: string, params?: any) {
67
+ const message = {
68
+ method,
69
+ url: url + QueryString.serialize(params),
70
+ headers: new Headers(),
71
+ properties: {},
72
+ };
73
+ const fetch = this.options.fetch;
74
+ if (!fetch) {
75
+ throw Error("fetch() is not properly configured");
76
+ }
77
+ const builder = new HttpBuilder(
78
+ message,
79
+ {
80
+ fetch,
81
+ timeout: this.options.timeout,
82
+ baseUrl: this.options.baseUrl,
83
+ },
84
+ this
85
+ );
86
+ return builder;
87
+ }
88
+
89
+ head(url: string, params?: any) {
90
+ return this.request("HEAD", url, params);
91
+ }
92
+
93
+ post(url: string, params?: any) {
94
+ return this.request("POST", url, params);
95
+ }
96
+
97
+ get(url: string, params?: any) {
98
+ return this.request("GET", url, params);
99
+ }
100
+
101
+ put(url: string, params?: any) {
102
+ return this.request("PUT", url, params);
103
+ }
104
+
105
+ patch(url: string, params?: any) {
106
+ return this.request("PATCH", url, params);
107
+ }
108
+
109
+ delete(url: string, params?: any) {
110
+ return this.request("DELETE", url, params);
111
+ }
112
+
113
+ onSend(callback: (request: Message) => void | Promise<void>): Subscription {
114
+ return this._onSend.subscribe(callback);
115
+ }
116
+
117
+ onSent(
118
+ callback: (response: HttpResponse, request: Message) => void | Promise<void>
119
+ ): Subscription {
120
+ return this._onSent.subscribe(callback);
121
+ }
122
+
123
+ onReceived(
124
+ callback: (
125
+ response: HttpResponseOfT<any>,
126
+ request: Message,
127
+ value: any
128
+ ) => void | Promise<void>
129
+ ): Subscription {
130
+ return this._onReceived.subscribe(callback);
131
+ }
116
132
  }
117
133
 
118
134
  export interface HttpOptions {
119
- fetch?: Fetch,
120
- timeout?: number,
121
- baseUrl?: string,
122
- }
135
+ fetch?: Fetch;
136
+ timeout?: number;
137
+ baseUrl?: string;
138
+ }