Libraries v5.2.0
    Preparing search index...

    Class EndpointBuilder<TParams, TBody, TQuery, THeaders, TServices, TPrincipal, TRoles, TResponse, TResponses, TUpload>

    Type Parameters

    • TParams = {}
    • TBody = undefined
    • TQuery = {}
    • THeaders = {}
    • TServices = {}
    • TPrincipal = undefined
    • TRoles extends string = string
    • TResponse = any
    • TResponses extends Record<number, any> = {}
    • TUpload extends UploadContract = false
    Index
    • Type Parameters

      • TParams = {}
      • TBody = undefined
      • TQuery = {}
      • THeaders = {}
      • TServices = {}
      • TPrincipal = undefined
      • TRoles extends string = string
      • TResponse = any
      • TResponses extends Record<number, any> = {}
      • TUpload extends UploadContract = false

      Parameters

      • method: string
      • basePath: string
      • pathTemplate: RoutePath
      • bodySchema: SchemaBuilder<any, any, any, any, any> | null
      • querySchema: ObjectSchemaBuilder<any, any, any, any, any, any, any> | null
      • headerSchema: ObjectSchemaBuilder<any, any, any, any, any, any, any> | null
      • serviceSchemas: Record<string, SchemaBuilder<any, any, any, any, any>> | null = null
      • authRoles: readonly string[] | null = null
      • summary: string | null = null
      • description: string | null = null
      • tags: readonly string[] = []
      • operationId: string | null = null
      • deprecated: boolean = false
      • responseSchema: SchemaBuilder<any, any, any, any, any> | null = null
      • responsesSchemas: Record<number, SchemaBuilder<any, any, any, any, any> | null> | null = null
      • example: unknown = null
      • examples:
            | Record<
                string,
                { description?: string; summary?: string; value: unknown },
            >
            | null = null
      • producesFile: { contentType?: string; description?: string } | null = null
      • produces: Record<string, { schema?: SchemaBuilder<any, any, any, any, any> }> | null = null
      • responseHeaderSchema: ObjectSchemaBuilder<any, any, any, any, any, any, any> | null = null
      • externalDocs: { description?: string; url: string } | null = null
      • links: Record<string, LinkDefinition<any>> | null = null
      • callbacks: Record<string, CallbackDefinition<any>> | null = null
      • fileUpload: UploadConfiguration | null = null
      • cacheTags: readonly CacheTagDefinition[] = []
      • idempotent: boolean = false

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          TResponses,
          TUpload,
      >

    • get path(): string

      The full path for this endpoint, combining basePath and pathTemplate.

      For static routes this is the exact URL path (e.g. "/todos"). For dynamic routes the template placeholders are included (e.g. "/todos/:id").

      Returns string

    • Declare a cache group for this endpoint.

      Use on GET / query endpoints to group responses into a named cache. The client-side cacheTags middleware caches responses keyed by this tag and flushes matching entries when a mutation calls clearsCacheTag.

      Simple tag (no properties — single cache entry). Tag with property descriptors for fine-grained keys.

      Parameters

      • name: string

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          TResponses,
      >

      // GET — responses cached under "todo" group, keyed by id
      endpoint.get('/api/todos/:id')
      .cacheTag('todo', p => ({
      id: p.params.id
      }))
    • Declare a cache group for this endpoint.

      Use on GET / query endpoints to group responses into a named cache. The client-side cacheTags middleware caches responses keyed by this tag and flushes matching entries when a mutation calls clearsCacheTag.

      Simple tag (no properties — single cache entry). Tag with property descriptors for fine-grained keys.

      Parameters

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          TResponses,
      >

      // GET — responses cached under "todo" group, keyed by id
      endpoint.get('/api/todos/:id')
      .cacheTag('todo', p => ({
      id: p.params.id
      }))
    • Declare which cache groups are cleared when this mutation succeeds.

      Use on POST / PUT / PATCH / DELETE endpoints. When the mutation completes, the cacheTags client middleware invalidates all cache entries matching the declared tag names (prefix match).

      Simple tag (clears all entries prefixed with the name). Tag with property descriptors for targeted invalidation.

      Parameters

      • name: string

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          TResponses,
      >

      // PATCH — clears "todo-list" / "todo" names and their computed ct2 keys
      endpoint.patch('/api/todos/:id')
      .clearsCacheTag('todo-list')
      .clearsCacheTag('todo', p => ({
      id: p.params.id
      }))
    • Declare which cache groups are cleared when this mutation succeeds.

      Use on POST / PUT / PATCH / DELETE endpoints. When the mutation completes, the cacheTags client middleware invalidates all cache entries matching the declared tag names (prefix match).

      Simple tag (clears all entries prefixed with the name). Tag with property descriptors for targeted invalidation.

      Parameters

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          TResponses,
      >

      // PATCH — clears "todo-list" / "todo" names and their computed ct2 keys
      endpoint.patch('/api/todos/:id')
      .clearsCacheTag('todo-list')
      .clearsCacheTag('todo', p => ({
      id: p.params.id
      }))
    • Declare that this endpoint can produce responses in multiple content types.

      Each key is a MIME type. Provide an optional schema to override the default response schema for that type; otherwise the schema from .returns() / .responses() is reused for every declared content type.

      When used alongside .producesFile(), the binary response takes precedence.

      Parameters

      • contentTypes: Record<string, { schema?: SchemaBuilder<any, any, any, any, any> }>

        Map of MIME type → optional schema override.

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          TResponses,
          TUpload,
      >

      endpoint.get('/api/items')
      .returns(object({ id: number(), name: string() }))
      .produces({
      'text/csv': {},
      'application/xml': { schema: string() }
      })
    • Declare per-status-code response schemas for OpenAPI generation and handler return-type enforcement.

      Pass null as the schema for body-less codes (e.g. 204).

      Type Parameters

      • const T extends Record<number, SchemaBuilder<any, any, any, any, any> | null>

      Parameters

      • map: T

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          InferResponsesMap<T>,
          TUpload,
      >

      const ep = endpoint
      .get('/api/todos/:id')
      .responses({
      200: object({ id: number(), title: string() }),
      404: object({ message: string() }),
      });

      const handler: Handler<typeof ep> = ({ params }) => {
      const todo = todos.get(params.id);
      if (!todo) return ActionResult.notFound({ message: 'Not found' });
      return todo; // plain object → 200
      };
    • Mark this endpoint as accepting multipart/form-data file uploads.

      When set, the server parses the request body with a streaming multipart parser instead of the default JSON deserializer. File fields are made available via arg.files. A schema of file() / array(file()) fields infers their names, cardinality and optionality. Without a schema, files remain a Record<string, FilePart>. Non-file fields use arg.body. File-only endpoints do not require a body schema. Typed uploads fail before the handler if their contract is invalid; limits produce 413.

      Type Parameters

      Parameters

      • schema: S
      • Optionaloptions: UploadOptions

        Upload configuration (max file size, allowed MIME types, etc.).

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          TResponses,
          S,
      >

      const UploadAvatar = endpoint
      .post('/api/avatar')
      .upload({ maxFileSize: 2 * 1024 * 1024, allowedMimeTypes: ['image/*'] })
      .authorize(PrincipalSchema)
      .responses({ 200: AvatarSchema });

      const handler: Handler<typeof UploadAvatar> = async ({ files }) => {
      const avatar = files['avatar'];
      // avatar: { filename, mimeType, buffer, size }
      };
    • Mark this endpoint as accepting multipart/form-data file uploads.

      When set, the server parses the request body with a streaming multipart parser instead of the default JSON deserializer. File fields are made available via arg.files. A schema of file() / array(file()) fields infers their names, cardinality and optionality. Without a schema, files remain a Record<string, FilePart>. Non-file fields use arg.body. File-only endpoints do not require a body schema. Typed uploads fail before the handler if their contract is invalid; limits produce 413.

      Parameters

      • Optionaloptions: UploadOptions

        Upload configuration (max file size, allowed MIME types, etc.).

      Returns EndpointBuilder<
          TParams,
          TBody,
          TQuery,
          THeaders,
          TServices,
          TPrincipal,
          TRoles,
          TResponse,
          TResponses,
          true,
      >

      const UploadAvatar = endpoint
      .post('/api/avatar')
      .upload({ maxFileSize: 2 * 1024 * 1024, allowedMimeTypes: ['image/*'] })
      .authorize(PrincipalSchema)
      .responses({ 200: AvatarSchema });

      const handler: Handler<typeof UploadAvatar> = async ({ files }) => {
      const avatar = files['avatar'];
      // avatar: { filename, mimeType, buffer, size }
      };