-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors.ts
49 lines (45 loc) · 1.62 KB
/
cors.ts
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
// Copyright 2023-2024 the Sequoia authors. All rights reserved. MIT license.
export interface CORSOptions {
readonly origin?: string
readonly methods?: string[]
readonly headers?: string[]
readonly exposeHeaders?: string[]
readonly credentials?: boolean
readonly maxAge?: number
}
export class CORS {
private internalHeaders: Headers
constructor(options: CORSOptions) {
this.internalHeaders = new Headers()
if (options.origin && options.origin.length) {
this.internalHeaders.set('Access-Control-Allow-Origin', options.origin)
}
if (options.methods && options.methods.length) {
this.internalHeaders.set('Access-Control-Allow-Methods', options.methods.join(', '))
}
if (typeof options.credentials === 'boolean') {
this.internalHeaders.set(
'Access-Control-Allow-Credentials',
options.credentials.toString(),
)
}
if (options.exposeHeaders && options.exposeHeaders.length) {
this.internalHeaders.set(
'Access-Control-Expose-Headers',
options.exposeHeaders.join(', '),
)
}
if (typeof options.maxAge === 'number') {
this.internalHeaders.set(
'Access-Control-Allow-Max-Age',
Math.floor(options.maxAge).toString(),
)
}
if (options.headers && options.headers.length) {
this.internalHeaders.set('Access-Control-Allow-Headers', options.headers.join(', '))
}
}
getHeaders() {
return this.internalHeaders
}
}