-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdirectus.ts
More file actions
54 lines (49 loc) · 1.97 KB
/
Copy pathdirectus.ts
File metadata and controls
54 lines (49 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Schema, components } from "./types/schema";
import { createDirectus, rest, staticToken } from "@directus/sdk";
// URL to access Directus from server side. Used to query the Directus instance (also see `directus()` below).
export const INTERNAL_DIRECTUS_URL = process.env.DIRECTUS_URL as string;
// URL to access Directus from client side. Used for images.
export const PUBLIC_DIRECTUS_URL = process.env
.NEXT_PUBLIC_DIRECTUS_URL as string;
/**
* Creates a handle to use Directus' API. See the [official documentation](https://docs.directus.io/guides/sdk/getting-started.html).
*
* May only be called server-side (e.g. in [`getServerSideProps()`](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props)).
*
* @returns a handle to Directus' API.
*/
export function directus() {
const directus = createDirectus<Schema>(INTERNAL_DIRECTUS_URL).with(rest());
return (process.env.DIRECTUS_TOKEN || "") !== ""
? directus.with(staticToken(process.env.DIRECTUS_TOKEN as string))
: directus;
}
/**
* Computes the full URL to access an image returned by the Directus instance,
* or `undefined` if no image is given.
* @param image the image object returned by directus
* @returns the full URL of the image, if any
*/
export function getDirectusImageUrl(
image: string | components["schemas"]["Files"] | null | undefined
): string | undefined {
return image
? `${PUBLIC_DIRECTUS_URL}/assets/${
typeof image === "string" ? image : image.filename_disk
}`
: undefined;
}
/**
* Computes the full URL to access an image in a format appropriate for an OG property.
* @param image the image object returned by directus
* @returns the full URL of the image, if any
*/
export function getDirectusOgImageUrl(
image: string | components["schemas"]["Files"] | null | undefined
): string | undefined {
return image
? `/api/og-image?id=${
typeof image === "string" ? image : image.filename_disk
}`
: undefined;
}