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

feat: add docs for bun runtime #433

Merged
merged 13 commits into from
Sep 14, 2023
211 changes: 208 additions & 3 deletions app/views/docs/functions-develop.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,43 @@ public class Handler {
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>import { Client } from 'node-appwrite';

// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }: any) => {
// Why not try the Appwrite SDK?
// const client = new Client()
// .setEndpoint('https://cloud.appwrite.io/v1')
// .setProject(Bun.env["APPWRITE_FUNCTION_PROJECT_ID"])
// .setKey(Bun.env["APPWRITE_API_KEY"]);

// You can log messages to the console
log("Hello, Logs!");

// If something goes wrong, log an error
error("Hello, Errors!");

// The `req` object contains the request data
if (req.method === "GET") {
// Send a response with the res object helpers
// `res.send()` dispatches a string back to the client
return res.send("Hello, World!");
}

// `res.json()` is a handy helper for sending JSON
return res.json({
motto: "Build Fast. Scale Big. All in One Place.",
learn: "https://appwrite.io/docs",
connect: "https://appwrite.io/discord",
getInspired: "https://builtwith.appwrite.io",
});
};</code></pre>
</div>
</li>
</ul>

<p>If you prefer to learn through more examples like this, explore the <a href="/docs/functions-examples">examples page</a>.</p>
Expand All @@ -444,10 +481,10 @@ public class Handler {
</p>

<p>You'll find these properties in the context object.</p>
<table class="full text-size-small">
<table cellspacing="0" cellpadding="0" border="0" class="full text-size-small">
christyjacob4 marked this conversation as resolved.
Show resolved Hide resolved
<thead>
<tr>
<td>Property</td>
<td style="width: 250px">Property</td>
christyjacob4 marked this conversation as resolved.
Show resolved Hide resolved
<td>Description</td>
</tr>
</thead>
Expand Down Expand Up @@ -503,6 +540,22 @@ export default async function (context: any) {
return context.res.send("This is a response!");
}

// after destructuring
export default async function ({ req, res, log, error }: any) {
log("This is a log!");
return res.send("This is a response!");
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>// before destructuring
export default async function (context: any) {
context.log("This is a log!");
return context.res.send("This is a response!");
}

// after destructuring
export default async function ({ req, res, log, error }: any) {
log("This is a log!");
Expand Down Expand Up @@ -756,6 +809,25 @@ public class Main {
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ req, res, log }: any) => {
log(req.bodyRaw); // Raw request body, contains request data
log(JSON.stringify(req.body)); // Object from parsed JSON request body, otherwise string
log(JSON.stringify(req.headers)); // String key-value pairs of all request headers, keys are lowercase
log(req.scheme); // Value of the x-forwarded-proto header, usually http or https
log(req.method); // Request method, such as GET, POST, PUT, DELETE, PATCH, etc.
log(req.url); // Full URL, for example: http://awesome.appwrite.io:8000/v1/hooks?limit=12&offset=50
log(req.host); // Hostname from the host header, such as awesome.appwrite.io
log(req.port); // Port from the host header, for example 8000
log(req.path); // Path part of URL, for example /v1/hooks
log(req.queryString); // Raw query params string. For example "limit=12&offset=50"
log(JSON.stringify(req.query)); // Parsed query params. For example, req.query.limit

return res.send("All the request parameters are logged to the Appwrite Console.");</code></pre>
</div>
</li>
</ul>

<h3><a href="#headers" id="headers">Headers</a></h3>
Expand Down Expand Up @@ -1091,6 +1163,29 @@ namespace runtime {
}
}
};
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ req, res, log }) => {

switch (req.query.type) {
case 'empty':
return res.empty();
case 'json':
return res.json({type": "This is a JSON response"});
christyjacob4 marked this conversation as resolved.
Show resolved Hide resolved
case 'redirect':
return res.redirect("https://appwrite.io", 301);
case 'html':
return res.send(
"&lt;h1&gt;This is an HTML response&lt;/h1&gt;", 200, {
"content-type": "text/html"
});
default:
return res.send("This is a text response");
}
}</code></pre>
</div>
</li>
Expand Down Expand Up @@ -1312,6 +1407,18 @@ namespace runtime {
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ res, log, error }: any) => {
log("This is a log, use for logging information to console");
log(`This function was called with ${context.req.method} method`);
christyjacob4 marked this conversation as resolved.
Show resolved Hide resolved
error("This is an error, use for logging errors to console");

return res.send("Check the Appwrite Console to see logs and errors!");
};</code></pre>
</div>
</li>
</ul>
<p>You can access these logs through the following steps.</p>
<ol class="margin-top margin-bottom-large text-size-normal">
Expand Down Expand Up @@ -1535,6 +1642,14 @@ namespace runtime {

return context.res.send(std::getenv("MY_VAR"));
};
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>export default async ({ req, res, log }) => {
return res.send(Bun.env.get('MY_VAR'));
}</code></pre>
</div>
</li>
Expand Down Expand Up @@ -1593,7 +1708,7 @@ namespace runtime {
</td>
<td><b>Deno</b></td>
<td><a href="https://deno.land/" target="_blank">deno</a></td>
<td><code>deno cache &ltENTRYPOINT_FILE&gt</code></td>
<td><code>deno install</code></td>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this right? I can't seem to find this 🧐

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</tr>
<tr>
<td>
Expand All @@ -1619,6 +1734,14 @@ namespace runtime {
<td><a href="https://www.nuget.org/" target="_blank">NuGet</a></td>
<td><code>dotnet restore</code></td>
</tr>
<tr>
<td>
<img src="" data-ls-attrs="src=/images/runtimes/bun.png" alt="Bun icon" class="avatar xxs" />
</td>
<td><b>Bun</b></td>
<td><a href="https://bun.sh/package-manager" target="_blank">bun</a></td>
<td><code>bun install</code></td>
</tr>
<tr>
<td>
<img src="" data-ls-attrs="src=/images/runtimes/kotlin.png" alt="Swift icon" class="avatar xxs" />
Expand Down Expand Up @@ -1997,6 +2120,35 @@ public class Main {

return context.res.send("Document created");
}
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>import { Client, Databases, ID } from 'node-appwrite';

export default function ({req, res, error}: any){
const client = new Client()
.setEndpoint("https://cloud.appwrite.io/v1")
.setProject(Bun.env.get("APPWRITE_FUNCTION_PROJECT_ID") || "")
.setKey(Bun.env.get("APPWRITE_API_KEY") || "");

const databases = new Databases(client);

try {
databases.createDocument(
"[DATABASE_ID]",
"[COLLECTION_ID]",
ID.unique(),
{}
);
} catch (e) {
error("Failed to create document: " + e.message);
return res.send("Failed to create document");
}

return res.send("Document created");
}</code></pre>
</div>
</li>
Expand Down Expand Up @@ -2387,6 +2539,40 @@ public class Main {
return context.res.send("Document created");

}
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>import { Client, Databases, ID } from 'node-appwrite';

export default function ({req, res, error}: any){
const client = new Client()
.setEndpoint("https://cloud.appwrite.io/v1")
.setProject(Bun.env.get("APPWRITE_FUNCTION_PROJECT_ID") || "")

if (req.headers["x-appwrite-user-jwt"]) {
client.setJWT(req.headers["x-appwrite-user-jwt"]);
} else {
return res.send("Please sign in, JWT not found");
}

const databases = new Databases(client);

try {
databases.createDocument(
"[DATABASE_ID]",
"[COLLECTION_ID]",
ID.unique(),
{}
);
} catch (e) {
error("Failed to create document: " + e.message)
return res.send("Failed to create document");
}

return res.send("Document created");
}</code></pre>
</div>
</li>
Expand Down Expand Up @@ -2620,6 +2806,25 @@ public class Main {
public RuntimeOutput main(RuntimeContext context) throws Exception {
return context.res.send(Utils.add(1, 2));
}
}</code></pre>
</div>
</li>
<li>
<h3>Bun</h3>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>// src/utils.ts

export function add(a: number, b: number): number {
return a + b;
}</code></pre>
</div>
<div class="ide margin-top-small" data-lang="typescript" data-lang-label="Bun">
<pre class="line-numbers"><code class="prism language-typescript" data-prism>// src/main.ts

import { add } from './utils.ts';

export default function ({res}: {res: any}) {
return res.send(add(1, 2));
}</code></pre>
</div>
</li>
Expand Down