Skip to content

Commit 4aabf87

Browse files
authored
fix(express): Fix application typings to work with typed configuration (#2539)
1 parent f85aac8 commit 4aabf87

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

packages/express/src/declarations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface ExpressOverrides<Services> {
2727
}
2828

2929
export type Application<Services = any, Settings = any> =
30-
Omit<Express, 'listen'|'use'> &
30+
Omit<Express, 'listen'|'use'|'get'|'set'> &
3131
FeathersApplication<Services, Settings> &
3232
ExpressOverrides<Services>;
3333

packages/express/test/index.test.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import axios from 'axios';
44
import fs from 'fs';
55
import path from 'path';
66
import https from 'https';
7-
import { feathers, HookContext, Id, Application } from '@feathersjs/feathers';
7+
import { feathers, HookContext, Id } from '@feathersjs/feathers';
88

9-
import * as expressify from '../src';
9+
import {
10+
default as feathersExpress, rest, notFound, errorHandler, original, serveStatic
11+
} from '../src';
1012
import { RequestListener } from 'http';
1113

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

1921
it('exports .default, .original .rest, .notFound and .errorHandler', () => {
20-
assert.strictEqual(expressify.original, express);
21-
assert.strictEqual(typeof expressify.rest, 'function');
22-
assert.ok(expressify.notFound);
23-
assert.ok(expressify.errorHandler);
22+
assert.strictEqual(original, express);
23+
assert.strictEqual(typeof rest, 'function');
24+
assert.ok(notFound);
25+
assert.ok(errorHandler);
2426
});
2527

26-
it('returns an Express application', () => {
27-
const app: Application = expressify.default(feathers());
28+
it('returns an Express application, keeps Feathers service and configuration typings typings', () => {
29+
type Config = {
30+
hostname: string;
31+
port: number;
32+
}
33+
34+
const app = feathersExpress<{}, Config>(feathers());
35+
36+
app.set('hostname', 'test.com');
37+
38+
const hostname = app.get('hostname');
2839

40+
assert.strictEqual(hostname, 'test.com');
2941
assert.strictEqual(typeof app, 'function');
3042
});
3143

3244
it('allows to use an existing Express instance', () => {
3345
const expressApp = express();
34-
const app = expressify.default(feathers(), expressApp);
46+
const app = feathersExpress(feathers(), expressApp);
3547

3648
assert.strictEqual(app, expressApp);
3749
});
3850

3951
it('exports `express.rest`', () => {
40-
assert.ok(typeof expressify.rest === 'function');
52+
assert.ok(typeof rest === 'function');
4153
});
4254

4355
it('returns a plain express app when no app is provided', () => {
44-
const app = expressify.default();
56+
const app = feathersExpress();
4557

4658
assert.strictEqual(typeof app.use, 'function');
4759
assert.strictEqual(typeof app.service, 'undefined');
@@ -51,7 +63,7 @@ describe('@feathersjs/express', () => {
5163
it('errors when app with wrong version is provided', () => {
5264
try {
5365
// @ts-ignore
54-
expressify.default({});
66+
feathersExpress({});
5567
} catch (e: any) {
5668
assert.strictEqual(e.message, '@feathersjs/express requires a valid Feathers application instance');
5769
}
@@ -60,7 +72,7 @@ describe('@feathersjs/express', () => {
6072
const app = feathers();
6173
app.version = '2.9.9';
6274

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

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

7890
it('Can use Express sub-apps', () => {
7991
const typedApp = feathers<{}>();
80-
const app = expressify.default(typedApp);
92+
const app = feathersExpress(typedApp);
8193
const child = express();
8294

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

8799
it('Can use express.static', () => {
88-
const app = expressify.default(feathers());
100+
const app = feathersExpress(feathers());
89101

90-
app.use('/path', expressify.static(__dirname));
102+
app.use('/path', serveStatic(__dirname));
91103
});
92104

93105
it('has Feathers functionality', async () => {
94-
const app = expressify.default(feathers());
106+
const app = feathersExpress(feathers());
95107

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

@@ -121,7 +133,7 @@ describe('@feathersjs/express', () => {
121133
});
122134

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

143155
it('.listen calls .setup', async () => {
144-
const app = expressify.default(feathers());
156+
const app = feathersExpress(feathers());
145157
let called = false;
146158

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

165177
it('passes middleware as options', () => {
166178
const feathersApp = feathers();
167-
const app = expressify.default(feathersApp);
179+
const app = feathersExpress(feathersApp);
168180
const oldUse = feathersApp.use;
169181
const a = (_req: Request, _res: Response, next: NextFunction) => next();
170182
const b = (_req: Request, _res: Response, next: NextFunction) => next();
@@ -189,7 +201,7 @@ describe('@feathersjs/express', () => {
189201
});
190202

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

194206
app.use('/test', {
195207
async get (id: Id) {
@@ -220,7 +232,7 @@ describe('@feathersjs/express', () => {
220232
}
221233
};
222234

223-
const app = expressify.default(feathers()).configure(expressify.rest());
235+
const app = feathersExpress(feathers()).configure(rest());
224236

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

0 commit comments

Comments
 (0)