Compare commits

...

3 Commits

Author SHA1 Message Date
c992579931 fix api route 2025-01-15 06:01:48 +01:00
5c0e3577a8 always return a response 2025-01-14 03:24:24 +01:00
831bdb8faa fix inconsistent no-response 2025-01-14 03:21:08 +01:00
2 changed files with 33 additions and 20 deletions

View File

@ -24,7 +24,7 @@ import {
SqliteQueryInterface, SqliteQueryInterface,
} from "@sequelize/sqlite3"; } from "@sequelize/sqlite3";
import { hashpassword } from "@/util/auth"; import { hashpassword } from "@/util/auth";
import { copyFile, mkdirSync, readFileSync } from "fs"; import { copyFileSync, mkdirSync, readFileSync } from "fs";
import path from "path"; import path from "path";
import { UUID } from "crypto"; import { UUID } from "crypto";
import { runApiAction } from "@/util/api"; import { runApiAction } from "@/util/api";
@ -76,8 +76,8 @@ async function seedPosts(qif: SqliteQueryInterface<SqliteDialect>) {
], ],
}, },
]; ];
projects.map((project) => { projects.map(async (project) => {
Project.create({ await Project.create({
name: project.name, name: project.name,
readableIdentifier: project.readableIdentifier, readableIdentifier: project.readableIdentifier,
}); });
@ -90,19 +90,19 @@ async function seedPosts(qif: SqliteQueryInterface<SqliteDialect>) {
user_id: post.user_id, user_id: post.user_id,
}); });
post.buckets.map(async (bucket) => { post.buckets.map(async (bucket) => {
pst.createBucket({ id: bucket.id as UUID }); await pst.createBucket({ id: bucket.id as UUID });
bucket.attachments.map((attachment) => { bucket.attachments.map(async (attachment) => {
Attachment.create({ await Attachment.create({
bucket_id: bucket.id as UUID, bucket_id: bucket.id as UUID,
filename: attachment.filename, filename: attachment.filename,
}).then((a) => { }).then(async (a) => {
console.log(bucket.id); console.log(bucket.id);
mkdirSync(`./bucket/${bucket.id}/`, { recursive: true }); mkdirSync(`./bucket/${bucket.id}/`, { recursive: true });
copyFile( copyFileSync(
path.resolve(".", "db", "seed", "posts", a.filename), path.resolve(".", "db", "seed", "posts", a.filename),
path.resolve(".", "bucket", bucket.id, a.filename), path.resolve(".", "bucket", bucket.id, a.filename)
() => {}
); );
return;
}); });
}); });
}); });
@ -114,19 +114,19 @@ async function seedPosts(qif: SqliteQueryInterface<SqliteDialect>) {
readableIdentifier: "blog", readableIdentifier: "blog",
}, },
}).then( }).then(
(project) => async (project) =>
project || project ||
Project.create({ (await Project.create({
name: "General Blog", name: "General Blog",
readableIdentifier: "blog", readableIdentifier: "blog",
}) }))
); );
} }
async function seedDatabase(qif: SqliteQueryInterface<SqliteDialect>) { async function seedDatabase(qif: SqliteQueryInterface<SqliteDialect>) {
await seedUsers(qif); await seedUsers(qif);
await seedPosts(qif); await seedPosts(qif);
return;
// await Post.create({ // await Post.create({
// title: 'Test Post', // title: 'Test Post',
// content: ` // content: `
@ -147,8 +147,8 @@ async function seedDatabase(qif: SqliteQueryInterface<SqliteDialect>) {
// }) // })
} }
export async function GET(request: Request) { export async function tryGET(request: Request) {
runApiAction(async (request) => { return await runApiAction(async (request) => {
await dbSync; await dbSync;
const queryInterface = sequelize.queryInterface; const queryInterface = sequelize.queryInterface;
@ -161,19 +161,18 @@ export async function GET(request: Request) {
case 1: case 1:
break; break;
default: default:
seedDatabase(queryInterface); await seedDatabase(queryInterface);
const postsRows: SqliteColumnsDescription = await queryInterface const postsRows: SqliteColumnsDescription = await queryInterface
.describeTable(Post.table.tableName) .describeTable(Post.table.tableName)
.then((t) => t); .then((t) => t);
if (!postsRows["project_id"]) if (!postsRows["project_id"])
queryInterface.addColumn("Posts", "project_id", { await queryInterface.addColumn("Posts", "project_id", {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
acceptsNull: () => false, acceptsNull: () => false,
defaultValue: 1, defaultValue: 1,
}); });
break; break;
} }
console.log("pp");
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
test: await queryInterface.describeTable("Posts").then((t) => t), test: await queryInterface.describeTable("Posts").then((t) => t),
@ -187,3 +186,16 @@ export async function GET(request: Request) {
); );
}, request); }, request);
} }
export async function GET(request: Request) {
try {
return await tryGET(request);
} catch (e) {
if (e instanceof APIError) {
return new Response(e.info.responseText, { status: e.info.status });
} else {
return new Response(JSON.stringify(e));
throw e;
}
}
}

View File

@ -13,11 +13,12 @@ import "@/app/index.css"
import { Post } from "@/models"; import { Post } from "@/models";
import { Attributes } from "@sequelize/core"; import { Attributes } from "@sequelize/core";
import { DeepPartial } from "@/util/deeppartial"; import { DeepPartial } from "@/util/deeppartial";
import { constructAPIUrl } from "@/util/url/urlConstructor";
async function getData(slug:string):Promise<Attributes<Post>> { async function getData(slug:string):Promise<Attributes<Post>> {
// Get all posts from the API // Get all posts from the API
const res = await fetch(`http://localhost:3000/api/post/${slug}`); const res = await fetch(await constructAPIUrl(`post/${slug}`));
// The return value is *not* serialized // The return value is *not* serialized
// You can return Date, Map, Set, etc. // You can return Date, Map, Set, etc.
// Recommendation: handle errors // Recommendation: handle errors