main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2024-present Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const request_examples = [
  20. {
  21. name: 'entity storage app read',
  22. fetch: async (args) => {
  23. return await fetch(`${window.api_origin}/drivers/call`, {
  24. headers: {
  25. "Content-Type": "application/json",
  26. "Authorization": `Bearer ${puter.authToken}`,
  27. },
  28. body: JSON.stringify({
  29. interface: 'puter-apps',
  30. method: 'read',
  31. args,
  32. }),
  33. method: "POST",
  34. });
  35. },
  36. out: async (resp) => {
  37. const data = await resp.json();
  38. if ( ! data.success ) return data;
  39. return data.result;
  40. },
  41. exec: async function exec (...a) {
  42. const resp = await this.fetch(...a);
  43. return await this.out(resp);
  44. },
  45. },
  46. {
  47. name: 'entity storage app select all',
  48. fetch: async () => {
  49. return await fetch(`${window.api_origin}/drivers/call`, {
  50. headers: {
  51. "Content-Type": "application/json",
  52. "Authorization": `Bearer ${puter.authToken}`,
  53. },
  54. body: JSON.stringify({
  55. interface: 'puter-apps',
  56. method: 'select',
  57. args: { predicate: [] },
  58. }),
  59. method: "POST",
  60. });
  61. },
  62. out: async (resp) => {
  63. const data = await resp.json();
  64. if ( ! data.success ) return data;
  65. return data.result;
  66. },
  67. exec: async function exec (...a) {
  68. const resp = await this.fetch(...a);
  69. return await this.out(resp);
  70. },
  71. },
  72. {
  73. name: 'grant permission from a user to a user',
  74. fetch: async (user, perm) => {
  75. return await fetch(`${window.api_origin}/auth/grant-user-user`, {
  76. "headers": {
  77. "Content-Type": "application/json",
  78. "Authorization": `Bearer ${puter.authToken}`,
  79. },
  80. "body": JSON.stringify({
  81. target_username: user,
  82. permission: perm,
  83. }),
  84. "method": "POST",
  85. });
  86. },
  87. out: async (resp) => {
  88. const data = await resp.json();
  89. return data;
  90. },
  91. exec: async function exec (...a) {
  92. const resp = await this.fetch(...a);
  93. return await this.out(resp);
  94. },
  95. },
  96. {
  97. name: 'write file',
  98. fetch: async (path, str) => {
  99. const endpoint = `${window.api_origin}/write`;
  100. const token = puter.authToken;
  101. const blob = new Blob([str], { type: 'text/plain' });
  102. const formData = new FormData();
  103. formData.append('create_missing_ancestors', true);
  104. formData.append('path', path);
  105. formData.append('size', 8);
  106. formData.append('overwrite', true);
  107. formData.append('file', blob, 'something.txt');
  108. const response = await fetch(endpoint, {
  109. method: 'POST',
  110. headers: { 'Authorization': `Bearer ${token}` },
  111. body: formData
  112. });
  113. return await response.json();
  114. },
  115. }
  116. ];
  117. globalThis.reqex = request_examples;
  118. globalThis.service_script(api => {
  119. api.on_ready(() => {
  120. });
  121. });