Provide getSchema function in hook/endpoint registration function

Fixes #4621
This commit is contained in:
rijkvanzanten
2021-03-22 20:13:49 -04:00
parent 71c8c4cc2c
commit 3eaa375952
4 changed files with 7 additions and 2 deletions

View File

@@ -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 });
}
}

View File

@@ -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>;

View File

@@ -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

View File

@@ -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