/// import { CatalogEndpoints } from '../endpoints/catalog'; import { PrivateEndpoints } from '../endpoints/private'; import { StatsEndpoints } from '../endpoints/stats'; import { UserEndpoints } from '../endpoints/user'; import { OAuth, IRefreshedToken } from './oauth'; import { EventEmitter } from 'events'; import { EnvatoHttpOptions, RequestOptions, RequestForm, EnvatoHttpResponse } from '../helpers/http'; export declare class Client extends EventEmitter { /** * A collection of endpoints for browsing the Envato Market catalog. */ readonly catalog: CatalogEndpoints; /** * A collection of endpoints for accessing private details about the current user. */ readonly private: PrivateEndpoints; /** * A collection of endpoints for accessing public details about users. */ readonly user: UserEndpoints; /** * A collection of endpoints for retrieving general statistics about the marketplaces. */ readonly stats: StatsEndpoints; /** * The client options. */ options: ClientOptions; /** * The queue for executing requests. */ private _queue; /** * The HTTP client for sending requests. */ private _httpClient; /** * Constructs a new `Client` instance. */ constructor(token: string); constructor(options: ClientOptions); /** * The current access token for the client, which may either be an OAuth access token or a personal token. */ get token(): string; set token(token: string); /** * The refresh token if one was provided when the client was instantiated. The refresh token is only applicable to * OAuth sessions and is used by the client to predict the expiration of access tokens for faster regeneration. * * If a refresh token is not known or available, this will be `undefined`. */ get refreshToken(): string | undefined; set refreshToken(token: string | undefined); /** * The timestamp (in milliseconds) when the current token expires. This will be `undefined` if the client was not * instantiated with an expiration time. */ get expiration(): number | Date | undefined; set expiration(expiration: number | Date | undefined); /** * This will be `true` if this token has expired. This will always return `false` if the client was not * instantiated with an expiration time. */ get expired(): boolean; /** * The number of milliseconds remaining until the current token expires. This can become negative if the expiration * time is in the past. If an expiration time is not set on the client, this will be `undefined`. */ get ttl(): number | undefined; /** * Whether or not the client is currently rate limited and deferring requests. You can also use the `ratelimit` * and `resume` client events to track changes to this value. */ get rateLimited(): boolean; /** * Returns the identity of the current token, which includes the account id, a list of all granted permissions, and * the number of seconds until the token expires. */ getIdentity(): Promise; /** * Returns the unique Envato Account ID for the current user. */ getId(): Promise; /** * Sends a `GET` request to the given path on the API and returns the parsed response. * * @param path The path to query (such as `"/catalog/item"`). * @param options Fetch options to use for this request. */ get(path: string, options?: RequestOptions): Promise; /** * Sends a `POST` request to the given path on the API and returns the parsed response. * * @param path The path to query (such as `"/catalog/item"`). * @param params The posted parameters to send with the request. * @param options Fetch options to use for this request. */ post(path: string, params?: RequestForm, options?: RequestOptions): Promise; /** * Sends a `PUT` request to the given path on the API and returns the parsed response. * * @param path The path to query (such as `"/catalog/item"`). * @param params The posted parameters to send with the request. * @param options Fetch options to use for this request. */ put(path: string, params?: RequestForm, options?: RequestOptions): Promise; /** * Sends a `PATCH` request to the given path on the API and returns the parsed response. * * @param path The path to query (such as `"/catalog/item"`). * @param params The posted parameters to send with the request. * @param options Fetch options to use for this request. */ patch(path: string, params?: RequestForm, options?: RequestOptions): Promise; /** * Sends a `DELETE` request to the given path on the API and returns the parsed response. * * @param path The path to query (such as `"/catalog/item"`). * @param params The posted parameters to send with the request. * @param options Fetch options to use for this request. */ delete(path: string, params?: RequestForm, options?: RequestOptions): Promise; /** * Performs an HTTP request to the API. * * @param method * @param path * @param form * @param options */ private _fetch; private _getRequestHeaders; private _getFullRequestUrl; /** * The `debug` event is invoked whenever an HTTP request completes. The response object is passed. * * @param event * @param listener */ on(event: 'debug', listener: (response: EnvatoHttpResponse) => void): this; /** * The `renew` event is invoked when the client's access token expires and it automatically renews. An object * containing the new access token and expiration timestamp is passed for storage. This only applies to OAuth * clients. * * @param event * @param listener */ on(event: 'renew', listener: (data: IRefreshedToken) => void): this; /** * The `ratelimit` event is invoked whenever the client becomes rate limited. The duration of the rate limit in * milliseconds is passed. The client will automatically pause and queue all requests until the rate limit expires. * * @param event * @param listener */ on(event: 'ratelimit', listener: (duration: number) => void): this; /** * The `resume` event is invoked when a rate limit ends. * @param event * @param listener */ on(event: 'resume', listener: () => void): this; /** * The `debug` event is invoked whenever an HTTP request completes. The response object is passed. * * @param event * @param listener */ once(event: 'debug', listener: (response: EnvatoHttpResponse) => void): this; /** * The `renew` event is invoked when the client's access token expires and it automatically renews. An object * containing the new access token and expiration timestamp is passed for storage. This only applies to OAuth * clients. * * @param event * @param listener */ once(event: 'renew', listener: (data: IRefreshedToken) => void): this; /** * The `ratelimit` event is invoked whenever the client becomes rate limited. The duration of the rate limit in * milliseconds is passed. The client will automatically pause and queue all requests until the rate limit expires. * * @param event * @param listener */ once(event: 'ratelimit', listener: (duration: number) => void): this; /** * The `resume` event is invoked when a rate limit ends. * @param event * @param listener */ once(event: 'resume', listener: () => void): this; } export interface ClientOptions { /** * The token to use for authorization. Acceptable values include: * * - Personal tokens. * - Access tokens (OAuth). */ token: string; /** * The user agent string to send with requests. This should briefly explain what your app is or its purpose. * Please do not use a generic browser user agent. * * Here are some examples of good user agents: * * - `"License activation for my themes"` * - `"Support forum authentication & license verification"` * - `"Gathering data on items"` */ userAgent?: string; /** * For OAuth sessions, you may optionally provide the refresh token to enable automatic token renewal when the * current access token expires. You must also supply the `expiration` option when providing this option. */ refreshToken?: string; /** * For OAuth sessions, you should provide a timestamp representing the time when the access token expires as a * number (in milliseconds) or a `Date`. The client will automatically generate a new access token using the * `refreshToken` option after the expiration time is reached, as long as the `oauth` option is provided. * * **Note:** If you need to store newly generated access tokens, listen for the `renew` event on the client. */ expiration?: Date | number; /** * The OAuth helper instance to use for automatically refreshing access tokens. This instance must have the same * credentials as the instance that was used to authenticate the OAuth session. */ oauth?: OAuth; /** * Optional configuration for outgoing HTTP requests. */ http?: EnvatoHttpOptions; /** * If set to `true`, the client will automatically handle rate limits. Any blocked requests will be retried when * the rate limit ends. Any additional requests sent during a rate limit event will be deferred. Rate limit events * will trigger a `throttled` event on the client when this feature is enabled. * * If set to `false`, rate limited requests will throw an `Envato.TooManyRequests` error and subsequent requests * will not be throttled. * * Defaults to `true`. */ handleRateLimits?: boolean; /** * The maximum number of simultaneous requests this client can send to Envato. It is highly recommended to set a * fairly low limit to avoid getting rate limited. * * If set to `0`, requests will not be throttled and will always be executed immediately. * * Defaults to `3`. */ concurrency?: number; /** * Use an unofficial sandbox for testing? When `true`, a public sandbox from the author of the `envato` package * will be used. You can also specify the base URI for a custom sandbox. * * Note: When sandboxing is enabled, your personal token and user agent is automatically removed from all requests * and a warning that sandbox mode is active will be printed to stdout. */ sandbox?: boolean | string; } export interface IdentityResponse { /** * The client ID of the application, if this is an OAuth session. Otherwise, this is `null`. */ clientId?: string; /** * The unique ID of the user who is authorized by the current token. */ userId: number; /** * A list of permissions (scopes) the current token has been granted. */ scopes: string[]; /** * The number of seconds remaining until the current token expires. This will always be `315360000` for personal * tokens as they are indefinitely valid. */ ttl: number; }