EdgeAIG/opencode / .opencode /node_modules /effect /dist /unstable /http /HttpServerRespondable.js
EdgeAIG's picture
download
raw
3.04 kB
/**
* Converts supported values into HTTP server responses.
*
* Server-side errors and helper values can implement `Respondable` when they
* know which status, headers, cookies, or body should be sent to the client.
* This module detects those values and converts them to `HttpServerResponse`
* values, with fallback handling for schema errors and missing values.
*
* @since 4.0.0
*/
import * as Cause from "../../Cause.js";
import * as Effect from "../../Effect.js";
import { hasProperty } from "../../Predicate.js";
import * as Schema from "../../Schema.js";
import * as Response from "./HttpServerResponse.js";
/**
* Protocol key used by values that can render themselves as
* `HttpServerResponse` values.
*
* @category type IDs
* @since 4.0.0
*/
export const symbol = "~effect/http/HttpServerRespondable";
/**
* Returns `true` when the supplied value implements the `Respondable` protocol.
*
* @category guards
* @since 4.0.0
*/
export const isRespondable = u => hasProperty(u, symbol);
const badRequest = /*#__PURE__*/Response.empty({
status: 400
});
const notFound = /*#__PURE__*/Response.empty({
status: 404
});
/**
* Converts a `Respondable` value into an `HttpServerResponse`.
*
* **Details**
*
* If the value is already an HTTP server response it is returned directly; errors
* from the response conversion are converted to defects.
*
* @category accessors
* @since 4.0.0
*/
export const toResponse = self => {
if (Response.isHttpServerResponse(self)) {
return Effect.succeed(self);
}
return Effect.orDie(self[symbol]());
};
/**
* Attempts to convert an unknown value into an `HttpServerResponse`, falling back
* to the supplied response when no conversion is available.
*
* **Details**
*
* `HttpServerResponse` and `Respondable` values are used directly, schema errors
* become `400` responses, and no-such-element errors become `404` responses.
*
* @category accessors
* @since 4.0.0
*/
export const toResponseOrElse = (u, orElse) => {
if (Response.isHttpServerResponse(u)) {
return Effect.succeed(u);
} else if (isRespondable(u)) {
return Effect.catchCause(u[symbol](), () => Effect.succeed(orElse));
// add support for some commmon types
} else if (Schema.isSchemaError(u)) {
return Effect.succeed(badRequest);
} else if (Cause.isNoSuchElementError(u)) {
return Effect.succeed(notFound);
}
return Effect.succeed(orElse);
};
/**
* Attempts to convert an unknown defect into an `HttpServerResponse`, falling
* back to the supplied response when no conversion is available.
*
* **Details**
*
* Only `HttpServerResponse` and `Respondable` values receive special handling.
*
* @category accessors
* @since 4.0.0
*/
export const toResponseOrElseDefect = (u, orElse) => {
if (Response.isHttpServerResponse(u)) {
return Effect.succeed(u);
} else if (isRespondable(u)) {
return Effect.catchCause(u[symbol](), () => Effect.succeed(orElse));
}
return Effect.succeed(orElse);
};
//# sourceMappingURL=HttpServerRespondable.js.map

Xet Storage Details

Size:
3.04 kB
·
Xet hash:
a979da58f6b30bda2a9aaa4096f073c7d94348ca10c3772d3c082166a5fc8133

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