Skip to content

Commit db1d06c

Browse files
committed
fix(directus-sdk): restructure cache directories and update imports
- Moved `DirectusCacheInterceptor` and related files to a new `cache` directory. - Updated all relevant imports to reflect new file paths. - Modified `.barrelsby.json` to exclude `cache` folder from barrel generation. - Adjusted `tsconfig` and package.json exports to support new cache path. - Added support for dynamically importing `@directus/api/cache` in `DirectusCacheInterceptor`.
1 parent 60cb0c6 commit db1d06c

File tree

10 files changed

+105
-70
lines changed

10 files changed

+105
-70
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"directory": ["./src"],
3-
"exclude": ["**/bin", "**/__mock__", "**/__mocks__", "**/*.spec.ts"],
3+
"exclude": ["**/bin", "**/__mock__", "**/__mocks__", "**/*.spec.ts", "**/cache"],
44
"delete": true
55
}

packages/third-parties/directus-sdk/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"import": "./lib/esm/index.js",
1515
"default": "./lib/esm/index.js"
1616
},
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+
},
1723
"./attach-logger": {
1824
"tsed-source": "./src/bootstrap/attach-logger.ts",
1925
"types": "./lib/types/bootstrap/attach-logger.d.ts",
@@ -39,6 +45,7 @@
3945
"@directus/types": "13.4.0",
4046
"@tsed/barrels": "workspace:*",
4147
"@tsed/logger-connect": "8.0.4",
48+
"typescript": "5.9.3",
4249
"vitest": "3.2.4"
4350
},
4451
"peerDependencies": {

packages/third-parties/directus-sdk/readme.md

Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -163,59 +163,39 @@ import {inject} from "@tsed/di";
163163
const myService = inject(MyService);
164164
```
165165

166-
### Cache Decorator
166+
### Directus Context Service
167167

168-
Cache method results using Directus cache infrastructure:
168+
Access Directus context from anywhere in your code:
169169

170170
```ts
171-
import {Injectable} from "@tsed/di";
172-
import {Cache} from "@tsed/directus-sdk";
171+
import {injectable} from "@tsed/di";
172+
import {DirectusContextService} from "@tsed/directus-sdk";
173173

174-
@Injectable()
175-
export class ApiClient {
176-
@Cache({ttl: 900000})
177-
async search(query: string) {
178-
// Expensive operation
179-
return this.performSearch(query);
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...
180181
}
181182

182-
@Cache({
183-
ttl: 60000,
184-
keyGenerator: (userId: string) => `user:${userId}`
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");
188186
}
189187
}
190-
```
191-
192-
**Options:**
193188

194-
- `ttl` - Time to live in milliseconds (default: 900000ms = 15 minutes)
195-
- `keyGenerator` - Custom function to generate cache keys
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+
```
205191

206-
```ts
207-
import {injectable} from "@tsed/di";
208-
import {useDirectusCache} from "@tsed/directus-sdk";
192+
**Methods:**
209193

210-
export class JiraIssueClient {
211-
search(query: string) {
212-
return this.performSearch(query);
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
215197

216-
injectable(JiraIssueClient);
217-
useDirectusCache(JiraIssueClient, "search", {ttl: 900000});
218-
```
198+
---
219199

220200
### Repository Pattern
221201

@@ -292,39 +272,87 @@ export default defineEndpoint({
292272
});
293273
```
294274

295-
### Directus Context Service
275+
### Cache
296276

297-
Access Directus context from anywhere in your code:
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.
298280

299-
```ts
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:
302282

303-
export class MyService {
304-
directusContext = inject(DirectusContextService);
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+
}
305292

306-
async doSomething() {
307-
const context = this.directusContext.get();
308-
const schema = await context.getSchema();
309-
// Use schema...
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);
310317
}
311318

312-
async getUsersService() {
313-
// Convenience method to get ItemsService
314-
return this.directusContext.getItemsService("users");
319+
@Cache({
320+
ttl: 60000,
321+
keyGenerator: (userId: string) => `user:${userId}`
322+
})
323+
async getUserById(userId: string) {
324+
return this.fetchUser(userId);
315325
}
316326
}
317-
318-
injectable(MyService);
319327
```
320328
321-
**Methods:**
329+
**Options:**
322330
323-
- `set(context)` - Set the Directus context (internal use)
324-
- `get()` - Get the current Directus context
325-
- `getItemsService(collection, options?)` - Create an ItemsService for a collection
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
326335
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+
```
328356
329357
## 🔧 Advanced Usage
330358

packages/third-parties/directus-sdk/src/services/DirectusCacheInterceptor.ts renamed to packages/third-parties/directus-sdk/src/cache/DirectusCacheInterceptor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {getCache, getCacheValue, setCacheValue} from "@directus/api/cache";
21
import type {Type} from "@tsed/core";
32
import {classOf} from "@tsed/core/utils/classOf.js";
43
import {nameOf} from "@tsed/core/utils/nameOf.js";
@@ -111,6 +110,7 @@ export class DirectusCacheInterceptor implements InterceptorMethods {
111110
* - The system cache is used by default, which persists across requests
112111
*/
113112
async intercept(context: InterceptorContext<unknown, DirectusCacheOptions>) {
113+
const {getCache, getCacheValue, setCacheValue} = await import("@directus/api/cache");
114114
const {
115115
ttl = 900000, // 15 minutes default
116116
keyGenerator = defaultKeyGenerator,

packages/third-parties/directus-sdk/src/decorators/cache.spec.ts renamed to packages/third-parties/directus-sdk/src/cache/decorators/cache.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {Intercept} from "@tsed/di";
2-
import {DITest} from "@tsed/di";
1+
import {DITest, Intercept} from "@tsed/di";
32
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
43

5-
import {DirectusCacheInterceptor} from "../services/DirectusCacheInterceptor.js";
4+
import {DirectusCacheInterceptor} from "../DirectusCacheInterceptor.js";
65
import {Cache} from "./cache.js";
76

87
vi.mock("@tsed/di", async () => {

packages/third-parties/directus-sdk/src/decorators/cache.ts renamed to packages/third-parties/directus-sdk/src/cache/decorators/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Intercept} from "@tsed/di";
22

3-
import {DirectusCacheInterceptor, type DirectusCacheOptions} from "../services/DirectusCacheInterceptor.js";
3+
import {DirectusCacheInterceptor, type DirectusCacheOptions} from "../DirectusCacheInterceptor.js";
44

55
/**
66
* Decorator to cache method results using Directus cache system.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./decorators/cache.js";
2+
export * from "./DirectusCacheInterceptor.js";

packages/third-parties/directus-sdk/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
*/
44
export * from "./bootstrap/attach-logger.js";
55
export * from "./bootstrap/runtime.js";
6-
export * from "./decorators/cache.js";
76
export * from "./fn/defineEndpoint.js";
87
export * from "./fn/defineHook.js";
98
export * from "./fn/defineOperationApi.js";
109
export * from "./fn/wrapEndpoint.js";
1110
export * from "./fn/wrapOperation.js";
12-
export * from "./services/DirectusCacheInterceptor.js";
1311
export * from "./services/DirectusContextService.js";
1412
export * from "./services/DirectusItemsRepository.js";

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12340,6 +12340,7 @@ __metadata:
1234012340
"@tsed/logger-connect": "npm:8.0.4"
1234112341
semver: "npm:^7.6.3"
1234212342
tslib: "npm:2.8.1"
12343+
typescript: "npm:5.9.3"
1234312344
vitest: "npm:3.2.4"
1234412345
peerDependencies:
1234512346
"@directus/api": ">=31.0.0"

0 commit comments

Comments
 (0)