| import * as Context from "../../Context.js"; | |
| import { pipeArguments } from "../../Pipeable.js"; | |
| import * as Predicate from "../../Predicate.js"; | |
| import * as Record from "../../Record.js"; | |
| import * as HttpApiEndpoint from "./HttpApiEndpoint.js"; | |
| import * as HttpApiSchema from "./HttpApiSchema.js"; | |
| const TypeId = "~effect/httpapi/HttpApi"; | |
| /** | |
| * Returns `true` when a value is an `HttpApi`. | |
| * | |
| * @category guards | |
| * @since 4.0.0 | |
| */ | |
| export const isHttpApi = u => Predicate.hasProperty(u, TypeId); | |
| const Proto = { | |
| [TypeId]: TypeId, | |
| pipe() { | |
| return pipeArguments(this, arguments); | |
| }, | |
| add(...toAdd) { | |
| const groups = { | |
| ...this.groups | |
| }; | |
| for (const group of toAdd) { | |
| groups[group.identifier] = group; | |
| } | |
| return makeProto({ | |
| identifier: this.identifier, | |
| groups, | |
| annotations: this.annotations | |
| }); | |
| }, | |
| addHttpApi(api) { | |
| const newGroups = { | |
| ...this.groups | |
| }; | |
| for (const key in api.groups) { | |
| const newGroup = api.groups[key]; | |
| newGroup.annotations = Context.merge(api.annotations, newGroup.annotations); | |
| newGroups[key] = newGroup; | |
| } | |
| return makeProto({ | |
| identifier: this.identifier, | |
| groups: newGroups, | |
| annotations: this.annotations | |
| }); | |
| }, | |
| prefix(prefix) { | |
| return makeProto({ | |
| identifier: this.identifier, | |
| groups: Record.map(this.groups, group => group.prefix(prefix)), | |
| annotations: this.annotations | |
| }); | |
| }, | |
| middleware(tag) { | |
| return makeProto({ | |
| identifier: this.identifier, | |
| groups: Record.map(this.groups, group => group.middleware(tag)), | |
| annotations: this.annotations | |
| }); | |
| }, | |
| annotate(key, value) { | |
| return makeProto({ | |
| identifier: this.identifier, | |
| groups: this.groups, | |
| annotations: Context.add(this.annotations, key, value) | |
| }); | |
| }, | |
| annotateMerge(annotations) { | |
| return makeProto({ | |
| identifier: this.identifier, | |
| groups: this.groups, | |
| annotations: Context.merge(this.annotations, annotations) | |
| }); | |
| } | |
| }; | |
| const makeProto = options => { | |
| function HttpApi() {} | |
| Object.setPrototypeOf(HttpApi, Proto); | |
| HttpApi.groups = options.groups; | |
| HttpApi.annotations = options.annotations; | |
| return HttpApi; | |
| }; | |
| /** | |
| * Creates an empty `HttpApi` with the supplied identifier. | |
| * | |
| * **When to use** | |
| * | |
| * Use when you need to start defining an HTTP API, add groups with `add` or | |
| * `addHttpApi`, provide endpoint implementations with `HttpApiBuilder.group`, | |
| * and register the API with `HttpApiBuilder.layer`. | |
| * | |
| * @category constructors | |
| * @since 4.0.0 | |
| */ | |
| export const make = identifier => makeProto({ | |
| identifier, | |
| groups: new Map(), | |
| annotations: Context.empty() | |
| }); | |
| /** | |
| * Describes the groups and endpoints in an `HttpApi`. | |
| * | |
| * **Details** | |
| * | |
| * The callbacks receive each group or endpoint with merged annotations, endpoint | |
| * middleware, and response schemas grouped by HTTP status. | |
| * | |
| * @category reflection | |
| * @since 4.0.0 | |
| */ | |
| export const reflect = (self, options) => { | |
| const groups = Object.values(self.groups); | |
| for (const group of groups) { | |
| const groupAnnotations = Context.merge(self.annotations, group.annotations); | |
| options.onGroup({ | |
| group, | |
| mergedAnnotations: groupAnnotations | |
| }); | |
| const endpoints = Object.values(group.endpoints); | |
| for (const endpoint of endpoints) { | |
| if (options.predicate && !options.predicate({ | |
| endpoint, | |
| group | |
| })) continue; | |
| options.onEndpoint({ | |
| group, | |
| endpoint, | |
| middleware: endpoint.middlewares, | |
| mergedAnnotations: Context.merge(groupAnnotations, endpoint.annotations), | |
| successes: extractResponseContent(HttpApiEndpoint.getSuccessSchemas(endpoint), HttpApiSchema.getStatusSuccess), | |
| errors: extractResponseContent(HttpApiEndpoint.getErrorSchemas(endpoint), HttpApiSchema.getStatusError) | |
| }); | |
| } | |
| } | |
| }; | |
| // ------------------------------------------------------------------------------------- | |
| const extractResponseContent = (schemas, getStatus) => { | |
| const map = new Map(); | |
| schemas.forEach(add); | |
| return map; | |
| function add(schema) { | |
| if (HttpApiSchema.isStreamSchema(schema)) return; | |
| const ast = schema.ast; | |
| const status = getStatus(ast); | |
| const schemas = map.get(status); | |
| if (schemas === undefined) { | |
| map.set(status, [schema]); | |
| } else { | |
| schemas.push(schema); | |
| } | |
| } | |
| }; | |
| /** | |
| * Adds additional schemas to components/schemas. | |
| * The provided schemas must have a `identifier` annotation. | |
| * | |
| * @category services | |
| * @since 4.0.0 | |
| */ | |
| export class AdditionalSchemas extends /*#__PURE__*/Context.Service()("effect/httpapi/HttpApi/AdditionalSchemas") {} | |
| //# sourceMappingURL=HttpApi.js.map |
Xet Storage Details
- Size:
- 4.71 kB
- Xet hash:
- f1383d4a48b1f888a5a8889fb835b2c8e9cb04299ad396ed8c09d40169e8f7a4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.