Gathering detailed insights and metrics for frappe-js-sdk
Gathering detailed insights and metrics for frappe-js-sdk
Gathering detailed insights and metrics for frappe-js-sdk
Gathering detailed insights and metrics for frappe-js-sdk
npm install frappe-js-sdk
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
119 Stars
145 Commits
43 Forks
7 Watching
5 Branches
10 Contributors
Updated on 28 Nov 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
25.1%
269
Compared to previous day
Last week
5.7%
1,253
Compared to previous week
Last month
6.2%
5,484
Compared to previous month
Last year
330.4%
44,218
Compared to previous year
1
4
TypeScript/JavaScript library for a Frappe Framework backend.
The library currently supports the following features:
We plan to add the following features in the future:
exists
in the database.The library uses Axios under the hood to make API calls to your Frappe backend.
Maintainer | GitHub | Social |
---|---|---|
Nikhil Kothari | nikkothari22 | @nik_kothari22 |
Janhvi Patil | janhvipatil | @janhvipatil_ |
Sumit Jain | sumitjain236 |
1npm install frappe-js-sdk
or
1yarn add frappe-js-sdk
To get started, initialise the library:
1import { FrappeApp } from 'frappe-js-sdk'; 2//Add your Frappe backend's URL 3const frappe = new FrappeApp('https://test.frappe.cloud');
In case you want to use the library with token based authentication (OAuth bearer tokens or API key/secret pairs), you can initialise the library like this:
1import { FrappeApp } from "frappe-js-sdk";
2
3const frappe = new FrappeApp("https://test.frappe.cloud", {
4 useToken: true,
5 // Pass a custom function that returns the token as a string - this could be fetched from LocalStorage or auth providers like Firebase, Auth0 etc.
6 token: getTokenFromLocalStorage(),
7 // This can be "Bearer" or "token"
8 type: "Bearer"
9})
1const auth = frappe.auth()
This makes an API call to the /api/method/login
endpoint.
1auth 2 .loginWithUsernamePassword({ username: 'admin', password: 'my-password' }) 3 .then((response) => console.log('Logged in')) 4 .catch((error) => console.error(error));
This makes an API call to the /api/method/frappe.auth.get_logged_user
endpoint.
1auth 2 .getLoggedInUser() 3 .then((user) => console.log(`User ${user} is logged in.`)) 4 .catch((error) => console.error(error));
This makes an API call to the /api/method/logout
endpoint.
1auth 2 .logout() 3 .then(() => console.log('Logged out.')) 4 .catch((error) => console.error(error));
This makes an API sends a password reset link to the specified email address.
1auth 2 .forgetPassword('example@example.com') 3 .then(() => console.log('Password Reset Email Sent!')) 4 .catch(() => console.error("We couldn't find your account."));
1const db = frappe.db();
1db.getDoc('DocType', 'My DocType Name') 2 .then((doc) => console.log(doc)) 3 .catch((error) => console.error(error));
1db.getDocList('DocType') 2 .then((docs) => console.log(docs)) 3 .catch((error) => console.error(error));
Optionally, a second argument can be provided to filter, sort, limit and paginate results.
1db.getDocList('DocType', {
2 /** Fields to be fetched */
3 fields: ['name', 'creation'],
4 /** Filters to be applied - SQL AND operation */
5 filters: [['creation', '>', '2021-10-09']],
6 /** Filters to be applied - SQL OR operation */
7 orFilters: [],
8 /** Fetch from nth document in filtered and sorted list. Used for pagination */
9 limit_start: 5,
10 /** Number of documents to be fetched. Default is 20 */
11 limit: 10,
12 /** Sort results by field and order */
13 orderBy: {
14 field: 'creation',
15 order: 'desc',
16 },
17 /** Group the results by particular field */
18 groupBy: 'name',
19 /** Fetch documents as a dictionary */
20 asDict: false,
21})
22 .then((docs) => console.log(docs))
23 .catch((error) => console.error(error));
Type declarations are available for the second argument in the source code.
1const filters = [['creation', '>', '2021-10-09']]; 2const useCache = true; /** Default is false - Optional **/ 3const debug = false; /** Default is false - Optional **/ 4 5db.getCount('DocType', filters, cache, debug) 6 .then((count) => console.log(count)) 7 .catch((error) => console.error(error));
To create a new document, pass the name of the DocType and the fields to createDoc
.
1db.createDoc('My Custom DocType', { 2 name: 'Test', 3 test_field: 'This is a test field', 4}) 5 .then((doc) => console.log(doc)) 6 .catch((error) => console.error(error));
To update an existing document, pass the name of the DocType, name of the document and the fields to be updated to updateDoc
.
1db.updateDoc('My Custom DocType', 'Test', { 2 test_field: 'This is an updated test field.', 3}) 4 .then((doc) => console.log(doc)) 5 .catch((error) => console.error(error));
To create a new document, pass the name of the DocType and the name of the document to be deleted to deleteDoc
.
1db.deleteDoc('My Custom DocType', 'Test') 2 .then((response) => console.log(response.message)) // Message will be "ok" 3 .catch((error) => console.error(error));
The library supports Typescript out of the box.
For example, to enforce type on the updateDoc
method:
1interface TestDoc { 2 test_field: string; 3} 4db.updateDoc<TestDoc>('My Custom DocType', 'Test', { 5 test_field: 'This is an updated test field.', 6});
The library also has an inbuilt type FrappeDoc
which adds the following fields to your type declarations when you use it with the database methods:
1export type FrappeDoc<T> = T & { 2 /** User who created the document */ 3 owner: string; 4 /** Date and time when the document was created - ISO format */ 5 creation: string; 6 /** Date and time when the document was last modified - ISO format */ 7 modified: string; 8 /** User who last modified the document */ 9 modified_by: string; 10 idx: number; 11 /** 0 - Saved, 1 - Submitted, 2 - Cancelled */ 12 docstatus: 0 | 1 | 2; 13 parent?: any; 14 parentfield?: any; 15 parenttype?: any; 16 /** The primary key of the DocType table */ 17 name: string; 18};
All document responses are returned as an intersection of FrappeDoc
and the specified type.
1const call = frappe.call();
Make sure all endpoints are whitelisted (@frappe.whitelist()
) in your backend
Make a GET request to your endpoint with parameters.
1const searchParams = { 2 doctype: 'Currency', 3 txt: 'IN', 4}; 5call 6 .get('frappe.desk.search_link', searchParams) 7 .then((result) => console.log(result)) 8 .catch((error) => console.error(error));
Make a POST request to your endpoint with parameters.
1const updatedFields = { 2 doctype: 'User', 3 name: 'Administrator', 4 fieldname: 'interest', 5 value: 'Frappe Framework, ERPNext', 6}; 7call 8 .post('frappe.client.set_value', updatedFields) 9 .then((result) => console.log(result)) 10 .catch((error) => console.error(error));
Make a PUT request to your endpoint with parameters.
1const updatedFields = { 2 doctype: 'User', 3 name: 'Administrator', 4 fieldname: 'interest', 5 value: 'Frappe Framework, ERPNext', 6}; 7call 8 .put('frappe.client.set_value', updatedFields) 9 .then((result) => console.log(result)) 10 .catch((error) => console.error(error));
Make a DELETE request to your endpoint with parameters.
1const documentToBeDeleted = { 2 doctype: 'Tag', 3 name: 'Random Tag', 4}; 5call 6 .put('frappe.client.delete', documentToBeDeleted) 7 .then((result) => console.log(result)) 8 .catch((error) => console.error(error));
1const file = frappe.file();
1const myFile; //Your File object
2
3const fileArgs = {
4 /** If the file access is private then set to TRUE (optional) */
5 "isPrivate": true,
6 /** Folder the file exists in (optional) */
7 "folder": "Home",
8 /** File URL (optional) */
9 "file_url": "",
10 /** Doctype associated with the file (optional) */
11 "doctype": "User",
12 /** Docname associated with the file (mandatory if doctype is present) */
13 "docname": "Administrator",
14 /** Field in the document **/
15 "fieldname": "image"
16}
17
18file.uploadFile(
19 myFile,
20 fileArgs,
21 /** Progress Indicator callback function **/
22 (completedBytes, totalBytes) => console.log(Math.round((completedBytes / totalBytes) * 100), " completed")
23 )
24 .then(() => console.log("File Upload complete"))
25 .catch(e => console.error(e))
See LICENSE.
No vulnerabilities found.
No security vulnerabilities found.