mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Start on reference / to-do
This commit is contained in:
@@ -9,7 +9,6 @@ import { Transformation } from '../types/assets';
|
||||
import storage from '../storage';
|
||||
import { PayloadService, AssetsService } from '../services';
|
||||
import useCollection from '../middleware/use-collection';
|
||||
import { respond } from '../middleware/respond';
|
||||
|
||||
const router = Router();
|
||||
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
],
|
||||
"author": "Rijk van Zanten <rijkvanzanten@me.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^0.19.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^4.0.3"
|
||||
}
|
||||
|
||||
@@ -1,3 +1,242 @@
|
||||
# Directus JS SDK
|
||||
|
||||
[WIP] v9
|
||||
|
||||
## To-Do
|
||||
|
||||
- [ ] Docs
|
||||
|
||||
- [ ] Items
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Activity
|
||||
- [ ] Read
|
||||
- [ ] Create Comment
|
||||
- [ ] Update Comment
|
||||
- [ ] Delete Comment
|
||||
- [ ] Assets??? (not sure if needed)
|
||||
- [ ] Read
|
||||
- [ ] Auth
|
||||
- [ ] Login
|
||||
- [ ] Refresh
|
||||
- [ ] Logout
|
||||
- [ ] Request Password Reset
|
||||
- [ ] Reset Password
|
||||
- [ ] oAuth
|
||||
- [ ] Get available providers
|
||||
- [ ] Login with provider
|
||||
- [ ] Collections
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Extensions??? (not sure if needed)
|
||||
- [ ] Read
|
||||
- [ ] Fields
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Files
|
||||
- [ ] Create (upload)
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Folders
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] GraphQL
|
||||
- [ ] Run
|
||||
- [ ] Permissions
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Presets
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Relations
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Revisions
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Roles
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Server
|
||||
- [ ] Specs
|
||||
- [ ] OAS
|
||||
- [ ] Ping
|
||||
- [ ] Info
|
||||
- [ ] Settings
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Users
|
||||
- [ ] Create
|
||||
- [ ] Read
|
||||
- [ ] Update
|
||||
- [ ] Delete
|
||||
- [ ] Invite
|
||||
- [ ] Accept Invite
|
||||
- [ ] Enable TFA
|
||||
- [ ] Disable TFA
|
||||
- [ ] Utils
|
||||
- [ ] Get random string
|
||||
- [ ] Hash a value
|
||||
- [ ] Verify a hashed value
|
||||
- [ ] Sort in collection
|
||||
- [ ] Revert revision
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm install @directus/sdk-js
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import DirectusSDK from '@directus/sdk-js';
|
||||
|
||||
const directus = new DirectusSDK('https://api.example.com/');
|
||||
|
||||
directus.items('articles').read(15);
|
||||
```
|
||||
|
||||
## Docs
|
||||
|
||||
**NOTE** All methods return promises. Make sure to await methods, for example:
|
||||
|
||||
```js
|
||||
import DirectusSDK from '@directus/sdk-js';
|
||||
|
||||
const directus = new DirectusSDK('https://api.example.com/');
|
||||
|
||||
async function getData() {
|
||||
await directus.auth.login({ email: 'admin@example.com', password: 'password' });
|
||||
return await directus.items('articles').read();
|
||||
}
|
||||
```
|
||||
|
||||
### Global SDK
|
||||
|
||||
#### Initialize
|
||||
|
||||
```js
|
||||
import DirectusSDK from '@directus/sdk-js';
|
||||
|
||||
const directus = new DirectusSDK('https://api.example.com/');
|
||||
```
|
||||
|
||||
#### Get / set API URL
|
||||
|
||||
```js
|
||||
// Get the used API base URL
|
||||
console.log(directus.url);
|
||||
// => https://api.example.com/
|
||||
|
||||
// Set the API base URL
|
||||
directus.url = 'https://api2.example.com';
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Items
|
||||
|
||||
#### Create
|
||||
|
||||
```js
|
||||
// Create an item
|
||||
directus.items('articles').create({
|
||||
title: 'My New Article'
|
||||
});
|
||||
|
||||
// Create multiple items
|
||||
directus.items('articles').create([
|
||||
{
|
||||
title: 'My First Article'
|
||||
},
|
||||
{
|
||||
title: 'My Second Article'
|
||||
},
|
||||
]);
|
||||
```
|
||||
|
||||
#### Read
|
||||
|
||||
```js
|
||||
// Get all
|
||||
directus.items('articles').read();
|
||||
|
||||
// Get item by ID (w/ optional query)
|
||||
directus.items('articles').read(15);
|
||||
directus.items('articles').read(15, { fields: ['title'] });
|
||||
|
||||
// Get items by IDs (w/ optional query)
|
||||
directus.items('articles').read([15, 42]);
|
||||
directus.items('articles').read([15, 42], { fields: ['title' ]});
|
||||
|
||||
// Get items by search query params
|
||||
directus.items('articles').read({
|
||||
search: 'Directus',
|
||||
filter: {
|
||||
date_published: {
|
||||
_gte: '$NOW'
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
#### Update
|
||||
|
||||
```js
|
||||
// Update an item (w/ optional query)
|
||||
directus.items('articles').update(15, {
|
||||
title: 'An Updated title'
|
||||
});
|
||||
directus.items('articles').update(
|
||||
15,
|
||||
{ title: 'An Updated title' },
|
||||
{ fields: ['title'] }
|
||||
);
|
||||
|
||||
// Update multiple items (w/ optional query)
|
||||
directus.items('articles').update([15, 42], {
|
||||
title: 'An Updated title'
|
||||
});
|
||||
directus.items('articles').update(
|
||||
[15, 42],
|
||||
{ title: 'An Updated title' },
|
||||
{ fields: ['title'] }
|
||||
);
|
||||
|
||||
// Update by query
|
||||
directus.items('articles').update(
|
||||
{ title: 'An Updated title' },
|
||||
{ filter: { title: 'An Old Title' }}
|
||||
);
|
||||
```
|
||||
|
||||
#### Delete
|
||||
|
||||
```js
|
||||
// Delete an item
|
||||
directus.items('articles').delete(15);
|
||||
|
||||
// Delete multiple items
|
||||
directus.items('articles').delete([15, 42]);
|
||||
```
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import { Items } from './items';
|
||||
|
||||
export default class DirectusSDK {
|
||||
axios: AxiosInstance;
|
||||
|
||||
constructor(url: string) {
|
||||
this.axios = axios.create({
|
||||
baseURL: url,
|
||||
});
|
||||
}
|
||||
|
||||
get url() {
|
||||
return this.axios.defaults.baseURL!;
|
||||
}
|
||||
|
||||
set url(val: string) {
|
||||
this.axios.defaults.baseURL = val;
|
||||
}
|
||||
|
||||
items(collection: string) {
|
||||
return new Items(collection, this.axios);
|
||||
}
|
||||
}
|
||||
|
||||
55
packages/sdk-js/src/items.ts
Normal file
55
packages/sdk-js/src/items.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Query, Item } from './types';
|
||||
import { AxiosInstance } from 'axios';
|
||||
|
||||
export class Items {
|
||||
collection: string;
|
||||
axios: AxiosInstance;
|
||||
|
||||
constructor(collection: string, axios: AxiosInstance) {
|
||||
this.collection = collection;
|
||||
this.axios = axios;
|
||||
}
|
||||
|
||||
async read(query?: Query): Promise<Item[]>;
|
||||
async read(query: Query & { single: true }): Promise<Item>;
|
||||
async read(key: string | number, query?: Query): Promise<Item>;
|
||||
async read(keys: (string | number)[], query?: Query): Promise<Item[]>;
|
||||
async read(keys: (string | number)[], query: Query & { single: true }): Promise<Item>;
|
||||
async read(
|
||||
keysOrQuery?: string | number | (string | number)[] | Query,
|
||||
query?: Query & { single: boolean }
|
||||
): Promise<Item | Item[]> {
|
||||
let keys: string | number | (string | number)[] | null = null;
|
||||
|
||||
if (
|
||||
keysOrQuery &&
|
||||
(Array.isArray(keysOrQuery) ||
|
||||
typeof keysOrQuery === 'string' ||
|
||||
typeof keysOrQuery === 'number')
|
||||
) {
|
||||
keys = keysOrQuery;
|
||||
}
|
||||
|
||||
let params: Query = {};
|
||||
|
||||
if (query) {
|
||||
params = query;
|
||||
} else if (
|
||||
!query &&
|
||||
typeof keysOrQuery === 'object' &&
|
||||
Array.isArray(keysOrQuery) === false
|
||||
) {
|
||||
params = keysOrQuery as Query;
|
||||
}
|
||||
|
||||
let endpoint = `/items/`;
|
||||
|
||||
if (keys) {
|
||||
endpoint += keys;
|
||||
}
|
||||
|
||||
const result = await this.axios.get(endpoint, { params });
|
||||
|
||||
return result.data;
|
||||
}
|
||||
}
|
||||
41
packages/sdk-js/src/types.ts
Normal file
41
packages/sdk-js/src/types.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
export type Item = Record<string, any>;
|
||||
export type Payload = Record<string, any>;
|
||||
|
||||
export enum Meta {
|
||||
TOTAL_COUNT = 'total_count',
|
||||
FILTER_COUNT = 'filter_count',
|
||||
}
|
||||
|
||||
export type Query = {
|
||||
fields?: string | string[];
|
||||
sort?: string;
|
||||
filter?: Filter;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
page?: number;
|
||||
single?: boolean;
|
||||
meta?: Meta[];
|
||||
search?: string;
|
||||
export?: 'json' | 'csv';
|
||||
deep?: Record<string, Query>;
|
||||
};
|
||||
|
||||
export type Filter = {
|
||||
[keyOrOperator: string]: Filter | string | boolean | number | string[];
|
||||
};
|
||||
|
||||
export type FilterOperator =
|
||||
| '_eq'
|
||||
| '_neq'
|
||||
| '_contains'
|
||||
| '_ncontains'
|
||||
| '_in'
|
||||
| '_nin'
|
||||
| '_gt'
|
||||
| '_gte'
|
||||
| '_lt'
|
||||
| '_lte'
|
||||
| '_null'
|
||||
| '_nnull'
|
||||
| '_empty'
|
||||
| '_nempty';
|
||||
Reference in New Issue
Block a user