JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 44
  • Score
    100M100P100Q49181F
  • 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.

you can find example code on github: https://github.com/Ota-Prokopec/appwrite-ssr-example

Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Web SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to https://appwrite.io/docs

🛠️ Installation Steps:

npm i appwrite-ssr
  or
yarn add appwrite-ssr

import appwrite-ssr

import appwrite from 'appwrite-ssr'

set Appwrite project and hostname of your server (you can also set apiKey if you want to use admin actions - it is optional)

import appwrite from 'appwrite-ssr'

appwrite.setProject({
    endPoint: 'https://cloud.appwrite.io/v1',
    projectId: 'your-project-id',
    hostname: 'your-server-hostname',
    apiKey: 'your-api-key',
})

how to begin?

Connect user to your Appwrite by setting users session, or use setAdmin to act like admin

// passing users session
const { Collection } = appwrite.setSession('users session here')
//authorizate by passing users cookies
const { Collection } = appwriteSSR.setCookie([{ name: '', value: '' }])
//dont authorizate user - use this for signing user into app
const { Collection } = appwriteSSR.none()
//act as admin (use your apiKey)
const { Collection } = appwrite.setAdmin()

Auth

log user into your application using email

const { account } = appwrite.none()
const { sessionLegacyToken, sessionToken } = await account.loginViaEmail('email', 'password')
event.cookies.set(sessionToken.name, sessionToken.value)

log user into your application using oAuth2

on client side use:

import { account } from 'appwrite' // https://www.npmjs.com/package/appwrite
await account.createOAuth2Session(platform, `${location.origin}/auth/oauth2/success`, `${location.origin}/oauth2/failure`)

on server side use:

//the path for this function has to be /auth/oauth2/success (strictly)
import appwrite from 'appwrite-ssr'

appwrite.setProject({
    endPoint: 'https://cloud.appwrite.io/v1',
    projectId: 'your-project-id',
    hostname: 'your-server-hostname',
    apiKey: 'your-api-key',
})
const { account } = appwrite.none()
const url = 'whole URL'
const { sessionLegacyToken, sessionToken } = await account.oauth2Login(url)
event.cookies.set(sessionToken.name, sessionToken.value)

log user out or your application

import appwrite from 'appwrite-ssr'

appwrite.setProject({
    endPoint: 'https://cloud.appwrite.io/v1',
    projectId: 'your-project-id',
    hostname: 'your-server-hostname',
    apiKey: 'your-api-key',
})
const { account } = appwrite.none()
const { sessionLegacyToken, sessionToken } = await account.logOut()
event.cookies.delete(sessionToken.name)

create your own instance

import type { Types } from 'appwrite-ssr'
import appwriteSSR, { Query } from 'appwrite-ssr'

export const appwrite = appwriteSSR.setProject({
    projectId: process.env.APPWRITE_PROJECT_ID,
    hostname: 'localhost',
    endpoint: process.env.APPWRITE_ENDPOINT,
    apiKey: process.env.APPWRITE_API_KEY,
})

example code (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 { Collection } = appwrite.setCookie(cookies)

const collectionGrass = new Collection<GrassDocumentGet, GrassDocumentCreate>('your-database-id', 'your-collection-id')

const grassQuery = Query<GrassDocumentGet>()

const query = grassQuery.equal('grassName', 'nameOfGrass')
const res = await collectionGrass.getDocument([query])

example code (buckets)

import appwrite, { permissions, type Types } from 'appwrite-ssr'

const { Bucket } = appwrite
    .setProject({
        endPoint: 'https://cloud.appwrite.io/v1',
        projectId: 'fads',
    })
    .setCookie(cookies)

const bucketGrass = new Bucket('myBucketId')

return { bucketGrass }

const buckets = setCookie(event.cookies.getAll())
const base64 = ''
const res = bucketGrass.createFile(base64, permissions.owner('myUserId'))

used packages/technologies:

Appwrite

🛡️ License:

This project is licensed under the MIT

💖 Any questions?

otaprokopec@gmail.com