@utiliread/http 1.17.8 → 1.19.0

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 (160) hide show
  1. package/.vscode/settings.json +2 -2
  2. package/dist/index.d.ts +231 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +564 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +535 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/dist/json.d.ts +15 -0
  9. package/dist/json.d.ts.map +1 -0
  10. package/dist/json.js +73 -0
  11. package/dist/json.js.map +1 -0
  12. package/dist/json.mjs +73 -0
  13. package/dist/json.mjs.map +1 -0
  14. package/dist/{esm/jsonpatch/index.d.ts → jsonpatch.d.ts} +10 -9
  15. package/dist/jsonpatch.d.ts.map +1 -0
  16. package/dist/jsonpatch.js +18 -0
  17. package/dist/jsonpatch.js.map +1 -0
  18. package/dist/jsonpatch.mjs +18 -0
  19. package/dist/jsonpatch.mjs.map +1 -0
  20. package/dist/msgpack.d.ts +9 -0
  21. package/dist/msgpack.d.ts.map +1 -0
  22. package/dist/msgpack.js +35 -0
  23. package/dist/msgpack.js.map +1 -0
  24. package/dist/msgpack.mjs +35 -0
  25. package/dist/msgpack.mjs.map +1 -0
  26. package/json.d.ts +1 -1
  27. package/jsonpatch.d.ts +1 -1
  28. package/mapper.d.ts +1 -0
  29. package/msgpack.d.ts +1 -1
  30. package/package.json +40 -38
  31. package/plugins/json/node_modules/@utiliread/http/package.json +4 -0
  32. package/plugins/json/package.json +15 -0
  33. package/plugins/json/src/index.ts +128 -122
  34. package/plugins/json/tsconfig.json +1 -2
  35. package/plugins/jsonpatch/node_modules/@utiliread/http/package.json +4 -0
  36. package/plugins/jsonpatch/package.json +16 -0
  37. package/plugins/jsonpatch/src/index.ts +31 -31
  38. package/plugins/jsonpatch/tsconfig.json +1 -2
  39. package/plugins/msgpack/node_modules/@utiliread/http/package.json +4 -0
  40. package/plugins/msgpack/package.json +16 -0
  41. package/plugins/msgpack/src/index.ts +60 -60
  42. package/plugins/msgpack/tsconfig.json +1 -2
  43. package/src/event-aggregator.ts +21 -21
  44. package/src/events.ts +39 -39
  45. package/src/helpers.ts +6 -6
  46. package/src/http-builder.ts +252 -218
  47. package/src/http-error.ts +30 -23
  48. package/src/http-response.ts +1 -1
  49. package/src/http.spec.ts +1 -1
  50. package/src/http.ts +8 -5
  51. package/src/index.ts +1 -1
  52. package/src/mapper.ts +52 -0
  53. package/src/pagination.ts +16 -16
  54. package/src/problem-details.ts +6 -6
  55. package/src/query-string.spec.ts +59 -43
  56. package/src/query-string.ts +56 -58
  57. package/src/status-codes.ts +67 -67
  58. package/src/timeout-error.ts +7 -7
  59. package/tsconfig.cjs.json +1 -0
  60. package/tsconfig.json +1 -2
  61. package/web-test-runner.config.mjs +7 -0
  62. package/dist/cjs/event-aggregator.js +0 -30
  63. package/dist/cjs/event-aggregator.js.map +0 -1
  64. package/dist/cjs/events.js +0 -19
  65. package/dist/cjs/events.js.map +0 -1
  66. package/dist/cjs/helpers.js +0 -16
  67. package/dist/cjs/helpers.js.map +0 -1
  68. package/dist/cjs/http-builder.js +0 -192
  69. package/dist/cjs/http-builder.js.map +0 -1
  70. package/dist/cjs/http-error.js +0 -30
  71. package/dist/cjs/http-error.js.map +0 -1
  72. package/dist/cjs/http-response.js +0 -51
  73. package/dist/cjs/http-response.js.map +0 -1
  74. package/dist/cjs/http.js +0 -96
  75. package/dist/cjs/http.js.map +0 -1
  76. package/dist/cjs/http.spec.js +0 -69
  77. package/dist/cjs/http.spec.js.map +0 -1
  78. package/dist/cjs/index.js +0 -38
  79. package/dist/cjs/index.js.map +0 -1
  80. package/dist/cjs/json/index.js +0 -74
  81. package/dist/cjs/json/index.js.map +0 -1
  82. package/dist/cjs/jsonpatch/index.js +0 -15
  83. package/dist/cjs/jsonpatch/index.js.map +0 -1
  84. package/dist/cjs/mapping.js +0 -37
  85. package/dist/cjs/mapping.js.map +0 -1
  86. package/dist/cjs/msgpack/index.js +0 -35
  87. package/dist/cjs/msgpack/index.js.map +0 -1
  88. package/dist/cjs/pagination.js +0 -3
  89. package/dist/cjs/pagination.js.map +0 -1
  90. package/dist/cjs/problem-details.js +0 -3
  91. package/dist/cjs/problem-details.js.map +0 -1
  92. package/dist/cjs/query-string.js +0 -66
  93. package/dist/cjs/query-string.js.map +0 -1
  94. package/dist/cjs/query-string.spec.js +0 -52
  95. package/dist/cjs/query-string.spec.js.map +0 -1
  96. package/dist/cjs/status-codes.js +0 -71
  97. package/dist/cjs/status-codes.js.map +0 -1
  98. package/dist/cjs/timeout-error.js +0 -14
  99. package/dist/cjs/timeout-error.js.map +0 -1
  100. package/dist/esm/event-aggregator.d.ts +0 -11
  101. package/dist/esm/event-aggregator.js +0 -26
  102. package/dist/esm/event-aggregator.js.map +0 -1
  103. package/dist/esm/events.d.ts +0 -11
  104. package/dist/esm/events.js +0 -15
  105. package/dist/esm/events.js.map +0 -1
  106. package/dist/esm/helpers.d.ts +0 -5
  107. package/dist/esm/helpers.js +0 -10
  108. package/dist/esm/helpers.js.map +0 -1
  109. package/dist/esm/http-builder.d.ts +0 -60
  110. package/dist/esm/http-builder.js +0 -187
  111. package/dist/esm/http-builder.js.map +0 -1
  112. package/dist/esm/http-error.d.ts +0 -10
  113. package/dist/esm/http-error.js +0 -26
  114. package/dist/esm/http-error.js.map +0 -1
  115. package/dist/esm/http-response.d.ts +0 -18
  116. package/dist/esm/http-response.js +0 -46
  117. package/dist/esm/http-response.js.map +0 -1
  118. package/dist/esm/http.d.ts +0 -33
  119. package/dist/esm/http.js +0 -92
  120. package/dist/esm/http.js.map +0 -1
  121. package/dist/esm/http.spec.d.ts +0 -1
  122. package/dist/esm/http.spec.js +0 -67
  123. package/dist/esm/http.spec.js.map +0 -1
  124. package/dist/esm/index.d.ts +0 -15
  125. package/dist/esm/index.js +0 -11
  126. package/dist/esm/index.js.map +0 -1
  127. package/dist/esm/json/index.d.ts +0 -15
  128. package/dist/esm/json/index.js +0 -72
  129. package/dist/esm/json/index.js.map +0 -1
  130. package/dist/esm/jsonpatch/index.js +0 -13
  131. package/dist/esm/jsonpatch/index.js.map +0 -1
  132. package/dist/esm/mapping.d.ts +0 -9
  133. package/dist/esm/mapping.js +0 -32
  134. package/dist/esm/mapping.js.map +0 -1
  135. package/dist/esm/msgpack/index.d.ts +0 -8
  136. package/dist/esm/msgpack/index.js +0 -33
  137. package/dist/esm/msgpack/index.js.map +0 -1
  138. package/dist/esm/pagination.d.ts +0 -23
  139. package/dist/esm/pagination.js +0 -2
  140. package/dist/esm/pagination.js.map +0 -1
  141. package/dist/esm/problem-details.d.ts +0 -7
  142. package/dist/esm/problem-details.js +0 -2
  143. package/dist/esm/problem-details.js.map +0 -1
  144. package/dist/esm/query-string.d.ts +0 -6
  145. package/dist/esm/query-string.js +0 -62
  146. package/dist/esm/query-string.js.map +0 -1
  147. package/dist/esm/query-string.spec.d.ts +0 -1
  148. package/dist/esm/query-string.spec.js +0 -50
  149. package/dist/esm/query-string.spec.js.map +0 -1
  150. package/dist/esm/status-codes.d.ts +0 -67
  151. package/dist/esm/status-codes.js +0 -68
  152. package/dist/esm/status-codes.js.map +0 -1
  153. package/dist/esm/timeout-error.d.ts +0 -3
  154. package/dist/esm/timeout-error.js +0 -10
  155. package/dist/esm/timeout-error.js.map +0 -1
  156. package/karma.config.js +0 -29
  157. package/plugins/json/tsconfig.cjs.json +0 -8
  158. package/plugins/jsonpatch/tsconfig.cjs.json +0 -8
  159. package/plugins/msgpack/tsconfig.cjs.json +0 -8
  160. package/src/mapping.ts +0 -44
@@ -1,122 +1,128 @@
1
- import { HttpBuilder, HttpBuilderOfT, Mapping } from "@utiliread/http";
2
- import type {
3
- InfinitePaginationResult,
4
- PaginationResult,
5
- } from "@utiliread/http";
6
- import { deserialize, serialize } from "@utiliread/json";
7
-
8
- // https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
9
- declare module "@utiliread/http" {
10
- interface HttpBuilder {
11
- withJson(content: any): this;
12
-
13
- expectJson<T>(typeOrMapper?: Mapping.TypeOrMapper<T>): HttpBuilderOfT<T>;
14
- expectJsonArray<T>(typeOrMapper: Mapping.TypeOrMapper<T>): HttpBuilderOfT<T[]>;
15
- expectJsonNullableArray<T>(
16
- typeOrMapper: Mapping.TypeOrMapper<T>
17
- ): HttpBuilderOfT<(T | null)[]>;
18
- expectJsonPaginationResult<T>(
19
- typeOrMapper: Mapping.TypeOrMapper<T>
20
- ): HttpBuilderOfT<PaginationResult<T>>;
21
- expectJsonInfinitePaginationResult<T>(
22
- typeOrMapper: Mapping.TypeOrMapper<T>
23
- ): HttpBuilderOfT<InfinitePaginationResult<T>>;
24
- }
25
- interface HttpBuilderOfT<T> {
26
- withJson(content: any): this;
27
- }
28
- }
29
-
30
- HttpBuilder.prototype.withJson = function (this: HttpBuilder, content: any) {
31
- this.message.content = serialize(content);
32
- this.message.contentType = "application/json";
33
- return this;
34
- };
35
-
36
- HttpBuilderOfT.prototype.withJson = function <T>(
37
- this: HttpBuilderOfT<T>,
38
- content: any
39
- ) {
40
- this.message.content = serialize(content);
41
- this.message.contentType = "application/json";
42
- return this;
43
- };
44
-
45
- HttpBuilder.prototype.expectJson = function <T>(
46
- this: HttpBuilder,
47
- typeOrMapper?: Mapping.TypeOrMapper<T>
48
- ) {
49
- this.message.headers.set("Accept", "application/json");
50
- return this.useHandler((response) => {
51
- return response.rawResponse
52
- .json()
53
- .then((x) => Mapping.getMapper(deserialize, typeOrMapper)(x));
54
- });
55
- };
56
-
57
- HttpBuilder.prototype.expectJsonArray = function <T>(
58
- this: HttpBuilder,
59
- typeOrMapper: Mapping.TypeOrMapper<T>
60
- ) {
61
- this.message.headers.set("Accept", "application/json");
62
- return this.useHandler((response) => {
63
- return response.rawResponse.json().then((x: any[]) => {
64
- const itemFactory = Mapping.getMapper(deserialize, typeOrMapper);
65
- return x.map(itemFactory);
66
- });
67
- });
68
- };
69
-
70
- HttpBuilder.prototype.expectJsonNullableArray = function <T>(
71
- this: HttpBuilder,
72
- typeOrMapper: Mapping.TypeOrMapper<T>
73
- ): HttpBuilderOfT<(T | null)[]> {
74
- this.message.headers.set("Accept", "application/json");
75
- return this.useHandler((response) => {
76
- return response.rawResponse.json().then((x: any[]) => {
77
- const itemFactory = Mapping.getNullableMapper(deserialize, typeOrMapper);
78
- return x.map(itemFactory);
79
- });
80
- });
81
- };
82
-
83
- HttpBuilder.prototype.expectJsonPaginationResult = function <T>(
84
- this: HttpBuilder,
85
- typeOrMapper: Mapping.TypeOrMapper<T>
86
- ) {
87
- this.message.headers.set("Accept", "application/json");
88
- return this.useHandler((response) => {
89
- return response.rawResponse.json().then((x: PaginationResult<any>) => {
90
- const itemFactory = Mapping.getMapper(deserialize, typeOrMapper);
91
- return {
92
- meta: {
93
- pageCount: x.meta.pageCount,
94
- pageSize: x.meta.pageSize,
95
- totalItems: x.meta.totalItems,
96
- },
97
- data: x.data.map(itemFactory),
98
- };
99
- });
100
- });
101
- };
102
-
103
- HttpBuilder.prototype.expectJsonInfinitePaginationResult = function <T>(
104
- this: HttpBuilder,
105
- typeOrMapper: Mapping.TypeOrMapper<T>
106
- ) {
107
- this.message.headers.set("Accept", "application/json");
108
- return this.useHandler((response) => {
109
- return response.rawResponse
110
- .json()
111
- .then((x: InfinitePaginationResult<any>) => {
112
- const itemFactory = Mapping.getMapper(deserialize, typeOrMapper);
113
- return {
114
- meta: {
115
- pageSize: x.meta.pageSize,
116
- continuationToken: x.meta.continuationToken,
117
- },
118
- data: x.data.map(itemFactory),
119
- };
120
- });
121
- });
122
- };
1
+ import {
2
+ HttpBuilder,
3
+ HttpBuilderOfT,
4
+ getMapper,
5
+ getNullableMapper
6
+ } from "@utiliread/http";
7
+ import type {
8
+ InfinitePaginationResult,
9
+ PaginationResult,
10
+ TypeOrMapper
11
+ } from "@utiliread/http";
12
+ import { deserialize, serialize } from "@utiliread/json";
13
+
14
+ // https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
15
+ declare module "@utiliread/http" {
16
+ interface HttpBuilder {
17
+ withJson(content: any): this;
18
+
19
+ expectJson<T>(typeOrMapper?: TypeOrMapper<T>): HttpBuilderOfT<T>;
20
+ expectJsonArray<T>(typeOrMapper: TypeOrMapper<T>): HttpBuilderOfT<T[]>;
21
+ expectJsonNullableArray<T>(
22
+ typeOrMapper: TypeOrMapper<T>,
23
+ ): HttpBuilderOfT<(T | null)[]>;
24
+ expectJsonPaginationResult<T>(
25
+ typeOrMapper: TypeOrMapper<T>,
26
+ ): HttpBuilderOfT<PaginationResult<T>>;
27
+ expectJsonInfinitePaginationResult<T>(
28
+ typeOrMapper: TypeOrMapper<T>,
29
+ ): HttpBuilderOfT<InfinitePaginationResult<T>>;
30
+ }
31
+ interface HttpBuilderOfT<T> {
32
+ withJson(content: any): this;
33
+ }
34
+ }
35
+
36
+ HttpBuilder.prototype.withJson = function (this: HttpBuilder, content: any) {
37
+ this.message.content = serialize(content);
38
+ this.message.contentType = "application/json";
39
+ return this;
40
+ };
41
+
42
+ HttpBuilderOfT.prototype.withJson = function <T>(
43
+ this: HttpBuilderOfT<T>,
44
+ content: any,
45
+ ) {
46
+ this.message.content = serialize(content);
47
+ this.message.contentType = "application/json";
48
+ return this;
49
+ };
50
+
51
+ HttpBuilder.prototype.expectJson = function <T>(
52
+ this: HttpBuilder,
53
+ typeOrMapper?: TypeOrMapper<T>,
54
+ ) {
55
+ this.message.headers.set("Accept", "application/json");
56
+ return this.useHandler((response) => {
57
+ return response.rawResponse
58
+ .json()
59
+ .then((x) => getMapper(deserialize, typeOrMapper)(x));
60
+ });
61
+ };
62
+
63
+ HttpBuilder.prototype.expectJsonArray = function <T>(
64
+ this: HttpBuilder,
65
+ typeOrMapper: TypeOrMapper<T>,
66
+ ) {
67
+ this.message.headers.set("Accept", "application/json");
68
+ return this.useHandler((response) => {
69
+ return response.rawResponse.json().then((x: any[]) => {
70
+ const itemFactory = getMapper(deserialize, typeOrMapper);
71
+ return x.map(itemFactory);
72
+ });
73
+ });
74
+ };
75
+
76
+ HttpBuilder.prototype.expectJsonNullableArray = function <T>(
77
+ this: HttpBuilder,
78
+ typeOrMapper: TypeOrMapper<T>,
79
+ ): HttpBuilderOfT<(T | null)[]> {
80
+ this.message.headers.set("Accept", "application/json");
81
+ return this.useHandler((response) => {
82
+ return response.rawResponse.json().then((x: any[]) => {
83
+ const itemFactory = getNullableMapper(deserialize, typeOrMapper);
84
+ return x.map(itemFactory);
85
+ });
86
+ });
87
+ };
88
+
89
+ HttpBuilder.prototype.expectJsonPaginationResult = function <T>(
90
+ this: HttpBuilder,
91
+ typeOrMapper: TypeOrMapper<T>,
92
+ ) {
93
+ this.message.headers.set("Accept", "application/json");
94
+ return this.useHandler((response) => {
95
+ return response.rawResponse.json().then((x: PaginationResult<any>) => {
96
+ const itemFactory = getMapper(deserialize, typeOrMapper);
97
+ return {
98
+ meta: {
99
+ pageCount: x.meta.pageCount,
100
+ pageSize: x.meta.pageSize,
101
+ totalItems: x.meta.totalItems,
102
+ },
103
+ data: x.data.map(itemFactory),
104
+ };
105
+ });
106
+ });
107
+ };
108
+
109
+ HttpBuilder.prototype.expectJsonInfinitePaginationResult = function <T>(
110
+ this: HttpBuilder,
111
+ typeOrMapper: TypeOrMapper<T>,
112
+ ) {
113
+ this.message.headers.set("Accept", "application/json");
114
+ return this.useHandler((response) => {
115
+ return response.rawResponse
116
+ .json()
117
+ .then((x: InfinitePaginationResult<any>) => {
118
+ const itemFactory = getMapper(deserialize, typeOrMapper);
119
+ return {
120
+ meta: {
121
+ pageSize: x.meta.pageSize,
122
+ continuationToken: x.meta.continuationToken,
123
+ },
124
+ data: x.data.map(itemFactory),
125
+ };
126
+ });
127
+ });
128
+ };
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "extends": "../../tsconfig.json",
3
3
  "compilerOptions": {
4
- "rootDir": "src",
5
- "outDir": "../../dist/esm/json"
4
+ "rootDir": "src"
6
5
  },
7
6
  "include": ["./src/*.ts"]
8
7
  }
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "@utiliread/http",
3
+ "types": "../../../../../dist/index.d.ts"
4
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "source": "src/index.ts",
3
+ "main": "./../../dist/jsonpatch.js",
4
+ "module": "./../../dist/jsonpatch.mjs",
5
+ "types": "./../../dist/jsonpatch.d.ts",
6
+ "alias": {
7
+ "@utiliread/http": false
8
+ },
9
+ "dependencies": {
10
+ "@utiliread/http": "file:../../"
11
+ },
12
+ "peerDependencies": {
13
+ "@utiliread/json": "file:../../node_modules/@utiliread/json",
14
+ "@utiliread/jsonpatch": "file:../../node_modules/@utiliread/jsonpatch"
15
+ }
16
+ }
@@ -1,31 +1,31 @@
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
- };
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
+ };
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "extends": "../../tsconfig.json",
3
3
  "compilerOptions": {
4
- "rootDir": "src",
5
- "outDir": "../../dist/esm/jsonpatch"
4
+ "rootDir": "src"
6
5
  },
7
6
  "include": ["./src/*.ts"]
8
7
  }
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "@utiliread/http",
3
+ "types": "../../../../../dist/index.d.ts"
4
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "source": "src/index.ts",
3
+ "main": "./../../dist/msgpack.js",
4
+ "module": "./../../dist/msgpack.mjs",
5
+ "types": "./../../dist/msgpack.d.ts",
6
+ "alias": {
7
+ "@utiliread/http": false
8
+ },
9
+ "dependencies": {
10
+ "@utiliread/http": "file:../../"
11
+ },
12
+ "peerDependencies": {
13
+ "@msgpack/msgpack": "file:../../node_modules/@msgpack/msgpack",
14
+ "@utiliread/msgpack": "file:../../node_modules/@utiliread/msgpack"
15
+ }
16
+ }
@@ -1,60 +1,60 @@
1
- import { HttpBuilder, HttpBuilderOfT, HttpResponse, Mapping } from "@utiliread/http";
2
- import { decodeArrayStream, decodeAsync } from "@msgpack/msgpack";
3
-
4
- import { deserialize } from "@utiliread/msgpack";
5
-
6
- // https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
7
- declare module "@utiliread/http" {
8
- interface HttpBuilder {
9
- expectMessagePack<T>(typeOrMapper?: Mapping.TypeOrMapper<T>): HttpBuilderOfT<T>;
10
- expectMessagePackArray<T>(
11
- typeOrMapper?: Mapping.TypeOrMapper<T>
12
- ): HttpBuilderOfT<T[]>;
13
- streamMessagePackArray<T>(
14
- typeOrMapper?: Mapping.TypeOrMapper<T>
15
- ): HttpBuilderOfT<AsyncGenerator<T, void, unknown>>;
16
- }
17
- }
18
-
19
- HttpBuilder.prototype.expectMessagePack = function <T>(
20
- this: HttpBuilder,
21
- typeOrMapper?: Mapping.TypeOrMapper<T>
22
- ) {
23
- this.message.headers.set("Accept", "application/x-msgpack");
24
- return this.useHandler(async (response) => {
25
- const itemFactory = Mapping.getMapper(deserialize, typeOrMapper);
26
- const decoded = await decodeAsync(response.rawResponse.body!);
27
- return itemFactory(decoded);
28
- });
29
- };
30
-
31
- HttpBuilder.prototype.expectMessagePackArray = function <T>(
32
- this: HttpBuilder,
33
- typeOrMapper?: Mapping.TypeOrMapper<T>
34
- ) {
35
- this.message.headers.set("Accept", "application/x-msgpack");
36
- return this.useHandler(async (response) => {
37
- const items: T[] = [];
38
- const itemFactory = Mapping.getMapper(deserialize, typeOrMapper);
39
- for await (const item of decodeArrayStream(response.rawResponse.body!)) {
40
- items.push(itemFactory(item));
41
- }
42
- return items;
43
- });
44
- };
45
-
46
- HttpBuilder.prototype.streamMessagePackArray = function <T>(
47
- this: HttpBuilder,
48
- typeOrMapper?: Mapping.TypeOrMapper<T>
49
- ) {
50
- this.message.headers.set("Accept", "application/x-msgpack");
51
-
52
- async function* handler(response: HttpResponse) {
53
- const itemFactory = Mapping.getMapper(deserialize, typeOrMapper);
54
- for await (const item of decodeArrayStream(response.rawResponse.body!)) {
55
- yield itemFactory(item);
56
- }
57
- }
58
-
59
- return this.useHandler((response) => Promise.resolve(handler(response)));
60
- };
1
+ import { HttpBuilder, HttpBuilderOfT, HttpResponse, TypeOrMapper, getMapper } from "@utiliread/http";
2
+ import { decodeArrayStream, decodeAsync } from "@msgpack/msgpack";
3
+
4
+ import { deserialize } from "@utiliread/msgpack";
5
+
6
+ // https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
7
+ declare module "@utiliread/http" {
8
+ interface HttpBuilder {
9
+ expectMessagePack<T>(typeOrMapper?: TypeOrMapper<T>): HttpBuilderOfT<T>;
10
+ expectMessagePackArray<T>(
11
+ typeOrMapper?: TypeOrMapper<T>,
12
+ ): HttpBuilderOfT<T[]>;
13
+ streamMessagePackArray<T>(
14
+ typeOrMapper?: TypeOrMapper<T>,
15
+ ): HttpBuilderOfT<AsyncGenerator<T, void, unknown>>;
16
+ }
17
+ }
18
+
19
+ HttpBuilder.prototype.expectMessagePack = function <T>(
20
+ this: HttpBuilder,
21
+ typeOrMapper?: TypeOrMapper<T>,
22
+ ) {
23
+ this.message.headers.set("Accept", "application/x-msgpack");
24
+ return this.useHandler(async (response) => {
25
+ const itemFactory = getMapper(deserialize, typeOrMapper);
26
+ const decoded = await decodeAsync(response.rawResponse.body!);
27
+ return itemFactory(decoded);
28
+ });
29
+ };
30
+
31
+ HttpBuilder.prototype.expectMessagePackArray = function <T>(
32
+ this: HttpBuilder,
33
+ typeOrMapper?: TypeOrMapper<T>,
34
+ ) {
35
+ this.message.headers.set("Accept", "application/x-msgpack");
36
+ return this.useHandler(async (response) => {
37
+ const items: T[] = [];
38
+ const itemFactory = getMapper(deserialize, typeOrMapper);
39
+ for await (const item of decodeArrayStream(response.rawResponse.body!)) {
40
+ items.push(itemFactory(item));
41
+ }
42
+ return items;
43
+ });
44
+ };
45
+
46
+ HttpBuilder.prototype.streamMessagePackArray = function <T>(
47
+ this: HttpBuilder,
48
+ typeOrMapper?: TypeOrMapper<T>,
49
+ ) {
50
+ this.message.headers.set("Accept", "application/x-msgpack");
51
+
52
+ async function* handler(response: HttpResponse) {
53
+ const itemFactory = getMapper(deserialize, typeOrMapper);
54
+ for await (const item of decodeArrayStream(response.rawResponse.body!)) {
55
+ yield itemFactory(item);
56
+ }
57
+ }
58
+
59
+ return this.useHandler((response) => Promise.resolve(handler(response)));
60
+ };
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "extends": "../../tsconfig.json",
3
3
  "compilerOptions": {
4
- "rootDir": "src",
5
- "outDir": "../../dist/esm/msgpack"
4
+ "rootDir": "src"
6
5
  },
7
6
  "include": ["./src/*.ts"]
8
7
  }
@@ -1,32 +1,32 @@
1
1
  type Callback<P extends any[] = any[]> = (...params: P) => void | Promise<void>;
2
2
 
3
3
  export class EventAggregator<P extends any[]> {
4
- private callbacks: Function[] = [];
4
+ private callbacks: Function[] = [];
5
5
 
6
- get any() {
7
- return this.callbacks.length > 0;
8
- }
6
+ get any() {
7
+ return this.callbacks.length > 0;
8
+ }
9
9
 
10
- subscribe(callback: Callback<P>): Subscription {
11
- this.callbacks.push(callback);
12
- return {
13
- dispose: () => {
14
- const index = this.callbacks.indexOf(callback);
15
- if (index >= 0) {
16
- this.callbacks.splice(index, 1);
17
- }
18
- }
10
+ subscribe(callback: Callback<P>): Subscription {
11
+ this.callbacks.push(callback);
12
+ return {
13
+ dispose: () => {
14
+ const index = this.callbacks.indexOf(callback);
15
+ if (index >= 0) {
16
+ this.callbacks.splice(index, 1);
19
17
  }
20
- }
18
+ },
19
+ };
20
+ }
21
21
 
22
- async publish(...params: P) {
23
- const callbacks = this.callbacks.slice();
24
- for (const callback of callbacks) {
25
- await Promise.resolve(callback.apply(this, params));
26
- }
22
+ async publish(...params: P) {
23
+ const callbacks = this.callbacks.slice();
24
+ for (const callback of callbacks) {
25
+ await Promise.resolve(callback.apply(this, params));
27
26
  }
27
+ }
28
28
  }
29
29
 
30
30
  export interface Subscription {
31
- dispose(): void;
32
- }
31
+ dispose(): void;
32
+ }
package/src/events.ts CHANGED
@@ -1,39 +1,39 @@
1
- import { HttpBuilder, HttpBuilderOfT, Message } from "./http-builder";
2
- import { HttpResponse } from "./http-response";
3
-
4
- interface Events {
5
- sent: (response: HttpResponse, request: Message) => void | Promise<void>;
6
- }
7
-
8
- interface EventsOfT<T> extends Events {
9
- received: (
10
- response: HttpResponse,
11
- request: Message,
12
- value: T
13
- ) => void | Promise<void>;
14
- }
15
-
16
- export function events<P extends any[]>(
17
- action: (...params: P) => HttpBuilder,
18
- configure: (...params: P) => Partial<Events>
19
- ): (...params: P) => HttpBuilder;
20
- export function events<B extends HttpBuilderOfT<T>, P extends any[], T>(
21
- action: (...params: P) => B,
22
- configure: (...params: P) => Partial<EventsOfT<T>>
23
- ): (...params: P) => B;
24
- export function events<B extends HttpBuilderOfT<T>, P extends any[], T>(
25
- action: (...params: P) => B,
26
- configure: (...params: P) => Partial<EventsOfT<T>>
27
- ): (...params: P) => B {
28
- return function (...params: P) {
29
- const builder = action(...params);
30
- const events = configure(...params);
31
- if (events.sent) {
32
- builder.onSent(events.sent);
33
- }
34
- if (builder instanceof HttpBuilderOfT && events.received) {
35
- builder.onReceived(events.received);
36
- }
37
- return builder;
38
- };
39
- }
1
+ import { HttpBuilder, HttpBuilderOfT, Message } from "./http-builder";
2
+ import { HttpResponse } from "./http-response";
3
+
4
+ interface Events {
5
+ sent: (response: HttpResponse, request: Message) => void | Promise<void>;
6
+ }
7
+
8
+ interface EventsOfT<T> extends Events {
9
+ received: (
10
+ response: HttpResponse,
11
+ request: Message,
12
+ value: T,
13
+ ) => void | Promise<void>;
14
+ }
15
+
16
+ export function events<P extends any[]>(
17
+ action: (...params: P) => HttpBuilder,
18
+ configure: (...params: P) => Partial<Events>,
19
+ ): (...params: P) => HttpBuilder;
20
+ export function events<B extends HttpBuilderOfT<T>, P extends any[], T>(
21
+ action: (...params: P) => B,
22
+ configure: (...params: P) => Partial<EventsOfT<T>>,
23
+ ): (...params: P) => B;
24
+ export function events<B extends HttpBuilderOfT<T>, P extends any[], T>(
25
+ action: (...params: P) => B,
26
+ configure: (...params: P) => Partial<EventsOfT<T>>,
27
+ ): (...params: P) => B {
28
+ return function (...params: P) {
29
+ const builder = action(...params);
30
+ const events = configure(...params);
31
+ if (events.sent) {
32
+ builder.onSent(events.sent);
33
+ }
34
+ if (builder instanceof HttpBuilderOfT && events.received) {
35
+ builder.onReceived(events.received);
36
+ }
37
+ return builder;
38
+ };
39
+ }