-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.ts
173 lines (163 loc) · 4.18 KB
/
types.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { z } from "npm:zod@3";
export const JSR_UPDATE = z.object({
latest: z.string(),
});
export const LOGIN_SCHEMA = z.discriminatedUnion("error", [
z.object({ error: z.literal(false), token: z.string() }),
z.object({ error: z.literal(true), type: z.string() }),
]);
export const AUTH_PACKET_SCHEMA = z.object({
cmd: z.literal("auth"),
val: z.object({ token: z.string() }),
});
/** An attachement as in {@link Post}. */
export type Attachment = {
filename: string;
height: number;
id: string;
mime: string;
size: number;
width: number;
};
type RealAttachment = Omit<Attachment, "height" | "width"> & {
height?: number;
width?: number;
};
export const ATTACHMENT_SCHEMA: z.ZodType<
Attachment,
z.ZodTypeDef,
RealAttachment
> = z.object({
filename: z.string(),
height: z.number().default(0),
id: z.string(),
mime: z.string(),
size: z.number(),
width: z.number().default(0),
});
/** A post returned from the Meower API. */
export type Post = {
attachments: Attachment[];
edited_at?: number;
isDeleted: boolean;
p: string;
post_id: string;
post_origin: string;
t: { e: number };
type: number;
u: string;
reactions: { count: number; emoji: string; user_reacted: boolean }[];
reply_to: (Post | null)[];
};
type RealPost = Omit<Post, "attachments" | "reply_to"> & {
attachments: RealAttachment[];
reply_to: (RealPost | null)[];
};
export const BASE_POST_SCHEMA = z.object({
attachments: ATTACHMENT_SCHEMA.array(),
edited_at: z.number().optional(),
isDeleted: z.literal(false),
p: z.string(),
post_id: z.string(),
post_origin: z.string(),
t: z.object({ e: z.number() }),
type: z.number(),
u: z.string(),
reactions: z
.object({
count: z.number(),
emoji: z.string(),
user_reacted: z.boolean(),
})
.array(),
});
const POST_SCHEMA: z.ZodType<Post, z.ZodTypeDef, RealPost> =
BASE_POST_SCHEMA.extend({
reply_to: z.lazy(() => POST_SCHEMA.nullable().array()),
});
export const API_POST_SCHEMA = z
.object({ error: z.literal(false) })
.and(POST_SCHEMA)
.or(z.object({ error: z.literal(true), type: z.string() }));
export const POST_PACKET_SCHEMA = z.object({
cmd: z.literal("post"),
val: POST_SCHEMA,
});
export const UPDATE_POST_PACKET_SCHEMA = z.object({
cmd: z.literal("update_post"),
val: POST_SCHEMA,
});
export const DELETE_POST_PACKET_SCHEMA = z.object({
cmd: z.literal("delete_post"),
val: z.object({
post_id: z.string(),
}),
});
/** An attachment as returned from the uploading API. */
export type UploadsAttachment = {
/**
* @deprecated This only exists for backwards compatibility. Meower has
* removed this key.
*/
bucket: string;
/**
* @deprecated This only exists for backwards compatibility. Meower has
* removed this key.
*/
claimed: boolean;
filename: string;
/**
* @deprecated This only exists for backwards compatibility. Meower has
* removed this key.
*/
hash: string;
id: string;
/**
* @deprecated This only exists for backwards compatibility. Meower has
* removed this key.
*/
uploaded_at: number;
/**
* @deprecated This only exists for backwards compatibility. Meower has
* removed this key.
*/
uploaded_by: string;
};
export const UPLOADS_ATTACHMENT_SCHEMA = z.object({
filename: z.string(),
id: z.string(),
});
/** A user from the API. */
export type User = {
_id: string;
avatar: string;
avatar_color: string;
banned: boolean;
created: number | null;
flags: number;
last_seen: number | null;
lower_username: string;
lvl: number;
permissions: number | null;
pfp_data: number | null;
quote: string | null;
uuid: string | null;
};
export const USER_SCHEMA: z.ZodType<User> = z.object({
_id: z.string(),
avatar: z.string(),
avatar_color: z.string(),
banned: z.boolean(),
created: z.number().nullable(),
flags: z.number(),
last_seen: z.number().nullable(),
lower_username: z.string(),
lvl: z.number(),
permissions: z.number().nullable(),
pfp_data: z.number().nullable(),
quote: z.string().nullable(),
uuid: z.string().nullable(),
});
export const API_USER_SCHEMA = USER_SCHEMA.and(
z.object({ error: z.literal(false) }),
).or(z.object({ error: z.literal(true), type: z.string() }));