@@ -2,57 +2,115 @@ import type http from 'node:http'
22
33import type { Locker , Upload } from './models'
44
5+ /**
6+ * Represents the configuration options for a server.
7+ */
58export type ServerOptions = {
6- // The route to accept requests.
9+ /**
10+ * The route to accept requests.
11+ */
712 path : string
8- // Return a relative URL as the `Location` header.
13+
14+ /**
15+ * Return a relative URL as the `Location` header.
16+ */
917 relativeLocation ?: boolean
10- // Allow `Forwarded`, `X-Forwarded-Proto`, and `X-Forwarded-Host` headers
11- // to override the `Location` header returned by the server.
18+
19+ /**
20+ * Allow `Forwarded`, `X-Forwarded-Proto`, and `X-Forwarded-Host` headers
21+ * to override the `Location` header returned by the server.
22+ */
1223 respectForwardedHeaders ?: boolean
13- // adds custom headers sent in `Access-Control-Allow-Headers`.
24+
25+ /**
26+ * Additional headers sent in `Access-Control-Allow-Headers`.
27+ */
1428 allowedHeaders ?: string [ ]
15- // Control how the upload url is generated
29+
30+ /**
31+ * Control how the upload URL is generated.
32+ * @param req - The incoming HTTP request.
33+ * @param options - Options for generating the URL.
34+ */
1635 generateUrl ?: (
1736 req : http . IncomingMessage ,
1837 options : { proto : string ; host : string ; baseUrl : string ; path : string ; id : string }
1938 ) => string
20- // Control how the Upload-ID is extracted from the request
39+
40+ /**
41+ * Control how the Upload-ID is extracted from the request.
42+ * @param req - The incoming HTTP request.
43+ */
2144 getFileIdFromRequest ?: ( req : http . IncomingMessage ) => string | void
22- // Control how you want to name files.
23- // It is important to make these unique to prevent data loss. Only use it if you really need to.
24- // Default uses `crypto.randomBytes(16).toString('hex')`.
45+
46+ /**
47+ * Control how you want to name files.
48+ * It is important to make these unique to prevent data loss.
49+ * Only use it if you really need to.
50+ * Default uses `crypto.randomBytes(16).toString('hex')`.
51+ * @param req - The incoming HTTP request.
52+ */
2553 namingFunction ?: ( req : http . IncomingMessage ) => string
26- // locker implementation to support distributed locks
27- locker ?:
54+
55+ /**
56+ * The Lock interface defines methods for implementing a locking mechanism.
57+ * It is primarily used to ensure exclusive access to resources, such as uploads and their metadata.
58+ */
59+ locker :
2860 | Locker
2961 | Promise < Locker >
3062 | ( ( req : http . IncomingMessage ) => Locker | Promise < Locker > )
31- // `onUploadCreate` will be invoked before a new upload is created.
32- // If the function returns the (modified) response, the upload will be created.
33- // If an error is thrown, the HTTP request will be aborted and the provided `body` and `status_code` (or their fallbacks)
34- // will be sent to the client. This can be used to implement validation of upload metadata or add headers.
63+
64+ /**
65+ * `onUploadCreate` will be invoked before a new upload is created.
66+ * If the function returns the (modified) response, the upload will be created.
67+ * If an error is thrown, the HTTP request will be aborted, and the provided `body` and `status_code`
68+ * (or their fallbacks) will be sent to the client. This can be used to implement validation of upload
69+ * metadata or add headers.
70+ * @param req - The incoming HTTP request.
71+ * @param res - The HTTP response.
72+ * @param upload - The Upload object.
73+ */
3574 onUploadCreate ?: (
3675 req : http . IncomingMessage ,
3776 res : http . ServerResponse ,
3877 upload : Upload
3978 ) => Promise < http . ServerResponse >
40- // `onUploadFinish` will be invoked after an upload is completed but before a response is returned to the client.
41- // If the function returns the (modified) response, the upload will finish.
42- // If an error is thrown, the HTTP request will be aborted and the provided `body` and `status_code` (or their fallbacks)
43- // will be sent to the client. This can be used to implement post-processing validation.
79+
80+ /**
81+ * `onUploadFinish` will be invoked after an upload is completed but before a response is returned to the client.
82+ * If the function returns the (modified) response, the upload will finish.
83+ * If an error is thrown, the HTTP request will be aborted, and the provided `body` and `status_code`
84+ * (or their fallbacks) will be sent to the client. This can be used to implement post-processing validation.
85+ * @param req - The incoming HTTP request.
86+ * @param res - The HTTP response.
87+ * @param upload - The Upload object.
88+ */
4489 onUploadFinish ?: (
4590 req : http . IncomingMessage ,
4691 res : http . ServerResponse ,
4792 upload : Upload
4893 ) => Promise < http . ServerResponse >
94+
95+ /**
96+ * `onIncomingRequest` will be invoked when an incoming request is received.
97+ * @param req - The incoming HTTP request.
98+ * @param res - The HTTP response.
99+ * @param uploadId - The ID of the upload.
100+ */
49101 onIncomingRequest ?: (
50102 req : http . IncomingMessage ,
51103 res : http . ServerResponse ,
52104 uploadId : string
53105 ) => Promise < void >
54- // `onResponseError` will be invoked when an error response is about to be sent by the server.
55- // you use this function to map custom errors to tus errors or for custom observability.
106+
107+ /**
108+ * `onResponseError` will be invoked when an error response is about to be sent by the server.
109+ * Use this function to map custom errors to tus errors or for custom observability.
110+ * @param req - The incoming HTTP request.
111+ * @param res - The HTTP response.
112+ * @param err - The error object or response.
113+ */
56114 onResponseError ?: (
57115 req : http . IncomingMessage ,
58116 res : http . ServerResponse ,
@@ -65,4 +123,6 @@ export type ServerOptions = {
65123
66124export type RouteHandler = ( req : http . IncomingMessage , res : http . ServerResponse ) => void
67125
126+ export type WithOptional < T , K extends keyof T > = Omit < T , K > & { [ P in K ] +?: T [ P ] }
127+
68128export type WithRequired < T , K extends keyof T > = T & { [ P in K ] -?: T [ P ] }
0 commit comments