-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathtypes.ts
More file actions
154 lines (133 loc) · 4.91 KB
/
types.ts
File metadata and controls
154 lines (133 loc) · 4.91 KB
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
import type {Locker, Upload} from '@tus/utils'
/**
* Represents the configuration options for a server.
*/
export type ServerOptions = {
/**
* The route to accept requests.
*/
path: string
/**
* Max file size allowed when uploading
*/
maxSize?: number | ((req: Request, uploadId: string | null) => Promise<number> | number)
/**
* Return a relative URL as the `Location` header.
*/
relativeLocation?: boolean
/**
* Allow `Forwarded`, `X-Forwarded-Proto`, and `X-Forwarded-Host` headers
* to override the `Location` header returned by the server.
*/
respectForwardedHeaders?: boolean
/**
* Additional headers sent in `Access-Control-Allow-Headers`.
*/
allowedHeaders?: string[]
/**
* Set `Access-Control-Allow-Credentials` to true or false (the default)
*/
allowedCredentials?: boolean
/**
* Add trusted origins to `Access-Control-Allow-Origin`.
*/
allowedOrigins?: string[]
/**
* Interval in milliseconds for sending progress of an upload over `EVENTS.POST_RECEIVE`
*/
postReceiveInterval?: number
/**
* Control how the upload URL is generated.
* @param req - The incoming HTTP request.
* @param options - Options for generating the URL.
*/
generateUrl?: (
req: Request,
options: {proto: string; host: string; path: string; id: string}
) => string
/**
* Control how the Upload-ID is extracted from the request.
* @param req - The incoming HTTP request.
*/
getFileIdFromRequest?: (req: Request, lastPath?: string) => string | undefined
/**
* Control how you want to name files.
* It is important to make these unique to prevent data loss.
* Only use it if you really need to.
* Default uses `crypto.randomBytes(16).toString('hex')`.
* @param req - The incoming HTTP request.
*/
namingFunction?: (
req: Request,
metadata?: Record<string, string | null>
) => string | Promise<string>
/**
* The Lock interface defines methods for implementing a locking mechanism.
* It is primarily used to ensure exclusive access to resources, such as uploads and their metadata.
*/
locker: Locker | Promise<Locker> | ((req: Request) => Locker | Promise<Locker>)
/**
* This timeout controls how long the server will wait a cancelled lock to do its cleanup.
*/
lockDrainTimeout?: number
/**
* Disallow termination for finished uploads.
*/
disableTerminationForFinishedUploads?: boolean
/**
* `onUploadCreate` will be invoked before a new upload is created.
* If the function returns the (modified) response, the upload will be created.
* If an error is thrown, the HTTP request will be aborted, and the provided `body` and `status_code`
* (or their fallbacks) will be sent to the client. This can be used to implement validation of upload
* metadata or add headers.
* @param req - The incoming HTTP request.
* @param res - The HTTP response.
* @param upload - The Upload object.
*/
onUploadCreate?: (
req: Request,
upload: Upload
) => Promise<{metadata?: Upload['metadata']}>
/**
* `onUploadFinish` will be invoked after an upload is completed but before a response is returned to the client.
* You can optionally return `status_code`, `headers` and `body` to modify the response.
* Note that the tus specification does not allow sending response body nor status code other than 204, but most clients support it.
* If an error is thrown, the HTTP request will be aborted, and the provided `body` and `status_code`
* (or their fallbacks) will be sent to the client. This can be used to implement post-processing validation.
* @param req - The incoming HTTP request.
* @param res - The HTTP response.
* @param upload - The Upload object.
*/
onUploadFinish?: (
req: Request,
upload: Upload
) => Promise<{
status_code?: number
headers?: Record<string, string | number>
body?: string
}>
/**
* `onIncomingRequest` will be invoked when an incoming request is received.
* @param req - The incoming HTTP request.
* @param res - The HTTP response.
* @param uploadId - The ID of the upload.
*/
onIncomingRequest?: (req: Request, uploadId: string) => Promise<void>
/**
* `onResponseError` will be invoked when an error response is about to be sent by the server.
* Use this function to map custom errors to tus errors or for custom observability.
* @param req - The incoming HTTP request.
* @param res - The HTTP response.
* @param err - The error object or response.
*/
onResponseError?: (
req: Request,
err: Error | {status_code: number; body: string}
) =>
| Promise<{status_code: number; body: string} | undefined>
| {status_code: number; body: string}
| undefined
}
export type RouteHandler = (req: Request) => Response | Promise<Response>
export type WithOptional<T, K extends keyof T> = Omit<T, K> & {[P in K]+?: T[P]}
export type WithRequired<T, K extends keyof T> = T & {[P in K]-?: T[P]}