Api
The cloud.Api resource represents a collection of HTTP endpoints that can be invoked by clients over the internet.
APIs often serve as the front door for applications to access data, business logic, or functionality from your backend services.
The Api resource models an endpoint as a collection of routes, each mapped to an event handler function.
A route is a combination of a path, like "/users/:userid" and a set of HTTP methods, like GET, POST, or DELETE.
When a client invokes a route, the corresponding event handler function executes.
Usage
REST API
The following example shows a complete REST API implementation using cloud.Api, cloud.Bucket & cloud.Counter
bring cloud;
let api = new cloud.Api();
// Used for generating unique id
let counter = new cloud.Counter();
// our employee data
let store = new cloud.Bucket();
api.get("/employees", inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
let result = MutJson [];
let var i = 0;
for employee in store.list() {
result.setAt(i, employee);
i = i + 1;
}
return cloud.ApiResponse {
status: 200,
body: Json.stringify(result)
};
});
api.get("/employees/:id", inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
let employee = store.get(request.vars.get("id"));
return cloud.ApiResponse {
status: 200,
body: Json.stringify(employee)
};
});
api.post("/employees", inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
if let body = request.body {
let employeeData = Json.parse(body);
let id = "{counter.inc()}";
store.putJson(id, employeeData);
return cloud.ApiResponse {
status: 201,
body: id
};
}
});
api.put("/employees/:id", inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
if let body = request.body {
let employeeData = Json.parse(body);
let id = request.vars.get("id");
store.putJson(id, employeeData);
return cloud.ApiResponse {
status: 200,
body: Json.stringify(employeeData)
};
}
});
api.delete("/employees/:id", inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
let id = request.vars.get("id");
store.delete(id);
return cloud.ApiResponse {
status: 204
};
});
Target-specific details
Simulator (sim)
The sim implementation of cloud.Api uses nodejs express.
AWS (tf-aws and awscdk)
The AWS implementation of cloud.Api uses Amazon API Gateway.
Azure (tf-azure)
🚧 Not supported yet (tracking issue: #625)
GCP (tf-gcp)
🚧 Not supported yet (tracking issue: #624)
API Reference
Api
Functionality shared between all Api implementations.
Initializers
bring cloud;
new cloud.Api(props?: ApiProps);
| Name | Type | Description |
|---|---|---|
| | No description. |
propsOptional
- Type: ApiProps
Methods
Preflight Methods
| Name | Description |
|---|---|
| Add a inflight handler to the api for CONNECT requests on the given path. |
| Add a inflight handler to the api for DELETE requests on the given path. |
| Add a inflight handler to the api for GET requests on the given path. |
| Add a inflight handler to the api for HEAD requests on the given path. |
| Add a inflight handler to the api for OPTIONS requests on the given path. |
| Add a inflight handler to the api for PATCH requests on the given path. |
| Add a inflight handler to the api for POST requests on the given path. |
| Add a inflight handler to the api for PUT requests on the given path. |
connect
connect(path: str, inflight: IApiEndpointHandler, props?: ApiConnectOptions): void
Add a inflight handler to the api for CONNECT requests on the given path.
pathRequired
- Type: str
The path to handle CONNECT requests for.
inflightRequired
- Type: IApiEndpointHandler
The function to handle the request.
propsOptional
- Type: ApiConnectOptions
Options for the route.
delete
delete(path: str, inflight: IApiEndpointHandler, props?: ApiDeleteOptions): void
Add a inflight handler to the api for DELETE requests on the given path.
pathRequired
- Type: str
The path to handle DELETE requests for.
inflightRequired
- Type: IApiEndpointHandler
The function to handle the request.
propsOptional
- Type: ApiDeleteOptions
Options for the route.
get
get(path: str, inflight: IApiEndpointHandler, props?: ApiGetOptions): void
Add a inflight handler to the api for GET requests on the given path.
pathRequired
- Type: str
The path to handle GET requests for.
inflightRequired
- Type: IApiEndpointHandler
The function to handle the request.
propsOptional
- Type: ApiGetOptions
Options for the route.
head
head(path: str, inflight: IApiEndpointHandler, props?: ApiHeadOptions): void
Add a inflight handler to the api for HEAD requests on the given path.
pathRequired
- Type: str
The path to handle HEAD requests for.
inflightRequired
- Type: IApiEndpointHandler
The function to handle the request.
propsOptional
- Type: ApiHeadOptions
Options for the route.
options
options(path: str, inflight: IApiEndpointHandler, props?: ApiOptionsOptions): void
Add a inflight handler to the api for OPTIONS requests on the given path.
pathRequired
- Type: str
The path to handle OPTIONS requests for.
inflightRequired
- Type: IApiEndpointHandler
The function to handle the request.
propsOptional
- Type: ApiOptionsOptions
Options for the route.
patch
patch(path: str, inflight: IApiEndpointHandler, props?: ApiPatchOptions): void
Add a inflight handler to the api for PATCH requests on the given path.
pathRequired
- Type: str
The path to handle PATCH requests for.
inflightRequired
- Type: IApiEndpointHandler
The function to handle the request.
propsOptional
- Type: ApiPatchOptions
Options for the route.
post
post(path: str, inflight: IApiEndpointHandler, props?: ApiPostOptions): void
Add a inflight handler to the api for POST requests on the given path.
pathRequired
- Type: str
The path to handle POST requests for.
inflightRequired
- Type: IApiEndpointHandler
The function to handle the request.
propsOptional
- Type: ApiPostOptions
Options for the route.
put
put(path: str, inflight: IApiEndpointHandler, props?: ApiPutOptions): void
Add a inflight handler to the api for PUT requests on the given path.
pathRequired
- Type: str
The path to handle PUT requests for.
inflightRequired
- Type: IApiEndpointHandler
The function to handle the request.
propsOptional
- Type: ApiPutOptions
Options for the route.
Static Functions
| Name | Description |
|---|---|
| A hook called by the Wing compiler once for each inflight host that needs to use this type inflight. |
| Generates an asynchronous JavaScript statement which can be used to create an inflight client for a resource. |
| Generates an object containing default CORS response headers and OPTIONS response headers. |
| Converts input path to a valid OpenAPI path (replaces : based path params with {}). |
onLiftType
bring cloud;
cloud.Api.onLiftType(host: IInflightHost, ops: MutArray<str>);
A hook called by the Wing compiler once for each inflight host that needs to use this type inflight.
The list of requested inflight methods
needed by the inflight host are given by ops.
This method is commonly used for adding permissions, environment variables, or other capabilities to the inflight host.
hostRequired
- Type: IInflightHost
opsRequired
- Type: MutArray<str>
toInflight
bring cloud;
cloud.Api.toInflight(obj: IResource);
Generates an asynchronous JavaScript statement which can be used to create an inflight client for a resource.
NOTE: This statement must be executed within an async context.
objRequired
- Type: IResource
renderCorsHeaders
bring cloud;
cloud.Api.renderCorsHeaders(corsOptions?: ApiCorsOptions);
Generates an object containing default CORS response headers and OPTIONS response headers.
corsOptionsOptional
- Type: ApiCorsOptions
The CORS options to generate the headers from.
renderOpenApiPath
bring cloud;
cloud.Api.renderOpenApiPath(path: str);
Converts input path to a valid OpenAPI path (replaces : based path params with {}).
pathRequired
- Type: str
The path to convert (assumes path is valid).
Properties
| Name | Type | Description |
|---|---|---|
| constructs.Node | The tree node. |
| str | The base URL of the API endpoint. |
nodeRequired
node: Node;
- Type: constructs.Node
The tree node.
urlRequired
url: str;
- Type: str
The base URL of the API endpoint.
Structs
ApiConnectOptions
Options for Api patch endpoint.
Initializer
bring cloud;
let ApiConnectOptions = cloud.ApiConnectOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiCorsOptions
Cors Options for Api.
Initializer
bring cloud;
let ApiCorsOptions = cloud.ApiCorsOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| bool | Whether to allow credentials. |
| MutArray<str> | The list of allowed headers. |
| | The list of allowed methods. |
| str | The allowed origin. |
| MutArray<str> | The list of exposed headers. |
| | How long the browser should cache preflight request results. |
allowCredentialsOptional
allowCredentials: bool;
- Type: bool
- Default: false
Whether to allow credentials.
allowHeadersOptional
allowHeaders: MutArray<str>;
- Type: MutArray<str>
- Default: ["Content-Type", "Authorization"]
The list of allowed headers.
Example
["Content-Type"]
allowMethodsOptional
allowMethods: MutArray<HttpMethod>;
- Type: MutArray<HttpMethod>
- Default: [HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH, HttpMethod.DELETE, HttpMethod.HEAD, HttpMethod.OPTIONS]
The list of allowed methods.
Example
[HttpMethod.GET, HttpMethod.POST]
allowOriginOptional
allowOrigin: str;
- Type: str
- Default: "*"
The allowed origin.
Example
"https://example.com"
exposeHeadersOptional
exposeHeaders: MutArray<str>;
- Type: MutArray<str>
- Default: []
The list of exposed headers.
Example
["Content-Type"]
maxAgeOptional
maxAge: duration;
- Type: duration
- Default: 300 seconds
How long the browser should cache preflight request results.
ApiDeleteOptions
Options for Api put endpoint.
Initializer
bring cloud;
let ApiDeleteOptions = cloud.ApiDeleteOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiEndpointOptions
Base options for Api endpoints.
Initializer
bring cloud;
let ApiEndpointOptions = cloud.ApiEndpointOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiGetOptions
Options for Api get endpoint.
Initializer
bring cloud;
let ApiGetOptions = cloud.ApiGetOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiHeadOptions
Options for Api patch endpoint.
Initializer
bring cloud;
let ApiHeadOptions = cloud.ApiHeadOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiOptionsOptions
Options for Api patch endpoint.
Initializer
bring cloud;
let ApiOptionsOptions = cloud.ApiOptionsOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiPatchOptions
Options for Api patch endpoint.
Initializer
bring cloud;
let ApiPatchOptions = cloud.ApiPatchOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiPostOptions
Options for Api post endpoint.
Initializer
bring cloud;
let ApiPostOptions = cloud.ApiPostOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiProps
Options for Api.
Initializer
bring cloud;
let ApiProps = cloud.ApiProps{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| bool | Options for configuring the API's CORS behavior across all routes. |
| | Options for configuring the API's CORS behavior across all routes. |
corsOptional
cors: bool;
- Type: bool
- Default: false, CORS configuration is disabled
Options for configuring the API's CORS behavior across all routes.
Options can also be overridden on a per-route basis. (not yet implemented)
When enabled this will add CORS headers with default options.
Can be customized by passing corsOptions
Example
true
corsOptionsOptional
corsOptions: ApiCorsOptions;
- Type: ApiCorsOptions
- Default: Default CORS options are applied when
corsis set totrueallowOrigin: "*", allowMethods: [ HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, HttpMethod.OPTIONS, ], allowHeaders: ["Content-Type", "Authorization"], exposeHeaders: [], allowCredentials: false,
Options for configuring the API's CORS behavior across all routes.
Options can also be overridden on a per-route basis. (not yet implemented)
Example
{ allowOrigin: "https://example.com" }
ApiPutOptions
Options for Api put endpoint.
Initializer
bring cloud;
let ApiPutOptions = cloud.ApiPutOptions{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| num | The maximum concurrent invocations that can run at one time. |
| MutMap<str> | Environment variables to pass to the function. |
| num | Specifies the number of days that function logs will be kept. |
| num | The amount of memory to allocate to the function, in MB. |
| | The maximum amount of time the function can run. |
concurrencyOptional
concurrency: num;
- Type: num
- Default: platform specific limits (100 on the simulator)
The maximum concurrent invocations that can run at one time.
envOptional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
logRetentionDaysOptional
logRetentionDays: num;
- Type: num
- Default: 30
Specifies the number of days that function logs will be kept.
Setting negative value means logs will not expire.
memoryOptional
memory: num;
- Type: num
- Default: 1024
The amount of memory to allocate to the function, in MB.
timeoutOptional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
ApiRequest
Shape of a request to an inflight handler.
Initializer
bring cloud;
let ApiRequest = cloud.ApiRequest{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| | The request's HTTP method. |
| str | The request's path. |
| MutMap<str> | The request's query string values. |
| MutMap<str> | The path variables. |
| str | The request's body. |
| MutMap<str> | The request's headers. |
methodRequired
method: HttpMethod;
- Type: HttpMethod
The request's HTTP method.
pathRequired
path: str;
- Type: str
The request's path.
queryRequired
query: MutMap<str>;
- Type: MutMap<str>
The request's query string values.
varsRequired
vars: MutMap<str>;
- Type: MutMap<str>
The path variables.
bodyOptional
body: str;
- Type: str
The request's body.
headersOptional
headers: MutMap<str>;
- Type: MutMap<str>
The request's headers.
ApiResponse
Shape of a response from a inflight handler.
Initializer
bring cloud;
let ApiResponse = cloud.ApiResponse{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| str | The response's body. |
| MutMap<str> | The response's headers. |
| num | The response's status code. |
bodyOptional
body: str;
- Type: str
- Default: no body
The response's body.
headersOptional
headers: MutMap<str>;
- Type: MutMap<str>
- Default: {}
The response's headers.
statusOptional
status: num;
- Type: num
- Default: 200
The response's status code.
CorsHeaders
Type definition for CORS headers which includes default and options headers.
Initializer
bring cloud;
let CorsHeaders = cloud.CorsHeaders{ ... };
Properties
| Name | Type | Description |
|---|---|---|
| MutMap<str> | Default CORS response headers. |
| MutMap<str> | CORS options response headers. |
defaultResponseRequired
defaultResponse: MutMap<str>;
- Type: MutMap<str>
Default CORS response headers.
optionsResponseRequired
optionsResponse: MutMap<str>;
- Type: MutMap<str>
CORS options response headers.
Protocols
IApiEndpointHandler
-
Extends: IInflight
-
Implemented By: IApiEndpointHandler
Inflight client: @winglang/sdk.cloud.IApiEndpointHandlerClient
A resource with an inflight "handle" method that can be passed to one of the Api request preflight methods.
IApiEndpointHandlerClient
- Implemented By: IApiEndpointHandlerClient
Inflight client for IApiEndpointHandler.
Methods
| Name | Description |
|---|---|
| Inflight that will be called when a request is made to the endpoint. |
handle
inflight handle(request: ApiRequest): ApiResponse?
Inflight that will be called when a request is made to the endpoint.
requestRequired
- Type: ApiRequest
Enums
HttpMethod
Allowed HTTP methods for a endpoint.
Members
| Name | Description |
|---|---|
| Get. |
| Head. |
| Post. |
| Put. |
| Delete. |
| Connect. |
| Options. |
| Patch. |
GET
Get.
HEAD
Head.
POST
Post.
PUT
Put.
DELETE
Delete.
CONNECT
Connect.
OPTIONS
Options.
PATCH
Patch.