Skip to content

Instantly share code, notes, and snippets.

@andyvanee
Last active December 21, 2021 21:56
Show Gist options
  • Save andyvanee/395990956854045f6ae050cba450d9a5 to your computer and use it in GitHub Desktop.
Save andyvanee/395990956854045f6ae050cba450d9a5 to your computer and use it in GitHub Desktop.
Directus http endpoint integration

The documentation on Directus 9 extensions is scarce. This is a slightly more detailed implementation than the example given.

Notes:

  • router is an express.Router object
  • services is a bunch of getters of available services (see below)
  • database is the knex instance
  • directus.auth.refresh() will throw. It's assumed that the user has authenticated in before calling fetchJson

https://github.com/directus/directus/tree/main/api/src/services

services: {
  ItemsService: [Getter],
  ActivityService: [Getter],
  AssetsService: [Getter],
  AuthenticationService: [Getter],
  CollectionsService: [Getter],
  DashboardsService: [Getter],
  FieldsService: [Getter],
  FilesService: [Getter],
  FoldersService: [Getter],
  GraphQLGeoJSON: [Getter],
  GraphQLDate: [Getter],
  GraphQLService: [Getter],
  ImportService: [Getter],
  MailService: [Getter],
  MetaService: [Getter],
  NotificationsService: [Getter],
  PanelsService: [Getter],
  PayloadService: [Getter],
  PermissionsService: [Getter],
  PresetsService: [Getter],
  RelationsService: [Getter],
  RevisionsService: [Getter],
  RolesService: [Getter],
  ServerService: [Getter],
  SettingsService: [Getter],
  SpecificationService: [Getter],
  TFAService: [Getter],
  UsersService: [Getter],
  UtilsService: [Getter],
  WebhooksService: [Getter]
}

References:

https://github.com/directus/directus/blob/main/api/src/services/users.ts

https://github.com/directus/directus/blob/main/api/src/services/items.ts

https://github.com/directus/directus/blob/ee3e9b59f908dc74691ed8c1047621b42d33d9ab/api/src/extensions.ts#L308

// directus client setup ...
const fetchJson = async (path, options={}) => {
try {
await directus.auth.refresh()
const url = new URL(path, directus.url)
options.headers = options.headers || {}
const { token } = directus.auth
options.headers["Authorization"] = `Bearer ${token}`
const res = await fetch(url, options)
if (!res.status == 200) {
throw new Error(`Invalid status code ${res.status}`)
}
const contentType = res.headers.get("content-type")
if (!/^application\/json/.test(contentType)) {
throw new Error(`Invalid return content type ${contentType}`)
}
return await res.json()
} catch (error) {
return { error }
}
}
fetchJson('/my-endpoint/accounts', {method: "GET"}).then(response => {
console.log({response})
}).catch(error => {
console.log({error})
})
export default (router, { services, logger, database, env }) => {
router.get("/accounts", async (req, res) => {
try {
const { accountability, schema } = req
const { user, admin, app } = accountability
const userService = new services.UsersService({
accountability,
schema
})
const users = await userService.readByQuery({ limit: 10 })
return res.json({ status: "ok", users, user, admin, app })
} catch (error) {
return res.json({error: `${error}`})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment