-
Notifications
You must be signed in to change notification settings - Fork 18
/
api.ts
304 lines (283 loc) · 9.77 KB
/
api.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as pulumi from "@pulumi/pulumi";
import { Endpoint } from "./service";
/**
* Request represents an API request.
*/
export interface Request {
/**
* The body of the HTTP request.
*/
body: Buffer;
/**
* The method of the HTTP request.
*/
method: string;
/**
* The path parameters of the HTTP request. Each `{param}` in the matched
* route is available as a property of this oject.
*/
params: { [param: string]: string; };
/**
* The headers of the HTTP request.
*/
headers: { [header: string]: string | string[]; };
/**
* The headers of the HTTP request.
*/
rawHeaders: string[];
/**
* The query parameters parsed from the query string of the request URL.
*/
query: { [query: string]: string | string[]; };
/**
* The raw path from the HTTP request.
*/
path: string;
/**
* The protocol of the request (e.g. HTTP/HTTPS).
*/
protocol: string;
/**
* The base url on which this http request was served.
*/
baseUrl: string;
/**
* The hostname of the request.
*/
hostname: string;
}
/**
* Response represents the response to an API request.
*/
export interface Response {
/**
* Object containing local variables scoped to a single request. Useful for
* exposing request-level information such as user settings.
*/
locals: any;
/**
* Sets the HTTP response status code and returns a `Response` for chaining
* operations.
*/
status(code: number): Response;
/**
* Gets the Headers for the Response
*/
getHeader(name: string): string;
/**
* Sets a header on the HTTP response and returns the `Response` for
* chaining operations.
*/
setHeader(name: string, value: string): Response;
/**
* Writes a string to the HTTP response body and returns the `Response` for
* chaining operations.
*/
write(data: string | Buffer, encoding?: string): Response;
/**
* Sends the HTTP response, optionally including data to write to the HTTP
* response body.
*/
end(data?: string | Buffer, encoding?: string): void;
/**
* JSON serializes an object, writes it to the HTTP response body, and sends
* the HTTP response.
*/
json(obj: any): void;
/**
* Mark the response to redirect the client to the provided URL with
* the optional status code, defaulting to 302.
*/
redirect(url: string): void;
redirect(status: number, url: string): void;
}
/**
* RouteHandler represents a handler for a route on an API.
*
* Implementations should invoke methods on `res` to respond to the request, or
* invoke `next` to pass control to the next available handler on the route for
* further processing.
*/
export type RouteHandler = (req: Request, res: Response, next: () => void) => void;
export interface APIConstructor {
new (apiName: string): API;
}
export let API: APIConstructor; // tslint:disable-line
export interface ServeStaticOptions {
/**
* The `content-type` to serve the file as. Only valid when localPath points to a file. If
* localPath points to a directory, the content types for all files will be inferred.
*/
contentType?: string;
/**
* By default API.static will also serve 'index.html' in response to a request on a
* directory. To disable this set false or to supply a new index pass a string.
*/
index?: boolean | string;
}
/**
* API publishes an internet-facing HTTP API, for serving web
* applications or REST APIs.
*
* ```javascript
* let api = new API("myapi")
* api.get("/", (req, res) => res.json({hello: "world"}));
* api.publish().url.then(url =>
* console.log(`Serving myapi at ${url}`)
* );
* ```
*
* Paths are `/` seperated. A path can use `{param}` to capture zero-or-more
* non-`/` characters and make the captured path segment available in
* `req.params.param`, or `{param+}` to greedily capture all remaining
* characters in the url path into `req.params.param`.
*
* Paths and routing are defined statically, and cannot overlap. Code inside a
* route handler can be used to provide dynamic decisions about sub-routing
* within a static path.
*/
export interface API {
/**
* static serves a file or directory from within the source folder at the requested path.
*
* @param path The route path at which to serve the file.
* @param localPath The local path. If not absolute, it is considered relative to the Pulumi
* program folder.
* @param options Optional options that can be provided to customize the serving behavior.
*/
static(path: string, localPath: string, options?: ServeStaticOptions): void;
/**
* proxy forwards an HTTP request to a target URL or Endpoint.
*
* @param path The route path at which to serve the file.
* @param target The target URL or Endpoint to proxy to. If a string is provided, it must be an Internet reachable
* URL. If an Endpoint is provided, it can be any endpoint exposed by the stack, including endpoints
* which are not exposed directly to the Internet.
*/
proxy(path: string, target: string | pulumi.Output<Endpoint>): void;
/**
* Routes any requests with given HTTP method on the given path to the
* provided handler(s).
* @param method The HTTP method to handle.
* @param path The path to handle requests on.
* @param handlers One or more handlers to apply to requests.
*/
route(method: string, path: string, ...handlers: RouteHandler[]): void;
/**
* Routes GET requests on the given path to the provided handler(s).
* @param path The path to handle requests on.
* @param handlers One or more handlers to apply to requests.
*/
get(path: string, ...handlers: RouteHandler[]): void;
/**
* Routes PUT requests on the given path to the provided handler(s).
* @param path The path to handle requests on.
* @param handlers One or more handlers to apply to requests.
*/
put(path: string, ...handlers: RouteHandler[]): void;
/**
* Routes POST requests on the given path to the provided handler(s).
* @param path The path to handle requests on.
* @param handlers One or more handlers to apply to requests.
*/
post(path: string, ...handlers: RouteHandler[]): void;
/**
* Routes DELETE requests on the given path to the provided handler(s).
* @param path The path to handle requests on.
* @param handlers One or more handlers to apply to requests.
*/
delete(path: string, ...handlers: RouteHandler[]): void;
/**
* Routes OPTIONS requests on the given path to the provided handler(s).
* @param path The path to handle requests on.
* @param handlers One or more handlers to apply to requests.
*/
options(path: string, ...handlers: RouteHandler[]): void;
/**
* Routes all HTTP methods on the given path to the provided handler(s).
* @param path The path to handle requests on.
* @param handlers One or more handlers to apply to requests.
*/
all(path: string, ...handlers: RouteHandler[]): void;
/**
* Attach a custom domain to this API.
*
* Provide a domain name you own, along with SSL certificates from a
* certificate authority (e.g. LetsEncrypt).
*
* Must be called prior to [publish]ing the API.
*
* _Note_: It is strongly encouraged to store certificates in config
* variables and not in source code.
*/
attachCustomDomain(domain: Domain): void;
/**
* Publishes an API to be internet accessible.
*
* This should be called after describing desired routes and domains.
* Throws an error if called multiple times on the same endpoint.
*
* @returns An HttpDeployment object representing the live API.
*/
publish(): HttpDeployment;
}
/**
* HttpDeployment represents an API that has been deployed and is
* available at a URL.
*/
export interface HttpDeployment {
/**
* The URL at which the HttpDeployment is available to the Internet.
*/
url: pulumi.Output<string>;
/**
* An optional list of custom domain names, each corresponding to a
* previous call to attachCustomDomain on the API. Each name
* should be mapped using a DNS A record.
*/
customDomainNames: pulumi.Output<string>[];
}
/**
* Domain includes the domain name and certificate data to enable hosting an
* API on a custom domain.
*/
export interface Domain {
/**
* The domain name to associate with the API.
*/
domainName: string;
/**
* An SSL/TLS certficicate issued for this domain (`cert.pem`).
*/
certificateBody: string;
/**
* An SSL/TLS private key issued for thie domain (`privkey.pem`).
*/
certificatePrivateKey: string;
/**
* The certificate chain for the SSL/TLS certificate provided for this
* domain (`chain.pem`).
*/
certificateChain: string;
}
/**
* @deprecated HttpEndpoint has been renamed to API
*/
export type HttpEndpoint = API;
/**
* @deprecated HttpEndpoint has been renamed to API
*/
export let HttpEndpoint: APIConstructor; // tslint:disable-line