create-fate 1.0.4 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/bin/create-fate.mjs +594 -9
- package/package.json +2 -2
- package/templates/fate/README.md +3 -0
- package/templates/fate/_shared/vue-app/package.json +54 -0
- package/templates/fate/_shared/vue-app/pages/index.vue +57 -0
- package/templates/fate/_shared/vue-app/pages/layout.vue +32 -0
- package/templates/fate/_shared/vue-app/pages/post/[id].vue +24 -0
- package/templates/fate/_shared/vue-app/src/App.css +63 -0
- package/templates/fate/_shared/vue-app/src/fateViews.ts +14 -0
- package/templates/fate/_shared/vue-app/src/lib/cx.ts +6 -0
- package/templates/fate/_shared/vue-app/src/lib/env.ts +3 -0
- package/templates/fate/_shared/vue-app/src/ui/Button.vue +67 -0
- package/templates/fate/_shared/vue-app/src/ui/Card.vue +16 -0
- package/templates/fate/_shared/vue-app/src/ui/Error.vue +9 -0
- package/templates/fate/_shared/vue-app/src/ui/ErrorBoundary.vue +22 -0
- package/templates/fate/_shared/vue-app/src/ui/Header.vue +35 -0
- package/templates/fate/_shared/vue-app/src/ui/Link.vue +15 -0
- package/templates/fate/_shared/vue-app/src/ui/PostCard.vue +29 -0
- package/templates/fate/_shared/vue-app/src/ui/Section.vue +19 -0
- package/templates/fate/_shared/vue-app/src/ui/Thinking.vue +11 -0
- package/templates/fate/_shared/vue-app/src/ui/UserCard.vue +23 -0
- package/templates/fate/_shared/vue-app/src/vue-shims.d.ts +6 -0
- package/templates/fate/_shared/vue-app/tsconfig.json +22 -0
- package/templates/fate/_shared/vue-app/vite.config.ts +58 -0
- package/templates/fate/_shared/vue-app/void.json +5 -0
- package/templates/fate/cloudflare/.github/workflows/push.yml +27 -0
- package/templates/fate/cloudflare/.vite-hooks/pre-commit +1 -0
- package/templates/fate/cloudflare/.vscode/extensions.json +12 -0
- package/templates/fate/cloudflare/.vscode/settings.json +25 -0
- package/templates/fate/cloudflare/AGENTS.md +55 -0
- package/templates/fate/cloudflare/README.md +126 -0
- package/templates/fate/cloudflare/_gitignore +20 -0
- package/templates/fate/cloudflare/client/package.json +56 -0
- package/templates/fate/cloudflare/client/pages/index.tsx +96 -0
- package/templates/fate/cloudflare/client/pages/layout.tsx +82 -0
- package/templates/fate/cloudflare/client/pages/login.tsx +107 -0
- package/templates/fate/cloudflare/client/pages/post/[id].tsx +22 -0
- package/templates/fate/cloudflare/client/pages/search.tsx +99 -0
- package/templates/fate/cloudflare/client/public/icon.svg +1 -0
- package/templates/fate/cloudflare/client/src/App.css +153 -0
- package/templates/fate/cloudflare/client/src/lib/AvailableLanguages.tsx +5 -0
- package/templates/fate/cloudflare/client/src/lib/cx.tsx +6 -0
- package/templates/fate/cloudflare/client/src/lib/env.tsx +5 -0
- package/templates/fate/cloudflare/client/src/ui/Badge.tsx +31 -0
- package/templates/fate/cloudflare/client/src/ui/Button.tsx +108 -0
- package/templates/fate/cloudflare/client/src/ui/Card.tsx +32 -0
- package/templates/fate/cloudflare/client/src/ui/CommentCard.tsx +75 -0
- package/templates/fate/cloudflare/client/src/ui/Error.tsx +19 -0
- package/templates/fate/cloudflare/client/src/ui/H2.tsx +15 -0
- package/templates/fate/cloudflare/client/src/ui/H3.tsx +20 -0
- package/templates/fate/cloudflare/client/src/ui/Header.tsx +98 -0
- package/templates/fate/cloudflare/client/src/ui/Input.tsx +17 -0
- package/templates/fate/cloudflare/client/src/ui/Link.tsx +11 -0
- package/templates/fate/cloudflare/client/src/ui/LocaleSwitcher.tsx +52 -0
- package/templates/fate/cloudflare/client/src/ui/PostCard.tsx +303 -0
- package/templates/fate/cloudflare/client/src/ui/Section.tsx +23 -0
- package/templates/fate/cloudflare/client/src/ui/UserCard.tsx +131 -0
- package/templates/fate/cloudflare/client/src/user/AuthClient.tsx +6 -0
- package/templates/fate/cloudflare/client/translations/de_DE.json +689 -0
- package/templates/fate/cloudflare/client/translations/ja_JP.json +689 -0
- package/templates/fate/cloudflare/client/vite.config.ts +45 -0
- package/templates/fate/cloudflare/client/void.json +29 -0
- package/templates/fate/cloudflare/package.json +46 -0
- package/templates/fate/cloudflare/pnpm-workspace.yaml +39 -0
- package/templates/fate/cloudflare/server/.env.example +1 -0
- package/templates/fate/cloudflare/server/db/db.ts +30 -0
- package/templates/fate/cloudflare/server/db/migrations/20260508073357_initial_schema.sql +133 -0
- package/templates/fate/cloudflare/server/db/migrations/20260508120500_seed_cloudflare_demo.sql +170 -0
- package/templates/fate/cloudflare/server/db/migrations/meta/20260508073357_snapshot.json +868 -0
- package/templates/fate/cloudflare/server/db/migrations/meta/_journal.json +20 -0
- package/templates/fate/cloudflare/server/db/queries.ts +211 -0
- package/templates/fate/cloudflare/server/db/schema.ts +332 -0
- package/templates/fate/cloudflare/server/package.json +34 -0
- package/templates/fate/cloudflare/server/src/fate/commentPermissions.ts +17 -0
- package/templates/fate/cloudflare/server/src/fate/context.ts +15 -0
- package/templates/fate/cloudflare/server/src/fate/live.ts +7 -0
- package/templates/fate/cloudflare/server/src/fate/server.ts +411 -0
- package/templates/fate/cloudflare/server/src/fate/views.ts +164 -0
- package/templates/fate/cloudflare/server/src/index.ts +80 -0
- package/templates/fate/cloudflare/server/src/lib/auth.ts +66 -0
- package/templates/fate/cloudflare/server/src/lib/origins.ts +17 -0
- package/templates/fate/cloudflare/server/src/router.ts +2 -0
- package/templates/fate/cloudflare/server/src/user/SessionUser.tsx +17 -0
- package/templates/fate/cloudflare/server/tsconfig.json +10 -0
- package/templates/fate/cloudflare/server/wrangler.jsonc +29 -0
- package/templates/fate/cloudflare/tsconfig.json +33 -0
- package/templates/fate/cloudflare/vite.config.ts +72 -0
- package/templates/fate/drizzle/README.md +2 -8
- package/templates/fate/drizzle/client/pages/layout.tsx +2 -2
- package/templates/fate/drizzle/client/src/ui/PostCard.tsx +38 -15
- package/templates/fate/drizzle/pnpm-workspace.yaml +9 -3
- package/templates/fate/drizzle/server/vite.config.ts +1 -1
- package/templates/fate/graphql/.github/workflows/push.yml +27 -0
- package/templates/fate/graphql/.vite-hooks/pre-commit +1 -0
- package/templates/fate/graphql/.vscode/extensions.json +12 -0
- package/templates/fate/graphql/.vscode/settings.json +25 -0
- package/templates/fate/graphql/AGENTS.md +55 -0
- package/templates/fate/graphql/README.md +121 -0
- package/templates/fate/graphql/_gitignore +23 -0
- package/templates/fate/graphql/client/package.json +57 -0
- package/templates/fate/graphql/client/pages/index.tsx +96 -0
- package/templates/fate/graphql/client/pages/layout.tsx +83 -0
- package/templates/fate/graphql/client/pages/login.tsx +107 -0
- package/templates/fate/graphql/client/pages/post/[id].tsx +22 -0
- package/templates/fate/graphql/client/pages/search.tsx +99 -0
- package/templates/fate/graphql/client/public/icon.svg +1 -0
- package/templates/fate/graphql/client/src/App.css +153 -0
- package/templates/fate/graphql/client/src/lib/AvailableLanguages.tsx +5 -0
- package/templates/fate/graphql/client/src/lib/cx.tsx +6 -0
- package/templates/fate/graphql/client/src/lib/env.tsx +5 -0
- package/templates/fate/graphql/client/src/ui/Badge.tsx +31 -0
- package/templates/fate/graphql/client/src/ui/Button.tsx +108 -0
- package/templates/fate/graphql/client/src/ui/Card.tsx +32 -0
- package/templates/fate/graphql/client/src/ui/CommentCard.tsx +75 -0
- package/templates/fate/graphql/client/src/ui/Error.tsx +19 -0
- package/templates/fate/graphql/client/src/ui/H2.tsx +15 -0
- package/templates/fate/graphql/client/src/ui/H3.tsx +20 -0
- package/templates/fate/graphql/client/src/ui/Header.tsx +98 -0
- package/templates/fate/graphql/client/src/ui/Input.tsx +17 -0
- package/templates/fate/graphql/client/src/ui/Link.tsx +11 -0
- package/templates/fate/graphql/client/src/ui/LocaleSwitcher.tsx +52 -0
- package/templates/fate/graphql/client/src/ui/PostCard.tsx +303 -0
- package/templates/fate/graphql/client/src/ui/Section.tsx +23 -0
- package/templates/fate/graphql/client/src/ui/UserCard.tsx +131 -0
- package/templates/fate/graphql/client/src/user/AuthClient.tsx +6 -0
- package/templates/fate/graphql/client/translations/de_DE.json +689 -0
- package/templates/fate/graphql/client/translations/ja_JP.json +689 -0
- package/templates/fate/graphql/client/vite.config.ts +40 -0
- package/templates/fate/graphql/client/void.json +29 -0
- package/templates/fate/graphql/docker-compose.yml +11 -0
- package/templates/fate/graphql/package.json +47 -0
- package/templates/fate/graphql/pnpm-workspace.yaml +45 -0
- package/templates/fate/graphql/server/.env.example +5 -0
- package/templates/fate/graphql/server/README.md +27 -0
- package/templates/fate/graphql/server/package.json +64 -0
- package/templates/fate/graphql/server/prisma.config.ts +17 -0
- package/templates/fate/graphql/server/scripts/generate-graphql.tsx +40 -0
- package/templates/fate/graphql/server/src/__tests__/index.test.tsx +5 -0
- package/templates/fate/graphql/server/src/app.tsx +47 -0
- package/templates/fate/graphql/server/src/graphql/builder.tsx +121 -0
- package/templates/fate/graphql/server/src/graphql/context.tsx +22 -0
- package/templates/fate/graphql/server/src/graphql/fate.ts +85 -0
- package/templates/fate/graphql/server/src/graphql/lib/authDirectives.tsx +93 -0
- package/templates/fate/graphql/server/src/graphql/lib/decodeGlobalID.tsx +12 -0
- package/templates/fate/graphql/server/src/graphql/lib/decodeIDOrThrow.tsx +10 -0
- package/templates/fate/graphql/server/src/graphql/lib/encodeGlobalID.tsx +5 -0
- package/templates/fate/graphql/server/src/graphql/live.tsx +241 -0
- package/templates/fate/graphql/server/src/graphql/mutations.tsx +265 -0
- package/templates/fate/graphql/server/src/graphql/nodes/Category.tsx +35 -0
- package/templates/fate/graphql/server/src/graphql/nodes/Comment.tsx +46 -0
- package/templates/fate/graphql/server/src/graphql/nodes/Event.tsx +71 -0
- package/templates/fate/graphql/server/src/graphql/nodes/Post.tsx +45 -0
- package/templates/fate/graphql/server/src/graphql/nodes/Tag.tsx +11 -0
- package/templates/fate/graphql/server/src/graphql/nodes/User.tsx +28 -0
- package/templates/fate/graphql/server/src/graphql/schema.tsx +16 -0
- package/templates/fate/graphql/server/src/graphql/views.ts +193 -0
- package/templates/fate/graphql/server/src/index.tsx +45 -0
- package/templates/fate/graphql/server/src/lib/auth.tsx +31 -0
- package/templates/fate/graphql/server/src/lib/env.tsx +3 -0
- package/templates/fate/graphql/server/src/lib/origins.ts +18 -0
- package/templates/fate/graphql/server/src/prisma/prisma.tsx +9 -0
- package/templates/fate/graphql/server/src/prisma/schema.prisma +181 -0
- package/templates/fate/graphql/server/src/prisma/seed.tsx +161 -0
- package/templates/fate/graphql/server/src/user/SessionUser.tsx +17 -0
- package/templates/fate/graphql/server/src/user/isAdmin.tsx +3 -0
- package/templates/fate/graphql/server/tsconfig.json +33 -0
- package/templates/fate/graphql/server/vite.config.ts +55 -0
- package/templates/fate/graphql/tsconfig.json +33 -0
- package/templates/fate/graphql/vite.config.ts +80 -0
- package/templates/fate/graphql-client/.env.example +3 -0
- package/templates/fate/graphql-client/AGENTS.md +3 -0
- package/templates/fate/graphql-client/README.md +69 -0
- package/templates/fate/graphql-client/_gitignore +14 -0
- package/templates/fate/graphql-client/package.json +62 -0
- package/templates/fate/graphql-client/pages/index.tsx +69 -0
- package/templates/fate/graphql-client/pages/layout.tsx +41 -0
- package/templates/fate/graphql-client/pnpm-workspace.yaml +38 -0
- package/templates/fate/graphql-client/public/icon.svg +8 -0
- package/templates/fate/graphql-client/src/App.css +28 -0
- package/templates/fate/graphql-client/src/fate/graphql.ts +52 -0
- package/templates/fate/graphql-client/tsconfig.json +30 -0
- package/templates/fate/graphql-client/vite.config.ts +87 -0
- package/templates/fate/graphql-client/void.json +29 -0
- package/templates/fate/http/README.md +2 -8
- package/templates/fate/http/client/pages/layout.tsx +2 -2
- package/templates/fate/http/client/src/ui/PostCard.tsx +37 -15
- package/templates/fate/http/pnpm-workspace.yaml +9 -3
- package/templates/fate/http/server/vite.config.ts +1 -1
- package/templates/fate/prisma/README.md +2 -8
- package/templates/fate/prisma/client/pages/layout.tsx +2 -2
- package/templates/fate/prisma/client/src/ui/PostCard.tsx +37 -15
- package/templates/fate/prisma/pnpm-workspace.yaml +9 -3
- package/templates/fate/prisma/server/vite.config.ts +1 -1
- package/templates/fate/prisma/vite.config.ts +1 -1
- package/templates/fate/void/package.json +10 -2
- package/templates/fate/void/pnpm-workspace.yaml +12 -4
- package/templates/fate/void/src/ui/CategoryCard.tsx +3 -7
- package/templates/fate/void/src/ui/PostCard.tsx +38 -15
- package/templates/fate/void/vite.config.ts +5 -0
- package/templates/fate/drizzle/public/fate-logo-dark.svg +0 -4
- package/templates/fate/drizzle/public/fate-logo.svg +0 -4
- package/templates/fate/http/public/fate-logo-dark.svg +0 -4
- package/templates/fate/http/public/fate-logo.svg +0 -4
- package/templates/fate/prisma/public/fate-logo-dark.svg +0 -4
- package/templates/fate/prisma/public/fate-logo.svg +0 -4
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createFateServer,
|
|
3
|
+
createSourcePlan,
|
|
4
|
+
FateRequestError,
|
|
5
|
+
getNestedSelection,
|
|
6
|
+
getScopedArgs,
|
|
7
|
+
hasNestedSelection,
|
|
8
|
+
} from '@nkzw/fate/server';
|
|
9
|
+
import { createDrizzleSourceAdapter } from '@nkzw/fate/server/drizzle';
|
|
10
|
+
import { createCloudflareFateLive } from 'cf-fate/server';
|
|
11
|
+
import { z } from 'zod';
|
|
12
|
+
import { db, eq, like } from '../../db/db.ts';
|
|
13
|
+
import {
|
|
14
|
+
type CommentItem,
|
|
15
|
+
type PostItem,
|
|
16
|
+
createCommentRecord,
|
|
17
|
+
createPostRecord,
|
|
18
|
+
deleteCommentRecord,
|
|
19
|
+
likePostRecord,
|
|
20
|
+
unlikePostRecord,
|
|
21
|
+
} from '../../db/queries.ts';
|
|
22
|
+
import schema, { comment, user as userTable } from '../../db/schema.ts';
|
|
23
|
+
import { assertCanDeleteComment, getCommentDeleteFetchSelection } from './commentPermissions.ts';
|
|
24
|
+
import { createContext, type AppContext } from './context.ts';
|
|
25
|
+
import { commentDataView, postDataView, Root, userDataView, type Post } from './views.ts';
|
|
26
|
+
|
|
27
|
+
type ResolverOptions<Input> = {
|
|
28
|
+
ctx: AppContext;
|
|
29
|
+
input: Input;
|
|
30
|
+
select: Array<string>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type ListResolverOptions = ResolverOptions<{
|
|
34
|
+
args?: Record<string, unknown>;
|
|
35
|
+
}>;
|
|
36
|
+
|
|
37
|
+
const source = createDrizzleSourceAdapter<AppContext>({
|
|
38
|
+
db,
|
|
39
|
+
schema,
|
|
40
|
+
views: Root,
|
|
41
|
+
});
|
|
42
|
+
const isDevelopment = Boolean(
|
|
43
|
+
(globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env?.DEV,
|
|
44
|
+
);
|
|
45
|
+
export const fateLive = createCloudflareFateLive();
|
|
46
|
+
export const { live } = fateLive;
|
|
47
|
+
|
|
48
|
+
type CloudflareAdapterContext = {
|
|
49
|
+
env?: {
|
|
50
|
+
BETTER_AUTH_SECRET?: string;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const requireUser = (ctx: AppContext) => {
|
|
55
|
+
if (!ctx.sessionUser) {
|
|
56
|
+
throw new FateRequestError('UNAUTHORIZED', 'You must be logged in.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return ctx.sessionUser;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const fateServer = createFateServer({
|
|
63
|
+
context: ({ adapterContext, request }) =>
|
|
64
|
+
createContext({
|
|
65
|
+
request,
|
|
66
|
+
secret: (adapterContext as CloudflareAdapterContext | undefined)?.env?.BETTER_AUTH_SECRET,
|
|
67
|
+
}),
|
|
68
|
+
lists: {
|
|
69
|
+
commentSearch: {
|
|
70
|
+
resolve: async ({ ctx, input, select }: ListResolverOptions) => {
|
|
71
|
+
const query = input.args?.query;
|
|
72
|
+
if (typeof query !== 'string' || !query.trim().length) {
|
|
73
|
+
return {
|
|
74
|
+
items: [],
|
|
75
|
+
pagination: { hasNext: false, hasPrevious: false },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (isDevelopment && query.trim().length > 1) {
|
|
80
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const first = typeof input.args?.first === 'number' ? input.args.first : 20;
|
|
84
|
+
const cursor = typeof input.args?.after === 'string' ? input.args.after : undefined;
|
|
85
|
+
const nodes = await source.resolveConnection({
|
|
86
|
+
ctx,
|
|
87
|
+
cursor,
|
|
88
|
+
direction: 'forward',
|
|
89
|
+
extra: {
|
|
90
|
+
where: like(comment.content, `%${query.trim()}%`),
|
|
91
|
+
},
|
|
92
|
+
input: { args: input.args, select },
|
|
93
|
+
skip: cursor ? 1 : undefined,
|
|
94
|
+
take: first + 1,
|
|
95
|
+
view: commentDataView,
|
|
96
|
+
});
|
|
97
|
+
const hasNext = nodes.length > first;
|
|
98
|
+
const items = nodes.slice(0, first).map((node) => ({
|
|
99
|
+
cursor: String(node.id),
|
|
100
|
+
node,
|
|
101
|
+
}));
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
items,
|
|
105
|
+
pagination: {
|
|
106
|
+
hasNext,
|
|
107
|
+
hasPrevious: Boolean(cursor),
|
|
108
|
+
nextCursor: items.at(-1)?.cursor,
|
|
109
|
+
previousCursor: cursor ? items[0]?.cursor : undefined,
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
type: 'Comment',
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
live,
|
|
117
|
+
mutations: {
|
|
118
|
+
'comment.add': {
|
|
119
|
+
input: z.object({
|
|
120
|
+
content: z.string().min(1, 'Content is required'),
|
|
121
|
+
postId: z.string().min(1, 'Post id is required'),
|
|
122
|
+
}),
|
|
123
|
+
resolve: async ({
|
|
124
|
+
ctx,
|
|
125
|
+
input,
|
|
126
|
+
select,
|
|
127
|
+
}: ResolverOptions<{ content: string; postId: string }>) => {
|
|
128
|
+
const user = requireUser(ctx);
|
|
129
|
+
const post = await source.resolveById({
|
|
130
|
+
ctx,
|
|
131
|
+
id: input.postId,
|
|
132
|
+
input: { select: ['id'] },
|
|
133
|
+
view: postDataView,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
if (!post) {
|
|
137
|
+
throw new Error('Post not found.');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const commentId = await createCommentRecord({
|
|
141
|
+
authorId: user.id,
|
|
142
|
+
content: input.content,
|
|
143
|
+
postId: input.postId,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
if (!commentId) {
|
|
147
|
+
throw new Error('Failed to create comment.');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const created = (await source.resolveById({
|
|
151
|
+
ctx,
|
|
152
|
+
id: commentId,
|
|
153
|
+
input: {
|
|
154
|
+
select: select.filter((field) => field !== 'post' && !field.startsWith('post.')),
|
|
155
|
+
},
|
|
156
|
+
view: commentDataView,
|
|
157
|
+
})) as (CommentItem & { post?: PostItem | null }) | null;
|
|
158
|
+
|
|
159
|
+
if (!created) {
|
|
160
|
+
throw new Error('Comment not found.');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const liveComment = await source.resolveById({
|
|
164
|
+
ctx,
|
|
165
|
+
id: commentId,
|
|
166
|
+
input: {
|
|
167
|
+
select: ['author.id', 'author.name', 'author.username', 'content', 'id'],
|
|
168
|
+
},
|
|
169
|
+
view: commentDataView,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
if (hasNestedSelection(select, 'post')) {
|
|
173
|
+
created.post = (await source.resolveById<PostItem>({
|
|
174
|
+
ctx,
|
|
175
|
+
id: input.postId,
|
|
176
|
+
input: {
|
|
177
|
+
args: getScopedArgs(undefined, 'post'),
|
|
178
|
+
select: getNestedSelection(select, 'post'),
|
|
179
|
+
},
|
|
180
|
+
view: postDataView,
|
|
181
|
+
})) as PostItem | null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
live
|
|
185
|
+
.connection('Post.comments', { id: input.postId })
|
|
186
|
+
.appendNode('Comment', commentId, { node: liveComment ?? created });
|
|
187
|
+
live.update('Post', input.postId, { changed: ['commentCount', 'comments'] });
|
|
188
|
+
|
|
189
|
+
return created as CommentItem & { post?: { commentCount: number } };
|
|
190
|
+
},
|
|
191
|
+
type: 'Comment',
|
|
192
|
+
},
|
|
193
|
+
'comment.delete': {
|
|
194
|
+
input: z.object({
|
|
195
|
+
id: z.string().min(1, 'Comment id is required'),
|
|
196
|
+
}),
|
|
197
|
+
resolve: async ({ ctx, input, select }: ResolverOptions<{ id: string }>) => {
|
|
198
|
+
const plan = createSourcePlan({
|
|
199
|
+
ctx,
|
|
200
|
+
select,
|
|
201
|
+
source: source.getSource(commentDataView),
|
|
202
|
+
});
|
|
203
|
+
const commentSelection = getCommentDeleteFetchSelection(select);
|
|
204
|
+
|
|
205
|
+
const existing = await source.fetchById<CommentItem>({
|
|
206
|
+
ctx,
|
|
207
|
+
extra: { extraFields: ['authorId', 'postId'] },
|
|
208
|
+
id: input.id,
|
|
209
|
+
plan: createSourcePlan({
|
|
210
|
+
ctx,
|
|
211
|
+
select: commentSelection,
|
|
212
|
+
source: source.getSource(commentDataView),
|
|
213
|
+
}),
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
if (!existing) {
|
|
217
|
+
throw new FateRequestError('NOT_FOUND', 'Comment not found.');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
assertCanDeleteComment(existing, ctx.sessionUser);
|
|
221
|
+
|
|
222
|
+
await deleteCommentRecord(input.id);
|
|
223
|
+
|
|
224
|
+
if (existing.postId && hasNestedSelection(select, 'post')) {
|
|
225
|
+
const post = await source.fetchById<PostItem>({
|
|
226
|
+
ctx,
|
|
227
|
+
id: existing.postId,
|
|
228
|
+
plan: createSourcePlan({
|
|
229
|
+
args: getScopedArgs(undefined, 'post'),
|
|
230
|
+
ctx,
|
|
231
|
+
select: getNestedSelection(select, 'post'),
|
|
232
|
+
source: source.getSource(postDataView),
|
|
233
|
+
}),
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
existing.post = post;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const resolved = (await plan.resolve(existing)) as CommentItem & {
|
|
240
|
+
post?: { commentCount: number };
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
if (existing.postId) {
|
|
244
|
+
live.connection('Post.comments', { id: existing.postId }).deleteEdge('Comment', input.id);
|
|
245
|
+
live.update('Post', existing.postId, { changed: ['commentCount', 'comments'] });
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return resolved;
|
|
249
|
+
},
|
|
250
|
+
type: 'Comment',
|
|
251
|
+
},
|
|
252
|
+
'post.add': {
|
|
253
|
+
input: z.object({
|
|
254
|
+
content: z.string().min(1, 'Content is required'),
|
|
255
|
+
title: z.string().min(1, 'Title is required'),
|
|
256
|
+
}),
|
|
257
|
+
resolve: async ({
|
|
258
|
+
ctx,
|
|
259
|
+
input,
|
|
260
|
+
select,
|
|
261
|
+
}: ResolverOptions<{ content: string; title: string }>) => {
|
|
262
|
+
const user = requireUser(ctx);
|
|
263
|
+
const postId = await createPostRecord({
|
|
264
|
+
authorId: user.id,
|
|
265
|
+
content: input.content,
|
|
266
|
+
title: input.title,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
if (!postId) {
|
|
270
|
+
throw new Error('Failed to create post.');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const post = await source.resolveById({
|
|
274
|
+
ctx,
|
|
275
|
+
id: postId,
|
|
276
|
+
input: { select },
|
|
277
|
+
view: postDataView,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
if (!post) {
|
|
281
|
+
throw new Error('Post not found.');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
live.connection('posts').prependNode('Post', postId);
|
|
285
|
+
|
|
286
|
+
return post as Post;
|
|
287
|
+
},
|
|
288
|
+
type: 'Post',
|
|
289
|
+
},
|
|
290
|
+
'post.like': {
|
|
291
|
+
input: z.object({
|
|
292
|
+
error: z.enum(['boundary', 'callSite']).optional(),
|
|
293
|
+
id: z.string().min(1, 'Post id is required.'),
|
|
294
|
+
slow: z.boolean().optional(),
|
|
295
|
+
}),
|
|
296
|
+
resolve: async ({
|
|
297
|
+
ctx,
|
|
298
|
+
input,
|
|
299
|
+
select,
|
|
300
|
+
}: ResolverOptions<{ error?: 'boundary' | 'callSite'; id: string; slow?: boolean }>) => {
|
|
301
|
+
if (input.slow) {
|
|
302
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (input.error === 'boundary') {
|
|
306
|
+
throw new Error('Simulated error.');
|
|
307
|
+
} else if (input.error === 'callSite') {
|
|
308
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
309
|
+
throw new Error('Gotta pay up.');
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const updated = await likePostRecord(input.id);
|
|
313
|
+
if (!updated) {
|
|
314
|
+
throw new Error('Post not found.');
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const post = await source.resolveById({
|
|
318
|
+
ctx,
|
|
319
|
+
id: input.id,
|
|
320
|
+
input: { select },
|
|
321
|
+
view: postDataView,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
if (!post) {
|
|
325
|
+
throw new Error('Post not found.');
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
live.update('Post', input.id, { changed: ['likes'], data: post });
|
|
329
|
+
|
|
330
|
+
return post as Post;
|
|
331
|
+
},
|
|
332
|
+
type: 'Post',
|
|
333
|
+
},
|
|
334
|
+
'post.unlike': {
|
|
335
|
+
input: z.object({
|
|
336
|
+
id: z.string().min(1, 'Post id is required.'),
|
|
337
|
+
}),
|
|
338
|
+
resolve: async ({ ctx, input, select }: ResolverOptions<{ id: string }>) => {
|
|
339
|
+
const updated = await unlikePostRecord(input.id);
|
|
340
|
+
if (!updated) {
|
|
341
|
+
throw new Error('Post not found.');
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const post = await source.resolveById({
|
|
345
|
+
ctx,
|
|
346
|
+
id: input.id,
|
|
347
|
+
input: { select },
|
|
348
|
+
view: postDataView,
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
if (!post) {
|
|
352
|
+
throw new Error('Post not found.');
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
live.update('Post', input.id, { changed: ['likes'], data: post });
|
|
356
|
+
|
|
357
|
+
return post as Post;
|
|
358
|
+
},
|
|
359
|
+
type: 'Post',
|
|
360
|
+
},
|
|
361
|
+
'user.update': {
|
|
362
|
+
input: z.object({
|
|
363
|
+
name: z
|
|
364
|
+
.string()
|
|
365
|
+
.trim()
|
|
366
|
+
.min(2, 'Name must be at least 2 characters.')
|
|
367
|
+
.max(50, 'Name must be at most 32 characters.'),
|
|
368
|
+
}),
|
|
369
|
+
resolve: async ({ ctx, input, select }: ResolverOptions<{ name: string }>) => {
|
|
370
|
+
const user = requireUser(ctx);
|
|
371
|
+
|
|
372
|
+
await db
|
|
373
|
+
.update(userTable)
|
|
374
|
+
.set({ name: input.name, updatedAt: new Date() })
|
|
375
|
+
.where(eq(userTable.id, user.id));
|
|
376
|
+
|
|
377
|
+
const updated = await source.resolveById({
|
|
378
|
+
ctx,
|
|
379
|
+
id: user.id,
|
|
380
|
+
input: { select },
|
|
381
|
+
view: userDataView,
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
if (!updated) {
|
|
385
|
+
throw new Error('User not found.');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return updated;
|
|
389
|
+
},
|
|
390
|
+
type: 'User',
|
|
391
|
+
},
|
|
392
|
+
},
|
|
393
|
+
queries: {
|
|
394
|
+
viewer: {
|
|
395
|
+
resolve: ({ ctx, select }: Omit<ResolverOptions<unknown>, 'input'>) =>
|
|
396
|
+
ctx.sessionUser
|
|
397
|
+
? source.resolveById({
|
|
398
|
+
ctx,
|
|
399
|
+
id: ctx.sessionUser.id,
|
|
400
|
+
input: { select },
|
|
401
|
+
view: Root.viewer,
|
|
402
|
+
})
|
|
403
|
+
: null,
|
|
404
|
+
type: 'User',
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
roots: Root,
|
|
408
|
+
sources: source,
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
export * from './views.ts';
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { computed, count, dataView, field, list, type Entity } from '@nkzw/fate/server';
|
|
2
|
+
import type {
|
|
3
|
+
CategoryItem,
|
|
4
|
+
CommentItem,
|
|
5
|
+
EventAttendeeItem,
|
|
6
|
+
EventItem,
|
|
7
|
+
PostItem,
|
|
8
|
+
} from '../../db/queries.ts';
|
|
9
|
+
import type { CategoryRow, TagRow, UserRow } from '../../db/schema.ts';
|
|
10
|
+
import type { AppContext } from './context.ts';
|
|
11
|
+
|
|
12
|
+
export const userDataView = dataView<UserRow>('User')({
|
|
13
|
+
email: computed<UserRow, string | null, AppContext>({
|
|
14
|
+
authorize: ({ id }, context) => context?.sessionUser?.id === id,
|
|
15
|
+
resolve: (_item, deps) => (deps.email as string | null) ?? null,
|
|
16
|
+
select: {
|
|
17
|
+
email: field('email'),
|
|
18
|
+
},
|
|
19
|
+
}),
|
|
20
|
+
id: true,
|
|
21
|
+
name: true,
|
|
22
|
+
username: true,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const tagDataView = dataView<TagRow>('Tag')({
|
|
26
|
+
description: true,
|
|
27
|
+
id: true,
|
|
28
|
+
name: true,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export const categorySummaryDataView = dataView<CategoryRow>('Category')({
|
|
32
|
+
id: true,
|
|
33
|
+
name: true,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const basePost = {
|
|
37
|
+
author: userDataView,
|
|
38
|
+
category: categorySummaryDataView,
|
|
39
|
+
commentCount: computed<PostItem, number>({
|
|
40
|
+
resolve: (_item, deps) => (deps.count as number) ?? 0,
|
|
41
|
+
select: {
|
|
42
|
+
count: count('comments'),
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
content: true,
|
|
46
|
+
id: true,
|
|
47
|
+
likes: true,
|
|
48
|
+
title: true,
|
|
49
|
+
} as const;
|
|
50
|
+
|
|
51
|
+
export const postSummaryDataView = dataView<PostItem>('Post')({
|
|
52
|
+
...basePost,
|
|
53
|
+
tags: list(tagDataView),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export const commentDataView = dataView<CommentItem>('Comment')({
|
|
57
|
+
author: userDataView,
|
|
58
|
+
content: true,
|
|
59
|
+
id: true,
|
|
60
|
+
post: postSummaryDataView,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export const postDataView = dataView<PostItem>('Post')({
|
|
64
|
+
...basePost,
|
|
65
|
+
comments: list(commentDataView, { orderBy: { createdAt: 'asc', id: 'asc' } }),
|
|
66
|
+
tags: list(tagDataView),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export const categoryDataView = dataView<CategoryItem>('Category')({
|
|
70
|
+
description: true,
|
|
71
|
+
id: true,
|
|
72
|
+
name: true,
|
|
73
|
+
postCount: computed<CategoryItem, number>({
|
|
74
|
+
resolve: (_item, deps) => (deps.count as number) ?? 0,
|
|
75
|
+
select: {
|
|
76
|
+
count: count('posts'),
|
|
77
|
+
},
|
|
78
|
+
}),
|
|
79
|
+
posts: list(postDataView),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
export const eventAttendeeDataView = dataView<EventAttendeeItem>('EventAttendee')({
|
|
83
|
+
id: true,
|
|
84
|
+
notes: true,
|
|
85
|
+
status: true,
|
|
86
|
+
user: userDataView,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
export const eventDataView = dataView<EventItem>('Event')({
|
|
90
|
+
attendees: list(eventAttendeeDataView, { orderBy: { createdAt: 'asc', id: 'asc' } }),
|
|
91
|
+
attendingCount: computed<EventItem, number>({
|
|
92
|
+
resolve: (_item, deps) => (deps.count as number) ?? 0,
|
|
93
|
+
select: {
|
|
94
|
+
count: count('attendees', {
|
|
95
|
+
where: { status: 'GOING' },
|
|
96
|
+
}),
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
capacity: true,
|
|
100
|
+
description: true,
|
|
101
|
+
endAt: true,
|
|
102
|
+
host: userDataView,
|
|
103
|
+
id: true,
|
|
104
|
+
livestreamUrl: true,
|
|
105
|
+
location: true,
|
|
106
|
+
name: true,
|
|
107
|
+
startAt: true,
|
|
108
|
+
topics: true,
|
|
109
|
+
type: true,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
export type User = Entity<typeof userDataView, 'User'>;
|
|
113
|
+
export type Tag = Entity<typeof tagDataView, 'Tag'>;
|
|
114
|
+
export type Comment = Entity<
|
|
115
|
+
typeof commentDataView,
|
|
116
|
+
'Comment',
|
|
117
|
+
{
|
|
118
|
+
author: User;
|
|
119
|
+
post: Post;
|
|
120
|
+
}
|
|
121
|
+
>;
|
|
122
|
+
export type EventAttendee = Entity<
|
|
123
|
+
typeof eventAttendeeDataView,
|
|
124
|
+
'EventAttendee',
|
|
125
|
+
{
|
|
126
|
+
user: User;
|
|
127
|
+
}
|
|
128
|
+
>;
|
|
129
|
+
export type Post = Entity<
|
|
130
|
+
typeof postDataView,
|
|
131
|
+
'Post',
|
|
132
|
+
{
|
|
133
|
+
author: User;
|
|
134
|
+
category: Category | null;
|
|
135
|
+
comments: Array<Comment>;
|
|
136
|
+
tags: Array<Tag>;
|
|
137
|
+
}
|
|
138
|
+
>;
|
|
139
|
+
export type Category = Entity<
|
|
140
|
+
typeof categoryDataView,
|
|
141
|
+
'Category',
|
|
142
|
+
{
|
|
143
|
+
posts: Array<Post>;
|
|
144
|
+
}
|
|
145
|
+
>;
|
|
146
|
+
export type Event = Entity<
|
|
147
|
+
typeof eventDataView,
|
|
148
|
+
'Event',
|
|
149
|
+
{
|
|
150
|
+
attendees: Array<EventAttendee>;
|
|
151
|
+
host: User;
|
|
152
|
+
}
|
|
153
|
+
>;
|
|
154
|
+
|
|
155
|
+
export const Root = {
|
|
156
|
+
categories: list(categoryDataView, { orderBy: { createdAt: 'asc', id: 'asc' } }),
|
|
157
|
+
commentSearch: {
|
|
158
|
+
procedure: 'search',
|
|
159
|
+
view: list(commentDataView, { orderBy: { createdAt: 'desc', id: 'desc' } }),
|
|
160
|
+
},
|
|
161
|
+
events: list(eventDataView, { orderBy: [{ startAt: 'asc' }, { id: 'asc' }] }),
|
|
162
|
+
posts: list(postDataView, { orderBy: { createdAt: 'desc', id: 'desc' } }),
|
|
163
|
+
viewer: userDataView,
|
|
164
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCloudflareFateLiveDurableObject,
|
|
3
|
+
defineCloudflareFateLiveRoute,
|
|
4
|
+
defineCloudflareFateRoute,
|
|
5
|
+
} from 'cf-fate/server';
|
|
6
|
+
import { withDatabase, type D1Binding } from '../db/db.ts';
|
|
7
|
+
import { fateStream } from './fate/live.ts';
|
|
8
|
+
import { fateLive, fateServer } from './fate/server.ts';
|
|
9
|
+
import { createAuth } from './lib/auth.ts';
|
|
10
|
+
import { clientOrigin, resolveCorsOrigin } from './lib/origins.ts';
|
|
11
|
+
|
|
12
|
+
type ExecutionContext = {
|
|
13
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type Env = Record<string, unknown> & {
|
|
17
|
+
BETTER_AUTH_SECRET?: string;
|
|
18
|
+
DB: D1Binding;
|
|
19
|
+
FATE_LIVE: unknown;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const fateRoute = defineCloudflareFateRoute(fateServer, fateLive, { stream: fateStream });
|
|
23
|
+
const fateLiveRoute = defineCloudflareFateLiveRoute(fateStream);
|
|
24
|
+
|
|
25
|
+
export const FateLiveDurableObject = createCloudflareFateLiveDurableObject({
|
|
26
|
+
binding: 'FATE_LIVE',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const corsHeaders = (request: Request): Headers => {
|
|
30
|
+
const origin = resolveCorsOrigin(request.headers.get('origin'));
|
|
31
|
+
const headers = new Headers();
|
|
32
|
+
if (origin) {
|
|
33
|
+
headers.set('access-control-allow-origin', origin);
|
|
34
|
+
headers.set('access-control-allow-credentials', 'true');
|
|
35
|
+
headers.set('vary', 'Origin');
|
|
36
|
+
}
|
|
37
|
+
return headers;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const withCors = (request: Request, response: Response): Response => {
|
|
41
|
+
const headers = new Headers(response.headers);
|
|
42
|
+
corsHeaders(request).forEach((value, key) => headers.set(key, value));
|
|
43
|
+
return new Response(response.body, {
|
|
44
|
+
headers,
|
|
45
|
+
status: response.status,
|
|
46
|
+
statusText: response.statusText,
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const optionsResponse = (request: Request) => {
|
|
51
|
+
const headers = corsHeaders(request);
|
|
52
|
+
headers.set('access-control-allow-headers', 'content-type, authorization');
|
|
53
|
+
headers.set('access-control-allow-methods', 'GET, POST, OPTIONS');
|
|
54
|
+
headers.set('access-control-max-age', '86400');
|
|
55
|
+
return new Response(null, { headers, status: 204 });
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export default {
|
|
59
|
+
fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
|
60
|
+
return withDatabase(env.DB, async () => {
|
|
61
|
+
if (request.method === 'OPTIONS') {
|
|
62
|
+
return optionsResponse(request);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const url = new URL(request.url);
|
|
66
|
+
let response: Response;
|
|
67
|
+
if (url.pathname === '/fate' || url.pathname.startsWith('/fate/')) {
|
|
68
|
+
response = await fateRoute.fetch(request, env, ctx);
|
|
69
|
+
} else if (url.pathname === '/fate-live' || url.pathname.startsWith('/fate-live/')) {
|
|
70
|
+
response = await fateLiveRoute.fetch(request, env, ctx);
|
|
71
|
+
} else if (url.pathname.startsWith('/api/auth/')) {
|
|
72
|
+
response = await createAuth(url.origin, env.BETTER_AUTH_SECRET).handler(request);
|
|
73
|
+
} else {
|
|
74
|
+
response = Response.redirect(clientOrigin, 302);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return withCors(request, response);
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
};
|