Skip to content

Commit 1265db8

Browse files
authored
Merge pull request #303 from devforth/AdminForth/758
feat: add ITranslateFunction, IAdminUserExpressRequest and ITransla…
2 parents c42ba35 + cbbddb4 commit 1265db8

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

adminforth/documentation/docs/tutorial/03-Customization/06-customPages.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,14 @@ Open `index.ts` file and add the following code *BEFORE* `admin.express.serve(`
306306
307307
```ts title="/index.ts"
308308
309+
import type { IAdminUserExpressRequest } from 'adminforth';
310+
import express from 'express';
311+
309312
....
310313
311314
app.get(`${ADMIN_BASE_URL}/api/dashboard/`,
312315
admin.express.authorize(
313-
async (req:any, res:any) => {
316+
async (req:IAdminUserExpressRequest, res: express.Response) => {
314317
const days = req.body.days || 7;
315318
const apartsByDays = admin.resource('aparts').dataConnector.client.prepare(
316319
`SELECT

adminforth/documentation/docs/tutorial/03-Customization/08-pageInjections.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,14 @@ Now create file `ApartsPie.vue` in the `custom` folder of your project:
9898
Also we have to add an Api to get percentages:
9999
100100
```ts title="./index.ts"
101+
import type { IAdminUserExpressRequest } from 'adminforth';
102+
import express from 'express';
103+
104+
....
105+
101106
app.get(`${ADMIN_BASE_URL}/api/aparts-by-room-percentages/`,
102107
admin.express.authorize(
103-
async (req, res) => {
108+
async (req: IAdminUserExpressRequest, res: express.Response) => {
104109
const roomPercentages = await admin.resource('aparts').dataConnector.client.prepare(
105110
`SELECT
106111
number_of_rooms,

adminforth/documentation/docs/tutorial/07-Plugins/01-AuditLog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,14 @@ Audit log is able to catch only standard actions like `create`, `update`, `delet
160160
If you have a custom, self coded actions in your API, you can log them by calling `logCustomAction` method of `AuditLogPlugin` instance:
161161
162162
```ts title="./resources/index.ts"
163+
import type { IAdminUserExpressRequest, ITranslateExpressRequest } from 'adminforth';
164+
import express from 'express';
165+
166+
....
163167

164168
app.get(`${ADMIN_BASE_URL}/api/dashboard/`,
165169
admin.express.authorize(
166-
async (req, res) => {
170+
async (req: IAdminUserExpressRequest, res: express.Response) => {
167171

168172
admin.getPluginByClassName<AuditLogPlugin>('AuditLogPlugin').logCustomAction({
169173
resourceId: 'aparts',

adminforth/documentation/docs/tutorial/07-Plugins/10-i18n.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,15 @@ onMounted(async () => {
425425
And on the backend side you can use tr function to translate the string:
426426
427427
```ts title="./index.ts"
428+
import type { IAdminUserExpressRequest, ITranslateExpressRequest } from 'adminforth';
429+
import express from 'express';
430+
431+
....
432+
428433
app.get(`${ADMIN_BASE_URL}/api/greeting`,
429434
admin.express.authorize(
430435
admin.express.translatable(
431-
async (req, res) => {
436+
async (req: IAdminUserExpressRequest & ITranslateExpressRequest, res: express.Response) => {
432437
res.json({
433438
text: await req.tr('Welcome, {name}', 'customApis', { name: req.adminUser.username }),
434439
});

adminforth/types/Back.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Express } from 'express';
1+
import type { Express, Request } from 'express';
22
import type { Writable } from 'stream';
33

44
import { ActionCheckSource, AdminForthFilterOperators, AdminForthSortDirections, AllowedActionsEnum,
@@ -105,6 +105,23 @@ export interface IExpressHttpServer extends IHttpServer {
105105
authorize(callable: Function): void;
106106
}
107107

108+
export interface ITranslateFunction {
109+
(
110+
msg: string,
111+
category: string,
112+
params: any,
113+
pluralizationNumber?: number
114+
): Promise<string>;
115+
}
116+
117+
// Omit <Request, 'param'> is used to remove 'param' method from Request type for correct docs generation
118+
export interface IAdminUserExpressRequest extends Omit<Request, 'param'> {
119+
adminUser: AdminUser;
120+
}
121+
122+
export interface ITranslateExpressRequest extends Omit<Request, 'param'> {
123+
tr: ITranslateFunction;
124+
}
108125

109126
export interface IAdminForthSingleFilter {
110127
field?: string;

dev-demo/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import betterSqlite3 from 'better-sqlite3';
22
import express from 'express';
33
import AdminForth, { AdminUser, Filters } from '../adminforth/index.js';
4-
4+
import type { IAdminUserExpressRequest, ITranslateExpressRequest } from 'adminforth';
55
import clicksResource from './resources/clicks.js';
66
import apartmentsResource from './resources/apartments.js';
77
import apartmentBuyersResource from './resources/apartment_buyers.js';
@@ -901,7 +901,7 @@ const port = process.env.PORT || 3000;
901901
app.get(
902902
'/api/testtest/',
903903
admin.express.authorize(
904-
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
904+
async (req: IAdminUserExpressRequest, res: express.Response, next: express.NextFunction) => {
905905
res.json({ ok: true, data: [1,2,3], adminUser: req.adminUser });
906906
}
907907
)
@@ -910,7 +910,7 @@ app.get(
910910
app.get(`${ADMIN_BASE_URL}/api/dashboard/`,
911911
admin.express.authorize(
912912
admin.express.translatable(
913-
async (req: any, res: express.Response) => {
913+
async (req: IAdminUserExpressRequest & ITranslateExpressRequest, res: express.Response) => {
914914
const days = req.body.days || 7;
915915
const apartsByDays = await admin.resource('aparts').dataConnector.client.prepare(
916916
`SELECT
@@ -1010,7 +1010,7 @@ app.get(`${ADMIN_BASE_URL}/api/dashboard/`,
10101010

10111011
app.get(`${ADMIN_BASE_URL}/api/aparts-by-room-percentages/`,
10121012
admin.express.authorize(
1013-
async (req, res) => {
1013+
async (req: IAdminUserExpressRequest, res: express.Response) => {
10141014
const roomPercentages = await admin.resource('aparts').dataConnector.client.prepare(
10151015
`SELECT
10161016
number_of_rooms,

0 commit comments

Comments
 (0)