Fully convert everything over to TypeScript

It's too bad the feathersjs CLI generator code doesn't know how to TypeScript
yet
This commit is contained in:
R. Tyler Croy 2018-09-09 17:53:34 -07:00
parent 957cce5fa7
commit 8f75f66a3d
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
11 changed files with 58 additions and 62 deletions

View File

@ -1,7 +1,9 @@
// Application hooks that run for every service
const log = require('./hooks/log');
import log from './hooks/log';
module.exports = {
import { HooksObject } from '@feathersjs/feathers';
export const appHooks : HooksObject = {
before: {
all: [ log() ],
find: [],

View File

@ -1,20 +1,20 @@
const path = require('path');
const favicon = require('serve-favicon');
const compress = require('compression');
const helmet = require('helmet');
const cors = require('cors');
const logger = require('./logger');
const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');
import path from 'path';
import favicon from 'serve-favicon';
import compress from 'compression';
import helmet from 'helmet';
import cors from 'cors';
import logger from './logger';
import feathers from '@feathersjs/feathers';
import configuration from '@feathersjs/configuration';
import express from '@feathersjs/express';
import socketio from '@feathersjs/socketio';
const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const channels = require('./channels');
import middleware from './middleware';
import services from './services';
import { appHooks } from './app.hooks';
import channels from './channels';
const app = express(feathers());
@ -47,4 +47,4 @@ app.use(express.errorHandler({ logger }));
app.hooks(appHooks);
module.exports = app;
export default app;

View File

@ -1,4 +1,4 @@
module.exports = function(app) {
export default (app) => {
if(typeof app.channel !== 'function') {
// If no real-time functionality has been configured just return
return;
@ -15,21 +15,21 @@ module.exports = function(app) {
if(connection) {
// Obtain the logged in user from the connection
// const user = connection.user;
// The connection is no longer anonymous, remove it
app.channel('anonymous').leave(connection);
// Add it to the authenticated user channel
app.channel('authenticated').join(connection);
// Channels can be named anything and joined on any condition
// Channels can be named anything and joined on any condition
// E.g. to send real-time events only to admins use
// if(user.isAdmin) { app.channel('admins').join(connection); }
// If the user has joined e.g. chat rooms
// if(Array.isArray(user.rooms)) user.rooms.forEach(room => app.channel(`rooms/${room.id}`).join(channel));
// Easily organize users by email and userid for things like messaging
// app.channel(`emails/${user.email}`).join(channel);
// app.channel(`userIds/$(user.id}`).join(channel);
@ -50,7 +50,7 @@ module.exports = function(app) {
// Here you can also add service specific event publishers
// e.g. the publish the `users` service `created` event to the `admins` channel
// app.service('users').publish('created', () => app.channel('admins'));
// With the userid and email organization from above you can easily select involved users
// app.service('messages').publish(() => {
// return [

View File

@ -1,22 +1,19 @@
// A hook that logs service method before, after and error
// See https://github.com/winstonjs/winston for documentation
// about the logger.
const logger = require('../logger');
const util = require('util');
import logger from '../logger';
import util from 'util';
// To see more detailed messages, uncomment the following line:
// logger.level = 'debug';
module.exports = function () {
export default () => {
return context => {
// This debugs the service call and a stringified version of the hook context
// You can customize the message (and logger) to your needs
logger.debug(`${context.type} app.service('${context.path}').${context.method}()`);
if(typeof context.toJSON === 'function' && logger.level === 'debug') {
logger.debug('Hook Context', util.inspect(context, {colors: false}));
}
if(context.error) {
logger.error(context.error.stack);
}

View File

@ -1,8 +1,9 @@
/* eslint-disable no-console */
const logger = require('./logger');
const app = require('./app');
const port = app.get('port');
const server = app.listen(port);
import logger from './logger';
import app from './app';
const port : Number = app.get('port');
const server : any = app.listen(port);
process.on('unhandledRejection', (reason, p) =>
logger.error('Unhandled Rejection at: Promise ', p, reason)

View File

@ -1,16 +0,0 @@
const { createLogger, format, transports } = require('winston');
// Configure the Winston logger. For the complete documentation see https://github.com/winstonjs/winston
const logger = createLogger({
// To see more detailed errors, change this to 'debug'
level: 'info',
format: format.combine(
format.splat(),
format.simple()
),
transports: [
new transports.Console()
],
});
module.exports = logger;

13
src/logger.ts Normal file
View File

@ -0,0 +1,13 @@
import { createLogger, format, transports } from 'winston';
export default createLogger({
// To see more detailed errors, change this to 'debug'
level: 'info',
format: format.combine(
format.splat(),
format.simple()
),
transports: [
new transports.Console()
],
});

View File

@ -1,5 +1,4 @@
// eslint-disable-next-line no-unused-vars
module.exports = function (app) {
export default (app) => {
// Add your custom middleware here. Remember that
// in Express, the order matters.
};

View File

@ -1,3 +0,0 @@
// eslint-disable-next-line no-unused-vars
module.exports = function (app) {
};

2
src/services/index.ts Normal file
View File

@ -0,0 +1,2 @@
export default (app) => {
};

View File

@ -1,6 +1,7 @@
const rp = require('request-promise');
const url = require('url');
const app = require('../src/app');
import url from 'url';
import request from 'request-promise';
import app from '../src/app';
const port = app.get('port') || 3030;
const getUrl = pathname => url.format({
@ -21,14 +22,14 @@ describe('Feathers application tests', () => {
});
it('starts and shows the index page', () => {
return rp(getUrl()).then(body =>
return request(getUrl()).then(body =>
expect(body.indexOf('<html>')).not.toEqual(-1)
);
});
describe('404', function() {
it('shows a 404 HTML page', () => {
return rp({
return request({
url: getUrl('path/to/nowhere'),
headers: {
'Accept': 'text/html'
@ -40,7 +41,7 @@ describe('Feathers application tests', () => {
});
it('shows a 404 JSON error without stack trace', () => {
return rp({
return request({
url: getUrl('path/to/nowhere'),
json: true
}).catch(res => {