Add: Success object

This commit is contained in:
Donatas Kirda 2024-05-16 12:25:00 +03:00
parent 4975c0b34f
commit 5d2c9c5f9a
Signed by: bloodwiing
GPG Key ID: 63020D8D3F4A164F
5 changed files with 18 additions and 16 deletions

View File

@ -68,7 +68,7 @@ export async function getCategories(category_ids = undefined) {
/**
* @param {number} category_id
* @returns {Promise<Category | import('$types/error').Error>}
* @returns {Promise<Category | import('$types/status').Error>}
*/
export async function getCategoryCached(category_id) {
const categories = await getCategoriesCached([category_id]);

View File

@ -96,7 +96,7 @@ export async function getPosts(opts = {}) {
* @param {{
* withMetrics?: boolean
* }} opts
* @returns {Promise<Post | import('$types/error').Error>}
* @returns {Promise<Post | import('$types/status').Error>}
*/
export async function getPost(post_id, opts = {}) {
const {

View File

@ -1,6 +1,8 @@
import { createCache } from '$lib/cache.server';
import { genSalt } from 'bcrypt';
import { cacheUpdater, cachedMethod, refExtendCachedMethod } from './root';
import { sql } from '$lib/db.server';
import { argon2id, hash, verify } from 'argon2';
import { PostgresError } from 'postgres';
const cache = createCache();
@ -32,7 +34,6 @@ function parseUserFromRow(row) {
const updateUserCache = cacheUpdater(cache);
/**
* @param {import('postgres').Sql} sql
* @param {number[]} user_ids
* @returns {Promise<Result<User>>}
*/
@ -41,11 +42,10 @@ export const getUsersCached = cachedMethod(cache, getUsers);
export const getUsersCachedByRef = refExtendCachedMethod(getUsersCached);
/**
* @param {import('postgres').Sql} sql
* @param {number[]} user_ids
* @returns {Promise<Result<User>>}
*/
export async function getUsers(sql, user_ids) {
export async function getUsers(user_ids) {
if (user_ids.length == 0) return new Map();
const query = sql`
@ -68,12 +68,11 @@ export async function getUsers(sql, user_ids) {
}
/**
* @param {import('postgres').Sql} sql
* @param {number} user_id
* @returns {Promise<User | import('$types/error').Error>}
* @returns {Promise<User | import('$types/status').Error>}
*/
export async function getUser(sql, user_id) {
const users = await getUsers(sql, [user_id]);
export async function getUser(user_id) {
const users = await getUsers([user_id]);
return users.get(user_id) || {
error: true,
@ -82,10 +81,9 @@ export async function getUser(sql, user_id) {
}
/**
* @param {import('postgres').Sql} sql
* @param {string} username
* @param {string} password
* @returns {Promise<string | import('$types/error').Error>}
* @returns {Promise<import('$types/status').Success | import('$types/status').Error>}
*/
export async function registerUser(sql, username, password) {
// const users = await getUsers(sql, [user_id]);

View File

@ -1,4 +0,0 @@
export type Error = {
error: boolean,
msg: string
};

8
src/types/status.ts Normal file
View File

@ -0,0 +1,8 @@
export type Error = {
error: boolean,
msg: string,
};
export type Success = {
success: boolean,
};