mirror of
https://github.com/directus/directus.git
synced 2026-01-29 04:28:04 -05:00
Improve types
This commit is contained in:
committed by
rijkvanzanten
parent
bfb2df432e
commit
ee6aaffe92
@@ -31,11 +31,14 @@ export class ActivityHandler {
|
||||
const response = await this.axios.post('/activity/comments', payload);
|
||||
return response.data;
|
||||
},
|
||||
update: async (key: PrimaryKey, payload: { comment: string }) => {
|
||||
update: async (
|
||||
key: PrimaryKey,
|
||||
payload: { comment: string }
|
||||
): Promise<{ data: Item | null }> => {
|
||||
const response = await this.axios.patch(`/activity/comments/${key}`, payload);
|
||||
return response.data;
|
||||
},
|
||||
delete: async (key: PrimaryKey) => {
|
||||
delete: async (key: PrimaryKey): Promise<void> => {
|
||||
await this.axios.delete(`/activity/comments/${key}`);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -13,6 +13,12 @@ export type AuthOptions = {
|
||||
storage: AuthStorage;
|
||||
};
|
||||
|
||||
export type AuthResponse = {
|
||||
access_token: string;
|
||||
expires: number;
|
||||
refresh_token?: string;
|
||||
};
|
||||
|
||||
export class AuthHandler {
|
||||
private axios: AxiosInstance;
|
||||
private storage: AuthStorage;
|
||||
@@ -30,7 +36,7 @@ export class AuthHandler {
|
||||
}
|
||||
}
|
||||
|
||||
get token() {
|
||||
get token(): string | null {
|
||||
return this.axios.defaults.headers?.Authorization?.split(' ')[1] || null;
|
||||
}
|
||||
|
||||
@@ -41,8 +47,11 @@ export class AuthHandler {
|
||||
};
|
||||
}
|
||||
|
||||
async login(credentials: LoginCredentials) {
|
||||
const response = await this.axios.post('/auth/login', { ...credentials, mode: this.mode });
|
||||
async login(credentials: LoginCredentials): Promise<{ data: AuthResponse }> {
|
||||
const response = await this.axios.post<{ data: AuthResponse }>('/auth/login', {
|
||||
...credentials,
|
||||
mode: this.mode,
|
||||
});
|
||||
|
||||
this.token = response.data.data.access_token;
|
||||
|
||||
@@ -57,7 +66,7 @@ export class AuthHandler {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async refresh() {
|
||||
async refresh(): Promise<{ data: AuthResponse }> {
|
||||
const payload: Record<string, any> = { mode: this.mode };
|
||||
|
||||
if (this.mode === 'json') {
|
||||
@@ -65,7 +74,7 @@ export class AuthHandler {
|
||||
payload['refresh_token'] = refreshToken;
|
||||
}
|
||||
|
||||
const response = await this.axios.post('/auth/refresh', payload);
|
||||
const response = await this.axios.post<{ data: AuthResponse }>('/auth/refresh', payload);
|
||||
|
||||
this.token = response.data.data.access_token;
|
||||
|
||||
@@ -80,17 +89,17 @@ export class AuthHandler {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async logout() {
|
||||
async logout(): Promise<void> {
|
||||
await this.axios.post('/auth/logout');
|
||||
this.token = null;
|
||||
}
|
||||
|
||||
password = {
|
||||
request: async (email: string) => {
|
||||
request: async (email: string): Promise<void> => {
|
||||
await this.axios.post('/auth/password/request', { email });
|
||||
},
|
||||
|
||||
reset: async (token: string, password: string) => {
|
||||
reset: async (token: string, password: string): Promise<void> => {
|
||||
await this.axios.post('/auth/password/reset', { token, password });
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,5 +1,30 @@
|
||||
import { AxiosInstance } from 'axios';
|
||||
|
||||
export type ServerInfo = {
|
||||
project: {
|
||||
project_name: string;
|
||||
project_logo: string | null;
|
||||
project_color: string | null;
|
||||
public_foreground: string | null;
|
||||
public_background: string | null;
|
||||
public_note: string | null;
|
||||
custom_css: null;
|
||||
};
|
||||
};
|
||||
|
||||
export type OasData = {
|
||||
openapi: string;
|
||||
info: {
|
||||
title: string;
|
||||
description: string;
|
||||
versions: string;
|
||||
};
|
||||
servers: { url: string; description: string }[];
|
||||
tags: { name: string; description: string }[];
|
||||
paths: Record<string, any>;
|
||||
components: Record<string, any>;
|
||||
};
|
||||
|
||||
export class ServerHandler {
|
||||
private axios: AxiosInstance;
|
||||
|
||||
@@ -8,18 +33,18 @@ export class ServerHandler {
|
||||
}
|
||||
|
||||
specs = {
|
||||
oas: async () => {
|
||||
oas: async (): Promise<OasData> => {
|
||||
const result = await this.axios.get('/server/specs/oas');
|
||||
return result.data;
|
||||
},
|
||||
};
|
||||
|
||||
async ping() {
|
||||
async ping(): Promise<'pong'> {
|
||||
await this.axios.get('/server/ping');
|
||||
return 'pong';
|
||||
}
|
||||
|
||||
async info() {
|
||||
async info(): Promise<{ data: ServerInfo }> {
|
||||
const result = await this.axios.get('/server/info');
|
||||
return result.data;
|
||||
}
|
||||
|
||||
@@ -2,34 +2,55 @@ import { AxiosInstance } from 'axios';
|
||||
import { ItemsHandler } from './items';
|
||||
import { Query, Payload } from '../types';
|
||||
|
||||
export type UserInfo = {
|
||||
avatar: string | null;
|
||||
description: string | null;
|
||||
email: string;
|
||||
first_name: string | null;
|
||||
id: string;
|
||||
language: string;
|
||||
last_access: string;
|
||||
last_name: string | null;
|
||||
last_page: string;
|
||||
location: string | null;
|
||||
password: string;
|
||||
role: string;
|
||||
status: string;
|
||||
tags: string[];
|
||||
tfa_secret: string | null;
|
||||
theme: 'auto' | 'dark' | 'light';
|
||||
title: string | null;
|
||||
token: string | null;
|
||||
};
|
||||
|
||||
export class UsersHandler extends ItemsHandler {
|
||||
constructor(axios: AxiosInstance) {
|
||||
super('directus_users', axios);
|
||||
}
|
||||
|
||||
async invite(email: string | string[], role: string) {
|
||||
async invite(email: string | string[], role: string): Promise<void> {
|
||||
await this.axios.post('/users/invite', { email, role });
|
||||
}
|
||||
|
||||
async acceptInvite(token: string, password: string) {
|
||||
async acceptInvite(token: string, password: string): Promise<void> {
|
||||
await this.axios.post('/users/invite/accept', { token, password });
|
||||
}
|
||||
|
||||
tfa = {
|
||||
enable: async (password: string) => {
|
||||
enable: async (password: string): Promise<void> => {
|
||||
await this.axios.post('/users/tfa/enable', { password });
|
||||
},
|
||||
disable: async (otp: string) => {
|
||||
disable: async (otp: string): Promise<void> => {
|
||||
await this.axios.post('/users/tfa/disable', { otp });
|
||||
},
|
||||
};
|
||||
|
||||
me = {
|
||||
read: async (query?: Query) => {
|
||||
read: async (query?: Query): Promise<{ data: UserInfo }> => {
|
||||
const response = await this.axios.get('/users/me', { params: query });
|
||||
return response.data;
|
||||
},
|
||||
update: async (payload: Payload, query?: Query) => {
|
||||
update: async (payload: Payload, query?: Query): Promise<{ data: UserInfo }> => {
|
||||
const response = await this.axios.patch('/users/me', payload, { params: query });
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -9,28 +9,28 @@ export class UtilsHandler {
|
||||
}
|
||||
|
||||
random = {
|
||||
string: async (length: number = 32) => {
|
||||
string: async (length: number = 32): Promise<{ data: string }> => {
|
||||
const result = await this.axios.get('/utils/random/string', { params: { length } });
|
||||
return result.data;
|
||||
},
|
||||
};
|
||||
|
||||
hash = {
|
||||
generate: async (string: string) => {
|
||||
generate: async (string: string): Promise<{ data: string }> => {
|
||||
const result = await this.axios.post('/utils/hash/generate', { string });
|
||||
return result.data;
|
||||
},
|
||||
verify: async (string: string, hash: string) => {
|
||||
verify: async (string: string, hash: string): Promise<{ data: boolean }> => {
|
||||
const result = await this.axios.post('/utils/hash/verify', { string, hash });
|
||||
return result.data;
|
||||
},
|
||||
};
|
||||
|
||||
async sort(collection: string, item: PrimaryKey, to: PrimaryKey) {
|
||||
async sort(collection: string, item: PrimaryKey, to: PrimaryKey): Promise<void> {
|
||||
await this.axios.post(`/utils/sort/${collection}`, { item, to });
|
||||
}
|
||||
|
||||
async revert(revision: PrimaryKey) {
|
||||
async revert(revision: PrimaryKey): Promise<void> {
|
||||
await this.axios.post(`/utils/revert/${revision}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ class DirectusSDK {
|
||||
// Global helpers
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
get url() {
|
||||
get url(): string {
|
||||
return this.axios.defaults.baseURL!;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ import { AuthStorage } from '../types';
|
||||
export class MemoryStore implements AuthStorage {
|
||||
private values: Record<string, any> = {};
|
||||
|
||||
async getItem(key: string) {
|
||||
async getItem<T = any>(key: string): Promise<T | undefined> {
|
||||
return this.values[key];
|
||||
}
|
||||
|
||||
async setItem(key: string, value: any) {
|
||||
async setItem(key: string, value: any): Promise<void> {
|
||||
this.values[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user