Initial conversion to react-basics.

This commit is contained in:
Mike Cao 2022-11-21 22:32:55 -08:00
parent c0a18e13fa
commit 2259ee8d76
10 changed files with 724 additions and 609 deletions

14
lib/client.ts Normal file
View file

@ -0,0 +1,14 @@
import { getItem, setItem, removeItem } from 'next-basics';
import { AUTH_TOKEN } from './constants';
export function getAuthToken() {
return getItem(AUTH_TOKEN);
}
export function setAuthToken(token) {
setItem(AUTH_TOKEN, token);
}
export function removeAuthToken() {
removeItem(AUTH_TOKEN);
}

View file

@ -3,7 +3,7 @@ import debug from 'debug';
import cors from 'cors';
import { validate } from 'uuid';
import { findSession } from 'lib/session';
import { parseShareToken, getAuthToken } from 'lib/auth';
import { getAuthToken, parseShareToken } from 'lib/auth';
import { secret } from 'lib/crypto';
import redis from 'lib/redis';
import { getUser } from '../queries';

View file

@ -1,39 +1,39 @@
import { createClient } from 'redis';
import debug from 'debug';
import Redis from 'ioredis';
import { REDIS } from 'lib/db';
const log = debug('umami:redis');
export const DELETED = 'deleted';
const REDIS = Symbol();
let redis;
const enabled = Boolean(process.env.REDIS_URL);
function getClient() {
if (!enabled) {
async function getClient() {
if (!process.env.REDIS_URL) {
return null;
}
const redis = new Redis(process.env.REDIS_URL, {
retryStrategy(times) {
log(`Redis reconnecting attempt: ${times}`);
return 5000;
},
});
const client = createClient({ url: process.env.REDIS_URL });
client.on('error', err => log(err));
await client.connect();
if (process.env.NODE_ENV !== 'production') {
global[REDIS] = redis;
global[REDIS] = client;
}
log('Redis initialized');
return redis;
return client;
}
async function get(key) {
await connect();
const data = await redis.get(key);
log({ key, data });
try {
return JSON.parse(await redis.get(key));
return JSON.parse(data);
} catch {
return null;
}
@ -53,7 +53,7 @@ async function del(key) {
async function connect() {
if (!redis && enabled) {
redis = global[REDIS] || getClient();
redis = global[REDIS] || (await getClient());
}
return redis;