@server/next 0.27.3 → 0.27.5

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.
Files changed (2) hide show
  1. package/index.d.ts +392 -0
  2. package/package.json +3 -1
package/index.d.ts ADDED
@@ -0,0 +1,392 @@
1
+ type Method = "get" | "post" | "put" | "patch" | "delete" | "head" | "options" | "socket";
2
+ type ServerConfig = {
3
+ User?: Record<string, string | number | boolean | Date | null | undefined>;
4
+ };
5
+ type RouteOptions = {
6
+ tags?: string | string[];
7
+ title?: string;
8
+ description?: string;
9
+ };
10
+ type Cookie = {
11
+ value?: string;
12
+ path?: string;
13
+ expires?: number | string | Date;
14
+ };
15
+ type RouterMethod = "*" | Method;
16
+ type Bucket = {
17
+ read: (path: string) => Promise<ReadableStream | null>;
18
+ write: (path: string, data: string | Buffer) => Promise<void | string>;
19
+ delete: (path: string) => Promise<boolean>;
20
+ };
21
+ type CorsSettings = {
22
+ origin: string | boolean;
23
+ methods: string;
24
+ headers: string;
25
+ };
26
+ type CorsOptions = boolean | string | string[] | {
27
+ origin?: string | string[];
28
+ methods?: string | Method[];
29
+ headers?: string | string[];
30
+ };
31
+ type BasicValue = string | number | boolean | null;
32
+ type SerializableValue = BasicValue | {
33
+ [key: string]: SerializableValue;
34
+ } | Array<SerializableValue>;
35
+ type KVStore = {
36
+ name?: string;
37
+ prefix: (key: string) => KVStore;
38
+ get: <T = SerializableValue>(key: string) => Promise<T>;
39
+ set: <T = SerializableValue>(key: string, value: T, options?: {
40
+ expires: string | number;
41
+ }) => Promise<void | string>;
42
+ has: (key: string) => Promise<boolean>;
43
+ del: (key: string) => Promise<void | string>;
44
+ keys: () => Promise<string[]>;
45
+ };
46
+ type Provider = "email" | "github";
47
+ type Strategy = "cookie" | "jwt" | "token";
48
+ type AuthSession = {
49
+ id: string;
50
+ provider: Provider;
51
+ strategy: Strategy;
52
+ user: string;
53
+ };
54
+ type AuthUser<T = object> = T & {
55
+ id: string | number;
56
+ provider: Provider;
57
+ strategy: Strategy;
58
+ email: string;
59
+ };
60
+ type ProviderString = Provider | `${Provider}|${Provider}`;
61
+ type AuthOption = `${Strategy}:${Provider | ProviderString}` | {
62
+ provider: Provider | ProviderString | Provider[];
63
+ strategy: Strategy;
64
+ session?: KVStore;
65
+ store?: KVStore;
66
+ redirect?: string;
67
+ cleanUser?: <T = AuthUser>(user: T) => T | Promise<T>;
68
+ };
69
+ type AuthSettings = {
70
+ provider: Provider[];
71
+ strategy: Strategy;
72
+ store: KVStore;
73
+ session: KVStore;
74
+ cleanUser: <T = AuthUser>(user: T) => T | Promise<T>;
75
+ redirect: string;
76
+ };
77
+ type Options = {
78
+ port?: number;
79
+ secret?: string;
80
+ views?: string | Bucket;
81
+ public?: string | Bucket;
82
+ uploads?: string | Bucket;
83
+ store?: KVStore;
84
+ cookies?: KVStore;
85
+ session?: KVStore | {
86
+ store: KVStore;
87
+ };
88
+ cors?: CorsOptions;
89
+ auth?: AuthOption;
90
+ openapi?: any;
91
+ };
92
+ type Settings = {
93
+ port: number;
94
+ secret: string;
95
+ views?: Bucket;
96
+ public?: Bucket;
97
+ uploads?: Bucket;
98
+ store?: KVStore;
99
+ cookies?: KVStore;
100
+ session?: {
101
+ store: KVStore;
102
+ };
103
+ cors?: CorsSettings;
104
+ auth?: AuthSettings;
105
+ openapi?: any;
106
+ };
107
+ type Time = {
108
+ (name: string): void;
109
+ times: [string, number][];
110
+ headers: () => string;
111
+ };
112
+ type Platform = {
113
+ provider: string | null;
114
+ runtime: string | null;
115
+ production: boolean;
116
+ };
117
+ type ExtractPathParams<Path extends string> = Path extends `${string}:${infer Param}(${infer Type})?/${infer Rest}` ? `${Param}:${Type}?` | ExtractPathParams<`/${Rest}`> : Path extends `${string}:${infer Param}(${infer Type})?` ? `${Param}:${Type}?` : Path extends `${string}:${infer Param}(${infer Type})/${infer Rest}` ? `${Param}:${Type}` | ExtractPathParams<`/${Rest}`> : Path extends `${string}:${infer Param}(${infer Type})` ? `${Param}:${Type}` : Path extends `${string}:${infer Param}?/${infer Rest}` ? `${Param}?` | ExtractPathParams<`/${Rest}`> : Path extends `${string}:${infer Param}?` ? `${Param}?` : Path extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractPathParams<`/${Rest}`> : Path extends `${string}:${infer Param}` ? Param : never;
118
+ type ParamTypeMap = {
119
+ string: string;
120
+ number: number;
121
+ date: Date;
122
+ };
123
+ type InferParamType<T extends string> = T extends keyof ParamTypeMap ? ParamTypeMap[T] : string;
124
+ type ParamsToObject<Params extends string> = {
125
+ [K in Params as K extends `${infer Key}:${infer _Type}?` ? Key : K extends `${infer Key}:${infer _Type}` ? Key : K extends `${infer Key}?` ? Key : K]: K extends `${infer _Key}:${infer Type}?` ? InferParamType<Type> | undefined : K extends `${infer _Key}:${infer Type}` ? InferParamType<Type> : K extends `${infer _Key}?` ? string | undefined : string;
126
+ };
127
+ type PathToParams<Path extends string> = ParamsToObject<ExtractPathParams<Path>>;
128
+ type BunEnv = Record<string, string> & {
129
+ upgrade?: (req: Request) => boolean;
130
+ };
131
+ type EventCallback = (data: Context & SerializableValue) => void;
132
+ type Events = Record<string, EventCallback[]> & {
133
+ on?: (key: string, cb: (value?: Context & SerializableValue) => void) => void;
134
+ trigger?: (key: string, value?: Partial<Context & SerializableValue>) => void;
135
+ };
136
+ type Context<Params extends Record<string, string> = Record<string, string>, O extends ServerConfig = object> = {
137
+ method: Method;
138
+ headers: Record<string, string | string[]>;
139
+ cookies: Record<string, string>;
140
+ body?: SerializableValue;
141
+ url: URL & {
142
+ params: Params;
143
+ query: Record<string, string>;
144
+ };
145
+ options: Settings;
146
+ platform: Platform;
147
+ time?: Time;
148
+ socket?: WebSocket;
149
+ sockets?: WebSocket[];
150
+ session?: Record<string, BasicValue>;
151
+ user?: O extends {
152
+ User: infer U;
153
+ } ? U & AuthUser : AuthUser;
154
+ init: number;
155
+ events: Events;
156
+ req?: Request;
157
+ res?: Response & {
158
+ cookies?: Record<string, string>;
159
+ };
160
+ app: Server;
161
+ };
162
+ type InlineReply = Response | {
163
+ body: string;
164
+ headers?: Headers;
165
+ } | SerializableValue;
166
+ type Body = InlineReply;
167
+ type Middleware<O extends ServerConfig = object, Params extends Record<string, string> = Record<string, string>> = (ctx: Context<Params, O>) => InlineReply | Promise<InlineReply> | void | Promise<void>;
168
+
169
+ type Variables = Record<string, string | string[]>;
170
+ type ExtendError = string | {
171
+ message: string;
172
+ status: number;
173
+ };
174
+ interface ServerErrorConstructor {
175
+ extend(errors: Record<string, ExtendError>): Record<string, ExtendError>;
176
+ [key: string]: ((vars?: Variables) => ServerError) | any;
177
+ }
178
+ declare class ServerError extends Error {
179
+ code: string;
180
+ status: number;
181
+ constructor(code: string, status: number, message: string | ((vars: Variables) => string), vars?: Variables);
182
+ static extend(errors: Record<string, ExtendError>): Record<string, ExtendError>;
183
+ }
184
+ declare const TypedServerError: typeof ServerError & ServerErrorConstructor;
185
+
186
+ declare global {
187
+ var env: Record<string, any>;
188
+ }
189
+
190
+ type Mids<O, Path extends string> = Middleware<O, PathToParams<Path>>[];
191
+ type PathOrMiddle<O extends ServerConfig = object> = string | Middleware<O>;
192
+ type FullRoute = [RouterMethod, string, ...Middleware[]][];
193
+ declare class Router<O extends ServerConfig = object> {
194
+ handlers: Record<Method, FullRoute>;
195
+ self(): this;
196
+ handle(method: RouterMethod, path: PathOrMiddle<O>, ...middleware: Middleware<O>[]): this;
197
+ socket<Path extends string>(path: Path, ...middleware: Mids<O, Path>): this;
198
+ socket<Path extends string>(path: Path, options: RouteOptions, ...middleware: Mids<O, Path>): this;
199
+ socket(...middleware: Middleware<O>[]): this;
200
+ socket(options: RouteOptions, ...middleware: Middleware<O>[]): this;
201
+ get<Path extends string>(path: Path, ...middleware: Mids<O, Path>): this;
202
+ get<Path extends string>(path: Path, options: RouteOptions, ...middleware: Mids<O, Path>): this;
203
+ get(...middleware: Middleware<O>[]): this;
204
+ get(options: RouteOptions, ...middleware: Middleware<O>[]): this;
205
+ head<Path extends string>(path: Path, ...middleware: Mids<O, Path>): this;
206
+ head<Path extends string>(path: Path, options: RouteOptions, ...middleware: Mids<O, Path>): this;
207
+ head(...middleware: Middleware<O>[]): this;
208
+ head(options: RouteOptions, ...middleware: Middleware<O>[]): this;
209
+ post<Path extends string>(path: Path, ...middleware: Mids<O, Path>): this;
210
+ post<Path extends string>(path: Path, options: RouteOptions, ...middleware: Mids<O, Path>): this;
211
+ post(...middleware: Middleware<O>[]): this;
212
+ post(options: RouteOptions, ...middleware: Middleware<O>[]): this;
213
+ put<Path extends string>(path: Path, ...middleware: Mids<O, Path>): this;
214
+ put<Path extends string>(path: Path, options: RouteOptions, ...middleware: Mids<O, Path>): this;
215
+ put(...middleware: Middleware<O>[]): this;
216
+ put(options: RouteOptions, ...middleware: Middleware<O>[]): this;
217
+ patch<Path extends string>(path: Path, ...middleware: Mids<O, Path>): this;
218
+ patch<Path extends string>(path: Path, options: RouteOptions, ...middleware: Mids<O, Path>): this;
219
+ patch(...middleware: Middleware<O>[]): this;
220
+ patch(options: RouteOptions, ...middleware: Middleware<O>[]): this;
221
+ del<Path extends string>(path: Path, ...middleware: Mids<O, Path>): this;
222
+ del<Path extends string>(path: Path, options: RouteOptions, ...middleware: Mids<O, Path>): this;
223
+ del(...middleware: Middleware<O>[]): this;
224
+ del(options: RouteOptions, ...middleware: Middleware<O>[]): this;
225
+ options<Path extends string>(path: Path, ...middleware: Mids<O, Path>): this;
226
+ options<Path extends string>(path: Path, options: RouteOptions, ...mid: Mids<O, Path>): this;
227
+ options(...middleware: Middleware<O>[]): this;
228
+ options(options: RouteOptions, ...middleware: Middleware<O>[]): this;
229
+ use(...middleware: Middleware[]): this;
230
+ use(path: string, ...middleware: Middleware[]): this;
231
+ use(router: Router): this;
232
+ use(path: string, router: Router): this;
233
+ }
234
+ declare function router(): Router;
235
+
236
+ type CookieOptions = string | string[] | Cookie | Cookie[] | null;
237
+ interface ResponseData {
238
+ headers: Headers;
239
+ status?: number;
240
+ }
241
+ declare class Reply {
242
+ res: ResponseData;
243
+ constructor();
244
+ status(status: number): this;
245
+ type(type?: string): this;
246
+ download(name?: string): this;
247
+ headers(key: string | Record<string, string>, value?: string): this;
248
+ cookies(key: string | Record<string, CookieOptions>, value?: CookieOptions): this;
249
+ json(body: unknown): Response;
250
+ redirect(path: string): Response;
251
+ file(path: string): Promise<Response>;
252
+ send(body?: string | Buffer | ReadableStream | any): Response;
253
+ }
254
+ type Params<K extends keyof Reply> = Reply[K] extends (...args: infer A) => any ? A : never;
255
+ declare const status: (...args: Params<"status">) => Reply;
256
+ declare const headers: (...args: Params<"headers">) => Reply;
257
+ declare const type: (...args: Params<"type">) => Reply;
258
+ declare const download: (...args: Params<"download">) => Reply;
259
+ declare const cookies: (...args: Params<"cookies">) => Reply;
260
+ declare const send: (...args: Params<"send">) => Response;
261
+ declare const json: (...args: Params<"json">) => Response;
262
+ declare const file: (...args: Params<"file">) => Promise<Response>;
263
+ declare const redirect: (...args: Params<"redirect">) => Response;
264
+
265
+ declare class Server<O extends ServerConfig = object> extends Router<O> {
266
+ settings: Settings;
267
+ platform: Platform;
268
+ sockets: any[];
269
+ websocket: any;
270
+ port?: number;
271
+ constructor(options?: Options);
272
+ self(): this;
273
+ node(): Promise<void>;
274
+ fetch(request: Request, env?: BunEnv): Promise<Response>;
275
+ callback(request: Request, context: unknown): Promise<Response>;
276
+ test(): {
277
+ get: (path: string, options?: {
278
+ cache?: RequestCache;
279
+ credentials?: RequestCredentials;
280
+ headers?: HeadersInit;
281
+ integrity?: string;
282
+ keepalive?: boolean;
283
+ method?: string;
284
+ mode?: RequestMode;
285
+ priority?: RequestPriority;
286
+ redirect?: RequestRedirect;
287
+ referrer?: string;
288
+ referrerPolicy?: ReferrerPolicy;
289
+ signal?: AbortSignal | null;
290
+ window?: null;
291
+ }) => Promise<Response>;
292
+ head: (path: string, options?: {
293
+ cache?: RequestCache;
294
+ credentials?: RequestCredentials;
295
+ headers?: HeadersInit;
296
+ integrity?: string;
297
+ keepalive?: boolean;
298
+ method?: string;
299
+ mode?: RequestMode;
300
+ priority?: RequestPriority;
301
+ redirect?: RequestRedirect;
302
+ referrer?: string;
303
+ referrerPolicy?: ReferrerPolicy;
304
+ signal?: AbortSignal | null;
305
+ window?: null;
306
+ }) => Promise<Response>;
307
+ post: (path: string, body?: string | number | boolean | ArrayBuffer | {
308
+ [key: string]: SerializableValue;
309
+ } | SerializableValue[] | ReadableStream<any> | Blob | ArrayBufferView<ArrayBuffer> | FormData | URLSearchParams, options?: {
310
+ cache?: RequestCache;
311
+ credentials?: RequestCredentials;
312
+ headers?: HeadersInit;
313
+ integrity?: string;
314
+ keepalive?: boolean;
315
+ method?: string;
316
+ mode?: RequestMode;
317
+ priority?: RequestPriority;
318
+ redirect?: RequestRedirect;
319
+ referrer?: string;
320
+ referrerPolicy?: ReferrerPolicy;
321
+ signal?: AbortSignal | null;
322
+ window?: null;
323
+ }) => Promise<Response>;
324
+ put: (path: string, body?: string | number | boolean | ArrayBuffer | {
325
+ [key: string]: SerializableValue;
326
+ } | SerializableValue[] | ReadableStream<any> | Blob | ArrayBufferView<ArrayBuffer> | FormData | URLSearchParams, options?: {
327
+ cache?: RequestCache;
328
+ credentials?: RequestCredentials;
329
+ headers?: HeadersInit;
330
+ integrity?: string;
331
+ keepalive?: boolean;
332
+ method?: string;
333
+ mode?: RequestMode;
334
+ priority?: RequestPriority;
335
+ redirect?: RequestRedirect;
336
+ referrer?: string;
337
+ referrerPolicy?: ReferrerPolicy;
338
+ signal?: AbortSignal | null;
339
+ window?: null;
340
+ }) => Promise<Response>;
341
+ patch: (path: string, body?: string | number | boolean | ArrayBuffer | {
342
+ [key: string]: SerializableValue;
343
+ } | SerializableValue[] | ReadableStream<any> | Blob | ArrayBufferView<ArrayBuffer> | FormData | URLSearchParams, options?: {
344
+ cache?: RequestCache;
345
+ credentials?: RequestCredentials;
346
+ headers?: HeadersInit;
347
+ integrity?: string;
348
+ keepalive?: boolean;
349
+ method?: string;
350
+ mode?: RequestMode;
351
+ priority?: RequestPriority;
352
+ redirect?: RequestRedirect;
353
+ referrer?: string;
354
+ referrerPolicy?: ReferrerPolicy;
355
+ signal?: AbortSignal | null;
356
+ window?: null;
357
+ }) => Promise<Response>;
358
+ delete: (path: string, options?: {
359
+ cache?: RequestCache;
360
+ credentials?: RequestCredentials;
361
+ headers?: HeadersInit;
362
+ integrity?: string;
363
+ keepalive?: boolean;
364
+ method?: string;
365
+ mode?: RequestMode;
366
+ priority?: RequestPriority;
367
+ redirect?: RequestRedirect;
368
+ referrer?: string;
369
+ referrerPolicy?: ReferrerPolicy;
370
+ signal?: AbortSignal | null;
371
+ window?: null;
372
+ }) => Promise<Response>;
373
+ options: (path: string, options?: {
374
+ cache?: RequestCache;
375
+ credentials?: RequestCredentials;
376
+ headers?: HeadersInit;
377
+ integrity?: string;
378
+ keepalive?: boolean;
379
+ method?: string;
380
+ mode?: RequestMode;
381
+ priority?: RequestPriority;
382
+ redirect?: RequestRedirect;
383
+ referrer?: string;
384
+ referrerPolicy?: ReferrerPolicy;
385
+ signal?: AbortSignal | null;
386
+ window?: null;
387
+ }) => Promise<Response>;
388
+ };
389
+ }
390
+ declare function server<Options>(options?: {}): Server<Options>;
391
+
392
+ export { type AuthOption, type AuthSession, type AuthSettings, type AuthUser, type BasicValue, type Body, type Bucket, type BunEnv, type Context, type Cookie, type CorsSettings, type EventCallback, type ExtractPathParams, type InferParamType, type InlineReply, type KVStore, type Method, type Middleware, type Options, type ParamTypeMap, type ParamsToObject, type PathToParams, type Platform, type Provider, type RouteOptions, type RouterMethod, type SerializableValue, Server, type ServerConfig, TypedServerError as ServerError, type Settings, type Strategy, type Time, cookies, server as default, download, file, headers, json, redirect, router, send, status, type };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.27.3",
3
+ "version": "0.27.5",
4
4
  "description": "A fully-fledged web server with routing, file uploads, sessions, static files, schema validation, websockets, testing, etc.",
5
5
  "homepage": "https://server-js.com/",
6
6
  "repository": "https://github.com/franciscop/server-next.git",
@@ -34,10 +34,12 @@
34
34
  "main": "index.js",
35
35
  "types": "index.d.ts",
36
36
  "files": [
37
+ "index.d.ts",
37
38
  "src/jsx/"
38
39
  ],
39
40
  "exports": {
40
41
  ".": "./index.js",
42
+ "./index.d.ts": "./index.d.ts",
41
43
  "./jsx-runtime": "./src/jsx/jsx-runtime.js",
42
44
  "./jsx-dev-runtime": "./src/jsx/jsx-dev-runtime.js"
43
45
  },