diff --git a/README.md b/README.md index 124da60..0e6802b 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,34 @@ const directus = createDirectus( })(); ``` +### Usage in Directus extensions + +```typescript +import {defineEndpoint} from '@directus/extensions-sdk'; +import type {CollectionName} from "../models"; +import type {ItemsService} from "@directus/api/dist/services"; +import {ItemIn} from "directus-app/models"; + +export default defineEndpoint((router, {services}) => { + /** + * You probably want to move this utility to a place more suitable + * for your extension + */ + function items(collectionName: C): ItemsService> { + return new services.ItemsService(collectionName); + } + + router.get('/', async (_req, res) => { + const settings = await items('directus_settings') + .readSingleton({}); + + res.json({ + name: settings.project_name + }); + }); +}); +``` + ## Note: Geometry Support I am still working on support for the geometry types. diff --git a/src/index.ts b/src/index.ts index 3426afd..ba2a67c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -196,10 +196,14 @@ export default defineHook(async ({init}, {services, getSchema, database, logger} source += generateIndex(collections); source += ` -type ItemType = + +type CollectionName = keyof Collections; + +type ItemIn = Collections[CollectionKey] extends (infer Item)[] - ? Item - : Collections[CollectionKey]; + ? Exclude + : Collections[CollectionKey] + ` await writeFile(file, source); process.exit(0);