Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit 324b3d7

Browse files
Merge pull request #433 from appwrite/feat-add-bun-examples
feat: add docs for bun runtime
2 parents e8b31df + e549657 commit 324b3d7

File tree

2 files changed

+219
-14
lines changed

2 files changed

+219
-14
lines changed

app/views/docs/functions-develop.phtml

+215-6
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,43 @@ public class Handler {
433433
}</code></pre>
434434
</div>
435435
</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>
436473
</ul>
437474

438475
<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 {
447484
<table class="full text-size-small">
448485
<thead>
449486
<tr>
450-
<td>Property</td>
487+
<td style="width: 250px">Property</td>
451488
<td>Description</td>
452489
</tr>
453490
</thead>
@@ -467,7 +504,6 @@ public class Handler {
467504
<tr>
468505
<td><code>error()</code></td>
469506
<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>
471507
</tr>
472508
</tbody>
473509
</table>
@@ -503,6 +539,22 @@ export default async function (context: any) {
503539
return context.res.send("This is a response!");
504540
}
505541

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+
506558
// after destructuring
507559
export default async function ({ req, res, log, error }: any) {
508560
log("This is a log!");
@@ -756,6 +808,25 @@ public class Main {
756808
}</code></pre>
757809
</div>
758810
</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>
759830
</ul>
760831

761832
<h3><a href="#headers" id="headers">Headers</a></h3>
@@ -925,7 +996,7 @@ end</code></pre>
925996
case 'empty':
926997
return res.empty();
927998
case 'json':
928-
return res.json({type": "This is a JSON response"});
999+
return res.json({"type": "This is a JSON response"});
9291000
case 'redirect':
9301001
return res.redirect("https://appwrite.io", 301);
9311002
case 'html':
@@ -1091,6 +1162,29 @@ namespace runtime {
10911162
}
10921163
}
10931164
};
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+
"&lt;h1&gt;This is an HTML response&lt;/h1&gt;", 200, {
1183+
"content-type": "text/html"
1184+
});
1185+
default:
1186+
return res.send("This is a text response");
1187+
}
10941188
}</code></pre>
10951189
</div>
10961190
</li>
@@ -1198,9 +1292,9 @@ end</code></pre>
11981292
<li>
11991293
<h3>Deno</h3>
12001294
<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) => {
12021296
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`);
12041298
error("This is an error, use for logging errors to console");
12051299

12061300
return res.send("Check the Appwrite Console to see logs and errors!");
@@ -1312,6 +1406,18 @@ namespace runtime {
13121406
}</code></pre>
13131407
</div>
13141408
</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>
13151421
</ul>
13161422
<p>You can access these logs through the following steps.</p>
13171423
<ol class="margin-top margin-bottom-large text-size-normal">
@@ -1535,6 +1641,14 @@ namespace runtime {
15351641

15361642
return context.res.send(std::getenv("MY_VAR"));
15371643
};
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'));
15381652
}</code></pre>
15391653
</div>
15401654
</li>
@@ -1619,6 +1733,14 @@ namespace runtime {
16191733
<td><a href="https://www.nuget.org/" target="_blank">NuGet</a></td>
16201734
<td><code>dotnet restore</code></td>
16211735
</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>
16221744
<tr>
16231745
<td>
16241746
<img src="" data-ls-attrs="src=/images/runtimes/kotlin.png" alt="Swift icon" class="avatar xxs" />
@@ -1997,6 +2119,35 @@ public class Main {
19972119

19982120
return context.res.send("Document created");
19992121
}
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");
20002151
}</code></pre>
20012152
</div>
20022153
</li>
@@ -2387,6 +2538,40 @@ public class Main {
23872538
return context.res.send("Document created");
23882539

23892540
}
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");
23902575
}</code></pre>
23912576
</div>
23922577
</li>
@@ -2620,6 +2805,25 @@ public class Main {
26202805
public RuntimeOutput main(RuntimeContext context) throws Exception {
26212806
return context.res.send(Utils.add(1, 2));
26222807
}
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));
26232827
}</code></pre>
26242828
</div>
26252829
</li>
@@ -2630,14 +2834,19 @@ public class Main {
26302834
<p>
26312835
Appwrite Functions received major updates in Appwrite version 1.4.
26322836
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.
26342838
</p>
26352839

26362840
<p>
26372841
Here's a checklist of things you need to know.
26382842
</p>
26392843

26402844
<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>
26412850
<li>
26422851
The parameter passed into functions has changed.
26432852
<code>req</code> and <code>res</code> has been replaced by <code>context</code>, which contains new logger methods.

app/views/docs/functions-runtimes.phtml

+4-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ foreach ($runtimes as $key => $item) {
4343
<th style="width: 70px">Name</th>
4444
<th style="width: 100px">Version</th>
4545
<th style="width: 60px"></th>
46-
<th>Image</th>
4746
<th style="width: 80px">Architectures</th>
4847
</tr>
4948
</thead>
@@ -53,8 +52,10 @@ foreach ($runtimes as $key => $item) {
5352
<td>
5453
<img src="" data-ls-attrs="src=/images/runtimes/<?php echo $this->escape($runtime['versions'][0]['logo'] ?? ''); ?>" alt="Function Env." class="avatar xxs" />
5554
</td>
56-
<td>
57-
<?php echo $this->escape($key); ?>
55+
<td>
56+
<a href="https://hub.docker.com/r/openruntimes/<?php echo $this->escape($runtime['versions'][0]['key']); ?>/tags" target="_blank" rel="noopener">
57+
<?php echo $this->escape($key); ?><i class="icon-link-ext"></i>
58+
</a>
5859
</td>
5960
<td>
6061
<?php foreach($runtime['versions'] as $key => $version): ?>
@@ -68,11 +69,6 @@ foreach ($runtimes as $key => $item) {
6869
<?php endif; ?>
6970
<?php endforeach; ?>
7071
</td>
71-
<td>
72-
<?php foreach($runtime['versions'] as $key => $version): ?>
73-
<a href="https://hub.docker.com/r/<?php echo $this->escape(strtok($version['image'], ':')); ?>" target="_blank" rel="noopener"><?php echo $this->escape($version['image'] ?? ''); ?> <i class="icon-link-ext"></i></a>
74-
<?php endforeach; ?>
75-
</td>
7672
<td><?php echo $this->escape(implode(' / ', $runtime['versions'][0]['supports'] ?? [])); ?></td>
7773
</tr>
7874
<?php endforeach; ?>

0 commit comments

Comments
 (0)