website/src/lib/index.ts

29 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-10-01 02:44:39 +03:00
import type { Cookies } from '@sveltejs/kit'
2024-10-01 04:59:41 +03:00
import { env } from '$env/dynamic/private'
2024-10-01 04:34:07 +03:00
import { writable } from 'svelte/store'
import { existsSync, readFileSync } from 'fs'
2024-10-30 16:33:08 +03:00
import { Agent, CredentialSession } from '@atproto/api'
2024-10-01 02:44:39 +03:00
export const scopeCookies = (cookies: Cookies, path: string) => {
return {
get: (key: string) => {
return cookies.get(key)
},
set: (key: string, value: string, props: import('cookie').CookieSerializeOptions = {}) => {
cookies.set(key, value, { ...props, path })
},
delete: (key: string, props: import('cookie').CookieSerializeOptions = {}) => {
cookies.delete(key, { ...props, path })
}
}
2024-10-01 04:34:07 +03:00
}
2024-10-01 04:59:41 +03:00
export const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount`
export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0'));
2024-10-30 16:33:08 +03:00
export const loginToBsky = async () => {
2024-10-30 16:33:08 +03:00
const creds = new CredentialSession(new URL("https://bsky.social"))
2024-11-02 00:53:21 +03:00
await creds.login({ identifier: 'gaze.systems', password: env.BSKY_PASSWORD ?? "" })
2024-10-30 16:33:08 +03:00
return new Agent(creds)
}
2024-11-02 00:05:10 +03:00
export const bskyClient = writable<null | Agent>(null)