Skip to content

Commit df03457

Browse files
fix instance app types
1 parent 4573b17 commit df03457

6 files changed

Lines changed: 67 additions & 29 deletions

File tree

packages/oc-fastify-server-adapter/src/index.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type ExpressMiddleware = (
9595
next: (err?: unknown) => void
9696
) => void;
9797

98-
export type HttpServerAdapter = {
98+
export type HttpServerAdapter<TNative = unknown> = {
9999
name: string;
100100
enableBodyParser(opts: { limit?: number | string }): void;
101101
enableCookies(): void;
@@ -120,7 +120,7 @@ export type HttpServerAdapter = {
120120
onServerError(cb: (err: Error) => void): void;
121121
close(cb: (err?: Error) => void): void;
122122
isListening(): boolean;
123-
native(): unknown;
123+
native(): TNative;
124124
httpServer(): http.Server;
125125
};
126126

@@ -150,22 +150,26 @@ type MutableOcRequest = OcRequest & {
150150
files?: UploadedFile[] | Record<string, UploadedFile[]>;
151151
};
152152

153-
type FastifyServerAdapterFactory = {
154-
(options?: unknown): HttpServerAdapter;
155-
readonly __serverAdapterOptions?:
156-
| FastifyServerAdapterOptions
157-
| number
158-
| string;
153+
export type HttpServerAdapterFactory<TOptions = unknown, TNative = unknown> = {
154+
(options?: unknown): HttpServerAdapter<TNative>;
155+
readonly __serverAdapterOptions?: TOptions;
159156
};
160157

161-
const createFastifyAdapter = ((options?: unknown): HttpServerAdapter =>
158+
type FastifyServerAdapterFactory = HttpServerAdapterFactory<
159+
FastifyServerAdapterOptions | number | string,
160+
FastifyInstance
161+
>;
162+
163+
const createFastifyAdapter = ((
164+
options?: unknown
165+
): HttpServerAdapter<FastifyInstance> =>
162166
new FastifyHttpServerAdapter(
163167
toFastifyAdapterOptions(options)
164168
)) as FastifyServerAdapterFactory;
165169

166170
export default createFastifyAdapter;
167171

168-
class FastifyHttpServerAdapter implements HttpServerAdapter {
172+
class FastifyHttpServerAdapter implements HttpServerAdapter<FastifyInstance> {
169173
name = 'fastify';
170174

171175
private app: FastifyInstance;

packages/oc-fastify-server-adapter/test/registry-node.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ import {
99
stat,
1010
writeFile
1111
} from 'node:fs/promises';
12+
import type http from 'node:http';
1213
import { createServer } from 'node:net';
1314
import { tmpdir } from 'node:os';
1415
import path from 'node:path';
1516
import { promisify } from 'node:util';
17+
import type { FastifyInstance } from 'fastify';
1618
import type { RegistryOptions } from '../../oc/dist/registry';
19+
import type { HttpServerAdapterFactory } from '../../oc/dist/registry/domain/http-server/types';
1720
import createFastifyAdapter from '..';
1821

1922
type RegistryFactory = typeof import('../../oc/dist/registry').default;
2023
type StartedRegistry = ReturnType<RegistryFactory>;
2124

2225
type StartedData = {
23-
app: unknown;
24-
server: { address(): string | { port: number } | null };
26+
app: FastifyInstance;
27+
server: http.Server;
2528
};
2629

2730
const Registry = require(path.resolve(__dirname, '../../../oc/dist/registry'))
@@ -44,7 +47,9 @@ type FileStorageOptions = {
4447
root: string;
4548
};
4649

47-
const inferRegistryOptions = <T, U>(options: RegistryOptions<T, U>) => options;
50+
const inferRegistryOptions = <T, U extends HttpServerAdapterFactory>(
51+
options: RegistryOptions<T, U>
52+
) => options;
4853
const fastifyRegistryOptions = inferRegistryOptions({
4954
baseUrl: 'http://localhost:3000/',
5055
server: {
@@ -199,7 +204,7 @@ const startRegistry = async (
199204
},
200205
verbosity: 0,
201206
...options
202-
} as never);
207+
});
203208

204209
const data = await new Promise<StartedData>((resolve, reject) => {
205210
registry.start((err, startData) => {

packages/oc/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
export { default as Client } from 'oc-client';
22
export { default as cli } from './cli/programmatic-api';
3+
export type { RegistryType } from './registry';
34
export { default as Registry, RegistryOptions } from './registry';
45

56
export type {
67
CookieOptions,
78
ExpressMiddleware,
89
HttpServerAdapter,
10+
HttpServerAdapterFactory,
911
Method,
12+
NativeApp,
1013
OcHandler,
1114
OcRequest,
1215
OcResponse,

packages/oc/src/registry/domain/http-server/express-adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function normaliseParams(raw: Record<string, unknown>): Record<string, string> {
4242

4343
export default function createExpressAdapter(
4444
options?: unknown
45-
): HttpServerAdapter {
45+
): HttpServerAdapter<Express> {
4646
const adapterOptions = options as
4747
| { port?: number | string }
4848
| number
@@ -53,7 +53,7 @@ export default function createExpressAdapter(
5353
);
5454
}
5555

56-
class ExpressHttpServerAdapter implements HttpServerAdapter {
56+
class ExpressHttpServerAdapter implements HttpServerAdapter<Express> {
5757
name = 'express';
5858

5959
private app: Express;

packages/oc/src/registry/domain/http-server/types.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type http from 'node:http';
22
import type { IncomingHttpHeaders } from 'node:http';
3+
import type express from 'express';
34
import type { Config } from '../../../types';
45

56
export interface CookieOptions {
@@ -83,8 +84,8 @@ export type ExpressMiddleware = (
8384
next: (err?: unknown) => void
8485
) => void;
8586

86-
export type HttpServerAdapterFactory<TOptions = unknown> = {
87-
(options?: unknown): HttpServerAdapter;
87+
export type HttpServerAdapterFactory<TOptions = unknown, TNative = unknown> = {
88+
(options?: unknown): HttpServerAdapter<TNative>;
8889
readonly __serverAdapterOptions?: TOptions;
8990
};
9091

@@ -96,7 +97,19 @@ export type HttpServerAdapterOptions<TAdapter> = TAdapter extends {
9697
? TOptions
9798
: unknown;
9899

99-
export interface HttpServerAdapter {
100+
export type NativeApp<TAdapter> = TAdapter extends (
101+
...args: any[]
102+
) => HttpServerAdapter<infer TNative>
103+
? unknown extends TNative
104+
? express.Express
105+
: TNative
106+
: TAdapter extends HttpServerAdapter<infer TNative>
107+
? unknown extends TNative
108+
? express.Express
109+
: TNative
110+
: express.Express;
111+
112+
export interface HttpServerAdapter<TNative = unknown> {
100113
name: string;
101114
enableBodyParser(opts: { limit?: number | string }): void;
102115
enableCookies(): void;
@@ -121,6 +134,6 @@ export interface HttpServerAdapter {
121134
onServerError(cb: (err: Error) => void): void;
122135
close(cb: (err?: Error) => void): void;
123136
isListening(): boolean;
124-
native(): unknown;
137+
native(): TNative;
125138
httpServer(): http.Server;
126139
}

packages/oc/src/registry/index.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type http from 'node:http';
22
import colors from 'colors/safe';
3-
import type express from 'express';
43

54
import type { Plugin } from '../types';
65
import appStart from './app-start';
76
import eventsHandler from './domain/events-handler';
8-
import type { HttpServerAdapterFactory } from './domain/http-server/types';
7+
import type {
8+
HttpServerAdapterFactory,
9+
NativeApp
10+
} from './domain/http-server/types';
911
import sanitiseOptions, { RegistryOptions } from './domain/options-sanitiser';
1012
import * as pluginsInitialiser from './domain/plugins-initialiser';
1113
import Repository from './domain/repository';
@@ -16,10 +18,24 @@ import { create as createRouter } from './router';
1618

1719
export { RegistryOptions };
1820

21+
export interface RegistryType<TApp = NativeApp<HttpServerAdapterFactory>> {
22+
close: (callback: (err?: Error | undefined | string) => void) => void;
23+
on: typeof eventsHandler.on;
24+
register: <T = any>(
25+
plugin: Plugin<T>,
26+
callback?: (...args: any[]) => void
27+
) => void;
28+
start: (
29+
callback: (err: unknown, data?: { app: TApp; server: http.Server }) => void
30+
) => Promise<void>;
31+
app: TApp;
32+
}
33+
1934
export default function registry<
2035
T = any,
21-
TServerAdapter extends HttpServerAdapterFactory = HttpServerAdapterFactory
22-
>(inputOptions: RegistryOptions<T, TServerAdapter>) {
36+
TServerAdapter extends HttpServerAdapterFactory = HttpServerAdapterFactory,
37+
TApp = NativeApp<TServerAdapter>
38+
>(inputOptions: RegistryOptions<T, TServerAdapter>): RegistryType<TApp> {
2339
const validationResult =
2440
validator.validateRegistryConfiguration(inputOptions);
2541
if (!validationResult.isValid) {
@@ -32,7 +48,7 @@ export default function registry<
3248
getHttpServerAdapter(options.server.adapter, options.server.options),
3349
options
3450
);
35-
const app = adapter.native() as express.Express;
51+
const app = adapter.native() as TApp;
3652
const repository = Repository(options);
3753

3854
const close = (
@@ -59,10 +75,7 @@ export default function registry<
5975
};
6076

6177
const start = async (
62-
callback: (
63-
err: unknown,
64-
data?: { app: express.Express; server: http.Server }
65-
) => void
78+
callback: (err: unknown, data?: { app: TApp; server: http.Server }) => void
6679
) => {
6780
const ok = (msg: string) => console.log(colors.green(msg));
6881

0 commit comments

Comments
 (0)