@mussnad/frappe-js-client
Version:
Next-generation TS/JS client for Frappe REST APIs, built on axios for robust, type-safe integration.
187 lines (186 loc) • 6.29 kB
JavaScript
"use strict";
/**
* @module frappe
* @description Main entry point for the Frappe SDK. Provides the FrappeApp class
* which serves as the primary interface for interacting with a Frappe instance.
*
* @packageDocumentation
*
* @example
* ```typescript
* import { FrappeApp } from '@frappe/sdk';
*
* // Initialize without authentication
* const app = new FrappeApp('https://example.com');
*
* // Initialize with token authentication
* const authenticatedApp = new FrappeApp('https://example.com', {
* useToken: true,
* token: () => localStorage.getItem('token'),
* type: 'Bearer'
* });
* ```
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FrappeApp = void 0;
var auth_1 = require("../auth");
var call_1 = require("../call");
var db_1 = require("../db");
var file_1 = require("../file");
var axios_1 = require("../utils/axios");
var client_1 = require("../client");
var permission_1 = require("../permission");
/**
* Main class for interacting with a Frappe instance.
*
* @class FrappeApp
* @description Provides a unified interface for all Frappe operations including
* authentication, database operations, file uploads, and API calls.
*
* @example
* ```typescript
* const app = new FrappeApp('https://instance.example.com');
*
* // Authenticate
* await app.auth().login('username', 'password');
*
* // Database operations
* const doc = await app.db().getDoc('User', 'administrator');
*
* // File operations
* await app.file().upload({
* file: fileBlob,
* filename: 'document.pdf'
* });
*
* // API calls
* const result = await app.call().get('api/method/ping');
* ```
*/
var FrappeApp = /** @class */ (function () {
/**
* Creates a new FrappeApp instance.
*
* @param url - The base URL of the Frappe instance
* @param tokenParams - Optional configuration for token-based authentication
* @param name - Optional name for the app instance
* @param customHeaders - Optional custom headers to include in all requests
*
* @example
* ```typescript
* // Basic initialization
* const app = new FrappeApp('https://instance.example.com');
*
* // With token auth and custom headers
* const app = new FrappeApp(
* 'https://instance.example.com',
* {
* useToken: true,
* token: () => localStorage.getItem('token'),
* type: 'Bearer'
* },
* 'MyApp',
* { 'Custom-Header': 'value' }
* );
* ```
*/
function FrappeApp(url, tokenParams, name, customHeaders) {
var _a, _b;
this.url = url;
this.name = name !== null && name !== void 0 ? name : 'FrappeApp';
this.useToken = (_a = tokenParams === null || tokenParams === void 0 ? void 0 : tokenParams.useToken) !== null && _a !== void 0 ? _a : false;
this.token = tokenParams === null || tokenParams === void 0 ? void 0 : tokenParams.token;
this.tokenType = (_b = tokenParams === null || tokenParams === void 0 ? void 0 : tokenParams.type) !== null && _b !== void 0 ? _b : 'Bearer';
this.customHeaders = customHeaders;
this.axios = (0, axios_1.getAxiosClient)(this.url, this.useToken, this.token, this.tokenType, this.customHeaders);
}
/**
* Returns a FrappeAuth object for authentication operations.
*
* @returns {FrappeAuth} An instance of FrappeAuth for handling authentication
*
* @example
* ```typescript
* const auth = app.auth();
* await auth.login('username', 'password');
* ```
*/
FrappeApp.prototype.auth = function () {
return new auth_1.FrappeAuth(this.url, this.axios, this.useToken, this.token, this.tokenType);
};
/**
* Returns a FrappeDB object for database operations.
*
* @returns {FrappeDB} An instance of FrappeDB for handling database operations
*
* @example
* ```typescript
* const db = app.db();
* const user = await db.getDoc('User', 'administrator');
* ```
*/
FrappeApp.prototype.db = function () {
return new db_1.FrappeDB(this.url, this.axios, this.useToken, this.token, this.tokenType);
};
/**
* Returns a FrappeFileUpload object for file operations.
*
* @returns {FrappeFileUpload} An instance of FrappeFileUpload for handling file operations
*
* @example
* ```typescript
* const fileUpload = app.file();
* await fileUpload.upload({
* file: fileBlob,
* filename: 'document.pdf'
* });
* ```
*/
FrappeApp.prototype.file = function () {
return new file_1.FrappeFileUpload(this.url, this.axios, this.useToken, this.token, this.tokenType, this.customHeaders);
};
/**
* Returns a FrappeCall object for making API calls.
*
* @returns {FrappeCall} An instance of FrappeCall for making API calls
*
* @example
* ```typescript
* const call = app.call();
* const response = await call.get('api/method/frappe.ping');
* ```
*/
FrappeApp.prototype.call = function () {
return new call_1.FrappeCall(this.url, this.axios, this.useToken, this.token, this.tokenType);
};
/**
* Returns a FrappeClient object for client operations.
*
* @returns {FrappeClient} An instance of FrappeClient for handling client operations
*
* @example
* ```typescript
* const client = app.client();
* const response = await client.get('api/method/frappe.ping');
* ```
*/
FrappeApp.prototype.client = function () {
return new client_1.FrappeClient(this.url, this.axios, this.useToken, this.token, this.tokenType);
};
/**
* Returns a Permission object for permission operations.
*
* @returns {Permission} An instance of Permission for handling permission operations
*
* @example
* ```typescript
* const permission = app.permission();
* const hasPermission = await permission.hasPermission('User', 'administrator', 'read');
* ```
*/
FrappeApp.prototype.perms = function () {
return new permission_1.Permission(this.url, this.axios, this.useToken, this.token, this.tokenType);
};
return FrappeApp;
}());
exports.FrappeApp = FrappeApp;