main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const request_examples = [
  2. {
  3. name: 'entity storage app read',
  4. fetch: async (args) => {
  5. return await fetch(`${window.api_origin}/drivers/call`, {
  6. headers: {
  7. "Content-Type": "application/json",
  8. "Authorization": `Bearer ${puter.authToken}`,
  9. },
  10. body: JSON.stringify({
  11. interface: 'puter-apps',
  12. method: 'read',
  13. args,
  14. }),
  15. method: "POST",
  16. });
  17. },
  18. out: async (resp) => {
  19. const data = await resp.json();
  20. if ( ! data.success ) return data;
  21. return data.result;
  22. },
  23. exec: async function exec (...a) {
  24. const resp = await this.fetch(...a);
  25. return await this.out(resp);
  26. },
  27. },
  28. {
  29. name: 'entity storage app select all',
  30. fetch: async () => {
  31. return await fetch(`${window.api_origin}/drivers/call`, {
  32. headers: {
  33. "Content-Type": "application/json",
  34. "Authorization": `Bearer ${puter.authToken}`,
  35. },
  36. body: JSON.stringify({
  37. interface: 'puter-apps',
  38. method: 'select',
  39. args: { predicate: [] },
  40. }),
  41. method: "POST",
  42. });
  43. },
  44. out: async (resp) => {
  45. const data = await resp.json();
  46. if ( ! data.success ) return data;
  47. return data.result;
  48. },
  49. exec: async function exec (...a) {
  50. const resp = await this.fetch(...a);
  51. return await this.out(resp);
  52. },
  53. },
  54. {
  55. name: 'grant permission from a user to a user',
  56. fetch: async (user, perm) => {
  57. return await fetch(`${window.api_origin}/auth/grant-user-user`, {
  58. "headers": {
  59. "Content-Type": "application/json",
  60. "Authorization": `Bearer ${puter.authToken}`,
  61. },
  62. "body": JSON.stringify({
  63. target_username: user,
  64. permission: perm,
  65. }),
  66. "method": "POST",
  67. });
  68. },
  69. out: async (resp) => {
  70. const data = await resp.json();
  71. return data;
  72. },
  73. exec: async function exec (...a) {
  74. const resp = await this.fetch(...a);
  75. return await this.out(resp);
  76. },
  77. },
  78. {
  79. name: 'write file',
  80. fetch: async (path, str) => {
  81. const endpoint = `${window.api_origin}/write`;
  82. const token = puter.authToken;
  83. const blob = new Blob([str], { type: 'text/plain' });
  84. const formData = new FormData();
  85. formData.append('create_missing_ancestors', true);
  86. formData.append('path', path);
  87. formData.append('size', 8);
  88. formData.append('overwrite', true);
  89. formData.append('file', blob, 'something.txt');
  90. const response = await fetch(endpoint, {
  91. method: 'POST',
  92. headers: { 'Authorization': `Bearer ${token}` },
  93. body: formData
  94. });
  95. return await response.json();
  96. },
  97. }
  98. ];
  99. globalThis.reqex = request_examples;
  100. globalThis.service_script(api => {
  101. api.on_ready(() => {
  102. });
  103. });