JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16
  • Score
    100M100P100Q49450F
  • License MIT

Create Appwrite apps with your server-side rendering

Package Exports

  • appwrite-ssr
  • appwrite-ssr/src/index.ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (appwrite-ssr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

appwrite-ssr

appwrite-ssr will help you to create ssr applications with appwrite.

🛠️ Installation Steps:

1. download appwrite-ssr

npm i appwrite-ssr
  or
yarn add appwrite-ssr

2. import appwrite-ssr

import appwriteSSR from "appwrite-ssr";

3. set up envirenment variables. Variables have to be APPWRITE_ENDPOINT and APPWRITE_PROJECT_ID or you can set your appwrite endpoint and project id in code.

APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1 APPWRITE_PROJECT_ID="jfdajfjajůlaj"
import appwrite, { Query } from 'appwrite-ssr'

appwrite.setProject({ projectEndPoint: 'https://cloud.appwrite.io/v1', projectId: 'experiences' })

4. set users session into appwriteSSR

appwriteSSR.setSession(); OR appwriteSSR.setCookie(cookies); OR appwriteSSR.none()

5. log user into your application

const { account } = appwriteSSR.none()
const { sessionLegacyToken, sessionToken } = await account.logInViaEmail(email, password)

6. initialize buckets

import appwriteSSR from '@app/appwrite-ssr'

const { Bucket } = appwriteSSR.setSession(session)

const bucket = new Bucket('bucket id')

bucket.getParamsFromURL(url)
bucket.createFile()

7. initialize collections

  import type {
    UserInfoDocument,
    UserInfoDocumentCreate,
  } from '@app/ts-types'

    import appwriteSSR from '@app/appwrite-ssr'

    const { Collection } = appwriteSSR.setSession(session)

    const userInfo =new Collection<UserInfoDocument, UserInfoDocumentCreate>('account', 'userInfo'),
    userInfo.createDocument({}, [userId])

example code with Sveltekit (collections)

import type { PageServerLoad } from './$types'
import type { Types } from 'appwrite-ssr'
import appwrite, { Query } from 'appwrite-ssr'

export type DocumentSkeleton = {
    $id: string
    $collectionId: string
    $databaseId: string
    $createdAt: string
    $updatedAt: string
    $permissions: string[]
}

export type Document<T extends Partial<DocumentSkeleton> & object> = {
    [Key in keyof T]: T[Key] extends Record<string, unknown> ? Document<T[Key]> : T[Key]
} & DocumentSkeleton

type GrassDocumentGet = Document<{
    grassName: string
    grassOptionalDescription: string //optional value with default value
    grassEnumValue: null | 'value' | 'value2'
}>
type GrassDocumentCreate = {
    grassName: string
    grassOptionalDescription?: string
    grassEnumValue: null | 'value' | 'value2'
}

// key2 is optional but there is always a default value

const setCookie = (cookies: Types.Cookie[]) => {
    const { Collection } = appwrite
        .setProject({
            projectEndPoint: 'https://cloud.appwrite.io/v1',
            projectId: '6526fb26efda65491a0e',
        })
        .setCookie(cookies)

    const collectionGrass = new Collection<GrassDocumentGet, GrassDocumentCreate>('experiences', 'userInfo')
    return { collectionGrass }
}

const grassQuery = Query<GrassDocumentGet>()

export const load: PageServerLoad = async (event) => {
    const cookies: Types.Cookie[] = event.cookies.getAll()
    const collections = setCookie(cookies)
    const result = await collections.collectionGrass.getDocument([grassQuery.equal('grassName', 'jj')])
    return { result }
}

example code with Sveltekit (buckets)

import appwrite, { permissions, type Types } from 'appwrite-ssr'
import type { PageServerLoad } from './$types'

const setCookie = (cookies: Types.Cookie[]) => {
    const { Bucket } = appwrite
        .setProject({
            projectEndPoint: 'https://cloud.appwrite.io/v1',
            projectId: '6526fb26efda65491a0e',
        })
        .setCookie(cookies)

    const bucketGrass = new Bucket('myBucketId')

    return { bucketGrass }
}

export const load: PageServerLoad = async (event) => {
    const buckets = setCookie(event.cookies.getAll())
    const base64 = ''
    const res = buckets.bucketGrass.createFile(base64, permissions.owner('myUserId'))
    return res
}

example code with Sveltekit (auth)

import appwrite from 'appwrite-ssr'

const app = appwrite.setProject({
    projectEndPoint: 'https://cloud.appwrite.io/v1',
    projectId: '6526fb26efda65491a0e',
})

const login = async (email: string, pass: string) => {
    const { account } = app.none()
    const { sessionLegacyToken, sessionToken } = await account.loginViaEmail(email, pass)
    // store sessions in cookies
}

const auth = async (cookies: [{ mySession: string }]) => {
    try {
        const session = cookies.at(0)?.mySession
        if (!session) throw new Error('session is not provided')
        const { account } = app.setSession(session) // or setCookies(cookies)
        const user = await account.get()
        console.log(`user: ${user.$id}`)
    } catch (error) {
        throw new Error('user is not authenticated')
    }
}

used packages/technologies:

appwrite

🛡️ License:

This project is licensed under the MIT

💖 Any questions?

otaprokopec@gmail.com