@kensio/simnaril 0.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.
- package/LICENSE +201 -0
- package/README.md +37 -0
- package/dist/api.d.ts +24 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +66 -0
- package/dist/api.js.map +1 -0
- package/dist/duplicate-entity-error.d.ts +7 -0
- package/dist/duplicate-entity-error.d.ts.map +1 -0
- package/dist/duplicate-entity-error.js +14 -0
- package/dist/duplicate-entity-error.js.map +1 -0
- package/dist/entity-not-found-error.d.ts +7 -0
- package/dist/entity-not-found-error.d.ts.map +1 -0
- package/dist/entity-not-found-error.js +13 -0
- package/dist/entity-not-found-error.js.map +1 -0
- package/dist/environment.d.ts +21 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +65 -0
- package/dist/environment.js.map +1 -0
- package/dist/http/interception.d.ts +13 -0
- package/dist/http/interception.d.ts.map +1 -0
- package/dist/http/interception.js +56 -0
- package/dist/http/interception.js.map +1 -0
- package/dist/http/operation-pipeline.d.ts +7 -0
- package/dist/http/operation-pipeline.d.ts.map +1 -0
- package/dist/http/operation-pipeline.js +24 -0
- package/dist/http/operation-pipeline.js.map +1 -0
- package/dist/http/operation-router.d.ts +9 -0
- package/dist/http/operation-router.d.ts.map +1 -0
- package/dist/http/operation-router.js +63 -0
- package/dist/http/operation-router.js.map +1 -0
- package/dist/http/operation.d.ts +51 -0
- package/dist/http/operation.d.ts.map +1 -0
- package/dist/http/operation.js +43 -0
- package/dist/http/operation.js.map +1 -0
- package/dist/http/resource-operation-registry.d.ts +13 -0
- package/dist/http/resource-operation-registry.d.ts.map +1 -0
- package/dist/http/resource-operation-registry.js +46 -0
- package/dist/http/resource-operation-registry.js.map +1 -0
- package/dist/http/resource-operation.d.ts +9 -0
- package/dist/http/resource-operation.d.ts.map +1 -0
- package/dist/http/resource-operation.js +25 -0
- package/dist/http/resource-operation.js.map +1 -0
- package/dist/http/rest-resource-operations.d.ts +9 -0
- package/dist/http/rest-resource-operations.d.ts.map +1 -0
- package/dist/http/rest-resource-operations.js +97 -0
- package/dist/http/rest-resource-operations.js.map +1 -0
- package/dist/http/route.d.ts +14 -0
- package/dist/http/route.d.ts.map +1 -0
- package/dist/http/route.js +77 -0
- package/dist/http/route.js.map +1 -0
- package/dist/http/semantic-http-operation.d.ts +16 -0
- package/dist/http/semantic-http-operation.d.ts.map +1 -0
- package/dist/http/semantic-http-operation.js +20 -0
- package/dist/http/semantic-http-operation.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/resource.d.ts +29 -0
- package/dist/resource.d.ts.map +1 -0
- package/dist/resource.js +78 -0
- package/dist/resource.js.map +1 -0
- package/dist/rest-resource-operation.d.ts +29 -0
- package/dist/rest-resource-operation.d.ts.map +1 -0
- package/dist/rest-resource-operation.js +2 -0
- package/dist/rest-resource-operation.js.map +1 -0
- package/dist/rest-resource.d.ts +37 -0
- package/dist/rest-resource.d.ts.map +1 -0
- package/dist/rest-resource.js +63 -0
- package/dist/rest-resource.js.map +1 -0
- package/dist/unclaimed-origin-error.d.ts +8 -0
- package/dist/unclaimed-origin-error.d.ts.map +1 -0
- package/dist/unclaimed-origin-error.js +16 -0
- package/dist/unclaimed-origin-error.js.map +1 -0
- package/dist/unimplemented-route-error.d.ts +8 -0
- package/dist/unimplemented-route-error.d.ts.map +1 -0
- package/dist/unimplemented-route-error.js +16 -0
- package/dist/unimplemented-route-error.js.map +1 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Simnaril
|
|
2
|
+
|
|
3
|
+
Simnaril is a TypeScript library for simulating third-party HTTP services in a
|
|
4
|
+
Node.js process. Application code keeps its production URLs and HTTP clients.
|
|
5
|
+
Simnaril intercepts those requests and sends them to stateful service objects in
|
|
6
|
+
memory.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { SimApi, SimEnvironment } from "@kensio/simnaril";
|
|
10
|
+
|
|
11
|
+
interface Widget {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const api = new SimApi();
|
|
17
|
+
const widgets = api.resource<Widget>({ path: "/widgets" });
|
|
18
|
+
widgets.seed({ id: "widget-1", name: "First widget" });
|
|
19
|
+
|
|
20
|
+
using environment = new SimEnvironment();
|
|
21
|
+
environment.register("https://api.example.com", api);
|
|
22
|
+
|
|
23
|
+
const response = await fetch("https://api.example.com/widgets/widget-1");
|
|
24
|
+
const widget = (await response.json()) as Widget;
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Simnaril requires Node.js 24 or later.
|
|
28
|
+
|
|
29
|
+
## Documentation
|
|
30
|
+
|
|
31
|
+
- [Getting started](docs/getting-started/README.md)
|
|
32
|
+
- [Simulation environments](docs/simulation-environments/README.md)
|
|
33
|
+
- [Resource state](docs/resource-state/README.md)
|
|
34
|
+
- [REST resources](docs/rest-resources/README.md)
|
|
35
|
+
- [Custom operations](docs/custom-operations/README.md)
|
|
36
|
+
- [Middleware](docs/middleware/README.md)
|
|
37
|
+
- [Composing a simulation](docs/composing-a-simulation/README.md)
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type HttpMiddleware, type HttpOperationContext, type RawHttpOperation } from "./http/operation.js";
|
|
2
|
+
import { RestResource } from "./rest-resource.js";
|
|
3
|
+
import type { RestResourceProps } from "./rest-resource-operation.js";
|
|
4
|
+
import { SimResource, type SimResourceProps } from "./resource.js";
|
|
5
|
+
/** Configures conventional resource state and its HTTP operations. */
|
|
6
|
+
export interface SimApiResourceProps<T extends object> extends SimResourceProps<T>, RestResourceProps {
|
|
7
|
+
}
|
|
8
|
+
/** Handles a raw HTTP operation after Simnaril has matched its route. */
|
|
9
|
+
export type RawHttpOperationHandler = (context: HttpOperationContext) => Promise<Response> | Response;
|
|
10
|
+
/** Routes HTTP requests to stateful simulated resources. */
|
|
11
|
+
export declare class SimApi {
|
|
12
|
+
#private;
|
|
13
|
+
/** Adds middleware around every matched operation in this API. */
|
|
14
|
+
use(middleware: HttpMiddleware): this;
|
|
15
|
+
/** Creates resource state and exposes its conventional HTTP operations. */
|
|
16
|
+
resource<T extends object>(props: SimApiResourceProps<T>): RestResource<T>;
|
|
17
|
+
/** Exposes existing resource state through conventional HTTP operations. */
|
|
18
|
+
expose<T extends object>(state: SimResource<T>, props: RestResourceProps): RestResource<T>;
|
|
19
|
+
/** Adds a raw HTTP operation to this API. */
|
|
20
|
+
operation(method: string, path: string, handle: RawHttpOperationHandler): RawHttpOperation;
|
|
21
|
+
/** Handles one request through the matching simulated HTTP operation. */
|
|
22
|
+
handle(request: Request): Promise<Response>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,oBAAoB,EAEzB,KAAK,gBAAgB,EACtB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAsB,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEnE,sEAAsE;AACtE,MAAM,WAAW,mBAAmB,CAAC,CAAC,SAAS,MAAM,CACnD,SAAQ,gBAAgB,CAAC,CAAC,CAAC,EAAE,iBAAiB;CAAG;AAEnD,yEAAyE;AACzE,MAAM,MAAM,uBAAuB,GAAG,CACpC,OAAO,EAAE,oBAAoB,KAC1B,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;AAElC,4DAA4D;AAC5D,qBAAa,MAAM;;IAIjB,kEAAkE;IAClE,GAAG,CAAC,UAAU,EAAE,cAAc,GAAG,IAAI,CAGpC;IAED,2EAA2E;IAC3E,QAAQ,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAKzE;IAED,4EAA4E;IAC5E,MAAM,CAAC,CAAC,SAAS,MAAM,EACrB,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,iBAAiB,GACvB,YAAY,CAAC,CAAC,CAAC,CAejB;IAED,6CAA6C;IAC7C,SAAS,CACP,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,uBAAuB,GAC9B,gBAAgB,CAelB;IAED,yEAAyE;IACzE,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAE1C;CAkBF"}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createRawHttpOperation, operationMiddleware, } from "./http/operation.js";
|
|
2
|
+
import { OperationRouter } from "./http/operation-router.js";
|
|
3
|
+
import { compileRoute, normalizeMethod } from "./http/route.js";
|
|
4
|
+
import { attachRestResource, RestResource } from "./rest-resource.js";
|
|
5
|
+
import { SimResource } from "./resource.js";
|
|
6
|
+
/** Routes HTTP requests to stateful simulated resources. */
|
|
7
|
+
export class SimApi {
|
|
8
|
+
#paths = new Set();
|
|
9
|
+
#router = new OperationRouter();
|
|
10
|
+
/** Adds middleware around every matched operation in this API. */
|
|
11
|
+
use(middleware) {
|
|
12
|
+
this.#router.use(middleware);
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
/** Creates resource state and exposes its conventional HTTP operations. */
|
|
16
|
+
resource(props) {
|
|
17
|
+
const { operations, path, ...stateProps } = props;
|
|
18
|
+
const restProps = operations === undefined ? { path } : { operations, path };
|
|
19
|
+
return this.expose(new SimResource(stateProps), restProps);
|
|
20
|
+
}
|
|
21
|
+
/** Exposes existing resource state through conventional HTTP operations. */
|
|
22
|
+
expose(state, props) {
|
|
23
|
+
this.#validateResourcePath(props.path);
|
|
24
|
+
if (this.#paths.has(props.path)) {
|
|
25
|
+
throw new TypeError(`A resource is already exposed at collection path "${props.path}".`);
|
|
26
|
+
}
|
|
27
|
+
const resource = new RestResource(state, props);
|
|
28
|
+
attachRestResource(resource, (operation) => {
|
|
29
|
+
this.#router.register(operation);
|
|
30
|
+
});
|
|
31
|
+
this.#paths.add(props.path);
|
|
32
|
+
return resource;
|
|
33
|
+
}
|
|
34
|
+
/** Adds a raw HTTP operation to this API. */
|
|
35
|
+
operation(method, path, handle) {
|
|
36
|
+
const route = compileRoute(path);
|
|
37
|
+
const operation = createRawHttpOperation();
|
|
38
|
+
this.#router.register({
|
|
39
|
+
...route,
|
|
40
|
+
decode: () => Promise.resolve(),
|
|
41
|
+
encode: (output) => output,
|
|
42
|
+
method: normalizeMethod(method),
|
|
43
|
+
middleware: operationMiddleware(operation),
|
|
44
|
+
operate: (_input, context) => handle(context),
|
|
45
|
+
resourceMiddleware: [],
|
|
46
|
+
transform: (decoded) => decoded,
|
|
47
|
+
});
|
|
48
|
+
return operation;
|
|
49
|
+
}
|
|
50
|
+
/** Handles one request through the matching simulated HTTP operation. */
|
|
51
|
+
handle(request) {
|
|
52
|
+
return this.#router.handle(request);
|
|
53
|
+
}
|
|
54
|
+
#validateResourcePath(path) {
|
|
55
|
+
const base = new URL("https://simnaril.invalid");
|
|
56
|
+
const url = new URL(path, base);
|
|
57
|
+
if (url.origin !== base.origin ||
|
|
58
|
+
url.pathname !== path ||
|
|
59
|
+
url.search !== "" ||
|
|
60
|
+
url.hash !== "" ||
|
|
61
|
+
!/^\/[^/:]+(?:\/[^/:]+)*$/u.test(path)) {
|
|
62
|
+
throw new TypeError(`Expected a resource collection path such as "/widgets", received "${path}".`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=api.js.map
|
package/dist/api.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EAGtB,mBAAmB,GAEpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEtE,OAAO,EAAE,WAAW,EAAyB,MAAM,eAAe,CAAC;AAWnE,4DAA4D;AAC5D,MAAM,OAAO,MAAM;IACR,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;IAC3B,OAAO,GAAG,IAAI,eAAe,EAAE,CAAC;IAEzC,kEAAkE;IAClE,GAAG,CAAC,UAA0B;QAC5B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,2EAA2E;IAC3E,QAAQ,CAAmB,KAA6B;QACtD,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,UAAU,EAAE,GAAG,KAAK,CAAC;QAClD,MAAM,SAAS,GACb,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,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C,OAAO,CAAC;YAC7C,kBAAkB,EAAE,EAAE;YACtB,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO;SAChC,CAAC,CAAC;QACH,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,yEAAyE;IACzE,MAAM,CAAC,OAAgB;QACrB,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;IAED,qBAAqB,CAAC,IAAY;QAChC,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,0BAA0B,CAAC,CAAC;QACjD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAEhC,IACE,GAAG,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;YAC1B,GAAG,CAAC,QAAQ,KAAK,IAAI;YACrB,GAAG,CAAC,MAAM,KAAK,EAAE;YACjB,GAAG,CAAC,IAAI,KAAK,EAAE;YACf,CAAC,0BAA0B,CAAC,IAAI,CAAC,IAAI,CAAC,EACtC,CAAC;YACD,MAAM,IAAI,SAAS,CACjB,qEAAqE,IAAI,IAAI,CAC9E,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Reports an identity already used by another entity in the resource. */
|
|
2
|
+
export declare class DuplicateEntityError extends Error {
|
|
3
|
+
readonly identity: string;
|
|
4
|
+
readonly resourceName: string | undefined;
|
|
5
|
+
constructor(identity: string, resourceName?: string);
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=duplicate-entity-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duplicate-entity-error.d.ts","sourceRoot":"","sources":["../src/duplicate-entity-error.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAE1C,YAAY,QAAQ,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,EASlD;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Reports an identity already used by another entity in the resource. */
|
|
2
|
+
export class DuplicateEntityError extends Error {
|
|
3
|
+
identity;
|
|
4
|
+
resourceName;
|
|
5
|
+
constructor(identity, resourceName) {
|
|
6
|
+
const entityName = resourceName ?? "entity";
|
|
7
|
+
const article = resourceName === undefined ? "An" : "A";
|
|
8
|
+
super(`${article} ${entityName} already exists with identity "${identity}".`);
|
|
9
|
+
this.name = "DuplicateEntityError";
|
|
10
|
+
this.identity = identity;
|
|
11
|
+
this.resourceName = resourceName;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=duplicate-entity-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duplicate-entity-error.js","sourceRoot":"","sources":["../src/duplicate-entity-error.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IACpC,QAAQ,CAAS;IACjB,YAAY,CAAqB;IAE1C,YAAY,QAAgB,EAAE,YAAqB;QACjD,MAAM,UAAU,GAAG,YAAY,IAAI,QAAQ,CAAC;QAC5C,MAAM,OAAO,GAAG,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC;QACxD,KAAK,CACH,GAAG,OAAO,IAAI,UAAU,kCAAkC,QAAQ,IAAI,CACvE,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Reports an operation that requires an entity absent from the resource. */
|
|
2
|
+
export declare class EntityNotFoundError extends Error {
|
|
3
|
+
readonly identity: string;
|
|
4
|
+
readonly resourceName: string | undefined;
|
|
5
|
+
constructor(identity: string, resourceName?: string);
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=entity-not-found-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-not-found-error.d.ts","sourceRoot":"","sources":["../src/entity-not-found-error.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAE1C,YAAY,QAAQ,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,EAMlD;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Reports an operation that requires an entity absent from the resource. */
|
|
2
|
+
export class EntityNotFoundError extends Error {
|
|
3
|
+
identity;
|
|
4
|
+
resourceName;
|
|
5
|
+
constructor(identity, resourceName) {
|
|
6
|
+
const entityName = resourceName ?? "entity";
|
|
7
|
+
super(`No ${entityName} exists with identity "${identity}".`);
|
|
8
|
+
this.name = "EntityNotFoundError";
|
|
9
|
+
this.identity = identity;
|
|
10
|
+
this.resourceName = resourceName;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=entity-not-found-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-not-found-error.js","sourceRoot":"","sources":["../src/entity-not-found-error.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACnC,QAAQ,CAAS;IACjB,YAAY,CAAqB;IAE1C,YAAY,QAAgB,EAAE,YAAqB;QACjD,MAAM,UAAU,GAAG,YAAY,IAAI,QAAQ,CAAC;QAC5C,KAAK,CAAC,MAAM,UAAU,0BAA0B,QAAQ,IAAI,CAAC,CAAC;QAC9D,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Handles HTTP requests for one simulated service. */
|
|
2
|
+
export interface SimService {
|
|
3
|
+
handle: (request: Request) => Promise<Response> | Response;
|
|
4
|
+
}
|
|
5
|
+
/** Selects what an environment does with requests outside every simulation. */
|
|
6
|
+
export type UnhandledRequestPolicy = "error" | "passthrough";
|
|
7
|
+
/** Configures how a simulated environment handles unclaimed requests. */
|
|
8
|
+
export interface SimEnvironmentProps {
|
|
9
|
+
unhandledRequest?: UnhandledRequestPolicy;
|
|
10
|
+
}
|
|
11
|
+
/** Routes HTTP requests to the simulated services registered with it. */
|
|
12
|
+
export declare class SimEnvironment implements Disposable {
|
|
13
|
+
#private;
|
|
14
|
+
constructor(props?: SimEnvironmentProps);
|
|
15
|
+
/** Registers a simulated service for every request to an HTTP origin. */
|
|
16
|
+
register(origin: string | URL, service: SimService): void;
|
|
17
|
+
/** Stops this environment from intercepting requests. */
|
|
18
|
+
dispose(): void;
|
|
19
|
+
[Symbol.dispose](): void;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=environment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../src/environment.ts"],"names":[],"mappings":"AAQA,uDAAuD;AACvD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;CAC5D;AAED,+EAA+E;AAC/E,MAAM,MAAM,sBAAsB,GAAG,OAAO,GAAG,aAAa,CAAC;AAE7D,yEAAyE;AACzE,MAAM,WAAW,mBAAmB;IAClC,gBAAgB,CAAC,EAAE,sBAAsB,CAAC;CAC3C;AAED,yEAAyE;AACzE,qBAAa,cAAe,YAAW,UAAU;;IAM/C,YAAY,KAAK,GAAE,mBAAwB,EAK1C;IAED,yEAAyE;IACzE,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,UAAU,GAAG,IAAI,CAuBxD;IAED,yDAAyD;IACzD,OAAO,IAAI,IAAI,CAWd;IAED,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAEvB;CA8BF"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { interceptHttpRequests, } from "./http/interception.js";
|
|
2
|
+
import { UnclaimedOriginError } from "./unclaimed-origin-error.js";
|
|
3
|
+
const activeOrigins = new Set();
|
|
4
|
+
/** Routes HTTP requests to the simulated services registered with it. */
|
|
5
|
+
export class SimEnvironment {
|
|
6
|
+
#services = new Map();
|
|
7
|
+
#interception;
|
|
8
|
+
#unhandledRequest;
|
|
9
|
+
#disposed = false;
|
|
10
|
+
constructor(props = {}) {
|
|
11
|
+
this.#unhandledRequest = props.unhandledRequest ?? "error";
|
|
12
|
+
this.#interception = interceptHttpRequests((request) => this.#handle(request));
|
|
13
|
+
}
|
|
14
|
+
/** Registers a simulated service for every request to an HTTP origin. */
|
|
15
|
+
register(origin, service) {
|
|
16
|
+
const normalizedOrigin = this.#normalizeOrigin(origin);
|
|
17
|
+
if (this.#disposed) {
|
|
18
|
+
throw new Error(`Cannot register ${normalizedOrigin} with a disposed SimEnvironment.`);
|
|
19
|
+
}
|
|
20
|
+
if (this.#services.has(normalizedOrigin)) {
|
|
21
|
+
throw new Error(`A simulated service is already registered for ${normalizedOrigin}.`);
|
|
22
|
+
}
|
|
23
|
+
if (activeOrigins.has(normalizedOrigin)) {
|
|
24
|
+
throw new Error(`Another active SimEnvironment is already registered for ${normalizedOrigin}.`);
|
|
25
|
+
}
|
|
26
|
+
this.#services.set(normalizedOrigin, service);
|
|
27
|
+
activeOrigins.add(normalizedOrigin);
|
|
28
|
+
}
|
|
29
|
+
/** Stops this environment from intercepting requests. */
|
|
30
|
+
dispose() {
|
|
31
|
+
if (this.#disposed) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.#disposed = true;
|
|
35
|
+
for (const origin of this.#services.keys()) {
|
|
36
|
+
activeOrigins.delete(origin);
|
|
37
|
+
}
|
|
38
|
+
this.#services.clear();
|
|
39
|
+
this.#interception.dispose();
|
|
40
|
+
}
|
|
41
|
+
[Symbol.dispose]() {
|
|
42
|
+
this.dispose();
|
|
43
|
+
}
|
|
44
|
+
#handle(request) {
|
|
45
|
+
const origin = new URL(request.url).origin;
|
|
46
|
+
const service = this.#services.get(origin);
|
|
47
|
+
if (service !== undefined) {
|
|
48
|
+
return service.handle(request);
|
|
49
|
+
}
|
|
50
|
+
if (activeOrigins.has(origin) || this.#unhandledRequest === "passthrough") {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
throw new UnclaimedOriginError(request);
|
|
54
|
+
}
|
|
55
|
+
#normalizeOrigin(origin) {
|
|
56
|
+
const url = new URL(origin);
|
|
57
|
+
const protocols = ["http:", "https:"];
|
|
58
|
+
const isOrigin = url.href === `${url.origin}/`;
|
|
59
|
+
if (!protocols.includes(url.protocol) || !isOrigin) {
|
|
60
|
+
throw new TypeError(`Expected an HTTP origin, received ${origin.toString()}.`);
|
|
61
|
+
}
|
|
62
|
+
return url.origin;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=environment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"environment.js","sourceRoot":"","sources":["../src/environment.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,GAEtB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAEnE,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAC;AAexC,yEAAyE;AACzE,MAAM,OAAO,cAAc;IAChB,SAA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-0,0 +1,13 @@
|
|
|
1
|
+
export type HttpRequestHandler = (request: Request) => Promise<Response | undefined> | Response | undefined;
|
|
2
|
+
export interface HttpInterception extends Disposable {
|
|
3
|
+
dispose: () => void;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Subscribes a handler to HTTP interception for this process.
|
|
7
|
+
*
|
|
8
|
+
* The handler returns a response for requests it owns. Returning `undefined`
|
|
9
|
+
* lets the next subscriber inspect the request. The first subscription starts
|
|
10
|
+
* interception, and disposing the last subscription stops it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function interceptHttpRequests(handle: HttpRequestHandler): HttpInterception;
|
|
13
|
+
//# sourceMappingURL=interception.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interception.d.ts","sourceRoot":"","sources":["../../src/http/interception.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,kBAAkB,GAAG,CAC/B,OAAO,EAAE,OAAO,KACb,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,GAAG,QAAQ,GAAG,SAAS,CAAC;AAE1D,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IAClD,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAwCD;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,kBAAkB,GACzB,gBAAgB,CAyBlB"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { HttpRequestInterceptor } from "@mswjs/interceptors/http";
|
|
2
|
+
const requestHandlers = new Map();
|
|
3
|
+
let activeInterceptor;
|
|
4
|
+
async function handleInterceptedRequest(request, handlers = requestHandlers.values()) {
|
|
5
|
+
const nextHandler = handlers.next();
|
|
6
|
+
if (nextHandler.done === true) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
const response = await nextHandler.value(request.clone());
|
|
10
|
+
return response ?? handleInterceptedRequest(request, handlers);
|
|
11
|
+
}
|
|
12
|
+
function startInterceptor() {
|
|
13
|
+
const interceptor = new HttpRequestInterceptor();
|
|
14
|
+
interceptor.on("request", async ({ request, controller }) => {
|
|
15
|
+
const response = await handleInterceptedRequest(request);
|
|
16
|
+
if (response === undefined) {
|
|
17
|
+
await controller.passthrough();
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
controller.respondWith(response);
|
|
21
|
+
});
|
|
22
|
+
interceptor.on("unhandledException", ({ controller, error }) => {
|
|
23
|
+
controller.errorWith(error);
|
|
24
|
+
});
|
|
25
|
+
interceptor.apply();
|
|
26
|
+
return interceptor;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Subscribes a handler to HTTP interception for this process.
|
|
30
|
+
*
|
|
31
|
+
* The handler returns a response for requests it owns. Returning `undefined`
|
|
32
|
+
* lets the next subscriber inspect the request. The first subscription starts
|
|
33
|
+
* interception, and disposing the last subscription stops it.
|
|
34
|
+
*/
|
|
35
|
+
export function interceptHttpRequests(handle) {
|
|
36
|
+
const subscription = {};
|
|
37
|
+
requestHandlers.set(subscription, handle);
|
|
38
|
+
activeInterceptor ??= startInterceptor();
|
|
39
|
+
let disposed = false;
|
|
40
|
+
const dispose = () => {
|
|
41
|
+
if (disposed) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
disposed = true;
|
|
45
|
+
requestHandlers.delete(subscription);
|
|
46
|
+
if (requestHandlers.size === 0) {
|
|
47
|
+
activeInterceptor?.dispose();
|
|
48
|
+
activeInterceptor = undefined;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
dispose,
|
|
53
|
+
[Symbol.dispose]: dispose,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=interception.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interception.js","sourceRoot":"","sources":["../../src/http/interception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAUlE,MAAM,e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CAAC,GAAG,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IAC1C,iBAAiB,KAAK,gBAAgB,EAAE,CAAC;IAEzC,IAAI,QAAQ,GAAG,KAAK,CAAC;IAErB,MAAM,OAAO,GAAG,GAAS,EAAE;QACzB,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO;QACT,CAAC;QAED,QAAQ,GAAG,IAAI,CAAC;QAChB,eAAe,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QAErC,IAAI,eAAe,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;YAC/B,iBAAiB,EAAE,OAAO,EAAE,CAAC;YAC7B,iBAAiB,GAAG,SAAS,CAAC;QAChC,CAAC;IACH,CAAC,CAAC;IAEF,OAAO;QACL,OAAO;QACP,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO;KAC1B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HttpMiddleware, HttpOperation, HttpOperationContext } from "./operation.js";
|
|
2
|
+
import type { RouteMatch } from "./route.js";
|
|
3
|
+
/** Runs the protocol and semantic steps for a matched HTTP operation. */
|
|
4
|
+
export declare function runHttpOperation(operation: HttpOperation, match: RouteMatch, context: HttpOperationContext): Promise<Response>;
|
|
5
|
+
/** Runs middleware in registration order and unwinds the response in reverse. */
|
|
6
|
+
export declare function runMiddleware(context: HttpOperationContext, middleware: readonly HttpMiddleware[], operation: () => Promise<Response>, translateErrors: (action: () => Promise<Response> | Response) => Promise<Response>): Promise<Response>;
|
|
7
|
+
//# sourceMappingURL=operation-pipeline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation-pipeline.d.ts","sourceRoot":"","sources":["../../src/http/operation-pipeline.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,aAAa,EACb,oBAAoB,EACrB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,yEAAyE;AACzE,wBAAsB,gBAAgB,CACpC,SAAS,EAAE,aAAa,EACxB,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,QAAQ,CAAC,CAKnB;AAED,iFAAiF;AACjF,wBAAsB,aAAa,CACjC,OAAO,EAAE,oBAAoB,EAC7B,UAAU,EAAE,SAAS,cAAc,EAAE,EACrC,SAAS,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,EAClC,eAAe,EAAE,CACf,MAAM,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,KACvC,OAAO,CAAC,QAAQ,CAAC,GACrB,OAAO,CAAC,QAAQ,CAAC,CAmBnB"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Runs the protocol and semantic steps for a matched HTTP operation. */
|
|
2
|
+
export async function runHttpOperation(operation, match, context) {
|
|
3
|
+
const decoded = await operation.decode(context.request);
|
|
4
|
+
const input = await operation.transform(decoded, match);
|
|
5
|
+
const output = await operation.operate(input, context);
|
|
6
|
+
return operation.encode(output);
|
|
7
|
+
}
|
|
8
|
+
/** Runs middleware in registration order and unwinds the response in reverse. */
|
|
9
|
+
export async function runMiddleware(context, middleware, operation, translateErrors) {
|
|
10
|
+
let activeIndex = -1;
|
|
11
|
+
const dispatch = async (index) => {
|
|
12
|
+
if (index <= activeIndex) {
|
|
13
|
+
throw new Error("Middleware called next() more than once.");
|
|
14
|
+
}
|
|
15
|
+
activeIndex = index;
|
|
16
|
+
const current = middleware[index];
|
|
17
|
+
if (current === undefined) {
|
|
18
|
+
return translateErrors(operation);
|
|
19
|
+
}
|
|
20
|
+
return translateErrors(() => current(context, () => dispatch(index + 1)));
|
|
21
|
+
};
|
|
22
|
+
return dispatch(0);
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=operation-pipeline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation-pipeline.js","sour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operation.js";
|
|
2
|
+
/** Selects and runs registered operations for one simulated API. */
|
|
3
|
+
export declare class OperationRouter {
|
|
4
|
+
#private;
|
|
5
|
+
use(middleware: HttpMiddleware): void;
|
|
6
|
+
register(operation: HttpOperation): void;
|
|
7
|
+
handle(request: Request): Promise<Response>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=operation-router.d.ts.map
|