From 5771b0a72c07b4f7b22d5dda3f1e00c9058b9a8c Mon Sep 17 00:00:00 2001 From: Conner Bachmann Date: Thu, 6 Oct 2022 20:49:23 +0200 Subject: [PATCH] feat(module): implemented support for revisions --- src/runtime/composables/useDirectusAuth.ts | 7 ++-- .../composables/useDirectusRevisions.ts | 41 +++++++++++++++++++ src/runtime/types/index.d.ts | 10 +++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/runtime/composables/useDirectusRevisions.ts diff --git a/src/runtime/composables/useDirectusAuth.ts b/src/runtime/composables/useDirectusAuth.ts index 5c882508..a4acc3c2 100644 --- a/src/runtime/composables/useDirectusAuth.ts +++ b/src/runtime/composables/useDirectusAuth.ts @@ -42,13 +42,14 @@ export const useDirectusAuth = () => { ) } if (config.directus.fetchUserParams) { - var res = await directus<{ data: DirectusUser }>('/users/me', { + const res = await directus<{ data: DirectusUser }>('/users/me', { params: config.directus.fetchUserParams }) + setUser(res.data) } else { - var res = await directus<{ data: DirectusUser }>('/users/me') + const res = await directus<{ data: DirectusUser }>('/users/me') + setUser(res.data) } - setUser(res.data) } catch (e) { setToken(null) } diff --git a/src/runtime/composables/useDirectusRevisions.ts b/src/runtime/composables/useDirectusRevisions.ts new file mode 100644 index 00000000..7836582a --- /dev/null +++ b/src/runtime/composables/useDirectusRevisions.ts @@ -0,0 +1,41 @@ +import { DirectusQueryParams, DirectusRevision } from '../types' +import { useDirectus } from './useDirectus' + +export const useDirectusRevisions = () => { + const directus = useDirectus() + + const getRevisions = async (data: { + params: DirectusQueryParams; + }): Promise => { + if (data.params?.filter) { + (data.params.filter as unknown) = JSON.stringify(data.params.filter) + } + if (data.params?.deep) { + (data.params.deep as unknown) = JSON.stringify(data.params.deep) + } + const revisions = await directus<{ data: T[] }>('/revisions', { + method: 'GET', + params: data.params + }) + return revisions.data + } + + const getRevisionById = async (data: { + id: number; + params: DirectusQueryParams; + }): Promise => { + const revisions = await directus<{ data: T[] }>( + `/revisions/${data.id}`, + { + method: 'GET', + params: data.params + } + ) + return revisions.data + } + + return { + getRevisionById, + getRevisions + } +} diff --git a/src/runtime/types/index.d.ts b/src/runtime/types/index.d.ts index a6de714f..243d78c4 100644 --- a/src/runtime/types/index.d.ts +++ b/src/runtime/types/index.d.ts @@ -134,3 +134,13 @@ export interface DirectusCollectionUpdate { collection: string; meta: DirectusCollectionMeta; } + +export interface DirectusRevision { + id: number; + activity?: any; + collection: string; + item?: string; + data?: Object; + delta?: Object; + parent?: any; +}