Add server and utils

This commit is contained in:
rijkvanzanten
2020-10-21 15:57:46 +02:00
parent be7d544796
commit cb5c7eae68
7 changed files with 165 additions and 19 deletions

View File

@@ -4,9 +4,7 @@
## To-Do
- [ ] Docs
- [ ] Items
- [x] Items
- [x] Create
- [x] Read
- [x] Update
@@ -76,11 +74,11 @@
- [ ] Read
- [ ] Update
- [ ] Delete
- [ ] Server
- [ ] Specs
- [ ] OAS
- [ ] Ping
- [ ] Info
- [x] Server
- [x] Specs
- [x] OAS
- [x] Ping
- [x] Info
- [ ] Settings
- [ ] Read
- [ ] Update
@@ -93,12 +91,12 @@
- [ ] Accept Invite
- [ ] Enable TFA
- [ ] Disable TFA
- [ ] Utils
- [ ] Get random string
- [ ] Hash a value
- [ ] Verify a hashed value
- [ ] Sort in collection
- [ ] Revert revision
- [x] Utils
- [x] Get random string
- [x] Hash a value
- [x] Verify a hashed value
- [x] Sort in collection
- [x] Revert revision
## Installation
@@ -262,3 +260,75 @@ directus.items('articles').delete(15);
// Delete multiple items
directus.items('articles').delete([15, 42]);
```
---
Bunch of others go here
---
### Server
#### Specs
##### OAS
```js
// Get the OAS specs for the current API instance
directus.server.specs.oas();
```
#### Ping
```js
directus.server.ping();
```
#### Info
```js
directus.server.info();
```
---
Settings
Users
---
### Utils
#### Random
```js
// Get a random string (w/ optional length)
directus.utils.random.string();
directus.utils.random.string(42);
```
#### Hash
```js
// Generate a hash for a string
directus.utils.hash.generate('Hello World');
// Verify if a hash is valid
directus.utils.hash.verify('$argon2.hash', 'Hello World');
```
#### Sort
```js
// Re-sort a collection based on a start / end position
directus.utils.sort('articles', 15, 42);
```
#### Revert
```js
// Revert a given revision
directus.utils.revert(13);
```

View File

@@ -0,0 +1,3 @@
export * from './items';
export * from './server';
export * from './utils';

View File

@@ -1,9 +1,9 @@
import { Query, Item, Payload, Response } from '../types';
import { AxiosInstance } from 'axios';
export class Items {
collection: string;
axios: AxiosInstance;
export class ItemsHandler {
private collection: string;
private axios: AxiosInstance;
constructor(collection: string, axios: AxiosInstance) {
this.collection = collection;

View File

@@ -0,0 +1,26 @@
import { AxiosInstance } from 'axios';
export class ServerHandler {
private axios: AxiosInstance;
constructor(axios: AxiosInstance) {
this.axios = axios;
}
specs = {
oas: async () => {
const result = await this.axios.get('/server/specs/oas');
return result.data;
},
};
async ping() {
await this.axios.get('/server/ping');
return 'pong';
}
async info() {
const result = await this.axios.get('/server/info');
return result.data;
}
}

View File

@@ -0,0 +1,36 @@
import { AxiosInstance } from 'axios';
import { PrimaryKey } from '../types';
export class UtilsHandler {
private axios: AxiosInstance;
constructor(axios: AxiosInstance) {
this.axios = axios;
}
random = {
string: async (length: number = 32) => {
const result = await this.axios.get('/utils/random/string', { params: { length } });
return result.data;
},
};
hash = {
generate: async (string: string) => {
const result = await this.axios.post('/utils/hash/generate', { string });
return result.data;
},
verify: async (string: string, hash: string) => {
const result = await this.axios.post('/utils/hash/generate', { string, hash });
return result.data;
},
};
async sort(collection: string, item: PrimaryKey, to: PrimaryKey) {
await this.axios.post(`/utils/sort/${collection}`, { item, to });
}
async revert(revision: PrimaryKey) {
await this.axios.post(`/utils/revert/${revision}`);
}
}

View File

@@ -1,5 +1,5 @@
import axios, { AxiosInstance } from 'axios';
import { Items } from './handlers/items';
import { ItemsHandler, ServerHandler, UtilsHandler } from './handlers';
export default class DirectusSDK {
axios: AxiosInstance;
@@ -19,6 +19,16 @@ export default class DirectusSDK {
}
items(collection: string) {
return new Items(collection, this.axios);
return new ItemsHandler(collection, this.axios);
}
get server() {
return new ServerHandler(this.axios);
}
get utils() {
return new UtilsHandler(this.axios);
}
}
const directus = new DirectusSDK('https://example.com');

View File

@@ -1,5 +1,6 @@
export type Item = Record<string, any>;
export type Payload = Record<string, any>;
export type PrimaryKey = string | number;
export enum Meta {
TOTAL_COUNT = 'total_count',