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,
} from "@sequelize/sqlite3";
import { hashpassword } from "@/util/auth";
import { copyFile, mkdirSync, readFileSync } from "fs";
import { copyFileSync, mkdirSync, readFileSync } from "fs";
import path from "path";
import { UUID } from "crypto";
import { runApiAction } from "@/util/api";
@ -76,8 +76,8 @@ async function seedPosts(qif: SqliteQueryInterface<SqliteDialect>) {
],
},
];
projects.map((project) => {
Project.create({
projects.map(async (project) => {
await Project.create({
name: project.name,
readableIdentifier: project.readableIdentifier,
});
@ -90,19 +90,19 @@ async function seedPosts(qif: SqliteQueryInterface<SqliteDialect>) {
user_id: post.user_id,
});
post.buckets.map(async (bucket) => {
pst.createBucket({ id: bucket.id as UUID });
bucket.attachments.map((attachment) => {
Attachment.create({
await pst.createBucket({ id: bucket.id as UUID });
bucket.attachments.map(async (attachment) => {
await Attachment.create({
bucket_id: bucket.id as UUID,
filename: attachment.filename,
}).then((a) => {
}).then(async (a) => {
console.log(bucket.id);
mkdirSync(`./bucket/${bucket.id}/`, { recursive: true });
copyFile(
copyFileSync(
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",
},
}).then(
(project) =>
async (project) =>
project ||
Project.create({
(await Project.create({
name: "General Blog",
readableIdentifier: "blog",
})
}))
);
}
async function seedDatabase(qif: SqliteQueryInterface<SqliteDialect>) {
await seedUsers(qif);
await seedPosts(qif);
return;
// await Post.create({
// title: 'Test Post',
// content: `
@ -147,8 +147,8 @@ async function seedDatabase(qif: SqliteQueryInterface<SqliteDialect>) {
// })
}
export async function GET(request: Request) {
runApiAction(async (request) => {
export async function tryGET(request: Request) {
return await runApiAction(async (request) => {
await dbSync;
const queryInterface = sequelize.queryInterface;
@ -161,19 +161,18 @@ export async function GET(request: Request) {
case 1:
break;
default:
seedDatabase(queryInterface);
await seedDatabase(queryInterface);
const postsRows: SqliteColumnsDescription = await queryInterface
.describeTable(Post.table.tableName)
.then((t) => t);
if (!postsRows["project_id"])
queryInterface.addColumn("Posts", "project_id", {
await queryInterface.addColumn("Posts", "project_id", {
type: DataTypes.INTEGER,
acceptsNull: () => false,
defaultValue: 1,
});
break;
}
console.log("pp");
return new Response(
JSON.stringify({
test: await queryInterface.describeTable("Posts").then((t) => t),
@ -187,3 +186,16 @@ export async function GET(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 { Attributes } from "@sequelize/core";
import { DeepPartial } from "@/util/deeppartial";
import { constructAPIUrl } from "@/util/url/urlConstructor";
async function getData(slug:string):Promise<Attributes<Post>> {
// 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
// You can return Date, Map, Set, etc.
// Recommendation: handle errors