EdgeAIG's picture
download
raw
2.69 kB
/**
* Tests `HttpApi` implementations through an in-memory generated client.
*
* The helpers use the same request encoding, routing, response encoding, and
* client decoding as the normal `HttpApiBuilder` and `HttpApiClient` pipeline,
* but they do not start an HTTP server. This is useful for focused handler
* tests, schema round trips, middleware behavior, and typed client calls.
*
* @since 4.0.0
*/
import * as Context from "../../Context.js";
import * as Effect from "../../Effect.js";
import * as Layer from "../../Layer.js";
import * as HttpClient from "../http/HttpClient.js";
import * as HttpRouter from "../http/HttpRouter.js";
import * as HttpServerRequest from "../http/HttpServerRequest.js";
import * as HttpServerResponse from "../http/HttpServerResponse.js";
import * as HttpApiBuilder from "./HttpApiBuilder.js";
import * as HttpApiClient from "./HttpApiClient.js";
/**
* Creates an in-memory client for testing selected groups of an `HttpApi`.
*
* **Details**
*
* Handlers for the selected groups are taken from the environment; unselected
* groups are wired with placeholder handlers that fail if called.
*
* @category testing
* @since 4.0.0
*/
export const groups = /*#__PURE__*/Effect.fnUntraced(function* (api, groupNames, options) {
let context = yield* Effect.context();
for (const name in api.groups) {
const group = api.groups[name];
if (groupNames.includes(name)) {
continue;
}
const handlers = new Map();
const routes = [];
for (const endpointName in group.endpoints) {
const endpoint = group.endpoints[endpointName];
const handler = {
endpoint: endpoint,
handler: () => Effect.die(new Error(`Unhandled endpoint: ${endpointName}`)),
isRaw: false,
uninterruptible: false
};
handlers.set(endpointName, handler);
routes.push(HttpApiBuilder.handlerToRoute(group, handler, context));
}
context = Context.add(context, group, {
handlers,
routes
});
}
const layer = HttpApiBuilder.layer(api).pipe(Layer.provide(Layer.succeedContext(context)));
const handler = yield* HttpRouter.toHttpEffect(layer);
const httpClient = HttpClient.make(Effect.fnUntraced(function* (request) {
const serverRequest = HttpServerRequest.fromClientRequest(request);
const response = yield* handler.pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, serverRequest), Effect.orDie);
return HttpServerResponse.toClientResponse(response);
}, Effect.scoped));
return yield* HttpApiClient.makeWith(api, {
httpClient,
baseUrl: options?.baseUrl ?? "http://localhost:3000"
});
});
//# sourceMappingURL=HttpApiTest.js.map

Xet Storage Details

Size:
2.69 kB
·
Xet hash:
98b15f5e1e5cff900de122154879f07c2e392c002e7d10d78a8fc217fd448275

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.