@tsed/directus-sdk 8.19.0 → 8.19.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.barrelsby.json +1 -1
- package/lib/esm/bootstrap/attach-logger.js +1 -0
- package/lib/esm/{services → cache}/DirectusCacheInterceptor.js +2 -2
- package/lib/esm/{decorators → cache/decorators}/cache.js +1 -1
- package/lib/esm/cache/index.js +2 -0
- package/lib/esm/index.js +0 -2
- package/lib/types/{decorators → cache/decorators}/cache.d.ts +1 -1
- package/lib/types/cache/index.d.ts +2 -0
- package/lib/types/index.d.ts +0 -2
- package/package.json +9 -2
- package/readme.md +90 -62
- /package/lib/types/{services → cache}/DirectusCacheInterceptor.d.ts +0 -0
package/.barrelsby.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { getCache, getCacheValue, setCacheValue } from "@directus/api/cache";
|
|
2
1
|
import { classOf } from "@tsed/core/utils/classOf.js";
|
|
3
2
|
import { nameOf } from "@tsed/core/utils/nameOf.js";
|
|
4
3
|
import { bindIntercept, injectable } from "@tsed/di";
|
|
5
|
-
import { snakeCase } from "
|
|
4
|
+
import { snakeCase } from "change-case";
|
|
6
5
|
/**
|
|
7
6
|
* Default cache key generator that serializes method arguments into a cache key.
|
|
8
7
|
*
|
|
@@ -93,6 +92,7 @@ export class DirectusCacheInterceptor {
|
|
|
93
92
|
* - The system cache is used by default, which persists across requests
|
|
94
93
|
*/
|
|
95
94
|
async intercept(context) {
|
|
95
|
+
const { getCache, getCacheValue, setCacheValue } = await import("@directus/api/cache");
|
|
96
96
|
const { ttl = 900000, // 15 minutes default
|
|
97
97
|
keyGenerator = defaultKeyGenerator, namespace = snakeCase(nameOf(context.target)) + ":" + String(context.propertyKey), useSystemCache = true } = context.options || {};
|
|
98
98
|
// Get appropriate cache instance
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Intercept } from "@tsed/di";
|
|
2
|
-
import { DirectusCacheInterceptor } from "../
|
|
2
|
+
import { DirectusCacheInterceptor } from "../DirectusCacheInterceptor.js";
|
|
3
3
|
/**
|
|
4
4
|
* Decorator to cache method results using Directus cache system.
|
|
5
5
|
*
|
package/lib/esm/index.js
CHANGED
|
@@ -3,12 +3,10 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export * from "./bootstrap/attach-logger.js";
|
|
5
5
|
export * from "./bootstrap/runtime.js";
|
|
6
|
-
export * from "./decorators/cache.js";
|
|
7
6
|
export * from "./fn/defineEndpoint.js";
|
|
8
7
|
export * from "./fn/defineHook.js";
|
|
9
8
|
export * from "./fn/defineOperationApi.js";
|
|
10
9
|
export * from "./fn/wrapEndpoint.js";
|
|
11
10
|
export * from "./fn/wrapOperation.js";
|
|
12
|
-
export * from "./services/DirectusCacheInterceptor.js";
|
|
13
11
|
export * from "./services/DirectusContextService.js";
|
|
14
12
|
export * from "./services/DirectusItemsRepository.js";
|
package/lib/types/index.d.ts
CHANGED
|
@@ -3,12 +3,10 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export * from "./bootstrap/attach-logger.js";
|
|
5
5
|
export * from "./bootstrap/runtime.js";
|
|
6
|
-
export * from "./decorators/cache.js";
|
|
7
6
|
export * from "./fn/defineEndpoint.js";
|
|
8
7
|
export * from "./fn/defineHook.js";
|
|
9
8
|
export * from "./fn/defineOperationApi.js";
|
|
10
9
|
export * from "./fn/wrapEndpoint.js";
|
|
11
10
|
export * from "./fn/wrapOperation.js";
|
|
12
|
-
export * from "./services/DirectusCacheInterceptor.js";
|
|
13
11
|
export * from "./services/DirectusContextService.js";
|
|
14
12
|
export * from "./services/DirectusItemsRepository.js";
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/directus-sdk",
|
|
3
3
|
"description": "A library to integrate Directus SDK with Ts.ED, facilitating the creation of extensions (endpoints, hooks, operations) and the use of Directus services.",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.19.
|
|
5
|
+
"version": "8.19.2",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -14,6 +14,12 @@
|
|
|
14
14
|
"import": "./lib/esm/index.js",
|
|
15
15
|
"default": "./lib/esm/index.js"
|
|
16
16
|
},
|
|
17
|
+
"./cache": {
|
|
18
|
+
"tsed-source": "./src/cache/index.ts",
|
|
19
|
+
"types": "./lib/types/cache/index.d.ts",
|
|
20
|
+
"import": "./lib/esm/cache/index.js",
|
|
21
|
+
"default": "./lib/esm/cache/index.js"
|
|
22
|
+
},
|
|
17
23
|
"./attach-logger": {
|
|
18
24
|
"tsed-source": "./src/bootstrap/attach-logger.ts",
|
|
19
25
|
"types": "./lib/types/bootstrap/attach-logger.d.ts",
|
|
@@ -37,8 +43,9 @@
|
|
|
37
43
|
"@directus/extensions-sdk": "13.1.1",
|
|
38
44
|
"@directus/sdk": "20.2.0",
|
|
39
45
|
"@directus/types": "13.4.0",
|
|
40
|
-
"@tsed/barrels": "8.19.
|
|
46
|
+
"@tsed/barrels": "8.19.2",
|
|
41
47
|
"@tsed/logger-connect": "8.0.4",
|
|
48
|
+
"typescript": "5.9.3",
|
|
42
49
|
"vitest": "3.2.4"
|
|
43
50
|
},
|
|
44
51
|
"peerDependencies": {
|
package/readme.md
CHANGED
|
@@ -163,59 +163,39 @@ import {inject} from "@tsed/di";
|
|
|
163
163
|
const myService = inject(MyService);
|
|
164
164
|
```
|
|
165
165
|
|
|
166
|
-
###
|
|
166
|
+
### Directus Context Service
|
|
167
167
|
|
|
168
|
-
|
|
168
|
+
Access Directus context from anywhere in your code:
|
|
169
169
|
|
|
170
170
|
```ts
|
|
171
|
-
import {
|
|
172
|
-
import {
|
|
171
|
+
import {injectable} from "@tsed/di";
|
|
172
|
+
import {DirectusContextService} from "@tsed/directus-sdk";
|
|
173
173
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
async
|
|
178
|
-
|
|
179
|
-
|
|
174
|
+
export class MyService {
|
|
175
|
+
directusContext = inject(DirectusContextService);
|
|
176
|
+
|
|
177
|
+
async doSomething() {
|
|
178
|
+
const context = this.directusContext.get();
|
|
179
|
+
const schema = await context.getSchema();
|
|
180
|
+
// Use schema...
|
|
180
181
|
}
|
|
181
182
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
})
|
|
186
|
-
async getUserById(userId: string) {
|
|
187
|
-
return this.fetchUser(userId);
|
|
183
|
+
async getUsersService() {
|
|
184
|
+
// Convenience method to get ItemsService
|
|
185
|
+
return this.directusContext.getItemsService("users");
|
|
188
186
|
}
|
|
189
187
|
}
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
**Options:**
|
|
193
188
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
- `namespace` - Custom namespace for cache keys
|
|
197
|
-
- `useSystemCache` - Use system cache (true) or regular cache (false). Default: true
|
|
198
|
-
|
|
199
|
-
> Important: to use decorator you have to configure your tsconfig with the appropriate options (
|
|
200
|
-
> `"experimentalDecorators": true`, `"emitDecoratorMetadata": true`)
|
|
201
|
-
|
|
202
|
-
### Programmatic Cache Binding
|
|
203
|
-
|
|
204
|
-
Apply cache programmatically using `useDirectusCache`:
|
|
189
|
+
injectable(MyService);
|
|
190
|
+
```
|
|
205
191
|
|
|
206
|
-
|
|
207
|
-
import {injectable} from "@tsed/di";
|
|
208
|
-
import {useDirectusCache} from "@tsed/directus-sdk";
|
|
192
|
+
**Methods:**
|
|
209
193
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
}
|
|
194
|
+
- `set(context)` - Set the Directus context (internal use)
|
|
195
|
+
- `get()` - Get the current Directus context
|
|
196
|
+
- `getItemsService(collection, options?)` - Create an ItemsService for a collection
|
|
215
197
|
|
|
216
|
-
|
|
217
|
-
useDirectusCache(JiraIssueClient, "search", {ttl: 900000});
|
|
218
|
-
```
|
|
198
|
+
---
|
|
219
199
|
|
|
220
200
|
### Repository Pattern
|
|
221
201
|
|
|
@@ -292,39 +272,87 @@ export default defineEndpoint({
|
|
|
292
272
|
});
|
|
293
273
|
```
|
|
294
274
|
|
|
295
|
-
###
|
|
275
|
+
### Cache
|
|
296
276
|
|
|
297
|
-
|
|
277
|
+
Because `@directus/api/cache` isn't declared as external dependency to build the extension you have to configure the
|
|
278
|
+
rollup bundler to
|
|
279
|
+
exclude it from the build.
|
|
298
280
|
|
|
299
|
-
|
|
300
|
-
import {injectable} from "@tsed/di";
|
|
301
|
-
import {DirectusContextService} from "@tsed/directus-sdk";
|
|
281
|
+
So for each extension you have to add the following to the `extension.config.js`, if your extension build fail:
|
|
302
282
|
|
|
303
|
-
|
|
304
|
-
|
|
283
|
+
```js
|
|
284
|
+
function externals() {
|
|
285
|
+
return {
|
|
286
|
+
name: "external-plus", // this name will show up in logs and errors
|
|
287
|
+
version: "1.0.0",
|
|
288
|
+
options(rawOptions) {
|
|
289
|
+
if (rawOptions.external?.includes("directus:api")) {
|
|
290
|
+
rawOptions.external.push("@directus/api/cache");
|
|
291
|
+
}
|
|
305
292
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
293
|
+
return rawOptions;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export default {
|
|
299
|
+
plugins: [externals()]
|
|
300
|
+
};
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
#### Cache Decorator
|
|
304
|
+
|
|
305
|
+
Cache method results using Directus cache infrastructure:
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
import {Injectable} from "@tsed/di";
|
|
309
|
+
import {Cache} from "@tsed/directus-sdk/cache";
|
|
310
|
+
|
|
311
|
+
@Injectable()
|
|
312
|
+
export class ApiClient {
|
|
313
|
+
@Cache({ttl: 900000})
|
|
314
|
+
async search(query: string) {
|
|
315
|
+
// Expensive operation
|
|
316
|
+
return this.performSearch(query);
|
|
310
317
|
}
|
|
311
318
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
319
|
+
@Cache({
|
|
320
|
+
ttl: 60000,
|
|
321
|
+
keyGenerator: (userId: string) => `user:${userId}`
|
|
322
|
+
})
|
|
323
|
+
async getUserById(userId: string) {
|
|
324
|
+
return this.fetchUser(userId);
|
|
315
325
|
}
|
|
316
326
|
}
|
|
317
|
-
|
|
318
|
-
injectable(MyService);
|
|
319
327
|
```
|
|
320
328
|
|
|
321
|
-
**
|
|
329
|
+
**Options:**
|
|
322
330
|
|
|
323
|
-
- `
|
|
324
|
-
- `
|
|
325
|
-
- `
|
|
331
|
+
- `ttl` - Time to live in milliseconds (default: 900000ms = 15 minutes)
|
|
332
|
+
- `keyGenerator` - Custom function to generate cache keys
|
|
333
|
+
- `namespace` - Custom namespace for cache keys
|
|
334
|
+
- `useSystemCache` - Use system cache (true) or regular cache (false). Default: true
|
|
326
335
|
|
|
327
|
-
|
|
336
|
+
> Important: to use decorator you have to configure your tsconfig with the appropriate options (
|
|
337
|
+
> `"experimentalDecorators": true`, `"emitDecoratorMetadata": true`)
|
|
338
|
+
|
|
339
|
+
#### Programmatic Cache Binding
|
|
340
|
+
|
|
341
|
+
Apply cache programmatically using `useDirectusCache`:
|
|
342
|
+
|
|
343
|
+
```ts
|
|
344
|
+
import {injectable} from "@tsed/di";
|
|
345
|
+
import {useDirectusCache} from "@tsed/directus-sdk/cache";
|
|
346
|
+
|
|
347
|
+
export class JiraIssueClient {
|
|
348
|
+
search(query: string) {
|
|
349
|
+
return this.performSearch(query);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
injectable(JiraIssueClient);
|
|
354
|
+
useDirectusCache(JiraIssueClient, "search", {ttl: 900000});
|
|
355
|
+
```
|
|
328
356
|
|
|
329
357
|
## 🔧 Advanced Usage
|
|
330
358
|
|
|
File without changes
|