Skip to content

Commit

Permalink
fix(express): Fix application typings to work with typed configuration (
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Feb 15, 2022
1 parent 55d1766 commit b9dfaee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/express/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface ExpressOverrides<Services> {
}

export type Application<Services = any, Settings = any> =
Omit<Express, 'listen'|'use'> &
Omit<Express, 'listen'|'use'|'get'|'set'> &
FeathersApplication<Services, Settings> &
ExpressOverrides<Services>;

Expand Down
58 changes: 35 additions & 23 deletions packages/express/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import axios from 'axios';
import fs from 'fs';
import path from 'path';
import https from 'https';
import { feathers, HookContext, Id, Application } from '@feathersjs/feathers';
import { feathers, HookContext, Id } from '@feathersjs/feathers';

import * as expressify from '../src';
import {
default as feathersExpress, rest, notFound, errorHandler, original, serveStatic
} from '../src';
import { RequestListener } from 'http';

describe('@feathersjs/express', () => {
Expand All @@ -17,31 +19,41 @@ describe('@feathersjs/express', () => {
};

it('exports .default, .original .rest, .notFound and .errorHandler', () => {
assert.strictEqual(expressify.original, express);
assert.strictEqual(typeof expressify.rest, 'function');
assert.ok(expressify.notFound);
assert.ok(expressify.errorHandler);
assert.strictEqual(original, express);
assert.strictEqual(typeof rest, 'function');
assert.ok(notFound);
assert.ok(errorHandler);
});

it('returns an Express application', () => {
const app: Application = expressify.default(feathers());
it('returns an Express application, keeps Feathers service and configuration typings typings', () => {
type Config = {
hostname: string;
port: number;
}

const app = feathersExpress<{}, Config>(feathers());

app.set('hostname', 'test.com');

const hostname = app.get('hostname');

assert.strictEqual(hostname, 'test.com');
assert.strictEqual(typeof app, 'function');
});

it('allows to use an existing Express instance', () => {
const expressApp = express();
const app = expressify.default(feathers(), expressApp);
const app = feathersExpress(feathers(), expressApp);

assert.strictEqual(app, expressApp);
});

it('exports `express.rest`', () => {
assert.ok(typeof expressify.rest === 'function');
assert.ok(typeof rest === 'function');
});

it('returns a plain express app when no app is provided', () => {
const app = expressify.default();
const app = feathersExpress();

assert.strictEqual(typeof app.use, 'function');
assert.strictEqual(typeof app.service, 'undefined');
Expand All @@ -51,7 +63,7 @@ describe('@feathersjs/express', () => {
it('errors when app with wrong version is provided', () => {
try {
// @ts-ignore
expressify.default({});
feathersExpress({});
} catch (e: any) {
assert.strictEqual(e.message, '@feathersjs/express requires a valid Feathers application instance');
}
Expand All @@ -60,7 +72,7 @@ describe('@feathersjs/express', () => {
const app = feathers();
app.version = '2.9.9';

expressify.default(app);
feathersExpress(app);
} catch (e: any) {
assert.strictEqual(e.message, '@feathersjs/express requires an instance of a Feathers application version 3.x or later (got 2.9.9)');
}
Expand All @@ -69,29 +81,29 @@ describe('@feathersjs/express', () => {
const app = feathers();
delete app.version;

expressify.default(app);
feathersExpress(app);
} catch (e: any) {
assert.strictEqual(e.message, '@feathersjs/express requires an instance of a Feathers application version 3.x or later (got unknown)');
}
});

it('Can use Express sub-apps', () => {
const typedApp = feathers<{}>();
const app = expressify.default(typedApp);
const app = feathersExpress(typedApp);
const child = express();

app.use('/path', child);
assert.strictEqual((child as any).parent, app);
});

it('Can use express.static', () => {
const app = expressify.default(feathers());
const app = feathersExpress(feathers());

app.use('/path', expressify.static(__dirname));
app.use('/path', serveStatic(__dirname));
});

it('has Feathers functionality', async () => {
const app = expressify.default(feathers());
const app = feathersExpress(feathers());

app.use('/myservice', service);

Expand Down Expand Up @@ -121,7 +133,7 @@ describe('@feathersjs/express', () => {
});

it('can register a service and start an Express server', async () => {
const app = expressify.default(feathers());
const app = feathersExpress(feathers());
const response = {
message: 'Hello world'
};
Expand All @@ -141,7 +153,7 @@ describe('@feathersjs/express', () => {
});

it('.listen calls .setup', async () => {
const app = expressify.default(feathers());
const app = feathersExpress(feathers());
let called = false;

app.use('/myservice', {
Expand All @@ -164,7 +176,7 @@ describe('@feathersjs/express', () => {

it('passes middleware as options', () => {
const feathersApp = feathers();
const app = expressify.default(feathersApp);
const app = feathersExpress(feathersApp);
const oldUse = feathersApp.use;
const a = (_req: Request, _res: Response, next: NextFunction) => next();
const b = (_req: Request, _res: Response, next: NextFunction) => next();
Expand All @@ -189,7 +201,7 @@ describe('@feathersjs/express', () => {
});

it('Express wrapped and context.app are the same', async () => {
const app = expressify.default(feathers());
const app = feathersExpress(feathers());

app.use('/test', {
async get (id: Id) {
Expand Down Expand Up @@ -220,7 +232,7 @@ describe('@feathersjs/express', () => {
}
};

const app = expressify.default(feathers()).configure(expressify.rest());
const app = feathersExpress(feathers()).configure(rest());

app.use('/secureTodos', todoService);

Expand Down

0 comments on commit b9dfaee

Please sign in to comment.