@@ -433,6 +433,43 @@ public class Handler {
433
433
}</code></pre>
434
434
</div>
435
435
</li>
436
+ <li>
437
+ <h3>Bun</h3>
438
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
439
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>import { Client } from 'node-appwrite';
440
+
441
+ // This is your Appwrite function
442
+ // It's executed each time we get a request
443
+ export default async ({ req, res, log, error }: any) => {
444
+ // Why not try the Appwrite SDK?
445
+ // const client = new Client()
446
+ // .setEndpoint('https://cloud.appwrite.io/v1')
447
+ // .setProject(Bun.env["APPWRITE_FUNCTION_PROJECT_ID"])
448
+ // .setKey(Bun.env["APPWRITE_API_KEY"]);
449
+
450
+ // You can log messages to the console
451
+ log("Hello, Logs!");
452
+
453
+ // If something goes wrong, log an error
454
+ error("Hello, Errors!");
455
+
456
+ // The `req` object contains the request data
457
+ if (req.method === "GET") {
458
+ // Send a response with the res object helpers
459
+ // `res.send()` dispatches a string back to the client
460
+ return res.send("Hello, World!");
461
+ }
462
+
463
+ // `res.json()` is a handy helper for sending JSON
464
+ return res.json({
465
+ motto: "Build Fast. Scale Big. All in One Place.",
466
+ learn: "https://appwrite.io/docs",
467
+ connect: "https://appwrite.io/discord",
468
+ getInspired: "https://builtwith.appwrite.io",
469
+ });
470
+ };</code></pre>
471
+ </div>
472
+ </li>
436
473
</ul>
437
474
438
475
<p>If you prefer to learn through more examples like this, explore the <a href="/docs/functions-examples">examples page</a>.</p>
@@ -447,7 +484,7 @@ public class Handler {
447
484
<table class="full text-size-small">
448
485
<thead>
449
486
<tr>
450
- <td>Property</td>
487
+ <td style="width: 250px" >Property</td>
451
488
<td>Description</td>
452
489
</tr>
453
490
</thead>
@@ -467,7 +504,6 @@ public class Handler {
467
504
<tr>
468
505
<td><code>error()</code></td>
469
506
<td>Methoc to log errors to the Appwrite Console, end users will not be able to see these errors. See full examples <a href="#logging">here</a>.</td>
470
- <td></td>
471
507
</tr>
472
508
</tbody>
473
509
</table>
@@ -503,6 +539,22 @@ export default async function (context: any) {
503
539
return context.res.send("This is a response!");
504
540
}
505
541
542
+ // after destructuring
543
+ export default async function ({ req, res, log, error }: any) {
544
+ log("This is a log!");
545
+ return res.send("This is a response!");
546
+ }</code></pre>
547
+ </div>
548
+ </li>
549
+ <li>
550
+ <h3>Bun</h3>
551
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
552
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>// before destructuring
553
+ export default async function (context: any) {
554
+ context.log("This is a log!");
555
+ return context.res.send("This is a response!");
556
+ }
557
+
506
558
// after destructuring
507
559
export default async function ({ req, res, log, error }: any) {
508
560
log("This is a log!");
@@ -756,6 +808,25 @@ public class Main {
756
808
}</code></pre>
757
809
</div>
758
810
</li>
811
+ <li>
812
+ <h3>Bun</h3>
813
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
814
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ req, res, log }: any) => {
815
+ log(req.bodyRaw); // Raw request body, contains request data
816
+ log(JSON.stringify(req.body)); // Object from parsed JSON request body, otherwise string
817
+ log(JSON.stringify(req.headers)); // String key-value pairs of all request headers, keys are lowercase
818
+ log(req.scheme); // Value of the x-forwarded-proto header, usually http or https
819
+ log(req.method); // Request method, such as GET, POST, PUT, DELETE, PATCH, etc.
820
+ log(req.url); // Full URL, for example: http://awesome.appwrite.io:8000/v1/hooks?limit=12&offset=50
821
+ log(req.host); // Hostname from the host header, such as awesome.appwrite.io
822
+ log(req.port); // Port from the host header, for example 8000
823
+ log(req.path); // Path part of URL, for example /v1/hooks
824
+ log(req.queryString); // Raw query params string. For example "limit=12&offset=50"
825
+ log(JSON.stringify(req.query)); // Parsed query params. For example, req.query.limit
826
+
827
+ return res.send("All the request parameters are logged to the Appwrite Console.");</code></pre>
828
+ </div>
829
+ </li>
759
830
</ul>
760
831
761
832
<h3><a href="#headers" id="headers">Headers</a></h3>
@@ -925,7 +996,7 @@ end</code></pre>
925
996
case 'empty':
926
997
return res.empty();
927
998
case 'json':
928
- return res.json({type": "This is a JSON response"});
999
+ return res.json({" type": "This is a JSON response"});
929
1000
case 'redirect':
930
1001
return res.redirect("https://appwrite.io", 301);
931
1002
case 'html':
@@ -1091,6 +1162,29 @@ namespace runtime {
1091
1162
}
1092
1163
}
1093
1164
};
1165
+ }</code></pre>
1166
+ </div>
1167
+ </li>
1168
+ <li>
1169
+ <h3>Bun</h3>
1170
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
1171
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ req, res, log }) => {
1172
+
1173
+ switch (req.query.type) {
1174
+ case 'empty':
1175
+ return res.empty();
1176
+ case 'json':
1177
+ return res.json({"type": "This is a JSON response"});
1178
+ case 'redirect':
1179
+ return res.redirect("https://appwrite.io", 301);
1180
+ case 'html':
1181
+ return res.send(
1182
+ "<h1>This is an HTML response</h1>", 200, {
1183
+ "content-type": "text/html"
1184
+ });
1185
+ default:
1186
+ return res.send("This is a text response");
1187
+ }
1094
1188
}</code></pre>
1095
1189
</div>
1096
1190
</li>
@@ -1198,9 +1292,9 @@ end</code></pre>
1198
1292
<li>
1199
1293
<h3>Deno</h3>
1200
1294
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Deno">
1201
- <pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ res, log, error }: any) => {
1295
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ req, res, log, error }: any) => {
1202
1296
log("This is a log, use for logging information to console");
1203
- log(`This function was called with ${context. req.method} method`);
1297
+ log(`This function was called with ${req.method} method`);
1204
1298
error("This is an error, use for logging errors to console");
1205
1299
1206
1300
return res.send("Check the Appwrite Console to see logs and errors!");
@@ -1312,6 +1406,18 @@ namespace runtime {
1312
1406
}</code></pre>
1313
1407
</div>
1314
1408
</li>
1409
+ <li>
1410
+ <h3>Bun</h3>
1411
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
1412
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ req, res, log, error }: any) => {
1413
+ log("This is a log, use for logging information to console");
1414
+ log(`This function was called with ${req.method} method`);
1415
+ error("This is an error, use for logging errors to console");
1416
+
1417
+ return res.send("Check the Appwrite Console to see logs and errors!");
1418
+ };</code></pre>
1419
+ </div>
1420
+ </li>
1315
1421
</ul>
1316
1422
<p>You can access these logs through the following steps.</p>
1317
1423
<ol class="margin-top margin-bottom-large text-size-normal">
@@ -1535,6 +1641,14 @@ namespace runtime {
1535
1641
1536
1642
return context.res.send(std::getenv("MY_VAR"));
1537
1643
};
1644
+ }</code></pre>
1645
+ </div>
1646
+ </li>
1647
+ <li>
1648
+ <h3>Bun</h3>
1649
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
1650
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ req, res, log }) => {
1651
+ return res.send(Bun.env.get('MY_VAR'));
1538
1652
}</code></pre>
1539
1653
</div>
1540
1654
</li>
@@ -1619,6 +1733,14 @@ namespace runtime {
1619
1733
<td><a href="https://www.nuget.org/" target="_blank">NuGet</a></td>
1620
1734
<td><code>dotnet restore</code></td>
1621
1735
</tr>
1736
+ <tr>
1737
+ <td>
1738
+ <img src="" data-ls-attrs="src=/images/runtimes/bun.png" alt="Bun icon" class="avatar xxs" />
1739
+ </td>
1740
+ <td><b>Bun</b></td>
1741
+ <td><a href="https://bun.sh/package-manager" target="_blank">bun</a></td>
1742
+ <td><code>bun install</code></td>
1743
+ </tr>
1622
1744
<tr>
1623
1745
<td>
1624
1746
<img src="" data-ls-attrs="src=/images/runtimes/kotlin.png" alt="Swift icon" class="avatar xxs" />
@@ -1997,6 +2119,35 @@ public class Main {
1997
2119
1998
2120
return context.res.send("Document created");
1999
2121
}
2122
+ }</code></pre>
2123
+ </div>
2124
+ </li>
2125
+ <li>
2126
+ <h3>Bun</h3>
2127
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
2128
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>import { Client, Databases, ID } from 'node-appwrite';
2129
+
2130
+ export default function ({req, res, error}: any){
2131
+ const client = new Client()
2132
+ .setEndpoint("https://cloud.appwrite.io/v1")
2133
+ .setProject(Bun.env.get("APPWRITE_FUNCTION_PROJECT_ID") || "")
2134
+ .setKey(Bun.env.get("APPWRITE_API_KEY") || "");
2135
+
2136
+ const databases = new Databases(client);
2137
+
2138
+ try {
2139
+ databases.createDocument(
2140
+ "[DATABASE_ID]",
2141
+ "[COLLECTION_ID]",
2142
+ ID.unique(),
2143
+ {}
2144
+ );
2145
+ } catch (e) {
2146
+ error("Failed to create document: " + e.message);
2147
+ return res.send("Failed to create document");
2148
+ }
2149
+
2150
+ return res.send("Document created");
2000
2151
}</code></pre>
2001
2152
</div>
2002
2153
</li>
@@ -2387,6 +2538,40 @@ public class Main {
2387
2538
return context.res.send("Document created");
2388
2539
2389
2540
}
2541
+ }</code></pre>
2542
+ </div>
2543
+ </li>
2544
+ <li>
2545
+ <h3>Bun</h3>
2546
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
2547
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>import { Client, Databases, ID } from 'node-appwrite';
2548
+
2549
+ export default function ({req, res, error}: any){
2550
+ const client = new Client()
2551
+ .setEndpoint("https://cloud.appwrite.io/v1")
2552
+ .setProject(Bun.env.get("APPWRITE_FUNCTION_PROJECT_ID") || "")
2553
+
2554
+ if (req.headers["x-appwrite-user-jwt"]) {
2555
+ client.setJWT(req.headers["x-appwrite-user-jwt"]);
2556
+ } else {
2557
+ return res.send("Please sign in, JWT not found");
2558
+ }
2559
+
2560
+ const databases = new Databases(client);
2561
+
2562
+ try {
2563
+ databases.createDocument(
2564
+ "[DATABASE_ID]",
2565
+ "[COLLECTION_ID]",
2566
+ ID.unique(),
2567
+ {}
2568
+ );
2569
+ } catch (e) {
2570
+ error("Failed to create document: " + e.message)
2571
+ return res.send("Failed to create document");
2572
+ }
2573
+
2574
+ return res.send("Document created");
2390
2575
}</code></pre>
2391
2576
</div>
2392
2577
</li>
@@ -2620,6 +2805,25 @@ public class Main {
2620
2805
public RuntimeOutput main(RuntimeContext context) throws Exception {
2621
2806
return context.res.send(Utils.add(1, 2));
2622
2807
}
2808
+ }</code></pre>
2809
+ </div>
2810
+ </li>
2811
+ <li>
2812
+ <h3>Bun</h3>
2813
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
2814
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>// src/utils.ts
2815
+
2816
+ export function add(a: number, b: number): number {
2817
+ return a + b;
2818
+ }</code></pre>
2819
+ </div>
2820
+ <div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
2821
+ <pre class="line-numbers"><code class="prism language-typescript" data-prism>// src/main.ts
2822
+
2823
+ import { add } from './utils.ts';
2824
+
2825
+ export default function ({res}: {res: any}) {
2826
+ return res.send(add(1, 2));
2623
2827
}</code></pre>
2624
2828
</div>
2625
2829
</li>
@@ -2630,14 +2834,19 @@ public class Main {
2630
2834
<p>
2631
2835
Appwrite Functions received major updates in Appwrite version 1.4.
2632
2836
If you still have functions from previous versions, they will be <b>read-only</b> in Appwrite 1.4.
2633
- You will have to migrate your old functions to follow new runtime syntax.
2837
+ You will have to recreate your old functions to follow new runtime syntax.
2634
2838
</p>
2635
2839
2636
2840
<p>
2637
2841
Here's a checklist of things you need to know.
2638
2842
</p>
2639
2843
2640
2844
<ol class="margin-top margin-bottom-large text-size-normal">
2845
+ <li>
2846
+ Your old function from 1.3 will continue to work, but it can't be updated directly to a 1.4 function.
2847
+ You need to create a new function following 1.4 syntax.
2848
+ After you've created your new function, point your application code to the new function and delete the old function.
2849
+ </li>
2641
2850
<li>
2642
2851
The parameter passed into functions has changed.
2643
2852
<code>req</code> and <code>res</code> has been replaced by <code>context</code>, which contains new logger methods.
0 commit comments