From aa279f45130ca9f643b6726f8ee109817d8b0830 Mon Sep 17 00:00:00 2001 From: Dan Caddigan Date: Sat, 26 Jan 2019 01:58:12 -0500 Subject: [PATCH] feat(app): rename app to server (#44) --- README.md | 6 +++--- examples/1-simple-model/src/index.ts | 6 +++--- examples/2-complex-example/src/index.test.ts | 10 +++++----- examples/2-complex-example/src/index.ts | 6 +++--- .../2-complex-example/src/{app.ts => server.ts} | 4 ++-- examples/2-complex-example/tools/generate.ts | 2 +- examples/2-complex-example/tools/seed.ts | 10 +++++----- examples/3-one-to-many-relationship/src/index.ts | 8 ++++---- examples/4-many-to-many-relationship/src/index.ts | 8 ++++---- src/core/index.ts | 2 +- src/core/{app.ts => server.ts} | 14 +++++++++----- 11 files changed, 40 insertions(+), 36 deletions(-) rename examples/2-complex-example/src/{app.ts => server.ts} (91%) rename src/core/{app.ts => server.ts} (96%) diff --git a/README.md b/README.md index f8abb61e..a9a44bc4 100644 --- a/README.md +++ b/README.md @@ -79,11 +79,11 @@ export class UserResolver extends BaseResolver { import 'reflect-metadata'; import { Container } from 'typedi'; -import { App } from 'warthog'; +import { Server } from 'warthog'; async function bootstrap() { - const app = new App({ container: Container }); - return app.start(); + const server = new Server({ container: Container }); + return server.start(); } bootstrap() diff --git a/examples/1-simple-model/src/index.ts b/examples/1-simple-model/src/index.ts index e3c8113f..3daa4b0d 100644 --- a/examples/1-simple-model/src/index.ts +++ b/examples/1-simple-model/src/index.ts @@ -4,15 +4,15 @@ import { Container } from 'typedi'; dotenv.config(); -import { App } from '../../../src/'; +import { Server } from '../../../src/'; async function bootstrap() { - const app = new App({ + const server = new Server({ container: Container, warthogImportPath: '../../../src' // Path written in generated classes }); - return app.start(); + return server.start(); } bootstrap().catch((error: Error) => { diff --git a/examples/2-complex-example/src/index.test.ts b/examples/2-complex-example/src/index.test.ts index e8429276..97b2daac 100644 --- a/examples/2-complex-example/src/index.test.ts +++ b/examples/2-complex-example/src/index.test.ts @@ -3,18 +3,18 @@ import 'reflect-metadata'; import { Binding } from '../generated/binding'; -import { getServer } from './app'; import { User } from './modules/user/user.model'; +import { getServer } from './server'; -const app = getServer({}, { logging: false }); +const server = getServer({}, { logging: false }); let binding: Binding; let testUser: User; beforeAll(async done => { console.error = jest.fn(); - await app.start(); - binding = ((await app.getBinding()) as unknown) as Binding; // TODO: clean this up + await server.start(); + binding = ((await server.getBinding()) as unknown) as Binding; // TODO: clean this up const key = new Date().getTime(); @@ -34,7 +34,7 @@ beforeAll(async done => { afterAll(async done => { (console.error as any).mockRestore(); - await app.stop(); + await server.stop(); done(); }); diff --git a/examples/2-complex-example/src/index.ts b/examples/2-complex-example/src/index.ts index 3b0a46f2..9dc07850 100644 --- a/examples/2-complex-example/src/index.ts +++ b/examples/2-complex-example/src/index.ts @@ -2,13 +2,13 @@ import 'reflect-metadata'; import * as dotenv from 'dotenv'; -import { getServer } from './app'; +import { getServer } from './server'; dotenv.config(); async function bootstrap() { - const app = getServer(); - await app.start(); + const server = getServer(); + await server.start(); } bootstrap().catch((error: Error) => { diff --git a/examples/2-complex-example/src/app.ts b/examples/2-complex-example/src/server.ts similarity index 91% rename from examples/2-complex-example/src/app.ts rename to examples/2-complex-example/src/server.ts index 336607de..360edac1 100644 --- a/examples/2-complex-example/src/app.ts +++ b/examples/2-complex-example/src/server.ts @@ -2,7 +2,7 @@ import 'reflect-metadata'; import { Container } from 'typedi'; -import { App, BaseContext } from '../../../src/'; +import { BaseContext, Server } from '../../../src/'; // import { User } from './modules/user/user.model'; @@ -15,7 +15,7 @@ interface Context extends BaseContext { } export function getServer(AppOptions = {}, dbOptions = {}) { - return new App( + return new Server( { container: Container, // Inject a fake user. In a real app you'd parse a JWT to add the user diff --git a/examples/2-complex-example/tools/generate.ts b/examples/2-complex-example/tools/generate.ts index 6724d126..d08cd3d6 100644 --- a/examples/2-complex-example/tools/generate.ts +++ b/examples/2-complex-example/tools/generate.ts @@ -1,6 +1,6 @@ import * as Debug from 'debug'; -import { getServer } from '../src/app'; +import { getServer } from '../src/server'; const logger = Debug('warthog:generate'); diff --git a/examples/2-complex-example/tools/seed.ts b/examples/2-complex-example/tools/seed.ts index ad501b07..fc2acca8 100644 --- a/examples/2-complex-example/tools/seed.ts +++ b/examples/2-complex-example/tools/seed.ts @@ -1,7 +1,7 @@ import * as Debug from 'debug'; import * as Faker from 'faker'; -import { getServer } from '../src/app'; +import { getServer } from '../src/server'; if (process.env.NODE_ENV !== 'development') { throw 'Seeding only available in development environment'; @@ -12,10 +12,10 @@ const logger = Debug('warthog:seed'); const NUM_USERS = 100; async function seedDatabase() { - const app = getServer(); - await app.start(); + const server = getServer(); + await server.start(); - const binding = await app.getBinding(); + const binding = await server.getBinding(); for (let index = 0; index < NUM_USERS; index++) { const random = new Date() @@ -45,7 +45,7 @@ async function seedDatabase() { } } - return app.stop(); + return server.stop(); } seedDatabase() diff --git a/examples/3-one-to-many-relationship/src/index.ts b/examples/3-one-to-many-relationship/src/index.ts index 3c8cf12f..c4bf5a7e 100644 --- a/examples/3-one-to-many-relationship/src/index.ts +++ b/examples/3-one-to-many-relationship/src/index.ts @@ -1,18 +1,18 @@ -import 'reflect-metadata'; import * as dotenv from 'dotenv'; +import 'reflect-metadata'; import { Container } from 'typedi'; dotenv.config(); -import { App } from '../../../src/'; +import { Server } from '../../../src/'; async function bootstrap() { - const app = new App({ + const server = new Server({ container: Container, warthogImportPath: '../../../src' // Path written in generated classes }); - await app.start(); + await server.start(); } bootstrap().catch((error: Error) => { diff --git a/examples/4-many-to-many-relationship/src/index.ts b/examples/4-many-to-many-relationship/src/index.ts index 3c8cf12f..c4bf5a7e 100644 --- a/examples/4-many-to-many-relationship/src/index.ts +++ b/examples/4-many-to-many-relationship/src/index.ts @@ -1,18 +1,18 @@ -import 'reflect-metadata'; import * as dotenv from 'dotenv'; +import 'reflect-metadata'; import { Container } from 'typedi'; dotenv.config(); -import { App } from '../../../src/'; +import { Server } from '../../../src/'; async function bootstrap() { - const app = new App({ + const server = new Server({ container: Container, warthogImportPath: '../../../src' // Path written in generated classes }); - await app.start(); + await server.start(); } bootstrap().catch((error: Error) => { diff --git a/src/core/index.ts b/src/core/index.ts index 6dd5a23e..2f401b00 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,4 +1,4 @@ -export * from './app'; +export * from './server'; export * from './BaseModel'; export * from './Context'; export * from './types'; diff --git a/src/core/app.ts b/src/core/server.ts similarity index 96% rename from src/core/app.ts rename to src/core/server.ts index a433afd0..40f17cef 100644 --- a/src/core/app.ts +++ b/src/core/server.ts @@ -24,9 +24,10 @@ import { CodeGenerator } from './code-generator'; import { BaseContext } from './Context'; import { Maybe } from './types'; -export interface AppOptions { - authChecker?: AuthChecker; +export interface ServerOptions { container: Container; + + authChecker?: AuthChecker; context?: (request: Request) => object; host?: string; generatedFolder?: string; @@ -34,11 +35,11 @@ export interface AppOptions { mockDBConnection?: boolean; openPlayground?: boolean; port?: string | number; - resolversPath: string[]; + resolversPath?: string[]; warthogImportPath?: string; } -export class App { +export class Server { appHost: string; appPort: number; authChecker: AuthChecker; @@ -52,7 +53,7 @@ export class App { schema?: GraphQLSchema; constructor( - private appOptions: AppOptions, + private appOptions: ServerOptions, private dbOptions: Partial = {} ) { if (!process.env.NODE_ENV) { @@ -190,3 +191,6 @@ export class App { return process.env.NODE_ENV === 'development'; } } + +// Backwards compatability. This was renamed. +export const App = Server;