Skip to content

Commit c7453ac

Browse files
committed
feat(arrest): Ported to await/async
BREAKING CHANGE: Ported to await/async
1 parent a516113 commit c7453ac

6 files changed

Lines changed: 182 additions & 209 deletions

File tree

src/api.ts

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -266,33 +266,32 @@ export class API implements Swagger {
266266
}
267267
}
268268

269-
listen(httpPort: number, httpsPort?: number, httpsOptions?: https.ServerOptions): Promise<any> {
269+
async listen(httpPort: number, httpsPort?: number, httpsOptions?: https.ServerOptions): Promise<any> {
270270
if (!httpPort && !httpsPort) {
271271
throw new Error('no listen ports specified');
272272
} else if (httpPort && !httpsPort) {
273273
this.schemes = [ 'http' ];
274274
} else if (!httpPort && httpsPort) {
275275
this.schemes = [ 'https' ];
276276
}
277-
return this.router().then(router => {
278-
let app = express();
279-
app.use(router);
280-
let out: any[] = [];
281-
if (httpsPort) {
282-
if (!httpsOptions) {
283-
throw new Error('no https options');
284-
} else {
285-
out.push(https.createServer(httpsOptions, app).listen(httpsPort));
286-
}
287-
}
288-
if (httpPort) {
289-
out.push(http.createServer(app).listen(httpPort));
277+
let router = await this.router();
278+
let app = express();
279+
app.use(router);
280+
let out: any[] = [];
281+
if (httpsPort) {
282+
if (!httpsOptions) {
283+
throw new Error('no https options');
284+
} else {
285+
out.push(https.createServer(httpsOptions, app).listen(httpsPort));
290286
}
291-
return out.length == 1 ? out[0] : out;
292-
});
287+
}
288+
if (httpPort) {
289+
out.push(http.createServer(app).listen(httpPort));
290+
}
291+
return out.length == 1 ? out[0] : out;
293292
}
294293

295-
router(options?: RouterOptions): Promise<Router> {
294+
async router(options?: RouterOptions): Promise<Router> {
296295
if (!this[__router]) {
297296
this.logger.info('creating router');
298297
let r = Router(options);
@@ -345,31 +344,26 @@ export class API implements Swagger {
345344
}
346345
});
347346
}
348-
let p:Promise<any> = Promise.resolve(true);
349347
for (let i in this[__schemas]) {
350348
if (typeof this[__schemas][i] !== 'function') {
351-
p = p.then(() => this.registry.create(this[__schemas][i]));
349+
await this.registry.create(this[__schemas][i]);
352350
}
353351
}
354-
p = p.then(() => this.registry.resolve(this)).then(() => {
355-
let promises: Promise<Router>[] = [];
356-
this.resources.forEach((resource: Resource) => {
357-
promises.push(resource.router(r, options));
358-
});
359-
return Promise.all(promises).then(() => {
360-
r.use(API.handleError);
361-
return r;
362-
});
352+
await this.registry.resolve(this)
353+
let promises: Promise<Router>[] = [];
354+
this.resources.forEach((resource: Resource) => {
355+
promises.push(resource.router(r, options));
363356
});
364-
this[__router] = p.then(() => r);
357+
await Promise.all(promises);
358+
r.use(API.handleError);
359+
this[__router] = Promise.resolve(r);
365360
}
366361
return this[__router];
367362
}
368-
attach(base:Router, options?: RouterOptions): Promise<Router> {
369-
return this.router(options).then(router => {
370-
base.use('/v' + semver.major(this.info.version), router);
371-
return base;
372-
});
363+
async attach(base:Router, options?: RouterOptions): Promise<Router> {
364+
let router = await this.router(options)
365+
base.use('/v' + semver.major(this.info.version), router);
366+
return base;
373367
}
374368
securityValidator(req:APIRequest, res:APIResponse, next:NextFunction) {
375369
req.logger.warn('using default security validator');

0 commit comments

Comments
 (0)