/** * @since 4.0.0 */ import type { NonEmptyReadonlyArray } from "../../Array.ts" import * as Context from "../../Context.ts" import { type Pipeable, pipeArguments } from "../../Pipeable.ts" import * as Predicate from "../../Predicate.ts" import * as Record from "../../Record.ts" import type { PathInput } from "../http/HttpRouter.ts" import type * as HttpApiEndpoint from "./HttpApiEndpoint.ts" import type * as HttpApiMiddleware from "./HttpApiMiddleware.ts" const TypeId = "~effect/httpapi/HttpApiGroup" /** * @since 4.0.0 * @category guards */ export const isHttpApiGroup = (u: unknown): u is Any => Predicate.hasProperty(u, TypeId) /** * An `HttpApiGroup` is a collection of `HttpApiEndpoint`s. You can use an `HttpApiGroup` to * represent a portion of your domain. * * The endpoints can be implemented later using the `HttpApiBuilder.group` api. * * @since 4.0.0 * @category models */ export interface HttpApiGroup< out Id extends string, out Endpoints extends HttpApiEndpoint.Any = never, out TopLevel extends boolean = false > extends Pipeable { new(_: never): {} readonly [TypeId]: typeof TypeId readonly identifier: Id readonly key: string readonly topLevel: TopLevel readonly endpoints: Record.ReadonlyRecord readonly annotations: Context.Context /** * Add an `HttpApiEndpoint` to an `HttpApiGroup`. */ add>( ...endpoints: A ): HttpApiGroup /** * Add a path prefix to all endpoints in an `HttpApiGroup`. Note that this will only * add the prefix to the endpoints before this api is called. */ prefix( prefix: Prefix ): HttpApiGroup, TopLevel> /** * Add an `HttpApiMiddleware` to the `HttpApiGroup`. * * Endpoints added after this api is called **will not** have the middleware * applied. */ middleware(middleware: Context.Key): HttpApiGroup< Id, HttpApiEndpoint.AddMiddleware, TopLevel > /** * Merge the annotations of an `HttpApiGroup` with the provided annotations. */ annotateMerge(annotations: Context.Context): HttpApiGroup /** * Add an annotation to an `HttpApiGroup`. */ annotate(key: Context.Key, value: S): HttpApiGroup /** * For each endpoint in an `HttpApiGroup`, update the annotations with a new * Context. * * Note that this will only update the annotations before this api is called. */ annotateEndpointsMerge(annotations: Context.Context): HttpApiGroup /** * For each endpoint in an `HttpApiGroup`, add an annotation. * * Note that this will only add the annotation to the endpoints before this api * is called. */ annotateEndpoints(key: Context.Key, value: S): HttpApiGroup } /** * @since 4.0.0 * @category models */ export interface ApiGroup { readonly _: unique symbol readonly apiId: ApiId readonly name: Name } /** * @since 4.0.0 * @category models */ export interface Any { readonly [TypeId]: typeof TypeId readonly identifier: string readonly key: string readonly endpoints: Record.ReadonlyRecord } /** * @since 4.0.0 * @category models */ export type AnyWithProps = HttpApiGroup /** * @since 4.0.0 * @category models */ export type ToService = A extends HttpApiGroup ? ApiGroup : never /** * @since 4.0.0 * @category models */ export type WithName = Extract /** * @since 4.0.0 * @category models */ export type Name = Group extends HttpApiGroup ? _Name : never /** * @since 4.0.0 * @category models */ export type Endpoints = Group extends HttpApiGroup ? _Endpoints : never /** * @since 4.0.0 * @category models */ export type ErrorServicesEncode = HttpApiEndpoint.ErrorServicesEncode> /** * @since 4.0.0 * @category models */ export type ErrorServicesDecode = HttpApiEndpoint.ErrorServicesDecode> /** * @since 4.0.0 * @category models */ export type MiddlewareError = HttpApiEndpoint.MiddlewareError> /** * @since 4.0.0 * @category models */ export type MiddlewareProvides = HttpApiEndpoint.MiddlewareProvides> /** * @since 4.0.0 * @category models */ export type MiddlewareClient = HttpApiEndpoint.MiddlewareClient> /** * @since 4.0.0 * @category models */ export type MiddlewareServices = HttpApiEndpoint.MiddlewareServices> /** * @since 4.0.0 * @category models */ export type EndpointsWithName = Endpoints> /** * @since 4.0.0 * @category models */ export type ClientServices = Group extends HttpApiGroup ? HttpApiEndpoint.ClientServices<_Endpoints> : never /** * @since 4.0.0 * @category models */ export type AddPrefix = Group extends HttpApiGroup ? HttpApiGroup<_Name, HttpApiEndpoint.AddPrefix<_Endpoints, Prefix>, _TopLevel> : never /** * @since 4.0.0 * @category models */ export type AddMiddleware = Group extends HttpApiGroup ? HttpApiGroup<_Name, HttpApiEndpoint.AddMiddleware<_Endpoints, Id>, _TopLevel> : never const Proto = { [TypeId]: TypeId, add(this: AnyWithProps, ...toAdd: NonEmptyReadonlyArray) { const endpoints = { ...this.endpoints } for (const endpoint of toAdd) { endpoints[endpoint.name] = endpoint } return makeProto({ identifier: this.identifier, topLevel: this.topLevel, endpoints, annotations: this.annotations }) }, prefix(this: AnyWithProps, prefix: PathInput) { return makeProto({ identifier: this.identifier, topLevel: this.topLevel, endpoints: Record.map(this.endpoints, (endpoint) => endpoint.prefix(prefix)), annotations: this.annotations }) }, middleware(this: AnyWithProps, middleware: HttpApiMiddleware.AnyService) { return makeProto({ identifier: this.identifier, topLevel: this.topLevel, endpoints: Record.map(this.endpoints, (endpoint) => endpoint.middleware(middleware as any)), annotations: this.annotations }) }, annotateMerge(this: AnyWithProps, annotations: Context.Context) { return makeProto({ identifier: this.identifier, topLevel: this.topLevel, endpoints: this.endpoints, annotations: Context.merge(this.annotations, annotations) }) }, annotate(this: AnyWithProps, annotation: Context.Key, value: S) { return makeProto({ identifier: this.identifier, topLevel: this.topLevel, endpoints: this.endpoints, annotations: Context.add(this.annotations, annotation, value) }) }, annotateEndpointsMerge(this: AnyWithProps, annotations: Context.Context) { return makeProto({ identifier: this.identifier, topLevel: this.topLevel, endpoints: Record.map(this.endpoints, (endpoint) => endpoint.annotateMerge(annotations)), annotations: this.annotations }) }, annotateEndpoints(this: AnyWithProps, annotation: Context.Key, value: S) { return makeProto({ identifier: this.identifier, topLevel: this.topLevel, endpoints: Record.map(this.endpoints, (endpoint) => endpoint.annotate(annotation, value)), annotations: this.annotations }) }, pipe() { return pipeArguments(this, arguments) } } const makeProto = < Id extends string, Endpoints extends HttpApiEndpoint.Any, TopLevel extends (true | false) >(options: { readonly identifier: Id readonly topLevel: TopLevel readonly endpoints: Record.ReadonlyRecord readonly annotations: Context.Context }): HttpApiGroup => { function HttpApiGroup() {} Object.setPrototypeOf(HttpApiGroup, Proto) HttpApiGroup.key = `effect/httpapi/HttpApiGroup/${options.identifier}` return Object.assign(HttpApiGroup, options) as any } /** * An `HttpApiGroup` is a collection of `HttpApiEndpoint`s. You can use an `HttpApiGroup` to * represent a portion of your domain. * * The endpoints can be implemented later using the `HttpApiBuilder.group` api. * * @since 4.0.0 * @category constructors */ export const make = (identifier: Id, options?: { readonly topLevel?: TopLevel | undefined }): HttpApiGroup => makeProto({ identifier, topLevel: options?.topLevel ?? false as any, endpoints: Record.empty(), annotations: Context.empty() }) as any