Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api-machine",
"version": "1.0.0",
"version": "1.0.1",
"description": "api-machine",
"private": "true",
"typescript-template": {
Expand All @@ -22,7 +22,7 @@
"publish": "npm run script -- publish"
},
"dependencies": {
"auto-oas": "^1.2.0",
"auto-oas": "^1.2.1",
"cors": "^2.8.5",
"express": "^5.1.0",
"swagger-ui-express": "^5.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/error/http-errors/http-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HttpErrorOptions } from '../error-options';
* All HTTP errors extend from this class
*/
export abstract class HTTPError extends Error {
public isApiMachineError = true;
public readonly options: HttpErrorOptions = {};
public readonly timestamp = new Date().toISOString();

Expand Down
8 changes: 4 additions & 4 deletions src/oas/oas-rest-server-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ export class OasRestServerConverter {

public async convertRouter(router: BaseApiRouter) {
for (const route of router.registeredRoutes) {
if (route instanceof BaseApiEndpoint) {
await this.convertEndpoint(route);
if (route.routeType === 'endpoint') {
await this.convertEndpoint(route as BaseApiEndpoint);
}
else if (route instanceof BaseApiRouter) {
await this.convertRouter(route);
else if (route.routeType === 'router') {
await this.convertRouter(route as BaseApiRouter);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/router/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export abstract class BaseApiRoute {
public fullPath: string;
public name: string;
public description?: string;
public routeType: 'router' | 'endpoint';

/**
* Optional array of Express middleware to apply
Expand Down
1 change: 1 addition & 0 deletions src/router/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum EndpointMethod {
}

export abstract class BaseApiEndpoint extends BaseApiRoute {
override routeType = 'endpoint' as const;
override path = '';
public tag?: string;
public method: EndpointMethod = EndpointMethod.GET;
Expand Down
11 changes: 7 additions & 4 deletions src/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiRoute, BaseApiRoute } from './base';
import { BaseApiEndpoint, EndpointMethod } from './endpoint';

export abstract class BaseApiRouter extends BaseApiRoute {
override routeType = 'router' as const;
protected router: ExpressRouter;

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

if (instance instanceof BaseApiEndpoint) {
instance.tag = tag;
if (instance.routeType === 'endpoint') {
(instance as BaseApiEndpoint).tag = tag;
}

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

this.registeredRoutes.push(instance);

// Track endpoint methods for 405 handling
if (instance instanceof BaseApiEndpoint) {
if (instance.routeType === 'endpoint') {
const path = instance.path;

if (!pathMethods.has(path)) {
pathMethods.set(path, new Set());
}

pathMethods.get(path)!.add(instance.method);
pathMethods
.get(path)!
.add((instance as BaseApiEndpoint).method);
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ export abstract class RestServer {
next: express.NextFunction
) => {
// Handle HTTPError instances
if (error instanceof HTTPError) {
if ((error as HTTPError).isApiMachineError) {
// Set custom headers if provided
Object.entries(error.headers).forEach(([key, value]) => {
response.setHeader(key, value);
});
Object.entries((error as HTTPError).headers).forEach(
([key, value]) => {
response.setHeader(key, value);
}
);

return response
.status(error.getStatusCode())
.json(error.getResponseJson());
.status((error as HTTPError).getStatusCode())
.json((error as HTTPError).getResponseJson());
}

if (
Expand Down