(options: StandardHandlerOptions<T>)
| 38 | } |
| 39 | |
| 40 | init(options: StandardHandlerOptions<T>): void { |
| 41 | options.rootInterceptors ??= [] |
| 42 | |
| 43 | options.rootInterceptors.unshift(async (interceptorOptions) => { |
| 44 | if (interceptorOptions.request.method === 'OPTIONS') { |
| 45 | const resHeaders: StandardHeaders = {} |
| 46 | |
| 47 | if (this.options.maxAge !== undefined) { |
| 48 | resHeaders['access-control-max-age'] = this.options.maxAge.toString() |
| 49 | } |
| 50 | |
| 51 | if (this.options.allowMethods?.length) { |
| 52 | resHeaders['access-control-allow-methods'] = flattenHeader(this.options.allowMethods) |
| 53 | } |
| 54 | |
| 55 | const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers['access-control-request-headers'] |
| 56 | |
| 57 | if (typeof allowHeaders === 'string' || allowHeaders?.length) { |
| 58 | resHeaders['access-control-allow-headers'] = flattenHeader(allowHeaders) |
| 59 | } |
| 60 | |
| 61 | return { |
| 62 | matched: true, |
| 63 | response: { |
| 64 | status: 204, |
| 65 | headers: resHeaders, |
| 66 | body: undefined, |
| 67 | }, |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return interceptorOptions.next() |
| 72 | }) |
| 73 | |
| 74 | options.rootInterceptors.unshift(async (interceptorOptions) => { |
| 75 | const result = await interceptorOptions.next() |
| 76 | |
| 77 | if (!result.matched) { |
| 78 | return result |
| 79 | } |
| 80 | |
| 81 | const origin = flattenHeader(interceptorOptions.request.headers.origin) ?? '' |
| 82 | |
| 83 | const allowedOrigin = await value(this.options.origin, origin, interceptorOptions) |
| 84 | const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin] |
| 85 | |
| 86 | if (allowedOriginArr.includes('*')) { |
| 87 | result.response.headers['access-control-allow-origin'] = '*' |
| 88 | } |
| 89 | else { |
| 90 | if (allowedOriginArr.includes(origin)) { |
| 91 | result.response.headers['access-control-allow-origin'] = origin |
| 92 | } |
| 93 | |
| 94 | result.response.headers.vary = interceptorOptions.request.headers.vary ?? 'origin' |
| 95 | } |
| 96 | |
| 97 | const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions) |
nothing calls this directly
no test coverage detected