From 57d96831358a6df5dd4f92a1d33715cbc3a575e0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 19:41:42 +0300 Subject: feat: enable logging hook for external requests --- hooks/logging.ts | 13 +++++++++++++ services/index.ts | 4 ++++ 2 files changed, 17 insertions(+) create mode 100644 hooks/logging.ts diff --git a/hooks/logging.ts b/hooks/logging.ts new file mode 100644 index 0000000..30353ae --- /dev/null +++ b/hooks/logging.ts @@ -0,0 +1,13 @@ +import { HookContext } from '@feathersjs/feathers'; + +export default async (context: HookContext): Promise => { + if (context.params.provider) { + const { method, path, id, params: { user: { username }} } = context; + const timestamp = new Date().toLocaleString('default', { timeStyle: 'medium', dateStyle: 'short' }); + const message = `${method.toUpperCase()}: /${path}/${id || ''}`; + + console.log(`[${timestamp}] ${message} ${username || ''}`); + } + return context; +}; + diff --git a/services/index.ts b/services/index.ts index ce20901..8d77429 100644 --- a/services/index.ts +++ b/services/index.ts @@ -6,6 +6,7 @@ import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; import tryAuthenticate from '../hooks/tryAuthenticate'; +import logging from '../hooks/logging'; export default (app: Application): void => { app.configure(Auth); @@ -17,6 +18,9 @@ export default (app: Application): void => { app.hooks({ before: { all: tryAuthenticate + }, + after: { + all: logging } }); }; -- cgit v1.2.3 From 263ed426cc5ea79d36bca2b82a8aa858e43072eb Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 19:44:12 +0300 Subject: feat: mark anonymous requests --- hooks/logging.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hooks/logging.ts b/hooks/logging.ts index 30353ae..840873c 100644 --- a/hooks/logging.ts +++ b/hooks/logging.ts @@ -2,11 +2,12 @@ import { HookContext } from '@feathersjs/feathers'; export default async (context: HookContext): Promise => { if (context.params.provider) { - const { method, path, id, params: { user: { username }} } = context; + const { method, path, id } = context; const timestamp = new Date().toLocaleString('default', { timeStyle: 'medium', dateStyle: 'short' }); const message = `${method.toUpperCase()}: /${path}/${id || ''}`; + const username = context.params.user?.username || 'anonymous'; - console.log(`[${timestamp}] ${message} ${username || ''}`); + console.log(`[${timestamp}] ${message} ${username}`); } return context; }; -- cgit v1.2.3 From 050d3e087b3ce2c9dd892a96daf215402292aa2c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 21:14:32 +0300 Subject: chore: update which-types --- package-lock.json | 7 +++---- package.json | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1402c27..6a3838a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3051,10 +3051,9 @@ } }, "which-types": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.4.0.tgz", - "integrity": "sha512-dq5f7AmZeataiQZxko+Dw1xRvWdzSXTwXrjKxtyL5zqLgS29J1PXjTgcAWMfbkZhbd8+iJLkfxKvYZ42itmxeA==", - "dev": true + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.4.1.tgz", + "integrity": "sha512-ZtN3cDwz/fQbJBwrItsZ0jpGafReTd/fIffHNQtFW4THrZqi8z4qnFTbyu1M6LnAmPlwU/FaRLZPfd67ZQ4mFw==" }, "word-wrap": { "version": "1.2.3", diff --git a/package.json b/package.json index 1a21fcf..e21de51 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "feathers-mongoose": "^8.3.0", "lodash": "^4.17.15", "mongoose": "^5.9.18", - "which-types": "^1.4.0" + "which-types": "^1.4.1" }, "repository": { "type": "git", -- cgit v1.2.3 From fd484a217c77dba42c29fae1cfdb2390422da847 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 21:25:18 +0300 Subject: feat: add logger and handle eveyrthing nicely --- hooks/handleErrors.ts | 11 +++++++++++ hooks/logging.ts | 4 ++-- hooks/requireAuth.ts | 5 ++++- logger.ts | 22 ++++++++++++++++++++++ package.json | 1 + services/index.ts | 7 ++++--- 6 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 hooks/handleErrors.ts create mode 100644 logger.ts diff --git a/hooks/handleErrors.ts b/hooks/handleErrors.ts new file mode 100644 index 0000000..2a3c728 --- /dev/null +++ b/hooks/handleErrors.ts @@ -0,0 +1,11 @@ +import { HookContext } from '@feathersjs/feathers'; +import logger from '../logger'; + + +export default async (context: HookContext): Promise => { + context.result = context.error.message; + context.statusCode = context.error.code; + logger.error(context.error); + return context; +}; + diff --git a/hooks/logging.ts b/hooks/logging.ts index 840873c..8babe9a 100644 --- a/hooks/logging.ts +++ b/hooks/logging.ts @@ -1,13 +1,13 @@ import { HookContext } from '@feathersjs/feathers'; +import logger from '../logger'; export default async (context: HookContext): Promise => { if (context.params.provider) { const { method, path, id } = context; - const timestamp = new Date().toLocaleString('default', { timeStyle: 'medium', dateStyle: 'short' }); const message = `${method.toUpperCase()}: /${path}/${id || ''}`; const username = context.params.user?.username || 'anonymous'; - console.log(`[${timestamp}] ${message} ${username}`); + logger.log(`${message} ${username}`); } return context; }; diff --git a/hooks/requireAuth.ts b/hooks/requireAuth.ts index a7b0e96..52e8974 100644 --- a/hooks/requireAuth.ts +++ b/hooks/requireAuth.ts @@ -1,7 +1,10 @@ +import { NotAuthenticated } from '@feathersjs/errors'; import { HookContext } from '@feathersjs/feathers'; export default async (context: HookContext): Promise => { - if (!context.params.user) throw new Error('This endpoint requires auth!'); + if (!context.params.authenticated) { + throw new NotAuthenticated('This endpoint requires auth!'); + } return context; }; diff --git a/logger.ts b/logger.ts new file mode 100644 index 0000000..f11b0c8 --- /dev/null +++ b/logger.ts @@ -0,0 +1,22 @@ +type Stream = 'log' | 'error' | 'debug'; + +interface ErrorWithCode extends Error { + code?: number +} + +const dateOpts = { timeStyle: 'medium', dateStyle: 'short' }; +// eslint-disable-next-line +// @ts-ignore +const timestamp = (): string => new Date().toLocaleString('en', dateOpts); + + +const logWithTimestamp = (stream: Stream, message: string): void => { + console[stream](`[${timestamp()}] ${message}`); +}; + +export default { + log: (message: string): void => logWithTimestamp('log', message), + debug: (message: string): void => logWithTimestamp('debug', message), + error: (err: ErrorWithCode): void => logWithTimestamp('error', `${err.code || ''} ${err.name}: ${err.message}`) +}; + diff --git a/package.json b/package.json index e21de51..c6e4038 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@feathersjs/authentication": "^4.5.3", "@feathersjs/authentication-local": "^4.5.4", "@feathersjs/configuration": "^4.5.3", + "@feathersjs/errors": "^4.5.3", "@feathersjs/express": "^4.5.4", "@feathersjs/feathers": "^4.5.3", "@feathersjs/socketio": "^4.5.4", diff --git a/services/index.ts b/services/index.ts index 8d77429..bdce04f 100644 --- a/services/index.ts +++ b/services/index.ts @@ -7,6 +7,7 @@ import Auth from './auth/auth.service'; import tryAuthenticate from '../hooks/tryAuthenticate'; import logging from '../hooks/logging'; +import handleErrors from '../hooks/handleErrors'; export default (app: Application): void => { app.configure(Auth); @@ -17,10 +18,10 @@ export default (app: Application): void => { app.hooks({ before: { - all: tryAuthenticate + all: [tryAuthenticate, logging] }, - after: { - all: logging + error: { + all: handleErrors } }); }; -- cgit v1.2.3