export type HttpVerb = 'get' | 'post' | 'put' | 'patch' | 'delete'; export type RelLinks = { first?: string; prev?: string; next?: string; last?: string; alternate?: string; }; export interface ResourceSelection { library?: string; // e.g. "u123" or "g456" collections?: string | null; publications?: null; items?: string | null; searches?: string | null; top?: null; trash?: null; tags?: string | null; children?: null; groups?: null; subcollections?: null; itemTypes?: null; itemFields?: null; schema?: null; creatorFields?: null; itemTypeFields?: null; itemTypeCreatorTypes?: null; template?: null; file?: null; fileUrl?: null; settings?: string | null; deleted?: null; fulltext?: null; fulltextIndex?: null; verifyKeyAccess?: null; local?: null; // flags a local API request, required by local API-only endpoints; defaults the address to http://localhost:23119/api/ authorize?: null; // local API only, requires local } export interface RequestOptions { // API endpoint configuration apiScheme?: string; // default: https apiAuthorityPart?: string; // default: api.zotero.org apiPath?: string; // default: '' // Headers authorization?: string; zoteroApiKey?: string; zoteroApiVersion?: number; // 'Zotero-API-Version' header. Optional; API defaults to 3 when omitted zoteroServerID?: string; // 'Zotero-Server-ID' header. Optional; used with the local API only zoteroAllowedRequest?: string; // 'Zotero-Allowed-Request' header. Optional; local API only, required with a browser-like user agent zoteroWriteToken?: string; zoteroSchemaVersion?: number; ifModifiedSinceVersion?: number; ifUnmodifiedSinceVersion?: number; ifNoneMatch?: string; contentType?: string | null; // Query params annotationType?: string; collectionKey?: string; content?: string; direction?: string; format?: string | null; include?: string; includeTrashed?: string | boolean; itemKey?: string | string[]; itemQ?: string; itemQMode?: string; itemTag?: string | string[]; itemType?: string; limit?: number | string; linkMode?: string; linkwrap?: string; locale?: string; q?: string; qmode?: string; searchKey?: string; since?: number | string; sort?: string; start?: number | string; style?: string; tag?: string | string[]; tagType?: number | string; // 0 for manual tags, 1 for automatic tags // Execution / fetch method?: HttpVerb | string; body?: any; appName?: string; // local API only: used to construct the authorize request body; incompatible with body mode?: string; cache?: string; credentials?: string; integrity?: string; keepalive?: boolean; priority?: string; redirect?: string; referrer?: string; referrerPolicy?: string; signal?: AbortSignal | any; // Retry retry?: number; retryDelay?: number | null; retryCount?: number; // Advanced pretend?: boolean; skipValidation?: boolean; // skip endpoint-combination validation (chain only; request() is never validated) resource?: ResourceSelection; executors?: Array<(config: any) => Promise | any>; // Version. Converted to a relevant header depending on request type version?: number; // File upload / patch fileName?: string; file?: ArrayBuffer; fileSize?: number; mtime?: number | null; md5sum?: string; filePatch?: BufferSource; algorithm?: 'xdelta' | 'vcdiff' | 'bsdiff'; uploadRegisterOnly?: boolean | null; zipMD5?: string; zipFilename?: string; } export class ApiResponse { constructor(data: TData, options: RequestOptions, response: any); raw: TData; options: RequestOptions; response: any; getResponseType(): string; getData(): TData; getLinks(): any; getMeta(): any; getVersion(): number | null; getServerID(): string | null; } export class SchemaResponse extends ApiResponse { getResponseType(): 'SchemaResponse'; getVersion(): number; getMeta(): null; } export type FullTextIndexStatusType = 'indexed' | 'incomplete' | 'reindexing' | 'deindexed'; export interface FullTextIndexStatus { status: FullTextIndexStatusType; indexedCount?: number; expectedCount?: number; } export class FullTextStatusResponse extends ApiResponse { getResponseType(): 'FullTextStatusResponse'; getStatus(): FullTextIndexStatusType | null; getIndexedCount(): number | null; getExpectedCount(): number | null; } export class SingleReadResponse extends ApiResponse { getResponseType(): 'SingleReadResponse'; getData(): TData; } export class MultiReadResponse extends ApiResponse { getResponseType(): 'MultiReadResponse'; getData(): TData[]; getLinks(): Array; getMeta(): Array; getTotalResults(): number | null; getRelLinks(): RelLinks } export class SingleWriteResponse extends ApiResponse> { getResponseType(): 'SingleWriteResponse'; getData(): Required & { version: number | null }; } export class MultiWriteResponse extends ApiResponse { getResponseType(): 'MultiWriteResponse'; isSuccess(): boolean; getData(): Array; getLinks(): Array; getMeta(): Array; getErrors(): Record; getEntityByKey(key: string): TItem; getEntityByIndex(index: number | string): TItem; } export class DeleteResponse extends ApiResponse { getResponseType(): 'DeleteResponse'; } export interface LocalAuthorizationData { key: string; remember: boolean; } export class AuthorizeResponse extends ApiResponse { getResponseType(): 'AuthorizeResponse'; getKey(): string | null; isRemembered(): boolean; } export class FileUploadResponse extends ApiResponse { uploadResponse?: any; registerResponse?: any; getResponseType(): 'FileUploadResponse'; getVersion(): number | null; } export class FileDownloadResponse extends ApiResponse { getResponseType(): 'FileDownloadResponse'; getData(): ArrayBuffer; } export class FileUrlResponse extends ApiResponse { getResponseType(): 'FileUrlResponse'; getData(): string; } export class RawApiResponse extends ApiResponse { constructor(rawResponse: any, options: RequestOptions); getResponseType(): 'RawApiResponse'; } export class PretendResponse extends ApiResponse<{ url: string; fetchConfig: any }> { getResponseType(): 'PretendResponse'; getVersion(): null; } export class ErrorResponse extends Error { constructor(message: string, reason: string, response: any, options: RequestOptions); response: any; reason: string; options: RequestOptions; getVersion(): number | null; getServerID(): string | null; getResponseType(): 'ErrorResponse'; } export type AnyResponse = | ApiResponse | SchemaResponse | FullTextStatusResponse | SingleReadResponse | MultiReadResponse | SingleWriteResponse | MultiWriteResponse | DeleteResponse | AuthorizeResponse | FileUploadResponse | FileDownloadResponse | FileUrlResponse | RawApiResponse | PretendResponse; // ------------------- API Chain ------------------- export interface ExtendArgs { config: RequestOptions; // The following are intentionally loose to keep extension typing flexible ef: (opts?: Partial) => ApiChain; efr: (resource?: Partial, opts?: Partial) => ApiChain; execute: (cfg: RequestOptions) => Promise; functions: Record; } export interface ApiChain { api(key?: string | null, opts?: Partial): ApiChain; // Resource configuration library(typeOrKey: 'user' | 'group', id: number): ApiChain; library(libraryKey: string): ApiChain; items(items?: string | null): ApiChain; itemTypes(): ApiChain; itemFields(): ApiChain; creatorFields(): ApiChain; schema(): ApiChain; itemTypeFields(itemType: string): ApiChain; itemTypeCreatorTypes(itemType: string): ApiChain; template(itemType: string, subType?: string): ApiChain; collections(collections?: string | null): ApiChain; subcollections(): ApiChain; publications(): ApiChain; tags(tags?: string | null): ApiChain; searches(searches?: string | null): ApiChain; top(): ApiChain; trash(): ApiChain; children(): ApiChain; settings(settings?: string | null): ApiChain; deleted(since: number): ApiChain; groups(): ApiChain; fulltext(): ApiChain; fulltextStatus(): ApiChain; version(version: number): ApiChain; apiVersion(apiVersion: number): ApiChain; serverID(serverID: string): ApiChain; // Files attachment( fileName?: string, file?: ArrayBuffer, mtime?: number | null, md5sum?: string, patch?: BufferSource, algorithm?: 'xdelta' | 'vcdiff' | 'bsdiff' | string, zipFilename?: string ): ApiChain; registerAttachment( fileName: string, fileSize: number, mtime: number, md5sum: string, zipMD5?: string, zipFilename?: string ): ApiChain; attachmentUrl(): ApiChain; // Access/keys verifyKeyAccess(): ApiChain; local(): ApiChain; authorize(appName: string): ApiChain; // Execution get(opts?: Partial): Promise; post(data?: any, opts?: Partial): Promise; put(data: any, opts?: Partial): Promise; patch(data?: any, opts?: Partial): Promise; delete(keysToDelete?: string[], opts?: Partial): Promise; pretend(verb?: HttpVerb, data?: any, opts?: Partial): Promise; // Utilities getConfig(): RequestOptions; use(extend: (args: ExtendArgs) => T): T; } export default function api(key?: string | null, opts?: Partial): ApiChain; export function request(config: RequestOptions): Promise<{ response: AnyResponse } & RequestOptions & { source: string }>;