Skip to content

Instantly share code, notes, and snippets.

@Fusseldieb
Last active April 6, 2022 17:15
Show Gist options
  • Save Fusseldieb/c7a8febf6acda689f1fdb4b3724a3f88 to your computer and use it in GitHub Desktop.
Save Fusseldieb/c7a8febf6acda689f1fdb4b3724a3f88 to your computer and use it in GitHub Desktop.

Revisions

  1. Fusseldieb revised this gist Apr 6, 2022. No changes.
  2. Fusseldieb revised this gist Apr 6, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion custom_endpoint_1_directusv9.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ module.exports = function registerEndpoint(router, { services, exceptions, datab
    const { ServiceUnavailableException } = exceptions;

    // Searches for the role named "Homepage" and gets its UUID for the accountability authentication
    // This is made that way to ease portability since there can be different UUUIDs on dev and prod server
    // This is made that way to ease portability since there can be different UUIDs on dev and prod server
    const role_res = await database("directus_roles").select("id").where({ name: "Homepage" });
    if (role_res.length === 0) {
    return next(new ServiceUnavailableException("Role doesn't exist"));
  3. Fusseldieb created this gist Apr 6, 2022.
    40 changes: 40 additions & 0 deletions custom_endpoint_1_directusv9.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    // Snippet for Directus' custom endpoint (tested on v9.8.0)
    // This snippet checks if a role named "Homepage" is present and authenticates with it to execute custom logic
    // In this case, it returns *all* items inside "products" where "visible_on_homepage" is set to true.

    const { getPermissions } = require('directus/utils/get-permissions');
    const { getSchema } = require('directus/utils/get-schema');

    module.exports = function registerEndpoint(router, { services, exceptions, database }) {
    router.get('/products', async (req, res, next) => {
    const { ItemsService } = services;
    const { ServiceUnavailableException } = exceptions;

    // Searches for the role named "Homepage" and gets its UUID for the accountability authentication
    // This is made that way to ease portability since there can be different UUUIDs on dev and prod server
    const role_res = await database("directus_roles").select("id").where({ name: "Homepage" });
    if (role_res.length === 0) {
    return next(new ServiceUnavailableException("Role doesn't exist"));
    }

    const accountability = {
    role: role_res[0].id,
    admin: false,
    app: false
    };

    const schema = await getSchema(accountability);
    accountability.permissions = await getPermissions(accountability, req.schema);

    const ProductsService = new ItemsService('products', { schema, accountability: accountability });
    ProductsService
    .readByQuery({ fields: ['model'], filter: { visible_on_homepage: true }, limit: -1 })
    .then((results) => {
    res.json(results);
    })
    .catch((error) => {
    return next(new ServiceUnavailableException(error.message));
    });
    });

    };