UNPKG

8.85 kBTypeScriptView Raw
1import { IResource as IResourceBase, Resource as ResourceConstruct } from '@aws-cdk/core';
2import { Construct } from 'constructs';
3import { CorsOptions } from './cors';
4import { Integration } from './integration';
5import { Method, MethodOptions } from './method';
6import { IRestApi, RestApi } from './restapi';
7export interface IResource extends IResourceBase {
8 /**
9 * The parent of this resource or undefined for the root resource.
10 */
11 readonly parentResource?: IResource;
12 /**
13 * The rest API that this resource is part of.
14 *
15 * @deprecated - Throws an error if this Resource is not associated with an instance of `RestApi`. Use `api` instead.
16 */
17 readonly restApi: RestApi;
18 /**
19 * The rest API that this resource is part of.
20 *
21 * The reason we need the RestApi object itself and not just the ID is because the model
22 * is being tracked by the top-level RestApi object for the purpose of calculating it's
23 * hash to determine the ID of the deployment. This allows us to automatically update
24 * the deployment when the model of the REST API changes.
25 */
26 readonly api: IRestApi;
27 /**
28 * The ID of the resource.
29 * @attribute
30 */
31 readonly resourceId: string;
32 /**
33 * The full path of this resource.
34 */
35 readonly path: string;
36 /**
37 * An integration to use as a default for all methods created within this
38 * API unless an integration is specified.
39 */
40 readonly defaultIntegration?: Integration;
41 /**
42 * Method options to use as a default for all methods created within this
43 * API unless custom options are specified.
44 */
45 readonly defaultMethodOptions?: MethodOptions;
46 /**
47 * Default options for CORS preflight OPTIONS method.
48 */
49 readonly defaultCorsPreflightOptions?: CorsOptions;
50 /**
51 * Gets or create all resources leading up to the specified path.
52 *
53 * - Path may only start with "/" if this method is called on the root resource.
54 * - All resources are created using default options.
55 *
56 * @param path The relative path
57 * @returns a new or existing resource.
58 */
59 resourceForPath(path: string): Resource;
60 /**
61 * Defines a new child resource where this resource is the parent.
62 * @param pathPart The path part for the child resource
63 * @param options Resource options
64 * @returns A Resource object
65 */
66 addResource(pathPart: string, options?: ResourceOptions): Resource;
67 /**
68 * Retrieves a child resource by path part.
69 *
70 * @param pathPart The path part of the child resource
71 * @returns the child resource or undefined if not found
72 */
73 getResource(pathPart: string): IResource | undefined;
74 /**
75 * Adds a greedy proxy resource ("{proxy+}") and an ANY method to this route.
76 * @param options Default integration and method options.
77 */
78 addProxy(options?: ProxyResourceOptions): ProxyResource;
79 /**
80 * Defines a new method for this resource.
81 * @param httpMethod The HTTP method
82 * @param target The target backend integration for this method
83 * @param options Method options, such as authentication.
84 *
85 * @returns The newly created `Method` object.
86 */
87 addMethod(httpMethod: string, target?: Integration, options?: MethodOptions): Method;
88 /**
89 * Adds an OPTIONS method to this resource which responds to Cross-Origin
90 * Resource Sharing (CORS) preflight requests.
91 *
92 * Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional
93 * HTTP headers to tell browsers to give a web application running at one
94 * origin, access to selected resources from a different origin. A web
95 * application executes a cross-origin HTTP request when it requests a
96 * resource that has a different origin (domain, protocol, or port) from its
97 * own.
98 *
99 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
100 * @param options CORS options
101 * @returns a `Method` object
102 */
103 addCorsPreflight(options: CorsOptions): Method;
104}
105export interface ResourceOptions {
106 /**
107 * An integration to use as a default for all methods created within this
108 * API unless an integration is specified.
109 *
110 * @default - Inherited from parent.
111 */
112 readonly defaultIntegration?: Integration;
113 /**
114 * Method options to use as a default for all methods created within this
115 * API unless custom options are specified.
116 *
117 * @default - Inherited from parent.
118 */
119 readonly defaultMethodOptions?: MethodOptions;
120 /**
121 * Adds a CORS preflight OPTIONS method to this resource and all child
122 * resources.
123 *
124 * You can add CORS at the resource-level using `addCorsPreflight`.
125 *
126 * @default - CORS is disabled
127 */
128 readonly defaultCorsPreflightOptions?: CorsOptions;
129}
130export interface ResourceProps extends ResourceOptions {
131 /**
132 * The parent resource of this resource. You can either pass another
133 * `Resource` object or a `RestApi` object here.
134 */
135 readonly parent: IResource;
136 /**
137 * A path name for the resource.
138 */
139 readonly pathPart: string;
140}
141export declare abstract class ResourceBase extends ResourceConstruct implements IResource {
142 abstract readonly parentResource?: IResource;
143 /**
144 * @deprecated - Throws an error if this Resource is not associated with an instance of `RestApi`. Use `api` instead.
145 */
146 abstract readonly restApi: RestApi;
147 abstract readonly api: IRestApi;
148 abstract readonly resourceId: string;
149 abstract readonly path: string;
150 abstract readonly defaultIntegration?: Integration;
151 abstract readonly defaultMethodOptions?: MethodOptions;
152 abstract readonly defaultCorsPreflightOptions?: CorsOptions;
153 private readonly children;
154 constructor(scope: Construct, id: string);
155 addResource(pathPart: string, options?: ResourceOptions): Resource;
156 addMethod(httpMethod: string, integration?: Integration, options?: MethodOptions): Method;
157 addProxy(options?: ProxyResourceOptions): ProxyResource;
158 addCorsPreflight(options: CorsOptions): Method;
159 getResource(pathPart: string): IResource | undefined;
160 /**
161 * @internal
162 */
163 _trackChild(pathPart: string, resource: Resource): void;
164 resourceForPath(path: string): Resource;
165 /**
166 * @deprecated - Throws error in some use cases that have been enabled since this deprecation notice. Use `RestApi.urlForPath()` instead.
167 */
168 get url(): string;
169}
170/**
171 * Attributes that can be specified when importing a Resource
172 */
173export interface ResourceAttributes {
174 /**
175 * The ID of the resource.
176 */
177 readonly resourceId: string;
178 /**
179 * The rest API that this resource is part of.
180 */
181 readonly restApi: IRestApi;
182 /**
183 * The full path of this resource.
184 */
185 readonly path: string;
186}
187export declare class Resource extends ResourceBase {
188 /**
189 * Import an existing resource
190 */
191 static fromResourceAttributes(scope: Construct, id: string, attrs: ResourceAttributes): IResource;
192 readonly parentResource?: IResource;
193 readonly api: IRestApi;
194 readonly resourceId: string;
195 readonly path: string;
196 readonly defaultIntegration?: Integration;
197 readonly defaultMethodOptions?: MethodOptions;
198 readonly defaultCorsPreflightOptions?: CorsOptions;
199 constructor(scope: Construct, id: string, props: ResourceProps);
200 /**
201 * The RestApi associated with this Resource
202 * @deprecated - Throws an error if this Resource is not associated with an instance of `RestApi`. Use `api` instead.
203 */
204 get restApi(): RestApi;
205}
206export interface ProxyResourceOptions extends ResourceOptions {
207 /**
208 * Adds an "ANY" method to this resource. If set to `false`, you will have to explicitly
209 * add methods to this resource after it's created.
210 *
211 * @default true
212 */
213 readonly anyMethod?: boolean;
214}
215export interface ProxyResourceProps extends ProxyResourceOptions {
216 /**
217 * The parent resource of this resource. You can either pass another
218 * `Resource` object or a `RestApi` object here.
219 */
220 readonly parent: IResource;
221}
222/**
223 * Defines a {proxy+} greedy resource and an ANY method on a route.
224 * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html
225 */
226export declare class ProxyResource extends Resource {
227 /**
228 * If `props.anyMethod` is `true`, this will be the reference to the 'ANY'
229 * method associated with this proxy resource.
230 */
231 readonly anyMethod?: Method;
232 constructor(scope: Construct, id: string, props: ProxyResourceProps);
233 addMethod(httpMethod: string, integration?: Integration, options?: MethodOptions): Method;
234}