|
| 1 | +import { Duration } from '@aws-cdk/core'; |
| 2 | +import { ALL_METHODS } from './util'; |
| 3 | + |
| 4 | +export interface CorsOptions { |
| 5 | + /** |
| 6 | + * Specifies the response status code returned from the OPTIONS method. |
| 7 | + * |
| 8 | + * @default 204 |
| 9 | + */ |
| 10 | + readonly statusCode?: number; |
| 11 | + |
| 12 | + /** |
| 13 | + * The Access-Control-Allow-Origin response header indicates whether the |
| 14 | + * response can be shared with requesting code from the given origin. |
| 15 | + * |
| 16 | + * Specifies the list of origins that are allowed to make requests to this resource. |
| 17 | + * |
| 18 | + * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin |
| 19 | + */ |
| 20 | + readonly allowOrigins: string[]; |
| 21 | + |
| 22 | + /** |
| 23 | + * The Access-Control-Allow-Headers response header is used in response to a |
| 24 | + * preflight request which includes the Access-Control-Request-Headers to |
| 25 | + * indicate which HTTP headers can be used during the actual request. |
| 26 | + * |
| 27 | + * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers |
| 28 | + * @default Cors.DEFAULT_HEADERS |
| 29 | + */ |
| 30 | + readonly allowHeaders?: string[]; |
| 31 | + |
| 32 | + /** |
| 33 | + * The Access-Control-Allow-Methods response header specifies the method or |
| 34 | + * methods allowed when accessing the resource in response to a preflight request. |
| 35 | + * |
| 36 | + * If `ANY` is specified, it will be expanded to `Cors.ALL_METHODS`. |
| 37 | + * |
| 38 | + * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods |
| 39 | + * @default Cors.ALL_METHODS |
| 40 | + */ |
| 41 | + readonly allowMethods?: string[]; |
| 42 | + |
| 43 | + /** |
| 44 | + * The Access-Control-Allow-Credentials response header tells browsers whether |
| 45 | + * to expose the response to frontend JavaScript code when the request's |
| 46 | + * credentials mode (Request.credentials) is "include". |
| 47 | + * |
| 48 | + * When a request's credentials mode (Request.credentials) is "include", |
| 49 | + * browsers will only expose the response to frontend JavaScript code if the |
| 50 | + * Access-Control-Allow-Credentials value is true. |
| 51 | + * |
| 52 | + * Credentials are cookies, authorization headers or TLS client certificates. |
| 53 | + * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials |
| 54 | + * @default false |
| 55 | + */ |
| 56 | + readonly allowCredentials?: boolean; |
| 57 | + |
| 58 | + /** |
| 59 | + * The Access-Control-Max-Age response header indicates how long the results of |
| 60 | + * a preflight request (that is the information contained in the |
| 61 | + * Access-Control-Allow-Methods and Access-Control-Allow-Headers headers) |
| 62 | + * can be cached. |
| 63 | + * |
| 64 | + * To disable caching altogther use `disableCache: true`. |
| 65 | + * |
| 66 | + * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age |
| 67 | + * @default - browser-specific (see reference) |
| 68 | + */ |
| 69 | + readonly maxAge?: Duration; |
| 70 | + |
| 71 | + /** |
| 72 | + * Sets Access-Control-Max-Age to -1, which means that caching is disabled. |
| 73 | + * This option cannot be used with `maxAge`. |
| 74 | + * |
| 75 | + * @default - cache is enabled |
| 76 | + */ |
| 77 | + readonly disableCache?: boolean; |
| 78 | + |
| 79 | + /** |
| 80 | + * The Access-Control-Expose-Headers response header indicates which headers |
| 81 | + * can be exposed as part of the response by listing their names. |
| 82 | + * |
| 83 | + * If you want clients to be able to access other headers, you have to list |
| 84 | + * them using the Access-Control-Expose-Headers header. |
| 85 | + * |
| 86 | + * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers |
| 87 | + * |
| 88 | + * @default - only the 6 CORS-safelisted response headers are exposed: |
| 89 | + * Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, |
| 90 | + * Pragma |
| 91 | + */ |
| 92 | + readonly exposeHeaders?: string[]; |
| 93 | +} |
| 94 | + |
| 95 | +export class Cors { |
| 96 | + /** |
| 97 | + * All HTTP methods. |
| 98 | + */ |
| 99 | + public static readonly ALL_METHODS = ALL_METHODS; |
| 100 | + |
| 101 | + /** |
| 102 | + * The set of default headers allowed for CORS and useful for API Gateway. |
| 103 | + */ |
| 104 | + public static readonly DEFAULT_HEADERS = [ 'Content-Type', 'X-Amz-Date', 'Authorization', 'X-Api-Key', 'X-Amz-Security-Token', 'X-Amz-User-Agent' ]; |
| 105 | +} |
0 commit comments