Skip to content

Commit 99b80a9

Browse files
committed
feat(rest): make servers configurable for openapi specs
1 parent 9c882d9 commit 99b80a9

File tree

5 files changed

+368
-120
lines changed

5 files changed

+368
-120
lines changed

docs/site/Server.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,73 @@ export class HelloWorldApp extends RestApplication {
5858

5959
## Configuration
6060

61+
The REST server can be configured by passing a `rest` property inside your
62+
RestApplication options. For example, the following code customizes the port
63+
number that a REST server listens on.
64+
65+
```ts
66+
const app = new RestApplication({
67+
rest: {
68+
port: 3001,
69+
},
70+
});
71+
```
72+
73+
### Customize How OpenAPI Spec is Served
74+
75+
There are a few options under `rest.openApiSpec` to configure how OpenAPI spec
76+
is served by the given REST server.
77+
78+
- servers: Configure servers for OpenAPI spec
79+
- setServersFromRequest: Set `servers` based on HTTP request headers, default to
80+
`false`
81+
- endpointMapping: Maps urls for various forms of the spec. Default to:
82+
83+
```js
84+
{
85+
'/openapi.json': {version: '3.0.0', format: 'json'},
86+
'/openapi.yaml': {version: '3.0.0', format: 'yaml'},
87+
}
88+
```
89+
90+
```ts
91+
const app = new RestApplication({
92+
rest: {
93+
openApiSpec: {
94+
servers: [{url: 'http://127.0.0.1:8080'}],
95+
setServersFromRequest: false,
96+
endpointMapping: {
97+
'/openapi.json': {version: '3.0.0', format: 'json'},
98+
'/openapi.yaml': {version: '3.0.0', format: 'yaml'},
99+
},
100+
},
101+
},
102+
});
103+
```
104+
105+
### Configure the API Explorer
106+
107+
LoopBack allows externally hosted API Explorer UI to render the OpenAPI
108+
endpoints for a REST server. Such URLs can be specified with `rest.apiExplorer`:
109+
110+
- url: URL for the hosted API Explorer UI, default to
111+
`https://loopback.io/api-explorer`.
112+
- httpUrl: URL for the API explorer served over plain http to deal with mixed
113+
content security imposed by browsers as the spec is exposed over `http` by
114+
default. See https://github.com/strongloop/loopback-next/issues/1603. Default
115+
to the value of `url`.
116+
117+
```ts
118+
const app = new RestApplication({
119+
rest: {
120+
apiExplorer: {
121+
url: 'https://petstore.swagger.io',
122+
httpUrl: 'http://petstore.swagger.io',
123+
},
124+
},
125+
});
126+
```
127+
61128
### Enable HTTPS
62129

63130
Enabling HTTPS for the LoopBack REST server is just a matter of specifying the
@@ -89,7 +156,19 @@ export async function main() {
89156
}
90157
```
91158

92-
### Add servers to application instance
159+
### `rest` options
160+
161+
| Property | Type | Purpose |
162+
| ----------- | ------------------- | --------------------------------------------------------------------------------------------------------- |
163+
| port | number | Specify the port on which the RestServer will listen for traffic. |
164+
| protocol | string (http/https) | Specify the protocol on which the RestServer will listen for traffic. |
165+
| key | string | Specify the SSL private key for https. |
166+
| cert | string | Specify the SSL certificate for https. |
167+
| sequence | SequenceHandler | Use a custom SequenceHandler to change the behavior of the RestServer for the request-response lifecycle. |
168+
| openApiSpec | OpenApiSpecOptions | Customize how OpenAPI spec is served |
169+
| apiExplorer | ApiExplorerOptions | Customize how API explorer is served |
170+
171+
## Add servers to application instance
93172

94173
You can add server instances to your application via the `app.server()` method
95174
individually or as an array using `app.servers()` method. Using `app.server()`

packages/rest/README.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,7 @@ app.handler(({request, response}, sequence) => {
4545

4646
## Configuration
4747

48-
The rest package is configured by passing a `rest` property inside of your
49-
Application options.
50-
51-
```ts
52-
const app = new RestApplication({
53-
rest: {
54-
port: 3001,
55-
},
56-
});
57-
```
58-
59-
### `rest` options
60-
61-
| Property | Type | Purpose |
62-
| -------- | --------------- | --------------------------------------------------------------------------------------------------------- |
63-
| port | number | Specify the port on which the RestServer will listen for traffic. |
64-
| sequence | SequenceHandler | Use a custom SequenceHandler to change the behavior of the RestServer for the request-response lifecycle. |
48+
See https://loopback.io/doc/en/lb4/Server.html#configuration.
6549

6650
## Contributions
6751

packages/rest/src/rest.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ export class RestComponent implements Component {
4848
@inject(RestBindings.CONFIG) config?: RestComponentConfig,
4949
) {
5050
app.bind(RestBindings.SEQUENCE).toClass(DefaultSequence);
51-
app.bind(RestBindings.API_SPEC).to(createEmptyApiSpec());
51+
const apiSpec = createEmptyApiSpec();
52+
// Merge the OpenAPI `servers` spec from the config into the empty one
53+
if (config && config.openApiSpec && config.openApiSpec.servers) {
54+
Object.assign(apiSpec, {servers: config.openApiSpec.servers});
55+
}
56+
app.bind(RestBindings.API_SPEC).to(apiSpec);
5257
}
5358
}
5459

0 commit comments

Comments
 (0)