Skip to content

Commit

Permalink
Add readme for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChappIO committed Aug 18, 2023
1 parent 677e380 commit cc971db
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ const directus = createDirectus<Collections>(
})();
```

### 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<C extends CollectionName>(collectionName: C): ItemsService<ItemIn<C>> {
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.
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,14 @@ export default defineHook(async ({init}, {services, getSchema, database, logger}
source += generateIndex(collections);

source += `
type ItemType<CollectionKey extends keyof Collections> =
type CollectionName = keyof Collections;
type ItemIn<CollectionKey extends CollectionName> =
Collections[CollectionKey] extends (infer Item)[]
? Item
: Collections[CollectionKey];
? Exclude<Item, unknown>
: Collections[CollectionKey]
`
await writeFile(file, source);
process.exit(0);
Expand Down

0 comments on commit cc971db

Please sign in to comment.