@mussnad/frappe-js-client
Version:
Next-generation TS/JS client for Frappe REST APIs, built on axios for robust, type-safe integration.
223 lines (222 loc) • 6.94 kB
JavaScript
;
/**
* @module call
* @description Provides HTTP API call functionality for Frappe.
* This module handles REST API calls with authentication support and
* standardized error handling.
*
* @packageDocumentation
*
* @example
* ```typescript
* import { FrappeApp } from '@frappe/sdk';
*
* const app = new FrappeApp('https://instance.example.com');
* const call = app.call();
*
* // Make a GET request
* const response = await call.get('frappe.ping');
*
* // Make a POST request with data
* const result = await call.post('frappe.handler', {
* data: { key: 'value' }
* });
* ```
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FrappeCall = void 0;
var axios_1 = require("../utils/axios");
/**
* Handles HTTP API calls to Frappe endpoints.
*
* @class FrappeCall
* @description Provides methods for making authenticated HTTP requests
* to Frappe API endpoints with standardized error handling.
*
* @example
* ```typescript
* const call = new FrappeCall(
* 'https://instance.example.com',
* axiosInstance,
* true,
* () => localStorage.getItem('token'),
* 'Bearer'
* );
*
* // Make API calls
* const response = await call.get('frappe.ping');
* ```
*/
var FrappeCall = /** @class */ (function () {
/**
* Creates a new FrappeCall instance.
*
* @param appURL - Base URL of the Frappe instance
* @param axios - Configured Axios instance for making requests
* @param useToken - Whether to use token-based authentication
* @param token - Function that returns the authentication token
* @param tokenType - Type of token to use ('Bearer' or 'token')
*
* @example
* ```typescript
* const call = new FrappeCall(
* 'https://instance.example.com',
* axiosInstance,
* true,
* () => localStorage.getItem('token'),
* 'Bearer'
* );
* ```
*/
function FrappeCall(appURL, axios, useToken, token, tokenType) {
this.appURL = appURL;
this.axios = axios;
this.useToken = useToken !== null && useToken !== void 0 ? useToken : false;
this.token = token;
this.tokenType = tokenType;
}
/**
* Makes a GET request to a Frappe API endpoint.
*
* @template T - Type of the response data
* @param path - API endpoint path
* @param params - Query parameters
* @returns Promise resolving to the response data
* @throws {Error} If the request fails
*
* @example
* ```typescript
* // Simple GET request
* const ping = await call.get('frappe.ping');
*
* // GET request with parameters
* const users = await call.get<User[]>('frappe.user.get_users', {
* filters: { user_type: 'System User' }
* });
* ```
*/
FrappeCall.prototype.get = function (path, params) {
var encodedParams = new URLSearchParams();
if (params) {
Object.entries(params).forEach(function (_a) {
var key = _a[0], value = _a[1];
if (value !== null && value !== undefined) {
var val = typeof value === 'object' ? JSON.stringify(value) : String(value);
encodedParams.set(key, val);
}
});
}
return (0, axios_1.handleRequest)({
axios: this.axios,
config: {
method: 'GET',
url: "/api/method/".concat(path),
params: encodedParams,
},
errorMessage: 'There was an error while making the GET request.',
transformResponse: function (response) { return response.data; },
});
};
/**
* Makes a POST request to a Frappe API endpoint.
*
* @template T - Type of the response data
* @param path - API endpoint path
* @param params - Request body data
* @returns Promise resolving to the response data
* @throws {Error} If the request fails
*
* @example
* ```typescript
* // Simple POST request
* const result = await call.post('frappe.handler', {
* data: { key: 'value' }
* });
*
* // POST request with typed response
* interface LoginResponse {
* token: string;
* user: User;
* }
* const login = await call.post<LoginResponse>('login', {
* usr: 'admin',
* pwd: 'password'
* });
* ```
*/
FrappeCall.prototype.post = function (path, params) {
return (0, axios_1.handleRequest)({
axios: this.axios,
config: {
method: 'POST',
url: "/api/method/".concat(path),
data: params,
},
errorMessage: 'There was an error while making the POST request.',
transformResponse: function (response) { return response.data; },
});
};
/**
* Makes a PUT request to a Frappe API endpoint.
*
* @template T - Type of the response data
* @param path - API endpoint path
* @param params - Request body data
* @returns Promise resolving to the response data
* @throws {Error} If the request fails
*
* @example
* ```typescript
* // Update user preferences
* const result = await call.put('frappe.user.update_prefs', {
* user: 'admin',
* preferences: {
* theme: 'dark'
* }
* });
* ```
*/
FrappeCall.prototype.put = function (path, params) {
return (0, axios_1.handleRequest)({
axios: this.axios,
config: {
method: 'PUT',
url: "/api/method/".concat(path),
data: params,
},
errorMessage: 'There was an error while making the PUT request.',
transformResponse: function (response) { return response.data; },
});
};
/**
* Makes a DELETE request to a Frappe API endpoint.
*
* @template T - Type of the response data
* @param path - API endpoint path
* @param params - Query parameters
* @returns Promise resolving to the response data
* @throws {Error} If the request fails
*
* @example
* ```typescript
* // Delete a temporary file
* await call.delete('frappe.handler.delete_file', {
* filename: 'temp.txt'
* });
* ```
*/
FrappeCall.prototype.delete = function (path, params) {
return (0, axios_1.handleRequest)({
axios: this.axios,
config: {
method: 'DELETE',
url: "/api/method/".concat(path),
params: params,
},
errorMessage: 'There was an error while making the DELETE request.',
transformResponse: function (response) { return response.data; },
});
};
return FrappeCall;
}());
exports.FrappeCall = FrappeCall;