Skip to content

Commit f325238

Browse files
committed
fix: instanceof not working on child repos
1 parent a3e893a commit f325238

6 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/error/http-errors/http-error.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { HttpErrorOptions } from '../error-options';
55
* All HTTP errors extend from this class
66
*/
77
export abstract class HTTPError extends Error {
8+
public isApiMachineError = true;
89
public readonly options: HttpErrorOptions = {};
910
public readonly timestamp = new Date().toISOString();
1011

src/oas/oas-rest-server-converter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ export class OasRestServerConverter {
5959

6060
public async convertRouter(router: BaseApiRouter) {
6161
for (const route of router.registeredRoutes) {
62-
if (route instanceof BaseApiEndpoint) {
63-
await this.convertEndpoint(route);
62+
if (route.routeType === 'endpoint') {
63+
await this.convertEndpoint(route as BaseApiEndpoint);
6464
}
65-
else if (route instanceof BaseApiRouter) {
66-
await this.convertRouter(route);
65+
else if (route.routeType === 'router') {
66+
await this.convertRouter(route as BaseApiRouter);
6767
}
6868
}
6969
}

src/router/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export abstract class BaseApiRoute {
66
public fullPath: string;
77
public name: string;
88
public description?: string;
9+
public routeType: 'router' | 'endpoint';
910

1011
/**
1112
* Optional array of Express middleware to apply

src/router/endpoint.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export enum EndpointMethod {
2727
}
2828

2929
export abstract class BaseApiEndpoint extends BaseApiRoute {
30+
override routeType = 'endpoint' as const;
3031
override path = '';
3132
public tag?: string;
3233
public method: EndpointMethod = EndpointMethod.GET;

src/router/router.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ApiRoute, BaseApiRoute } from './base';
33
import { BaseApiEndpoint, EndpointMethod } from './endpoint';
44

55
export abstract class BaseApiRouter extends BaseApiRoute {
6+
override routeType = 'router' as const;
67
protected router: ExpressRouter;
78

89
public registeredRoutes: BaseApiRoute[] = [];
@@ -45,23 +46,25 @@ export abstract class BaseApiRouter extends BaseApiRoute {
4546
// Set parent relationship for authentication cascading
4647
instance.parentRoute = this;
4748

48-
if (instance instanceof BaseApiEndpoint) {
49-
instance.tag = tag;
49+
if (instance.routeType === 'endpoint') {
50+
(instance as BaseApiEndpoint).tag = tag;
5051
}
5152

5253
await instance.register(this.router, this.fullPath);
5354

5455
this.registeredRoutes.push(instance);
5556

5657
// Track endpoint methods for 405 handling
57-
if (instance instanceof BaseApiEndpoint) {
58+
if (instance.routeType === 'endpoint') {
5859
const path = instance.path;
5960

6061
if (!pathMethods.has(path)) {
6162
pathMethods.set(path, new Set());
6263
}
6364

64-
pathMethods.get(path)!.add(instance.method);
65+
pathMethods
66+
.get(path)!
67+
.add((instance as BaseApiEndpoint).method);
6568
}
6669
}
6770

src/server/server.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,17 @@ export abstract class RestServer {
174174
next: express.NextFunction
175175
) => {
176176
// Handle HTTPError instances
177-
if (error instanceof HTTPError) {
177+
if ((error as HTTPError).isApiMachineError) {
178178
// Set custom headers if provided
179-
Object.entries(error.headers).forEach(([key, value]) => {
180-
response.setHeader(key, value);
181-
});
179+
Object.entries((error as HTTPError).headers).forEach(
180+
([key, value]) => {
181+
response.setHeader(key, value);
182+
}
183+
);
182184

183185
return response
184-
.status(error.getStatusCode())
185-
.json(error.getResponseJson());
186+
.status((error as HTTPError).getStatusCode())
187+
.json((error as HTTPError).getResponseJson());
186188
}
187189

188190
if (

0 commit comments

Comments
 (0)