@effect/platform 0.87.0 → 0.87.2
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/HttpLayerRouter/package.json +6 -0
- package/README.md +272 -2
- package/dist/cjs/HttpApiBuilder.js +15 -1
- package/dist/cjs/HttpApiBuilder.js.map +1 -1
- package/dist/cjs/HttpApiScalar.js +29 -11
- package/dist/cjs/HttpApiScalar.js.map +1 -1
- package/dist/cjs/HttpApiSwagger.js +32 -15
- package/dist/cjs/HttpApiSwagger.js.map +1 -1
- package/dist/cjs/HttpLayerRouter.js +494 -0
- package/dist/cjs/HttpLayerRouter.js.map +1 -0
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/internal/httpRouter.js +3 -1
- package/dist/cjs/internal/httpRouter.js.map +1 -1
- package/dist/dts/HttpApiBuilder.d.ts +6 -0
- package/dist/dts/HttpApiBuilder.d.ts.map +1 -1
- package/dist/dts/HttpApiScalar.d.ts +14 -2
- package/dist/dts/HttpApiScalar.d.ts.map +1 -1
- package/dist/dts/HttpApiSwagger.d.ts +12 -2
- package/dist/dts/HttpApiSwagger.d.ts.map +1 -1
- package/dist/dts/HttpLayerRouter.d.ts +481 -0
- package/dist/dts/HttpLayerRouter.d.ts.map +1 -0
- package/dist/dts/index.d.ts +4 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/internal/httpRouter.d.ts.map +1 -1
- package/dist/esm/HttpApiBuilder.js +14 -0
- package/dist/esm/HttpApiBuilder.js.map +1 -1
- package/dist/esm/HttpApiScalar.js +28 -10
- package/dist/esm/HttpApiScalar.js.map +1 -1
- package/dist/esm/HttpApiSwagger.js +31 -14
- package/dist/esm/HttpApiSwagger.js.map +1 -1
- package/dist/esm/HttpLayerRouter.js +468 -0
- package/dist/esm/HttpLayerRouter.js.map +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/httpRouter.js +3 -1
- package/dist/esm/internal/httpRouter.js.map +1 -1
- package/package.json +10 -2
- package/src/HttpApiBuilder.ts +30 -0
- package/src/HttpApiScalar.ts +79 -41
- package/src/HttpApiSwagger.ts +49 -18
- package/src/HttpLayerRouter.ts +920 -0
- package/src/index.ts +5 -0
- package/src/internal/httpRouter.ts +4 -1
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MiddlewareTypeId = exports.HttpRouter = exports.FindMyWay = void 0;
|
|
7
|
+
Object.defineProperty(exports, "RouteContext", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _HttpRouter.RouteContext;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
exports.middleware = exports.make = exports.layer = exports.cors = exports.addHttpApi = exports.TypeId = exports.RouterConfig = exports.RouteTypeId = void 0;
|
|
14
|
+
Object.defineProperty(exports, "params", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () {
|
|
17
|
+
return _HttpRouter.params;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
exports.route = exports.prefixRoute = exports.prefixPath = void 0;
|
|
21
|
+
Object.defineProperty(exports, "schemaJson", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _HttpRouter.schemaJson;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(exports, "schemaNoBody", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () {
|
|
30
|
+
return _HttpRouter.schemaNoBody;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(exports, "schemaParams", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: function () {
|
|
36
|
+
return _HttpRouter.schemaParams;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(exports, "schemaPathParams", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () {
|
|
42
|
+
return _HttpRouter.schemaPathParams;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
exports.use = exports.toHttpEffect = exports.serve = void 0;
|
|
46
|
+
var HttpServerRequest = _interopRequireWildcard(require("@effect/platform/HttpServerRequest"));
|
|
47
|
+
var HttpServerResponse = _interopRequireWildcard(require("@effect/platform/HttpServerResponse"));
|
|
48
|
+
var Arr = _interopRequireWildcard(require("effect/Array"));
|
|
49
|
+
var Context = _interopRequireWildcard(require("effect/Context"));
|
|
50
|
+
var Effect = _interopRequireWildcard(require("effect/Effect"));
|
|
51
|
+
var FiberRef = _interopRequireWildcard(require("effect/FiberRef"));
|
|
52
|
+
var _Function = require("effect/Function");
|
|
53
|
+
var Layer = _interopRequireWildcard(require("effect/Layer"));
|
|
54
|
+
var Option = _interopRequireWildcard(require("effect/Option"));
|
|
55
|
+
var Scope = _interopRequireWildcard(require("effect/Scope"));
|
|
56
|
+
var Tracer = _interopRequireWildcard(require("effect/Tracer"));
|
|
57
|
+
var FindMyWay = _interopRequireWildcard(require("find-my-way-ts"));
|
|
58
|
+
var _FindMyWay = FindMyWay;
|
|
59
|
+
exports.FindMyWay = FindMyWay;
|
|
60
|
+
var HttpApi = _interopRequireWildcard(require("./HttpApi.js"));
|
|
61
|
+
var HttpApiBuilder = _interopRequireWildcard(require("./HttpApiBuilder.js"));
|
|
62
|
+
var HttpMiddleware = _interopRequireWildcard(require("./HttpMiddleware.js"));
|
|
63
|
+
var _HttpRouter = require("./HttpRouter.js");
|
|
64
|
+
var HttpServer = _interopRequireWildcard(require("./HttpServer.js"));
|
|
65
|
+
var HttpServerError = _interopRequireWildcard(require("./HttpServerError.js"));
|
|
66
|
+
var OpenApi = _interopRequireWildcard(require("./OpenApi.js"));
|
|
67
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
68
|
+
/**
|
|
69
|
+
* @since 1.0.0
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @since 1.0.0
|
|
74
|
+
* @category Re-exports
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @since 1.0.0
|
|
79
|
+
* @category HttpRouter
|
|
80
|
+
*/
|
|
81
|
+
const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("@effect/platform/HttpLayerRouter/HttpRouter");
|
|
82
|
+
/**
|
|
83
|
+
* @since 1.0.0
|
|
84
|
+
* @category HttpRouter
|
|
85
|
+
*/
|
|
86
|
+
const HttpRouter = exports.HttpRouter = /*#__PURE__*/Context.GenericTag("@effect/platform/HttpLayerRouter");
|
|
87
|
+
/**
|
|
88
|
+
* @since 1.0.0
|
|
89
|
+
* @category HttpRouter
|
|
90
|
+
*/
|
|
91
|
+
const make = exports.make = /*#__PURE__*/Effect.gen(function* () {
|
|
92
|
+
const router = FindMyWay.make(yield* RouterConfig);
|
|
93
|
+
const addAll = routes => Effect.contextWith(context => {
|
|
94
|
+
const middleware = getMiddleware(context);
|
|
95
|
+
const applyMiddleware = effect => {
|
|
96
|
+
for (let i = 0; i < middleware.length; i++) {
|
|
97
|
+
effect = middleware[i](effect);
|
|
98
|
+
}
|
|
99
|
+
return effect;
|
|
100
|
+
};
|
|
101
|
+
for (let i = 0; i < routes.length; i++) {
|
|
102
|
+
const route = middleware.length === 0 ? routes[i] : makeRoute({
|
|
103
|
+
...routes[i],
|
|
104
|
+
handler: applyMiddleware(routes[i].handler)
|
|
105
|
+
});
|
|
106
|
+
if (route.method === "*") {
|
|
107
|
+
router.all(route.path, route);
|
|
108
|
+
} else {
|
|
109
|
+
router.on(route.method, route.path, route);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return HttpRouter.of({
|
|
114
|
+
[TypeId]: TypeId,
|
|
115
|
+
prefixed(prefix) {
|
|
116
|
+
return HttpRouter.of({
|
|
117
|
+
[TypeId]: TypeId,
|
|
118
|
+
asHttpEffect: this.asHttpEffect,
|
|
119
|
+
prefixed: newPrefix => this.prefixed(prefixPath(prefix, newPrefix)),
|
|
120
|
+
addAll: routes => addAll(routes.map(prefixRoute(prefix))),
|
|
121
|
+
add: (method, path, handler, options) => addAll([makeRoute({
|
|
122
|
+
method,
|
|
123
|
+
path: prefixPath(path, prefix),
|
|
124
|
+
handler: Effect.isEffect(handler) ? handler : Effect.flatMap(HttpServerRequest.HttpServerRequest, handler),
|
|
125
|
+
uninterruptible: options?.uninterruptible ?? false,
|
|
126
|
+
prefix: Option.some(prefix)
|
|
127
|
+
})])
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
addAll,
|
|
131
|
+
add: (method, path, handler, options) => addAll([route(method, path, handler, options)]),
|
|
132
|
+
asHttpEffect() {
|
|
133
|
+
return Effect.withFiberRuntime(fiber => {
|
|
134
|
+
const contextMap = new Map(fiber.currentContext.unsafeMap);
|
|
135
|
+
const request = contextMap.get(HttpServerRequest.HttpServerRequest.key);
|
|
136
|
+
let result = router.find(request.method, request.url);
|
|
137
|
+
if (result === undefined && request.method === "HEAD") {
|
|
138
|
+
result = router.find("GET", request.url);
|
|
139
|
+
}
|
|
140
|
+
if (result === undefined) {
|
|
141
|
+
return Effect.fail(new HttpServerError.RouteNotFound({
|
|
142
|
+
request
|
|
143
|
+
}));
|
|
144
|
+
}
|
|
145
|
+
const route = result.handler;
|
|
146
|
+
if (route.prefix._tag === "Some") {
|
|
147
|
+
contextMap.set(HttpServerRequest.HttpServerRequest.key, sliceRequestUrl(request, route.prefix.value));
|
|
148
|
+
}
|
|
149
|
+
contextMap.set(HttpServerRequest.ParsedSearchParams.key, result.searchParams);
|
|
150
|
+
contextMap.set(_HttpRouter.RouteContext.key, {
|
|
151
|
+
[_HttpRouter.RouteContextTypeId]: _HttpRouter.RouteContextTypeId,
|
|
152
|
+
route,
|
|
153
|
+
params: result.params
|
|
154
|
+
});
|
|
155
|
+
const span = contextMap.get(Tracer.ParentSpan.key);
|
|
156
|
+
if (span && span._tag === "Span") {
|
|
157
|
+
span.attribute("http.route", route.path);
|
|
158
|
+
}
|
|
159
|
+
return Effect.locally(route.uninterruptible ? route.handler : Effect.interruptible(route.handler), FiberRef.currentContext, Context.unsafeMake(contextMap));
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
function sliceRequestUrl(request, prefix) {
|
|
165
|
+
const prefexLen = prefix.length;
|
|
166
|
+
return request.modify({
|
|
167
|
+
url: request.url.length <= prefexLen ? "/" : request.url.slice(prefexLen)
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* @since 1.0.0
|
|
172
|
+
* @category Configuration
|
|
173
|
+
*/
|
|
174
|
+
class RouterConfig extends /*#__PURE__*/Context.Reference()("@effect/platform/HttpLayerRouter/RouterConfig", {
|
|
175
|
+
defaultValue: /*#__PURE__*/(0, _Function.constant)({})
|
|
176
|
+
}) {}
|
|
177
|
+
exports.RouterConfig = RouterConfig;
|
|
178
|
+
/**
|
|
179
|
+
* A helper function that is the equivalent of:
|
|
180
|
+
*
|
|
181
|
+
* ```ts
|
|
182
|
+
* import * as HttpLayerRouter from "@effect/platform/HttpLayerRouter"
|
|
183
|
+
* import * as Effect from "effect/Effect"
|
|
184
|
+
* import * as Layer from "effect/Layer"
|
|
185
|
+
*
|
|
186
|
+
* const MyRoute = Layer.scopedDiscard(Effect.gen(function*() {
|
|
187
|
+
* const router = yield* HttpLayerRouter.HttpRouter
|
|
188
|
+
*
|
|
189
|
+
* // then use `router.add` to add a route
|
|
190
|
+
* }))
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @since 1.0.0
|
|
194
|
+
* @category HttpRouter
|
|
195
|
+
*/
|
|
196
|
+
const use = f => Layer.scopedDiscard(Effect.flatMap(HttpRouter, f));
|
|
197
|
+
/**
|
|
198
|
+
* @since 1.0.0
|
|
199
|
+
* @category HttpRouter
|
|
200
|
+
*/
|
|
201
|
+
exports.use = use;
|
|
202
|
+
const layer = exports.layer = /*#__PURE__*/Layer.effect(HttpRouter, make);
|
|
203
|
+
/**
|
|
204
|
+
* @since 1.0.0
|
|
205
|
+
* @category HttpRouter
|
|
206
|
+
*/
|
|
207
|
+
const toHttpEffect = appLayer => Effect.gen(function* () {
|
|
208
|
+
const scope = yield* Effect.scope;
|
|
209
|
+
const memoMap = yield* Layer.CurrentMemoMap;
|
|
210
|
+
const context = yield* Layer.buildWithMemoMap(Layer.provideMerge(appLayer, layer), memoMap, scope);
|
|
211
|
+
const router = Context.get(context, HttpRouter);
|
|
212
|
+
return router.asHttpEffect();
|
|
213
|
+
});
|
|
214
|
+
/**
|
|
215
|
+
* @since 1.0.0
|
|
216
|
+
* @category Route
|
|
217
|
+
*/
|
|
218
|
+
exports.toHttpEffect = toHttpEffect;
|
|
219
|
+
const RouteTypeId = exports.RouteTypeId = /*#__PURE__*/Symbol.for("@effect/platform/HttpLayerRouter/Route");
|
|
220
|
+
const makeRoute = options => ({
|
|
221
|
+
...options,
|
|
222
|
+
uninterruptible: options.uninterruptible ?? false,
|
|
223
|
+
prefix: options.prefix ?? Option.none(),
|
|
224
|
+
[RouteTypeId]: RouteTypeId
|
|
225
|
+
});
|
|
226
|
+
/**
|
|
227
|
+
* @since 1.0.0
|
|
228
|
+
* @category Route
|
|
229
|
+
*/
|
|
230
|
+
const route = (method, path, handler, options) => makeRoute({
|
|
231
|
+
...options,
|
|
232
|
+
method,
|
|
233
|
+
path,
|
|
234
|
+
handler: Effect.isEffect(handler) ? handler : Effect.flatMap(HttpServerRequest.HttpServerRequest, handler),
|
|
235
|
+
uninterruptible: options?.uninterruptible ?? false
|
|
236
|
+
});
|
|
237
|
+
exports.route = route;
|
|
238
|
+
const removeTrailingSlash = path => path.endsWith("/") ? path.slice(0, -1) : path;
|
|
239
|
+
/**
|
|
240
|
+
* @since 1.0.0
|
|
241
|
+
* @category PathInput
|
|
242
|
+
*/
|
|
243
|
+
const prefixPath = exports.prefixPath = /*#__PURE__*/(0, _Function.dual)(2, (self, prefix) => {
|
|
244
|
+
prefix = removeTrailingSlash(prefix);
|
|
245
|
+
return self === "/" ? prefix : prefix + self;
|
|
246
|
+
});
|
|
247
|
+
/**
|
|
248
|
+
* @since 1.0.0
|
|
249
|
+
* @category Route
|
|
250
|
+
*/
|
|
251
|
+
const prefixRoute = exports.prefixRoute = /*#__PURE__*/(0, _Function.dual)(2, (self, prefix) => makeRoute({
|
|
252
|
+
...self,
|
|
253
|
+
path: prefixPath(self.path, prefix),
|
|
254
|
+
prefix: Option.match(self.prefix, {
|
|
255
|
+
onNone: () => Option.some(prefix),
|
|
256
|
+
onSome: existingPrefix => Option.some(prefixPath(existingPrefix, prefix))
|
|
257
|
+
})
|
|
258
|
+
}));
|
|
259
|
+
/**
|
|
260
|
+
* @since 1.0.0
|
|
261
|
+
* @category Middleware
|
|
262
|
+
*/
|
|
263
|
+
const MiddlewareTypeId = exports.MiddlewareTypeId = /*#__PURE__*/Symbol.for("@effect/platform/HttpLayerRouter/Middleware");
|
|
264
|
+
/**
|
|
265
|
+
* Create a middleware layer that can be used to modify requests and responses.
|
|
266
|
+
*
|
|
267
|
+
* ```ts
|
|
268
|
+
* import * as HttpLayerRouter from "@effect/platform/HttpLayerRouter"
|
|
269
|
+
* import * as HttpMiddleware from "@effect/platform/HttpMiddleware"
|
|
270
|
+
* import * as HttpServerResponse from "@effect/platform/HttpServerResponse"
|
|
271
|
+
* import * as Context from "effect/Context"
|
|
272
|
+
* import * as Effect from "effect/Effect"
|
|
273
|
+
* import * as Layer from "effect/Layer"
|
|
274
|
+
*
|
|
275
|
+
* // Here we are defining a CORS middleware
|
|
276
|
+
* const CorsMiddleware = HttpLayerRouter.middleware(HttpMiddleware.cors()).layer
|
|
277
|
+
* // You can also use HttpLayerRouter.cors() to create a CORS middleware
|
|
278
|
+
*
|
|
279
|
+
* class CurrentSession extends Context.Tag("CurrentSession")<CurrentSession, {
|
|
280
|
+
* readonly token: string
|
|
281
|
+
* }>() {}
|
|
282
|
+
*
|
|
283
|
+
* // You can create middleware that provides a service to the HTTP requests.
|
|
284
|
+
* const SessionMiddleware = HttpLayerRouter.middleware<{
|
|
285
|
+
* provides: CurrentSession
|
|
286
|
+
* }>()(
|
|
287
|
+
* Effect.gen(function*() {
|
|
288
|
+
* yield* Effect.log("SessionMiddleware initialized")
|
|
289
|
+
*
|
|
290
|
+
* return (httpEffect) =>
|
|
291
|
+
* Effect.provideService(httpEffect, CurrentSession, {
|
|
292
|
+
* token: "dummy-token"
|
|
293
|
+
* })
|
|
294
|
+
* })
|
|
295
|
+
* ).layer
|
|
296
|
+
*
|
|
297
|
+
* Effect.gen(function*() {
|
|
298
|
+
* const router = yield* HttpLayerRouter.HttpRouter
|
|
299
|
+
* yield* router.add(
|
|
300
|
+
* "GET",
|
|
301
|
+
* "/hello",
|
|
302
|
+
* Effect.gen(function*() {
|
|
303
|
+
* // Requests can now access the current session
|
|
304
|
+
* const session = yield* CurrentSession
|
|
305
|
+
* return HttpServerResponse.text(`Hello, World! Your token is ${session.token}`)
|
|
306
|
+
* })
|
|
307
|
+
* )
|
|
308
|
+
* }).pipe(
|
|
309
|
+
* Layer.effectDiscard,
|
|
310
|
+
* // Provide the SessionMiddleware & CorsMiddleware to some routes
|
|
311
|
+
* Layer.provide([SessionMiddleware, CorsMiddleware])
|
|
312
|
+
* )
|
|
313
|
+
* ```
|
|
314
|
+
*
|
|
315
|
+
* @since 1.0.0
|
|
316
|
+
* @category Middleware
|
|
317
|
+
*/
|
|
318
|
+
const middleware = function () {
|
|
319
|
+
if (arguments.length === 0) {
|
|
320
|
+
return makeMiddleware;
|
|
321
|
+
}
|
|
322
|
+
return makeMiddleware(arguments[0]);
|
|
323
|
+
};
|
|
324
|
+
exports.middleware = middleware;
|
|
325
|
+
const makeMiddleware = middleware => new MiddlewareImpl(Effect.isEffect(middleware) ? Layer.scopedContext(Effect.map(middleware, fn => Context.unsafeMake(new Map([[fnContextKey, fn]])))) : Layer.succeedContext(Context.unsafeMake(new Map([[fnContextKey, middleware]]))));
|
|
326
|
+
let middlewareId = 0;
|
|
327
|
+
const fnContextKey = "@effect/platform/HttpLayerRouter/MiddlewareFn";
|
|
328
|
+
class MiddlewareImpl {
|
|
329
|
+
layerFn;
|
|
330
|
+
dependencies;
|
|
331
|
+
[MiddlewareTypeId] = {};
|
|
332
|
+
constructor(layerFn, dependencies) {
|
|
333
|
+
this.layerFn = layerFn;
|
|
334
|
+
this.dependencies = dependencies;
|
|
335
|
+
const contextKey = `@effect/platform/HttpLayerRouter/Middleware-${++middlewareId}`;
|
|
336
|
+
this.layer = Layer.scopedContext(Effect.gen(this, function* () {
|
|
337
|
+
const context = yield* Effect.context();
|
|
338
|
+
const stack = [context.unsafeMap.get(fnContextKey)];
|
|
339
|
+
if (this.dependencies) {
|
|
340
|
+
const memoMap = yield* Layer.CurrentMemoMap;
|
|
341
|
+
const scope = Context.get(context, Scope.Scope);
|
|
342
|
+
const depsContext = yield* Layer.buildWithMemoMap(this.dependencies, memoMap, scope);
|
|
343
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
344
|
+
stack.push(...getMiddleware(depsContext));
|
|
345
|
+
}
|
|
346
|
+
return Context.unsafeMake(new Map([[contextKey, stack]]));
|
|
347
|
+
})).pipe(Layer.provide(this.layerFn));
|
|
348
|
+
}
|
|
349
|
+
layer;
|
|
350
|
+
combine(other) {
|
|
351
|
+
return new MiddlewareImpl(this.layerFn, this.dependencies ? Layer.provideMerge(this.dependencies, other.layer) : other.layer);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const middlewareCache = /*#__PURE__*/new WeakMap();
|
|
355
|
+
const getMiddleware = context => {
|
|
356
|
+
let arr = middlewareCache.get(context);
|
|
357
|
+
if (arr) return arr;
|
|
358
|
+
const topLevel = Arr.empty();
|
|
359
|
+
let maxLength = 0;
|
|
360
|
+
for (const [key, value] of context.unsafeMap) {
|
|
361
|
+
if (key.startsWith("@effect/platform/HttpLayerRouter/Middleware-")) {
|
|
362
|
+
topLevel.push(value);
|
|
363
|
+
if (value.length > maxLength) {
|
|
364
|
+
maxLength = value.length;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (topLevel.length === 0) {
|
|
369
|
+
arr = [];
|
|
370
|
+
} else {
|
|
371
|
+
const middleware = new Set();
|
|
372
|
+
for (let i = maxLength - 1; i >= 0; i--) {
|
|
373
|
+
for (const arr of topLevel) {
|
|
374
|
+
if (i < arr.length) {
|
|
375
|
+
middleware.add(arr[i]);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
arr = Arr.fromIterable(middleware).reverse();
|
|
380
|
+
}
|
|
381
|
+
middlewareCache.set(context, arr);
|
|
382
|
+
return arr;
|
|
383
|
+
};
|
|
384
|
+
/**
|
|
385
|
+
* A middleware that applies CORS headers to the HTTP response.
|
|
386
|
+
*
|
|
387
|
+
* @since 1.0.0
|
|
388
|
+
* @category Middleware
|
|
389
|
+
*/
|
|
390
|
+
const cors = options => middleware(HttpMiddleware.cors(options)).layer;
|
|
391
|
+
/**
|
|
392
|
+
* ```ts
|
|
393
|
+
* import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"
|
|
394
|
+
* import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
|
|
395
|
+
* import * as HttpApi from "@effect/platform/HttpApi"
|
|
396
|
+
* import * as HttpApiBuilder from "@effect/platform/HttpApiBuilder"
|
|
397
|
+
* import * as HttpApiEndpoint from "@effect/platform/HttpApiEndpoint"
|
|
398
|
+
* import * as HttpApiGroup from "@effect/platform/HttpApiGroup"
|
|
399
|
+
* import * as HttpApiScalar from "@effect/platform/HttpApiScalar"
|
|
400
|
+
* import * as HttpLayerRouter from "@effect/platform/HttpLayerRouter"
|
|
401
|
+
* import * as HttpMiddleware from "@effect/platform/HttpMiddleware"
|
|
402
|
+
* import * as Effect from "effect/Effect"
|
|
403
|
+
* import * as Layer from "effect/Layer"
|
|
404
|
+
* import { createServer } from "http"
|
|
405
|
+
*
|
|
406
|
+
* // First, we define our HttpApi
|
|
407
|
+
* class MyApi extends HttpApi.make("api").add(
|
|
408
|
+
* HttpApiGroup.make("users").add(
|
|
409
|
+
* HttpApiEndpoint.get("me", "/me")
|
|
410
|
+
* ).prefix("/users")
|
|
411
|
+
* ) {}
|
|
412
|
+
*
|
|
413
|
+
* // Implement the handlers for the API
|
|
414
|
+
* const UsersApiLayer = HttpApiBuilder.group(MyApi, "users", (handers) => handers.handle("me", () => Effect.void))
|
|
415
|
+
*
|
|
416
|
+
* // Use `HttpLayerRouter.addHttpApi` to register the API with the router
|
|
417
|
+
* const HttpApiRoutes = HttpLayerRouter.addHttpApi(MyApi, {
|
|
418
|
+
* openapiPath: "/docs/openapi.json"
|
|
419
|
+
* }).pipe(
|
|
420
|
+
* // Provide the api handlers layer
|
|
421
|
+
* Layer.provide(UsersApiLayer)
|
|
422
|
+
* )
|
|
423
|
+
*
|
|
424
|
+
* // Create a /docs route for the API documentation
|
|
425
|
+
* const DocsRoute = HttpApiScalar.layerHttpLayerRouter({
|
|
426
|
+
* api: MyApi,
|
|
427
|
+
* path: "/docs"
|
|
428
|
+
* })
|
|
429
|
+
*
|
|
430
|
+
* const CorsMiddleware = HttpLayerRouter.middleware(HttpMiddleware.cors())
|
|
431
|
+
* // You can also use HttpLayerRouter.cors() to create a CORS middleware
|
|
432
|
+
*
|
|
433
|
+
* // Finally, we merge all routes and serve them using the Node HTTP server
|
|
434
|
+
* const AllRoutes = Layer.mergeAll(
|
|
435
|
+
* HttpApiRoutes,
|
|
436
|
+
* DocsRoute
|
|
437
|
+
* ).pipe(
|
|
438
|
+
* Layer.provide(CorsMiddleware.layer)
|
|
439
|
+
* )
|
|
440
|
+
*
|
|
441
|
+
* HttpLayerRouter.serve(AllRoutes).pipe(
|
|
442
|
+
* Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })),
|
|
443
|
+
* Layer.launch,
|
|
444
|
+
* NodeRuntime.runMain
|
|
445
|
+
* )
|
|
446
|
+
* ```
|
|
447
|
+
*
|
|
448
|
+
* @since 1.0.0
|
|
449
|
+
* @category HttpApi
|
|
450
|
+
*/
|
|
451
|
+
exports.cors = cors;
|
|
452
|
+
const addHttpApi = (api, options) => {
|
|
453
|
+
const ApiMiddleware = middleware(HttpApiBuilder.buildMiddleware(api)).layer;
|
|
454
|
+
return HttpApiBuilder.Router.unwrap(Effect.fnUntraced(function* (router_) {
|
|
455
|
+
const contextMap = new Map();
|
|
456
|
+
const router = yield* HttpRouter;
|
|
457
|
+
const routes = Arr.empty();
|
|
458
|
+
const context = yield* Effect.context();
|
|
459
|
+
contextMap.set(HttpApi.Api.key, {
|
|
460
|
+
api,
|
|
461
|
+
context
|
|
462
|
+
});
|
|
463
|
+
for (const route of router_.routes) {
|
|
464
|
+
routes.push(makeRoute(route));
|
|
465
|
+
}
|
|
466
|
+
yield* router.addAll(routes);
|
|
467
|
+
if (options?.openapiPath) {
|
|
468
|
+
const spec = OpenApi.fromApi(api);
|
|
469
|
+
yield* router.add("GET", options.openapiPath, Effect.succeed(HttpServerResponse.unsafeJson(spec)));
|
|
470
|
+
}
|
|
471
|
+
return Context.unsafeMake(contextMap);
|
|
472
|
+
}, Layer.effectContext)).pipe(Layer.provide(ApiMiddleware));
|
|
473
|
+
};
|
|
474
|
+
/**
|
|
475
|
+
* Serves the provided application layer as an HTTP server.
|
|
476
|
+
*
|
|
477
|
+
* @since 1.0.0
|
|
478
|
+
* @category Server
|
|
479
|
+
*/
|
|
480
|
+
exports.addHttpApi = addHttpApi;
|
|
481
|
+
const serve = (appLayer, options) => {
|
|
482
|
+
let middleware = options?.middleware;
|
|
483
|
+
if (options?.disableLogger !== true) {
|
|
484
|
+
middleware = middleware ? (0, _Function.compose)(middleware, HttpMiddleware.logger) : HttpMiddleware.logger;
|
|
485
|
+
}
|
|
486
|
+
const RouterLayer = options?.routerConfig ? Layer.provide(layer, Layer.succeed(RouterConfig, options.routerConfig)) : layer;
|
|
487
|
+
return Effect.gen(function* () {
|
|
488
|
+
const router = yield* HttpRouter;
|
|
489
|
+
const handler = router.asHttpEffect();
|
|
490
|
+
return middleware ? HttpServer.serve(handler, middleware) : HttpServer.serve(handler);
|
|
491
|
+
}).pipe(Layer.unwrapScoped, options?.disableListenLog ? _Function.identity : HttpServer.withLogAddress, Layer.provide(appLayer), Layer.provide(RouterLayer));
|
|
492
|
+
};
|
|
493
|
+
exports.serve = serve;
|
|
494
|
+
//# sourceMappingURL=HttpLayerRouter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpLayerRouter.js","names":["HttpServerRequest","_interopRequireWildcard","require","HttpServerResponse","Arr","Context","Effect","FiberRef","_Function","Layer","Option","Scope","Tracer","FindMyWay","_FindMyWay","exports","HttpApi","HttpApiBuilder","HttpMiddleware","_HttpRouter","HttpServer","HttpServerError","OpenApi","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","TypeId","Symbol","for","HttpRouter","GenericTag","make","gen","router","RouterConfig","addAll","routes","contextWith","context","middleware","getMiddleware","applyMiddleware","effect","length","route","makeRoute","handler","method","all","path","on","of","prefixed","prefix","asHttpEffect","newPrefix","prefixPath","map","prefixRoute","add","options","isEffect","flatMap","uninterruptible","some","withFiberRuntime","fiber","contextMap","Map","currentContext","unsafeMap","request","key","result","find","url","undefined","fail","RouteNotFound","_tag","sliceRequestUrl","value","ParsedSearchParams","searchParams","RouteContext","RouteContextTypeId","params","span","ParentSpan","attribute","locally","interruptible","unsafeMake","prefexLen","modify","slice","Reference","defaultValue","constant","use","scopedDiscard","layer","toHttpEffect","appLayer","scope","memoMap","CurrentMemoMap","buildWithMemoMap","provideMerge","RouteTypeId","none","removeTrailingSlash","endsWith","dual","self","match","onNone","onSome","existingPrefix","MiddlewareTypeId","arguments","makeMiddleware","MiddlewareImpl","scopedContext","fn","fnContextKey","succeedContext","middlewareId","layerFn","dependencies","constructor","contextKey","stack","depsContext","push","pipe","provide","combine","other","middlewareCache","arr","topLevel","empty","maxLength","startsWith","Set","fromIterable","reverse","cors","addHttpApi","api","ApiMiddleware","buildMiddleware","Router","unwrap","fnUntraced","router_","Api","openapiPath","spec","fromApi","succeed","unsafeJson","effectContext","serve","disableLogger","compose","logger","RouterLayer","routerConfig","unwrapScoped","disableListenLog","identity","withLogAddress"],"sources":["../../src/HttpLayerRouter.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAAA,iBAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,kBAAA,GAAAF,uBAAA,CAAAC,OAAA;AACA,IAAAE,GAAA,GAAAH,uBAAA,CAAAC,OAAA;AACA,IAAAG,OAAA,GAAAJ,uBAAA,CAAAC,OAAA;AACA,IAAAI,MAAA,GAAAL,uBAAA,CAAAC,OAAA;AACA,IAAAK,QAAA,GAAAN,uBAAA,CAAAC,OAAA;AACA,IAAAM,SAAA,GAAAN,OAAA;AACA,IAAAO,KAAA,GAAAR,uBAAA,CAAAC,OAAA;AACA,IAAAQ,MAAA,GAAAT,uBAAA,CAAAC,OAAA;AACA,IAAAS,KAAA,GAAAV,uBAAA,CAAAC,OAAA;AACA,IAAAU,MAAA,GAAAX,uBAAA,CAAAC,OAAA;AACA,IAAAW,SAAA,GAAAZ,uBAAA,CAAAC,OAAA;AAA2C,IAAAY,UAAA,GAAAD,SA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AA,GAAAA,UAAA;AAMO,MAAMe,KAAK,GAAGA,CACnB7D,QAA8B,EAC9B3C,OAsBC,KAC8G;EAC/G,IAAIrB,UAAU,GAAQqB,OAAO,EAAErB,UAAU;EACzC,IAAIqB,OAAO,EAAEyG,aAAa,KAAK,IAAI,EAAE;IACnC9H,UAAU,GAAGA,UAAU,GAAG,IAAA+H,iBAAO,EAAC/H,UAAU,EAAErC,cAAc,CAACqK,MAAM,CAAC,GAAGrK,cAAc,CAACqK,MAAM;EAC9F;EACA,MAAMC,WAAW,GAAG5G,OAAO,EAAE6G,YAAY,GACrChL,KAAK,CAAC+I,OAAO,CAACnC,KAAK,EAAE5G,KAAK,CAACwK,OAAO,CAAC/H,YAAY,EAAE0B,OAAO,CAAC6G,YAAY,CAAC,CAAC,GACvEpE,KAAK;EACT,OAAO/G,MAAM,CAAC0C,GAAG,CAAC,aAAS;IACzB,MAAMC,MAAM,GAAG,OAAOJ,UAAU;IAChC,MAAMiB,OAAO,GAAGb,MAAM,CAACqB,YAAY,EAAE;IACrC,OAAOf,UAAU,GAAGnC,UAAU,CAACgK,KAAK,CAACtH,OAAO,EAAEP,UAAU,CAAC,GAAGnC,UAAU,CAACgK,KAAK,CAACtH,OAAO,CAAC;EACvF,CAAC,CAAC,CAACyF,IAAI,CACL9I,KAAK,CAACiL,YAAY,EAClB9G,OAAO,EAAE+G,gBAAgB,GAAGC,kBAAQ,GAAGxK,UAAU,CAACyK,cAAc,EAChEpL,KAAK,CAAC+I,OAAO,CAACjC,QAAQ,CAAC,EACvB9G,KAAK,CAAC+I,OAAO,CAACgC,WAAW,CAAC,CACpB;AACV,CAAC;AAAAzK,OAAA,CAAAqK,KAAA,GAAAA,KAAA","ignoreList":[]}
|
package/dist/cjs/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.WorkerRunner = exports.WorkerError = exports.Worker = exports.UrlParams = exports.Url = exports.Transferable = exports.Terminal = exports.Template = exports.SocketServer = exports.Socket = exports.Runtime = exports.PlatformLogger = exports.PlatformConfigProvider = exports.Path = exports.OpenApiJsonSchema = exports.OpenApi = exports.Ndjson = exports.Multipart = exports.MsgPack = exports.KeyValueStore = exports.HttpTraceContext = exports.HttpServerResponse = exports.HttpServerRespondable = exports.HttpServerRequest = exports.HttpServerError = exports.HttpServer = exports.HttpRouter = exports.HttpPlatform = exports.HttpMultiplex = exports.HttpMiddleware = exports.HttpMethod = exports.HttpIncomingMessage = exports.HttpClientResponse = exports.HttpClientRequest = exports.HttpClientError = exports.HttpClient = exports.HttpBody = exports.HttpApp = exports.HttpApiSwagger = exports.HttpApiSecurity = exports.HttpApiSchema = exports.HttpApiScalar = exports.HttpApiMiddleware = exports.HttpApiGroup = exports.HttpApiError = exports.HttpApiEndpoint = exports.HttpApiClient = exports.HttpApiBuilder = exports.HttpApi = exports.Headers = exports.FileSystem = exports.FetchHttpClient = exports.Etag = exports.Error = exports.Effectify = exports.Cookies = exports.CommandExecutor = exports.Command = exports.ChannelSchema = void 0;
|
|
6
|
+
exports.WorkerRunner = exports.WorkerError = exports.Worker = exports.UrlParams = exports.Url = exports.Transferable = exports.Terminal = exports.Template = exports.SocketServer = exports.Socket = exports.Runtime = exports.PlatformLogger = exports.PlatformConfigProvider = exports.Path = exports.OpenApiJsonSchema = exports.OpenApi = exports.Ndjson = exports.Multipart = exports.MsgPack = exports.KeyValueStore = exports.HttpTraceContext = exports.HttpServerResponse = exports.HttpServerRespondable = exports.HttpServerRequest = exports.HttpServerError = exports.HttpServer = exports.HttpRouter = exports.HttpPlatform = exports.HttpMultiplex = exports.HttpMiddleware = exports.HttpMethod = exports.HttpLayerRouter = exports.HttpIncomingMessage = exports.HttpClientResponse = exports.HttpClientRequest = exports.HttpClientError = exports.HttpClient = exports.HttpBody = exports.HttpApp = exports.HttpApiSwagger = exports.HttpApiSecurity = exports.HttpApiSchema = exports.HttpApiScalar = exports.HttpApiMiddleware = exports.HttpApiGroup = exports.HttpApiError = exports.HttpApiEndpoint = exports.HttpApiClient = exports.HttpApiBuilder = exports.HttpApi = exports.Headers = exports.FileSystem = exports.FetchHttpClient = exports.Etag = exports.Error = exports.Effectify = exports.Cookies = exports.CommandExecutor = exports.Command = exports.ChannelSchema = void 0;
|
|
7
7
|
var _ChannelSchema = _interopRequireWildcard(require("./ChannelSchema.js"));
|
|
8
8
|
exports.ChannelSchema = _ChannelSchema;
|
|
9
9
|
var _Command = _interopRequireWildcard(require("./Command.js"));
|
|
@@ -60,6 +60,8 @@ var _HttpClientResponse = _interopRequireWildcard(require("./HttpClientResponse.
|
|
|
60
60
|
exports.HttpClientResponse = _HttpClientResponse;
|
|
61
61
|
var _HttpIncomingMessage = _interopRequireWildcard(require("./HttpIncomingMessage.js"));
|
|
62
62
|
exports.HttpIncomingMessage = _HttpIncomingMessage;
|
|
63
|
+
var _HttpLayerRouter = _interopRequireWildcard(require("./HttpLayerRouter.js"));
|
|
64
|
+
exports.HttpLayerRouter = _HttpLayerRouter;
|
|
63
65
|
var _HttpMethod = _interopRequireWildcard(require("./HttpMethod.js"));
|
|
64
66
|
exports.HttpMethod = _HttpMethod;
|
|
65
67
|
var _HttpMiddleware = _interopRequireWildcard(require("./HttpMiddleware.js"));
|
|
@@ -141,9 +141,11 @@ const toHttpApp = (self, config) => {
|
|
|
141
141
|
const context = Context.unsafeMake(new Map(fiber.getFiberRef(FiberRef.currentContext).unsafeMap));
|
|
142
142
|
const request = Context.unsafeGet(context, ServerRequest.HttpServerRequest);
|
|
143
143
|
if (mountsLen > 0) {
|
|
144
|
+
const searchIndex = request.url.indexOf("?");
|
|
145
|
+
const pathname = searchIndex === -1 ? request.url : request.url.slice(0, searchIndex);
|
|
144
146
|
for (let i = 0; i < mountsLen; i++) {
|
|
145
147
|
const [path, routeContext, options] = mounts[i];
|
|
146
|
-
if (
|
|
148
|
+
if (pathname === path || pathname.startsWith(path + "/")) {
|
|
147
149
|
context.unsafeMap.set(RouteContext.key, routeContext);
|
|
148
150
|
if (options?.includePrefix !== true) {
|
|
149
151
|
context.unsafeMap.set(ServerRequest.HttpServerRequest.key, sliceRequestUrl(request, path));
|