-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathcommon.ts
More file actions
131 lines (114 loc) · 3.08 KB
/
common.ts
File metadata and controls
131 lines (114 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Common types and placeholder implementations for recipe examples
*
* These are example types that should be replaced with actual
* implementations in real applications.
*
* NOTE: Instead of using extended context types with casting,
* prefer using Router generics: new Router<StateT, ContextT>()
* This provides better type safety without runtime casting.
*/
import type { RouterContext } from './router-module-loader';
// ===========================================
// Domain Types
// ===========================================
export type User = {
id: string;
email: string;
name: string;
role?: string;
};
export type Post = {
id: string;
userId: string;
title: string;
content: string;
};
export type Resource = {
id: string;
name: string;
};
// ===========================================
// Extended Context Types (Legacy)
// ===========================================
/**
* @deprecated Prefer using Router generics: new Router<StateT>()
* Context with request body (for POST/PUT/PATCH requests)
*/
export type ContextWithBody = RouterContext & {
request: RouterContext['request'] & {
body?: Record<string, unknown>;
};
};
/**
* @deprecated Prefer using Router generics: new Router<StateT>()
* Context with authenticated user state
*/
export type ContextWithUser = RouterContext & {
state: RouterContext['state'] & {
user?: User;
resource?: Resource;
};
};
// Re-export Next type for convenience
export type { Next } from 'koa';
export const db = {
authenticate: async (): Promise<void> => {}
};
export const redis = {
ping: async (): Promise<string> => {
return 'PONG';
}
};
type UserCreateData = { email?: string; name?: string };
type UserUpdateData = { email?: string; name?: string };
export const User = {
findById: async (_id: string): Promise<User | null> => {
return null;
},
findAll: async (): Promise<User[]> => {
return [];
},
findAndCountAll: async (_options: {
limit: number;
offset: number;
}): Promise<{ count: number; rows: User[] }> => {
return { count: 0, rows: [] };
},
create: async (data: UserCreateData): Promise<User> => {
return { id: '1', email: data.email || '', name: data.name || '' };
},
update: async (_id: string, data: UserUpdateData): Promise<User> => {
return { id: _id, email: data.email || '', name: data.name || '' };
},
delete: async (_id: string): Promise<void> => {}
};
export const Post = {
findAll: async (_options: {
limit: number;
offset: number;
}): Promise<Post[]> => {
return [];
},
findByUserId: async (_userId: string): Promise<Post[]> => {
return [];
}
};
export const Resource = {
findById: async (_id: string): Promise<Resource | null> => {
return null;
}
};
export const createUser = async (data: {
email: string;
password: string;
name: string;
}): Promise<User> => {
return { id: '1', email: data.email, name: data.name };
};
export const updateUser = async (
_id: string,
data: { email?: string; name?: string }
): Promise<User> => {
return { id: _id, email: data.email || '', name: data.name || '' };
};