Skip to content

Commit 9573079

Browse files
committed
fix: enhance endpoint options with authenticated and no-auth variants
1 parent 36e391e commit 9573079

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

adminforth/servers/express.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ import { Express } from 'express';
55
import type { AnySchemaObject } from 'ajv';
66
import { apiReference } from '@scalar/express-api-reference';
77
import fetch from 'node-fetch';
8-
import { AdminUserAuthorizeFunction, IAdminForth, IAdminForthEndpointOptions, IAdminForthExpressRouteSchema, IExpressHttpServer, HttpExtra } from '../types/Back.js';
8+
import {
9+
AdminUserAuthorizeFunction,
10+
IAdminForth,
11+
IAdminForthAuthenticatedEndpointOptions,
12+
IAdminForthEndpointOptions,
13+
IAdminForthExpressRouteSchema,
14+
IAdminForthNoAuthEndpointOptions,
15+
IExpressHttpServer,
16+
HttpExtra,
17+
} from '../types/Back.js';
918
import { WebSocketServer } from 'ws';
1019
import { WebSocketClient } from './common.js';
1120
import { AdminUser } from '../types/Common.js';
@@ -547,6 +556,8 @@ class ExpressServer implements IExpressHttpServer {
547556
}));
548557
}
549558

559+
endpoint(options: IAdminForthAuthenticatedEndpointOptions): void;
560+
endpoint(options: IAdminForthNoAuthEndpointOptions): void;
550561
endpoint(options: IAdminForthEndpointOptions) {
551562
const {
552563
method='GET',

adminforth/types/Back.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,35 @@ export interface IAdminForthEndpointHandlerInput {
5454
tr: ITranslateFunction;
5555
}
5656

57-
export interface IAdminForthEndpointOptions {
57+
export interface IAdminForthAuthenticatedEndpointHandlerInput extends IAdminForthEndpointHandlerInput {
58+
adminUser: AdminUser;
59+
}
60+
61+
export interface IAdminForthEndpointOptionsBase {
5862
method: string,
59-
noAuth?: boolean,
6063
path: string,
6164
description?: string,
6265
request_schema?: AnySchemaObject,
6366
response_schema?: AnySchemaObject,
6467
responce_schema?: AnySchemaObject,
6568
meta?: Record<string, unknown>,
6669
target?: 'json' | 'upload',
70+
}
71+
72+
export interface IAdminForthAuthenticatedEndpointOptions extends IAdminForthEndpointOptionsBase {
73+
noAuth?: false,
74+
handler: (input: IAdminForthAuthenticatedEndpointHandlerInput) => void | Promise<any>,
75+
}
76+
77+
export interface IAdminForthNoAuthEndpointOptions extends IAdminForthEndpointOptionsBase {
78+
noAuth: true,
6779
handler: (input: IAdminForthEndpointHandlerInput) => void | Promise<any>,
6880
}
6981

82+
export type IAdminForthEndpointOptions =
83+
| IAdminForthAuthenticatedEndpointOptions
84+
| IAdminForthNoAuthEndpointOptions;
85+
7086
export type AdminForthExpressSchemaInput = AnySchemaObject | ZodType;
7187

7288
export interface IAdminForthExpressRouteSchema {
@@ -143,7 +159,8 @@ export interface IHttpServer {
143159
*
144160
* @param options : Object with method, path and handler properties.
145161
*/
146-
endpoint(options: IAdminForthEndpointOptions): void;
162+
endpoint(options: IAdminForthAuthenticatedEndpointOptions): void;
163+
endpoint(options: IAdminForthNoAuthEndpointOptions): void;
147164

148165
}
149166

0 commit comments

Comments
 (0)