@everyonesoftware/common 3.0.0 → 4.0.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 (176) hide show
  1. package/outputs/chunk-AY3YWKOM.js +7741 -0
  2. package/outputs/chunk-AY3YWKOM.js.map +1 -0
  3. package/outputs/sources.cjs +9007 -0
  4. package/outputs/sources.cjs.map +1 -0
  5. package/outputs/sources.d.cts +4082 -0
  6. package/outputs/sources.d.ts +4082 -0
  7. package/outputs/sources.js +1370 -0
  8. package/outputs/sources.js.map +1 -0
  9. package/outputs/tests.cjs +17208 -0
  10. package/outputs/tests.cjs.map +1 -0
  11. package/outputs/tests.d.cts +3 -0
  12. package/outputs/tests.d.ts +3 -0
  13. package/outputs/tests.js +9798 -0
  14. package/outputs/tests.js.map +1 -0
  15. package/package.json +6 -1
  16. package/.c8rc.json +0 -12
  17. package/.github/workflows/publish.yml +0 -54
  18. package/.mocharc.json +0 -9
  19. package/sources/assertMessageParameters.ts +0 -22
  20. package/sources/asyncIterator.ts +0 -437
  21. package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +0 -48
  22. package/sources/asyncResult.ts +0 -95
  23. package/sources/basicDisposable.ts +0 -57
  24. package/sources/byteList.ts +0 -202
  25. package/sources/byteListStream.ts +0 -121
  26. package/sources/byteReadStream.ts +0 -24
  27. package/sources/byteWriteStream.ts +0 -16
  28. package/sources/bytes.ts +0 -25
  29. package/sources/characterList.ts +0 -195
  30. package/sources/characterListStream.ts +0 -151
  31. package/sources/characterReadStream.ts +0 -81
  32. package/sources/characterReadStreamIterator.ts +0 -128
  33. package/sources/characterWriteStream.ts +0 -45
  34. package/sources/commandLineParameter.ts +0 -45
  35. package/sources/commandLineParameters.ts +0 -21
  36. package/sources/comparable.ts +0 -144
  37. package/sources/comparer.ts +0 -133
  38. package/sources/comparison.ts +0 -20
  39. package/sources/concatenateIterable.ts +0 -119
  40. package/sources/concatenateIterator.ts +0 -165
  41. package/sources/condition.ts +0 -329
  42. package/sources/currentProcess.ts +0 -158
  43. package/sources/dateTime.ts +0 -130
  44. package/sources/depthFirstSearch.ts +0 -230
  45. package/sources/disposable.ts +0 -31
  46. package/sources/emptyError.ts +0 -10
  47. package/sources/english.ts +0 -45
  48. package/sources/equalFunctions.ts +0 -123
  49. package/sources/fetchHttpClient.ts +0 -89
  50. package/sources/fetchHttpResponse.ts +0 -106
  51. package/sources/flatMapIterable.ts +0 -104
  52. package/sources/flatMapIterator.ts +0 -152
  53. package/sources/generator.ts +0 -251
  54. package/sources/httpClient.ts +0 -36
  55. package/sources/httpHeader.ts +0 -37
  56. package/sources/httpHeaders.ts +0 -216
  57. package/sources/httpIncomingRequest.ts +0 -30
  58. package/sources/httpIncomingResponse.ts +0 -19
  59. package/sources/httpMethod.ts +0 -164
  60. package/sources/httpOutgoingRequest.ts +0 -119
  61. package/sources/httpOutgoingResponse.ts +0 -113
  62. package/sources/httpServer.ts +0 -34
  63. package/sources/inMemoryCharacterWriteStream.ts +0 -78
  64. package/sources/index.ts +0 -101
  65. package/sources/iterable.ts +0 -345
  66. package/sources/iterator.ts +0 -481
  67. package/sources/iteratorToJavascriptIteratorAdapter.ts +0 -48
  68. package/sources/javascript.ts +0 -59
  69. package/sources/javascriptArrayList.ts +0 -175
  70. package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +0 -124
  71. package/sources/javascriptIteratorToIteratorAdapter.ts +0 -133
  72. package/sources/javascriptMapMap.ts +0 -143
  73. package/sources/javascriptSetSet.ts +0 -134
  74. package/sources/list.ts +0 -330
  75. package/sources/listQueue.ts +0 -62
  76. package/sources/listStack.ts +0 -62
  77. package/sources/luxonDateTime.ts +0 -109
  78. package/sources/map.ts +0 -302
  79. package/sources/mapAsyncIterator.ts +0 -141
  80. package/sources/mapIterable.ts +0 -105
  81. package/sources/mapIterator.ts +0 -145
  82. package/sources/mutableCondition.ts +0 -451
  83. package/sources/mutableHttpHeaders.ts +0 -204
  84. package/sources/mutableMap.ts +0 -292
  85. package/sources/network.ts +0 -18
  86. package/sources/node.ts +0 -37
  87. package/sources/nodeJSCharacterWriteStream.ts +0 -42
  88. package/sources/nodeJSHttpIncomingRequest.ts +0 -132
  89. package/sources/nodeJSHttpServer.ts +0 -134
  90. package/sources/notFoundError.ts +0 -12
  91. package/sources/postCondition.ts +0 -284
  92. package/sources/postConditionError.ts +0 -12
  93. package/sources/preCondition.ts +0 -284
  94. package/sources/preConditionError.ts +0 -12
  95. package/sources/promiseAsyncResult.ts +0 -174
  96. package/sources/property.ts +0 -63
  97. package/sources/queue.ts +0 -49
  98. package/sources/realNetwork.ts +0 -28
  99. package/sources/recreationDotGovClient.ts +0 -259
  100. package/sources/searchControl.ts +0 -42
  101. package/sources/set.ts +0 -244
  102. package/sources/skipAsyncIterator.ts +0 -145
  103. package/sources/skipIterator.ts +0 -155
  104. package/sources/stack.ts +0 -48
  105. package/sources/stringComparer.ts +0 -33
  106. package/sources/stringIterator.ts +0 -149
  107. package/sources/strings.ts +0 -322
  108. package/sources/syncResult.ts +0 -300
  109. package/sources/takeAsyncIterator.ts +0 -141
  110. package/sources/takeIterator.ts +0 -151
  111. package/sources/toStringFunctions.ts +0 -185
  112. package/sources/types.ts +0 -371
  113. package/sources/whereAsyncIterator.ts +0 -143
  114. package/sources/whereIterable.ts +0 -108
  115. package/sources/whereIterator.ts +0 -157
  116. package/sources/wonderlandTrailClient.ts +0 -1503
  117. package/tests/assertTest.ts +0 -113
  118. package/tests/assertTestTests.ts +0 -75
  119. package/tests/basicTestSkip.ts +0 -51
  120. package/tests/byteListStreamTests.ts +0 -390
  121. package/tests/byteListTests.ts +0 -27
  122. package/tests/bytesTests.ts +0 -43
  123. package/tests/characterListStreamTests.ts +0 -391
  124. package/tests/characterListTests.ts +0 -250
  125. package/tests/characterWriteStreamTests.ts +0 -12
  126. package/tests/comparerTests.ts +0 -92
  127. package/tests/conditionTests.ts +0 -877
  128. package/tests/consoleTestRunner.ts +0 -404
  129. package/tests/consoleTestRunnerTests.ts +0 -651
  130. package/tests/dateTimeTests.ts +0 -30
  131. package/tests/depthFirstSearchTests.ts +0 -106
  132. package/tests/disposableTests.ts +0 -121
  133. package/tests/englishTests.ts +0 -103
  134. package/tests/equalFunctionsTests.ts +0 -223
  135. package/tests/failedTest.ts +0 -43
  136. package/tests/fetchHttpClientTests.ts +0 -33
  137. package/tests/generatorTests.ts +0 -86
  138. package/tests/httpClientTests.ts +0 -18
  139. package/tests/inMemoryCharacterWriteStreamTests.ts +0 -117
  140. package/tests/iterableTests.ts +0 -141
  141. package/tests/iteratorTests.ts +0 -1086
  142. package/tests/javascriptMapMapTests.ts +0 -21
  143. package/tests/listTests.ts +0 -338
  144. package/tests/mapIteratorTests.ts +0 -55
  145. package/tests/mapTests.ts +0 -104
  146. package/tests/mutableConditionTests.ts +0 -273
  147. package/tests/mutableMapTests.ts +0 -154
  148. package/tests/nodeJSHttpServerTests.ts +0 -75
  149. package/tests/notFoundErrorTests.ts +0 -24
  150. package/tests/postConditionErrorTests.ts +0 -24
  151. package/tests/preConditionErrorTests.ts +0 -24
  152. package/tests/promiseAsyncResultTests.ts +0 -688
  153. package/tests/propertyTests.ts +0 -63
  154. package/tests/queueTests.ts +0 -29
  155. package/tests/recreationDotGovClientTests.ts +0 -191
  156. package/tests/setTests.ts +0 -140
  157. package/tests/skippedTest.ts +0 -39
  158. package/tests/stackTests.ts +0 -66
  159. package/tests/stringComparerTests.ts +0 -60
  160. package/tests/stringIteratorTests.ts +0 -156
  161. package/tests/stringsTests.ts +0 -516
  162. package/tests/syncResultTests.ts +0 -1251
  163. package/tests/test.ts +0 -228
  164. package/tests/testAction.ts +0 -75
  165. package/tests/testActionTests.ts +0 -93
  166. package/tests/testFailureTests.ts +0 -12
  167. package/tests/testRunner.ts +0 -267
  168. package/tests/testRunnerTests.ts +0 -895
  169. package/tests/testSkip.ts +0 -34
  170. package/tests/tests.ts +0 -103
  171. package/tests/toStringFunctionsTests.ts +0 -55
  172. package/tests/typesTests.ts +0 -257
  173. package/tests/whereIteratorTests.ts +0 -77
  174. package/tests/wonderlandTrailClientTests.ts +0 -452
  175. package/tsconfig.json +0 -20
  176. package/tsup.config.ts +0 -13
@@ -1,230 +0,0 @@
1
- import { JavascriptIterable, JavascriptIterator } from "./javascript";
2
- import { List } from "./list";
3
- import { ListStack } from "./listStack";
4
- import { PreCondition } from "./preCondition";
5
- import { SearchControl } from "./searchControl";
6
- import { Stack } from "./stack";
7
- import { Set } from "./set";
8
- import { Iterator } from "./iterator";
9
- import { SyncResult } from "./syncResult";
10
- import { isJavascriptIterable, Type } from "./types";
11
-
12
- class SearchBreakError extends Error
13
- {
14
- }
15
-
16
- class DepthFirstSearch<TVisit,TResult> implements SearchControl<TVisit,TResult>, Iterator<TResult>
17
- {
18
- private readonly searchAction: (searchControl: SearchControl<TVisit,TResult>, visiting: TVisit) => void;
19
- private readonly toVisit: ListStack<TVisit>;
20
- private readonly visited: Set<TVisit>;
21
- private readonly results: List<TResult>;
22
- private started: boolean;
23
- private done: boolean;
24
-
25
- private constructor(initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, visiting: TVisit) => void)
26
- {
27
- PreCondition.assertNotEmpty(initialToVisit, "initialToVisit");
28
- PreCondition.assertNotUndefinedAndNotNull(searchAction, "searchAction");
29
-
30
- this.searchAction = searchAction;
31
- this.toVisit = Stack.create();
32
- this.toVisit.addAll(initialToVisit).await();
33
- this.visited = Set.create();
34
- this.results = List.create();
35
- this.started = false;
36
- this.done = false;
37
- }
38
-
39
- public static create<TVisit,TResult>(initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, visiting: TVisit) => void): DepthFirstSearch<TVisit,TResult>
40
- {
41
- return new DepthFirstSearch(initialToVisit, searchAction);
42
- }
43
-
44
- public addToVisit(toVisit: TVisit): void
45
- {
46
- if (!this.hasVisited(toVisit))
47
- {
48
- this.toVisit.add(toVisit);
49
- }
50
- }
51
-
52
- public addAllToVisit(values: JavascriptIterable<TVisit>): void
53
- {
54
- for (const value of values)
55
- {
56
- this.addToVisit(value);
57
- }
58
- }
59
-
60
- public hasVisited(toVisit: TVisit): boolean
61
- {
62
- return this.visited.contains(toVisit).await();
63
- }
64
-
65
- public addResult(result: TResult): void
66
- {
67
- this.results.add(result);
68
- }
69
-
70
- public addResults(results: JavascriptIterable<TResult>): void
71
- {
72
- this.results.addAll(results);
73
- }
74
-
75
- public break(): never
76
- {
77
- throw new SearchBreakError();
78
- }
79
-
80
- public next(): SyncResult<boolean>
81
- {
82
- return SyncResult.create(() =>
83
- {
84
- let result: boolean = false;
85
- if (!this.done)
86
- {
87
- if (!this.started)
88
- {
89
- this.started = true;
90
- }
91
- else
92
- {
93
- this.results.removeFirst().await();
94
- }
95
-
96
- while (!this.hasCurrent() && this.toVisit.any().await())
97
- {
98
- const current: TVisit = this.toVisit.remove().await();
99
- this.visited.add(current);
100
- this.searchAction(this, current);
101
- }
102
-
103
- result = this.hasCurrent();
104
- this.done = !result;
105
- }
106
- return result;
107
- });
108
- }
109
-
110
- public hasStarted(): boolean
111
- {
112
- return this.started;
113
- }
114
-
115
- public hasCurrent(): boolean
116
- {
117
- return this.results.any().await();
118
- }
119
-
120
- public getCurrent(): TResult
121
- {
122
- PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
123
-
124
- return this.results.first().await();
125
- }
126
-
127
- public start(): SyncResult<this>
128
- {
129
- return Iterator.start<TResult,this>(this);
130
- }
131
-
132
- public takeCurrent(): SyncResult<TResult>
133
- {
134
- return Iterator.takeCurrent(this);
135
- }
136
-
137
- public any(): SyncResult<boolean>
138
- {
139
- return Iterator.any(this);
140
- }
141
-
142
- public getCount(): SyncResult<number>
143
- {
144
- return Iterator.getCount(this);
145
- }
146
-
147
- public toArray(): SyncResult<TResult[]>
148
- {
149
- return Iterator.toArray(this);
150
- }
151
-
152
- public concatenate(...toConcatenate: JavascriptIterable<TResult>[]): Iterator<TResult>
153
- {
154
- return Iterator.concatenate(this, ...toConcatenate);
155
- }
156
-
157
- public where(condition: (value: TResult) => (boolean | SyncResult<boolean>)): Iterator<TResult>
158
- {
159
- return Iterator.where(this, condition);
160
- }
161
-
162
- public map<TOutput>(mapping: (value: TResult) => TOutput | SyncResult<TOutput>): Iterator<TOutput>
163
- {
164
- return Iterator.map(this, mapping);
165
- }
166
-
167
- public flatMap<TOutput>(mapping: (value: TResult) => JavascriptIterable<TOutput>): Iterator<TOutput>
168
- {
169
- return Iterator.flatMap(this, mapping);
170
- }
171
-
172
- public whereInstanceOf<U extends TResult>(typeCheck: (value: TResult) => value is U): Iterator<U>
173
- {
174
- return Iterator.whereInstanceOf(this, typeCheck);
175
- }
176
-
177
- public whereInstanceOfType<U extends TResult>(type: Type<U>): Iterator<U>
178
- {
179
- return Iterator.whereInstanceOfType(this, type);
180
- }
181
-
182
- public first(condition?: ((value: TResult) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<TResult>
183
- {
184
- return Iterator.first(this, condition);
185
- }
186
-
187
- public last(condition?: ((value: TResult) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<TResult>
188
- {
189
- return Iterator.last(this, condition);
190
- }
191
-
192
- public take(maximumToTake: number): Iterator<TResult>
193
- {
194
- return Iterator.take(this, maximumToTake);
195
- }
196
-
197
- public skip(maximumToSkip: number): Iterator<TResult>
198
- {
199
- return Iterator.skip(this, maximumToSkip);
200
- }
201
-
202
- public [Symbol.iterator](): JavascriptIterator<TResult>
203
- {
204
- return Iterator[Symbol.iterator](this);
205
- }
206
- }
207
-
208
- export function depthFirstSearch<TVisit,TResult>(initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, current: TVisit) => void): Iterator<TResult>;
209
- export function depthFirstSearch<TVisit,TResult>(parameters: { initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, current: TVisit) => void }): Iterator<TResult>;
210
- export function depthFirstSearch<TVisit,TResult>(parametersOrInitialToVisit: JavascriptIterable<TVisit> | { initialToVisit: JavascriptIterable<TVisit>, searchAction: (searchControl: SearchControl<TVisit,TResult>, current: TVisit) => void }, searchAction?: (searchControl: SearchControl<TVisit,TResult>, current: TVisit) => void): Iterator<TResult>
211
- {
212
- let initialToVisit: JavascriptIterable<TVisit>;
213
- if (isJavascriptIterable(parametersOrInitialToVisit))
214
- {
215
- initialToVisit = parametersOrInitialToVisit;
216
- searchAction = searchAction!;
217
- }
218
- else
219
- {
220
- PreCondition.assertNotUndefinedAndNotNull(parametersOrInitialToVisit, "parameters");
221
-
222
- initialToVisit = parametersOrInitialToVisit.initialToVisit;
223
- searchAction = parametersOrInitialToVisit.searchAction;
224
- }
225
-
226
- PreCondition.assertNotUndefinedAndNotNull(initialToVisit, "initialToVisit");
227
- PreCondition.assertNotUndefinedAndNotNull(searchAction, "searchAction");
228
-
229
- return DepthFirstSearch.create(initialToVisit, searchAction);
230
- }
@@ -1,31 +0,0 @@
1
- import { SyncDisposable } from "./basicDisposable";
2
- import { AsyncResult } from "./asyncResult";
3
-
4
- /**
5
- * An object that can be disposed.
6
- */
7
- export abstract class Disposable
8
- {
9
- /**
10
- * Create a new {@link Disposable} that will invoke the provided {@link Function} when it is
11
- * disposed.
12
- * @param disposedFunction The function to invoke when the returned {@link Disposable} is
13
- * disposed.
14
- */
15
- public static create(disposedFunction: () => void): SyncDisposable
16
- {
17
- return SyncDisposable.create(disposedFunction);
18
- }
19
-
20
- /**
21
- * Synchronously clean up any resources that this object is using. This function will return
22
- * true if this invocation disposed of the object. Subsequent calls to dispose() will return
23
- * false.
24
- */
25
- public abstract dispose(): AsyncResult<boolean>;
26
-
27
- /**
28
- * Get whether this {@link Disposable} has been disposed yet.
29
- */
30
- public abstract isDisposed(): boolean;
31
- }
@@ -1,10 +0,0 @@
1
- /**
2
- * An {@link Error} that is created when something is empty.
3
- */
4
- export class EmptyError extends Error
5
- {
6
- public constructor(message?: string)
7
- {
8
- super(message);
9
- }
10
- }
@@ -1,45 +0,0 @@
1
- import { Iterator } from "./iterator";
2
- import { JavascriptIterable } from "./javascript";
3
- import { PreCondition } from "./preCondition";
4
-
5
- export function andList(values: JavascriptIterable<string>): string
6
- {
7
- return list("and", values);
8
- }
9
-
10
- export function orList(values: JavascriptIterable<string>): string
11
- {
12
- return list("or", values);
13
- }
14
-
15
- function list(conjunction: string, values: JavascriptIterable<string>): string
16
- {
17
- PreCondition.assertNotEmpty(conjunction, "conjunction");
18
- PreCondition.assertNotUndefinedAndNotNull(values, "values");
19
-
20
- let result: string = "";
21
- let index = 0;
22
- const iterator: Iterator<string> = Iterator.create(values).start().await();
23
- while (iterator.hasCurrent())
24
- {
25
- const currentValue: string = iterator.takeCurrent().await();
26
- if (index >= 1)
27
- {
28
- if (iterator.hasCurrent())
29
- {
30
- result += `, `;
31
- }
32
- else
33
- {
34
- if (index >= 2)
35
- {
36
- result += `,`;
37
- }
38
- result += ` ${conjunction} `;
39
- }
40
- }
41
- result += currentValue;
42
- index++;
43
- }
44
- return result;
45
- }
@@ -1,123 +0,0 @@
1
- import { Comparer } from "./comparer";
2
- import { Iterable } from "./iterable";
3
- import { isMap, Map } from "./map";
4
- import { PreCondition } from "./preCondition";
5
- import { SyncResult } from "./syncResult";
6
- import { getPropertyNames, hasProperty, isBoolean, isJavascriptIterable, isObject, isString } from "./types";
7
-
8
- /**
9
- * A collection of {@link Function}s that can be used to determine if two values are equal.
10
- */
11
- export class EqualFunctions
12
- {
13
- private readonly equalFunctions: ((left: unknown, right: unknown) => (boolean | SyncResult<boolean> | undefined))[];
14
-
15
- private constructor()
16
- {
17
- this.equalFunctions = [];
18
- }
19
-
20
- public static create(): EqualFunctions
21
- {
22
- return new EqualFunctions();
23
- }
24
-
25
- private defaultEqualFunction(left: unknown, right: unknown): SyncResult<boolean>
26
- {
27
- return SyncResult.create(() =>
28
- {
29
- let result: boolean | undefined = Comparer.equalSameUndefinedNull(left, right);
30
- if (result === undefined)
31
- {
32
- result = false;
33
-
34
- if (isMap(left))
35
- {
36
- if (isMap(right))
37
- {
38
- result = Map.equals(left, right, this).await();
39
- }
40
- }
41
- else if (isJavascriptIterable(left) && !isString(left))
42
- {
43
- if (isJavascriptIterable(right) && !isString(right) && !isMap(right))
44
- {
45
- result = Iterable.equals(left, right, this).await();
46
- }
47
- }
48
- else if (isObject(left))
49
- {
50
- if (isObject(right))
51
- {
52
- result = true;
53
-
54
- for (const leftPropertyName of getPropertyNames(left))
55
- {
56
- if (!hasProperty(left, leftPropertyName) || !hasProperty(right, leftPropertyName))
57
- {
58
- result = false;
59
- break;
60
- }
61
- else
62
- {
63
- result = this.areEqual(left[leftPropertyName], right[leftPropertyName]).await();
64
- }
65
- }
66
-
67
- if (result)
68
- {
69
- for (const rightPropertyName of getPropertyNames(right))
70
- {
71
- if (!hasProperty(left, rightPropertyName) || !hasProperty(right, rightPropertyName))
72
- {
73
- result = false;
74
- break;
75
- }
76
- else
77
- {
78
- result = this.areEqual(left[rightPropertyName], right[rightPropertyName]).await();
79
- }
80
- }
81
- }
82
- }
83
- }
84
- }
85
- return result;
86
- });
87
- }
88
-
89
- /**
90
- * Get whether the provided values are equal based on the registered equal {@link Function}s.
91
- * @param left The left value in the comparison.
92
- * @param right The right value in the comparison.
93
- */
94
- public areEqual(left: unknown, right: unknown): SyncResult<boolean>
95
- {
96
- return SyncResult.create(() =>
97
- {
98
- let result: boolean | SyncResult<boolean> | undefined;
99
- for (const equalFunction of this.equalFunctions)
100
- {
101
- result = equalFunction(left, right);
102
- if (result !== undefined)
103
- {
104
- break;
105
- }
106
- }
107
- if (result === undefined)
108
- {
109
- result = this.defaultEqualFunction(left, right);
110
- }
111
- return isBoolean(result) ? result : result.await();
112
- });
113
- }
114
-
115
- public add(equalFunction: (left: unknown, right: unknown) => (boolean | undefined)): this
116
- {
117
- PreCondition.assertNotUndefinedAndNotNull(equalFunction, "equalFunction");
118
-
119
- this.equalFunctions.unshift(equalFunction);
120
-
121
- return this;
122
- }
123
- }
@@ -1,89 +0,0 @@
1
- import { FetchHttpIncomingResponse } from "./fetchHttpResponse";
2
- import { HttpClient } from "./httpClient";
3
- import { HttpOutgoingRequest } from "./httpOutgoingRequest";
4
- import { HttpHeader } from "./httpHeader";
5
- import { HttpMethod } from "./httpMethod";
6
- import { PostCondition } from "./postCondition";
7
- import { PreCondition } from "./preCondition";
8
- import { PromiseAsyncResult } from "./promiseAsyncResult";
9
-
10
- /**
11
- * A {@link HttpClient} that uses {@link fetch}() to make network requests.
12
- */
13
- export class FetchHttpClient implements HttpClient
14
- {
15
- protected constructor()
16
- {
17
- }
18
-
19
- public static create(): FetchHttpClient
20
- {
21
- return new FetchHttpClient();
22
- }
23
-
24
- public sendRequest(request: HttpOutgoingRequest): PromiseAsyncResult<FetchHttpIncomingResponse>
25
- {
26
- PreCondition.assertNotUndefinedAndNotNull(request, "request");
27
-
28
- return PromiseAsyncResult.create(async () =>
29
- {
30
- const requestInit: RequestInit = {
31
- method: FetchHttpClient.convertMethod(request.getMethod()),
32
- headers: request.getHeaders()
33
- .map<[string, string]>((header: HttpHeader) => [header.getName(), header.getValue()])
34
- .toArray()
35
- .await(),
36
- body: request.getBody() || undefined,
37
- };
38
-
39
- const fetchResponse: Response = await fetch(request.getURL(), requestInit);
40
- return FetchHttpIncomingResponse.create(fetchResponse);
41
- });
42
- }
43
-
44
- public sendGetRequest(url: string): PromiseAsyncResult<FetchHttpIncomingResponse>
45
- {
46
- return this.sendRequest(HttpOutgoingRequest.create(HttpMethod.GET, url));
47
- }
48
-
49
- public static convertMethod(method: HttpMethod): string
50
- {
51
- PreCondition.assertNotUndefinedAndNotNull(method, "method");
52
-
53
- let result: string;
54
- switch (method)
55
- {
56
- case HttpMethod.CONNECT:
57
- result = "CONNECT";
58
- break;
59
- case HttpMethod.DELETE:
60
- result = "DELETE";
61
- break;
62
- case HttpMethod.GET:
63
- result = "GET";
64
- break;
65
- case HttpMethod.HEAD:
66
- result = "HEAD";
67
- break;
68
- case HttpMethod.OPTIONS:
69
- result = "OPTIONS";
70
- break;
71
- case HttpMethod.PATCH:
72
- result = "PATCH";
73
- break;
74
- case HttpMethod.POST:
75
- result = "POST";
76
- break;
77
- case HttpMethod.PUT:
78
- result = "PUT";
79
- break;
80
- case HttpMethod.TRACE:
81
- result = "TRACE";
82
- break;
83
- }
84
-
85
- PostCondition.assertNotEmpty(result, "result");
86
-
87
- return result;
88
- }
89
- }
@@ -1,106 +0,0 @@
1
- import { PromiseAsyncResult } from "./promiseAsyncResult";
2
- import { HttpHeader } from "./httpHeader";
3
- import { HttpHeaders } from "./httpHeaders";
4
- import { HttpIncomingResponse } from "./httpIncomingResponse";
5
- import { MutableHttpHeaders } from "./mutableHttpHeaders";
6
- import { NotFoundError } from "./notFoundError";
7
- import { PreCondition } from "./preCondition";
8
- import { escapeAndQuote } from "./strings";
9
- import { SyncResult } from "./syncResult";
10
-
11
- /**
12
- * An {@link HttpIncomingResponse} that comes from a {@link FetchHttpClient}.
13
- */
14
- export class FetchHttpIncomingResponse extends HttpIncomingResponse
15
- {
16
- private readonly response: Response;
17
-
18
- private constructor(response: Response)
19
- {
20
- PreCondition.assertNotUndefinedAndNotNull(response, "response");
21
-
22
- super();
23
-
24
- this.response = response;
25
- }
26
-
27
- public static create(response: Response): FetchHttpIncomingResponse
28
- {
29
- return new FetchHttpIncomingResponse(response);
30
- }
31
-
32
- public getStatusCode(): number
33
- {
34
- return this.response.status;
35
- }
36
-
37
- public getHeaders(): SyncResult<HttpHeaders>
38
- {
39
- return SyncResult.create(() =>
40
- {
41
- const result: MutableHttpHeaders = HttpHeaders.create();
42
- for (const header of this.response.headers)
43
- {
44
- result.set(header[0], header[1]);
45
- }
46
- return result;
47
- });
48
- }
49
-
50
- public getHeader(headerName: string): SyncResult<HttpHeader>
51
- {
52
- PreCondition.assertNotEmpty(headerName, "headerName");
53
-
54
- return SyncResult.create(() =>
55
- {
56
- let result: HttpHeader | undefined;
57
-
58
- const lowerHeaderName: string = headerName.toLowerCase();
59
- for (const header of this.response.headers)
60
- {
61
- if (lowerHeaderName === header[0].toLowerCase())
62
- {
63
- result = HttpHeader.create(header[0], header[1]);
64
- break;
65
- }
66
- }
67
- if (result === undefined)
68
- {
69
- throw new NotFoundError(`Could not find a header with the name ${escapeAndQuote(headerName)}.`)
70
- }
71
-
72
- return result;
73
- });
74
- }
75
-
76
- public getHeaderValue(headerName: string): SyncResult<string>
77
- {
78
- PreCondition.assertNotEmpty(headerName, "headerName");
79
-
80
- return SyncResult.create(() =>
81
- {
82
- let result: string | undefined;
83
-
84
- const lowerHeaderName: string = headerName.toLowerCase();
85
- for (const header of this.response.headers)
86
- {
87
- if (lowerHeaderName === header[0].toLowerCase())
88
- {
89
- result = header[1];
90
- break;
91
- }
92
- }
93
- if (result === undefined)
94
- {
95
- throw new NotFoundError(`Could not find a header with the name ${escapeAndQuote(headerName)}.`)
96
- }
97
-
98
- return result;
99
- });
100
- }
101
-
102
- public getBody(): PromiseAsyncResult<string>
103
- {
104
- return PromiseAsyncResult.create(this.response.text());
105
- }
106
- }