npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tanglemedia/svelte-starter-directus-api

v1.0.1

Published

directus API wrapper for all the directus sdk functionality

Downloads

126

Readme

Directus-api

This package is experimental and is meant for internal use only.

Application

This is the entry point for initialing the application.

// boot/index.ts
import { createApplication } from '@tanglemedia/svelte-starter-core';
import { registerDirectusProvider } from '@tanglemedia/svelte-starter-directus-api';

const configuredApp = createApplication({
  provider: {
    // This register the directus provider, i.e, creates a directus service
    register: [registerDirectusProvider()]
  }
});

export default configuredApp;

export const { configureService, getConfiguration, application, provideConfig } = configuredApp;

Configuration file

This is the entry point for your Directus adapter configs, inside the api.yml file.

# This defines your default adapter
default: 'directus'

adapters:
  # These are each custom adapters you can set, the key name can be what ever you want
  directus-client:
    adapter: 'directus'
    configuration:
      host: $PUBLIC_DIRECTUS_HOST
      protocol: $PUBLIC_DIRECTUS_PROTOCOL
      auth:
        mode: 'json'
        config:
          storage: 'localStorage'
          autoRefresh: true
          msRefreshBeforeExpires: 30000
          credentials: 'include'

  directus-auth:
    adapter: 'directus'
    configuration:
      host: $PUBLIC_DIRECTUS_HOST
      protocol: $PUBLIC_DIRECTUS_PROTOCOL
      auth:
        mode: 'json'
        config:
          storage: 'localStorage'
          autoRefresh: true
          msRefreshBeforeExpires: 30000
          credentials: 'include'

  directus-static:
    adapter: 'directus'
    configuration:
      host: $PUBLIC_DIRECTUS_HOST
      protocol: $PUBLIC_DIRECTUS_PROTOCOL
      staticToken: $PUBLIC_DIRECTUS_ACCESS_TOKEN

Services

Services is a simple abstraction layer that makes some assumptions around common api requests and responses. Sevices rely on an underlying adapter which is responsible for transforming the request/response payloads and handling the actual requests to an api. Once you have that you can create different services to handle different collections of your Directus project, here I am going to show three different services examples:

// DIRECTUS AUTH SERVICE
import { configureService } from '../../boot';
import { DirectusAuthServices } from '@tanglemedia/svelte-starter-directus-api';
import { createQuery, type Query, type QueryObserverOptions } from '@tanstack/svelte-query';

interface FilesInterface {
  id: string;
}

const authService = configureService(DirectusAuthServices, {
  path: '',
  transform: (data) => data,
  adapterKey: 'directus-auth'
});

// This is a specific service to have access to the base api functions, like find, create, update... that you get from the Core package, but also you get specific Auth functions like:
await authService.getToken();
// or
await authService.logout();
// or
await authService.getCurrentUser();
// or
await authService.updateCurrentUser();
// or
await authService.login(formValues.email, formValues.password);
// or
await authService.refresh();
// or
await authService.readFile(file);
// DIRECTUS STATIC SERVICE
import { configureService } from '../../boot';
import { DirectusStaticServices } from '@tanglemedia/svelte-starter-directus-api';

const staticService = configureService(DirectusStaticServices, {
  path: '',
  transform: (data) => data,
  adapterKey: 'directus-static'
});

// This is a specific service to have access to the base api functions, like find, create, update... that you get from the Core package, but also you get specific Static functions that you need the static token from Directus:
await authService.createUser(userData);
// DIRECTUS CLIENT SERVICE
import type { FormInterface, FormResultInterface } from '$lib/models/form';
import { createQuery, type Query, type QueryObserverOptions } from '@tanstack/svelte-query';
import { configureService } from '../../boot';
import { ServiceAbstract } from '@tanglemedia/svelte-starter-core';

class Forms extends ServiceAbstract {}
class FormResults extends ServiceAbstract {}

export type FormsQuery = Query<FormInterface>;
export type FormResultsQuery = Query<FormResultInterface>;

type CreateOpt = {
  enabled?: QueryObserverOptions['enabled'];
};

export const formsServiceClient = configureService<FormInterface>(Forms, {
  // path is the name of your collection in Directus
  path: 'forms',
  transform: (data) => data,
  adapterKey: 'directus-client'
});

export default formsServiceClient;

// This is a specific service to have access to the base api functions, like find, create, update... that you get from the Core package, example:
form = await formsServiceClient
  .findOne(params.id, { fields: ['name', 'description'] })
  .then((res) => res.data);

Breaking Changes

Migrating from 0.1.4 to 1.0.0

On this migration we removed completely some deprecated Service functions:

  1. ApiAuthDirectus
  2. ApiClientDirectus
  3. LoadDirectusApiProvider
  4. ApiStaticDirectus

These types of initializing Auth, Static, and Client Directus services won't work on new major version:

// LEGACY WAY - NOW DEPRECATED AND NOT SUPPORTED
import { ApiAuthDirectus } from '@tanglemedia/svelte-starter-directus-api';
import { getConfiguration } from '../boot';

let directus: ApiAuthDirectus;

const configApi = async () => {
  const config = await getConfiguration();
  const protocol: string = config.config(`api.adapters.directus.protocol`) || '';
  const host: string = config.config(`api.adapters.directus.host`) || '';
  const direcutsUrl = `${protocol}://${host}`.trim();
  directus = new ApiAuthDirectus(direcutsUrl, 'json');
};

async function getDirectus() {
  if (!directus) {
    await configApi();
  }
  return directus;
}

export { getDirectus };

// CURRENT WAY
import { configureService } from '../../boot';
import { DirectusAuthServices } from '@tanglemedia/svelte-starter-directus-api';
import { createQuery, type Query, type QueryObserverOptions } from '@tanstack/svelte-query';

interface FilesInterface {
  id: string;
}

const authService = configureService(DirectusAuthServices, {
  path: '',
  transform: (data) => data,
  adapterKey: 'directus-auth'
});

export default authService;
// LEGACY WAY - NOW DEPRECATED AND NOT SUPPORTED
import { getConfiguration } from '../boot';
import { LoadDirectusApiProvider } from '@tanglemedia/svelte-starter-directus-api';

const configApi = async () => {
  const res = await getConfiguration();
  return res;
};

const apiProvider = new LoadDirectusApiProvider(configApi, 'json');

export { apiProvider };

// CURRENT WAY
import type { FormInterface, FormResultInterface } from '$lib/models/form';
import { createQuery, type Query, type QueryObserverOptions } from '@tanstack/svelte-query';
import { configureService } from '../../boot';
import { ServiceAbstract } from '@tanglemedia/svelte-starter-core';

class Forms extends ServiceAbstract {}
class FormResults extends ServiceAbstract {}

export type FormsQuery = Query<FormInterface>;
export type FormResultsQuery = Query<FormResultInterface>;

type CreateOpt = {
  enabled?: QueryObserverOptions['enabled'];
};

export const formsServiceClient = configureService<FormInterface>(Forms, {
  path: 'forms',
  transform: (data) => data,
  adapterKey: 'directus-client'
});