import { TokenCache } from './authentication'; /** * Response from OAuth token endpoint. */ export interface AccessTokenResponse { accessToken: string; expiresIn: number; } /** * Configuration of an API to send queries to. Used to instantiate the Api class. */ export interface ApiConfig { /** * URL to the Spinque Query API deployment. * * @default https://rest.spinque.com/ */ baseUrl?: string; /** * Version of the Spinque Query API deployment. * * @default 4 */ version?: string; /** * Name of the Spinque workspace that should be addressed. * The Spinque Desk administrator working on your project knowns this value. */ workspace?: string; /** * Name of the API that is provided by the workspace. * The Spinque Desk administrator working on your project knowns this value. */ api?: string; /** * Name of the configuration of the Spinque workspace that should be used. * Usually, this is something like 'production', 'development' or 'default'. * The Spinque Desk administrator working on your project knowns this value. * * @default default */ config?: string; /** * Authentication that should be used when querying the API. */ authentication?: ApiAuthenticationConfig; } /** * Authentication configuration for an API. */ export type ApiAuthenticationConfig = { /** * OAuth 2.0 Client Credentials flow. * Uses Client ID and Client Secret to directly request an access token. * * @see https://oauth.net/2/grant-types/client-credentials/ */ type: 'client-credentials'; /** * URL to the authentication server to use. * * @default https://login.spinque.com/ */ authServer?: string; clientId: string; clientSecret: string; /** * Implementation of a TokenCache. Defines a get and set method that put the token in some sort of storage. * This is especially useful during development, when in-memory caching doesn't work due to frequent server restarts. */ tokenCache?: TokenCache; } | { /** * OAuth 2.0 PKCE flow. * * @see https://oauth.net/2/pkce/ */ type: 'pkce'; /** * URL to the authentication server to use. * * @default https://login.spinque.com/ */ authServer?: string; clientId: string; callback: string; /** * Implementation of a TokenCache. Defines a get and set method that put the token in some sort of storage. * This is especially useful during development, when in-memory caching doesn't work due to frequent server restarts. */ tokenCache?: TokenCache; }; /** * Represent a single query, consisting of an endpoint name and a map of parameters (name and value). */ export interface Query { /** * Name of the endpoint. */ endpoint: string; /** * Map of filled parameters (name and value). */ parameters?: { [name: string]: string; }; } /** * Any request to the Spinque Query API must be one of these types. */ export declare enum RequestType { /** * Request a count of the results */ Count = "count", /** * Request statistics, which includes result count and probability distribution */ Statistics = "statistics", /** * Fetch a page with results, using the entity view formatter */ ResultPage = "resultpage", /** * Fetch a single item, using the entity view formatter */ ResultItem = "resultitem", /** * Fetch a page with results, using the result description formatter */ Results = "results", /** * Fetch a page with results, using the result description formatter, and include the total count */ ResultsAndCount = "results,count", /** * Fetch facet options for facets following the :FILTER convention */ Options = "options" } export interface CountAndOffset { count?: number; offset?: number; } export type ResultsRequestOptions = CountAndOffset & { format?: 'json' | 'xml' | 'rdf' | 'csv' | 'xlsx'; homogeneousArrays?: boolean; }; export type ResultItemRequestOptions = { rank?: number; column?: number; multiselect?: boolean; }; export type OptionsRequestOptions = CountAndOffset & { multiselect?: boolean; }; /** * Map from RequestType to a Response type */ type OptionsMap = { [RequestType.Count]: CountAndOffset; [RequestType.Statistics]: CountAndOffset; [RequestType.ResultPage]: CountAndOffset; [RequestType.ResultItem]: ResultItemRequestOptions; [RequestType.Results]: ResultsRequestOptions; [RequestType.ResultsAndCount]: ResultsRequestOptions; [RequestType.Options]: OptionsRequestOptions; }; /** * ResponseType based on RequestType */ export type OptionsType = U extends keyof ResponseMap ? OptionsMap[U] : never; /** * Map from RequestType to a Response type */ type ResponseMap = { [RequestType.Count]: CountResponse; [RequestType.Statistics]: StatisticsResponse; [RequestType.ResultPage]: ResultsResponse; [RequestType.ResultItem]: ResultObject; [RequestType.Results]: ResultsResponse; [RequestType.ResultsAndCount]: ResultsAndCountResponse; [RequestType.Options]: OptionsResponse; }; export type ResponseTypes = ResponseMap[keyof ResponseMap]; /** * ResponseType based on RequestType */ export type ResponseType = U extends keyof ResponseMap ? ResponseMap[U] : never; /** * Data types known by Spinque. * OBJ is an object with attributes. * TUPLE_LIST is a list of tuples. * STRING, DATE, INTEGER, DOUBLE are simple data types. */ export type DataType = 'OBJ' | 'STRING' | 'DATE' | 'INTEGER' | 'DOUBLE' | 'TUPLE_LIST'; export type TupleTypes = string | number | SpinqueResultObject | ResultObject; /** * Response to a Query that contains the results */ export interface ResultsResponse { count: number; offset: number; type: DataType[]; items: ResultItem[]; } export interface ResultItem { rank: number; probability: number; tuple: T; } /** * Output format of Spinque's OBJ type, using the result description formatter */ export interface SpinqueResultObject { /** * The unique identifier of the object. * Will always be a valid URI. */ id: string; /** * Array of classes assigned to this object. * * If a value is in this list, then there is * a * relation between that value and `id`. */ class: string[]; /** * Object with attribute names and values. * Values can be any valid JSON type. */ attributes?: Record; } /** * Result object using entity view formatter */ export type ResultObject = { [key: string]: string | number | ResultObject; }; /** * Response to a Query that contains statistics */ export interface StatisticsResponse { total: number; stats: { cutoff: string; numResults: number; }[]; } /** * Response to a Query that contains the count */ export interface CountResponse { total: number; } export type ResultsAndCountResponse = [ResultsResponse, StatisticsResponse]; /** * Response to a Query for facet options */ export interface OptionsResponse { /** * Name of the facet options endpoint */ id: string; title: string; selected: { id: string; score: number; value: string; title: string; }[]; options: { id: string; score: number; value: string; title: string; }[]; } /** * Generic error response class. Is implemented by more specific error type classes. */ export declare class ErrorResponse { message: string; status: number; constructor(message: string, status: number); } /** * Error class used when Spinque cannot find the workspace configuration you requested. * The workspace or configuration might be misspelled or removed. */ export declare class WorkspaceConfigNotFoundError implements ErrorResponse { message: string; status: number; constructor(message: string, status: number); } /** * Error class used when Spinque cannot find the API you requested. * The API might be misspelled or removed. */ export declare class ApiNotFoundError implements ErrorResponse { message: string; status: number; constructor(message: string, status: number); } /** * Error class used when Spinque cannot find the endpoint you requested. * The endpoint might be misspelled or removed. */ export declare class EndpointNotFoundError implements ErrorResponse { message: string; status: number; constructor(message: string, status: number); } /** * Error class used when you are not authorized to request results for * this workspace, API or endpoint. */ export declare class UnauthorizedError implements ErrorResponse { message: string; status: number; constructor(message: string, status: number); } /** * Error class when something fails on the side of Spinque. Please contact * your system administrator when this happens. */ export declare class ServerError implements ErrorResponse { message: string; status: number; constructor(message: string, status: number); } export type Filter = FacetFilter | ParameterizedFilter | SimpleFilter; export declare enum FacetType { single = "single", multiple = "multiple" } /** * Endpoints to a) retrieve filter options given a result query stack, * and b) filter results based on a selection of options. * * Currently, this interface only supports: * - facets without a parameter for the optionsEndpoint * - facets with only one parameter for the filterEndpoint * - for facets with type=multiple: only disjunctive facets */ export interface FacetFilter { optionsEndpoint: string; filterEndpoint: string; filterParameterName: string; filterParameterValue: string | number | undefined; type: FacetType; resetOnQueryChange: boolean; } /** * Endpoint that filters results based on the value of its parameter. */ export interface ParameterizedFilter { filterEndpoint: string; filterParameterName: string; filterParameterValue: string | number | undefined; resetOnQueryChange: boolean; } /** * Endpoint that filters results. */ export interface SimpleFilter { filterEndpoint: string; } /** * Type guard for ResultsResponse */ export declare function isResultsResponse(json: unknown): json is ResultsResponse; /** * Type guard for CountResponse */ export declare function isCountResponse(json: unknown): json is CountResponse; /** * Type guard for StatisticsResponse */ export declare function isStatisticsResponse(json: unknown): json is StatisticsResponse; /** * Type guard for OptionsResponse */ export declare function isOptionsResponse(json: unknown): json is OptionsResponse; /** * Type guard for OAuth token response from the authorization server */ export declare function isOAuthTokenResponse(json: unknown): json is { access_token: string; expires_in: number; }; export {};