Skip to content

fix: improve code snippets for product pages #1693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/routes/products/auth/(components)/(snippets)/astro.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Client, Account } from 'node-appwrite'

async function getLoggedInUser(context) {
const session = cookies().get('custom-session-cookie');
if (!session) return;

const client = new Client()
.setEndpoint(import.meta.env.PUBLIC_APPWRITE_ENDPOINT)
.setProject(import.meta.env.PUBLIC_APPWRITE_PROJECT_ID);

client.setSession(session.value);
const account = new Account(client);

return account.get()
}
16 changes: 16 additions & 0 deletions src/routes/products/auth/(components)/(snippets)/next.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Client, Account } from 'node-appwrite'
import { cookies } from 'next/headers'

async function getLoggedInUser() {
const session = cookies().get('custom-session-cookie');
if (!session) return;

const client = new Client()
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID);

client.setSession(session.value);
const account = new Account(client);

return account.get();
}
16 changes: 16 additions & 0 deletions src/routes/products/auth/(components)/(snippets)/nuxt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Client, Account } from 'node-appwrite';
import { H3Event, getCookie } from 'h3';

async function getLoggedInUser(event) {
const session = getCookie(event, 'custom-session-cookie');
if (!session) return;

const client = new Client()
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID);

client.setSession(session.value);
const account = new Account(client);

return account.get();
}
21 changes: 21 additions & 0 deletions src/routes/products/auth/(components)/(snippets)/remix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Client, Account } from 'node-appwrite';
import { createCookie } from '@remix-run/node';

export const customSessionCookie = createCookie('custom-session-cookie', {
maxAge: 604800,
})

async function getLoggedInUser(request) {
const cookies = request.headers.get('Cookie');
const session = await customSessionCookie.parse(cookies);
if (!session) return;

const client = new Client()
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID);

client.setSession(session.value);
const account = new Account(client);

return account.get();
}
15 changes: 15 additions & 0 deletions src/routes/products/auth/(components)/(snippets)/sveltekit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Client, Account } from 'node-appwrite';

async function getLoggedInUser() {
const session = cookies().get('custom-session-cookie');
if (!session) return;

const client = new Client()
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID);

client.setSession(session.value);
const account = new Account(client);

return account.get();
}
97 changes: 10 additions & 87 deletions src/routes/products/auth/(components)/SSR.svelte
Original file line number Diff line number Diff line change
@@ -1,116 +1,39 @@
<script lang="ts">
import { PUBLIC_APPWRITE_DASHBOARD } from '$env/static/public';
import { trackEvent } from '$lib/actions/analytics';
import { Tooltip } from '$lib/components';
import { Framework, Platform } from '$lib/utils/references';
import MultiFrameworkCode from './MultiFrameworkCode.svelte';
import NextSnippet from './(snippets)/next.txt?raw';
import SvelteSnippet from './(snippets)/sveltekit.txt?raw';
import AstroSnippet from './(snippets)/astro.txt?raw';
import NuxtSnippet from './(snippets)/nuxt.txt?raw';
import RemixSnippet from './(snippets)/remix.txt?raw';

const codeSnippets = [
{
language: Platform.ClientWeb,
platform: Framework.NextJs,
content: `import { Client, Account } from 'node-appwrite'
import { cookies } from 'next/headers'

async function getLoggedInUser() {
const session = cookies().get("custom-session-cookie");
if (!session) return

const client = new Client()
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID)

client.setSession(session.value)
const account = new Account(client);

return await account.get()
}
`
content: NextSnippet
},
{
language: Platform.ClientWeb,
platform: Framework.SvelteKit,
content: `import { Client, Account } from 'node-appwrite'

async function getLoggedInUser() {
const session = cookies().get("custom-session-cookie");
if (!session) return

const client = new Client()
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID)

client.setSession(session.value)
const account = new Account(client);

return await account.get()
}
`
content: SvelteSnippet
},
{
language: Platform.ClientWeb,
platform: Framework.Astro,
content: `import { Client, Account } from 'node-appwrite'

async function getLoggedInUser(context) {
const session = cookies().get("custom-session-cookie");
if (!session) return

const client = new Client()
.setEndpoint(import.meta.env.PUBLIC_APPWRITE_ENDPOINT)
.setProject(import.meta.env.PUBLIC_APPWRITE_PROJECT_ID)

client.setSession(session.value)
const account = new Account(client);

return await account.get()
}
`
content: AstroSnippet
},
{
language: Platform.ClientWeb,
platform: Framework.Nuxt3,
content: `import { Client, Account } from 'node-appwrite'
import { H3Event, getCookie } from 'h3'

async function getLoggedInUser(event) {
const session = getCookie(event, "custom-session-cookie");
if (!session) return

const client = new Client()
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID)

client.setSession(session.value)
const account = new Account(client);

return await account.get()
}`
content: NuxtSnippet
},
{
language: Platform.ClientWeb,
platform: Framework.Remix,
content: `import { Client, Account } from 'node-appwrite'
import { createCookie } from '@remix-run/node'

export const customSessionCookie = createCookie("custom-session-cookie", {
maxAge: 604800,
})

async function getLoggedInUser(request) {
const cookies = request.headers.get("Cookie")
const session = await customSessionCookie.parse(cookies)
if (!session) return

const client = new Client()
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID)

client.setSession(session.value)
const account = new Account(client);

return await account.get()
}`
content: RemixSnippet
}
];

Expand Down
42 changes: 42 additions & 0 deletions src/routes/products/functions/(components)/(snippets)/csharp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace DotNetRuntime;

using Appwrite;
using Appwrite.Services;
using Appwrite.Models;

public class Handler {

// This is your Appwrite function
// It"s executed each time we get a request
public async Task Main(RuntimeContext Context)
{
// Why not try the Appwrite SDK?
//
// Set project and set API key
// var client = new Client()
// .SetProject(Environment.GetEnvironmentVariable("APPWRITE_FUNCTION_PROJECT_ID"))
// .SetKey(Context.Req.Headers["x-appwrite-key"]);

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

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

// The 'Context.Req' object contains the request data
if (Context.Req.Method == "GET") {
// Send a response with the res object helpers
// 'Context.Res.Text()' dispatches a string back to the client
return Context.Res.Text("Hello, World!");
}

// 'Context.Res.Json()' is a handy helper for sending JSON
return Context.Res.Json(new Dictionary()
{
{ "motto", "Build like a team of hundreds_" },
{ "learn", "https://appwrite.io/docs" },
{ "connect", "https://appwrite.io/discord" },
{ "getInspired", "https://builtwith.appwrite.io" },
});
}
}
35 changes: 35 additions & 0 deletions src/routes/products/functions/(components)/(snippets)/dart.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'dart:async';
import 'package:dart_appwrite/dart_appwrite.dart';

// This is your Appwrite function
// It's executed each time we get a request
Future main(final context) async {
// Why not try the Appwrite SDK?
//
// Set project and set API key
// final client = Client()
// .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID'])
// .setKey(context.req.headers['x-appwrite-key']);


// You can log messages to the console
context.log('Hello, Logs!');

// If something goes wrong, log an error
context.error('Hello, Errors!');

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

// 'res.json()' is a handy helper for sending JSON
return context.res.json({
'motto': 'Build like a team of hundreds_',
'learn': 'https://appwrite.io/docs',
'connect': 'https://appwrite.io/discord',
'getInspired': 'https://builtwith.appwrite.io',
});
}
33 changes: 33 additions & 0 deletions src/routes/products/functions/(components)/(snippets)/deno.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Client } from "https://deno.land/x/[email protected]/mod.ts";

// This is your Appwrite function
// It's executed each time we get a request
export default ({ req, res, log, error }: any) => {
// Why not try the Appwrite SDK?
//
// Set project and set API key
// const client = new Client()
// .setProject(Deno.env.get("APPWRITE_FUNCTION_PROJECT_ID") || "")
// .setKey(req.headers["x-appwrite-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.text()' dispatches a string back to the client
return res.text("Hello, World!");
}

// 'res.json()' is a handy helper for sending JSON
return res.json({
motto: "Build like a team of hundreds_",
learn: "https://appwrite.io/docs",
connect: "https://appwrite.io/discord",
getInspired: "https://builtwith.appwrite.io",
});
};
45 changes: 45 additions & 0 deletions src/routes/products/functions/(components)/(snippets)/go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package handler

import (
"fmt"
"os"
"github.com/appwrite/sdk-for-go/appwrite"
"github.com/open-runtimes/types-for-go/v4/openruntimes"
)

type Response struct {
Motto string 'json:"motto"'
Learn string 'json:"learn"'
Connect string 'json:"connect"'
GetInspired string 'json:"getInspired"'
}

func Main(Context openruntimes.Context) openruntimes.Response {
// This is your Appwrite function
// It's executed each time we get a request service
var _ = appwrite.NewClient(
appwrite.WithProject(os.Getenv("APPWRITE_FUNCTION_PROJECT_ID")),
appwrite.WithKey(Context.Req.Headers["x-appwrite-key"]),
)

// You can log messages to the console
fmt.Println("Hello, Logs!")

fmt.Fprintln(os.Stderr, "Error:", "Hello, Errors!")

// The 'Context.Req' object contains the request data
if Context.Req.Method == "GET" {
// Send a response with the Context.Res object helpers
// 'Context.Res.Text()' dispatches a string back to the client
return Context.Res.Text("Hello, World!")
}

// 'res.json()' is a handy helper for sending JSON
return Context.Res.Json(
Response{
Motto: "Build like a team of hundreds_",
Learn: "https://appwrite.io/docs",
Connect: "https://appwrite.io/discord",
GetInspired: "https://builtwith.appwrite.io",
})
}
Loading
Loading