@pinia-orm/axios-edge
Version:
Axios plugin for pinia-orm
172 lines (163 loc) • 4.96 kB
text/typescript
import { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
import { Element, Model, Repository, Database, Collection, Constructor, PiniaOrmPlugin } from 'pinia-orm';
import { Pinia } from 'pinia';
type PersistMethods = 'save' | 'insert';
type PersistOptions = {
[P in PersistMethods]?: string[];
};
interface Config extends AxiosRequestConfig {
dataKey?: string;
url?: string;
method?: string;
data?: any;
dataTransformer?: (response: AxiosResponse) => Element | Element[];
save?: boolean;
persistBy?: PersistMethods;
persistOptions?: PersistOptions;
/**
* This tells the api to delete the record from the store after the call is finished.
*
* It needs the `primaryKey` value (ID) of the entity as argument.
*/
delete?: string | number;
actions?: {
[name: string]: any;
};
}
interface GlobalConfig extends Config {
axios?: AxiosInstance;
}
declare class AxiosRepository<M extends Model = Model> extends Repository<M> {
axios: AxiosInstance;
globalApiConfig: GlobalConfig;
apiConfig: Config;
constructor(database: Database, pinia?: Pinia);
api(): Request;
setAxios(axios: AxiosInstance): this;
}
declare class Response {
/**
* The repository that called the request.
*/
repository: AxiosRepository;
/**
* The request configuration.
*/
config: Config;
/**
* The axios response instance.
*/
response: AxiosResponse;
/**
* Entities created by Pinia ORM.
*/
entities: Collection | null;
/**
* Whether if response data is saved to the store or not.
*/
isSaved: boolean;
/**
* Create a new response instance.
*/
constructor(repository: AxiosRepository, config: Config, response: AxiosResponse);
/**
* Save response data to the store.
*/
save(): Promise<void>;
/**
* Delete the entity record where the `delete` option is configured.
*/
delete(): Promise<void>;
/**
* Get the response data from the axios response object. If a `dataTransformer`
* option is configured, it will be applied to the response object. If the
* `dataKey` option is configured, it will return the data from the given
* property within the response body.
*/
getDataFromResponse(): Element | Element[];
/**
* Get persist options if any set in config.
*/
/**
* Validate the given data to ensure the Pinia ORM persist methods accept it.
*/
protected validateData(data: any): data is Element | Element[];
/**
* Validate the given string as to ensure it correlates with the available
* Pinia ORM persist methods.
*/
protected validatePersistAction(action: string): action is PersistMethods;
}
declare class Request {
/**
* The repository class.
*/
repository: AxiosRepository;
/**
* The default config.
*/
config: Config;
/**
* Create a new api instance.
*/
constructor(repository: AxiosRepository);
/**
* Index key for the user defined actions.
*/
[action: string]: any;
/**
* Get the axios client.
*/
get axios(): AxiosInstance;
/**
* Register actions from the repository config.
*/
private registerActions;
/**
* Register the given object action.
*/
private registerObjectAction;
/**
* Register the given function action.
*/
private registerFunctionAction;
/**
* Perform a get request.
*/
get(url: string, config?: Config): Promise<Response>;
/**
* Perform a post request.
*/
post(url: string, data?: any, config?: Config): Promise<Response>;
/**
* Perform a put request.
*/
put(url: string, data?: any, config?: Config): Promise<Response>;
/**
* Perform a patch request.
*/
patch(url: string, data?: any, config?: Config): Promise<Response>;
/**
* Perform a delete request.
*/
delete(url: string, config?: Config): Promise<Response>;
/**
* Perform an api request.
*/
request(config: Config): Promise<Response>;
/**
* Create a new config by merging the global config, the repository config,
* and the given config.
*/
private createConfig;
/**
* Create a new response instance by applying a few initialization processes.
* For example, it saves response data if `save` option id set to `true`.
*/
private createResponse;
}
declare function useAxiosApi(repository: AxiosRepository<any>): Request;
declare function useAxiosRepo<M extends Model>(model: Constructor<M>): AxiosRepository<Model>;
declare function createPiniaOrmAxios(axiosConfig?: GlobalConfig): PiniaOrmPlugin;
declare const piniaOrmPluginAxios: PiniaOrmPlugin;
export { AxiosRepository, Request, Response, createPiniaOrmAxios, piniaOrmPluginAxios, useAxiosApi, useAxiosRepo };