rest-vir - v2.2.0
    Preparing search index...

    Class RestVirClient<ClientApi>

    Type Parameters

    Index

    Constructors

    Properties

    api: Readonly<ClientApi>
    baseUrl: string

    All route paths are joined to this URL.

    fetchOverride?: ClientFetch

    Optional fetch override to wrap or reimplement the native fetch function.

    webSocketConstructor?: new (
        ...params: WebSocketConnectWebSocketConstructorParams,
    ) => CommonWebSocket

    Optional WebSocket constructor used as the fallback when RestVirClient.connectWebSocket is called without an explicit webSocketConstructor param.

    Methods

    • Type Parameters

      • const Endpoint extends EndpointDefinition & { path: keyof ClientApi["endpoints"] }
      • const Method extends "GET" | "PUT" | "POST" | "DELETE" | "PATCH" | "TRACE"

      Parameters

      Returns { requestInit: RequestInit; url: string }

      Error if the given params are invalid for the given endpoint.

    • Return a method-keyed object for the given endpoint. Each property is a function that runs the fetch for that specific HTTP method.

      Type Parameters

      Parameters

      Returns {
          [Method in "GET" | "PUT" | "POST" | "DELETE" | "PATCH" | "TRACE"]: BivariantFunction<
              EndpointFetchParams<NoInfer<Endpoint>, Method>,
              Promise<
                  If<
                      IsAny<
                          DefinedEndpointFetchOutputs<Endpoint, Method, true> & {
                              unexpectedError: UnknownFetchOutput<true>;
                          },
                      >,
                      any,
                      If<
                          IsNever<
                              DefinedEndpointFetchOutputs<Endpoint, Method, true> & {
                                  unexpectedError: UnknownFetchOutput<true>;
                              },
                          >,
                          never,
                          If<
                              IsNever<
                                  | "unexpectedError"
                                  | keyof DefinedEndpointFetchOutputs<Endpoint, Method, true>,
                              >,
                              never,
                              _RequireExactlyOne<
                                  DefinedEndpointFetchOutputs<Endpoint, Method, true> & {
                                      unexpectedError: UnknownFetchOutput<true>;
                                  },
                                  If<
                                      IsAny<"unexpectedError" | keyof (...)>,

                                          | "unexpectedError"
                                          | keyof DefinedEndpointFetchOutputs<(...), (...), (...)>,

                                          | "unexpectedError"
                                          | keyof DefinedEndpointFetchOutputs<(...), (...), (...)>,
                                  >,
                              >,
                          >,
                      >,
                  >,
              >,
          >
      }

      const response = await client.fetch(healthEndpoint).GET();
      const created = await client.fetch(usersEndpoint).POST({requestData: ...});
    • Validate that the endpoint is registered, build its request init, send the request, and shape the response into the status-keyed output. The body of RestVirClient.fetch and RestVirClient.fetchStream; their only divergent step is how they read responseData out of the response.

      Type Parameters

      • const Endpoint extends EndpointDefinition & { path: keyof ClientApi["endpoints"] }
      • const Method extends "GET" | "PUT" | "POST" | "DELETE" | "PATCH" | "TRACE"

      Parameters

      • endpoint: Endpoint
      • method: Method
      • params:
            | {
                fetchOverride?: ClientFetch;
                options?: Omit<RequestInit, "body" | "method">;
                pathParams?: GenericPathParams;
                requestData?: any;
                requiredHeaders?: Record<string, string>;
                searchParams?: Readonly<Record<string, AllowedSearchParamValue>>;
                skipAutomaticContentTypeHeader?: boolean;
            }
            | undefined
        • {
              fetchOverride?: ClientFetch;
              options?: Omit<RequestInit, "body" | "method">;
              pathParams?: GenericPathParams;
              requestData?: any;
              requiredHeaders?: Record<string, string>;
              searchParams?: Readonly<Record<string, AllowedSearchParamValue>>;
              skipAutomaticContentTypeHeader?: boolean;
          }
          • OptionalfetchOverride?: ClientFetch

            Override the fetch function that is used when fetching the endpoint.

            fetch // the global fetch
            
          • Optionaloptions?: Omit<RequestInit, "body" | "method">

            Set the standard request init options that fetch allows.

          • OptionalpathParams?: GenericPathParams

            Set the required path params, if any. These are only allowed if the endpoint's path has param or wildcard strings.

          • OptionalrequestData?: any

            Set the endpoint fetch's request data, if allowed by the endpoint.

          • OptionalrequiredHeaders?: Record<string, string>

            Set headers that are required by this route's definition, if any. Note that additional headers can be supplied in the options property, but any headers provided in both will instead use the values from here here (in requiredHeaders).

          • OptionalsearchParams?: Readonly<Record<string, AllowedSearchParamValue>>

            Set search params on the endpoint's URL.

          • OptionalskipAutomaticContentTypeHeader?: boolean

            If true, automatic request data 'Content-Type' header assignment will be skipped.

        • undefined
      • OptionalgetResponseDataOverride: HandleDeclaredResponseStatusOverride

      Returns Promise<Record<string, UnknownFetchOutput>>