mirror of
https://github.com/directus/directus.git
synced 2026-01-23 04:08:14 -05:00
Provide getSchema function in hook/endpoint registration function
Fixes #4621
This commit is contained in:
@@ -7,6 +7,7 @@ import emitter from './emitter';
|
||||
import logger from './logger';
|
||||
import { HookRegisterFunction, EndpointRegisterFunction } from './types';
|
||||
import { ensureDir } from 'fs-extra';
|
||||
import { getSchema } from './utils/get-schema';
|
||||
|
||||
import * as exceptions from './exceptions';
|
||||
import * as services from './services';
|
||||
@@ -93,7 +94,7 @@ function registerHooks(hooks: string[]) {
|
||||
}
|
||||
}
|
||||
|
||||
let events = register({ services, exceptions, env, database });
|
||||
let events = register({ services, exceptions, env, database, getSchema });
|
||||
for (const [event, handler] of Object.entries(events)) {
|
||||
emitter.on(event, handler);
|
||||
}
|
||||
@@ -126,6 +127,6 @@ function registerEndpoints(endpoints: string[], router: Router) {
|
||||
const scopedRouter = express.Router();
|
||||
router.use(`/${endpoint}/`, scopedRouter);
|
||||
|
||||
register(scopedRouter, { services, exceptions, env, database });
|
||||
register(scopedRouter, { services, exceptions, env, database, getSchema });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,14 @@ import * as exceptions from '../exceptions';
|
||||
import env from '../env';
|
||||
import { Knex } from 'knex';
|
||||
import { Router } from 'express';
|
||||
import { getSchema } from '../utils/get-schema';
|
||||
|
||||
export type ExtensionContext = {
|
||||
services: typeof services;
|
||||
exceptions: typeof exceptions;
|
||||
database: Knex;
|
||||
env: typeof env;
|
||||
getSchema: typeof getSchema;
|
||||
};
|
||||
|
||||
export type HookRegisterFunction = (context: ExtensionContext) => Record<string, ListenerFn>;
|
||||
|
||||
@@ -38,6 +38,7 @@ that is scoped to `/custom/<extension-name>`, while `context` holds the followin
|
||||
- `services` — All API internal services.
|
||||
- `exceptions` — API exception objects that can be used to throw "proper" errors.
|
||||
- `database` — Knex instance that is connected to the current database.
|
||||
- `getSchema` — Async function that reads the full available schema for use in services
|
||||
- `env` — Parsed environment variables.
|
||||
|
||||
## 3. Restart the API
|
||||
|
||||
@@ -126,6 +126,7 @@ The `registerHook` function receives a context parameter with the following prop
|
||||
- `services` — All API internal services
|
||||
- `exceptions` — API exception objects that can be used for throwing "proper" errors
|
||||
- `database` — Knex instance that is connected to the current database
|
||||
- `getSchema` — Async function that reads the full available schema for use in services
|
||||
- `env` — Parsed environment variables
|
||||
|
||||
### Event Handler Function
|
||||
|
||||
Reference in New Issue
Block a user