@better-auth/i18n 1.6.0-beta.0 → 1.6.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.
package/dist/client.d.mts
CHANGED
package/dist/client.mjs
CHANGED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
import { BetterAuthPluginRegistry, BetterAuthPluginRegistryIdentifier, GenericEndpointContext, UnionToIntersection } from "@better-auth/core";
|
|
2
|
+
|
|
3
|
+
//#region ../../node_modules/.pnpm/better-call@1.3.5_zod@4.3.6/node_modules/better-call/dist/standard-schema.d.mts
|
|
4
|
+
//#region src/standard-schema.d.ts
|
|
5
|
+
/** The Standard Schema interface. */
|
|
6
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
7
|
+
/** The Standard Schema properties. */
|
|
8
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
9
|
+
}
|
|
10
|
+
declare namespace StandardSchemaV1 {
|
|
11
|
+
/** The Standard Schema properties interface. */
|
|
12
|
+
interface Props<Input = unknown, Output = Input> {
|
|
13
|
+
/** The version number of the standard. */
|
|
14
|
+
readonly version: 1;
|
|
15
|
+
/** The vendor name of the schema library. */
|
|
16
|
+
readonly vendor: string;
|
|
17
|
+
/** Validates unknown input values. */
|
|
18
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
19
|
+
/** Inferred types associated with the schema. */
|
|
20
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
21
|
+
}
|
|
22
|
+
/** The result interface of the validate function. */
|
|
23
|
+
type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
24
|
+
/** The result interface if validation succeeds. */
|
|
25
|
+
interface SuccessResult<Output> {
|
|
26
|
+
/** The typed output value. */
|
|
27
|
+
readonly value: Output;
|
|
28
|
+
/** The non-existent issues. */
|
|
29
|
+
readonly issues?: undefined;
|
|
30
|
+
}
|
|
31
|
+
/** The result interface if validation fails. */
|
|
32
|
+
interface FailureResult {
|
|
33
|
+
/** The issues of failed validation. */
|
|
34
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
35
|
+
}
|
|
36
|
+
/** The issue interface of the failure output. */
|
|
37
|
+
interface Issue {
|
|
38
|
+
/** The error message of the issue. */
|
|
39
|
+
readonly message: string;
|
|
40
|
+
/** The path of the issue, if any. */
|
|
41
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
42
|
+
}
|
|
43
|
+
/** The path segment interface of the issue. */
|
|
44
|
+
interface PathSegment {
|
|
45
|
+
/** The key representing a path segment. */
|
|
46
|
+
readonly key: PropertyKey;
|
|
47
|
+
}
|
|
48
|
+
/** The Standard Schema types interface. */
|
|
49
|
+
interface Types<Input = unknown, Output = Input> {
|
|
50
|
+
/** The input type of the schema. */
|
|
51
|
+
readonly input: Input;
|
|
52
|
+
/** The output type of the schema. */
|
|
53
|
+
readonly output: Output;
|
|
54
|
+
}
|
|
55
|
+
/** Infers the input type of a Standard Schema. */
|
|
56
|
+
type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
57
|
+
/** Infers the output type of a Standard Schema. */
|
|
58
|
+
type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
59
|
+
} //#endregion
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region ../../node_modules/.pnpm/better-call@1.3.5_zod@4.3.6/node_modules/better-call/dist/error.d.mts
|
|
62
|
+
declare const statusCodes: {
|
|
63
|
+
OK: number;
|
|
64
|
+
CREATED: number;
|
|
65
|
+
ACCEPTED: number;
|
|
66
|
+
NO_CONTENT: number;
|
|
67
|
+
MULTIPLE_CHOICES: number;
|
|
68
|
+
MOVED_PERMANENTLY: number;
|
|
69
|
+
FOUND: number;
|
|
70
|
+
SEE_OTHER: number;
|
|
71
|
+
NOT_MODIFIED: number;
|
|
72
|
+
TEMPORARY_REDIRECT: number;
|
|
73
|
+
BAD_REQUEST: number;
|
|
74
|
+
UNAUTHORIZED: number;
|
|
75
|
+
PAYMENT_REQUIRED: number;
|
|
76
|
+
FORBIDDEN: number;
|
|
77
|
+
NOT_FOUND: number;
|
|
78
|
+
METHOD_NOT_ALLOWED: number;
|
|
79
|
+
NOT_ACCEPTABLE: number;
|
|
80
|
+
PROXY_AUTHENTICATION_REQUIRED: number;
|
|
81
|
+
REQUEST_TIMEOUT: number;
|
|
82
|
+
CONFLICT: number;
|
|
83
|
+
GONE: number;
|
|
84
|
+
LENGTH_REQUIRED: number;
|
|
85
|
+
PRECONDITION_FAILED: number;
|
|
86
|
+
PAYLOAD_TOO_LARGE: number;
|
|
87
|
+
URI_TOO_LONG: number;
|
|
88
|
+
UNSUPPORTED_MEDIA_TYPE: number;
|
|
89
|
+
RANGE_NOT_SATISFIABLE: number;
|
|
90
|
+
EXPECTATION_FAILED: number;
|
|
91
|
+
"I'M_A_TEAPOT": number;
|
|
92
|
+
MISDIRECTED_REQUEST: number;
|
|
93
|
+
UNPROCESSABLE_ENTITY: number;
|
|
94
|
+
LOCKED: number;
|
|
95
|
+
FAILED_DEPENDENCY: number;
|
|
96
|
+
TOO_EARLY: number;
|
|
97
|
+
UPGRADE_REQUIRED: number;
|
|
98
|
+
PRECONDITION_REQUIRED: number;
|
|
99
|
+
TOO_MANY_REQUESTS: number;
|
|
100
|
+
REQUEST_HEADER_FIELDS_TOO_LARGE: number;
|
|
101
|
+
UNAVAILABLE_FOR_LEGAL_REASONS: number;
|
|
102
|
+
INTERNAL_SERVER_ERROR: number;
|
|
103
|
+
NOT_IMPLEMENTED: number;
|
|
104
|
+
BAD_GATEWAY: number;
|
|
105
|
+
SERVICE_UNAVAILABLE: number;
|
|
106
|
+
GATEWAY_TIMEOUT: number;
|
|
107
|
+
HTTP_VERSION_NOT_SUPPORTED: number;
|
|
108
|
+
VARIANT_ALSO_NEGOTIATES: number;
|
|
109
|
+
INSUFFICIENT_STORAGE: number;
|
|
110
|
+
LOOP_DETECTED: number;
|
|
111
|
+
NOT_EXTENDED: number;
|
|
112
|
+
NETWORK_AUTHENTICATION_REQUIRED: number;
|
|
113
|
+
};
|
|
114
|
+
type Status = 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511;
|
|
115
|
+
declare class InternalAPIError extends Error {
|
|
116
|
+
status: keyof typeof statusCodes | Status;
|
|
117
|
+
body: ({
|
|
118
|
+
message?: string;
|
|
119
|
+
code?: string;
|
|
120
|
+
cause?: unknown;
|
|
121
|
+
} & Record<string, any>) | undefined;
|
|
122
|
+
headers: HeadersInit;
|
|
123
|
+
statusCode: number;
|
|
124
|
+
constructor(status?: keyof typeof statusCodes | Status, body?: ({
|
|
125
|
+
message?: string;
|
|
126
|
+
code?: string;
|
|
127
|
+
cause?: unknown;
|
|
128
|
+
} & Record<string, any>) | undefined, headers?: HeadersInit, statusCode?: number);
|
|
129
|
+
}
|
|
130
|
+
type APIError = InstanceType<typeof InternalAPIError>;
|
|
131
|
+
declare const APIError: new (status?: Status | "OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "FOUND" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "REQUEST_TIMEOUT" | "CONFLICT" | "GONE" | "LENGTH_REQUIRED" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "URI_TOO_LONG" | "UNSUPPORTED_MEDIA_TYPE" | "RANGE_NOT_SATISFIABLE" | "EXPECTATION_FAILED" | "I'M_A_TEAPOT" | "MISDIRECTED_REQUEST" | "UNPROCESSABLE_ENTITY" | "LOCKED" | "FAILED_DEPENDENCY" | "TOO_EARLY" | "UPGRADE_REQUIRED" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "REQUEST_HEADER_FIELDS_TOO_LARGE" | "UNAVAILABLE_FOR_LEGAL_REASONS" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "HTTP_VERSION_NOT_SUPPORTED" | "VARIANT_ALSO_NEGOTIATES" | "INSUFFICIENT_STORAGE" | "LOOP_DETECTED" | "NOT_EXTENDED" | "NETWORK_AUTHENTICATION_REQUIRED" | undefined, body?: ({
|
|
132
|
+
message?: string;
|
|
133
|
+
code?: string;
|
|
134
|
+
cause?: unknown;
|
|
135
|
+
} & Record<string, any>) | undefined, headers?: HeadersInit | undefined, statusCode?: number | undefined) => InternalAPIError & {
|
|
136
|
+
errorStack: string | undefined;
|
|
137
|
+
}; //#endregion
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region ../../node_modules/.pnpm/better-call@1.3.5_zod@4.3.6/node_modules/better-call/dist/openapi.d.mts
|
|
140
|
+
//#region src/openapi.d.ts
|
|
141
|
+
type OpenAPISchemaType = "string" | "number" | "integer" | "boolean" | "array" | "object";
|
|
142
|
+
interface OpenAPIParameter {
|
|
143
|
+
in: "query" | "path" | "header" | "cookie";
|
|
144
|
+
name?: string;
|
|
145
|
+
description?: string;
|
|
146
|
+
required?: boolean;
|
|
147
|
+
schema?: {
|
|
148
|
+
type: OpenAPISchemaType;
|
|
149
|
+
format?: string | undefined;
|
|
150
|
+
items?: {
|
|
151
|
+
type: OpenAPISchemaType;
|
|
152
|
+
};
|
|
153
|
+
enum?: string[];
|
|
154
|
+
minLength?: number;
|
|
155
|
+
description?: string | undefined;
|
|
156
|
+
default?: string | undefined;
|
|
157
|
+
example?: string | undefined;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region ../../node_modules/.pnpm/better-call@1.3.5_zod@4.3.6/node_modules/better-call/dist/endpoint.d.mts
|
|
162
|
+
//#region src/endpoint.d.ts
|
|
163
|
+
interface EndpointBaseOptions {
|
|
164
|
+
/**
|
|
165
|
+
* Query Schema
|
|
166
|
+
*/
|
|
167
|
+
query?: StandardSchemaV1;
|
|
168
|
+
/**
|
|
169
|
+
* Error Schema
|
|
170
|
+
*/
|
|
171
|
+
error?: StandardSchemaV1;
|
|
172
|
+
/**
|
|
173
|
+
* If true headers will be required to be passed in the context
|
|
174
|
+
*/
|
|
175
|
+
requireHeaders?: boolean;
|
|
176
|
+
/**
|
|
177
|
+
* If true request object will be required
|
|
178
|
+
*/
|
|
179
|
+
requireRequest?: boolean;
|
|
180
|
+
/**
|
|
181
|
+
* Clone the request object from the router
|
|
182
|
+
*/
|
|
183
|
+
cloneRequest?: boolean;
|
|
184
|
+
/**
|
|
185
|
+
* If true the body will be undefined
|
|
186
|
+
*/
|
|
187
|
+
disableBody?: boolean;
|
|
188
|
+
/**
|
|
189
|
+
* Endpoint metadata
|
|
190
|
+
*/
|
|
191
|
+
metadata?: {
|
|
192
|
+
/**
|
|
193
|
+
* Open API definition
|
|
194
|
+
*/
|
|
195
|
+
openapi?: {
|
|
196
|
+
summary?: string;
|
|
197
|
+
description?: string;
|
|
198
|
+
tags?: string[];
|
|
199
|
+
operationId?: string;
|
|
200
|
+
parameters?: OpenAPIParameter[];
|
|
201
|
+
requestBody?: {
|
|
202
|
+
content: {
|
|
203
|
+
"application/json": {
|
|
204
|
+
schema: {
|
|
205
|
+
type?: OpenAPISchemaType;
|
|
206
|
+
properties?: Record<string, any>;
|
|
207
|
+
required?: string[];
|
|
208
|
+
$ref?: string;
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
responses?: {
|
|
214
|
+
[status: string]: {
|
|
215
|
+
description: string;
|
|
216
|
+
content?: {
|
|
217
|
+
"application/json"?: {
|
|
218
|
+
schema: {
|
|
219
|
+
type?: OpenAPISchemaType;
|
|
220
|
+
properties?: Record<string, any>;
|
|
221
|
+
required?: string[];
|
|
222
|
+
$ref?: string;
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
"text/plain"?: {
|
|
226
|
+
schema?: {
|
|
227
|
+
type?: OpenAPISchemaType;
|
|
228
|
+
properties?: Record<string, any>;
|
|
229
|
+
required?: string[];
|
|
230
|
+
$ref?: string;
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
"text/html"?: {
|
|
234
|
+
schema?: {
|
|
235
|
+
type?: OpenAPISchemaType;
|
|
236
|
+
properties?: Record<string, any>;
|
|
237
|
+
required?: string[];
|
|
238
|
+
$ref?: string;
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* Infer body and query type from ts interface
|
|
247
|
+
*
|
|
248
|
+
* useful for generic and dynamic types
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* const endpoint = createEndpoint("/path", {
|
|
253
|
+
* method: "POST",
|
|
254
|
+
* body: z.record(z.string()),
|
|
255
|
+
* $Infer: {
|
|
256
|
+
* body: {} as {
|
|
257
|
+
* type: InferTypeFromOptions<Option> // custom type inference
|
|
258
|
+
* }
|
|
259
|
+
* }
|
|
260
|
+
* }, async(ctx)=>{
|
|
261
|
+
* const body = ctx.body
|
|
262
|
+
* })
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
$Infer?: {
|
|
266
|
+
/**
|
|
267
|
+
* Body
|
|
268
|
+
*/
|
|
269
|
+
body?: any;
|
|
270
|
+
/**
|
|
271
|
+
* Query
|
|
272
|
+
*/
|
|
273
|
+
query?: Record<string, any>;
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* If enabled, endpoint won't be exposed over a router
|
|
277
|
+
* @deprecated Use path-less endpoints instead
|
|
278
|
+
*/
|
|
279
|
+
SERVER_ONLY?: boolean;
|
|
280
|
+
/**
|
|
281
|
+
* If enabled, endpoint won't be exposed as an action to the client
|
|
282
|
+
* @deprecated Use path-less endpoints instead
|
|
283
|
+
*/
|
|
284
|
+
isAction?: boolean;
|
|
285
|
+
/**
|
|
286
|
+
* Defines the places where the endpoint will be available
|
|
287
|
+
*
|
|
288
|
+
* Possible options:
|
|
289
|
+
* - `rpc` - the endpoint is exposed to the router, can be invoked directly and is available to the client
|
|
290
|
+
* - `server` - the endpoint is exposed to the router, can be invoked directly, but is not available to the client
|
|
291
|
+
* - `http` - the endpoint is only exposed to the router
|
|
292
|
+
* @default "rpc"
|
|
293
|
+
*/
|
|
294
|
+
scope?: "rpc" | "server" | "http";
|
|
295
|
+
/**
|
|
296
|
+
* List of allowed media types (MIME types) for the endpoint
|
|
297
|
+
*
|
|
298
|
+
* if provided, only the media types in the list will be allowed to be passed in the body
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```ts
|
|
302
|
+
* const endpoint = createEndpoint("/path", {
|
|
303
|
+
* method: "POST",
|
|
304
|
+
* allowedMediaTypes: ["application/json", "application/x-www-form-urlencoded"],
|
|
305
|
+
* }, async(ctx)=>{
|
|
306
|
+
* const body = ctx.body
|
|
307
|
+
* })
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
allowedMediaTypes?: string[];
|
|
311
|
+
/**
|
|
312
|
+
* Extra metadata
|
|
313
|
+
*/
|
|
314
|
+
[key: string]: any;
|
|
315
|
+
};
|
|
316
|
+
/**
|
|
317
|
+
* List of middlewares to use
|
|
318
|
+
*/
|
|
319
|
+
use?: Middleware[];
|
|
320
|
+
/**
|
|
321
|
+
* A callback to run before any API error is throw or returned
|
|
322
|
+
*
|
|
323
|
+
* @param e - The API error
|
|
324
|
+
* @returns - The response to return
|
|
325
|
+
*/
|
|
326
|
+
onAPIError?: (e: APIError) => void | Promise<void>;
|
|
327
|
+
/**
|
|
328
|
+
* A callback to run before a validation error is thrown
|
|
329
|
+
* You can customize the validation error message by throwing your own APIError
|
|
330
|
+
*/
|
|
331
|
+
onValidationError?: ({
|
|
332
|
+
issues,
|
|
333
|
+
message
|
|
334
|
+
}: {
|
|
335
|
+
message: string;
|
|
336
|
+
issues: readonly StandardSchemaV1.Issue[];
|
|
337
|
+
}) => void | Promise<void>;
|
|
338
|
+
}
|
|
339
|
+
type EndpointBodyMethodOptions = {
|
|
340
|
+
/**
|
|
341
|
+
* Request Method
|
|
342
|
+
*/
|
|
343
|
+
method: "POST" | "PUT" | "DELETE" | "PATCH" | ("POST" | "PUT" | "DELETE" | "PATCH")[];
|
|
344
|
+
/**
|
|
345
|
+
* Body Schema
|
|
346
|
+
*/
|
|
347
|
+
body?: StandardSchemaV1;
|
|
348
|
+
} | {
|
|
349
|
+
/**
|
|
350
|
+
* Request Method
|
|
351
|
+
*/
|
|
352
|
+
method: "GET" | "HEAD" | ("GET" | "HEAD")[];
|
|
353
|
+
/**
|
|
354
|
+
* Body Schema
|
|
355
|
+
*/
|
|
356
|
+
body?: never;
|
|
357
|
+
} | {
|
|
358
|
+
/**
|
|
359
|
+
* Request Method
|
|
360
|
+
*/
|
|
361
|
+
method: "*";
|
|
362
|
+
/**
|
|
363
|
+
* Body Schema
|
|
364
|
+
*/
|
|
365
|
+
body?: StandardSchemaV1;
|
|
366
|
+
} | {
|
|
367
|
+
/**
|
|
368
|
+
* Request Method
|
|
369
|
+
*/
|
|
370
|
+
method: ("POST" | "PUT" | "DELETE" | "PATCH" | "GET" | "HEAD")[];
|
|
371
|
+
/**
|
|
372
|
+
* Body Schema
|
|
373
|
+
*/
|
|
374
|
+
body?: StandardSchemaV1;
|
|
375
|
+
};
|
|
376
|
+
type EndpointOptions = EndpointBaseOptions & EndpointBodyMethodOptions;
|
|
377
|
+
//#endregion
|
|
378
|
+
//#region ../../node_modules/.pnpm/better-call@1.3.5_zod@4.3.6/node_modules/better-call/dist/middleware.d.mts
|
|
379
|
+
//#region src/middleware.d.ts
|
|
380
|
+
interface MiddlewareOptions extends Omit<EndpointOptions, "method"> {}
|
|
381
|
+
type MiddlewareInputContext<Options extends MiddlewareOptions> = InferBodyInput<Options> & InferQueryInput<Options> & InferRequestInput<Options> & InferHeadersInput<Options> & {
|
|
382
|
+
asResponse?: boolean;
|
|
383
|
+
returnHeaders?: boolean;
|
|
384
|
+
use?: Middleware[];
|
|
385
|
+
};
|
|
386
|
+
type Middleware<Options extends MiddlewareOptions = MiddlewareOptions, Handler extends (inputCtx: any) => Promise<any> = any> = Handler & {
|
|
387
|
+
options: Options;
|
|
388
|
+
}; //#endregion
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region ../../node_modules/.pnpm/better-call@1.3.5_zod@4.3.6/node_modules/better-call/dist/context.d.mts
|
|
391
|
+
type InferBodyInput<Options extends EndpointOptions | MiddlewareOptions, Body = (Options["metadata"] extends {
|
|
392
|
+
$Infer: {
|
|
393
|
+
body: infer B;
|
|
394
|
+
};
|
|
395
|
+
} ? B : Options["body"] extends StandardSchemaV1 ? StandardSchemaV1.InferInput<Options["body"]> : undefined)> = undefined extends Body ? {
|
|
396
|
+
body?: Body;
|
|
397
|
+
} : {
|
|
398
|
+
body: Body;
|
|
399
|
+
};
|
|
400
|
+
type InferQueryInput<Options extends EndpointOptions | MiddlewareOptions, Query = (Options["metadata"] extends {
|
|
401
|
+
$Infer: {
|
|
402
|
+
query: infer Query;
|
|
403
|
+
};
|
|
404
|
+
} ? Query : Options["query"] extends StandardSchemaV1 ? StandardSchemaV1.InferInput<Options["query"]> : Record<string, any> | undefined)> = undefined extends Query ? {
|
|
405
|
+
query?: Query;
|
|
406
|
+
} : {
|
|
407
|
+
query: Query;
|
|
408
|
+
};
|
|
409
|
+
type InferRequestInput<Option extends EndpointOptions | MiddlewareOptions> = Option["requireRequest"] extends true ? {
|
|
410
|
+
request: Request;
|
|
411
|
+
} : {
|
|
412
|
+
request?: Request;
|
|
413
|
+
};
|
|
414
|
+
type InferHeadersInput<Option extends EndpointOptions | MiddlewareOptions> = Option["requireHeaders"] extends true ? {
|
|
415
|
+
headers: HeadersInit;
|
|
416
|
+
} : {
|
|
417
|
+
headers?: HeadersInit;
|
|
418
|
+
};
|
|
419
|
+
//#endregion
|
|
420
|
+
//#region src/types.d.ts
|
|
421
|
+
type ALL_PLUGIN_ERROR_CODE_KEYS = keyof UnionToIntersection<{ [Key in Exclude<BetterAuthPluginRegistryIdentifier, "i18n">]: BetterAuthPluginRegistry<unknown, unknown>[Key] extends {
|
|
422
|
+
creator: infer C;
|
|
423
|
+
} ? C extends ((...args: any[]) => infer P) ? P extends {
|
|
424
|
+
$ERROR_CODES: infer E;
|
|
425
|
+
} ? E : {} : {} : {} }[Exclude<BetterAuthPluginRegistryIdentifier, "i18n">]>;
|
|
426
|
+
type InternalTranslationDictionary = Partial<{ [Key in ALL_PLUGIN_ERROR_CODE_KEYS]: string }>;
|
|
427
|
+
/**
|
|
428
|
+
* Translation dictionary mapping error codes to translated messages
|
|
429
|
+
*/
|
|
430
|
+
type TranslationDictionary = InternalTranslationDictionary & Record<string, string>;
|
|
431
|
+
/**
|
|
432
|
+
* Locale detection strategy
|
|
433
|
+
*/
|
|
434
|
+
type LocaleDetectionStrategy = "header" | "cookie" | "session" | "callback";
|
|
435
|
+
/**
|
|
436
|
+
* Options for the i18n plugin
|
|
437
|
+
*/
|
|
438
|
+
interface I18nOptions<Locales extends string[]> {
|
|
439
|
+
/**
|
|
440
|
+
* Translation dictionaries keyed by locale code
|
|
441
|
+
* @example
|
|
442
|
+
* {
|
|
443
|
+
* en: { USER_NOT_FOUND: "User not found" },
|
|
444
|
+
* fr: { USER_NOT_FOUND: "Utilisateur non trouvé" }
|
|
445
|
+
* }
|
|
446
|
+
*/
|
|
447
|
+
translations: { [Locale in Locales[number]]: TranslationDictionary };
|
|
448
|
+
/**
|
|
449
|
+
* Default/fallback locale when detection fails
|
|
450
|
+
* @default "en"
|
|
451
|
+
*/
|
|
452
|
+
defaultLocale?: Locales[number] | undefined;
|
|
453
|
+
/**
|
|
454
|
+
* Locale detection strategies in priority order
|
|
455
|
+
* @default ["header"]
|
|
456
|
+
*/
|
|
457
|
+
detection?: LocaleDetectionStrategy[] | undefined;
|
|
458
|
+
/**
|
|
459
|
+
* Cookie name for locale detection (when "cookie" strategy is used)
|
|
460
|
+
* @default "locale"
|
|
461
|
+
*/
|
|
462
|
+
localeCookie?: string | undefined;
|
|
463
|
+
/**
|
|
464
|
+
* User field name for stored locale preference (when "session" strategy is used)
|
|
465
|
+
* @default "locale"
|
|
466
|
+
*/
|
|
467
|
+
userLocaleField?: string | undefined;
|
|
468
|
+
/**
|
|
469
|
+
* Custom locale detection function (when "callback" strategy is used).
|
|
470
|
+
* @example
|
|
471
|
+
* getLocale: (ctx) => {
|
|
472
|
+
* return ctx.headers?.get("X-Custom-Locale") ?? null;
|
|
473
|
+
* }
|
|
474
|
+
*/
|
|
475
|
+
getLocale?: undefined | ((ctx: GenericEndpointContext) => Promise<Locales[number] | null> | Locales[number] | null);
|
|
476
|
+
}
|
|
477
|
+
//#endregion
|
|
478
|
+
//#region src/index.d.ts
|
|
479
|
+
declare module "@better-auth/core" {
|
|
480
|
+
interface BetterAuthPluginRegistry<AuthOptions, Options> {
|
|
481
|
+
i18n: {
|
|
482
|
+
creator: typeof i18n;
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* i18n plugin for Better Auth
|
|
488
|
+
*
|
|
489
|
+
* Translates error messages based on detected locale.
|
|
490
|
+
*
|
|
491
|
+
* @example
|
|
492
|
+
* ```ts
|
|
493
|
+
* import { betterAuth } from "better-auth";
|
|
494
|
+
* import { i18n } from "@better-auth/i18n";
|
|
495
|
+
*
|
|
496
|
+
* export const auth = betterAuth({
|
|
497
|
+
* plugins: [
|
|
498
|
+
* i18n({
|
|
499
|
+
* translations: {
|
|
500
|
+
* en: { USER_NOT_FOUND: "User not found" },
|
|
501
|
+
* fr: { USER_NOT_FOUND: "Utilisateur non trouvé" },
|
|
502
|
+
* },
|
|
503
|
+
* detection: ["header", "cookie"],
|
|
504
|
+
* }),
|
|
505
|
+
* ],
|
|
506
|
+
* });
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
declare const i18n: <Locales extends string[]>(options: I18nOptions<Locales>) => {
|
|
510
|
+
id: "i18n";
|
|
511
|
+
version: string;
|
|
512
|
+
hooks: {
|
|
513
|
+
after: {
|
|
514
|
+
matcher: () => true;
|
|
515
|
+
handler: (inputContext: MiddlewareInputContext<MiddlewareOptions>) => Promise<void>;
|
|
516
|
+
}[];
|
|
517
|
+
};
|
|
518
|
+
options: {
|
|
519
|
+
translations: { [Locale in Locales[number]]: TranslationDictionary };
|
|
520
|
+
defaultLocale: Locales[number];
|
|
521
|
+
detection: LocaleDetectionStrategy[];
|
|
522
|
+
localeCookie: string;
|
|
523
|
+
userLocaleField: string;
|
|
524
|
+
getLocale?: ((ctx: GenericEndpointContext) => Locales[number] | Promise<Locales[number] | null> | null) | undefined;
|
|
525
|
+
};
|
|
526
|
+
};
|
|
527
|
+
//#endregion
|
|
528
|
+
export { TranslationDictionary as i, I18nOptions as n, LocaleDetectionStrategy as r, i18n as t };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as TranslationDictionary, n as I18nOptions, r as LocaleDetectionStrategy, t as i18n } from "./index-
|
|
1
|
+
import { i as TranslationDictionary, n as I18nOptions, r as LocaleDetectionStrategy, t as i18n } from "./index-Dfuc66o9.mjs";
|
|
2
2
|
export { I18nOptions, LocaleDetectionStrategy, TranslationDictionary, i18n };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as PACKAGE_VERSION } from "./version-
|
|
1
|
+
import { t as PACKAGE_VERSION } from "./version-CfoAqE_A.mjs";
|
|
2
2
|
import { APIError, createAuthMiddleware, isAPIError } from "better-auth/api";
|
|
3
3
|
import { parseCookies } from "better-auth/cookies";
|
|
4
4
|
//#region src/index.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-auth/i18n",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "i18n plugin for Better Auth - translate error messages",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,12 +51,12 @@
|
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"tsdown": "0.21.1",
|
|
54
|
-
"@better-auth/core": "1.6.
|
|
55
|
-
"better-auth": "1.6.
|
|
54
|
+
"@better-auth/core": "1.6.1",
|
|
55
|
+
"better-auth": "1.6.1"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
|
-
"@better-auth/core": "^1.6.
|
|
59
|
-
"better-auth": "^1.6.
|
|
58
|
+
"@better-auth/core": "^1.6.1",
|
|
59
|
+
"better-auth": "^1.6.1"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|
|
@@ -1,431 +0,0 @@
|
|
|
1
|
-
import { BetterAuthPluginRegistry, BetterAuthPluginRegistryIdentifier, GenericEndpointContext, UnionToIntersection } from "@better-auth/core";
|
|
2
|
-
|
|
3
|
-
//#region ../../node_modules/.pnpm/better-call@2.0.3_zod@4.3.6/node_modules/better-call/dist/cookies.d.mts
|
|
4
|
-
//#region src/cookies.d.ts
|
|
5
|
-
type CookiePrefixOptions = "host" | "secure";
|
|
6
|
-
type CookieOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* Domain of the cookie
|
|
9
|
-
*
|
|
10
|
-
* The Domain attribute specifies which server can receive a cookie. If specified, cookies are
|
|
11
|
-
* available on the specified server and its subdomains. If the it is not
|
|
12
|
-
* specified, the cookies are available on the server that sets it but not on
|
|
13
|
-
* its subdomains.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* `domain: "example.com"`
|
|
17
|
-
*/
|
|
18
|
-
domain?: string;
|
|
19
|
-
/**
|
|
20
|
-
* A lifetime of a cookie. Permanent cookies are deleted after the date specified in the
|
|
21
|
-
* Expires attribute:
|
|
22
|
-
*
|
|
23
|
-
* Expires has been available for longer than Max-Age, however Max-Age is less error-prone, and
|
|
24
|
-
* takes precedence when both are set. The rationale behind this is that when you set an
|
|
25
|
-
* Expires date and time, they're relative to the client the cookie is being set on. If the
|
|
26
|
-
* server is set to a different time, this could cause errors
|
|
27
|
-
*/
|
|
28
|
-
expires?: Date;
|
|
29
|
-
/**
|
|
30
|
-
* Forbids JavaScript from accessing the cookie, for example, through the Document.cookie
|
|
31
|
-
* property. Note that a cookie that has been created with HttpOnly will still be sent with
|
|
32
|
-
* JavaScript-initiated requests, for example, when calling XMLHttpRequest.send() or fetch().
|
|
33
|
-
* This mitigates attacks against cross-site scripting
|
|
34
|
-
*/
|
|
35
|
-
httpOnly?: boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Indicates the number of seconds until the cookie expires. A zero or negative number will
|
|
38
|
-
* expire the cookie immediately. If both Expires and Max-Age are set, Max-Age has precedence.
|
|
39
|
-
*
|
|
40
|
-
* @example 604800 - 7 days
|
|
41
|
-
*/
|
|
42
|
-
maxAge?: number;
|
|
43
|
-
/**
|
|
44
|
-
* Indicates the path that must exist in the requested URL for the browser to send the Cookie
|
|
45
|
-
* header.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* "/docs"
|
|
49
|
-
* // -> the request paths /docs, /docs/, /docs/Web/, and /docs/Web/HTTP will all match. the request paths /, /fr/docs will not match.
|
|
50
|
-
*/
|
|
51
|
-
path?: string;
|
|
52
|
-
/**
|
|
53
|
-
* Indicates that the cookie is sent to the server only when a request is made with the https:
|
|
54
|
-
* scheme (except on localhost), and therefore, is more resistant to man-in-the-middle attacks.
|
|
55
|
-
*/
|
|
56
|
-
secure?: boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Controls whether or not a cookie is sent with cross-site requests, providing some protection
|
|
59
|
-
* against cross-site request forgery attacks (CSRF).
|
|
60
|
-
*
|
|
61
|
-
* Strict - Means that the browser sends the cookie only for same-site requests, that is,
|
|
62
|
-
* requests originating from the same site that set the cookie. If a request originates from a
|
|
63
|
-
* different domain or scheme (even with the same domain), no cookies with the SameSite=Strict
|
|
64
|
-
* attribute are sent.
|
|
65
|
-
*
|
|
66
|
-
* Lax - Means that the cookie is not sent on cross-site requests, such as on requests to load
|
|
67
|
-
* images or frames, but is sent when a user is navigating to the origin site from an external
|
|
68
|
-
* site (for example, when following a link). This is the default behavior if the SameSite
|
|
69
|
-
* attribute is not specified.
|
|
70
|
-
*
|
|
71
|
-
* None - Means that the browser sends the cookie with both cross-site and same-site requests.
|
|
72
|
-
* The Secure attribute must also be set when setting this value.
|
|
73
|
-
*/
|
|
74
|
-
sameSite?: "Strict" | "Lax" | "None" | "strict" | "lax" | "none";
|
|
75
|
-
/**
|
|
76
|
-
* Indicates that the cookie should be stored using partitioned storage. Note that if this is
|
|
77
|
-
* set, the Secure directive must also be set.
|
|
78
|
-
*
|
|
79
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies
|
|
80
|
-
*/
|
|
81
|
-
partitioned?: boolean;
|
|
82
|
-
/**
|
|
83
|
-
* Cooke Prefix
|
|
84
|
-
*
|
|
85
|
-
* - secure: `__Secure-` -> `__Secure-cookie-name`
|
|
86
|
-
* - host: `__Host-` -> `__Host-cookie-name`
|
|
87
|
-
*
|
|
88
|
-
* `secure` must be set to true to use prefixes
|
|
89
|
-
*/
|
|
90
|
-
prefix?: CookiePrefixOptions;
|
|
91
|
-
};
|
|
92
|
-
//#endregion
|
|
93
|
-
//#region ../../node_modules/.pnpm/better-call@2.0.3_zod@4.3.6/node_modules/better-call/dist/error.d.mts
|
|
94
|
-
declare const statusCodes: {
|
|
95
|
-
OK: number;
|
|
96
|
-
CREATED: number;
|
|
97
|
-
ACCEPTED: number;
|
|
98
|
-
NO_CONTENT: number;
|
|
99
|
-
MULTIPLE_CHOICES: number;
|
|
100
|
-
MOVED_PERMANENTLY: number;
|
|
101
|
-
FOUND: number;
|
|
102
|
-
SEE_OTHER: number;
|
|
103
|
-
NOT_MODIFIED: number;
|
|
104
|
-
TEMPORARY_REDIRECT: number;
|
|
105
|
-
BAD_REQUEST: number;
|
|
106
|
-
UNAUTHORIZED: number;
|
|
107
|
-
PAYMENT_REQUIRED: number;
|
|
108
|
-
FORBIDDEN: number;
|
|
109
|
-
NOT_FOUND: number;
|
|
110
|
-
METHOD_NOT_ALLOWED: number;
|
|
111
|
-
NOT_ACCEPTABLE: number;
|
|
112
|
-
PROXY_AUTHENTICATION_REQUIRED: number;
|
|
113
|
-
REQUEST_TIMEOUT: number;
|
|
114
|
-
CONFLICT: number;
|
|
115
|
-
GONE: number;
|
|
116
|
-
LENGTH_REQUIRED: number;
|
|
117
|
-
PRECONDITION_FAILED: number;
|
|
118
|
-
PAYLOAD_TOO_LARGE: number;
|
|
119
|
-
URI_TOO_LONG: number;
|
|
120
|
-
UNSUPPORTED_MEDIA_TYPE: number;
|
|
121
|
-
RANGE_NOT_SATISFIABLE: number;
|
|
122
|
-
EXPECTATION_FAILED: number;
|
|
123
|
-
"I'M_A_TEAPOT": number;
|
|
124
|
-
MISDIRECTED_REQUEST: number;
|
|
125
|
-
UNPROCESSABLE_ENTITY: number;
|
|
126
|
-
LOCKED: number;
|
|
127
|
-
FAILED_DEPENDENCY: number;
|
|
128
|
-
TOO_EARLY: number;
|
|
129
|
-
UPGRADE_REQUIRED: number;
|
|
130
|
-
PRECONDITION_REQUIRED: number;
|
|
131
|
-
TOO_MANY_REQUESTS: number;
|
|
132
|
-
REQUEST_HEADER_FIELDS_TOO_LARGE: number;
|
|
133
|
-
UNAVAILABLE_FOR_LEGAL_REASONS: number;
|
|
134
|
-
INTERNAL_SERVER_ERROR: number;
|
|
135
|
-
NOT_IMPLEMENTED: number;
|
|
136
|
-
BAD_GATEWAY: number;
|
|
137
|
-
SERVICE_UNAVAILABLE: number;
|
|
138
|
-
GATEWAY_TIMEOUT: number;
|
|
139
|
-
HTTP_VERSION_NOT_SUPPORTED: number;
|
|
140
|
-
VARIANT_ALSO_NEGOTIATES: number;
|
|
141
|
-
INSUFFICIENT_STORAGE: number;
|
|
142
|
-
LOOP_DETECTED: number;
|
|
143
|
-
NOT_EXTENDED: number;
|
|
144
|
-
NETWORK_AUTHENTICATION_REQUIRED: number;
|
|
145
|
-
};
|
|
146
|
-
type Status = 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511;
|
|
147
|
-
declare class InternalAPIError extends Error {
|
|
148
|
-
status: keyof typeof statusCodes | Status;
|
|
149
|
-
body: ({
|
|
150
|
-
message?: string;
|
|
151
|
-
code?: string;
|
|
152
|
-
cause?: unknown;
|
|
153
|
-
} & Record<string, any>) | undefined;
|
|
154
|
-
headers: HeadersInit;
|
|
155
|
-
statusCode: number;
|
|
156
|
-
constructor(status?: keyof typeof statusCodes | Status, body?: ({
|
|
157
|
-
message?: string;
|
|
158
|
-
code?: string;
|
|
159
|
-
cause?: unknown;
|
|
160
|
-
} & Record<string, any>) | undefined, headers?: HeadersInit, statusCode?: number);
|
|
161
|
-
}
|
|
162
|
-
type APIError = InstanceType<typeof InternalAPIError>;
|
|
163
|
-
declare const APIError: new (status?: Status | "OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "FOUND" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "REQUEST_TIMEOUT" | "CONFLICT" | "GONE" | "LENGTH_REQUIRED" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "URI_TOO_LONG" | "UNSUPPORTED_MEDIA_TYPE" | "RANGE_NOT_SATISFIABLE" | "EXPECTATION_FAILED" | "I'M_A_TEAPOT" | "MISDIRECTED_REQUEST" | "UNPROCESSABLE_ENTITY" | "LOCKED" | "FAILED_DEPENDENCY" | "TOO_EARLY" | "UPGRADE_REQUIRED" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "REQUEST_HEADER_FIELDS_TOO_LARGE" | "UNAVAILABLE_FOR_LEGAL_REASONS" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "HTTP_VERSION_NOT_SUPPORTED" | "VARIANT_ALSO_NEGOTIATES" | "INSUFFICIENT_STORAGE" | "LOOP_DETECTED" | "NOT_EXTENDED" | "NETWORK_AUTHENTICATION_REQUIRED" | undefined, body?: ({
|
|
164
|
-
message?: string;
|
|
165
|
-
code?: string;
|
|
166
|
-
cause?: unknown;
|
|
167
|
-
} & Record<string, any>) | undefined, headers?: HeadersInit | undefined, statusCode?: number | undefined) => InternalAPIError & {
|
|
168
|
-
errorStack: string | undefined;
|
|
169
|
-
}; //#endregion
|
|
170
|
-
//#endregion
|
|
171
|
-
//#region ../../node_modules/.pnpm/better-call@2.0.3_zod@4.3.6/node_modules/better-call/dist/helper.d.mts
|
|
172
|
-
type Prettify<T> = 0 extends 1 & T ? any : { [K in keyof T]: T[K] } & {};
|
|
173
|
-
//#endregion
|
|
174
|
-
//#region ../../node_modules/.pnpm/better-call@2.0.3_zod@4.3.6/node_modules/better-call/dist/middleware.d.mts
|
|
175
|
-
//#region src/middleware.d.ts
|
|
176
|
-
type MiddlewareContext<Context = {}> = {
|
|
177
|
-
/**
|
|
178
|
-
* Method
|
|
179
|
-
*
|
|
180
|
-
* The request method
|
|
181
|
-
*/
|
|
182
|
-
method: string;
|
|
183
|
-
/**
|
|
184
|
-
* Path
|
|
185
|
-
*
|
|
186
|
-
* The path of the endpoint
|
|
187
|
-
*/
|
|
188
|
-
path: string;
|
|
189
|
-
/**
|
|
190
|
-
* Body
|
|
191
|
-
*
|
|
192
|
-
* The body object will be the parsed JSON from the request and validated
|
|
193
|
-
* against the body schema if it exists
|
|
194
|
-
*/
|
|
195
|
-
body: any;
|
|
196
|
-
/**
|
|
197
|
-
* Query
|
|
198
|
-
*
|
|
199
|
-
* The query object will be the parsed query string from the request
|
|
200
|
-
* and validated against the query schema if it exists
|
|
201
|
-
*/
|
|
202
|
-
query: Record<string, any> | undefined;
|
|
203
|
-
/**
|
|
204
|
-
* Params
|
|
205
|
-
*
|
|
206
|
-
* If the path is `/user/:id` and the request is `/user/1` then the
|
|
207
|
-
* params will be `{ id: "1" }` and if the path includes a wildcard like
|
|
208
|
-
* `/user/*` then the params will be `{ _: "1" }` where `_` is the wildcard
|
|
209
|
-
* key. If the wildcard is named like `/user/**:name` then the params will
|
|
210
|
-
* be `{ name: string }`
|
|
211
|
-
*/
|
|
212
|
-
params: Record<string, any> | undefined;
|
|
213
|
-
/**
|
|
214
|
-
* Request object
|
|
215
|
-
*
|
|
216
|
-
* If `requireRequest` is set to true in the endpoint options this will be
|
|
217
|
-
* required
|
|
218
|
-
*/
|
|
219
|
-
request: Request | undefined;
|
|
220
|
-
/**
|
|
221
|
-
* Headers
|
|
222
|
-
*
|
|
223
|
-
* If `requireHeaders` is set to true in the endpoint options this will be
|
|
224
|
-
* required
|
|
225
|
-
*/
|
|
226
|
-
headers: Headers | undefined;
|
|
227
|
-
/**
|
|
228
|
-
* Set header
|
|
229
|
-
*
|
|
230
|
-
* If it's called outside of a request it will just be ignored.
|
|
231
|
-
*/
|
|
232
|
-
setHeader: (key: string, value: string) => void;
|
|
233
|
-
/**
|
|
234
|
-
* Set the response status code
|
|
235
|
-
*/
|
|
236
|
-
setStatus: (status: Status) => void;
|
|
237
|
-
/**
|
|
238
|
-
* Get header
|
|
239
|
-
*
|
|
240
|
-
* If it's called outside of a request it will just return null
|
|
241
|
-
*
|
|
242
|
-
* @param key - The key of the header
|
|
243
|
-
*/
|
|
244
|
-
getHeader: (key: string) => string | null;
|
|
245
|
-
/**
|
|
246
|
-
* Get a cookie value from the request
|
|
247
|
-
*
|
|
248
|
-
* @param key - The key of the cookie
|
|
249
|
-
* @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
|
|
250
|
-
* @returns The value of the cookie
|
|
251
|
-
*/
|
|
252
|
-
getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
|
|
253
|
-
/**
|
|
254
|
-
* Get a signed cookie value from the request
|
|
255
|
-
*
|
|
256
|
-
* @param key - The key of the cookie
|
|
257
|
-
* @param secret - The secret of the signed cookie
|
|
258
|
-
* @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
|
|
259
|
-
* @returns The value of the cookie or null if the cookie is not found or false if the signature is invalid
|
|
260
|
-
*/
|
|
261
|
-
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | null | false>;
|
|
262
|
-
/**
|
|
263
|
-
* Set a cookie value in the response
|
|
264
|
-
*
|
|
265
|
-
* @param key - The key of the cookie
|
|
266
|
-
* @param value - The value to set
|
|
267
|
-
* @param options - The options of the cookie
|
|
268
|
-
* @returns The cookie string
|
|
269
|
-
*/
|
|
270
|
-
setCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
271
|
-
/**
|
|
272
|
-
* Set signed cookie
|
|
273
|
-
*
|
|
274
|
-
* @param key - The key of the cookie
|
|
275
|
-
* @param value - The value to set
|
|
276
|
-
* @param secret - The secret to sign the cookie with
|
|
277
|
-
* @param options - The options of the cookie
|
|
278
|
-
* @returns The cookie string
|
|
279
|
-
*/
|
|
280
|
-
setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
|
|
281
|
-
/**
|
|
282
|
-
* JSON
|
|
283
|
-
*
|
|
284
|
-
* A helper function to create a JSON response with the correct headers
|
|
285
|
-
* and status code. If `asResponse` is set to true in the context then
|
|
286
|
-
* it will return a Response object instead of the JSON object.
|
|
287
|
-
*
|
|
288
|
-
* @param json - The JSON object to return
|
|
289
|
-
* @param routerResponse - The response object to return if `asResponse` is
|
|
290
|
-
* true in the context this will take precedence
|
|
291
|
-
*/
|
|
292
|
-
json: <R extends Record<string, any> | null>(json: R, routerResponse?: {
|
|
293
|
-
status?: number;
|
|
294
|
-
headers?: Record<string, string>;
|
|
295
|
-
response?: Response;
|
|
296
|
-
body?: Record<string, any>;
|
|
297
|
-
} | Response) => R;
|
|
298
|
-
/**
|
|
299
|
-
* Middleware context
|
|
300
|
-
*/
|
|
301
|
-
context: Prettify<Context>;
|
|
302
|
-
/**
|
|
303
|
-
* Redirect to a new URL
|
|
304
|
-
*/
|
|
305
|
-
redirect: (url: string) => APIError;
|
|
306
|
-
/**
|
|
307
|
-
* Return error
|
|
308
|
-
*/
|
|
309
|
-
error: (status: keyof typeof statusCodes | Status, body?: {
|
|
310
|
-
message?: string;
|
|
311
|
-
code?: string;
|
|
312
|
-
} & Record<string, any>, headers?: HeadersInit) => APIError;
|
|
313
|
-
asResponse?: boolean;
|
|
314
|
-
returnHeaders?: boolean;
|
|
315
|
-
returnStatus?: boolean;
|
|
316
|
-
responseHeaders: Headers;
|
|
317
|
-
};
|
|
318
|
-
type DefaultHandler = (inputCtx: MiddlewareContext<any>) => Promise<any>;
|
|
319
|
-
type Middleware<Handler extends (inputCtx: MiddlewareContext<any>) => Promise<any> = DefaultHandler> = Handler & {
|
|
320
|
-
options: Record<string, any>;
|
|
321
|
-
};
|
|
322
|
-
//#endregion
|
|
323
|
-
//#region src/types.d.ts
|
|
324
|
-
type ALL_PLUGIN_ERROR_CODE_KEYS = keyof UnionToIntersection<{ [Key in Exclude<BetterAuthPluginRegistryIdentifier, "i18n">]: BetterAuthPluginRegistry<unknown, unknown>[Key] extends {
|
|
325
|
-
creator: infer C;
|
|
326
|
-
} ? C extends ((...args: any[]) => infer P) ? P extends {
|
|
327
|
-
$ERROR_CODES: infer E;
|
|
328
|
-
} ? E : {} : {} : {} }[Exclude<BetterAuthPluginRegistryIdentifier, "i18n">]>;
|
|
329
|
-
type InternalTranslationDictionary = Partial<{ [Key in ALL_PLUGIN_ERROR_CODE_KEYS]: string }>;
|
|
330
|
-
/**
|
|
331
|
-
* Translation dictionary mapping error codes to translated messages
|
|
332
|
-
*/
|
|
333
|
-
type TranslationDictionary = InternalTranslationDictionary & Record<string, string>;
|
|
334
|
-
/**
|
|
335
|
-
* Locale detection strategy
|
|
336
|
-
*/
|
|
337
|
-
type LocaleDetectionStrategy = "header" | "cookie" | "session" | "callback";
|
|
338
|
-
/**
|
|
339
|
-
* Options for the i18n plugin
|
|
340
|
-
*/
|
|
341
|
-
interface I18nOptions<Locales extends string[]> {
|
|
342
|
-
/**
|
|
343
|
-
* Translation dictionaries keyed by locale code
|
|
344
|
-
* @example
|
|
345
|
-
* {
|
|
346
|
-
* en: { USER_NOT_FOUND: "User not found" },
|
|
347
|
-
* fr: { USER_NOT_FOUND: "Utilisateur non trouvé" }
|
|
348
|
-
* }
|
|
349
|
-
*/
|
|
350
|
-
translations: { [Locale in Locales[number]]: TranslationDictionary };
|
|
351
|
-
/**
|
|
352
|
-
* Default/fallback locale when detection fails
|
|
353
|
-
* @default "en"
|
|
354
|
-
*/
|
|
355
|
-
defaultLocale?: Locales[number] | undefined;
|
|
356
|
-
/**
|
|
357
|
-
* Locale detection strategies in priority order
|
|
358
|
-
* @default ["header"]
|
|
359
|
-
*/
|
|
360
|
-
detection?: LocaleDetectionStrategy[] | undefined;
|
|
361
|
-
/**
|
|
362
|
-
* Cookie name for locale detection (when "cookie" strategy is used)
|
|
363
|
-
* @default "locale"
|
|
364
|
-
*/
|
|
365
|
-
localeCookie?: string | undefined;
|
|
366
|
-
/**
|
|
367
|
-
* User field name for stored locale preference (when "session" strategy is used)
|
|
368
|
-
* @default "locale"
|
|
369
|
-
*/
|
|
370
|
-
userLocaleField?: string | undefined;
|
|
371
|
-
/**
|
|
372
|
-
* Custom locale detection function (when "callback" strategy is used).
|
|
373
|
-
* @example
|
|
374
|
-
* getLocale: (ctx) => {
|
|
375
|
-
* return ctx.headers?.get("X-Custom-Locale") ?? null;
|
|
376
|
-
* }
|
|
377
|
-
*/
|
|
378
|
-
getLocale?: undefined | ((ctx: GenericEndpointContext) => Promise<Locales[number] | null> | Locales[number] | null);
|
|
379
|
-
}
|
|
380
|
-
//#endregion
|
|
381
|
-
//#region src/index.d.ts
|
|
382
|
-
declare module "@better-auth/core" {
|
|
383
|
-
interface BetterAuthPluginRegistry<AuthOptions, Options> {
|
|
384
|
-
i18n: {
|
|
385
|
-
creator: typeof i18n;
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* i18n plugin for Better Auth
|
|
391
|
-
*
|
|
392
|
-
* Translates error messages based on detected locale.
|
|
393
|
-
*
|
|
394
|
-
* @example
|
|
395
|
-
* ```ts
|
|
396
|
-
* import { betterAuth } from "better-auth";
|
|
397
|
-
* import { i18n } from "@better-auth/i18n";
|
|
398
|
-
*
|
|
399
|
-
* export const auth = betterAuth({
|
|
400
|
-
* plugins: [
|
|
401
|
-
* i18n({
|
|
402
|
-
* translations: {
|
|
403
|
-
* en: { USER_NOT_FOUND: "User not found" },
|
|
404
|
-
* fr: { USER_NOT_FOUND: "Utilisateur non trouvé" },
|
|
405
|
-
* },
|
|
406
|
-
* detection: ["header", "cookie"],
|
|
407
|
-
* }),
|
|
408
|
-
* ],
|
|
409
|
-
* });
|
|
410
|
-
* ```
|
|
411
|
-
*/
|
|
412
|
-
declare const i18n: <Locales extends string[]>(options: I18nOptions<Locales>) => {
|
|
413
|
-
id: "i18n";
|
|
414
|
-
version: string;
|
|
415
|
-
hooks: {
|
|
416
|
-
after: {
|
|
417
|
-
matcher: () => true;
|
|
418
|
-
handler: Middleware<(inputContext: Record<string, any>) => Promise<void>>;
|
|
419
|
-
}[];
|
|
420
|
-
};
|
|
421
|
-
options: {
|
|
422
|
-
translations: { [Locale in Locales[number]]: TranslationDictionary };
|
|
423
|
-
defaultLocale: Locales[number];
|
|
424
|
-
detection: LocaleDetectionStrategy[];
|
|
425
|
-
localeCookie: string;
|
|
426
|
-
userLocaleField: string;
|
|
427
|
-
getLocale?: ((ctx: GenericEndpointContext) => Locales[number] | Promise<Locales[number] | null> | null) | undefined;
|
|
428
|
-
};
|
|
429
|
-
};
|
|
430
|
-
//#endregion
|
|
431
|
-
export { TranslationDictionary as i, I18nOptions as n, LocaleDetectionStrategy as r, i18n as t };
|