@server/next 0.46.0 → 0.47.1

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 (3) hide show
  1. package/index.d.ts +94 -53
  2. package/index.js +747 -816
  3. package/package.json +3 -3
package/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import * as http from 'http';
2
2
  import { Readable } from 'node:stream';
3
- export { default as kv } from 'polystore';
4
3
  export { default as bucket } from 'bucket';
5
4
 
6
5
  type LimitOptions = {
@@ -170,59 +169,75 @@ type BasicValue = string | number | boolean | null;
170
169
  type SerializableValue = BasicValue | {
171
170
  [key: string]: SerializableValue;
172
171
  } | Array<SerializableValue>;
173
- type StoreSource = KVStore | Map<string, any> | string | Record<string, any>;
174
- type KVStore = {
175
- name?: string;
176
- prefix: (prefix?: string) => KVStore;
177
- get: <T = SerializableValue>(key: string) => Promise<T | null>;
178
- set: <T = SerializableValue>(key: string, value: T, options?: {
179
- expires?: string | number | null;
180
- }) => Promise<void | string>;
181
- has: (key: string) => Promise<boolean>;
182
- del: (key: string) => Promise<void | string>;
183
- keys: () => Promise<string[]>;
184
- };
185
- type Provider = "email" | "github" | "google" | "microsoft" | "discord" | "facebook" | "apple";
186
- type Strategy = "cookie" | "jwt" | "token";
187
- type AuthSession = {
188
- user: string;
189
- provider: Provider;
190
- created: string;
191
- };
192
- type AuthUser<T = Record<string, any>> = T & {
193
- id: string | number;
194
- provider: Provider;
195
- strategy: Strategy;
172
+ type Strategy = "session" | "cookie" | "token" | "jwt";
173
+ type AuthProfile = {
174
+ provider: string;
175
+ id: string;
196
176
  email: string;
177
+ name?: string;
178
+ avatar?: string;
179
+ accessToken?: string;
180
+ refreshToken?: string;
181
+ raw: Record<string, any>;
197
182
  };
198
- type ProfileUser = {
199
- id: string | number;
200
- email: string;
183
+ type AuthMeta = {
184
+ issuedAt: Date;
185
+ expiresAt?: Date;
186
+ strategy?: Strategy;
187
+ provider?: string;
188
+ };
189
+ type AuthClaims = {
190
+ sub: string;
191
+ } & Record<string, any>;
192
+ type Awaitable<T> = T | Promise<T>;
193
+ type ProviderOptions = {
194
+ id?: string;
195
+ secret?: string;
196
+ scope?: string | string[];
197
+ issuer?: string;
201
198
  } & Record<string, any>;
202
- type AuthOption = `${Strategy}:${Provider}` | {
203
- strategy: Strategy;
204
- providers?: Provider | Provider[];
205
- users?: StoreSource;
206
- sessions?: StoreSource;
207
- redirect?: string;
208
- onProfile?: (raw: any, provider: Provider) => ProfileUser | Promise<ProfileUser>;
209
- onLogin?: (loginUser: AuthUser, existingUser: AuthUser | null, ctx: Context) => ProfileUser | Promise<ProfileUser>;
210
- onUser?: <T = AuthUser>(user: T, ctx: Context) => T | Promise<T>;
211
- onToken?: (user: AuthUser, ctx: Context) => ProfileUser | Promise<ProfileUser>;
212
- onLogout?: (ctx: Context) => unknown;
213
- };
214
- type AuthSettings = {
215
- providers: Provider[];
216
- strategy: Strategy;
217
- users: KVStore;
218
- sessions: KVStore;
219
- onProfile?: (raw: any, provider: Provider) => ProfileUser | Promise<ProfileUser>;
220
- onLogin?: (loginUser: AuthUser, existingUser: AuthUser | null, ctx: Context) => ProfileUser | Promise<ProfileUser>;
221
- onUser: <T = AuthUser>(user: T, ctx: Context) => T | Promise<T>;
222
- onToken: (user: AuthUser, ctx: Context) => ProfileUser | Promise<ProfileUser>;
223
- onLogout?: (ctx: Context) => unknown;
224
- redirect: string;
199
+ type RedirectOption = string | ((user: any, ctx: Context) => Awaitable<string>) | {
200
+ login?: string | ((user: any, ctx: Context) => Awaitable<string>);
201
+ logout?: string;
202
+ error?: string;
203
+ };
204
+ type AuthConfig<U = AuthProfile> = {
205
+ providers: string | readonly string[] | Record<string, string | ProviderOptions>;
206
+ strategy?: Strategy | readonly Strategy[];
207
+ expires?: string;
208
+ redirect?: RedirectOption;
209
+ onLogin?: (profile: AuthProfile, ctx: Context) => Awaitable<string | number | undefined>;
210
+ getUser?: (id: string, ctx: Context) => Awaitable<U>;
211
+ toPublicUser?: (user: any) => Awaitable<any>;
212
+ onLogout?: (id: string, ctx: Context) => Awaitable<void>;
213
+ };
214
+ type AuthVerify<U = AuthClaims> = {
215
+ verify: string;
216
+ audience: string | readonly string[];
217
+ cookie?: string;
218
+ audienceClaim?: string | readonly string[];
219
+ getUser?: (id: string, ctx: Context) => Awaitable<U>;
220
+ };
221
+ type AuthInstance = {
222
+ handler: (request: Request) => Awaitable<Response>;
223
+ path?: string;
224
+ user?: (ctx: Context) => Awaitable<any>;
225
+ };
226
+ type AuthFunction<U = any> = (ctx: Context<any>) => Awaitable<U>;
227
+ type AuthOption = string | AuthFunction | AuthConfig<any> | AuthVerify<any> | AuthInstance | readonly AuthOption[];
228
+ type UserOf<A> = A extends readonly (infer M)[] ? UserOf<M> : A extends (...args: any[]) => infer R ? NonNullable<Awaited<R>> : A extends {
229
+ getUser: (...args: any[]) => infer R;
230
+ } ? NonNullable<Awaited<R>> : A extends {
231
+ verify: any;
232
+ } ? AuthClaims : A extends {
233
+ providers: any;
234
+ } ? AuthProfile : A extends string ? AuthProfile : never;
235
+ type AuthEntry = {
236
+ name: string;
237
+ user: (ctx: Context) => Promise<any>;
238
+ routes?: (app: Server) => void;
225
239
  };
240
+ type AuthSettings = AuthEntry[];
226
241
  type LogLevel = "info";
227
242
  type Logger = {
228
243
  level?: LogLevel;
@@ -253,13 +268,13 @@ type SecuritySettings = {
253
268
  };
254
269
  type OnError = (error: Error, ctx: Context) => Response | Promise<Response>;
255
270
  type OnResponse = (response: Response, ctx: Context) => Response | void | Promise<Response | void>;
256
- type Options = {
271
+ type Options<A = AuthOption> = {
257
272
  port?: number;
258
273
  secrets?: string | string[];
259
274
  public?: string | Bucket;
260
275
  uploads?: string | Bucket | UploadOptions;
261
276
  cors?: CorsOptions;
262
- auth?: AuthOption;
277
+ auth?: A;
263
278
  openapi?: boolean | string | {
264
279
  path?: string;
265
280
  title?: string;
@@ -339,6 +354,7 @@ type Context<C extends ContextTypes = {}> = {
339
354
  socket?: WebSocket;
340
355
  sockets?: WebSocket[];
341
356
  user?: Field<C, "user", Record<string, any>>;
357
+ auth?: AuthMeta;
342
358
  init: number;
343
359
  app: Server;
344
360
  } & ("body" extends keyof C ? {
@@ -560,6 +576,31 @@ declare class Server<C extends ContextTypes = {}> extends Router<C> {
560
576
  }) => Promise<Response>;
561
577
  };
562
578
  }
579
+ declare function server<U = AuthProfile>(options: Omit<Options, "auth"> & {
580
+ auth: AuthConfig<U>;
581
+ }): Server<{
582
+ user: U;
583
+ }>;
584
+ declare function server(options: Omit<Options, "auth"> & {
585
+ auth: string;
586
+ }): Server<{
587
+ user: AuthProfile;
588
+ }>;
589
+ declare function server<U = AuthClaims>(options: Omit<Options, "auth"> & {
590
+ auth: AuthVerify<U>;
591
+ }): Server<{
592
+ user: U;
593
+ }>;
594
+ declare function server<U>(options: Omit<Options, "auth"> & {
595
+ auth: AuthFunction<U>;
596
+ }): Server<{
597
+ user: NonNullable<Awaited<U>>;
598
+ }>;
599
+ declare function server<A extends readonly AuthOption[]>(options: Omit<Options, "auth"> & {
600
+ auth: A;
601
+ }): Server<{
602
+ user: UserOf<A[number]>;
603
+ }>;
563
604
  declare function server<C extends ContextTypes = {}>(options?: Options): Server<C>;
564
605
 
565
- export { type AuthOption, type AuthSession, type AuthSettings, type AuthUser, type BasicValue, type Body, type BodyMode, type Bucket, type BucketFile, type BunEnv, type CacheOption, type Context, type ContextExtension, type ContextTypes, type Cookie, type CorsSettings, type ExtractPathParams, type FileInfo, type InferParamType, type InlineReply, type KVStore, type LogLevel, type Logger, type Method, type Middleware, type Options, type ParamTypeMap, type ParamsToObject, type PathToParams, type Platform, type ProfileUser, type Provider, type Route, type RouteOptions, type RouteSchema, type RouterMethod, type SchemaOutput, type SecurityOptions, type SecuritySettings, type SerializableValue, Server, TypedServerError as ServerError, type Settings, type StandardIssue, type StandardSchemaV1, type StoreSource, type Strategy, type Time, type UploadOptions, type UploadedFile, ValidationError, cache, cookies, server as default, download, file, headers, json, redirect, router, send, status, type };
606
+ export { type AuthClaims, type AuthConfig, type AuthEntry, type AuthFunction, type AuthInstance, type AuthMeta, type AuthOption, type AuthProfile, type AuthSettings, type AuthVerify, type BasicValue, type Body, type BodyMode, type Bucket, type BucketFile, type BunEnv, type CacheOption, type Context, type ContextExtension, type ContextTypes, type Cookie, type CorsSettings, type ExtractPathParams, type FileInfo, type InferParamType, type InlineReply, type LogLevel, type Logger, type Method, type Middleware, type Options, type ParamTypeMap, type ParamsToObject, type PathToParams, type Platform, type ProviderOptions, type RedirectOption, type Route, type RouteOptions, type RouteSchema, type RouterMethod, type SchemaOutput, type SecurityOptions, type SecuritySettings, type SerializableValue, Server, TypedServerError as ServerError, type Settings, type StandardIssue, type StandardSchemaV1, type Strategy, type Time, type UploadOptions, type UploadedFile, type UserOf, ValidationError, cache, cookies, server as default, download, file, headers, json, redirect, router, send, status, type };